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; 1036061e38SJoe Perchesuse File::Basename; 1136061e38SJoe Perchesuse Cwd 'abs_path'; 120a920b5bSAndy Whitcroft 130a920b5bSAndy Whitcroftmy $P = $0; 1436061e38SJoe Perchesmy $D = dirname(abs_path($P)); 150a920b5bSAndy Whitcroft 16000d1cc1SJoe Perchesmy $V = '0.32'; 170a920b5bSAndy Whitcroft 180a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 190a920b5bSAndy Whitcroft 200a920b5bSAndy Whitcroftmy $quiet = 0; 210a920b5bSAndy Whitcroftmy $tree = 1; 220a920b5bSAndy Whitcroftmy $chk_signoff = 1; 230a920b5bSAndy Whitcroftmy $chk_patch = 1; 24773647a0SAndy Whitcroftmy $tst_only; 256c72ffaaSAndy Whitcroftmy $emacs = 0; 268905a67cSAndy Whitcroftmy $terse = 0; 276c72ffaaSAndy Whitcroftmy $file = 0; 286c72ffaaSAndy Whitcroftmy $check = 0; 292ac73b4fSJoe Perchesmy $check_orig = 0; 308905a67cSAndy Whitcroftmy $summary = 1; 318905a67cSAndy Whitcroftmy $mailback = 0; 3213214adfSAndy Whitcroftmy $summary_file = 0; 33000d1cc1SJoe Perchesmy $show_types = 0; 343705ce5bSJoe Perchesmy $fix = 0; 359624b8d6SJoe Perchesmy $fix_inplace = 0; 366c72ffaaSAndy Whitcroftmy $root; 37c2fdda0dSAndy Whitcroftmy %debug; 383445686aSJoe Perchesmy %camelcase = (); 3991bfe484SJoe Perchesmy %use_type = (); 4091bfe484SJoe Perchesmy @use = (); 4191bfe484SJoe Perchesmy %ignore_type = (); 42000d1cc1SJoe Perchesmy @ignore = (); 4377f5b10aSHannes Edermy $help = 0; 44000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 456cd7f386SJoe Perchesmy $max_line_length = 80; 46d62a201fSDave Hansenmy $ignore_perl_version = 0; 47d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 4856193274SVadim Bendeburymy $min_conf_desc_length = 4; 4966b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 5077f5b10aSHannes Eder 5177f5b10aSHannes Edersub help { 5277f5b10aSHannes Eder my ($exitcode) = @_; 5377f5b10aSHannes Eder 5477f5b10aSHannes Eder print << "EOM"; 5577f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 5677f5b10aSHannes EderVersion: $V 5777f5b10aSHannes Eder 5877f5b10aSHannes EderOptions: 5977f5b10aSHannes Eder -q, --quiet quiet 6077f5b10aSHannes Eder --no-tree run without a kernel tree 6177f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 6277f5b10aSHannes Eder --patch treat FILE as patchfile (default) 6377f5b10aSHannes Eder --emacs emacs compile window format 6477f5b10aSHannes Eder --terse one line per report 6577f5b10aSHannes Eder -f, --file treat FILE as regular source file 6677f5b10aSHannes Eder --subjective, --strict enable more subjective tests 6791bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 68000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 696cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 7056193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 71000d1cc1SJoe Perches --show-types show the message "types" in the output 7277f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 7377f5b10aSHannes Eder --no-summary suppress the per-file summary 7477f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 7577f5b10aSHannes Eder --summary-file include the filename in summary 7677f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 7777f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 7877f5b10aSHannes Eder is all off) 7977f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 8077f5b10aSHannes Eder literally 813705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 823705ce5bSJoe Perches If correctable single-line errors exist, create 833705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 843705ce5bSJoe Perches with potential errors corrected to the preferred 853705ce5bSJoe Perches checkpatch style 869624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 879624b8d6SJoe Perches Is the same as --fix, but overwrites the input 889624b8d6SJoe Perches file. It's your fault if there's no backup or git 89d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 90d62a201fSDave Hansen runtime errors. 9177f5b10aSHannes Eder -h, --help, --version display this help and exit 9277f5b10aSHannes Eder 9377f5b10aSHannes EderWhen FILE is - read standard input. 9477f5b10aSHannes EderEOM 9577f5b10aSHannes Eder 9677f5b10aSHannes Eder exit($exitcode); 9777f5b10aSHannes Eder} 9877f5b10aSHannes Eder 99000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 100000d1cc1SJoe Perchesif (-f $conf) { 101000d1cc1SJoe Perches my @conf_args; 102000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 103000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 104000d1cc1SJoe Perches 105000d1cc1SJoe Perches while (<$conffile>) { 106000d1cc1SJoe Perches my $line = $_; 107000d1cc1SJoe Perches 108000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 109000d1cc1SJoe Perches $line =~ s/^\s*//g; 110000d1cc1SJoe Perches $line =~ s/\s+/ /g; 111000d1cc1SJoe Perches 112000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 113000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 114000d1cc1SJoe Perches 115000d1cc1SJoe Perches my @words = split(" ", $line); 116000d1cc1SJoe Perches foreach my $word (@words) { 117000d1cc1SJoe Perches last if ($word =~ m/^#/); 118000d1cc1SJoe Perches push (@conf_args, $word); 119000d1cc1SJoe Perches } 120000d1cc1SJoe Perches } 121000d1cc1SJoe Perches close($conffile); 122000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 123000d1cc1SJoe Perches} 124000d1cc1SJoe Perches 1250a920b5bSAndy WhitcroftGetOptions( 1266c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1270a920b5bSAndy Whitcroft 'tree!' => \$tree, 1280a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1290a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 1306c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 1318905a67cSAndy Whitcroft 'terse!' => \$terse, 13277f5b10aSHannes Eder 'f|file!' => \$file, 1336c72ffaaSAndy Whitcroft 'subjective!' => \$check, 1346c72ffaaSAndy Whitcroft 'strict!' => \$check, 135000d1cc1SJoe Perches 'ignore=s' => \@ignore, 13691bfe484SJoe Perches 'types=s' => \@use, 137000d1cc1SJoe Perches 'show-types!' => \$show_types, 1386cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 13956193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 1406c72ffaaSAndy Whitcroft 'root=s' => \$root, 1418905a67cSAndy Whitcroft 'summary!' => \$summary, 1428905a67cSAndy Whitcroft 'mailback!' => \$mailback, 14313214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 1443705ce5bSJoe Perches 'fix!' => \$fix, 1459624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 146d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 147c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 148773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 14977f5b10aSHannes Eder 'h|help' => \$help, 15077f5b10aSHannes Eder 'version' => \$help 15177f5b10aSHannes Eder) or help(1); 15277f5b10aSHannes Eder 15377f5b10aSHannes Ederhelp(0) if ($help); 1540a920b5bSAndy Whitcroft 1559624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 1562ac73b4fSJoe Perches$check_orig = $check; 1579624b8d6SJoe Perches 1580a920b5bSAndy Whitcroftmy $exit = 0; 1590a920b5bSAndy Whitcroft 160d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 161d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 162d62a201fSDave Hansen if (!$ignore_perl_version) { 163d62a201fSDave Hansen exit(1); 164d62a201fSDave Hansen } 165d62a201fSDave Hansen} 166d62a201fSDave Hansen 1670a920b5bSAndy Whitcroftif ($#ARGV < 0) { 16877f5b10aSHannes Eder print "$P: no input files\n"; 1690a920b5bSAndy Whitcroft exit(1); 1700a920b5bSAndy Whitcroft} 1710a920b5bSAndy Whitcroft 17291bfe484SJoe Perchessub hash_save_array_words { 17391bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 17491bfe484SJoe Perches 17591bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 17691bfe484SJoe Perches foreach my $word (@array) { 177000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 178000d1cc1SJoe Perches $word =~ s/^\s*//g; 179000d1cc1SJoe Perches $word =~ s/\s+/ /g; 180000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 181000d1cc1SJoe Perches 182000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 183000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 184000d1cc1SJoe Perches 18591bfe484SJoe Perches $hashRef->{$word}++; 186000d1cc1SJoe Perches } 18791bfe484SJoe Perches} 18891bfe484SJoe Perches 18991bfe484SJoe Perchessub hash_show_words { 19091bfe484SJoe Perches my ($hashRef, $prefix) = @_; 19191bfe484SJoe Perches 19258cb3cf6SJoe Perches if ($quiet == 0 && keys %$hashRef) { 19391bfe484SJoe Perches print "NOTE: $prefix message types:"; 19458cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 19591bfe484SJoe Perches print " $word"; 19691bfe484SJoe Perches } 19791bfe484SJoe Perches print "\n\n"; 19891bfe484SJoe Perches } 19991bfe484SJoe Perches} 20091bfe484SJoe Perches 20191bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 20291bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 203000d1cc1SJoe Perches 204c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 205c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 2067429c690SAndy Whitcroftmy $dbg_type = 0; 207a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 208c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 20921caa13cSAndy Whitcroft ## no critic 21021caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 21121caa13cSAndy Whitcroft die "$@" if ($@); 212c2fdda0dSAndy Whitcroft} 213c2fdda0dSAndy Whitcroft 214d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 215d2c0a235SAndy Whitcroft 2168905a67cSAndy Whitcroftif ($terse) { 2178905a67cSAndy Whitcroft $emacs = 1; 2188905a67cSAndy Whitcroft $quiet++; 2198905a67cSAndy Whitcroft} 2208905a67cSAndy Whitcroft 2216c72ffaaSAndy Whitcroftif ($tree) { 2226c72ffaaSAndy Whitcroft if (defined $root) { 2236c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 2246c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 2256c72ffaaSAndy Whitcroft } 2266c72ffaaSAndy Whitcroft } else { 2276c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 2286c72ffaaSAndy Whitcroft $root = '.'; 2296c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 2306c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 2316c72ffaaSAndy Whitcroft $root = $1; 2326c72ffaaSAndy Whitcroft } 2336c72ffaaSAndy Whitcroft } 2346c72ffaaSAndy Whitcroft 2356c72ffaaSAndy Whitcroft if (!defined $root) { 2360a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 2370a920b5bSAndy Whitcroft exit(2); 2380a920b5bSAndy Whitcroft } 2396c72ffaaSAndy Whitcroft} 2406c72ffaaSAndy Whitcroft 2416c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 2426c72ffaaSAndy Whitcroft 2432ceb532bSAndy Whitcroftour $Ident = qr{ 2442ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 2452ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 2462ceb532bSAndy Whitcroft }x; 2476c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 2486c72ffaaSAndy Whitcroftour $Sparse = qr{ 2496c72ffaaSAndy Whitcroft __user| 2506c72ffaaSAndy Whitcroft __kernel| 2516c72ffaaSAndy Whitcroft __force| 2526c72ffaaSAndy Whitcroft __iomem| 2536c72ffaaSAndy Whitcroft __must_check| 2546c72ffaaSAndy Whitcroft __init_refok| 255417495edSAndy Whitcroft __kprobes| 256165e72a6SSven Eckelmann __ref| 257165e72a6SSven Eckelmann __rcu 2586c72ffaaSAndy Whitcroft }x; 259e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 260e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 261e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 262e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 263e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 2648716de38SJoe Perches 26552131292SWolfram Sang# Notes to $Attribute: 26652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 2676c72ffaaSAndy Whitcroftour $Attribute = qr{ 2686c72ffaaSAndy Whitcroft const| 26903f1df7dSJoe Perches __percpu| 27003f1df7dSJoe Perches __nocast| 27103f1df7dSJoe Perches __safe| 27203f1df7dSJoe Perches __bitwise__| 27303f1df7dSJoe Perches __packed__| 27403f1df7dSJoe Perches __packed2__| 27503f1df7dSJoe Perches __naked| 27603f1df7dSJoe Perches __maybe_unused| 27703f1df7dSJoe Perches __always_unused| 27803f1df7dSJoe Perches __noreturn| 27903f1df7dSJoe Perches __used| 28003f1df7dSJoe Perches __cold| 281*e23ef1f3SJoe Perches __pure| 28203f1df7dSJoe Perches __noclone| 28303f1df7dSJoe Perches __deprecated| 2846c72ffaaSAndy Whitcroft __read_mostly| 2856c72ffaaSAndy Whitcroft __kprobes| 2868716de38SJoe Perches $InitAttribute| 28724e1d81aSAndy Whitcroft ____cacheline_aligned| 28824e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 2895fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 2905fe3af11SAndy Whitcroft __weak 2916c72ffaaSAndy Whitcroft }x; 292c45dcabdSAndy Whitcroftour $Modifier; 29391cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 2946c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 2956c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 2966c72ffaaSAndy Whitcroft 29795e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 29895e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 29995e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 30095e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 3012435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 302c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 303326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 304326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 305326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 30674349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 3072435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 308326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 309447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 31023f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 3116c72ffaaSAndy Whitcroftour $Operators = qr{ 3126c72ffaaSAndy Whitcroft <=|>=|==|!=| 3136c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 31423f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 3156c72ffaaSAndy Whitcroft }x; 3166c72ffaaSAndy Whitcroft 31791cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 31891cb5195SJoe Perches 3198905a67cSAndy Whitcroftour $NonptrType; 3201813087dSJoe Perchesour $NonptrTypeMisordered; 3218716de38SJoe Perchesour $NonptrTypeWithAttr; 3228905a67cSAndy Whitcroftour $Type; 3231813087dSJoe Perchesour $TypeMisordered; 3248905a67cSAndy Whitcroftour $Declare; 3251813087dSJoe Perchesour $DeclareMisordered; 3268905a67cSAndy Whitcroft 32715662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 32815662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 329171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 330171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 331171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 332171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 333171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 334171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 335171ae1a4SAndy Whitcroft}x; 336171ae1a4SAndy Whitcroft 33715662b3eSJoe Perchesour $UTF8 = qr{ 33815662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 33915662b3eSJoe Perches | $NON_ASCII_UTF8 34015662b3eSJoe Perches}x; 34115662b3eSJoe Perches 3428ed22cadSAndy Whitcroftour $typeTypedefs = qr{(?x: 343fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 3448ed22cadSAndy Whitcroft atomic_t 3458ed22cadSAndy Whitcroft)}; 3468ed22cadSAndy Whitcroft 347691e669bSJoe Perchesour $logFunctions = qr{(?x: 3486e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 3497d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 3506e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 351b0531722SJoe Perches panic| 35206668727SJoe Perches MODULE_[A-Z_]+| 35306668727SJoe Perches seq_vprintf|seq_printf|seq_puts 354691e669bSJoe Perches)}; 355691e669bSJoe Perches 35620112475SJoe Perchesour $signature_tags = qr{(?xi: 35720112475SJoe Perches Signed-off-by:| 35820112475SJoe Perches Acked-by:| 35920112475SJoe Perches Tested-by:| 36020112475SJoe Perches Reviewed-by:| 36120112475SJoe Perches Reported-by:| 3628543ae12SMugunthan V N Suggested-by:| 36320112475SJoe Perches To:| 36420112475SJoe Perches Cc: 36520112475SJoe Perches)}; 36620112475SJoe Perches 3671813087dSJoe Perchesour @typeListMisordered = ( 3681813087dSJoe Perches qr{char\s+(?:un)?signed}, 3691813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 3701813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 3711813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 3721813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 3731813087dSJoe Perches qr{short\s+(?:un)?signed}, 3741813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 3751813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 3761813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 3771813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 3781813087dSJoe Perches qr{int\s+(?:un)?signed}, 3791813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 3801813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 3811813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 3821813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 3831813087dSJoe Perches qr{long\s+(?:un)?signed}, 3841813087dSJoe Perches); 3851813087dSJoe Perches 3868905a67cSAndy Whitcroftour @typeList = ( 3878905a67cSAndy Whitcroft qr{void}, 3880c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 3890c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 3900c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 3910c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 3920c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 3930c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 3940c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 3950c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 3960c773d9dSJoe Perches qr{(?:un)?signed}, 3978905a67cSAndy Whitcroft qr{float}, 3988905a67cSAndy Whitcroft qr{double}, 3998905a67cSAndy Whitcroft qr{bool}, 4008905a67cSAndy Whitcroft qr{struct\s+$Ident}, 4018905a67cSAndy Whitcroft qr{union\s+$Ident}, 4028905a67cSAndy Whitcroft qr{enum\s+$Ident}, 4038905a67cSAndy Whitcroft qr{${Ident}_t}, 4048905a67cSAndy Whitcroft qr{${Ident}_handler}, 4058905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 4061813087dSJoe Perches @typeListMisordered, 4078905a67cSAndy Whitcroft); 4088716de38SJoe Perchesour @typeListWithAttr = ( 4098716de38SJoe Perches @typeList, 4108716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 4118716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 4128716de38SJoe Perches); 4138716de38SJoe Perches 414c45dcabdSAndy Whitcroftour @modifierList = ( 415c45dcabdSAndy Whitcroft qr{fastcall}, 416c45dcabdSAndy Whitcroft); 4178905a67cSAndy Whitcroft 4182435880fSJoe Perchesour @mode_permission_funcs = ( 4192435880fSJoe Perches ["module_param", 3], 4202435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 4212435880fSJoe Perches ["module_param_array_named", 5], 4222435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 4232435880fSJoe Perches ["proc_create(?:_data|)", 2], 4242435880fSJoe Perches ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], 4252435880fSJoe Perches); 4262435880fSJoe Perches 427515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 428515a235eSJoe Perchesour $mode_perms_search = ""; 429515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 430515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 431515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 432515a235eSJoe Perches} 433515a235eSJoe Perches 4347840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 4357840a94cSWolfram Sang irq| 436cdcee686SSergey Ryazanov memory| 437cdcee686SSergey Ryazanov time| 438cdcee686SSergey Ryazanov reboot 4397840a94cSWolfram Sang)}; 4407840a94cSWolfram Sang# memory.h: ARM has a custom one 4417840a94cSWolfram Sang 44266b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 44366b47b4aSKees Cookmy $misspellings; 44466b47b4aSKees Cookmy %spelling_fix; 44536061e38SJoe Perches 44636061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 44736061e38SJoe Perches my @spelling_list; 44866b47b4aSKees Cook while (<$spelling>) { 44966b47b4aSKees Cook my $line = $_; 45066b47b4aSKees Cook 45166b47b4aSKees Cook $line =~ s/\s*\n?$//g; 45266b47b4aSKees Cook $line =~ s/^\s*//g; 45366b47b4aSKees Cook 45466b47b4aSKees Cook next if ($line =~ m/^\s*#/); 45566b47b4aSKees Cook next if ($line =~ m/^\s*$/); 45666b47b4aSKees Cook 45766b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 45866b47b4aSKees Cook 45966b47b4aSKees Cook push(@spelling_list, $suspect); 46066b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 46166b47b4aSKees Cook } 46266b47b4aSKees Cook close($spelling); 46366b47b4aSKees Cook $misspellings = join("|", @spelling_list); 46436061e38SJoe Perches} else { 46536061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 46636061e38SJoe Perches} 46766b47b4aSKees Cook 4688905a67cSAndy Whitcroftsub build_types { 469d2172eb5SAndy Whitcroft my $mods = "(?x: \n" . join("|\n ", @modifierList) . "\n)"; 470d2172eb5SAndy Whitcroft my $all = "(?x: \n" . join("|\n ", @typeList) . "\n)"; 4711813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 4728716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 473c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 4748905a67cSAndy Whitcroft $NonptrType = qr{ 475d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 476cf655043SAndy Whitcroft (?: 4776b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 4788ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 479c45dcabdSAndy Whitcroft (?:${all}\b) 480cf655043SAndy Whitcroft ) 481c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 4828905a67cSAndy Whitcroft }x; 4831813087dSJoe Perches $NonptrTypeMisordered = qr{ 4841813087dSJoe Perches (?:$Modifier\s+|const\s+)* 4851813087dSJoe Perches (?: 4861813087dSJoe Perches (?:${Misordered}\b) 4871813087dSJoe Perches ) 4881813087dSJoe Perches (?:\s+$Modifier|\s+const)* 4891813087dSJoe Perches }x; 4908716de38SJoe Perches $NonptrTypeWithAttr = qr{ 4918716de38SJoe Perches (?:$Modifier\s+|const\s+)* 4928716de38SJoe Perches (?: 4938716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 4948716de38SJoe Perches (?:$typeTypedefs\b)| 4958716de38SJoe Perches (?:${allWithAttr}\b) 4968716de38SJoe Perches ) 4978716de38SJoe Perches (?:\s+$Modifier|\s+const)* 4988716de38SJoe Perches }x; 4998905a67cSAndy Whitcroft $Type = qr{ 500c45dcabdSAndy Whitcroft $NonptrType 5011574a29fSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 502c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 5038905a67cSAndy Whitcroft }x; 5041813087dSJoe Perches $TypeMisordered = qr{ 5051813087dSJoe Perches $NonptrTypeMisordered 5061813087dSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 5071813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 5081813087dSJoe Perches }x; 50991cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 5101813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 5118905a67cSAndy Whitcroft} 5128905a67cSAndy Whitcroftbuild_types(); 5136c72ffaaSAndy Whitcroft 5147d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 515d1fe9c09SJoe Perches 516d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 517d1fe9c09SJoe Perches# requires at least perl version v5.10.0 518d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 519d1fe9c09SJoe Perches 520d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 5212435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 522c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 5237d2367afSJoe Perches 524f8422308SJoe Perchesour $declaration_macros = qr{(?x: 525f8422308SJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(| 526f8422308SJoe Perches (?:$Storage\s+)?LIST_HEAD\s*\(| 527f8422308SJoe Perches (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\( 528f8422308SJoe Perches)}; 529f8422308SJoe Perches 5307d2367afSJoe Perchessub deparenthesize { 5317d2367afSJoe Perches my ($string) = @_; 5327d2367afSJoe Perches return "" if (!defined($string)); 5335b9553abSJoe Perches 5345b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 5355b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 5365b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 5375b9553abSJoe Perches } 5385b9553abSJoe Perches 5397d2367afSJoe Perches $string =~ s@\s+@ @g; 5405b9553abSJoe Perches 5417d2367afSJoe Perches return $string; 5427d2367afSJoe Perches} 5437d2367afSJoe Perches 5443445686aSJoe Perchessub seed_camelcase_file { 5453445686aSJoe Perches my ($file) = @_; 5463445686aSJoe Perches 5473445686aSJoe Perches return if (!(-f $file)); 5483445686aSJoe Perches 5493445686aSJoe Perches local $/; 5503445686aSJoe Perches 5513445686aSJoe Perches open(my $include_file, '<', "$file") 5523445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 5533445686aSJoe Perches my $text = <$include_file>; 5543445686aSJoe Perches close($include_file); 5553445686aSJoe Perches 5563445686aSJoe Perches my @lines = split('\n', $text); 5573445686aSJoe Perches 5583445686aSJoe Perches foreach my $line (@lines) { 5593445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 5603445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 5613445686aSJoe Perches $camelcase{$1} = 1; 56211ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 56311ea516aSJoe Perches $camelcase{$1} = 1; 56411ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 5653445686aSJoe Perches $camelcase{$1} = 1; 5663445686aSJoe Perches } 5673445686aSJoe Perches } 5683445686aSJoe Perches} 5693445686aSJoe Perches 5703445686aSJoe Perchesmy $camelcase_seeded = 0; 5713445686aSJoe Perchessub seed_camelcase_includes { 5723445686aSJoe Perches return if ($camelcase_seeded); 5733445686aSJoe Perches 5743445686aSJoe Perches my $files; 575c707a81dSJoe Perches my $camelcase_cache = ""; 576c707a81dSJoe Perches my @include_files = (); 577c707a81dSJoe Perches 578c707a81dSJoe Perches $camelcase_seeded = 1; 579351b2a1fSJoe Perches 5803645e328SRichard Genoud if (-e ".git") { 581351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 582351b2a1fSJoe Perches chomp $git_last_include_commit; 583c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 584c707a81dSJoe Perches } else { 585c707a81dSJoe Perches my $last_mod_date = 0; 586c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 587c707a81dSJoe Perches @include_files = split('\n', $files); 588c707a81dSJoe Perches foreach my $file (@include_files) { 589c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 590c707a81dSJoe Perches localtime((stat $file)[9])); 591c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 592c707a81dSJoe Perches } 593c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 594c707a81dSJoe Perches } 595c707a81dSJoe Perches 596c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 597c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 598c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 599351b2a1fSJoe Perches while (<$camelcase_file>) { 600351b2a1fSJoe Perches chomp; 601351b2a1fSJoe Perches $camelcase{$_} = 1; 602351b2a1fSJoe Perches } 603351b2a1fSJoe Perches close($camelcase_file); 604351b2a1fSJoe Perches 605351b2a1fSJoe Perches return; 606351b2a1fSJoe Perches } 607c707a81dSJoe Perches 6083645e328SRichard Genoud if (-e ".git") { 609c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 610c707a81dSJoe Perches @include_files = split('\n', $files); 6113445686aSJoe Perches } 612c707a81dSJoe Perches 6133445686aSJoe Perches foreach my $file (@include_files) { 6143445686aSJoe Perches seed_camelcase_file($file); 6153445686aSJoe Perches } 616351b2a1fSJoe Perches 617c707a81dSJoe Perches if ($camelcase_cache ne "") { 618351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 619c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 620c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 621351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 622351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 623351b2a1fSJoe Perches } 624351b2a1fSJoe Perches close($camelcase_file); 625351b2a1fSJoe Perches } 6263445686aSJoe Perches} 6273445686aSJoe Perches 628d311cd44SJoe Perchessub git_commit_info { 629d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 630d311cd44SJoe Perches 631d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 632d311cd44SJoe Perches 633d311cd44SJoe Perches my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; 634d311cd44SJoe Perches $output =~ s/^\s*//gm; 635d311cd44SJoe Perches my @lines = split("\n", $output); 636d311cd44SJoe Perches 637d311cd44SJoe Perches if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { 638d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 639d311cd44SJoe Perches# all matching commit ids, but it's very slow... 640d311cd44SJoe Perches# 641d311cd44SJoe Perches# echo "checking commits $1..." 642d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 643d311cd44SJoe Perches# while read line ; do 644d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 645d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 646d311cd44SJoe Perches# done 647d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 648d311cd44SJoe Perches } else { 649d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 650d311cd44SJoe Perches $desc = substr($lines[0], 41); 651d311cd44SJoe Perches } 652d311cd44SJoe Perches 653d311cd44SJoe Perches return ($id, $desc); 654d311cd44SJoe Perches} 655d311cd44SJoe Perches 6566c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 6570a920b5bSAndy Whitcroft 65800df344fSAndy Whitcroftmy @rawlines = (); 659c2fdda0dSAndy Whitcroftmy @lines = (); 6603705ce5bSJoe Perchesmy @fixed = (); 661d752fcc8SJoe Perchesmy @fixed_inserted = (); 662d752fcc8SJoe Perchesmy @fixed_deleted = (); 663194f66fcSJoe Perchesmy $fixlinenr = -1; 664194f66fcSJoe Perches 665c2fdda0dSAndy Whitcroftmy $vname; 6666c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 66721caa13cSAndy Whitcroft my $FILE; 6686c72ffaaSAndy Whitcroft if ($file) { 66921caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 6706c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 67121caa13cSAndy Whitcroft } elsif ($filename eq '-') { 67221caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 6736c72ffaaSAndy Whitcroft } else { 67421caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 6756c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 6766c72ffaaSAndy Whitcroft } 677c2fdda0dSAndy Whitcroft if ($filename eq '-') { 678c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 679c2fdda0dSAndy Whitcroft } else { 680c2fdda0dSAndy Whitcroft $vname = $filename; 681c2fdda0dSAndy Whitcroft } 68221caa13cSAndy Whitcroft while (<$FILE>) { 6830a920b5bSAndy Whitcroft chomp; 68400df344fSAndy Whitcroft push(@rawlines, $_); 6856c72ffaaSAndy Whitcroft } 68621caa13cSAndy Whitcroft close($FILE); 687c2fdda0dSAndy Whitcroft if (!process($filename)) { 6880a920b5bSAndy Whitcroft $exit = 1; 6890a920b5bSAndy Whitcroft } 69000df344fSAndy Whitcroft @rawlines = (); 69113214adfSAndy Whitcroft @lines = (); 6923705ce5bSJoe Perches @fixed = (); 693d752fcc8SJoe Perches @fixed_inserted = (); 694d752fcc8SJoe Perches @fixed_deleted = (); 695194f66fcSJoe Perches $fixlinenr = -1; 6960a920b5bSAndy Whitcroft} 6970a920b5bSAndy Whitcroft 6980a920b5bSAndy Whitcroftexit($exit); 6990a920b5bSAndy Whitcroft 7000a920b5bSAndy Whitcroftsub top_of_kernel_tree { 7016c72ffaaSAndy Whitcroft my ($root) = @_; 7026c72ffaaSAndy Whitcroft 7036c72ffaaSAndy Whitcroft my @tree_check = ( 7046c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 7056c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 7066c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 7076c72ffaaSAndy Whitcroft ); 7086c72ffaaSAndy Whitcroft 7096c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 7106c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 7110a920b5bSAndy Whitcroft return 0; 7120a920b5bSAndy Whitcroft } 7136c72ffaaSAndy Whitcroft } 7146c72ffaaSAndy Whitcroft return 1; 7156c72ffaaSAndy Whitcroft} 7160a920b5bSAndy Whitcroft 71720112475SJoe Perchessub parse_email { 71820112475SJoe Perches my ($formatted_email) = @_; 71920112475SJoe Perches 72020112475SJoe Perches my $name = ""; 72120112475SJoe Perches my $address = ""; 72220112475SJoe Perches my $comment = ""; 72320112475SJoe Perches 72420112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 72520112475SJoe Perches $name = $1; 72620112475SJoe Perches $address = $2; 72720112475SJoe Perches $comment = $3 if defined $3; 72820112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 72920112475SJoe Perches $address = $1; 73020112475SJoe Perches $comment = $2 if defined $2; 73120112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 73220112475SJoe Perches $address = $1; 73320112475SJoe Perches $comment = $2 if defined $2; 73420112475SJoe Perches $formatted_email =~ s/$address.*$//; 73520112475SJoe Perches $name = $formatted_email; 7363705ce5bSJoe Perches $name = trim($name); 73720112475SJoe Perches $name =~ s/^\"|\"$//g; 73820112475SJoe Perches # If there's a name left after stripping spaces and 73920112475SJoe Perches # leading quotes, and the address doesn't have both 74020112475SJoe Perches # leading and trailing angle brackets, the address 74120112475SJoe Perches # is invalid. ie: 74220112475SJoe Perches # "joe smith [email protected]" bad 74320112475SJoe Perches # "joe smith <[email protected]" bad 74420112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 74520112475SJoe Perches $name = ""; 74620112475SJoe Perches $address = ""; 74720112475SJoe Perches $comment = ""; 74820112475SJoe Perches } 74920112475SJoe Perches } 75020112475SJoe Perches 7513705ce5bSJoe Perches $name = trim($name); 75220112475SJoe Perches $name =~ s/^\"|\"$//g; 7533705ce5bSJoe Perches $address = trim($address); 75420112475SJoe Perches $address =~ s/^\<|\>$//g; 75520112475SJoe Perches 75620112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 75720112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 75820112475SJoe Perches $name = "\"$name\""; 75920112475SJoe Perches } 76020112475SJoe Perches 76120112475SJoe Perches return ($name, $address, $comment); 76220112475SJoe Perches} 76320112475SJoe Perches 76420112475SJoe Perchessub format_email { 76520112475SJoe Perches my ($name, $address) = @_; 76620112475SJoe Perches 76720112475SJoe Perches my $formatted_email; 76820112475SJoe Perches 7693705ce5bSJoe Perches $name = trim($name); 77020112475SJoe Perches $name =~ s/^\"|\"$//g; 7713705ce5bSJoe Perches $address = trim($address); 77220112475SJoe Perches 77320112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 77420112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 77520112475SJoe Perches $name = "\"$name\""; 77620112475SJoe Perches } 77720112475SJoe Perches 77820112475SJoe Perches if ("$name" eq "") { 77920112475SJoe Perches $formatted_email = "$address"; 78020112475SJoe Perches } else { 78120112475SJoe Perches $formatted_email = "$name <$address>"; 78220112475SJoe Perches } 78320112475SJoe Perches 78420112475SJoe Perches return $formatted_email; 78520112475SJoe Perches} 78620112475SJoe Perches 787d311cd44SJoe Perchessub which { 788d311cd44SJoe Perches my ($bin) = @_; 789d311cd44SJoe Perches 790d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 791d311cd44SJoe Perches if (-e "$path/$bin") { 792d311cd44SJoe Perches return "$path/$bin"; 793d311cd44SJoe Perches } 794d311cd44SJoe Perches } 795d311cd44SJoe Perches 796d311cd44SJoe Perches return ""; 797d311cd44SJoe Perches} 798d311cd44SJoe Perches 799000d1cc1SJoe Perchessub which_conf { 800000d1cc1SJoe Perches my ($conf) = @_; 801000d1cc1SJoe Perches 802000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 803000d1cc1SJoe Perches if (-e "$path/$conf") { 804000d1cc1SJoe Perches return "$path/$conf"; 805000d1cc1SJoe Perches } 806000d1cc1SJoe Perches } 807000d1cc1SJoe Perches 808000d1cc1SJoe Perches return ""; 809000d1cc1SJoe Perches} 810000d1cc1SJoe Perches 8110a920b5bSAndy Whitcroftsub expand_tabs { 8120a920b5bSAndy Whitcroft my ($str) = @_; 8130a920b5bSAndy Whitcroft 8140a920b5bSAndy Whitcroft my $res = ''; 8150a920b5bSAndy Whitcroft my $n = 0; 8160a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 8170a920b5bSAndy Whitcroft if ($c eq "\t") { 8180a920b5bSAndy Whitcroft $res .= ' '; 8190a920b5bSAndy Whitcroft $n++; 8200a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 8210a920b5bSAndy Whitcroft $res .= ' '; 8220a920b5bSAndy Whitcroft } 8230a920b5bSAndy Whitcroft next; 8240a920b5bSAndy Whitcroft } 8250a920b5bSAndy Whitcroft $res .= $c; 8260a920b5bSAndy Whitcroft $n++; 8270a920b5bSAndy Whitcroft } 8280a920b5bSAndy Whitcroft 8290a920b5bSAndy Whitcroft return $res; 8300a920b5bSAndy Whitcroft} 8316c72ffaaSAndy Whitcroftsub copy_spacing { 832773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 8336c72ffaaSAndy Whitcroft return $res; 8346c72ffaaSAndy Whitcroft} 8350a920b5bSAndy Whitcroft 8364a0df2efSAndy Whitcroftsub line_stats { 8374a0df2efSAndy Whitcroft my ($line) = @_; 8384a0df2efSAndy Whitcroft 8394a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 8404a0df2efSAndy Whitcroft $line =~ s/^.//; 8414a0df2efSAndy Whitcroft $line = expand_tabs($line); 8424a0df2efSAndy Whitcroft 8434a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 8444a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 8454a0df2efSAndy Whitcroft 8464a0df2efSAndy Whitcroft return (length($line), length($white)); 8474a0df2efSAndy Whitcroft} 8484a0df2efSAndy Whitcroft 849773647a0SAndy Whitcroftmy $sanitise_quote = ''; 850773647a0SAndy Whitcroft 851773647a0SAndy Whitcroftsub sanitise_line_reset { 852773647a0SAndy Whitcroft my ($in_comment) = @_; 853773647a0SAndy Whitcroft 854773647a0SAndy Whitcroft if ($in_comment) { 855773647a0SAndy Whitcroft $sanitise_quote = '*/'; 856773647a0SAndy Whitcroft } else { 857773647a0SAndy Whitcroft $sanitise_quote = ''; 858773647a0SAndy Whitcroft } 859773647a0SAndy Whitcroft} 86000df344fSAndy Whitcroftsub sanitise_line { 86100df344fSAndy Whitcroft my ($line) = @_; 86200df344fSAndy Whitcroft 86300df344fSAndy Whitcroft my $res = ''; 86400df344fSAndy Whitcroft my $l = ''; 86500df344fSAndy Whitcroft 866c2fdda0dSAndy Whitcroft my $qlen = 0; 867773647a0SAndy Whitcroft my $off = 0; 868773647a0SAndy Whitcroft my $c; 86900df344fSAndy Whitcroft 870773647a0SAndy Whitcroft # Always copy over the diff marker. 871773647a0SAndy Whitcroft $res = substr($line, 0, 1); 872773647a0SAndy Whitcroft 873773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 874773647a0SAndy Whitcroft $c = substr($line, $off, 1); 875773647a0SAndy Whitcroft 876773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 877773647a0SAndy Whitcroft # and end, all to $;. 878773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 879773647a0SAndy Whitcroft $sanitise_quote = '*/'; 880773647a0SAndy Whitcroft 881773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 882773647a0SAndy Whitcroft $off++; 88300df344fSAndy Whitcroft next; 884773647a0SAndy Whitcroft } 88581bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 886773647a0SAndy Whitcroft $sanitise_quote = ''; 887773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 888773647a0SAndy Whitcroft $off++; 889773647a0SAndy Whitcroft next; 890773647a0SAndy Whitcroft } 891113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 892113f04a8SDaniel Walker $sanitise_quote = '//'; 893113f04a8SDaniel Walker 894113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 895113f04a8SDaniel Walker $off++; 896113f04a8SDaniel Walker next; 897113f04a8SDaniel Walker } 898773647a0SAndy Whitcroft 899773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 900773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 901773647a0SAndy Whitcroft $c eq "\\") { 902773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 903773647a0SAndy Whitcroft $off++; 904773647a0SAndy Whitcroft next; 905773647a0SAndy Whitcroft } 906773647a0SAndy Whitcroft # Regular quotes. 907773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 908773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 909773647a0SAndy Whitcroft $sanitise_quote = $c; 910773647a0SAndy Whitcroft 911773647a0SAndy Whitcroft substr($res, $off, 1, $c); 912773647a0SAndy Whitcroft next; 913773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 914773647a0SAndy Whitcroft $sanitise_quote = ''; 91500df344fSAndy Whitcroft } 91600df344fSAndy Whitcroft } 917773647a0SAndy Whitcroft 918fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 919773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 920773647a0SAndy Whitcroft substr($res, $off, 1, $;); 921113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 922113f04a8SDaniel Walker substr($res, $off, 1, $;); 923773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 924773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 92500df344fSAndy Whitcroft } else { 926773647a0SAndy Whitcroft substr($res, $off, 1, $c); 92700df344fSAndy Whitcroft } 928c2fdda0dSAndy Whitcroft } 929c2fdda0dSAndy Whitcroft 930113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 931113f04a8SDaniel Walker $sanitise_quote = ''; 932113f04a8SDaniel Walker } 933113f04a8SDaniel Walker 934c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 935c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 936c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 937c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 938c2fdda0dSAndy Whitcroft 939c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 940c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 941c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 942c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 943c2fdda0dSAndy Whitcroft } 944c2fdda0dSAndy Whitcroft 94500df344fSAndy Whitcroft return $res; 94600df344fSAndy Whitcroft} 94700df344fSAndy Whitcroft 948a6962d72SJoe Perchessub get_quoted_string { 949a6962d72SJoe Perches my ($line, $rawline) = @_; 950a6962d72SJoe Perches 9515e4f6ba5SJoe Perches return "" if ($line !~ m/(\"[X\t]+\")/g); 952a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 953a6962d72SJoe Perches} 954a6962d72SJoe Perches 9558905a67cSAndy Whitcroftsub ctx_statement_block { 9568905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 9578905a67cSAndy Whitcroft my $line = $linenr - 1; 9588905a67cSAndy Whitcroft my $blk = ''; 9598905a67cSAndy Whitcroft my $soff = $off; 9608905a67cSAndy Whitcroft my $coff = $off - 1; 961773647a0SAndy Whitcroft my $coff_set = 0; 9628905a67cSAndy Whitcroft 96313214adfSAndy Whitcroft my $loff = 0; 96413214adfSAndy Whitcroft 9658905a67cSAndy Whitcroft my $type = ''; 9668905a67cSAndy Whitcroft my $level = 0; 967a2750645SAndy Whitcroft my @stack = (); 968cf655043SAndy Whitcroft my $p; 9698905a67cSAndy Whitcroft my $c; 9708905a67cSAndy Whitcroft my $len = 0; 97113214adfSAndy Whitcroft 97213214adfSAndy Whitcroft my $remainder; 9738905a67cSAndy Whitcroft while (1) { 974a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 975a2750645SAndy Whitcroft 976773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 9778905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 9788905a67cSAndy Whitcroft # context. 9798905a67cSAndy Whitcroft if ($off >= $len) { 9808905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 981dea33496SAndy Whitcroft last if (!defined $lines[$line]); 982c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 9838905a67cSAndy Whitcroft $remain--; 98413214adfSAndy Whitcroft $loff = $len; 985c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 9868905a67cSAndy Whitcroft $len = length($blk); 9878905a67cSAndy Whitcroft $line++; 9888905a67cSAndy Whitcroft last; 9898905a67cSAndy Whitcroft } 9908905a67cSAndy Whitcroft # Bail if there is no further context. 9918905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 99213214adfSAndy Whitcroft if ($off >= $len) { 9938905a67cSAndy Whitcroft last; 9948905a67cSAndy Whitcroft } 995f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 996f74bd194SAndy Whitcroft $level++; 997f74bd194SAndy Whitcroft $type = '#'; 998f74bd194SAndy Whitcroft } 9998905a67cSAndy Whitcroft } 1000cf655043SAndy Whitcroft $p = $c; 10018905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 100213214adfSAndy Whitcroft $remainder = substr($blk, $off); 10038905a67cSAndy Whitcroft 1004773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 10054635f4fbSAndy Whitcroft 10064635f4fbSAndy Whitcroft # Handle nested #if/#else. 10074635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 10084635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 10094635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 10104635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 10114635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 10124635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 10134635f4fbSAndy Whitcroft } 10144635f4fbSAndy Whitcroft 10158905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 10168905a67cSAndy Whitcroft # outermost level. 10178905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 10188905a67cSAndy Whitcroft last; 10198905a67cSAndy Whitcroft } 10208905a67cSAndy Whitcroft 102113214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1022773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1023773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1024773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1025773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1026773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1027773647a0SAndy Whitcroft $coff_set = 1; 1028773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1029773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 103013214adfSAndy Whitcroft } 103113214adfSAndy Whitcroft 10328905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 10338905a67cSAndy Whitcroft $level++; 10348905a67cSAndy Whitcroft $type = '('; 10358905a67cSAndy Whitcroft } 10368905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 10378905a67cSAndy Whitcroft $level--; 10388905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 10398905a67cSAndy Whitcroft 10408905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 10418905a67cSAndy Whitcroft $coff = $off; 1042773647a0SAndy Whitcroft $coff_set = 1; 1043773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 10448905a67cSAndy Whitcroft } 10458905a67cSAndy Whitcroft } 10468905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 10478905a67cSAndy Whitcroft $level++; 10488905a67cSAndy Whitcroft $type = '{'; 10498905a67cSAndy Whitcroft } 10508905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 10518905a67cSAndy Whitcroft $level--; 10528905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 10538905a67cSAndy Whitcroft 10548905a67cSAndy Whitcroft if ($level == 0) { 1055b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1056b998e001SPatrick Pannuto $off++; 1057b998e001SPatrick Pannuto } 10588905a67cSAndy Whitcroft last; 10598905a67cSAndy Whitcroft } 10608905a67cSAndy Whitcroft } 1061f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1062f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1063f74bd194SAndy Whitcroft $level--; 1064f74bd194SAndy Whitcroft $type = ''; 1065f74bd194SAndy Whitcroft $off++; 1066f74bd194SAndy Whitcroft last; 1067f74bd194SAndy Whitcroft } 10688905a67cSAndy Whitcroft $off++; 10698905a67cSAndy Whitcroft } 1070a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 107113214adfSAndy Whitcroft if ($off == $len) { 1072a3bb97a7SAndy Whitcroft $loff = $len + 1; 107313214adfSAndy Whitcroft $line++; 107413214adfSAndy Whitcroft $remain--; 107513214adfSAndy Whitcroft } 10768905a67cSAndy Whitcroft 10778905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 10788905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 10798905a67cSAndy Whitcroft 10808905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 10818905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 10828905a67cSAndy Whitcroft 1083773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 108413214adfSAndy Whitcroft 108513214adfSAndy Whitcroft return ($statement, $condition, 108613214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 108713214adfSAndy Whitcroft} 108813214adfSAndy Whitcroft 1089cf655043SAndy Whitcroftsub statement_lines { 1090cf655043SAndy Whitcroft my ($stmt) = @_; 1091cf655043SAndy Whitcroft 1092cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1093cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1094cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1095cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1096cf655043SAndy Whitcroft 1097cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1098cf655043SAndy Whitcroft 1099cf655043SAndy Whitcroft return $#stmt_lines + 2; 1100cf655043SAndy Whitcroft} 1101cf655043SAndy Whitcroft 1102cf655043SAndy Whitcroftsub statement_rawlines { 1103cf655043SAndy Whitcroft my ($stmt) = @_; 1104cf655043SAndy Whitcroft 1105cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1106cf655043SAndy Whitcroft 1107cf655043SAndy Whitcroft return $#stmt_lines + 2; 1108cf655043SAndy Whitcroft} 1109cf655043SAndy Whitcroft 1110cf655043SAndy Whitcroftsub statement_block_size { 1111cf655043SAndy Whitcroft my ($stmt) = @_; 1112cf655043SAndy Whitcroft 1113cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1114cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1115cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1116cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1117cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1118cf655043SAndy Whitcroft 1119cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1120cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1121cf655043SAndy Whitcroft 1122cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1123cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1124cf655043SAndy Whitcroft 1125cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1126cf655043SAndy Whitcroft return $stmt_lines; 1127cf655043SAndy Whitcroft } else { 1128cf655043SAndy Whitcroft return $stmt_statements; 1129cf655043SAndy Whitcroft } 1130cf655043SAndy Whitcroft} 1131cf655043SAndy Whitcroft 113213214adfSAndy Whitcroftsub ctx_statement_full { 113313214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 113413214adfSAndy Whitcroft my ($statement, $condition, $level); 113513214adfSAndy Whitcroft 113613214adfSAndy Whitcroft my (@chunks); 113713214adfSAndy Whitcroft 1138cf655043SAndy Whitcroft # Grab the first conditional/block pair. 113913214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 114013214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1141773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 114213214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1143cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1144cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1145cf655043SAndy Whitcroft } 1146cf655043SAndy Whitcroft 1147cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1148cf655043SAndy Whitcroft # could continue the statement. 1149cf655043SAndy Whitcroft for (;;) { 115013214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 115113214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1152cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1153773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1154cf655043SAndy Whitcroft #print "C: push\n"; 1155cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 115613214adfSAndy Whitcroft } 115713214adfSAndy Whitcroft 115813214adfSAndy Whitcroft return ($level, $linenr, @chunks); 11598905a67cSAndy Whitcroft} 11608905a67cSAndy Whitcroft 11614a0df2efSAndy Whitcroftsub ctx_block_get { 1162f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 11634a0df2efSAndy Whitcroft my $line; 11644a0df2efSAndy Whitcroft my $start = $linenr - 1; 11654a0df2efSAndy Whitcroft my $blk = ''; 11664a0df2efSAndy Whitcroft my @o; 11674a0df2efSAndy Whitcroft my @c; 11684a0df2efSAndy Whitcroft my @res = (); 11694a0df2efSAndy Whitcroft 1170f0a594c1SAndy Whitcroft my $level = 0; 11714635f4fbSAndy Whitcroft my @stack = ($level); 117200df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 117300df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 117400df344fSAndy Whitcroft $remain--; 117500df344fSAndy Whitcroft 117600df344fSAndy Whitcroft $blk .= $rawlines[$line]; 11774635f4fbSAndy Whitcroft 11784635f4fbSAndy Whitcroft # Handle nested #if/#else. 117901464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 11804635f4fbSAndy Whitcroft push(@stack, $level); 118101464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 11824635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 118301464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 11844635f4fbSAndy Whitcroft $level = pop(@stack); 11854635f4fbSAndy Whitcroft } 11864635f4fbSAndy Whitcroft 118701464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1188f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1189f0a594c1SAndy Whitcroft if ($off > 0) { 1190f0a594c1SAndy Whitcroft $off--; 1191f0a594c1SAndy Whitcroft next; 1192f0a594c1SAndy Whitcroft } 11934a0df2efSAndy Whitcroft 1194f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1195f0a594c1SAndy Whitcroft $level--; 1196f0a594c1SAndy Whitcroft last if ($level == 0); 1197f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1198f0a594c1SAndy Whitcroft $level++; 1199f0a594c1SAndy Whitcroft } 1200f0a594c1SAndy Whitcroft } 12014a0df2efSAndy Whitcroft 1202f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 120300df344fSAndy Whitcroft push(@res, $rawlines[$line]); 12044a0df2efSAndy Whitcroft } 12054a0df2efSAndy Whitcroft 1206f0a594c1SAndy Whitcroft last if ($level == 0); 12074a0df2efSAndy Whitcroft } 12084a0df2efSAndy Whitcroft 1209f0a594c1SAndy Whitcroft return ($level, @res); 12104a0df2efSAndy Whitcroft} 12114a0df2efSAndy Whitcroftsub ctx_block_outer { 12124a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 12134a0df2efSAndy Whitcroft 1214f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1215f0a594c1SAndy Whitcroft return @r; 12164a0df2efSAndy Whitcroft} 12174a0df2efSAndy Whitcroftsub ctx_block { 12184a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 12194a0df2efSAndy Whitcroft 1220f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1221f0a594c1SAndy Whitcroft return @r; 1222653d4876SAndy Whitcroft} 1223653d4876SAndy Whitcroftsub ctx_statement { 1224f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1225f0a594c1SAndy Whitcroft 1226f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1227f0a594c1SAndy Whitcroft return @r; 1228f0a594c1SAndy Whitcroft} 1229f0a594c1SAndy Whitcroftsub ctx_block_level { 1230653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1231653d4876SAndy Whitcroft 1232f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 12334a0df2efSAndy Whitcroft} 12349c0ca6f9SAndy Whitcroftsub ctx_statement_level { 12359c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 12369c0ca6f9SAndy Whitcroft 12379c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 12389c0ca6f9SAndy Whitcroft} 12394a0df2efSAndy Whitcroft 12404a0df2efSAndy Whitcroftsub ctx_locate_comment { 12414a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 12424a0df2efSAndy Whitcroft 12434a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1244beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 12454a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 12464a0df2efSAndy Whitcroft 12474a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 12484a0df2efSAndy Whitcroft # comment. 12494a0df2efSAndy Whitcroft my $in_comment = 0; 12504a0df2efSAndy Whitcroft $current_comment = ''; 12514a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 125200df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 125300df344fSAndy Whitcroft #warn " $line\n"; 12544a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 12554a0df2efSAndy Whitcroft $in_comment = 1; 12564a0df2efSAndy Whitcroft } 12574a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 12584a0df2efSAndy Whitcroft $in_comment = 1; 12594a0df2efSAndy Whitcroft } 12604a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 12614a0df2efSAndy Whitcroft $current_comment = ''; 12624a0df2efSAndy Whitcroft } 12634a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 12644a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 12654a0df2efSAndy Whitcroft $in_comment = 0; 12664a0df2efSAndy Whitcroft } 12674a0df2efSAndy Whitcroft } 12684a0df2efSAndy Whitcroft 12694a0df2efSAndy Whitcroft chomp($current_comment); 12704a0df2efSAndy Whitcroft return($current_comment); 12714a0df2efSAndy Whitcroft} 12724a0df2efSAndy Whitcroftsub ctx_has_comment { 12734a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 12744a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 12754a0df2efSAndy Whitcroft 127600df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 12774a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 12784a0df2efSAndy Whitcroft 12794a0df2efSAndy Whitcroft return ($cmt ne ''); 12804a0df2efSAndy Whitcroft} 12814a0df2efSAndy Whitcroft 12824d001e4dSAndy Whitcroftsub raw_line { 12834d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 12844d001e4dSAndy Whitcroft 12854d001e4dSAndy Whitcroft my $offset = $linenr - 1; 12864d001e4dSAndy Whitcroft $cnt++; 12874d001e4dSAndy Whitcroft 12884d001e4dSAndy Whitcroft my $line; 12894d001e4dSAndy Whitcroft while ($cnt) { 12904d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 12914d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 12924d001e4dSAndy Whitcroft $cnt--; 12934d001e4dSAndy Whitcroft } 12944d001e4dSAndy Whitcroft 12954d001e4dSAndy Whitcroft return $line; 12964d001e4dSAndy Whitcroft} 12974d001e4dSAndy Whitcroft 12980a920b5bSAndy Whitcroftsub cat_vet { 12990a920b5bSAndy Whitcroft my ($vet) = @_; 13009c0ca6f9SAndy Whitcroft my ($res, $coded); 13010a920b5bSAndy Whitcroft 13029c0ca6f9SAndy Whitcroft $res = ''; 13036c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 13046c72ffaaSAndy Whitcroft $res .= $1; 13056c72ffaaSAndy Whitcroft if ($2 ne '') { 13069c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 13076c72ffaaSAndy Whitcroft $res .= $coded; 13086c72ffaaSAndy Whitcroft } 13099c0ca6f9SAndy Whitcroft } 13109c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 13110a920b5bSAndy Whitcroft 13129c0ca6f9SAndy Whitcroft return $res; 13130a920b5bSAndy Whitcroft} 13140a920b5bSAndy Whitcroft 1315c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1316cf655043SAndy Whitcroftmy $av_pending; 1317c2fdda0dSAndy Whitcroftmy @av_paren_type; 13181f65f947SAndy Whitcroftmy $av_pend_colon; 1319c2fdda0dSAndy Whitcroft 1320c2fdda0dSAndy Whitcroftsub annotate_reset { 1321c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1322cf655043SAndy Whitcroft $av_pending = '_'; 1323cf655043SAndy Whitcroft @av_paren_type = ('E'); 13241f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1325c2fdda0dSAndy Whitcroft} 1326c2fdda0dSAndy Whitcroft 13276c72ffaaSAndy Whitcroftsub annotate_values { 13286c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 13296c72ffaaSAndy Whitcroft 13306c72ffaaSAndy Whitcroft my $res; 13311f65f947SAndy Whitcroft my $var = '_' x length($stream); 13326c72ffaaSAndy Whitcroft my $cur = $stream; 13336c72ffaaSAndy Whitcroft 1334c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 13356c72ffaaSAndy Whitcroft 13366c72ffaaSAndy Whitcroft while (length($cur)) { 1337773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1338cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1339171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 13406c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1341c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1342c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1343cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1344c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 13456c72ffaaSAndy Whitcroft } 13466c72ffaaSAndy Whitcroft 1347c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 13489446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 13499446ef56SAndy Whitcroft push(@av_paren_type, $type); 1350addcdceaSAndy Whitcroft $type = 'c'; 13519446ef56SAndy Whitcroft 1352e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1353c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 13546c72ffaaSAndy Whitcroft $type = 'T'; 13556c72ffaaSAndy Whitcroft 1356389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1357389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1358389a2fe5SAndy Whitcroft $type = 'T'; 1359389a2fe5SAndy Whitcroft 1360c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1361171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1362c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1363171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1364171ae1a4SAndy Whitcroft if ($2 ne '') { 1365cf655043SAndy Whitcroft $av_pending = 'N'; 1366171ae1a4SAndy Whitcroft } 1367171ae1a4SAndy Whitcroft $type = 'E'; 1368171ae1a4SAndy Whitcroft 1369c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1370171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1371171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1372171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 13736c72ffaaSAndy Whitcroft 1374c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1375cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1376c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1377cf655043SAndy Whitcroft 1378cf655043SAndy Whitcroft push(@av_paren_type, $type); 1379cf655043SAndy Whitcroft push(@av_paren_type, $type); 1380171ae1a4SAndy Whitcroft $type = 'E'; 1381cf655043SAndy Whitcroft 1382c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1383cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1384cf655043SAndy Whitcroft $av_preprocessor = 1; 1385cf655043SAndy Whitcroft 1386cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1387cf655043SAndy Whitcroft 1388171ae1a4SAndy Whitcroft $type = 'E'; 1389cf655043SAndy Whitcroft 1390c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1391cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1392cf655043SAndy Whitcroft 1393cf655043SAndy Whitcroft $av_preprocessor = 1; 1394cf655043SAndy Whitcroft 1395cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1396cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1397cf655043SAndy Whitcroft pop(@av_paren_type); 1398cf655043SAndy Whitcroft push(@av_paren_type, $type); 1399171ae1a4SAndy Whitcroft $type = 'E'; 14006c72ffaaSAndy Whitcroft 14016c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1402c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 14036c72ffaaSAndy Whitcroft 1404171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1405171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1406171ae1a4SAndy Whitcroft $av_pending = $type; 1407171ae1a4SAndy Whitcroft $type = 'N'; 1408171ae1a4SAndy Whitcroft 14096c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1410c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 14116c72ffaaSAndy Whitcroft if (defined $2) { 1412cf655043SAndy Whitcroft $av_pending = 'V'; 14136c72ffaaSAndy Whitcroft } 14146c72ffaaSAndy Whitcroft $type = 'N'; 14156c72ffaaSAndy Whitcroft 141614b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1417c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 141814b111c1SAndy Whitcroft $av_pending = 'E'; 14196c72ffaaSAndy Whitcroft $type = 'N'; 14206c72ffaaSAndy Whitcroft 14211f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 14221f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 14231f65f947SAndy Whitcroft $av_pend_colon = 'C'; 14241f65f947SAndy Whitcroft $type = 'N'; 14251f65f947SAndy Whitcroft 142614b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1427c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 14286c72ffaaSAndy Whitcroft $type = 'N'; 14296c72ffaaSAndy Whitcroft 14306c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1431c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1432cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1433cf655043SAndy Whitcroft $av_pending = '_'; 14346c72ffaaSAndy Whitcroft $type = 'N'; 14356c72ffaaSAndy Whitcroft 14366c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1437cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1438cf655043SAndy Whitcroft if ($new_type ne '_') { 1439cf655043SAndy Whitcroft $type = $new_type; 1440c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1441c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 14426c72ffaaSAndy Whitcroft } else { 1443c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 14446c72ffaaSAndy Whitcroft } 14456c72ffaaSAndy Whitcroft 1446c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1447c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1448c8cb2ca3SAndy Whitcroft $type = 'V'; 1449cf655043SAndy Whitcroft $av_pending = 'V'; 14506c72ffaaSAndy Whitcroft 14518e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 14528e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 14531f65f947SAndy Whitcroft $av_pend_colon = 'B'; 14548e761b04SAndy Whitcroft } elsif ($type eq 'E') { 14558e761b04SAndy Whitcroft $av_pend_colon = 'L'; 14561f65f947SAndy Whitcroft } 14571f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 14581f65f947SAndy Whitcroft $type = 'V'; 14591f65f947SAndy Whitcroft 14606c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1461c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 14626c72ffaaSAndy Whitcroft $type = 'V'; 14636c72ffaaSAndy Whitcroft 14646c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1465c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 14666c72ffaaSAndy Whitcroft $type = 'N'; 14676c72ffaaSAndy Whitcroft 1468cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1469c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 147013214adfSAndy Whitcroft $type = 'E'; 14711f65f947SAndy Whitcroft $av_pend_colon = 'O'; 147213214adfSAndy Whitcroft 14738e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 14748e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 14758e761b04SAndy Whitcroft $type = 'C'; 14768e761b04SAndy Whitcroft 14771f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 14781f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 14791f65f947SAndy Whitcroft $type = 'N'; 14801f65f947SAndy Whitcroft 14811f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 14821f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 14831f65f947SAndy Whitcroft 14841f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 14851f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 14861f65f947SAndy Whitcroft $type = 'E'; 14871f65f947SAndy Whitcroft } else { 14881f65f947SAndy Whitcroft $type = 'N'; 14891f65f947SAndy Whitcroft } 14901f65f947SAndy Whitcroft $av_pend_colon = 'O'; 14911f65f947SAndy Whitcroft 14928e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 149313214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 14946c72ffaaSAndy Whitcroft $type = 'N'; 14956c72ffaaSAndy Whitcroft 14960d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 149774048ed8SAndy Whitcroft my $variant; 149874048ed8SAndy Whitcroft 149974048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 150074048ed8SAndy Whitcroft if ($type eq 'V') { 150174048ed8SAndy Whitcroft $variant = 'B'; 150274048ed8SAndy Whitcroft } else { 150374048ed8SAndy Whitcroft $variant = 'U'; 150474048ed8SAndy Whitcroft } 150574048ed8SAndy Whitcroft 150674048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 150774048ed8SAndy Whitcroft $type = 'N'; 150874048ed8SAndy Whitcroft 15096c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1510c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 15116c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 15126c72ffaaSAndy Whitcroft $type = 'N'; 15136c72ffaaSAndy Whitcroft } 15146c72ffaaSAndy Whitcroft 15156c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1516c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 15176c72ffaaSAndy Whitcroft } 15186c72ffaaSAndy Whitcroft if (defined $1) { 15196c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 15206c72ffaaSAndy Whitcroft $res .= $type x length($1); 15216c72ffaaSAndy Whitcroft } 15226c72ffaaSAndy Whitcroft } 15236c72ffaaSAndy Whitcroft 15241f65f947SAndy Whitcroft return ($res, $var); 15256c72ffaaSAndy Whitcroft} 15266c72ffaaSAndy Whitcroft 15278905a67cSAndy Whitcroftsub possible { 152813214adfSAndy Whitcroft my ($possible, $line) = @_; 15299a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 15300776e594SAndy Whitcroft ^(?: 15310776e594SAndy Whitcroft $Modifier| 15320776e594SAndy Whitcroft $Storage| 15330776e594SAndy Whitcroft $Type| 15349a974fdbSAndy Whitcroft DEFINE_\S+ 15359a974fdbSAndy Whitcroft )$| 15369a974fdbSAndy Whitcroft ^(?: 15370776e594SAndy Whitcroft goto| 15380776e594SAndy Whitcroft return| 15390776e594SAndy Whitcroft case| 15400776e594SAndy Whitcroft else| 15410776e594SAndy Whitcroft asm|__asm__| 154289a88353SAndy Whitcroft do| 154389a88353SAndy Whitcroft \#| 154489a88353SAndy Whitcroft \#\#| 15459a974fdbSAndy Whitcroft )(?:\s|$)| 15460776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 15479a974fdbSAndy Whitcroft )}x; 15489a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 15499a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1550c45dcabdSAndy Whitcroft # Check for modifiers. 1551c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1552c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1553c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1554c45dcabdSAndy Whitcroft 1555c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1556c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1557d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 15589a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1559d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1560d2506586SAndy Whitcroft push(@modifierList, $modifier); 1561d2506586SAndy Whitcroft } 15629a974fdbSAndy Whitcroft } 1563c45dcabdSAndy Whitcroft 1564c45dcabdSAndy Whitcroft } else { 156513214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 15668905a67cSAndy Whitcroft push(@typeList, $possible); 1567c45dcabdSAndy Whitcroft } 15688905a67cSAndy Whitcroft build_types(); 15690776e594SAndy Whitcroft } else { 15700776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 15718905a67cSAndy Whitcroft } 15728905a67cSAndy Whitcroft} 15738905a67cSAndy Whitcroft 15746c72ffaaSAndy Whitcroftmy $prefix = ''; 15756c72ffaaSAndy Whitcroft 1576000d1cc1SJoe Perchessub show_type { 1577cbec18afSJoe Perches my ($type) = @_; 157891bfe484SJoe Perches 1579cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 1580cbec18afSJoe Perches 1581cbec18afSJoe Perches return !defined $ignore_type{$type}; 1582000d1cc1SJoe Perches} 1583000d1cc1SJoe Perches 1584f0a594c1SAndy Whitcroftsub report { 1585cbec18afSJoe Perches my ($level, $type, $msg) = @_; 1586cbec18afSJoe Perches 1587cbec18afSJoe Perches if (!show_type($type) || 1588cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1589773647a0SAndy Whitcroft return 0; 1590773647a0SAndy Whitcroft } 1591000d1cc1SJoe Perches my $line; 1592000d1cc1SJoe Perches if ($show_types) { 1593cbec18afSJoe Perches $line = "$prefix$level:$type: $msg\n"; 1594000d1cc1SJoe Perches } else { 1595cbec18afSJoe Perches $line = "$prefix$level: $msg\n"; 1596000d1cc1SJoe Perches } 15978905a67cSAndy Whitcroft $line = (split('\n', $line))[0] . "\n" if ($terse); 15988905a67cSAndy Whitcroft 159913214adfSAndy Whitcroft push(our @report, $line); 1600773647a0SAndy Whitcroft 1601773647a0SAndy Whitcroft return 1; 1602f0a594c1SAndy Whitcroft} 1603cbec18afSJoe Perches 1604f0a594c1SAndy Whitcroftsub report_dump { 160513214adfSAndy Whitcroft our @report; 1606f0a594c1SAndy Whitcroft} 1607000d1cc1SJoe Perches 1608d752fcc8SJoe Perchessub fixup_current_range { 1609d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 1610d752fcc8SJoe Perches 1611d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 1612d752fcc8SJoe Perches my $o = $1; 1613d752fcc8SJoe Perches my $l = $2; 1614d752fcc8SJoe Perches my $no = $o + $offset; 1615d752fcc8SJoe Perches my $nl = $l + $length; 1616d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 1617d752fcc8SJoe Perches } 1618d752fcc8SJoe Perches} 1619d752fcc8SJoe Perches 1620d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 1621d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 1622d752fcc8SJoe Perches 1623d752fcc8SJoe Perches my $range_last_linenr = 0; 1624d752fcc8SJoe Perches my $delta_offset = 0; 1625d752fcc8SJoe Perches 1626d752fcc8SJoe Perches my $old_linenr = 0; 1627d752fcc8SJoe Perches my $new_linenr = 0; 1628d752fcc8SJoe Perches 1629d752fcc8SJoe Perches my $next_insert = 0; 1630d752fcc8SJoe Perches my $next_delete = 0; 1631d752fcc8SJoe Perches 1632d752fcc8SJoe Perches my @lines = (); 1633d752fcc8SJoe Perches 1634d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 1635d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 1636d752fcc8SJoe Perches 1637d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 1638d752fcc8SJoe Perches my $save_line = 1; 1639d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 1640d752fcc8SJoe Perches if ($line =~ /^(?:\+\+\+\|\-\-\-)\s+\S+/) { #new filename 1641d752fcc8SJoe Perches $delta_offset = 0; 1642d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 1643d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 1644d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 1645d752fcc8SJoe Perches } 1646d752fcc8SJoe Perches 1647d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 1648d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 1649d752fcc8SJoe Perches $save_line = 0; 1650d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 1651d752fcc8SJoe Perches } 1652d752fcc8SJoe Perches 1653d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 1654d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 1655d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 1656d752fcc8SJoe Perches $new_linenr++; 1657d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 1658d752fcc8SJoe Perches } 1659d752fcc8SJoe Perches 1660d752fcc8SJoe Perches if ($save_line) { 1661d752fcc8SJoe Perches push(@lines, $line); 1662d752fcc8SJoe Perches $new_linenr++; 1663d752fcc8SJoe Perches } 1664d752fcc8SJoe Perches 1665d752fcc8SJoe Perches $old_linenr++; 1666d752fcc8SJoe Perches } 1667d752fcc8SJoe Perches 1668d752fcc8SJoe Perches return @lines; 1669d752fcc8SJoe Perches} 1670d752fcc8SJoe Perches 1671f2d7e4d4SJoe Perchessub fix_insert_line { 1672f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 1673f2d7e4d4SJoe Perches 1674f2d7e4d4SJoe Perches my $inserted = { 1675f2d7e4d4SJoe Perches LINENR => $linenr, 1676f2d7e4d4SJoe Perches LINE => $line, 1677f2d7e4d4SJoe Perches }; 1678f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 1679f2d7e4d4SJoe Perches} 1680f2d7e4d4SJoe Perches 1681f2d7e4d4SJoe Perchessub fix_delete_line { 1682f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 1683f2d7e4d4SJoe Perches 1684f2d7e4d4SJoe Perches my $deleted = { 1685f2d7e4d4SJoe Perches LINENR => $linenr, 1686f2d7e4d4SJoe Perches LINE => $line, 1687f2d7e4d4SJoe Perches }; 1688f2d7e4d4SJoe Perches 1689f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 1690f2d7e4d4SJoe Perches} 1691f2d7e4d4SJoe Perches 1692de7d4f0eSAndy Whitcroftsub ERROR { 1693cbec18afSJoe Perches my ($type, $msg) = @_; 1694cbec18afSJoe Perches 1695cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 1696de7d4f0eSAndy Whitcroft our $clean = 0; 16976c72ffaaSAndy Whitcroft our $cnt_error++; 16983705ce5bSJoe Perches return 1; 1699de7d4f0eSAndy Whitcroft } 17003705ce5bSJoe Perches return 0; 1701773647a0SAndy Whitcroft} 1702de7d4f0eSAndy Whitcroftsub WARN { 1703cbec18afSJoe Perches my ($type, $msg) = @_; 1704cbec18afSJoe Perches 1705cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 1706de7d4f0eSAndy Whitcroft our $clean = 0; 17076c72ffaaSAndy Whitcroft our $cnt_warn++; 17083705ce5bSJoe Perches return 1; 1709de7d4f0eSAndy Whitcroft } 17103705ce5bSJoe Perches return 0; 1711773647a0SAndy Whitcroft} 1712de7d4f0eSAndy Whitcroftsub CHK { 1713cbec18afSJoe Perches my ($type, $msg) = @_; 1714cbec18afSJoe Perches 1715cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 1716de7d4f0eSAndy Whitcroft our $clean = 0; 17176c72ffaaSAndy Whitcroft our $cnt_chk++; 17183705ce5bSJoe Perches return 1; 17196c72ffaaSAndy Whitcroft } 17203705ce5bSJoe Perches return 0; 1721de7d4f0eSAndy Whitcroft} 1722de7d4f0eSAndy Whitcroft 17236ecd9674SAndy Whitcroftsub check_absolute_file { 17246ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 17256ecd9674SAndy Whitcroft my $file = $absolute; 17266ecd9674SAndy Whitcroft 17276ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 17286ecd9674SAndy Whitcroft 17296ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 17306ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 17316ecd9674SAndy Whitcroft if (-f "$root/$file") { 17326ecd9674SAndy Whitcroft ##print "file<$file>\n"; 17336ecd9674SAndy Whitcroft last; 17346ecd9674SAndy Whitcroft } 17356ecd9674SAndy Whitcroft } 17366ecd9674SAndy Whitcroft if (! -f _) { 17376ecd9674SAndy Whitcroft return 0; 17386ecd9674SAndy Whitcroft } 17396ecd9674SAndy Whitcroft 17406ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 17416ecd9674SAndy Whitcroft my $prefix = $absolute; 17426ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 17436ecd9674SAndy Whitcroft 17446ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 17456ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1746000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1747000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 17486ecd9674SAndy Whitcroft } 17496ecd9674SAndy Whitcroft} 17506ecd9674SAndy Whitcroft 17513705ce5bSJoe Perchessub trim { 17523705ce5bSJoe Perches my ($string) = @_; 17533705ce5bSJoe Perches 1754b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 1755b34c648bSJoe Perches 1756b34c648bSJoe Perches return $string; 1757b34c648bSJoe Perches} 1758b34c648bSJoe Perches 1759b34c648bSJoe Perchessub ltrim { 1760b34c648bSJoe Perches my ($string) = @_; 1761b34c648bSJoe Perches 1762b34c648bSJoe Perches $string =~ s/^\s+//; 1763b34c648bSJoe Perches 1764b34c648bSJoe Perches return $string; 1765b34c648bSJoe Perches} 1766b34c648bSJoe Perches 1767b34c648bSJoe Perchessub rtrim { 1768b34c648bSJoe Perches my ($string) = @_; 1769b34c648bSJoe Perches 1770b34c648bSJoe Perches $string =~ s/\s+$//; 17713705ce5bSJoe Perches 17723705ce5bSJoe Perches return $string; 17733705ce5bSJoe Perches} 17743705ce5bSJoe Perches 177552ea8506SJoe Perchessub string_find_replace { 177652ea8506SJoe Perches my ($string, $find, $replace) = @_; 177752ea8506SJoe Perches 177852ea8506SJoe Perches $string =~ s/$find/$replace/g; 177952ea8506SJoe Perches 178052ea8506SJoe Perches return $string; 178152ea8506SJoe Perches} 178252ea8506SJoe Perches 17833705ce5bSJoe Perchessub tabify { 17843705ce5bSJoe Perches my ($leading) = @_; 17853705ce5bSJoe Perches 17863705ce5bSJoe Perches my $source_indent = 8; 17873705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 17883705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 17893705ce5bSJoe Perches 17903705ce5bSJoe Perches #convert leading spaces to tabs 17913705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 17923705ce5bSJoe Perches #Remove spaces before a tab 17933705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 17943705ce5bSJoe Perches 17953705ce5bSJoe Perches return "$leading"; 17963705ce5bSJoe Perches} 17973705ce5bSJoe Perches 1798d1fe9c09SJoe Perchessub pos_last_openparen { 1799d1fe9c09SJoe Perches my ($line) = @_; 1800d1fe9c09SJoe Perches 1801d1fe9c09SJoe Perches my $pos = 0; 1802d1fe9c09SJoe Perches 1803d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1804d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1805d1fe9c09SJoe Perches 1806d1fe9c09SJoe Perches my $last_openparen = 0; 1807d1fe9c09SJoe Perches 1808d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1809d1fe9c09SJoe Perches return -1; 1810d1fe9c09SJoe Perches } 1811d1fe9c09SJoe Perches 1812d1fe9c09SJoe Perches my $len = length($line); 1813d1fe9c09SJoe Perches 1814d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1815d1fe9c09SJoe Perches my $string = substr($line, $pos); 1816d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1817d1fe9c09SJoe Perches $pos += length($1) - 1; 1818d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1819d1fe9c09SJoe Perches $last_openparen = $pos; 1820d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1821d1fe9c09SJoe Perches last; 1822d1fe9c09SJoe Perches } 1823d1fe9c09SJoe Perches } 1824d1fe9c09SJoe Perches 182591cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 1826d1fe9c09SJoe Perches} 1827d1fe9c09SJoe Perches 18280a920b5bSAndy Whitcroftsub process { 18290a920b5bSAndy Whitcroft my $filename = shift; 18300a920b5bSAndy Whitcroft 18310a920b5bSAndy Whitcroft my $linenr=0; 18320a920b5bSAndy Whitcroft my $prevline=""; 1833c2fdda0dSAndy Whitcroft my $prevrawline=""; 18340a920b5bSAndy Whitcroft my $stashline=""; 1835c2fdda0dSAndy Whitcroft my $stashrawline=""; 18360a920b5bSAndy Whitcroft 18374a0df2efSAndy Whitcroft my $length; 18380a920b5bSAndy Whitcroft my $indent; 18390a920b5bSAndy Whitcroft my $previndent=0; 18400a920b5bSAndy Whitcroft my $stashindent=0; 18410a920b5bSAndy Whitcroft 1842de7d4f0eSAndy Whitcroft our $clean = 1; 18430a920b5bSAndy Whitcroft my $signoff = 0; 18440a920b5bSAndy Whitcroft my $is_patch = 0; 18450a920b5bSAndy Whitcroft 184629ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 184715662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 184813f1937eSJoe Perches my $reported_maintainer_file = 0; 1849fa64205dSPasi Savanainen my $non_utf8_charset = 0; 1850fa64205dSPasi Savanainen 1851365dd4eaSJoe Perches my $last_blank_line = 0; 18525e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 1853365dd4eaSJoe Perches 185413214adfSAndy Whitcroft our @report = (); 18556c72ffaaSAndy Whitcroft our $cnt_lines = 0; 18566c72ffaaSAndy Whitcroft our $cnt_error = 0; 18576c72ffaaSAndy Whitcroft our $cnt_warn = 0; 18586c72ffaaSAndy Whitcroft our $cnt_chk = 0; 18596c72ffaaSAndy Whitcroft 18600a920b5bSAndy Whitcroft # Trace the real file/line as we go. 18610a920b5bSAndy Whitcroft my $realfile = ''; 18620a920b5bSAndy Whitcroft my $realline = 0; 18630a920b5bSAndy Whitcroft my $realcnt = 0; 18640a920b5bSAndy Whitcroft my $here = ''; 18650a920b5bSAndy Whitcroft my $in_comment = 0; 1866c2fdda0dSAndy Whitcroft my $comment_edge = 0; 18670a920b5bSAndy Whitcroft my $first_line = 0; 18681e855726SWolfram Sang my $p1_prefix = ''; 18690a920b5bSAndy Whitcroft 187013214adfSAndy Whitcroft my $prev_values = 'E'; 187113214adfSAndy Whitcroft 187213214adfSAndy Whitcroft # suppression flags 1873773647a0SAndy Whitcroft my %suppress_ifbraces; 1874170d3a22SAndy Whitcroft my %suppress_whiletrailers; 18752b474a1aSAndy Whitcroft my %suppress_export; 18763e469cdcSAndy Whitcroft my $suppress_statement = 0; 1877653d4876SAndy Whitcroft 18787e51f197SJoe Perches my %signatures = (); 1879323c1260SJoe Perches 1880c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1881de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1882c2fdda0dSAndy Whitcroft # 1883de7d4f0eSAndy Whitcroft my @setup_docs = (); 1884de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1885773647a0SAndy Whitcroft 1886d8b07710SJoe Perches my $camelcase_file_seeded = 0; 1887d8b07710SJoe Perches 1888773647a0SAndy Whitcroft sanitise_line_reset(); 1889c2fdda0dSAndy Whitcroft my $line; 1890c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1891773647a0SAndy Whitcroft $linenr++; 1892773647a0SAndy Whitcroft $line = $rawline; 1893c2fdda0dSAndy Whitcroft 18943705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 18953705ce5bSJoe Perches 1896773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1897de7d4f0eSAndy Whitcroft $setup_docs = 0; 1898de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1899de7d4f0eSAndy Whitcroft $setup_docs = 1; 1900de7d4f0eSAndy Whitcroft } 1901773647a0SAndy Whitcroft #next; 1902de7d4f0eSAndy Whitcroft } 1903773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 1904773647a0SAndy Whitcroft $realline=$1-1; 1905773647a0SAndy Whitcroft if (defined $2) { 1906773647a0SAndy Whitcroft $realcnt=$3+1; 1907773647a0SAndy Whitcroft } else { 1908773647a0SAndy Whitcroft $realcnt=1+1; 1909773647a0SAndy Whitcroft } 1910c45dcabdSAndy Whitcroft $in_comment = 0; 1911773647a0SAndy Whitcroft 1912773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 1913773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 1914773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 1915773647a0SAndy Whitcroft # at context start. 1916773647a0SAndy Whitcroft my $edge; 191701fa9147SAndy Whitcroft my $cnt = $realcnt; 191801fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 191901fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 192001fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 192101fa9147SAndy Whitcroft $cnt--; 192201fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 1923721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 1924fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 1925fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 1926fae17daeSAndy Whitcroft ($edge) = $1; 1927fae17daeSAndy Whitcroft last; 1928fae17daeSAndy Whitcroft } 1929773647a0SAndy Whitcroft } 1930773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 1931773647a0SAndy Whitcroft $in_comment = 1; 1932773647a0SAndy Whitcroft } 1933773647a0SAndy Whitcroft 1934773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 1935773647a0SAndy Whitcroft # is the start of a diff block and this line starts 1936773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 1937773647a0SAndy Whitcroft if (!defined $edge && 193883242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 1939773647a0SAndy Whitcroft { 1940773647a0SAndy Whitcroft $in_comment = 1; 1941773647a0SAndy Whitcroft } 1942773647a0SAndy Whitcroft 1943773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 1944773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 1945773647a0SAndy Whitcroft 1946171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 1947773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 1948171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 1949773647a0SAndy Whitcroft $line = sanitise_line($rawline); 1950773647a0SAndy Whitcroft } 1951773647a0SAndy Whitcroft push(@lines, $line); 1952773647a0SAndy Whitcroft 1953773647a0SAndy Whitcroft if ($realcnt > 1) { 1954773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 1955773647a0SAndy Whitcroft } else { 1956773647a0SAndy Whitcroft $realcnt = 0; 1957773647a0SAndy Whitcroft } 1958773647a0SAndy Whitcroft 1959773647a0SAndy Whitcroft #print "==>$rawline\n"; 1960773647a0SAndy Whitcroft #print "-->$line\n"; 1961de7d4f0eSAndy Whitcroft 1962de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 1963de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 1964de7d4f0eSAndy Whitcroft } 1965de7d4f0eSAndy Whitcroft } 1966de7d4f0eSAndy Whitcroft 19676c72ffaaSAndy Whitcroft $prefix = ''; 19686c72ffaaSAndy Whitcroft 1969773647a0SAndy Whitcroft $realcnt = 0; 1970773647a0SAndy Whitcroft $linenr = 0; 1971194f66fcSJoe Perches $fixlinenr = -1; 19720a920b5bSAndy Whitcroft foreach my $line (@lines) { 19730a920b5bSAndy Whitcroft $linenr++; 1974194f66fcSJoe Perches $fixlinenr++; 19751b5539b1SJoe Perches my $sline = $line; #copy of $line 19761b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 19770a920b5bSAndy Whitcroft 1978c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 19796c72ffaaSAndy Whitcroft 19800a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 19816c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 19820a920b5bSAndy Whitcroft $is_patch = 1; 19834a0df2efSAndy Whitcroft $first_line = $linenr + 1; 19840a920b5bSAndy Whitcroft $realline=$1-1; 19850a920b5bSAndy Whitcroft if (defined $2) { 19860a920b5bSAndy Whitcroft $realcnt=$3+1; 19870a920b5bSAndy Whitcroft } else { 19880a920b5bSAndy Whitcroft $realcnt=1+1; 19890a920b5bSAndy Whitcroft } 1990c2fdda0dSAndy Whitcroft annotate_reset(); 199113214adfSAndy Whitcroft $prev_values = 'E'; 199213214adfSAndy Whitcroft 1993773647a0SAndy Whitcroft %suppress_ifbraces = (); 1994170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 19952b474a1aSAndy Whitcroft %suppress_export = (); 19963e469cdcSAndy Whitcroft $suppress_statement = 0; 19970a920b5bSAndy Whitcroft next; 19980a920b5bSAndy Whitcroft 19994a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 20004a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 20014a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2002773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 20030a920b5bSAndy Whitcroft $realline++; 2004d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 20050a920b5bSAndy Whitcroft 20064a0df2efSAndy Whitcroft # Measure the line length and indent. 2007c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 20080a920b5bSAndy Whitcroft 20090a920b5bSAndy Whitcroft # Track the previous line. 20100a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 20110a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2012c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2013c2fdda0dSAndy Whitcroft 2014773647a0SAndy Whitcroft #warn "line<$line>\n"; 20156c72ffaaSAndy Whitcroft 2016d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2017d8aaf121SAndy Whitcroft $realcnt--; 20180a920b5bSAndy Whitcroft } 20190a920b5bSAndy Whitcroft 2020cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2021cc77cdcaSAndy Whitcroft 20220a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 2023773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 2024773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 2025773647a0SAndy Whitcroft 20266c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 20276c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2028773647a0SAndy Whitcroft 20292ac73b4fSJoe Perches my $found_file = 0; 2030773647a0SAndy Whitcroft # extract the filename as it passes 20313bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 20323bf9a009SRabin Vincent $realfile = $1; 20332b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2034270c49a0SJoe Perches $in_commit_log = 0; 20352ac73b4fSJoe Perches $found_file = 1; 20363bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2037773647a0SAndy Whitcroft $realfile = $1; 20382b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2039270c49a0SJoe Perches $in_commit_log = 0; 20401e855726SWolfram Sang 20411e855726SWolfram Sang $p1_prefix = $1; 2042e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2043e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2044000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2045000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 20461e855726SWolfram Sang } 2047773647a0SAndy Whitcroft 2048c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2049000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2050000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2051773647a0SAndy Whitcroft } 20522ac73b4fSJoe Perches $found_file = 1; 20532ac73b4fSJoe Perches } 20542ac73b4fSJoe Perches 20552ac73b4fSJoe Perches if ($found_file) { 20562ac73b4fSJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@) { 20572ac73b4fSJoe Perches $check = 1; 20582ac73b4fSJoe Perches } else { 20592ac73b4fSJoe Perches $check = $check_orig; 20602ac73b4fSJoe Perches } 2061773647a0SAndy Whitcroft next; 2062773647a0SAndy Whitcroft } 2063773647a0SAndy Whitcroft 2064389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 20650a920b5bSAndy Whitcroft 2066c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2067c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2068c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 20690a920b5bSAndy Whitcroft 20706c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 20716c72ffaaSAndy Whitcroft 20723bf9a009SRabin Vincent# Check for incorrect file permissions 20733bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 20743bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 207504db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 207604db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2077000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2078000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 20793bf9a009SRabin Vincent } 20803bf9a009SRabin Vincent } 20813bf9a009SRabin Vincent 208220112475SJoe Perches# Check the patch for a signoff: 2083d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 20844a0df2efSAndy Whitcroft $signoff++; 208515662b3eSJoe Perches $in_commit_log = 0; 20860a920b5bSAndy Whitcroft } 208720112475SJoe Perches 2088e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2089e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2090e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2091e0d975b1SJoe Perches $reported_maintainer_file = 1; 2092e0d975b1SJoe Perches } 2093e0d975b1SJoe Perches 209420112475SJoe Perches# Check signature styles 2095270c49a0SJoe Perches if (!$in_header_lines && 2096ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 209720112475SJoe Perches my $space_before = $1; 209820112475SJoe Perches my $sign_off = $2; 209920112475SJoe Perches my $space_after = $3; 210020112475SJoe Perches my $email = $4; 210120112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 210220112475SJoe Perches 2103ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2104ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2105ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2106ce0338dfSJoe Perches } 210720112475SJoe Perches if (defined $space_before && $space_before ne "") { 21083705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 21093705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 21103705ce5bSJoe Perches $fix) { 2111194f66fcSJoe Perches $fixed[$fixlinenr] = 21123705ce5bSJoe Perches "$ucfirst_sign_off $email"; 21133705ce5bSJoe Perches } 211420112475SJoe Perches } 211520112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 21163705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 21173705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 21183705ce5bSJoe Perches $fix) { 2119194f66fcSJoe Perches $fixed[$fixlinenr] = 21203705ce5bSJoe Perches "$ucfirst_sign_off $email"; 21213705ce5bSJoe Perches } 21223705ce5bSJoe Perches 212320112475SJoe Perches } 212420112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 21253705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 21263705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 21273705ce5bSJoe Perches $fix) { 2128194f66fcSJoe Perches $fixed[$fixlinenr] = 21293705ce5bSJoe Perches "$ucfirst_sign_off $email"; 21303705ce5bSJoe Perches } 213120112475SJoe Perches } 213220112475SJoe Perches 213320112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 213420112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 213520112475SJoe Perches if ($suggested_email eq "") { 2136000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2137000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 213820112475SJoe Perches } else { 213920112475SJoe Perches my $dequoted = $suggested_email; 214020112475SJoe Perches $dequoted =~ s/^"//; 214120112475SJoe Perches $dequoted =~ s/" </ </; 214220112475SJoe Perches # Don't force email to have quotes 214320112475SJoe Perches # Allow just an angle bracketed address 214420112475SJoe Perches if ("$dequoted$comment" ne $email && 214520112475SJoe Perches "<$email_address>$comment" ne $email && 214620112475SJoe Perches "$suggested_email$comment" ne $email) { 2147000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2148000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 214920112475SJoe Perches } 21500a920b5bSAndy Whitcroft } 21517e51f197SJoe Perches 21527e51f197SJoe Perches# Check for duplicate signatures 21537e51f197SJoe Perches my $sig_nospace = $line; 21547e51f197SJoe Perches $sig_nospace =~ s/\s//g; 21557e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 21567e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 21577e51f197SJoe Perches WARN("BAD_SIGN_OFF", 21587e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 21597e51f197SJoe Perches } else { 21607e51f197SJoe Perches $signatures{$sig_nospace} = 1; 21617e51f197SJoe Perches } 21620a920b5bSAndy Whitcroft } 21630a920b5bSAndy Whitcroft 21649b3189ebSJoe Perches# Check for old stable address 21659b3189ebSJoe Perches if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) { 21669b3189ebSJoe Perches ERROR("STABLE_ADDRESS", 21679b3189ebSJoe Perches "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr); 21689b3189ebSJoe Perches } 21699b3189ebSJoe Perches 21707ebd05efSChristopher Covington# Check for unwanted Gerrit info 21717ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 21727ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 21737ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 21747ebd05efSChristopher Covington } 21757ebd05efSChristopher Covington 2176d311cd44SJoe Perches# Check for improperly formed commit descriptions 2177d311cd44SJoe Perches if ($in_commit_log && 2178d311cd44SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}/i && 217966881735SJoe Perches !($line =~ /\b[Cc]ommit [0-9a-f]{12,40} \("/ || 218066881735SJoe Perches ($line =~ /\b[Cc]ommit [0-9a-f]{12,40}\s*$/ && 218166881735SJoe Perches defined $rawlines[$linenr] && 218266881735SJoe Perches $rawlines[$linenr] =~ /^\s*\("/))) { 2183d311cd44SJoe Perches $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i; 2184d311cd44SJoe Perches my $init_char = $1; 2185d311cd44SJoe Perches my $orig_commit = lc($2); 2186d311cd44SJoe Perches my $id = '01234567890ab'; 2187d311cd44SJoe Perches my $desc = 'commit description'; 2188d311cd44SJoe Perches ($id, $desc) = git_commit_info($orig_commit, $id, $desc); 2189d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 21903f6316b4SJoe Perches "Please use 12 or more chars for the git commit ID like: '${init_char}ommit $id (\"$desc\")'\n" . $herecurr); 2191d311cd44SJoe Perches } 2192d311cd44SJoe Perches 219313f1937eSJoe Perches# Check for added, moved or deleted files 219413f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 219513f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 219613f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 219713f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 219813f1937eSJoe Perches (defined($1) || defined($2))))) { 219913f1937eSJoe Perches $reported_maintainer_file = 1; 220013f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 220113f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 220213f1937eSJoe Perches } 220313f1937eSJoe Perches 220400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 22058905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2206000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2207000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 22086c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2209de7d4f0eSAndy Whitcroft } 2210de7d4f0eSAndy Whitcroft 22116ecd9674SAndy Whitcroft# Check for absolute kernel paths. 22126ecd9674SAndy Whitcroft if ($tree) { 22136ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 22146ecd9674SAndy Whitcroft my $file = $1; 22156ecd9674SAndy Whitcroft 22166ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 22176ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 22186ecd9674SAndy Whitcroft # 22196ecd9674SAndy Whitcroft } else { 22206ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 22216ecd9674SAndy Whitcroft } 22226ecd9674SAndy Whitcroft } 22236ecd9674SAndy Whitcroft } 22246ecd9674SAndy Whitcroft 2225de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2226de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2227171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2228171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2229171ae1a4SAndy Whitcroft 2230171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2231171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2232171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2233171ae1a4SAndy Whitcroft 223434d99219SJoe Perches CHK("INVALID_UTF8", 2235000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 223600df344fSAndy Whitcroft } 22370a920b5bSAndy Whitcroft 223815662b3eSJoe Perches# Check if it's the start of a commit log 223915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 224015662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 224129ee1b0cSJoe Perches !($rawline =~ /^\s+\S/ || 224229ee1b0cSJoe Perches $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { 224315662b3eSJoe Perches $in_header_lines = 0; 224415662b3eSJoe Perches $in_commit_log = 1; 224515662b3eSJoe Perches } 224615662b3eSJoe Perches 2247fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2248fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2249fa64205dSPasi Savanainen if ($in_header_lines && 2250fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2251fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2252fa64205dSPasi Savanainen $non_utf8_charset = 1; 2253fa64205dSPasi Savanainen } 2254fa64205dSPasi Savanainen 2255fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 225615662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2257fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 225815662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 225915662b3eSJoe Perches } 226015662b3eSJoe Perches 226166b47b4aSKees Cook# Check for various typo / spelling mistakes 226236061e38SJoe Perches if (defined($misspellings) && ($in_commit_log || $line =~ /^\+/)) { 226366b47b4aSKees Cook while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:$|[^a-z@])/gi) { 226466b47b4aSKees Cook my $typo = $1; 226566b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 226666b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 226766b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 226866b47b4aSKees Cook my $msg_type = \&WARN; 226966b47b4aSKees Cook $msg_type = \&CHK if ($file); 227066b47b4aSKees Cook if (&{$msg_type}("TYPO_SPELLING", 227166b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 227266b47b4aSKees Cook $fix) { 227366b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 227466b47b4aSKees Cook } 227566b47b4aSKees Cook } 227666b47b4aSKees Cook } 227766b47b4aSKees Cook 227830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 227930670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 228000df344fSAndy Whitcroft 22810a920b5bSAndy Whitcroft#trailing whitespace 22829c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2283c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2284d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2285d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2286d5e616fcSJoe Perches $fix) { 2287194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2288d5e616fcSJoe Perches } 2289c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2290c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 22913705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 22923705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 22933705ce5bSJoe Perches $fix) { 2294194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 22953705ce5bSJoe Perches } 22963705ce5bSJoe Perches 2297d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 22980a920b5bSAndy Whitcroft } 22995368df20SAndy Whitcroft 23004783f894SJosh Triplett# Check for FSF mailing addresses. 2301109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 23023e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 23033e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 23044783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 23054783f894SJosh Triplett my $msg_type = \&ERROR; 23064783f894SJosh Triplett $msg_type = \&CHK if ($file); 23074783f894SJosh Triplett &{$msg_type}("FSF_MAILING_ADDRESS", 23084783f894SJosh 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) 23094783f894SJosh Triplett } 23104783f894SJosh Triplett 23113354957aSAndi Kleen# check for Kconfig help text having a real description 23129fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 23139fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 23143354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 23158d73e0e7SJoe Perches $line =~ /^\+\s*config\s+/) { 23163354957aSAndi Kleen my $length = 0; 23179fe287d7SAndy Whitcroft my $cnt = $realcnt; 23189fe287d7SAndy Whitcroft my $ln = $linenr + 1; 23199fe287d7SAndy Whitcroft my $f; 2320a1385803SAndy Whitcroft my $is_start = 0; 23219fe287d7SAndy Whitcroft my $is_end = 0; 2322a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 23239fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 23249fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 23259fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 23269fe287d7SAndy Whitcroft 23279fe287d7SAndy Whitcroft next if ($f =~ /^-/); 23288d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 2329a1385803SAndy Whitcroft 23308d73e0e7SJoe Perches if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) { 2331a1385803SAndy Whitcroft $is_start = 1; 23328d73e0e7SJoe Perches } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { 2333a1385803SAndy Whitcroft $length = -1; 2334a1385803SAndy Whitcroft } 2335a1385803SAndy Whitcroft 23369fe287d7SAndy Whitcroft $f =~ s/^.//; 23373354957aSAndi Kleen $f =~ s/#.*//; 23383354957aSAndi Kleen $f =~ s/^\s+//; 23393354957aSAndi Kleen next if ($f =~ /^$/); 23409fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 23419fe287d7SAndy Whitcroft $is_end = 1; 23429fe287d7SAndy Whitcroft last; 23439fe287d7SAndy Whitcroft } 23443354957aSAndi Kleen $length++; 23453354957aSAndi Kleen } 234656193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 2347000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 234856193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 234956193274SVadim Bendebury } 2350a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 23513354957aSAndi Kleen } 23523354957aSAndi Kleen 23531ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 23541ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 23551ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 23561ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 23571ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 23581ba8dfd1SKees Cook } 23591ba8dfd1SKees Cook 2360c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2361c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2362c68e5878SArnaud Lacombe my $flag = $1; 2363c68e5878SArnaud Lacombe my $replacement = { 2364c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2365c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2366c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2367c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2368c68e5878SArnaud Lacombe }; 2369c68e5878SArnaud Lacombe 2370c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2371c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2372c68e5878SArnaud Lacombe } 2373c68e5878SArnaud Lacombe 2374bff5da43SRob Herring# check for DT compatible documentation 23757dd05b38SFlorian Vaussard if (defined $root && 23767dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 23777dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 23787dd05b38SFlorian Vaussard 2379bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2380bff5da43SRob Herring 2381cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2382cc93319bSFlorian Vaussard my $vp_file = $dt_path . "vendor-prefixes.txt"; 2383cc93319bSFlorian Vaussard 2384bff5da43SRob Herring foreach my $compat (@compats) { 2385bff5da43SRob Herring my $compat2 = $compat; 2386185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 2387185d566bSRob Herring my $compat3 = $compat; 2388185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 2389185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 2390bff5da43SRob Herring if ( $? >> 8 ) { 2391bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2392bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2393bff5da43SRob Herring } 2394bff5da43SRob Herring 23954fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 23964fbf32a6SFlorian Vaussard my $vendor = $1; 2397cc93319bSFlorian Vaussard `grep -Eq "^$vendor\\b" $vp_file`; 2398bff5da43SRob Herring if ( $? >> 8 ) { 2399bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2400cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 2401bff5da43SRob Herring } 2402bff5da43SRob Herring } 2403bff5da43SRob Herring } 2404bff5da43SRob Herring 24055368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 2406de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/); 24075368df20SAndy Whitcroft 24086cd7f386SJoe Perches#line length limit 2409c45dcabdSAndy Whitcroft if ($line =~ /^\+/ && $prevrawline !~ /\/\*\*/ && 2410f4c014c0SAndy Whitcroft $rawline !~ /^.\s*\*\s*\@$Ident\s/ && 24110fccc622SJoe Perches !($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(KERN_\S+\s*|[^"]*))?"[X\t]*"\s*(?:|,|\)\s*;)\s*$/ || 24128bbea968SJoe Perches $line =~ /^\+\s*"[^"]*"\s*(?:\s*|,|\)\s*;)\s*$/) && 24136cd7f386SJoe Perches $length > $max_line_length) 2414c45dcabdSAndy Whitcroft { 2415000d1cc1SJoe Perches WARN("LONG_LINE", 24166cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 24170a920b5bSAndy Whitcroft } 24180a920b5bSAndy Whitcroft 24198905a67cSAndy Whitcroft# check for adding lines without a newline. 24208905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2421000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 2422000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 24238905a67cSAndy Whitcroft } 24248905a67cSAndy Whitcroft 242542e41c54SMike Frysinger# Blackfin: use hi/lo macros 242642e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 242742e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 242842e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2429000d1cc1SJoe Perches ERROR("LO_MACRO", 2430000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 243142e41c54SMike Frysinger } 243242e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 243342e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2434000d1cc1SJoe Perches ERROR("HI_MACRO", 2435000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 243642e41c54SMike Frysinger } 243742e41c54SMike Frysinger } 243842e41c54SMike Frysinger 2439b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 2440de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 24410a920b5bSAndy Whitcroft 24420a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 24430a920b5bSAndy Whitcroft# more than 8 must use tabs. 2444c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 2445c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 2446c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2447d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 24483705ce5bSJoe Perches if (ERROR("CODE_INDENT", 24493705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 24503705ce5bSJoe Perches $fix) { 2451194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 24523705ce5bSJoe Perches } 24530a920b5bSAndy Whitcroft } 24540a920b5bSAndy Whitcroft 245508e44365SAlberto Panizzo# check for space before tabs. 245608e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 245708e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 24583705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 24593705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 24603705ce5bSJoe Perches $fix) { 2461194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 2462d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 2463194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 2464c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 24653705ce5bSJoe Perches } 246608e44365SAlberto Panizzo } 246708e44365SAlberto Panizzo 2468d1fe9c09SJoe Perches# check for && or || at the start of a line 2469d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 2470d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 2471d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 2472d1fe9c09SJoe Perches } 2473d1fe9c09SJoe Perches 2474d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 2475d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 247691cb5195SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 2477d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 2478d1fe9c09SJoe Perches my $oldindent = $1; 2479d1fe9c09SJoe Perches my $rest = $2; 2480d1fe9c09SJoe Perches 2481d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 2482d1fe9c09SJoe Perches if ($pos >= 0) { 2483b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 2484b34a26f3SJoe Perches my $newindent = $2; 2485d1fe9c09SJoe Perches 2486d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 2487d1fe9c09SJoe Perches "\t" x ($pos / 8) . 2488d1fe9c09SJoe Perches " " x ($pos % 8); 2489d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 2490d1fe9c09SJoe Perches 2491d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 2492d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 24933705ce5bSJoe Perches 24943705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 24953705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 24963705ce5bSJoe Perches $fix && $line =~ /^\+/) { 2497194f66fcSJoe Perches $fixed[$fixlinenr] =~ 24983705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 24993705ce5bSJoe Perches } 2500d1fe9c09SJoe Perches } 2501d1fe9c09SJoe Perches } 2502d1fe9c09SJoe Perches } 2503d1fe9c09SJoe Perches 250404941aa7SJoe Perches if ($line =~ /^\+.*(\w+\s*)?\(\s*$Type\s*\)[ \t]+(?!$Assignment|$Arithmetic|[,;\({\[\<\>])/ && 250504941aa7SJoe Perches (!defined($1) || $1 !~ /sizeof\s*/)) { 25063705ce5bSJoe Perches if (CHK("SPACING", 2507f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 25083705ce5bSJoe Perches $fix) { 2509194f66fcSJoe Perches $fixed[$fixlinenr] =~ 2510f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 25113705ce5bSJoe Perches } 2512aad4f614SJoe Perches } 2513aad4f614SJoe Perches 251405880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2515fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 251685ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 251785ad978cSJoe Perches $realline > 2) { 251805880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 251905880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 252005880600SJoe Perches } 252105880600SJoe Perches 252205880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2523a605e32eSJoe Perches $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* 2524a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 252561135e96SJoe Perches $rawline =~ /^\+/ && #line is new 2526a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 2527a605e32eSJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2528a605e32eSJoe Perches "networking block comments start with * on subsequent lines\n" . $hereprev); 2529a605e32eSJoe Perches } 2530a605e32eSJoe Perches 2531a605e32eSJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2532c24f9f19SJoe Perches $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2533c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2534c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2535c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 253605880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 253705880600SJoe Perches "networking block comments put the trailing */ on a separate line\n" . $herecurr); 253805880600SJoe Perches } 253905880600SJoe Perches 25407f619191SJoe Perches# check for missing blank lines after struct/union declarations 25417f619191SJoe Perches# with exceptions for various attributes and macros 25427f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 25437f619191SJoe Perches $line =~ /^\+/ && 25447f619191SJoe Perches !($line =~ /^\+\s*$/ || 25457f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 25467f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 25477f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 25487f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 25497f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 25507f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 25517f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 2552d752fcc8SJoe Perches if (CHK("LINE_SPACING", 2553d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 2554d752fcc8SJoe Perches $fix) { 2555f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 2556d752fcc8SJoe Perches } 25577f619191SJoe Perches } 25587f619191SJoe Perches 2559365dd4eaSJoe Perches# check for multiple consecutive blank lines 2560365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 2561365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 2562365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 2563d752fcc8SJoe Perches if (CHK("LINE_SPACING", 2564d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 2565d752fcc8SJoe Perches $fix) { 2566f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 2567d752fcc8SJoe Perches } 2568d752fcc8SJoe Perches 2569365dd4eaSJoe Perches $last_blank_line = $linenr; 2570365dd4eaSJoe Perches } 2571365dd4eaSJoe Perches 25723b617e3bSJoe Perches# check for missing blank lines after declarations 25733f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 25743f7bac03SJoe Perches # actual declarations 25753f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 25765a4e1fd3SJoe Perches # function pointer declarations 25775a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 25783f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 25793f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 25803f7bac03SJoe Perches # known declaration macros 25813f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 25823f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 25833f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 25843f7bac03SJoe Perches # other possible extensions of declaration lines 25853f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 25863f7bac03SJoe Perches # not starting a section or a macro "\" extended line 25873f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 25883f7bac03SJoe Perches # looks like a declaration 25893f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 25905a4e1fd3SJoe Perches # function pointer declarations 25915a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 25923f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 25933f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 25943f7bac03SJoe Perches # known declaration macros 25953f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 25963f7bac03SJoe Perches # start of struct or union or enum 25973b617e3bSJoe Perches $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || 25983f7bac03SJoe Perches # start or end of block or continuation of declaration 25993f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 26003f7bac03SJoe Perches # bitfield continuation 26013f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 26023f7bac03SJoe Perches # other possible extensions of declaration lines 26033f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 26043f7bac03SJoe Perches # indentation of previous and current line are the same 26053f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 2606d752fcc8SJoe Perches if (WARN("LINE_SPACING", 2607d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 2608d752fcc8SJoe Perches $fix) { 2609f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 2610d752fcc8SJoe Perches } 26113b617e3bSJoe Perches } 26123b617e3bSJoe Perches 26135f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 26146b4c5bebSAndy Whitcroft# Exceptions: 26156b4c5bebSAndy Whitcroft# 1) within comments 26166b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 26176b4c5bebSAndy Whitcroft# 3) hanging labels 26183705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 26195f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 26203705ce5bSJoe Perches if (WARN("LEADING_SPACE", 26213705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 26223705ce5bSJoe Perches $fix) { 2623194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 26243705ce5bSJoe Perches } 26255f7ddae6SRaffaele Recalcati } 26265f7ddae6SRaffaele Recalcati 2627b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 2628b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 2629b9ea10d6SAndy Whitcroft 2630032a4c0fSJoe Perches# check indentation of any line with a bare else 2631840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 2632032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 2633032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 2634032a4c0fSJoe Perches my $tabs = length($1) + 1; 2635840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 2636840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 2637840080a0SJoe Perches defined $lines[$linenr] && 2638840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 2639032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 2640032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 2641032a4c0fSJoe Perches } 2642032a4c0fSJoe Perches } 2643032a4c0fSJoe Perches 2644c00df19aSJoe Perches# check indentation of a line with a break; 2645c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 2646c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 2647c00df19aSJoe Perches my $tabs = $1; 2648c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 2649c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 2650c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 2651c00df19aSJoe Perches } 2652c00df19aSJoe Perches } 2653c00df19aSJoe Perches 26541ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 26551ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 26561ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 26571ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 26581ba8dfd1SKees Cook } 26591ba8dfd1SKees Cook 2660c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 2661cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 2662000d1cc1SJoe Perches WARN("CVS_KEYWORD", 2663000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 2664c2fdda0dSAndy Whitcroft } 266522f2a2efSAndy Whitcroft 266642e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 266742e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 266842e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2669000d1cc1SJoe Perches ERROR("CSYNC", 2670000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 267142e41c54SMike Frysinger } 267242e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 267342e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2674000d1cc1SJoe Perches ERROR("SSYNC", 2675000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 267642e41c54SMike Frysinger } 267742e41c54SMike Frysinger 267856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 267956e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 268056e77d70SJoe Perches WARN("HOTPLUG_SECTION", 268156e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 268256e77d70SJoe Perches } 268356e77d70SJoe Perches 26849c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 26852b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 26862b474a1aSAndy Whitcroft $realline_next); 26873e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 26883e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 26891b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 2690170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2691f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2692171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 2693171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 2694171ae1a4SAndy Whitcroft 26953e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 26963e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 26973e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 26983e469cdcSAndy Whitcroft # until we hit end of it. 26993e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 27003e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 27013e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 27023e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 27033e469cdcSAndy Whitcroft } 2704f74bd194SAndy Whitcroft 27052b474a1aSAndy Whitcroft # Find the real next line. 27062b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 27072b474a1aSAndy Whitcroft if (defined $realline_next && 27082b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 27092b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 27102b474a1aSAndy Whitcroft $realline_next++; 27112b474a1aSAndy Whitcroft } 27122b474a1aSAndy Whitcroft 2713171ae1a4SAndy Whitcroft my $s = $stat; 2714171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 2715cf655043SAndy Whitcroft 2716c2fdda0dSAndy Whitcroft # Ignore goto labels. 2717171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 2718c2fdda0dSAndy Whitcroft 2719c2fdda0dSAndy Whitcroft # Ignore functions being called 2720171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 2721c2fdda0dSAndy Whitcroft 2722463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 2723463f2864SAndy Whitcroft 2724c45dcabdSAndy Whitcroft # declarations always start with types 2725d2506586SAndy 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) { 2726c45dcabdSAndy Whitcroft my $type = $1; 2727c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 2728c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 2729c45dcabdSAndy Whitcroft 27306c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 2731a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 2732c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 2733c2fdda0dSAndy Whitcroft } 27348905a67cSAndy Whitcroft 27356c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 273665863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 2737c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 27389c0ca6f9SAndy Whitcroft } 27398905a67cSAndy Whitcroft 27408905a67cSAndy Whitcroft # Check for any sort of function declaration. 27418905a67cSAndy Whitcroft # int foo(something bar, other baz); 27428905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 2743171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 27448905a67cSAndy Whitcroft my ($name_len) = length($1); 27458905a67cSAndy Whitcroft 2746cf655043SAndy Whitcroft my $ctx = $s; 2747773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 27488905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 2749cf655043SAndy Whitcroft 27508905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 2751c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 27528905a67cSAndy Whitcroft 2753c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 27548905a67cSAndy Whitcroft } 27558905a67cSAndy Whitcroft } 27568905a67cSAndy Whitcroft } 27578905a67cSAndy Whitcroft 27589c0ca6f9SAndy Whitcroft } 27599c0ca6f9SAndy Whitcroft 276000df344fSAndy Whitcroft# 276100df344fSAndy Whitcroft# Checks which may be anchored in the context. 276200df344fSAndy Whitcroft# 276300df344fSAndy Whitcroft 276400df344fSAndy Whitcroft# Check for switch () and associated case and default 276500df344fSAndy Whitcroft# statements should be at the same indent. 276600df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 276700df344fSAndy Whitcroft my $err = ''; 276800df344fSAndy Whitcroft my $sep = ''; 276900df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 277000df344fSAndy Whitcroft shift(@ctx); 277100df344fSAndy Whitcroft for my $ctx (@ctx) { 277200df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 277300df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 277400df344fSAndy Whitcroft $indent != $cindent) { 277500df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 277600df344fSAndy Whitcroft $sep = ''; 277700df344fSAndy Whitcroft } else { 277800df344fSAndy Whitcroft $sep = "[...]\n"; 277900df344fSAndy Whitcroft } 278000df344fSAndy Whitcroft } 278100df344fSAndy Whitcroft if ($err ne '') { 2782000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 2783000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 2784de7d4f0eSAndy Whitcroft } 2785de7d4f0eSAndy Whitcroft } 2786de7d4f0eSAndy Whitcroft 2787de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 2788de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 27890fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2790773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 2791773647a0SAndy Whitcroft 27929c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 27938eef05ddSJoe Perches 27948eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 27958eef05ddSJoe Perches WARN("DEEP_INDENTATION", 27968eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 27978eef05ddSJoe Perches } 27988eef05ddSJoe Perches 2799de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 2800de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 2801de7d4f0eSAndy Whitcroft 2802548596d5SAndy Whitcroft my $ctx_ln = $linenr; 2803548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 2804de7d4f0eSAndy Whitcroft 2805548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 2806548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 2807548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 2808548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 2809548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 2810773647a0SAndy Whitcroft $ctx_ln++; 2811773647a0SAndy Whitcroft } 2812548596d5SAndy Whitcroft 281353210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 281453210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 2815773647a0SAndy Whitcroft 2816773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 2817000d1cc1SJoe Perches ERROR("OPEN_BRACE", 2818000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 281901464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 282000df344fSAndy Whitcroft } 2821773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 2822773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 2823773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 2824773647a0SAndy Whitcroft { 28259c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 28269c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 2827000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 2828000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 282901464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 28309c0ca6f9SAndy Whitcroft } 28319c0ca6f9SAndy Whitcroft } 283200df344fSAndy Whitcroft } 283300df344fSAndy Whitcroft 28344d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 28350fe3dc2bSJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 28363e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 28373e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 28383e469cdcSAndy Whitcroft if (!defined $stat); 28394d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 28404d001e4dSAndy Whitcroft 28414d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 28424d001e4dSAndy Whitcroft 28434d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 28444d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 28454d001e4dSAndy Whitcroft # where necessary. 28464d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 28474d001e4dSAndy Whitcroft 28484d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 28496f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 28506f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 28514d001e4dSAndy Whitcroft 28524d001e4dSAndy Whitcroft # We want to check the first line inside the block 28534d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 28544d001e4dSAndy Whitcroft # 1) any blank line termination 28554d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 28564d001e4dSAndy Whitcroft # 3) any do (...) { 28574d001e4dSAndy Whitcroft my $continuation = 0; 28584d001e4dSAndy Whitcroft my $check = 0; 28594d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 28604d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 28614d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 28624d001e4dSAndy Whitcroft $continuation = 1; 28634d001e4dSAndy Whitcroft } 28649bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 28654d001e4dSAndy Whitcroft $check = 1; 28664d001e4dSAndy Whitcroft $cond_lines++; 28674d001e4dSAndy Whitcroft } 28684d001e4dSAndy Whitcroft 28694d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 28704d001e4dSAndy Whitcroft # preprocessor statement. 28714d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 28724d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 28734d001e4dSAndy Whitcroft $check = 0; 28744d001e4dSAndy Whitcroft } 28754d001e4dSAndy Whitcroft 28769bd49efeSAndy Whitcroft my $cond_ptr = -1; 2877740504c6SAndy Whitcroft $continuation = 0; 28789bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 28799bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 28804d001e4dSAndy Whitcroft 2881f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 2882f16fa28fSAndy Whitcroft # is not linear. 2883f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 2884f16fa28fSAndy Whitcroft $check = 0; 2885f16fa28fSAndy Whitcroft } 2886f16fa28fSAndy Whitcroft 28879bd49efeSAndy Whitcroft # Ignore: 28889bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 28899bd49efeSAndy Whitcroft # 2) preprocessor lines, and 28909bd49efeSAndy Whitcroft # 3) labels. 2891740504c6SAndy Whitcroft if ($continuation || 2892740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 28939bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 28949bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 2895740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 289630dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 28979bd49efeSAndy Whitcroft $cond_lines++; 28989bd49efeSAndy Whitcroft } 28994d001e4dSAndy Whitcroft } 290030dad6ebSAndy Whitcroft } 29014d001e4dSAndy Whitcroft 29024d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 29034d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 29044d001e4dSAndy Whitcroft 29054d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 29064d001e4dSAndy Whitcroft # this is not this patch's fault. 29074d001e4dSAndy Whitcroft if (!defined($stat_real) || 29084d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 29094d001e4dSAndy Whitcroft $check = 0; 29104d001e4dSAndy Whitcroft } 29114d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 29124d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 29134d001e4dSAndy Whitcroft } 29144d001e4dSAndy Whitcroft 29159bd49efeSAndy 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"; 29164d001e4dSAndy Whitcroft 29174d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 29184d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 2919000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 2920000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 29214d001e4dSAndy Whitcroft } 29224d001e4dSAndy Whitcroft } 29234d001e4dSAndy Whitcroft 29246c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 29256c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 29261f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 29271f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 29286c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 2929c2fdda0dSAndy Whitcroft if ($dbg_values) { 2930c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 2931cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 2932cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 29331f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 2934c2fdda0dSAndy Whitcroft } 29356c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 29366c72ffaaSAndy Whitcroft 293700df344fSAndy Whitcroft#ignore lines not being added 29383705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 293900df344fSAndy Whitcroft 2940653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 29417429c690SAndy Whitcroft if ($dbg_type) { 29427429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 2943000d1cc1SJoe Perches ERROR("TEST_TYPE", 2944000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 29457429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 2946000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 2947000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 29487429c690SAndy Whitcroft } 2949653d4876SAndy Whitcroft next; 2950653d4876SAndy Whitcroft } 2951a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 2952a1ef277eSAndy Whitcroft if ($dbg_attr) { 29539360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 2954000d1cc1SJoe Perches ERROR("TEST_ATTR", 2955000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 29569360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 2957000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 2958000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 2959a1ef277eSAndy Whitcroft } 2960a1ef277eSAndy Whitcroft next; 2961a1ef277eSAndy Whitcroft } 2962653d4876SAndy Whitcroft 2963f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 296499423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 296599423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 2966d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 2967d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 2968f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 2969f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 2970f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 2971d752fcc8SJoe Perches my $fixedline = $prevrawline; 2972d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 2973f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 2974d752fcc8SJoe Perches $fixedline = $line; 2975d752fcc8SJoe Perches $fixedline =~ s/^(.\s*){\s*/$1/; 2976f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 2977d752fcc8SJoe Perches } 2978f0a594c1SAndy Whitcroft } 2979f0a594c1SAndy Whitcroft 298000df344fSAndy Whitcroft# 298100df344fSAndy Whitcroft# Checks which are anchored on the added line. 298200df344fSAndy Whitcroft# 298300df344fSAndy Whitcroft 2984653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 2985c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 2986653d4876SAndy Whitcroft my $path = $1; 2987653d4876SAndy Whitcroft if ($path =~ m{//}) { 2988000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 2989495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 2990495e9d84SJoe Perches } 2991495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 2992495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 2993495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 2994653d4876SAndy Whitcroft } 2995653d4876SAndy Whitcroft } 2996653d4876SAndy Whitcroft 299700df344fSAndy Whitcroft# no C99 // comments 299800df344fSAndy Whitcroft if ($line =~ m{//}) { 29993705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 30003705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 30013705ce5bSJoe Perches $fix) { 3002194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 30033705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 30043705ce5bSJoe Perches my $comment = trim($1); 3005194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 30063705ce5bSJoe Perches } 30073705ce5bSJoe Perches } 300800df344fSAndy Whitcroft } 300900df344fSAndy Whitcroft # Remove C99 comments. 30100a920b5bSAndy Whitcroft $line =~ s@//.*@@; 30116c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 30120a920b5bSAndy Whitcroft 30132b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 30142b474a1aSAndy Whitcroft# the whole statement. 30152b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 30162b474a1aSAndy Whitcroft if (defined $realline_next && 30172b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 30182b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 30192b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 30202b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 30213cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 30223cbf62dfSAndy Whitcroft # a prefix: 30233cbf62dfSAndy Whitcroft # XXX(foo); 30243cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3025653d4876SAndy Whitcroft my $name = $1; 302687a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 30273cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 30283cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 30293cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 30303cbf62dfSAndy Whitcroft 30313cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 30322b474a1aSAndy Whitcroft \n.}\s*$| 303348012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 303448012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 303548012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 30362b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 30372b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 303848012058SAndy Whitcroft )/x) { 30392b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 30402b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 30412b474a1aSAndy Whitcroft } else { 30422b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 30430a920b5bSAndy Whitcroft } 30440a920b5bSAndy Whitcroft } 30452b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 30462b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 30472b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 30482b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 30492b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 30502b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 30512b474a1aSAndy Whitcroft } 30522b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 30532b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3054000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3055000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 30562b474a1aSAndy Whitcroft } 30570a920b5bSAndy Whitcroft 30585150bda4SJoe Eloff# check for global initialisers. 3059d5e616fcSJoe Perches if ($line =~ /^\+(\s*$Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/) { 3060d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 3061000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 3062d5e616fcSJoe Perches $herecurr) && 3063d5e616fcSJoe Perches $fix) { 3064194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/($Type\s*$Ident\s*(?:\s+$Modifier))*\s*=\s*(0|NULL|false)\s*;/$1;/; 3065d5e616fcSJoe Perches } 3066f0a594c1SAndy Whitcroft } 30670a920b5bSAndy Whitcroft# check for static initialisers. 3068d5e616fcSJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 3069d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 3070000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 3071d5e616fcSJoe Perches $herecurr) && 3072d5e616fcSJoe Perches $fix) { 3073194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; 3074d5e616fcSJoe Perches } 30750a920b5bSAndy Whitcroft } 30760a920b5bSAndy Whitcroft 30771813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 30781813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 30791813087dSJoe Perches my $tmp = trim($1); 30801813087dSJoe Perches WARN("MISORDERED_TYPE", 30811813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 30821813087dSJoe Perches } 30831813087dSJoe Perches 3084cb710ecaSJoe Perches# check for static const char * arrays. 3085cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3086000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3087000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3088cb710ecaSJoe Perches $herecurr); 3089cb710ecaSJoe Perches } 3090cb710ecaSJoe Perches 3091cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3092cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3093000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3094000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 3095cb710ecaSJoe Perches $herecurr); 3096cb710ecaSJoe Perches } 3097cb710ecaSJoe Perches 30989b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 30999b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 31009b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 31019b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 31029b0fa60dSJoe Perches $herecurr); 31039b0fa60dSJoe Perches } 31049b0fa60dSJoe Perches 3105b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 3106b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 3107b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 3108b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 3109b36190c5SJoe Perches $fix) { 3110194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 3111b36190c5SJoe Perches } 3112b36190c5SJoe Perches } 3113b36190c5SJoe Perches 311492e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE 311592e112fdSJoe Perches if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { 311692e112fdSJoe Perches if (WARN("DEFINE_PCI_DEVICE_TABLE", 311792e112fdSJoe Perches "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && 311892e112fdSJoe Perches $fix) { 3119194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /; 312092e112fdSJoe Perches } 312193ed0e2dSJoe Perches } 312293ed0e2dSJoe Perches 3123653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 3124653d4876SAndy Whitcroft# make sense. 3125653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 31268054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 3127c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 31288ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 3129653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 3130000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 3131000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 31320a920b5bSAndy Whitcroft } 31330a920b5bSAndy Whitcroft 31340a920b5bSAndy Whitcroft# * goes on variable not on type 313565863862SAndy Whitcroft # (char*[ const]) 3136bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 3137bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 31383705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 3139d8aaf121SAndy Whitcroft 314065863862SAndy Whitcroft # Should start with a space. 314165863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 314265863862SAndy Whitcroft # Should not end with a space. 314365863862SAndy Whitcroft $to =~ s/\s+$//; 314465863862SAndy Whitcroft # '*'s should not have spaces between. 3145f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 314665863862SAndy Whitcroft } 3147d8aaf121SAndy Whitcroft 31483705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 314965863862SAndy Whitcroft if ($from ne $to) { 31503705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 31513705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 31523705ce5bSJoe Perches $fix) { 31533705ce5bSJoe Perches my $sub_from = $ident; 31543705ce5bSJoe Perches my $sub_to = $ident; 31553705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3156194f66fcSJoe Perches $fixed[$fixlinenr] =~ 31573705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 31583705ce5bSJoe Perches } 315965863862SAndy Whitcroft } 3160bfcb2cc7SAndy Whitcroft } 3161bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 3162bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 31633705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 3164d8aaf121SAndy Whitcroft 316565863862SAndy Whitcroft # Should start with a space. 316665863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 316765863862SAndy Whitcroft # Should not end with a space. 316865863862SAndy Whitcroft $to =~ s/\s+$//; 316965863862SAndy Whitcroft # '*'s should not have spaces between. 3170f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 317165863862SAndy Whitcroft } 317265863862SAndy Whitcroft # Modifiers should have spaces. 317365863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 317465863862SAndy Whitcroft 31753705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 3176667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 31773705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 31783705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 31793705ce5bSJoe Perches $fix) { 31803705ce5bSJoe Perches 31813705ce5bSJoe Perches my $sub_from = $match; 31823705ce5bSJoe Perches my $sub_to = $match; 31833705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3184194f66fcSJoe Perches $fixed[$fixlinenr] =~ 31853705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 31863705ce5bSJoe Perches } 318765863862SAndy Whitcroft } 31880a920b5bSAndy Whitcroft } 31890a920b5bSAndy Whitcroft 31900a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 31910a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 31920a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 31930a920b5bSAndy Whitcroft# print "$herecurr"; 31940a920b5bSAndy Whitcroft# $clean = 0; 31950a920b5bSAndy Whitcroft# } 31960a920b5bSAndy Whitcroft 31978905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 3198000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 3199000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 32008905a67cSAndy Whitcroft } 32018905a67cSAndy Whitcroft 320217441227SJoe Perches# check for uses of printk_ratelimit 320317441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 3204000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 3205000d1cc1SJoe Perches"Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 320617441227SJoe Perches } 320717441227SJoe Perches 320800df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 320900df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 321000df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 321125985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 321200df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 3213f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 321400df344fSAndy Whitcroft my $ok = 0; 321500df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 321600df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 321725985edcSLucas De Marchi # we have a preceding printk if it ends 321800df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 321900df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 322000df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 322100df344fSAndy Whitcroft $ok = 1; 322200df344fSAndy Whitcroft } 322300df344fSAndy Whitcroft last; 322400df344fSAndy Whitcroft } 322500df344fSAndy Whitcroft } 322600df344fSAndy Whitcroft if ($ok == 0) { 3227000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 3228000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 32290a920b5bSAndy Whitcroft } 323000df344fSAndy Whitcroft } 32310a920b5bSAndy Whitcroft 3232243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 3233243f3803SJoe Perches my $orig = $1; 3234243f3803SJoe Perches my $level = lc($orig); 3235243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 32368f26b837SJoe Perches my $level2 = $level; 32378f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 3238243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 3239daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 3240243f3803SJoe Perches } 3241243f3803SJoe Perches 3242243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 3243d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 3244d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 3245d5e616fcSJoe Perches $fix) { 3246194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3247d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 3248d5e616fcSJoe Perches } 3249243f3803SJoe Perches } 3250243f3803SJoe Perches 3251dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 3252dc139313SJoe Perches my $orig = $1; 3253dc139313SJoe Perches my $level = lc($orig); 3254dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 3255dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 3256dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 3257dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 3258dc139313SJoe Perches } 3259dc139313SJoe Perches 3260653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 3261653d4876SAndy Whitcroft# or if closed on same line 32628d182478SJoe Perches if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and 3263c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 32648d182478SJoe Perches if (ERROR("OPEN_BRACE", 32658d182478SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr) && 32668d182478SJoe Perches $fix) { 32678d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 32688d182478SJoe Perches my $fixed_line = $rawline; 32698d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 32708d182478SJoe Perches my $line1 = $1; 32718d182478SJoe Perches my $line2 = $2; 32728d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 32738d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 32748d182478SJoe Perches if ($line2 !~ /^\s*$/) { 32758d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 32768d182478SJoe Perches } 32778d182478SJoe Perches } 32780a920b5bSAndy Whitcroft } 3279653d4876SAndy Whitcroft 32808905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 32818905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 32828905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 32838d182478SJoe Perches if (ERROR("OPEN_BRACE", 32848d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 32858d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 32868d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 32878d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 32888d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 32898d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 32908d182478SJoe Perches $fixedline = $rawline; 32918d182478SJoe Perches $fixedline =~ s/^(.\s*){\s*/$1\t/; 32928d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 32938d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 32948d182478SJoe Perches } 32958d182478SJoe Perches } 32968905a67cSAndy Whitcroft } 32978905a67cSAndy Whitcroft 32980c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 32993705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 33003705ce5bSJoe Perches if (WARN("SPACING", 33013705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 33023705ce5bSJoe Perches $fix) { 3303194f66fcSJoe Perches $fixed[$fixlinenr] =~ 33043705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 33053705ce5bSJoe Perches } 33060c73b4ebSAndy Whitcroft } 33070c73b4ebSAndy Whitcroft 330831070b5dSJoe Perches# Function pointer declarations 330931070b5dSJoe Perches# check spacing between type, funcptr, and args 331031070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 331191f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 331231070b5dSJoe Perches my $declare = $1; 331331070b5dSJoe Perches my $pre_pointer_space = $2; 331431070b5dSJoe Perches my $post_pointer_space = $3; 331531070b5dSJoe Perches my $funcname = $4; 331631070b5dSJoe Perches my $post_funcname_space = $5; 331731070b5dSJoe Perches my $pre_args_space = $6; 331831070b5dSJoe Perches 331991f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 332091f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 332191f72e9cSJoe Perches# don't need a space so don't warn for those. 332291f72e9cSJoe Perches my $post_declare_space = ""; 332391f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 332491f72e9cSJoe Perches $post_declare_space = $1; 332591f72e9cSJoe Perches $declare = rtrim($declare); 332691f72e9cSJoe Perches } 332791f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 332831070b5dSJoe Perches WARN("SPACING", 332931070b5dSJoe Perches "missing space after return type\n" . $herecurr); 333091f72e9cSJoe Perches $post_declare_space = " "; 333131070b5dSJoe Perches } 333231070b5dSJoe Perches 333331070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 333491f72e9cSJoe Perches# This test is not currently implemented because these declarations are 333591f72e9cSJoe Perches# equivalent to 333691f72e9cSJoe Perches# int foo(int bar, ...) 333791f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 333891f72e9cSJoe Perches# 333991f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 334091f72e9cSJoe Perches# WARN("SPACING", 334191f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 334291f72e9cSJoe Perches# } 334331070b5dSJoe Perches 334431070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 334531070b5dSJoe Perches if (defined $pre_pointer_space && 334631070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 334731070b5dSJoe Perches WARN("SPACING", 334831070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 334931070b5dSJoe Perches } 335031070b5dSJoe Perches 335131070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 335231070b5dSJoe Perches if (defined $post_pointer_space && 335331070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 335431070b5dSJoe Perches WARN("SPACING", 335531070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 335631070b5dSJoe Perches } 335731070b5dSJoe Perches 335831070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 335931070b5dSJoe Perches if (defined $post_funcname_space && 336031070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 336131070b5dSJoe Perches WARN("SPACING", 336231070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 336331070b5dSJoe Perches } 336431070b5dSJoe Perches 336531070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 336631070b5dSJoe Perches if (defined $pre_args_space && 336731070b5dSJoe Perches $pre_args_space =~ /^\s/) { 336831070b5dSJoe Perches WARN("SPACING", 336931070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 337031070b5dSJoe Perches } 337131070b5dSJoe Perches 337231070b5dSJoe Perches if (show_type("SPACING") && $fix) { 3373194f66fcSJoe Perches $fixed[$fixlinenr] =~ 337491f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 337531070b5dSJoe Perches } 337631070b5dSJoe Perches } 337731070b5dSJoe Perches 33788d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 33798d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 3380fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 3381fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 33828d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 33838d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 33848d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 3385fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 3386daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 33873705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 33883705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 33893705ce5bSJoe Perches $fix) { 3390194f66fcSJoe Perches $fixed[$fixlinenr] =~ 33913705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 33923705ce5bSJoe Perches } 33938d31cfceSAndy Whitcroft } 33948d31cfceSAndy Whitcroft } 33958d31cfceSAndy Whitcroft 3396f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 33976c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 3398c2fdda0dSAndy Whitcroft my $name = $1; 3399773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 3400773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 3401c2fdda0dSAndy Whitcroft 3402c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 3403773647a0SAndy Whitcroft if ($name =~ /^(?: 3404773647a0SAndy Whitcroft if|for|while|switch|return|case| 3405773647a0SAndy Whitcroft volatile|__volatile__| 3406773647a0SAndy Whitcroft __attribute__|format|__extension__| 3407773647a0SAndy Whitcroft asm|__asm__)$/x) 3408773647a0SAndy Whitcroft { 3409c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 3410c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 3411c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 3412c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 3413773647a0SAndy Whitcroft 3414773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 3415c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 3416c2fdda0dSAndy Whitcroft 3417c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 3418c2fdda0dSAndy Whitcroft # likely a typedef for a function. 3419773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 3420c2fdda0dSAndy Whitcroft 3421c2fdda0dSAndy Whitcroft } else { 34223705ce5bSJoe Perches if (WARN("SPACING", 34233705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 34243705ce5bSJoe Perches $fix) { 3425194f66fcSJoe Perches $fixed[$fixlinenr] =~ 34263705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 34273705ce5bSJoe Perches } 3428f0a594c1SAndy Whitcroft } 34296c72ffaaSAndy Whitcroft } 34309a4cad4eSEric Nelson 3431653d4876SAndy Whitcroft# Check operator spacing. 34320a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 34333705ce5bSJoe Perches my $fixed_line = ""; 34343705ce5bSJoe Perches my $line_fixed = 0; 34353705ce5bSJoe Perches 34369c0ca6f9SAndy Whitcroft my $ops = qr{ 34379c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 34389c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 34399c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 34401f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 344184731623SJoe Perches \?:|\?|: 34429c0ca6f9SAndy Whitcroft }x; 3443cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 34443705ce5bSJoe Perches 34453705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 34463705ce5bSJoe Perches## foreach my $el (@elements) { 34473705ce5bSJoe Perches## print("el: <$el>\n"); 34483705ce5bSJoe Perches## } 34493705ce5bSJoe Perches 34503705ce5bSJoe Perches my @fix_elements = (); 345100df344fSAndy Whitcroft my $off = 0; 34526c72ffaaSAndy Whitcroft 34533705ce5bSJoe Perches foreach my $el (@elements) { 34543705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 34553705ce5bSJoe Perches $off += length($el); 34563705ce5bSJoe Perches } 34573705ce5bSJoe Perches 34583705ce5bSJoe Perches $off = 0; 34593705ce5bSJoe Perches 34606c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 3461b34c648bSJoe Perches my $last_after = -1; 34626c72ffaaSAndy Whitcroft 34630a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 34643705ce5bSJoe Perches 34653705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 34663705ce5bSJoe Perches 34673705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 34683705ce5bSJoe Perches 34694a0df2efSAndy Whitcroft $off += length($elements[$n]); 34704a0df2efSAndy Whitcroft 347125985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 3472773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 3473773647a0SAndy Whitcroft my $cc = ''; 3474773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 3475773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 3476773647a0SAndy Whitcroft } 3477773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 3478773647a0SAndy Whitcroft 34794a0df2efSAndy Whitcroft my $a = ''; 34804a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 34814a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 3482cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 34834a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 34844a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 3485773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 34864a0df2efSAndy Whitcroft 34870a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 34884a0df2efSAndy Whitcroft 34894a0df2efSAndy Whitcroft my $c = ''; 34900a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 34914a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 34924a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 3493cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 34944a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 34954a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 34968b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 34974a0df2efSAndy Whitcroft } else { 34984a0df2efSAndy Whitcroft $c = 'E'; 34990a920b5bSAndy Whitcroft } 35000a920b5bSAndy Whitcroft 35014a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 35024a0df2efSAndy Whitcroft 35034a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 35044a0df2efSAndy Whitcroft 35056c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 3506de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 35070a920b5bSAndy Whitcroft 350874048ed8SAndy Whitcroft # Pull out the value of this operator. 35096c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 35100a920b5bSAndy Whitcroft 35111f65f947SAndy Whitcroft # Get the full operator variant. 35121f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 35131f65f947SAndy Whitcroft 351413214adfSAndy Whitcroft # Ignore operators passed as parameters. 351513214adfSAndy Whitcroft if ($op_type ne 'V' && 351613214adfSAndy Whitcroft $ca =~ /\s$/ && $cc =~ /^\s*,/) { 351713214adfSAndy Whitcroft 3518cf655043SAndy Whitcroft# # Ignore comments 3519cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 352013214adfSAndy Whitcroft 3521d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 352213214adfSAndy Whitcroft } elsif ($op eq ';') { 3523cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 3524cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 35253705ce5bSJoe Perches if (ERROR("SPACING", 35263705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 3527b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 35283705ce5bSJoe Perches $line_fixed = 1; 35293705ce5bSJoe Perches } 3530d8aaf121SAndy Whitcroft } 3531d8aaf121SAndy Whitcroft 3532d8aaf121SAndy Whitcroft # // is a comment 3533d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 35340a920b5bSAndy Whitcroft 3535b00e4814SJoe Perches # : when part of a bitfield 3536b00e4814SJoe Perches } elsif ($opv eq ':B') { 3537b00e4814SJoe Perches # skip the bitfield test for now 3538b00e4814SJoe Perches 35391f65f947SAndy Whitcroft # No spaces for: 35401f65f947SAndy Whitcroft # -> 3541b00e4814SJoe Perches } elsif ($op eq '->') { 35424a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 35433705ce5bSJoe Perches if (ERROR("SPACING", 35443705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 3545b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 35463705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 35473705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 35483705ce5bSJoe Perches } 3549b34c648bSJoe Perches $line_fixed = 1; 35503705ce5bSJoe Perches } 35510a920b5bSAndy Whitcroft } 35520a920b5bSAndy Whitcroft 35532381097bSJoe Perches # , must not have a space before and must have a space on the right. 35540a920b5bSAndy Whitcroft } elsif ($op eq ',') { 35552381097bSJoe Perches my $rtrim_before = 0; 35562381097bSJoe Perches my $space_after = 0; 35572381097bSJoe Perches if ($ctx =~ /Wx./) { 35582381097bSJoe Perches if (ERROR("SPACING", 35592381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 35602381097bSJoe Perches $line_fixed = 1; 35612381097bSJoe Perches $rtrim_before = 1; 35622381097bSJoe Perches } 35632381097bSJoe Perches } 3564cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 35653705ce5bSJoe Perches if (ERROR("SPACING", 35663705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 35673705ce5bSJoe Perches $line_fixed = 1; 3568b34c648bSJoe Perches $last_after = $n; 35692381097bSJoe Perches $space_after = 1; 35702381097bSJoe Perches } 35712381097bSJoe Perches } 35722381097bSJoe Perches if ($rtrim_before || $space_after) { 35732381097bSJoe Perches if ($rtrim_before) { 35742381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 35752381097bSJoe Perches } else { 35762381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 35772381097bSJoe Perches } 35782381097bSJoe Perches if ($space_after) { 35792381097bSJoe Perches $good .= " "; 35803705ce5bSJoe Perches } 35810a920b5bSAndy Whitcroft } 35820a920b5bSAndy Whitcroft 35839c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 358474048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 35859c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 35869c0ca6f9SAndy Whitcroft 35879c0ca6f9SAndy Whitcroft # unary operators should have a space before and 35889c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 35899c0ca6f9SAndy Whitcroft # unary operator, or a cast 35909c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 359174048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 35920d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 3593cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 35943705ce5bSJoe Perches if (ERROR("SPACING", 35953705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 3596b34c648bSJoe Perches if ($n != $last_after + 2) { 3597b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 35983705ce5bSJoe Perches $line_fixed = 1; 35993705ce5bSJoe Perches } 36000a920b5bSAndy Whitcroft } 3601b34c648bSJoe Perches } 3602a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 3603171ae1a4SAndy Whitcroft # A unary '*' may be const 3604171ae1a4SAndy Whitcroft 3605171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 36063705ce5bSJoe Perches if (ERROR("SPACING", 36073705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3608b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 36093705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 36103705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 36113705ce5bSJoe Perches } 3612b34c648bSJoe Perches $line_fixed = 1; 36133705ce5bSJoe Perches } 36140a920b5bSAndy Whitcroft } 36150a920b5bSAndy Whitcroft 36160a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 36170a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 3618773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 36193705ce5bSJoe Perches if (ERROR("SPACING", 36203705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 3621b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 36223705ce5bSJoe Perches $line_fixed = 1; 36233705ce5bSJoe Perches } 36240a920b5bSAndy Whitcroft } 3625773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 3626773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 36273705ce5bSJoe Perches if (ERROR("SPACING", 36283705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3629b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 36303705ce5bSJoe Perches $line_fixed = 1; 36313705ce5bSJoe Perches } 3632653d4876SAndy Whitcroft } 3633773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 36343705ce5bSJoe Perches if (ERROR("SPACING", 36353705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3636b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 36373705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 36383705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3639773647a0SAndy Whitcroft } 3640b34c648bSJoe Perches $line_fixed = 1; 36413705ce5bSJoe Perches } 36423705ce5bSJoe Perches } 36430a920b5bSAndy Whitcroft 36440a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 36459c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 36469c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 36479c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 3648c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 3649c2fdda0dSAndy Whitcroft $op eq '%') 36500a920b5bSAndy Whitcroft { 3651773647a0SAndy Whitcroft if ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 36523705ce5bSJoe Perches if (ERROR("SPACING", 36533705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 3654b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3655b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3656b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3657b34c648bSJoe Perches } 36583705ce5bSJoe Perches $line_fixed = 1; 36593705ce5bSJoe Perches } 36600a920b5bSAndy Whitcroft } 36610a920b5bSAndy Whitcroft 36621f65f947SAndy Whitcroft # A colon needs no spaces before when it is 36631f65f947SAndy Whitcroft # terminating a case value or a label. 36641f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 36651f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 36663705ce5bSJoe Perches if (ERROR("SPACING", 36673705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3668b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 36693705ce5bSJoe Perches $line_fixed = 1; 36703705ce5bSJoe Perches } 36711f65f947SAndy Whitcroft } 36721f65f947SAndy Whitcroft 36730a920b5bSAndy Whitcroft # All the others need spaces both sides. 3674cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 36751f65f947SAndy Whitcroft my $ok = 0; 36761f65f947SAndy Whitcroft 367722f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 36781f65f947SAndy Whitcroft if (($op eq '<' && 36791f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 36801f65f947SAndy Whitcroft ($op eq '>' && 36811f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 36821f65f947SAndy Whitcroft { 36831f65f947SAndy Whitcroft $ok = 1; 36841f65f947SAndy Whitcroft } 36851f65f947SAndy Whitcroft 368684731623SJoe Perches # messages are ERROR, but ?: are CHK 36871f65f947SAndy Whitcroft if ($ok == 0) { 368884731623SJoe Perches my $msg_type = \&ERROR; 368984731623SJoe Perches $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 369084731623SJoe Perches 369184731623SJoe Perches if (&{$msg_type}("SPACING", 36923705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 3693b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3694b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3695b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3696b34c648bSJoe Perches } 36973705ce5bSJoe Perches $line_fixed = 1; 36983705ce5bSJoe Perches } 36990a920b5bSAndy Whitcroft } 370022f2a2efSAndy Whitcroft } 37014a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 37023705ce5bSJoe Perches 37033705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 37043705ce5bSJoe Perches 37053705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 37060a920b5bSAndy Whitcroft } 37073705ce5bSJoe Perches 37083705ce5bSJoe Perches if (($#elements % 2) == 0) { 37093705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 37103705ce5bSJoe Perches } 37113705ce5bSJoe Perches 3712194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 3713194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 37143705ce5bSJoe Perches } 37153705ce5bSJoe Perches 37163705ce5bSJoe Perches 37170a920b5bSAndy Whitcroft } 37180a920b5bSAndy Whitcroft 3719786b6326SJoe Perches# check for whitespace before a non-naked semicolon 3720d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 3721786b6326SJoe Perches if (WARN("SPACING", 3722786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 3723786b6326SJoe Perches $fix) { 3724194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 3725786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 3726786b6326SJoe Perches } 3727786b6326SJoe Perches } 3728786b6326SJoe Perches 3729f0a594c1SAndy Whitcroft# check for multiple assignments 3730f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 3731000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 3732000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 3733f0a594c1SAndy Whitcroft } 3734f0a594c1SAndy Whitcroft 373522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 373622f2a2efSAndy Whitcroft## # continuation. 373722f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 373822f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 373922f2a2efSAndy Whitcroft## 374022f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 374122f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 374222f2a2efSAndy Whitcroft## my $ln = $line; 374322f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 374422f2a2efSAndy Whitcroft## } 374522f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 3746000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 3747000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 374822f2a2efSAndy Whitcroft## } 374922f2a2efSAndy Whitcroft## } 3750f0a594c1SAndy Whitcroft 37510a920b5bSAndy Whitcroft#need space before brace following if, while, etc 375222f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 375322f2a2efSAndy Whitcroft $line =~ /do{/) { 37543705ce5bSJoe Perches if (ERROR("SPACING", 37553705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 37563705ce5bSJoe Perches $fix) { 3757194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/; 37583705ce5bSJoe Perches } 3759de7d4f0eSAndy Whitcroft } 3760de7d4f0eSAndy Whitcroft 3761c4a62ef9SJoe Perches## # check for blank lines before declarations 3762c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 3763c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 3764c4a62ef9SJoe Perches## WARN("SPACING", 3765c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 3766c4a62ef9SJoe Perches## } 3767c4a62ef9SJoe Perches## 3768c4a62ef9SJoe Perches 3769de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 3770de7d4f0eSAndy Whitcroft# on the line 3771de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 3772d5e616fcSJoe Perches if (ERROR("SPACING", 3773d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 3774d5e616fcSJoe Perches $fix) { 3775194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3776d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 3777d5e616fcSJoe Perches } 37780a920b5bSAndy Whitcroft } 37790a920b5bSAndy Whitcroft 378022f2a2efSAndy Whitcroft# check spacing on square brackets 378122f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 37823705ce5bSJoe Perches if (ERROR("SPACING", 37833705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 37843705ce5bSJoe Perches $fix) { 3785194f66fcSJoe Perches $fixed[$fixlinenr] =~ 37863705ce5bSJoe Perches s/\[\s+/\[/; 37873705ce5bSJoe Perches } 378822f2a2efSAndy Whitcroft } 378922f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 37903705ce5bSJoe Perches if (ERROR("SPACING", 37913705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 37923705ce5bSJoe Perches $fix) { 3793194f66fcSJoe Perches $fixed[$fixlinenr] =~ 37943705ce5bSJoe Perches s/\s+\]/\]/; 37953705ce5bSJoe Perches } 379622f2a2efSAndy Whitcroft } 379722f2a2efSAndy Whitcroft 3798c45dcabdSAndy Whitcroft# check spacing on parentheses 37999c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 38009c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 38013705ce5bSJoe Perches if (ERROR("SPACING", 38023705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 38033705ce5bSJoe Perches $fix) { 3804194f66fcSJoe Perches $fixed[$fixlinenr] =~ 38053705ce5bSJoe Perches s/\(\s+/\(/; 38063705ce5bSJoe Perches } 380722f2a2efSAndy Whitcroft } 380813214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 3809c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 3810c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 38113705ce5bSJoe Perches if (ERROR("SPACING", 38123705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 38133705ce5bSJoe Perches $fix) { 3814194f66fcSJoe Perches $fixed[$fixlinenr] =~ 38153705ce5bSJoe Perches s/\s+\)/\)/; 38163705ce5bSJoe Perches } 381722f2a2efSAndy Whitcroft } 381822f2a2efSAndy Whitcroft 3819e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 3820e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 3821e2826fd0SJoe Perches 3822e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 3823ea4acbb1SJoe Perches my $var = $1; 3824ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 3825ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 3826ea4acbb1SJoe Perches $fix) { 3827ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 3828ea4acbb1SJoe Perches } 3829ea4acbb1SJoe Perches } 3830ea4acbb1SJoe Perches 3831ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 3832ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 3833ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 3834ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 3835ea4acbb1SJoe Perches my $var = $2; 3836ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 3837ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 3838ea4acbb1SJoe Perches $fix) { 3839ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 3840ea4acbb1SJoe Perches $var2 =~ s/\s//g; 3841ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 3842ea4acbb1SJoe Perches } 3843e2826fd0SJoe Perches } 3844e2826fd0SJoe Perches 38450a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 38464a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 38470a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 38483705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 38493705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 38503705ce5bSJoe Perches $fix) { 3851194f66fcSJoe Perches $fixed[$fixlinenr] =~ 38523705ce5bSJoe Perches s/^(.)\s+/$1/; 38533705ce5bSJoe Perches } 38540a920b5bSAndy Whitcroft } 38550a920b5bSAndy Whitcroft 38565b9553abSJoe Perches# return is not a function 3857507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 3858c45dcabdSAndy Whitcroft my $spacing = $1; 3859507e5141SJoe Perches if ($^V && $^V ge 5.10.0 && 38605b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 38615b9553abSJoe Perches my $value = $1; 38625b9553abSJoe Perches $value = deparenthesize($value); 38635b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 3864000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 3865000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 38665b9553abSJoe Perches } 3867c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 3868000d1cc1SJoe Perches ERROR("SPACING", 3869000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 3870c45dcabdSAndy Whitcroft } 3871c45dcabdSAndy Whitcroft } 3872507e5141SJoe Perches 3873b43ae21bSJoe Perches# unnecessary return in a void function 3874b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 3875b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 3876b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 3877b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 3878b43ae21bSJoe Perches $linenr >= 3 && 3879b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 3880b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 38819819cf25SJoe Perches WARN("RETURN_VOID", 3882b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 38839819cf25SJoe Perches } 38849819cf25SJoe Perches 3885189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 3886189248d8SJoe Perches if ($^V && $^V ge 5.10.0 && 3887189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 3888189248d8SJoe Perches my $openparens = $1; 3889189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 3890189248d8SJoe Perches my $msg = ""; 3891189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 3892189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 3893189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 3894189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 3895189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 3896189248d8SJoe Perches } 3897189248d8SJoe Perches } 3898189248d8SJoe Perches 389953a3c448SAndy Whitcroft# Return of what appears to be an errno should normally be -'ve 390053a3c448SAndy Whitcroft if ($line =~ /^.\s*return\s*(E[A-Z]*)\s*;/) { 390153a3c448SAndy Whitcroft my $name = $1; 390253a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 3903000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 3904000d1cc1SJoe Perches "return of an errno should typically be -ve (return -$1)\n" . $herecurr); 390553a3c448SAndy Whitcroft } 390653a3c448SAndy Whitcroft } 3907c45dcabdSAndy Whitcroft 39080a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 39094a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 39103705ce5bSJoe Perches if (ERROR("SPACING", 39113705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 39123705ce5bSJoe Perches $fix) { 3913194f66fcSJoe Perches $fixed[$fixlinenr] =~ 39143705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 39153705ce5bSJoe Perches } 39160a920b5bSAndy Whitcroft } 39170a920b5bSAndy Whitcroft 3918f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 3919f5fe35ddSAndy Whitcroft# statements after the conditional. 3920170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 39213e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 39223e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 39233e469cdcSAndy Whitcroft if (!defined $stat); 3924170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 3925170d3a22SAndy Whitcroft $remain_next, $off_next); 3926170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 3927170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 3928170d3a22SAndy Whitcroft 3929170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 3930170d3a22SAndy Whitcroft # If the statement carries leading newlines, 3931170d3a22SAndy Whitcroft # then count those as offsets. 3932170d3a22SAndy Whitcroft my ($whitespace) = 3933170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 3934170d3a22SAndy Whitcroft my $offset = 3935170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 3936170d3a22SAndy Whitcroft 3937170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 3938170d3a22SAndy Whitcroft $offset} = 1; 3939170d3a22SAndy Whitcroft } 3940170d3a22SAndy Whitcroft } 3941170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 3942c11230f4SJoe Perches defined($stat) && defined($cond) && 3943170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 3944171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 39458905a67cSAndy Whitcroft 3946b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 3947000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 3948000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 39498905a67cSAndy Whitcroft } 39508905a67cSAndy Whitcroft 39518905a67cSAndy Whitcroft # Find out what is on the end of the line after the 39528905a67cSAndy Whitcroft # conditional. 3953773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 39548905a67cSAndy Whitcroft $s =~ s/\n.*//g; 395513214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 395653210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 395753210168SAndy Whitcroft $c !~ /}\s*while\s*/) 3958773647a0SAndy Whitcroft { 3959bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 3960bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 3961bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 396242bdf74cSHidetoshi Seto my $stat_real = ''; 3963bb44ad39SAndy Whitcroft 396442bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 396542bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 3966bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 3967bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 3968bb44ad39SAndy Whitcroft } 3969bb44ad39SAndy Whitcroft 3970000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3971000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 39728905a67cSAndy Whitcroft } 39738905a67cSAndy Whitcroft } 39748905a67cSAndy Whitcroft 397513214adfSAndy Whitcroft# Check for bitwise tests written as boolean 397613214adfSAndy Whitcroft if ($line =~ / 397713214adfSAndy Whitcroft (?: 397813214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 397913214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 398013214adfSAndy Whitcroft (?:\&\&|\|\|) 398113214adfSAndy Whitcroft | 398213214adfSAndy Whitcroft (?:\&\&|\|\|) 398313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 398413214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 398513214adfSAndy Whitcroft )/x) 398613214adfSAndy Whitcroft { 3987000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 3988000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 398913214adfSAndy Whitcroft } 399013214adfSAndy Whitcroft 39918905a67cSAndy Whitcroft# if and else should not have general statements after it 399213214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 399313214adfSAndy Whitcroft my $s = $1; 399413214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 399513214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 3996000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 3997000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 39980a920b5bSAndy Whitcroft } 399913214adfSAndy Whitcroft } 400039667782SAndy Whitcroft# if should not continue a brace 400139667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4002000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4003048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 400439667782SAndy Whitcroft $herecurr); 400539667782SAndy Whitcroft } 4006a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4007a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4008a1080bf8SAndy Whitcroft $line !~ /\G(?: 40093fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4010a1080bf8SAndy Whitcroft \s*return\s+ 4011a1080bf8SAndy Whitcroft )/xg) 4012a1080bf8SAndy Whitcroft { 4013000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4014000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4015a1080bf8SAndy Whitcroft } 40160a920b5bSAndy Whitcroft 40170a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 40180a920b5bSAndy Whitcroft # indent level to be relevant to each other. 40198b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 40200a920b5bSAndy Whitcroft $previndent == $indent) { 40218b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 40228b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 40238b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 40248b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 40258b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 40268b8856f4SJoe Perches my $fixedline = $prevrawline; 40278b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 40288b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 40298b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 40308b8856f4SJoe Perches } 40318b8856f4SJoe Perches $fixedline = $rawline; 40328b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 40338b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 40348b8856f4SJoe Perches } 40350a920b5bSAndy Whitcroft } 40360a920b5bSAndy Whitcroft 40378b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 4038c2fdda0dSAndy Whitcroft $previndent == $indent) { 4039c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 4040c2fdda0dSAndy Whitcroft 4041c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 4042c2fdda0dSAndy Whitcroft # conditional. 4043773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 4044c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 4045c2fdda0dSAndy Whitcroft 4046c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 40478b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 40488b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 40498b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 40508b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 40518b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 40528b8856f4SJoe Perches my $fixedline = $prevrawline; 40538b8856f4SJoe Perches my $trailing = $rawline; 40548b8856f4SJoe Perches $trailing =~ s/^\+//; 40558b8856f4SJoe Perches $trailing = trim($trailing); 40568b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 40578b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 40588b8856f4SJoe Perches } 4059c2fdda0dSAndy Whitcroft } 4060c2fdda0dSAndy Whitcroft } 4061c2fdda0dSAndy Whitcroft 406295e2c602SJoe Perches#Specific variable tests 4063323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 4064323c1260SJoe Perches my $var = $1; 406595e2c602SJoe Perches 406695e2c602SJoe Perches#gcc binary extension 406795e2c602SJoe Perches if ($var =~ /^$Binary$/) { 4068d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 4069d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 4070d5e616fcSJoe Perches $fix) { 4071d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 4072194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4073d5e616fcSJoe Perches s/\b$var\b/$hexval/; 4074d5e616fcSJoe Perches } 407595e2c602SJoe Perches } 407695e2c602SJoe Perches 407795e2c602SJoe Perches#CamelCase 4078807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 4079be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 408022735ce8SJoe Perches#Ignore Page<foo> variants 4081807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 408222735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 4083f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && 4084f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 4085f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 40867e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 40877e781f67SJoe Perches my $word = $1; 40887e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 4089d8b07710SJoe Perches if ($check) { 4090d8b07710SJoe Perches seed_camelcase_includes(); 4091d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 4092d8b07710SJoe Perches seed_camelcase_file($realfile); 4093d8b07710SJoe Perches $camelcase_file_seeded = 1; 4094d8b07710SJoe Perches } 4095d8b07710SJoe Perches } 40967e781f67SJoe Perches if (!defined $camelcase{$word}) { 40977e781f67SJoe Perches $camelcase{$word} = 1; 4098be79794bSJoe Perches CHK("CAMELCASE", 40997e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 41007e781f67SJoe Perches } 4101323c1260SJoe Perches } 4102323c1260SJoe Perches } 41033445686aSJoe Perches } 41040a920b5bSAndy Whitcroft 41050a920b5bSAndy Whitcroft#no spaces allowed after \ in define 4106d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 4107d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 4108d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 4109d5e616fcSJoe Perches $fix) { 4110194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 4111d5e616fcSJoe Perches } 41120a920b5bSAndy Whitcroft } 41130a920b5bSAndy Whitcroft 4114653d4876SAndy Whitcroft#warn if <asm/foo.h> is #included and <linux/foo.h> is available (uses RAW line) 4115c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 4116e09dec48SAndy Whitcroft my $file = "$1.h"; 4117e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 4118e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 4119e09dec48SAndy Whitcroft $realfile ne $checkfile && 41207840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 4121c45dcabdSAndy Whitcroft { 4122e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 4123000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 4124000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4125e09dec48SAndy Whitcroft } else { 4126000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 4127000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4128e09dec48SAndy Whitcroft } 41290a920b5bSAndy Whitcroft } 41300a920b5bSAndy Whitcroft } 41310a920b5bSAndy Whitcroft 4132653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 4133653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 4134cf655043SAndy Whitcroft# in a known good container 4135b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 4136b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 4137d8aaf121SAndy Whitcroft my $ln = $linenr; 4138d8aaf121SAndy Whitcroft my $cnt = $realcnt; 4139c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 4140c45dcabdSAndy Whitcroft my $ctx = ''; 414108a2843eSJoe Perches my $has_flow_statement = 0; 414208a2843eSJoe Perches my $has_arg_concat = 0; 4143c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 4144f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 4145f74bd194SAndy Whitcroft $ctx = $dstat; 4146c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4147a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4148c45dcabdSAndy Whitcroft 414908a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 415008a2843eSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/); 415108a2843eSJoe Perches 4152f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 4153292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 4154c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 4155c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 4156c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 4157c45dcabdSAndy Whitcroft 4158c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 4159bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 4160bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 4161c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 4162bf30d6edSAndy Whitcroft { 4163c45dcabdSAndy Whitcroft } 4164c45dcabdSAndy Whitcroft 4165e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 4166e45bab8eSAndy Whitcroft while ($dstat =~ s/("X*")\s*$Ident/$1/ || 4167e45bab8eSAndy Whitcroft $dstat =~ s/$Ident\s*("X*")/$1/) 4168e45bab8eSAndy Whitcroft { 4169e45bab8eSAndy Whitcroft } 4170e45bab8eSAndy Whitcroft 4171c45dcabdSAndy Whitcroft my $exceptions = qr{ 4172c45dcabdSAndy Whitcroft $Declare| 4173c45dcabdSAndy Whitcroft module_param_named| 4174a0a0a7a9SKees Cook MODULE_PARM_DESC| 4175c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 4176c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 4177383099fdSAndy Whitcroft __typeof__\(| 417822fd2d3eSStefani Seibold union| 417922fd2d3eSStefani Seibold struct| 4180ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 4181ea71a0a0SAndy Whitcroft ^\"|\"$ 4182c45dcabdSAndy Whitcroft }x; 41835eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 4184f74bd194SAndy Whitcroft if ($dstat ne '' && 4185f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 4186f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 41873cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 4188356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 4189f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 4190f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 4191e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 419272f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 4193f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 4194f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 4195f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 4196f95a7e6aSJoe Perches $dstat !~ /^\({/ && # ({... 4197f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 4198c45dcabdSAndy Whitcroft { 4199f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 4200f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 4201f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 4202f74bd194SAndy Whitcroft 4203f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 4204f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 4205c45dcabdSAndy Whitcroft } 4206c45dcabdSAndy Whitcroft 4207f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 4208f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 4209f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 4210f74bd194SAndy Whitcroft } else { 4211000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 4212388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 4213d8aaf121SAndy Whitcroft } 42140a920b5bSAndy Whitcroft } 42155023d347SJoe Perches 421608a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 421708a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 421808a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 421908a2843eSJoe Perches my $herectx = $here . "\n"; 422008a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 422108a2843eSJoe Perches 422208a2843eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 422308a2843eSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 422408a2843eSJoe Perches } 422508a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 422608a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 422708a2843eSJoe Perches } 422808a2843eSJoe Perches 4229481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 42305023d347SJoe Perches 42315023d347SJoe Perches } else { 42325023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 4233481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 4234481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 42355023d347SJoe Perches $line =~ /^\+.*\\$/) { 42365023d347SJoe Perches WARN("LINE_CONTINUATIONS", 42375023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 42385023d347SJoe Perches } 4239653d4876SAndy Whitcroft } 42400a920b5bSAndy Whitcroft 4241b13edf7fSJoe Perches# do {} while (0) macro tests: 4242b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 4243b13edf7fSJoe Perches# macro should not end with a semicolon 4244b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 4245b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 4246b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 4247b13edf7fSJoe Perches my $ln = $linenr; 4248b13edf7fSJoe Perches my $cnt = $realcnt; 4249b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 4250b13edf7fSJoe Perches my $ctx = ''; 4251b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 4252b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 4253b13edf7fSJoe Perches $ctx = $dstat; 4254b13edf7fSJoe Perches 4255b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 4256b13edf7fSJoe Perches 4257b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 4258b13edf7fSJoe Perches my $stmts = $2; 4259b13edf7fSJoe Perches my $semis = $3; 4260b13edf7fSJoe Perches 4261b13edf7fSJoe Perches $ctx =~ s/\n*$//; 4262b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 4263b13edf7fSJoe Perches my $herectx = $here . "\n"; 4264b13edf7fSJoe Perches 4265b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4266b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4267b13edf7fSJoe Perches } 4268b13edf7fSJoe Perches 4269ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 4270ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 4271b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 4272b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 4273b13edf7fSJoe Perches } 4274b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 4275b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 4276b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 4277b13edf7fSJoe Perches } 4278f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 4279f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 4280f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 4281f5ef95b1SJoe Perches my $herectx = $here . "\n"; 4282f5ef95b1SJoe Perches 4283f5ef95b1SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4284f5ef95b1SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4285f5ef95b1SJoe Perches } 4286f5ef95b1SJoe Perches 4287f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 4288f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 4289b13edf7fSJoe Perches } 4290b13edf7fSJoe Perches } 4291b13edf7fSJoe Perches 4292080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 4293080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 4294080ba929SMike Frysinger# . 4295080ba929SMike Frysinger# ALIGN(...) 4296080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 4297080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 4298000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 4299000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 4300080ba929SMike Frysinger } 4301080ba929SMike Frysinger 4302f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 430313214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 430413214adfSAndy Whitcroft my ($level, $endln, @chunks) = 4305cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 430613214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 4307cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 4308cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 4309aad4f614SJoe Perches my @allowed = (); 4310aad4f614SJoe Perches my $allow = 0; 431113214adfSAndy Whitcroft my $seen = 0; 4312773647a0SAndy Whitcroft my $herectx = $here . "\n"; 4313cf655043SAndy Whitcroft my $ln = $linenr - 1; 431413214adfSAndy Whitcroft for my $chunk (@chunks) { 431513214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 431613214adfSAndy Whitcroft 4317773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 4318773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 4319773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 4320773647a0SAndy Whitcroft 4321aad4f614SJoe Perches $allowed[$allow] = 0; 4322773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 4323773647a0SAndy Whitcroft 4324773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 4325773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 4326773647a0SAndy Whitcroft 4327773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 4328cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 4329cf655043SAndy Whitcroft 4330773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 433113214adfSAndy Whitcroft 433213214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 433313214adfSAndy Whitcroft 4334aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 4335cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 4336cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 4337aad4f614SJoe Perches $allowed[$allow] = 1; 433813214adfSAndy Whitcroft } 433913214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 4340cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 4341aad4f614SJoe Perches $allowed[$allow] = 1; 434213214adfSAndy Whitcroft } 4343cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 4344cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 4345aad4f614SJoe Perches $allowed[$allow] = 1; 434613214adfSAndy Whitcroft } 4347aad4f614SJoe Perches $allow++; 434813214adfSAndy Whitcroft } 4349aad4f614SJoe Perches if ($seen) { 4350aad4f614SJoe Perches my $sum_allowed = 0; 4351aad4f614SJoe Perches foreach (@allowed) { 4352aad4f614SJoe Perches $sum_allowed += $_; 4353aad4f614SJoe Perches } 4354aad4f614SJoe Perches if ($sum_allowed == 0) { 4355000d1cc1SJoe Perches WARN("BRACES", 4356000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 4357aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 4358aad4f614SJoe Perches $seen != $allow) { 4359aad4f614SJoe Perches CHK("BRACES", 4360aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 4361aad4f614SJoe Perches } 436213214adfSAndy Whitcroft } 436313214adfSAndy Whitcroft } 436413214adfSAndy Whitcroft } 4365773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 436613214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 4367cf655043SAndy Whitcroft my $allowed = 0; 4368f0a594c1SAndy Whitcroft 4369cf655043SAndy Whitcroft # Check the pre-context. 4370cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 4371cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 4372cf655043SAndy Whitcroft $allowed = 1; 4373f0a594c1SAndy Whitcroft } 4374773647a0SAndy Whitcroft 4375773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 4376773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 4377773647a0SAndy Whitcroft 4378cf655043SAndy Whitcroft # Check the condition. 4379cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 4380773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 4381cf655043SAndy Whitcroft if (defined $cond) { 4382773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 4383cf655043SAndy Whitcroft } 4384cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 4385cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 4386cf655043SAndy Whitcroft $allowed = 1; 4387cf655043SAndy Whitcroft } 4388cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 4389cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 4390cf655043SAndy Whitcroft $allowed = 1; 4391cf655043SAndy Whitcroft } 4392cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 4393cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 4394cf655043SAndy Whitcroft $allowed = 1; 4395cf655043SAndy Whitcroft } 4396cf655043SAndy Whitcroft # Check the post-context. 4397cf655043SAndy Whitcroft if (defined $chunks[1]) { 4398cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 4399cf655043SAndy Whitcroft if (defined $cond) { 4400773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 4401cf655043SAndy Whitcroft } 4402cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 4403cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 4404cf655043SAndy Whitcroft $allowed = 1; 4405cf655043SAndy Whitcroft } 4406cf655043SAndy Whitcroft } 4407cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 440869932487SJustin P. Mattock my $herectx = $here . "\n"; 4409f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 4410cf655043SAndy Whitcroft 4411f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 441269932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 4413cf655043SAndy Whitcroft } 4414cf655043SAndy Whitcroft 4415000d1cc1SJoe Perches WARN("BRACES", 4416000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 4417f0a594c1SAndy Whitcroft } 4418f0a594c1SAndy Whitcroft } 4419f0a594c1SAndy Whitcroft 44200979ae66SJoe Perches# check for unnecessary blank lines around braces 442177b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 44220979ae66SJoe Perches CHK("BRACES", 44230979ae66SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev); 44240979ae66SJoe Perches } 442577b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 44260979ae66SJoe Perches CHK("BRACES", 44270979ae66SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev); 44280979ae66SJoe Perches } 44290979ae66SJoe Perches 44304a0df2efSAndy Whitcroft# no volatiles please 44316c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 44326c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 4433000d1cc1SJoe Perches WARN("VOLATILE", 4434000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 44354a0df2efSAndy Whitcroft } 44364a0df2efSAndy Whitcroft 44375e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 44385e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 44395e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 44405e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 44415e4f6ba5SJoe Perches if ($line =~ /^\+\s*"[X\t]*"/ && 44425e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 44435e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 44445e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 44455e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 44465e4f6ba5SJoe Perches $fix && 44475e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 44485e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 44495e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 44505e4f6ba5SJoe Perches my $comma_close = ""; 44515e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 44525e4f6ba5SJoe Perches $comma_close = $1; 44535e4f6ba5SJoe Perches } 44545e4f6ba5SJoe Perches 44555e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 44565e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 44575e4f6ba5SJoe Perches my $fixedline = $prevrawline; 44585e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 44595e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 44605e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 44615e4f6ba5SJoe Perches $fixedline = $rawline; 44625e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 44635e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 44645e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 44655e4f6ba5SJoe Perches } 44665e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 44675e4f6ba5SJoe Perches } 44685e4f6ba5SJoe Perches } 44695e4f6ba5SJoe Perches 44705e4f6ba5SJoe Perches# check for missing a space in a string concatenation 44715e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 44725e4f6ba5SJoe Perches WARN('MISSING_SPACE', 44735e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 44745e4f6ba5SJoe Perches } 44755e4f6ba5SJoe Perches 44765e4f6ba5SJoe Perches# check for spaces before a quoted newline 44775e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 44785e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 44795e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 44805e4f6ba5SJoe Perches $fix) { 44815e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 44825e4f6ba5SJoe Perches } 44835e4f6ba5SJoe Perches 44845e4f6ba5SJoe Perches } 44855e4f6ba5SJoe Perches 4486f17dba4fSJoe Perches# concatenated string without spaces between elements 4487f17dba4fSJoe Perches if ($line =~ /"X+"[A-Z_]+/ || $line =~ /[A-Z_]+"X+"/) { 4488f17dba4fSJoe Perches CHK("CONCATENATED_STRING", 4489f17dba4fSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr); 4490f17dba4fSJoe Perches } 4491f17dba4fSJoe Perches 449290ad30e5SJoe Perches# uncoalesced string fragments 449390ad30e5SJoe Perches if ($line =~ /"X*"\s*"/) { 449490ad30e5SJoe Perches WARN("STRING_FRAGMENTS", 449590ad30e5SJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr); 449690ad30e5SJoe Perches } 449790ad30e5SJoe Perches 44985e4f6ba5SJoe Perches# check for %L{u,d,i} in strings 44995e4f6ba5SJoe Perches my $string; 45005e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 45015e4f6ba5SJoe Perches $string = substr($rawline, $-[1], $+[1] - $-[1]); 45025e4f6ba5SJoe Perches $string =~ s/%%/__/g; 45035e4f6ba5SJoe Perches if ($string =~ /(?<!%)%L[udi]/) { 45045e4f6ba5SJoe Perches WARN("PRINTF_L", 45055e4f6ba5SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 45065e4f6ba5SJoe Perches last; 45075e4f6ba5SJoe Perches } 45085e4f6ba5SJoe Perches } 45095e4f6ba5SJoe Perches 45105e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 45115e4f6ba5SJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 45125e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 45135e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 45145e4f6ba5SJoe Perches } 45155e4f6ba5SJoe Perches 451600df344fSAndy Whitcroft# warn about #if 0 4517c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 4518000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 4519000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 4520de7d4f0eSAndy Whitcroft $herecurr); 45214a0df2efSAndy Whitcroft } 45224a0df2efSAndy Whitcroft 452303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 452403df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 452503df4b51SAndy Whitcroft my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 452603df4b51SAndy Whitcroft if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 452703df4b51SAndy Whitcroft WARN('NEEDLESS_IF', 452815160f90SFabio Estevam "$1(NULL) is safe and this check is probably not required\n" . $hereprev); 45294c432a8fSGreg Kroah-Hartman } 45304c432a8fSGreg Kroah-Hartman } 4531f0a594c1SAndy Whitcroft 4532ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 4533ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 4534ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 4535ebfdc409SJoe Perches (defined $1 || defined $3) && 4536ebfdc409SJoe Perches $linenr > 3) { 4537ebfdc409SJoe Perches my $testval = $2; 4538ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 4539ebfdc409SJoe Perches 4540ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 4541ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 4542ebfdc409SJoe Perches 4543ebfdc409SJoe Perches if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) { 4544ebfdc409SJoe Perches WARN("OOM_MESSAGE", 4545ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 4546ebfdc409SJoe Perches } 4547ebfdc409SJoe Perches } 4548ebfdc409SJoe Perches 4549f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 4550f78d98f6SJoe Perches if ($line !~ /printk\s*\(/ && 4551f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 4552f78d98f6SJoe Perches my $level = $1; 4553f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 4554f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 4555f78d98f6SJoe Perches $fix) { 4556f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 4557f78d98f6SJoe Perches } 4558f78d98f6SJoe Perches } 4559f78d98f6SJoe Perches 4560abb08a53SJoe Perches# check for mask then right shift without a parentheses 4561abb08a53SJoe Perches if ($^V && $^V ge 5.10.0 && 4562abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 4563abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 4564abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 4565abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 4566abb08a53SJoe Perches } 4567abb08a53SJoe Perches 4568b75ac618SJoe Perches# check for pointer comparisons to NULL 4569b75ac618SJoe Perches if ($^V && $^V ge 5.10.0) { 4570b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 4571b75ac618SJoe Perches my $val = $1; 4572b75ac618SJoe Perches my $equal = "!"; 4573b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 4574b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 4575b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 4576b75ac618SJoe Perches $fix) { 4577b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 4578b75ac618SJoe Perches } 4579b75ac618SJoe Perches } 4580b75ac618SJoe Perches } 4581b75ac618SJoe Perches 45828716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 45838716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 45848716de38SJoe Perches my $attr = $1; 45858716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 45868716de38SJoe Perches my $ptr = $1; 45878716de38SJoe Perches my $var = $2; 45888716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 45898716de38SJoe Perches ERROR("MISPLACED_INIT", 45908716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 45918716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 45928716de38SJoe Perches WARN("MISPLACED_INIT", 45938716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 45948716de38SJoe Perches $fix) { 4595194f66fcSJoe Perches $fixed[$fixlinenr] =~ 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; 45968716de38SJoe Perches } 45978716de38SJoe Perches } 45988716de38SJoe Perches } 45998716de38SJoe Perches 4600e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 4601e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 4602e970b884SJoe Perches my $attr = $1; 4603e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 4604e970b884SJoe Perches my $attr_prefix = $1; 4605e970b884SJoe Perches my $attr_type = $2; 4606e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 4607e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 4608e970b884SJoe Perches $fix) { 4609194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4610e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 4611e970b884SJoe Perches } 4612e970b884SJoe Perches } 4613e970b884SJoe Perches 4614e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 4615e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 4616e970b884SJoe Perches my $attr = $1; 4617e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 4618e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 4619e970b884SJoe Perches $fix) { 4620194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 4621e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 4622e970b884SJoe Perches $lead = rtrim($1); 4623e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 4624e970b884SJoe Perches $lead = "${lead}const "; 4625194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 4626e970b884SJoe Perches } 4627e970b884SJoe Perches } 4628e970b884SJoe Perches 4629fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 4630fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 4631fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 4632fbdb8138SJoe Perches my $constant_func = $1; 4633fbdb8138SJoe Perches my $func = $constant_func; 4634fbdb8138SJoe Perches $func =~ s/^__constant_//; 4635fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 4636fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 4637fbdb8138SJoe Perches $fix) { 4638194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 4639fbdb8138SJoe Perches } 4640fbdb8138SJoe Perches } 4641fbdb8138SJoe Perches 46421a15a250SPatrick Pannuto# prefer usleep_range over udelay 464337581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 464443c1d77cSJoe Perches my $delay = $1; 46451a15a250SPatrick Pannuto # ignore udelay's < 10, however 464643c1d77cSJoe Perches if (! ($delay < 10) ) { 4647000d1cc1SJoe Perches CHK("USLEEP_RANGE", 464843c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 464943c1d77cSJoe Perches } 465043c1d77cSJoe Perches if ($delay > 2000) { 465143c1d77cSJoe Perches WARN("LONG_UDELAY", 465243c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 46531a15a250SPatrick Pannuto } 46541a15a250SPatrick Pannuto } 46551a15a250SPatrick Pannuto 465609ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 465709ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 465809ef8725SPatrick Pannuto if ($1 < 20) { 4659000d1cc1SJoe Perches WARN("MSLEEP", 466043c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 466109ef8725SPatrick Pannuto } 466209ef8725SPatrick Pannuto } 466309ef8725SPatrick Pannuto 466436ec1939SJoe Perches# check for comparisons of jiffies 466536ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 466636ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 466736ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 466836ec1939SJoe Perches } 466936ec1939SJoe Perches 46709d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 46719d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 46729d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 46739d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 46749d7a34a5SJoe Perches } 46759d7a34a5SJoe Perches 467600df344fSAndy Whitcroft# warn about #ifdefs in C files 4677c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 467800df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 467900df344fSAndy Whitcroft# print "$herecurr"; 468000df344fSAndy Whitcroft# $clean = 0; 468100df344fSAndy Whitcroft# } 468200df344fSAndy Whitcroft 468322f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 4684c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 46853705ce5bSJoe Perches if (ERROR("SPACING", 46863705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 46873705ce5bSJoe Perches $fix) { 4688194f66fcSJoe Perches $fixed[$fixlinenr] =~ 46893705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 46903705ce5bSJoe Perches } 46913705ce5bSJoe Perches 469222f2a2efSAndy Whitcroft } 469322f2a2efSAndy Whitcroft 46944a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 4695171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 4696171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 46974a0df2efSAndy Whitcroft my $which = $1; 46984a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 4699000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 4700000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 47014a0df2efSAndy Whitcroft } 47024a0df2efSAndy Whitcroft } 47034a0df2efSAndy Whitcroft# check for memory barriers without a comment. 47044a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 47054a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 4706c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 4707000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 47084a0df2efSAndy Whitcroft } 47094a0df2efSAndy Whitcroft } 47104a0df2efSAndy Whitcroft# check of hardware specific defines 4711c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 4712000d1cc1SJoe Perches CHK("ARCH_DEFINES", 4713000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 47140a920b5bSAndy Whitcroft } 4715653d4876SAndy Whitcroft 4716d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 4717d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 4718000d1cc1SJoe Perches WARN("STORAGE_CLASS", 4719000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 4720d4977c78STobias Klauser } 4721d4977c78STobias Klauser 4722de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 4723de7d4f0eSAndy Whitcroft# storage class and type. 47249c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 47259c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 4726000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 4727000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 4728de7d4f0eSAndy Whitcroft } 4729de7d4f0eSAndy Whitcroft 47308905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 47312b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 47322b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 4733d5e616fcSJoe Perches if (WARN("INLINE", 4734d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 4735d5e616fcSJoe Perches $fix) { 4736194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 4737d5e616fcSJoe Perches 4738d5e616fcSJoe Perches } 47398905a67cSAndy Whitcroft } 47408905a67cSAndy Whitcroft 47413d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 47422b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 47432b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 4744000d1cc1SJoe Perches WARN("PREFER_PACKED", 4745000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 47463d130fd0SJoe Perches } 47473d130fd0SJoe Perches 474839b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 47492b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 47502b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 4751000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 4752000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 475339b7e287SJoe Perches } 475439b7e287SJoe Perches 47555f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 47562b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 47572b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 4758d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 4759d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 4760d5e616fcSJoe Perches $fix) { 4761194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 4762d5e616fcSJoe Perches 4763d5e616fcSJoe Perches } 47645f14d3bdSJoe Perches } 47655f14d3bdSJoe Perches 47666061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 47672b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 47682b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 4769d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 4770d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 4771d5e616fcSJoe Perches $fix) { 4772194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 4773d5e616fcSJoe Perches } 47746061d949SJoe Perches } 47756061d949SJoe Perches 4776619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 4777619a908aSJoe Perches if ($^V && $^V ge 5.10.0 && 4778619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 4779619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 4780619a908aSJoe Perches $line =~ /\b__weak\b/)) { 4781619a908aSJoe Perches ERROR("WEAK_DECLARATION", 4782619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 4783619a908aSJoe Perches } 4784619a908aSJoe Perches 47858f53a9b8SJoe Perches# check for sizeof(&) 47868f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 4787000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 4788000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 47898f53a9b8SJoe Perches } 47908f53a9b8SJoe Perches 479166c80b60SJoe Perches# check for sizeof without parenthesis 479266c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 4793d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 4794d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 4795d5e616fcSJoe Perches $fix) { 4796194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 4797d5e616fcSJoe Perches } 479866c80b60SJoe Perches } 479966c80b60SJoe Perches 480088982feaSJoe Perches# check for struct spinlock declarations 480188982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 480288982feaSJoe Perches WARN("USE_SPINLOCK_T", 480388982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 480488982feaSJoe Perches } 480588982feaSJoe Perches 4806a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 480706668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 4808a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 480906668727SJoe Perches if ($fmt ne "" && $fmt !~ /[^\\]\%/) { 4810d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 4811d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 4812d5e616fcSJoe Perches $fix) { 4813194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 4814d5e616fcSJoe Perches } 4815a6962d72SJoe Perches } 4816a6962d72SJoe Perches } 4817a6962d72SJoe Perches 4818554e165cSAndy Whitcroft# Check for misused memsets 4819d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 4820d1fe9c09SJoe Perches defined $stat && 4821d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 4822554e165cSAndy Whitcroft 4823d7c76ba7SJoe Perches my $ms_addr = $2; 4824d1fe9c09SJoe Perches my $ms_val = $7; 4825d1fe9c09SJoe Perches my $ms_size = $12; 4826d7c76ba7SJoe Perches 4827554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 4828554e165cSAndy Whitcroft ERROR("MEMSET", 4829d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 4830554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 4831554e165cSAndy Whitcroft WARN("MEMSET", 4832d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 4833d7c76ba7SJoe Perches } 4834d7c76ba7SJoe Perches } 4835d7c76ba7SJoe Perches 483698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 483798a9bba5SJoe Perches if ($^V && $^V ge 5.10.0 && 483898a9bba5SJoe Perches $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) { 483998a9bba5SJoe Perches if (WARN("PREFER_ETHER_ADDR_COPY", 484098a9bba5SJoe Perches "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) && 484198a9bba5SJoe Perches $fix) { 4842194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 484398a9bba5SJoe Perches } 484498a9bba5SJoe Perches } 484598a9bba5SJoe Perches 4846d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 4847d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 4848d1fe9c09SJoe Perches defined $stat && 4849d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 4850d1fe9c09SJoe Perches if (defined $2 || defined $7) { 4851d7c76ba7SJoe Perches my $call = $1; 4852d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 4853d7c76ba7SJoe Perches my $arg1 = $3; 4854d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 4855d1fe9c09SJoe Perches my $arg2 = $8; 4856d7c76ba7SJoe Perches my $cast; 4857d7c76ba7SJoe Perches 4858d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 4859d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 4860d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 4861d7c76ba7SJoe Perches $cast = $cast1; 4862d7c76ba7SJoe Perches } else { 4863d7c76ba7SJoe Perches $cast = $cast2; 4864d7c76ba7SJoe Perches } 4865d7c76ba7SJoe Perches WARN("MINMAX", 4866d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 4867554e165cSAndy Whitcroft } 4868554e165cSAndy Whitcroft } 4869554e165cSAndy Whitcroft 48704a273195SJoe Perches# check usleep_range arguments 48714a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 48724a273195SJoe Perches defined $stat && 48734a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 48744a273195SJoe Perches my $min = $1; 48754a273195SJoe Perches my $max = $7; 48764a273195SJoe Perches if ($min eq $max) { 48774a273195SJoe Perches WARN("USLEEP_RANGE", 48784a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 48794a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 48804a273195SJoe Perches $min > $max) { 48814a273195SJoe Perches WARN("USLEEP_RANGE", 48824a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 48834a273195SJoe Perches } 48844a273195SJoe Perches } 48854a273195SJoe Perches 4886823b794cSJoe Perches# check for naked sscanf 4887823b794cSJoe Perches if ($^V && $^V ge 5.10.0 && 4888823b794cSJoe Perches defined $stat && 48896c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 4890823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 4891823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 4892823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 4893823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 4894823b794cSJoe Perches $lc = $lc + $linenr; 4895823b794cSJoe Perches my $stat_real = raw_line($linenr, 0); 4896823b794cSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 4897823b794cSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 4898823b794cSJoe Perches } 4899823b794cSJoe Perches WARN("NAKED_SSCANF", 4900823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 4901823b794cSJoe Perches } 4902823b794cSJoe Perches 4903afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 4904afc819abSJoe Perches if ($^V && $^V ge 5.10.0 && 4905afc819abSJoe Perches defined $stat && 4906afc819abSJoe Perches $line =~ /\bsscanf\b/) { 4907afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 4908afc819abSJoe Perches $lc = $lc + $linenr; 4909afc819abSJoe Perches my $stat_real = raw_line($linenr, 0); 4910afc819abSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 4911afc819abSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 4912afc819abSJoe Perches } 4913afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 4914afc819abSJoe Perches my $format = $6; 4915afc819abSJoe Perches my $count = $format =~ tr@%@%@; 4916afc819abSJoe Perches if ($count == 1 && 4917afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 4918afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 4919afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 4920afc819abSJoe Perches } 4921afc819abSJoe Perches } 4922afc819abSJoe Perches } 4923afc819abSJoe Perches 492470dc8a48SJoe Perches# check for new externs in .h files. 492570dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 492670dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 4927d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 492870dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 492970dc8a48SJoe Perches $fix) { 4930194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 493170dc8a48SJoe Perches } 493270dc8a48SJoe Perches } 493370dc8a48SJoe Perches 4934de7d4f0eSAndy Whitcroft# check for new externs in .c files. 4935171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 4936c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 4937171ae1a4SAndy Whitcroft { 4938c45dcabdSAndy Whitcroft my $function_name = $1; 4939c45dcabdSAndy Whitcroft my $paren_space = $2; 4940171ae1a4SAndy Whitcroft 4941171ae1a4SAndy Whitcroft my $s = $stat; 4942171ae1a4SAndy Whitcroft if (defined $cond) { 4943171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 4944171ae1a4SAndy Whitcroft } 4945c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 4946c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 4947c45dcabdSAndy Whitcroft { 4948000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 4949000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 4950de7d4f0eSAndy Whitcroft } 4951de7d4f0eSAndy Whitcroft 4952171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 4953000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 4954000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 4955171ae1a4SAndy Whitcroft } 49569c9ba34eSAndy Whitcroft 49579c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 49589c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 49599c9ba34eSAndy Whitcroft { 4960000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 4961000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 4962171ae1a4SAndy Whitcroft } 4963171ae1a4SAndy Whitcroft 4964de7d4f0eSAndy Whitcroft# checks for new __setup's 4965de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 4966de7d4f0eSAndy Whitcroft my $name = $1; 4967de7d4f0eSAndy Whitcroft 4968de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 4969000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 4970000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 4971de7d4f0eSAndy Whitcroft } 4972653d4876SAndy Whitcroft } 49739c0ca6f9SAndy Whitcroft 49749c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 4975caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 4976000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 4977000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 49789c0ca6f9SAndy Whitcroft } 497913214adfSAndy Whitcroft 4980a640d25cSJoe Perches# alloc style 4981a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 4982a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 4983a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 4984a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 4985a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 4986a640d25cSJoe Perches } 4987a640d25cSJoe Perches 498860a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 498960a55369SJoe Perches if ($^V && $^V ge 5.10.0 && 4990e367455aSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 499160a55369SJoe Perches my $oldfunc = $3; 499260a55369SJoe Perches my $a1 = $4; 499360a55369SJoe Perches my $a2 = $10; 499460a55369SJoe Perches my $newfunc = "kmalloc_array"; 499560a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 499660a55369SJoe Perches my $r1 = $a1; 499760a55369SJoe Perches my $r2 = $a2; 499860a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 499960a55369SJoe Perches $r1 = $a2; 500060a55369SJoe Perches $r2 = $a1; 500160a55369SJoe Perches } 5002e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 5003e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 5004e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 5005e367455aSJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) && 5006e367455aSJoe Perches $fix) { 5007194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e; 500860a55369SJoe Perches 500960a55369SJoe Perches } 501060a55369SJoe Perches } 501160a55369SJoe Perches } 501260a55369SJoe Perches 5013972fdea2SJoe Perches# check for krealloc arg reuse 5014972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 5015972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 5016972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 5017972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 5018972fdea2SJoe Perches } 5019972fdea2SJoe Perches 50205ce59ae0SJoe Perches# check for alloc argument mismatch 50215ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 50225ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 50235ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 50245ce59ae0SJoe Perches } 50255ce59ae0SJoe Perches 5026caf2a54fSJoe Perches# check for multiple semicolons 5027caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 5028d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 5029d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 5030d5e616fcSJoe Perches $fix) { 5031194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 5032d5e616fcSJoe Perches } 5033d1e2ad07SJoe Perches } 5034d1e2ad07SJoe Perches 50350ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit) 50360ab90191SJoe Perches if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 50370ab90191SJoe Perches my $ull = ""; 50380ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 50390ab90191SJoe Perches if (CHK("BIT_MACRO", 50400ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 50410ab90191SJoe Perches $fix) { 50420ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 50430ab90191SJoe Perches } 50440ab90191SJoe Perches } 50450ab90191SJoe Perches 5046e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 5047c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 5048c34c09a8SJoe Perches my $has_break = 0; 5049c34c09a8SJoe Perches my $has_statement = 0; 5050c34c09a8SJoe Perches my $count = 0; 5051c34c09a8SJoe Perches my $prevline = $linenr; 5052e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 5053c34c09a8SJoe Perches $prevline--; 5054c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 5055c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 5056c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 5057c34c09a8SJoe Perches next if ($fline =~ /^\-/); 5058c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 5059c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 5060c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 5061c34c09a8SJoe Perches $has_statement = 1; 5062c34c09a8SJoe Perches $count++; 5063c34c09a8SJoe Perches $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); 5064c34c09a8SJoe Perches } 5065c34c09a8SJoe Perches if (!$has_break && $has_statement) { 5066c34c09a8SJoe Perches WARN("MISSING_BREAK", 5067c34c09a8SJoe Perches "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); 5068c34c09a8SJoe Perches } 5069c34c09a8SJoe Perches } 5070c34c09a8SJoe Perches 5071d1e2ad07SJoe Perches# check for switch/default statements without a break; 5072d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 5073d1e2ad07SJoe Perches defined $stat && 5074d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 5075d1e2ad07SJoe Perches my $ctx = ''; 5076d1e2ad07SJoe Perches my $herectx = $here . "\n"; 5077d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 5078d1e2ad07SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 5079d1e2ad07SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 5080d1e2ad07SJoe Perches } 5081d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 5082d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 5083caf2a54fSJoe Perches } 5084caf2a54fSJoe Perches 508513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 5086d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 5087d5e616fcSJoe Perches if (WARN("USE_FUNC", 5088d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 5089d5e616fcSJoe Perches $fix) { 5090194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 5091d5e616fcSJoe Perches } 509213214adfSAndy Whitcroft } 5093773647a0SAndy Whitcroft 509462ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 509562ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 509662ec818fSJoe Perches ERROR("DATE_TIME", 509762ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 509862ec818fSJoe Perches } 509962ec818fSJoe Perches 51002c92488aSJoe Perches# check for use of yield() 51012c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 51022c92488aSJoe Perches WARN("YIELD", 51032c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 51042c92488aSJoe Perches } 51052c92488aSJoe Perches 5106179f8f40SJoe Perches# check for comparisons against true and false 5107179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 5108179f8f40SJoe Perches my $lead = $1; 5109179f8f40SJoe Perches my $arg = $2; 5110179f8f40SJoe Perches my $test = $3; 5111179f8f40SJoe Perches my $otype = $4; 5112179f8f40SJoe Perches my $trail = $5; 5113179f8f40SJoe Perches my $op = "!"; 5114179f8f40SJoe Perches 5115179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 5116179f8f40SJoe Perches 5117179f8f40SJoe Perches my $type = lc($otype); 5118179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 5119179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 5120179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 5121179f8f40SJoe Perches $op = ""; 5122179f8f40SJoe Perches } 5123179f8f40SJoe Perches 5124179f8f40SJoe Perches CHK("BOOL_COMPARISON", 5125179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 5126179f8f40SJoe Perches 5127179f8f40SJoe Perches## maybe suggesting a correct construct would better 5128179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 5129179f8f40SJoe Perches 5130179f8f40SJoe Perches } 5131179f8f40SJoe Perches } 5132179f8f40SJoe Perches 51334882720bSThomas Gleixner# check for semaphores initialized locked 51344882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 5135000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 5136000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 5137773647a0SAndy Whitcroft } 51386712d858SJoe Perches 513967d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 514067d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 5141000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 514267d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 5143773647a0SAndy Whitcroft } 51446712d858SJoe Perches 5145ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 5146f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 5147000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 5148ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 5149f3db6639SMichael Ellerman } 51506712d858SJoe Perches 515179404849SEmese Revfy# check for various ops structs, ensure they are const. 515279404849SEmese Revfy my $struct_ops = qr{acpi_dock_ops| 515379404849SEmese Revfy address_space_operations| 515479404849SEmese Revfy backlight_ops| 515579404849SEmese Revfy block_device_operations| 515679404849SEmese Revfy dentry_operations| 515779404849SEmese Revfy dev_pm_ops| 515879404849SEmese Revfy dma_map_ops| 515979404849SEmese Revfy extent_io_ops| 516079404849SEmese Revfy file_lock_operations| 516179404849SEmese Revfy file_operations| 516279404849SEmese Revfy hv_ops| 516379404849SEmese Revfy ide_dma_ops| 516479404849SEmese Revfy intel_dvo_dev_ops| 516579404849SEmese Revfy item_operations| 516679404849SEmese Revfy iwl_ops| 516779404849SEmese Revfy kgdb_arch| 516879404849SEmese Revfy kgdb_io| 516979404849SEmese Revfy kset_uevent_ops| 517079404849SEmese Revfy lock_manager_operations| 517179404849SEmese Revfy microcode_ops| 517279404849SEmese Revfy mtrr_ops| 517379404849SEmese Revfy neigh_ops| 517479404849SEmese Revfy nlmsvc_binding| 517579404849SEmese Revfy pci_raw_ops| 517679404849SEmese Revfy pipe_buf_operations| 517779404849SEmese Revfy platform_hibernation_ops| 517879404849SEmese Revfy platform_suspend_ops| 517979404849SEmese Revfy proto_ops| 518079404849SEmese Revfy rpc_pipe_ops| 518179404849SEmese Revfy seq_operations| 518279404849SEmese Revfy snd_ac97_build_ops| 518379404849SEmese Revfy soc_pcmcia_socket_ops| 518479404849SEmese Revfy stacktrace_ops| 518579404849SEmese Revfy sysfs_ops| 518679404849SEmese Revfy tty_operations| 518779404849SEmese Revfy usb_mon_operations| 518879404849SEmese Revfy wd_ops}x; 51896903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 519079404849SEmese Revfy $line =~ /\bstruct\s+($struct_ops)\b/) { 5191000d1cc1SJoe Perches WARN("CONST_STRUCT", 5192000d1cc1SJoe Perches "struct $1 should normally be const\n" . 51936903ffb2SAndy Whitcroft $herecurr); 51942b6db5cbSAndy Whitcroft } 5195773647a0SAndy Whitcroft 5196773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 5197773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 5198773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 5199c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 5200c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 5201171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 5202171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 5203171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 5204773647a0SAndy Whitcroft { 5205000d1cc1SJoe Perches WARN("NR_CPUS", 5206000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 5207773647a0SAndy Whitcroft } 52089c9ba34eSAndy Whitcroft 520952ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 521052ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 521152ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 521252ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 521352ea8506SJoe Perches } 521452ea8506SJoe Perches 5215691d77b6SAndy Whitcroft# whine mightly about in_atomic 5216691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 5217691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 5218000d1cc1SJoe Perches ERROR("IN_ATOMIC", 5219000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 5220f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 5221000d1cc1SJoe Perches WARN("IN_ATOMIC", 5222000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 5223691d77b6SAndy Whitcroft } 5224691d77b6SAndy Whitcroft } 52251704f47bSPeter Zijlstra 52261704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 52271704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 52281704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 52291704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 52301704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 52311704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 5232000d1cc1SJoe Perches ERROR("LOCKDEP", 5233000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 52341704f47bSPeter Zijlstra } 52351704f47bSPeter Zijlstra } 523688f8831cSDave Jones 523788f8831cSDave Jones if ($line =~ /debugfs_create_file.*S_IWUGO/ || 523888f8831cSDave Jones $line =~ /DEVICE_ATTR.*S_IWUGO/ ) { 5239000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 5240000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 524188f8831cSDave Jones } 52422435880fSJoe Perches 5243515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 5244515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 5245515a235eSJoe Perches if ($^V && $^V ge 5.10.0 && 5246515a235eSJoe Perches $line =~ /$mode_perms_search/) { 52472435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 52482435880fSJoe Perches my $func = $entry->[0]; 52492435880fSJoe Perches my $arg_pos = $entry->[1]; 52502435880fSJoe Perches 52512435880fSJoe Perches my $skip_args = ""; 52522435880fSJoe Perches if ($arg_pos > 1) { 52532435880fSJoe Perches $arg_pos--; 52542435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 52552435880fSJoe Perches } 52562435880fSJoe Perches my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; 5257515a235eSJoe Perches if ($line =~ /$test/) { 52582435880fSJoe Perches my $val = $1; 52592435880fSJoe Perches $val = $6 if ($skip_args ne ""); 52602435880fSJoe Perches 52611727cc70SJoe Perches if ($val !~ /^0$/ && 52621727cc70SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 52631727cc70SJoe Perches length($val) ne 4)) { 52642435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 52651727cc70SJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); 5266c0a5c898SJoe Perches } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) { 5267c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 5268c0a5c898SJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 52692435880fSJoe Perches } 52702435880fSJoe Perches } 52712435880fSJoe Perches } 527213214adfSAndy Whitcroft } 5273515a235eSJoe Perches } 527413214adfSAndy Whitcroft 527513214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 527613214adfSAndy Whitcroft # so just keep quiet. 527713214adfSAndy Whitcroft if ($#rawlines == -1) { 527813214adfSAndy Whitcroft exit(0); 52790a920b5bSAndy Whitcroft } 52800a920b5bSAndy Whitcroft 52818905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 52828905a67cSAndy Whitcroft # things that appear to be patches. 52838905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 52848905a67cSAndy Whitcroft exit(0); 52858905a67cSAndy Whitcroft } 52868905a67cSAndy Whitcroft 52878905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 52888905a67cSAndy Whitcroft # just keep quiet. 52898905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 52908905a67cSAndy Whitcroft exit(0); 52918905a67cSAndy Whitcroft } 52928905a67cSAndy Whitcroft 52938905a67cSAndy Whitcroft if (!$is_patch) { 5294000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 5295000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 52960a920b5bSAndy Whitcroft } 52970a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 5298000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 5299000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 53000a920b5bSAndy Whitcroft } 53010a920b5bSAndy Whitcroft 5302f0a594c1SAndy Whitcroft print report_dump(); 530313214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 530413214adfSAndy Whitcroft print "$filename " if ($summary_file); 53056c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 53066c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 53076c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 53088905a67cSAndy Whitcroft print "\n" if ($quiet == 0); 53096c72ffaaSAndy Whitcroft } 53108905a67cSAndy Whitcroft 5311d2c0a235SAndy Whitcroft if ($quiet == 0) { 5312d1fe9c09SJoe Perches 5313d1fe9c09SJoe Perches if ($^V lt 5.10.0) { 5314d1fe9c09SJoe Perches print("NOTE: perl $^V is not modern enough to detect all possible issues.\n"); 5315d1fe9c09SJoe Perches print("An upgrade to at least perl v5.10.0 is suggested.\n\n"); 5316d1fe9c09SJoe Perches } 5317d1fe9c09SJoe Perches 5318d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 5319d2c0a235SAndy Whitcroft # then suggest that. 5320d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 5321d2c0a235SAndy Whitcroft print "NOTE: whitespace errors detected, you may wish to use scripts/cleanpatch or\n"; 5322d2c0a235SAndy Whitcroft print " scripts/cleanfile\n\n"; 5323b0781216SMike Frysinger $rpt_cleaners = 0; 5324d2c0a235SAndy Whitcroft } 5325d2c0a235SAndy Whitcroft } 5326d2c0a235SAndy Whitcroft 532791bfe484SJoe Perches hash_show_words(\%use_type, "Used"); 532891bfe484SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 5329000d1cc1SJoe Perches 5330d752fcc8SJoe Perches if ($clean == 0 && $fix && 5331d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 5332d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 53339624b8d6SJoe Perches my $newfile = $filename; 53349624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 53353705ce5bSJoe Perches my $linecount = 0; 53363705ce5bSJoe Perches my $f; 53373705ce5bSJoe Perches 5338d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 5339d752fcc8SJoe Perches 53403705ce5bSJoe Perches open($f, '>', $newfile) 53413705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 53423705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 53433705ce5bSJoe Perches $linecount++; 53443705ce5bSJoe Perches if ($file) { 53453705ce5bSJoe Perches if ($linecount > 3) { 53463705ce5bSJoe Perches $fixed_line =~ s/^\+//; 53473705ce5bSJoe Perches print $f $fixed_line . "\n"; 53483705ce5bSJoe Perches } 53493705ce5bSJoe Perches } else { 53503705ce5bSJoe Perches print $f $fixed_line . "\n"; 53513705ce5bSJoe Perches } 53523705ce5bSJoe Perches } 53533705ce5bSJoe Perches close($f); 53543705ce5bSJoe Perches 53553705ce5bSJoe Perches if (!$quiet) { 53563705ce5bSJoe Perches print << "EOM"; 53573705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 53583705ce5bSJoe Perches 53593705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 53603705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 53613705ce5bSJoe Perches 53623705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 53633705ce5bSJoe PerchesNo warranties, expressed or implied... 53643705ce5bSJoe Perches 53653705ce5bSJoe PerchesEOM 53663705ce5bSJoe Perches } 53673705ce5bSJoe Perches } 53683705ce5bSJoe Perches 53690a920b5bSAndy Whitcroft if ($clean == 1 && $quiet == 0) { 5370c2fdda0dSAndy Whitcroft print "$vname has no obvious style problems and is ready for submission.\n" 53710a920b5bSAndy Whitcroft } 53720a920b5bSAndy Whitcroft if ($clean == 0 && $quiet == 0) { 5373000d1cc1SJoe Perches print << "EOM"; 5374000d1cc1SJoe Perches$vname has style problems, please review. 5375000d1cc1SJoe Perches 5376000d1cc1SJoe PerchesIf any of these errors are false positives, please report 5377000d1cc1SJoe Perchesthem to the maintainer, see CHECKPATCH in MAINTAINERS. 5378000d1cc1SJoe PerchesEOM 53790a920b5bSAndy Whitcroft } 538013214adfSAndy Whitcroft 53810a920b5bSAndy Whitcroft return $clean; 53820a920b5bSAndy Whitcroft} 5383