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'; 12*57230297SJoe Perchesuse Term::ANSIColor qw(:constants); 130a920b5bSAndy Whitcroft 140a920b5bSAndy Whitcroftmy $P = $0; 1536061e38SJoe Perchesmy $D = dirname(abs_path($P)); 160a920b5bSAndy Whitcroft 17000d1cc1SJoe Perchesmy $V = '0.32'; 180a920b5bSAndy Whitcroft 190a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev); 200a920b5bSAndy Whitcroft 210a920b5bSAndy Whitcroftmy $quiet = 0; 220a920b5bSAndy Whitcroftmy $tree = 1; 230a920b5bSAndy Whitcroftmy $chk_signoff = 1; 240a920b5bSAndy Whitcroftmy $chk_patch = 1; 25773647a0SAndy Whitcroftmy $tst_only; 266c72ffaaSAndy Whitcroftmy $emacs = 0; 278905a67cSAndy Whitcroftmy $terse = 0; 286c72ffaaSAndy Whitcroftmy $file = 0; 296c72ffaaSAndy Whitcroftmy $check = 0; 302ac73b4fSJoe Perchesmy $check_orig = 0; 318905a67cSAndy Whitcroftmy $summary = 1; 328905a67cSAndy Whitcroftmy $mailback = 0; 3313214adfSAndy Whitcroftmy $summary_file = 0; 34000d1cc1SJoe Perchesmy $show_types = 0; 353705ce5bSJoe Perchesmy $fix = 0; 369624b8d6SJoe Perchesmy $fix_inplace = 0; 376c72ffaaSAndy Whitcroftmy $root; 38c2fdda0dSAndy Whitcroftmy %debug; 393445686aSJoe Perchesmy %camelcase = (); 4091bfe484SJoe Perchesmy %use_type = (); 4191bfe484SJoe Perchesmy @use = (); 4291bfe484SJoe Perchesmy %ignore_type = (); 43000d1cc1SJoe Perchesmy @ignore = (); 4477f5b10aSHannes Edermy $help = 0; 45000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 466cd7f386SJoe Perchesmy $max_line_length = 80; 47d62a201fSDave Hansenmy $ignore_perl_version = 0; 48d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 4956193274SVadim Bendeburymy $min_conf_desc_length = 4; 5066b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 51ebfd7d62SJoe Perchesmy $codespell = 0; 52ebfd7d62SJoe Perchesmy $codespellfile = "/usr/local/share/codespell/dictionary.txt"; 53*57230297SJoe Perchesmy $color = 1; 5477f5b10aSHannes Eder 5577f5b10aSHannes Edersub help { 5677f5b10aSHannes Eder my ($exitcode) = @_; 5777f5b10aSHannes Eder 5877f5b10aSHannes Eder print << "EOM"; 5977f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 6077f5b10aSHannes EderVersion: $V 6177f5b10aSHannes Eder 6277f5b10aSHannes EderOptions: 6377f5b10aSHannes Eder -q, --quiet quiet 6477f5b10aSHannes Eder --no-tree run without a kernel tree 6577f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 6677f5b10aSHannes Eder --patch treat FILE as patchfile (default) 6777f5b10aSHannes Eder --emacs emacs compile window format 6877f5b10aSHannes Eder --terse one line per report 6977f5b10aSHannes Eder -f, --file treat FILE as regular source file 7077f5b10aSHannes Eder --subjective, --strict enable more subjective tests 7191bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 72000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 736cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 7456193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 75000d1cc1SJoe Perches --show-types show the message "types" in the output 7677f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 7777f5b10aSHannes Eder --no-summary suppress the per-file summary 7877f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 7977f5b10aSHannes Eder --summary-file include the filename in summary 8077f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 8177f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 8277f5b10aSHannes Eder is all off) 8377f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 8477f5b10aSHannes Eder literally 853705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 863705ce5bSJoe Perches If correctable single-line errors exist, create 873705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 883705ce5bSJoe Perches with potential errors corrected to the preferred 893705ce5bSJoe Perches checkpatch style 909624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 919624b8d6SJoe Perches Is the same as --fix, but overwrites the input 929624b8d6SJoe Perches file. It's your fault if there's no backup or git 93d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 94d62a201fSDave Hansen runtime errors. 95ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 96ebfd7d62SJoe Perches (default:/usr/local/share/codespell/dictionary.txt) 97ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 98*57230297SJoe Perches --color Use colors when output is STDOUT (default: on) 9977f5b10aSHannes Eder -h, --help, --version display this help and exit 10077f5b10aSHannes Eder 10177f5b10aSHannes EderWhen FILE is - read standard input. 10277f5b10aSHannes EderEOM 10377f5b10aSHannes Eder 10477f5b10aSHannes Eder exit($exitcode); 10577f5b10aSHannes Eder} 10677f5b10aSHannes Eder 107000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 108000d1cc1SJoe Perchesif (-f $conf) { 109000d1cc1SJoe Perches my @conf_args; 110000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 111000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 112000d1cc1SJoe Perches 113000d1cc1SJoe Perches while (<$conffile>) { 114000d1cc1SJoe Perches my $line = $_; 115000d1cc1SJoe Perches 116000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 117000d1cc1SJoe Perches $line =~ s/^\s*//g; 118000d1cc1SJoe Perches $line =~ s/\s+/ /g; 119000d1cc1SJoe Perches 120000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 121000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 122000d1cc1SJoe Perches 123000d1cc1SJoe Perches my @words = split(" ", $line); 124000d1cc1SJoe Perches foreach my $word (@words) { 125000d1cc1SJoe Perches last if ($word =~ m/^#/); 126000d1cc1SJoe Perches push (@conf_args, $word); 127000d1cc1SJoe Perches } 128000d1cc1SJoe Perches } 129000d1cc1SJoe Perches close($conffile); 130000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 131000d1cc1SJoe Perches} 132000d1cc1SJoe Perches 1330a920b5bSAndy WhitcroftGetOptions( 1346c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1350a920b5bSAndy Whitcroft 'tree!' => \$tree, 1360a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1370a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 1386c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 1398905a67cSAndy Whitcroft 'terse!' => \$terse, 14077f5b10aSHannes Eder 'f|file!' => \$file, 1416c72ffaaSAndy Whitcroft 'subjective!' => \$check, 1426c72ffaaSAndy Whitcroft 'strict!' => \$check, 143000d1cc1SJoe Perches 'ignore=s' => \@ignore, 14491bfe484SJoe Perches 'types=s' => \@use, 145000d1cc1SJoe Perches 'show-types!' => \$show_types, 1466cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 14756193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 1486c72ffaaSAndy Whitcroft 'root=s' => \$root, 1498905a67cSAndy Whitcroft 'summary!' => \$summary, 1508905a67cSAndy Whitcroft 'mailback!' => \$mailback, 15113214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 1523705ce5bSJoe Perches 'fix!' => \$fix, 1539624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 154d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 155c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 156773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 157ebfd7d62SJoe Perches 'codespell!' => \$codespell, 158ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 159*57230297SJoe Perches 'color!' => \$color, 16077f5b10aSHannes Eder 'h|help' => \$help, 16177f5b10aSHannes Eder 'version' => \$help 16277f5b10aSHannes Eder) or help(1); 16377f5b10aSHannes Eder 16477f5b10aSHannes Ederhelp(0) if ($help); 1650a920b5bSAndy Whitcroft 1669624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 1672ac73b4fSJoe Perches$check_orig = $check; 1689624b8d6SJoe Perches 1690a920b5bSAndy Whitcroftmy $exit = 0; 1700a920b5bSAndy Whitcroft 171d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 172d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 173d62a201fSDave Hansen if (!$ignore_perl_version) { 174d62a201fSDave Hansen exit(1); 175d62a201fSDave Hansen } 176d62a201fSDave Hansen} 177d62a201fSDave Hansen 1780a920b5bSAndy Whitcroftif ($#ARGV < 0) { 17977f5b10aSHannes Eder print "$P: no input files\n"; 1800a920b5bSAndy Whitcroft exit(1); 1810a920b5bSAndy Whitcroft} 1820a920b5bSAndy Whitcroft 18391bfe484SJoe Perchessub hash_save_array_words { 18491bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 18591bfe484SJoe Perches 18691bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 18791bfe484SJoe Perches foreach my $word (@array) { 188000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 189000d1cc1SJoe Perches $word =~ s/^\s*//g; 190000d1cc1SJoe Perches $word =~ s/\s+/ /g; 191000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 192000d1cc1SJoe Perches 193000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 194000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 195000d1cc1SJoe Perches 19691bfe484SJoe Perches $hashRef->{$word}++; 197000d1cc1SJoe Perches } 19891bfe484SJoe Perches} 19991bfe484SJoe Perches 20091bfe484SJoe Perchessub hash_show_words { 20191bfe484SJoe Perches my ($hashRef, $prefix) = @_; 20291bfe484SJoe Perches 20358cb3cf6SJoe Perches if ($quiet == 0 && keys %$hashRef) { 204d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 20558cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 20691bfe484SJoe Perches print " $word"; 20791bfe484SJoe Perches } 208d8469f16SJoe Perches print "\n"; 20991bfe484SJoe Perches } 21091bfe484SJoe Perches} 21191bfe484SJoe Perches 21291bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 21391bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 214000d1cc1SJoe Perches 215c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 216c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 2177429c690SAndy Whitcroftmy $dbg_type = 0; 218a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 219c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 22021caa13cSAndy Whitcroft ## no critic 22121caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 22221caa13cSAndy Whitcroft die "$@" if ($@); 223c2fdda0dSAndy Whitcroft} 224c2fdda0dSAndy Whitcroft 225d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 226d2c0a235SAndy Whitcroft 2278905a67cSAndy Whitcroftif ($terse) { 2288905a67cSAndy Whitcroft $emacs = 1; 2298905a67cSAndy Whitcroft $quiet++; 2308905a67cSAndy Whitcroft} 2318905a67cSAndy Whitcroft 2326c72ffaaSAndy Whitcroftif ($tree) { 2336c72ffaaSAndy Whitcroft if (defined $root) { 2346c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 2356c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 2366c72ffaaSAndy Whitcroft } 2376c72ffaaSAndy Whitcroft } else { 2386c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 2396c72ffaaSAndy Whitcroft $root = '.'; 2406c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 2416c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 2426c72ffaaSAndy Whitcroft $root = $1; 2436c72ffaaSAndy Whitcroft } 2446c72ffaaSAndy Whitcroft } 2456c72ffaaSAndy Whitcroft 2466c72ffaaSAndy Whitcroft if (!defined $root) { 2470a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 2480a920b5bSAndy Whitcroft exit(2); 2490a920b5bSAndy Whitcroft } 2506c72ffaaSAndy Whitcroft} 2516c72ffaaSAndy Whitcroft 2526c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 2536c72ffaaSAndy Whitcroft 2542ceb532bSAndy Whitcroftour $Ident = qr{ 2552ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 2562ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 2572ceb532bSAndy Whitcroft }x; 2586c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 2596c72ffaaSAndy Whitcroftour $Sparse = qr{ 2606c72ffaaSAndy Whitcroft __user| 2616c72ffaaSAndy Whitcroft __kernel| 2626c72ffaaSAndy Whitcroft __force| 2636c72ffaaSAndy Whitcroft __iomem| 2646c72ffaaSAndy Whitcroft __must_check| 2656c72ffaaSAndy Whitcroft __init_refok| 266417495edSAndy Whitcroft __kprobes| 267165e72a6SSven Eckelmann __ref| 268165e72a6SSven Eckelmann __rcu 2696c72ffaaSAndy Whitcroft }x; 270e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 271e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 272e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 273e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 274e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 2758716de38SJoe Perches 27652131292SWolfram Sang# Notes to $Attribute: 27752131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 2786c72ffaaSAndy Whitcroftour $Attribute = qr{ 2796c72ffaaSAndy Whitcroft const| 28003f1df7dSJoe Perches __percpu| 28103f1df7dSJoe Perches __nocast| 28203f1df7dSJoe Perches __safe| 28303f1df7dSJoe Perches __bitwise__| 28403f1df7dSJoe Perches __packed__| 28503f1df7dSJoe Perches __packed2__| 28603f1df7dSJoe Perches __naked| 28703f1df7dSJoe Perches __maybe_unused| 28803f1df7dSJoe Perches __always_unused| 28903f1df7dSJoe Perches __noreturn| 29003f1df7dSJoe Perches __used| 29103f1df7dSJoe Perches __cold| 292e23ef1f3SJoe Perches __pure| 29303f1df7dSJoe Perches __noclone| 29403f1df7dSJoe Perches __deprecated| 2956c72ffaaSAndy Whitcroft __read_mostly| 2966c72ffaaSAndy Whitcroft __kprobes| 2978716de38SJoe Perches $InitAttribute| 29824e1d81aSAndy Whitcroft ____cacheline_aligned| 29924e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 3005fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 3015fe3af11SAndy Whitcroft __weak 3026c72ffaaSAndy Whitcroft }x; 303c45dcabdSAndy Whitcroftour $Modifier; 30491cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 3056c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 3066c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 3076c72ffaaSAndy Whitcroft 30895e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 30995e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 31095e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 31195e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 3122435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 313c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 314326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 315326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 316326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 31774349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 3182435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 319326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 320447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 32123f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 3226c72ffaaSAndy Whitcroftour $Operators = qr{ 3236c72ffaaSAndy Whitcroft <=|>=|==|!=| 3246c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 32523f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 3266c72ffaaSAndy Whitcroft }x; 3276c72ffaaSAndy Whitcroft 32891cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 32991cb5195SJoe Perches 330ab7e23f3SJoe Perchesour $BasicType; 3318905a67cSAndy Whitcroftour $NonptrType; 3321813087dSJoe Perchesour $NonptrTypeMisordered; 3338716de38SJoe Perchesour $NonptrTypeWithAttr; 3348905a67cSAndy Whitcroftour $Type; 3351813087dSJoe Perchesour $TypeMisordered; 3368905a67cSAndy Whitcroftour $Declare; 3371813087dSJoe Perchesour $DeclareMisordered; 3388905a67cSAndy Whitcroft 33915662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 34015662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 341171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 342171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 343171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 344171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 345171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 346171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 347171ae1a4SAndy Whitcroft}x; 348171ae1a4SAndy Whitcroft 34915662b3eSJoe Perchesour $UTF8 = qr{ 35015662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 35115662b3eSJoe Perches | $NON_ASCII_UTF8 35215662b3eSJoe Perches}x; 35315662b3eSJoe Perches 354e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 355021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 356021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 357021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 358021158b4SJoe Perches)}; 359e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 360fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 3618ed22cadSAndy Whitcroft atomic_t 3628ed22cadSAndy Whitcroft)}; 363e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 364e6176fa4SJoe Perches $typeC99Typedefs\b| 365e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 366e6176fa4SJoe Perches $typeKernelTypedefs\b 367e6176fa4SJoe Perches)}; 3688ed22cadSAndy Whitcroft 369691e669bSJoe Perchesour $logFunctions = qr{(?x: 3706e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 3717d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 3726e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 373b0531722SJoe Perches panic| 37406668727SJoe Perches MODULE_[A-Z_]+| 37506668727SJoe Perches seq_vprintf|seq_printf|seq_puts 376691e669bSJoe Perches)}; 377691e669bSJoe Perches 37820112475SJoe Perchesour $signature_tags = qr{(?xi: 37920112475SJoe Perches Signed-off-by:| 38020112475SJoe Perches Acked-by:| 38120112475SJoe Perches Tested-by:| 38220112475SJoe Perches Reviewed-by:| 38320112475SJoe Perches Reported-by:| 3848543ae12SMugunthan V N Suggested-by:| 38520112475SJoe Perches To:| 38620112475SJoe Perches Cc: 38720112475SJoe Perches)}; 38820112475SJoe Perches 3891813087dSJoe Perchesour @typeListMisordered = ( 3901813087dSJoe Perches qr{char\s+(?:un)?signed}, 3911813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 3921813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 3931813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 3941813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 3951813087dSJoe Perches qr{short\s+(?:un)?signed}, 3961813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 3971813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 3981813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 3991813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 4001813087dSJoe Perches qr{int\s+(?:un)?signed}, 4011813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 4021813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 4031813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 4041813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 4051813087dSJoe Perches qr{long\s+(?:un)?signed}, 4061813087dSJoe Perches); 4071813087dSJoe Perches 4088905a67cSAndy Whitcroftour @typeList = ( 4098905a67cSAndy Whitcroft qr{void}, 4100c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 4110c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 4120c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 4130c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 4140c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 4150c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 4160c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 4170c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 4180c773d9dSJoe Perches qr{(?:un)?signed}, 4198905a67cSAndy Whitcroft qr{float}, 4208905a67cSAndy Whitcroft qr{double}, 4218905a67cSAndy Whitcroft qr{bool}, 4228905a67cSAndy Whitcroft qr{struct\s+$Ident}, 4238905a67cSAndy Whitcroft qr{union\s+$Ident}, 4248905a67cSAndy Whitcroft qr{enum\s+$Ident}, 4258905a67cSAndy Whitcroft qr{${Ident}_t}, 4268905a67cSAndy Whitcroft qr{${Ident}_handler}, 4278905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 4281813087dSJoe Perches @typeListMisordered, 4298905a67cSAndy Whitcroft); 430485ff23eSAlex Dowadour @typeListFile = (); 4318716de38SJoe Perchesour @typeListWithAttr = ( 4328716de38SJoe Perches @typeList, 4338716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 4348716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 4358716de38SJoe Perches); 4368716de38SJoe Perches 437c45dcabdSAndy Whitcroftour @modifierList = ( 438c45dcabdSAndy Whitcroft qr{fastcall}, 439c45dcabdSAndy Whitcroft); 440485ff23eSAlex Dowadour @modifierListFile = (); 4418905a67cSAndy Whitcroft 4422435880fSJoe Perchesour @mode_permission_funcs = ( 4432435880fSJoe Perches ["module_param", 3], 4442435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 4452435880fSJoe Perches ["module_param_array_named", 5], 4462435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 4472435880fSJoe Perches ["proc_create(?:_data|)", 2], 4482435880fSJoe Perches ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], 4492435880fSJoe Perches); 4502435880fSJoe Perches 451515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 452515a235eSJoe Perchesour $mode_perms_search = ""; 453515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 454515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 455515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 456515a235eSJoe Perches} 457515a235eSJoe Perches 458b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 459b392c64fSJoe Perches S_IWUGO | 460b392c64fSJoe Perches S_IWOTH | 461b392c64fSJoe Perches S_IRWXUGO | 462b392c64fSJoe Perches S_IALLUGO | 463b392c64fSJoe Perches 0[0-7][0-7][2367] 464b392c64fSJoe Perches}x; 465b392c64fSJoe Perches 4667840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 4677840a94cSWolfram Sang irq| 468cdcee686SSergey Ryazanov memory| 469cdcee686SSergey Ryazanov time| 470cdcee686SSergey Ryazanov reboot 4717840a94cSWolfram Sang)}; 4727840a94cSWolfram Sang# memory.h: ARM has a custom one 4737840a94cSWolfram Sang 47466b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 47566b47b4aSKees Cookmy $misspellings; 47666b47b4aSKees Cookmy %spelling_fix; 47736061e38SJoe Perches 47836061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 47966b47b4aSKees Cook while (<$spelling>) { 48066b47b4aSKees Cook my $line = $_; 48166b47b4aSKees Cook 48266b47b4aSKees Cook $line =~ s/\s*\n?$//g; 48366b47b4aSKees Cook $line =~ s/^\s*//g; 48466b47b4aSKees Cook 48566b47b4aSKees Cook next if ($line =~ m/^\s*#/); 48666b47b4aSKees Cook next if ($line =~ m/^\s*$/); 48766b47b4aSKees Cook 48866b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 48966b47b4aSKees Cook 49066b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 49166b47b4aSKees Cook } 49266b47b4aSKees Cook close($spelling); 49336061e38SJoe Perches} else { 49436061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 49536061e38SJoe Perches} 49666b47b4aSKees Cook 497ebfd7d62SJoe Perchesif ($codespell) { 498ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 499ebfd7d62SJoe Perches while (<$spelling>) { 500ebfd7d62SJoe Perches my $line = $_; 501ebfd7d62SJoe Perches 502ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 503ebfd7d62SJoe Perches $line =~ s/^\s*//g; 504ebfd7d62SJoe Perches 505ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 506ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 507ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 508ebfd7d62SJoe Perches 509ebfd7d62SJoe Perches $line =~ s/,.*$//; 510ebfd7d62SJoe Perches 511ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 512ebfd7d62SJoe Perches 513ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 514ebfd7d62SJoe Perches } 515ebfd7d62SJoe Perches close($spelling); 516ebfd7d62SJoe Perches } else { 517ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 518ebfd7d62SJoe Perches } 519ebfd7d62SJoe Perches} 520ebfd7d62SJoe Perches 521ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 522ebfd7d62SJoe Perches 5238905a67cSAndy Whitcroftsub build_types { 524485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 525485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 5261813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 5278716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 528c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 529ab7e23f3SJoe Perches $BasicType = qr{ 530ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 531ab7e23f3SJoe Perches (?:${all}\b) 532ab7e23f3SJoe Perches }x; 5338905a67cSAndy Whitcroft $NonptrType = qr{ 534d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 535cf655043SAndy Whitcroft (?: 5366b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 5378ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 538c45dcabdSAndy Whitcroft (?:${all}\b) 539cf655043SAndy Whitcroft ) 540c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 5418905a67cSAndy Whitcroft }x; 5421813087dSJoe Perches $NonptrTypeMisordered = qr{ 5431813087dSJoe Perches (?:$Modifier\s+|const\s+)* 5441813087dSJoe Perches (?: 5451813087dSJoe Perches (?:${Misordered}\b) 5461813087dSJoe Perches ) 5471813087dSJoe Perches (?:\s+$Modifier|\s+const)* 5481813087dSJoe Perches }x; 5498716de38SJoe Perches $NonptrTypeWithAttr = qr{ 5508716de38SJoe Perches (?:$Modifier\s+|const\s+)* 5518716de38SJoe Perches (?: 5528716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 5538716de38SJoe Perches (?:$typeTypedefs\b)| 5548716de38SJoe Perches (?:${allWithAttr}\b) 5558716de38SJoe Perches ) 5568716de38SJoe Perches (?:\s+$Modifier|\s+const)* 5578716de38SJoe Perches }x; 5588905a67cSAndy Whitcroft $Type = qr{ 559c45dcabdSAndy Whitcroft $NonptrType 5601574a29fSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 561c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 5628905a67cSAndy Whitcroft }x; 5631813087dSJoe Perches $TypeMisordered = qr{ 5641813087dSJoe Perches $NonptrTypeMisordered 5651813087dSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 5661813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 5671813087dSJoe Perches }x; 56891cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 5691813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 5708905a67cSAndy Whitcroft} 5718905a67cSAndy Whitcroftbuild_types(); 5726c72ffaaSAndy Whitcroft 5737d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 574d1fe9c09SJoe Perches 575d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 576d1fe9c09SJoe Perches# requires at least perl version v5.10.0 577d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 578d1fe9c09SJoe Perches 579d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 5802435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 581c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 5827d2367afSJoe Perches 583f8422308SJoe Perchesour $declaration_macros = qr{(?x: 584f8422308SJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,2}\s*\(| 585f8422308SJoe Perches (?:$Storage\s+)?LIST_HEAD\s*\(| 586f8422308SJoe Perches (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\( 587f8422308SJoe Perches)}; 588f8422308SJoe Perches 5897d2367afSJoe Perchessub deparenthesize { 5907d2367afSJoe Perches my ($string) = @_; 5917d2367afSJoe Perches return "" if (!defined($string)); 5925b9553abSJoe Perches 5935b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 5945b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 5955b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 5965b9553abSJoe Perches } 5975b9553abSJoe Perches 5987d2367afSJoe Perches $string =~ s@\s+@ @g; 5995b9553abSJoe Perches 6007d2367afSJoe Perches return $string; 6017d2367afSJoe Perches} 6027d2367afSJoe Perches 6033445686aSJoe Perchessub seed_camelcase_file { 6043445686aSJoe Perches my ($file) = @_; 6053445686aSJoe Perches 6063445686aSJoe Perches return if (!(-f $file)); 6073445686aSJoe Perches 6083445686aSJoe Perches local $/; 6093445686aSJoe Perches 6103445686aSJoe Perches open(my $include_file, '<', "$file") 6113445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 6123445686aSJoe Perches my $text = <$include_file>; 6133445686aSJoe Perches close($include_file); 6143445686aSJoe Perches 6153445686aSJoe Perches my @lines = split('\n', $text); 6163445686aSJoe Perches 6173445686aSJoe Perches foreach my $line (@lines) { 6183445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 6193445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 6203445686aSJoe Perches $camelcase{$1} = 1; 62111ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 62211ea516aSJoe Perches $camelcase{$1} = 1; 62311ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 6243445686aSJoe Perches $camelcase{$1} = 1; 6253445686aSJoe Perches } 6263445686aSJoe Perches } 6273445686aSJoe Perches} 6283445686aSJoe Perches 6293445686aSJoe Perchesmy $camelcase_seeded = 0; 6303445686aSJoe Perchessub seed_camelcase_includes { 6313445686aSJoe Perches return if ($camelcase_seeded); 6323445686aSJoe Perches 6333445686aSJoe Perches my $files; 634c707a81dSJoe Perches my $camelcase_cache = ""; 635c707a81dSJoe Perches my @include_files = (); 636c707a81dSJoe Perches 637c707a81dSJoe Perches $camelcase_seeded = 1; 638351b2a1fSJoe Perches 6393645e328SRichard Genoud if (-e ".git") { 640351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 641351b2a1fSJoe Perches chomp $git_last_include_commit; 642c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 643c707a81dSJoe Perches } else { 644c707a81dSJoe Perches my $last_mod_date = 0; 645c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 646c707a81dSJoe Perches @include_files = split('\n', $files); 647c707a81dSJoe Perches foreach my $file (@include_files) { 648c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 649c707a81dSJoe Perches localtime((stat $file)[9])); 650c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 651c707a81dSJoe Perches } 652c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 653c707a81dSJoe Perches } 654c707a81dSJoe Perches 655c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 656c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 657c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 658351b2a1fSJoe Perches while (<$camelcase_file>) { 659351b2a1fSJoe Perches chomp; 660351b2a1fSJoe Perches $camelcase{$_} = 1; 661351b2a1fSJoe Perches } 662351b2a1fSJoe Perches close($camelcase_file); 663351b2a1fSJoe Perches 664351b2a1fSJoe Perches return; 665351b2a1fSJoe Perches } 666c707a81dSJoe Perches 6673645e328SRichard Genoud if (-e ".git") { 668c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 669c707a81dSJoe Perches @include_files = split('\n', $files); 6703445686aSJoe Perches } 671c707a81dSJoe Perches 6723445686aSJoe Perches foreach my $file (@include_files) { 6733445686aSJoe Perches seed_camelcase_file($file); 6743445686aSJoe Perches } 675351b2a1fSJoe Perches 676c707a81dSJoe Perches if ($camelcase_cache ne "") { 677351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 678c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 679c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 680351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 681351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 682351b2a1fSJoe Perches } 683351b2a1fSJoe Perches close($camelcase_file); 684351b2a1fSJoe Perches } 6853445686aSJoe Perches} 6863445686aSJoe Perches 687d311cd44SJoe Perchessub git_commit_info { 688d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 689d311cd44SJoe Perches 690d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 691d311cd44SJoe Perches 692d311cd44SJoe Perches my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; 693d311cd44SJoe Perches $output =~ s/^\s*//gm; 694d311cd44SJoe Perches my @lines = split("\n", $output); 695d311cd44SJoe Perches 6960d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 6970d7835fcSJoe Perches 698d311cd44SJoe Perches if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { 699d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 700d311cd44SJoe Perches# all matching commit ids, but it's very slow... 701d311cd44SJoe Perches# 702d311cd44SJoe Perches# echo "checking commits $1..." 703d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 704d311cd44SJoe Perches# while read line ; do 705d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 706d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 707d311cd44SJoe Perches# done 708d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 709d311cd44SJoe Perches } else { 710d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 711d311cd44SJoe Perches $desc = substr($lines[0], 41); 712d311cd44SJoe Perches } 713d311cd44SJoe Perches 714d311cd44SJoe Perches return ($id, $desc); 715d311cd44SJoe Perches} 716d311cd44SJoe Perches 7176c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 7180a920b5bSAndy Whitcroft 71900df344fSAndy Whitcroftmy @rawlines = (); 720c2fdda0dSAndy Whitcroftmy @lines = (); 7213705ce5bSJoe Perchesmy @fixed = (); 722d752fcc8SJoe Perchesmy @fixed_inserted = (); 723d752fcc8SJoe Perchesmy @fixed_deleted = (); 724194f66fcSJoe Perchesmy $fixlinenr = -1; 725194f66fcSJoe Perches 726c2fdda0dSAndy Whitcroftmy $vname; 7276c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 72821caa13cSAndy Whitcroft my $FILE; 7296c72ffaaSAndy Whitcroft if ($file) { 73021caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 7316c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 73221caa13cSAndy Whitcroft } elsif ($filename eq '-') { 73321caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 7346c72ffaaSAndy Whitcroft } else { 73521caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 7366c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 7376c72ffaaSAndy Whitcroft } 738c2fdda0dSAndy Whitcroft if ($filename eq '-') { 739c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 740c2fdda0dSAndy Whitcroft } else { 741c2fdda0dSAndy Whitcroft $vname = $filename; 742c2fdda0dSAndy Whitcroft } 74321caa13cSAndy Whitcroft while (<$FILE>) { 7440a920b5bSAndy Whitcroft chomp; 74500df344fSAndy Whitcroft push(@rawlines, $_); 7466c72ffaaSAndy Whitcroft } 74721caa13cSAndy Whitcroft close($FILE); 748d8469f16SJoe Perches 749d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 750d8469f16SJoe Perches print '-' x length($vname) . "\n"; 751d8469f16SJoe Perches print "$vname\n"; 752d8469f16SJoe Perches print '-' x length($vname) . "\n"; 753d8469f16SJoe Perches } 754d8469f16SJoe Perches 755c2fdda0dSAndy Whitcroft if (!process($filename)) { 7560a920b5bSAndy Whitcroft $exit = 1; 7570a920b5bSAndy Whitcroft } 75800df344fSAndy Whitcroft @rawlines = (); 75913214adfSAndy Whitcroft @lines = (); 7603705ce5bSJoe Perches @fixed = (); 761d752fcc8SJoe Perches @fixed_inserted = (); 762d752fcc8SJoe Perches @fixed_deleted = (); 763194f66fcSJoe Perches $fixlinenr = -1; 764485ff23eSAlex Dowad @modifierListFile = (); 765485ff23eSAlex Dowad @typeListFile = (); 766485ff23eSAlex Dowad build_types(); 7670a920b5bSAndy Whitcroft} 7680a920b5bSAndy Whitcroft 769d8469f16SJoe Perchesif (!$quiet) { 770d8469f16SJoe Perches if ($^V lt 5.10.0) { 771d8469f16SJoe Perches print << "EOM" 772d8469f16SJoe Perches 773d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 774d8469f16SJoe Perches An upgrade to at least perl v5.10.0 is suggested. 775d8469f16SJoe PerchesEOM 776d8469f16SJoe Perches } 777d8469f16SJoe Perches if ($exit) { 778d8469f16SJoe Perches print << "EOM" 779d8469f16SJoe Perches 780d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 781d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 782d8469f16SJoe PerchesEOM 783d8469f16SJoe Perches } 784d8469f16SJoe Perches} 785d8469f16SJoe Perches 7860a920b5bSAndy Whitcroftexit($exit); 7870a920b5bSAndy Whitcroft 7880a920b5bSAndy Whitcroftsub top_of_kernel_tree { 7896c72ffaaSAndy Whitcroft my ($root) = @_; 7906c72ffaaSAndy Whitcroft 7916c72ffaaSAndy Whitcroft my @tree_check = ( 7926c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 7936c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 7946c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 7956c72ffaaSAndy Whitcroft ); 7966c72ffaaSAndy Whitcroft 7976c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 7986c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 7990a920b5bSAndy Whitcroft return 0; 8000a920b5bSAndy Whitcroft } 8016c72ffaaSAndy Whitcroft } 8026c72ffaaSAndy Whitcroft return 1; 8036c72ffaaSAndy Whitcroft} 8040a920b5bSAndy Whitcroft 80520112475SJoe Perchessub parse_email { 80620112475SJoe Perches my ($formatted_email) = @_; 80720112475SJoe Perches 80820112475SJoe Perches my $name = ""; 80920112475SJoe Perches my $address = ""; 81020112475SJoe Perches my $comment = ""; 81120112475SJoe Perches 81220112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 81320112475SJoe Perches $name = $1; 81420112475SJoe Perches $address = $2; 81520112475SJoe Perches $comment = $3 if defined $3; 81620112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 81720112475SJoe Perches $address = $1; 81820112475SJoe Perches $comment = $2 if defined $2; 81920112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 82020112475SJoe Perches $address = $1; 82120112475SJoe Perches $comment = $2 if defined $2; 82220112475SJoe Perches $formatted_email =~ s/$address.*$//; 82320112475SJoe Perches $name = $formatted_email; 8243705ce5bSJoe Perches $name = trim($name); 82520112475SJoe Perches $name =~ s/^\"|\"$//g; 82620112475SJoe Perches # If there's a name left after stripping spaces and 82720112475SJoe Perches # leading quotes, and the address doesn't have both 82820112475SJoe Perches # leading and trailing angle brackets, the address 82920112475SJoe Perches # is invalid. ie: 83020112475SJoe Perches # "joe smith [email protected]" bad 83120112475SJoe Perches # "joe smith <[email protected]" bad 83220112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 83320112475SJoe Perches $name = ""; 83420112475SJoe Perches $address = ""; 83520112475SJoe Perches $comment = ""; 83620112475SJoe Perches } 83720112475SJoe Perches } 83820112475SJoe Perches 8393705ce5bSJoe Perches $name = trim($name); 84020112475SJoe Perches $name =~ s/^\"|\"$//g; 8413705ce5bSJoe Perches $address = trim($address); 84220112475SJoe Perches $address =~ s/^\<|\>$//g; 84320112475SJoe Perches 84420112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 84520112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 84620112475SJoe Perches $name = "\"$name\""; 84720112475SJoe Perches } 84820112475SJoe Perches 84920112475SJoe Perches return ($name, $address, $comment); 85020112475SJoe Perches} 85120112475SJoe Perches 85220112475SJoe Perchessub format_email { 85320112475SJoe Perches my ($name, $address) = @_; 85420112475SJoe Perches 85520112475SJoe Perches my $formatted_email; 85620112475SJoe Perches 8573705ce5bSJoe Perches $name = trim($name); 85820112475SJoe Perches $name =~ s/^\"|\"$//g; 8593705ce5bSJoe Perches $address = trim($address); 86020112475SJoe Perches 86120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 86220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 86320112475SJoe Perches $name = "\"$name\""; 86420112475SJoe Perches } 86520112475SJoe Perches 86620112475SJoe Perches if ("$name" eq "") { 86720112475SJoe Perches $formatted_email = "$address"; 86820112475SJoe Perches } else { 86920112475SJoe Perches $formatted_email = "$name <$address>"; 87020112475SJoe Perches } 87120112475SJoe Perches 87220112475SJoe Perches return $formatted_email; 87320112475SJoe Perches} 87420112475SJoe Perches 875d311cd44SJoe Perchessub which { 876d311cd44SJoe Perches my ($bin) = @_; 877d311cd44SJoe Perches 878d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 879d311cd44SJoe Perches if (-e "$path/$bin") { 880d311cd44SJoe Perches return "$path/$bin"; 881d311cd44SJoe Perches } 882d311cd44SJoe Perches } 883d311cd44SJoe Perches 884d311cd44SJoe Perches return ""; 885d311cd44SJoe Perches} 886d311cd44SJoe Perches 887000d1cc1SJoe Perchessub which_conf { 888000d1cc1SJoe Perches my ($conf) = @_; 889000d1cc1SJoe Perches 890000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 891000d1cc1SJoe Perches if (-e "$path/$conf") { 892000d1cc1SJoe Perches return "$path/$conf"; 893000d1cc1SJoe Perches } 894000d1cc1SJoe Perches } 895000d1cc1SJoe Perches 896000d1cc1SJoe Perches return ""; 897000d1cc1SJoe Perches} 898000d1cc1SJoe Perches 8990a920b5bSAndy Whitcroftsub expand_tabs { 9000a920b5bSAndy Whitcroft my ($str) = @_; 9010a920b5bSAndy Whitcroft 9020a920b5bSAndy Whitcroft my $res = ''; 9030a920b5bSAndy Whitcroft my $n = 0; 9040a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 9050a920b5bSAndy Whitcroft if ($c eq "\t") { 9060a920b5bSAndy Whitcroft $res .= ' '; 9070a920b5bSAndy Whitcroft $n++; 9080a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 9090a920b5bSAndy Whitcroft $res .= ' '; 9100a920b5bSAndy Whitcroft } 9110a920b5bSAndy Whitcroft next; 9120a920b5bSAndy Whitcroft } 9130a920b5bSAndy Whitcroft $res .= $c; 9140a920b5bSAndy Whitcroft $n++; 9150a920b5bSAndy Whitcroft } 9160a920b5bSAndy Whitcroft 9170a920b5bSAndy Whitcroft return $res; 9180a920b5bSAndy Whitcroft} 9196c72ffaaSAndy Whitcroftsub copy_spacing { 920773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 9216c72ffaaSAndy Whitcroft return $res; 9226c72ffaaSAndy Whitcroft} 9230a920b5bSAndy Whitcroft 9244a0df2efSAndy Whitcroftsub line_stats { 9254a0df2efSAndy Whitcroft my ($line) = @_; 9264a0df2efSAndy Whitcroft 9274a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 9284a0df2efSAndy Whitcroft $line =~ s/^.//; 9294a0df2efSAndy Whitcroft $line = expand_tabs($line); 9304a0df2efSAndy Whitcroft 9314a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 9324a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 9334a0df2efSAndy Whitcroft 9344a0df2efSAndy Whitcroft return (length($line), length($white)); 9354a0df2efSAndy Whitcroft} 9364a0df2efSAndy Whitcroft 937773647a0SAndy Whitcroftmy $sanitise_quote = ''; 938773647a0SAndy Whitcroft 939773647a0SAndy Whitcroftsub sanitise_line_reset { 940773647a0SAndy Whitcroft my ($in_comment) = @_; 941773647a0SAndy Whitcroft 942773647a0SAndy Whitcroft if ($in_comment) { 943773647a0SAndy Whitcroft $sanitise_quote = '*/'; 944773647a0SAndy Whitcroft } else { 945773647a0SAndy Whitcroft $sanitise_quote = ''; 946773647a0SAndy Whitcroft } 947773647a0SAndy Whitcroft} 94800df344fSAndy Whitcroftsub sanitise_line { 94900df344fSAndy Whitcroft my ($line) = @_; 95000df344fSAndy Whitcroft 95100df344fSAndy Whitcroft my $res = ''; 95200df344fSAndy Whitcroft my $l = ''; 95300df344fSAndy Whitcroft 954c2fdda0dSAndy Whitcroft my $qlen = 0; 955773647a0SAndy Whitcroft my $off = 0; 956773647a0SAndy Whitcroft my $c; 95700df344fSAndy Whitcroft 958773647a0SAndy Whitcroft # Always copy over the diff marker. 959773647a0SAndy Whitcroft $res = substr($line, 0, 1); 960773647a0SAndy Whitcroft 961773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 962773647a0SAndy Whitcroft $c = substr($line, $off, 1); 963773647a0SAndy Whitcroft 964773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 965773647a0SAndy Whitcroft # and end, all to $;. 966773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 967773647a0SAndy Whitcroft $sanitise_quote = '*/'; 968773647a0SAndy Whitcroft 969773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 970773647a0SAndy Whitcroft $off++; 97100df344fSAndy Whitcroft next; 972773647a0SAndy Whitcroft } 97381bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 974773647a0SAndy Whitcroft $sanitise_quote = ''; 975773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 976773647a0SAndy Whitcroft $off++; 977773647a0SAndy Whitcroft next; 978773647a0SAndy Whitcroft } 979113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 980113f04a8SDaniel Walker $sanitise_quote = '//'; 981113f04a8SDaniel Walker 982113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 983113f04a8SDaniel Walker $off++; 984113f04a8SDaniel Walker next; 985113f04a8SDaniel Walker } 986773647a0SAndy Whitcroft 987773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 988773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 989773647a0SAndy Whitcroft $c eq "\\") { 990773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 991773647a0SAndy Whitcroft $off++; 992773647a0SAndy Whitcroft next; 993773647a0SAndy Whitcroft } 994773647a0SAndy Whitcroft # Regular quotes. 995773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 996773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 997773647a0SAndy Whitcroft $sanitise_quote = $c; 998773647a0SAndy Whitcroft 999773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1000773647a0SAndy Whitcroft next; 1001773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1002773647a0SAndy Whitcroft $sanitise_quote = ''; 100300df344fSAndy Whitcroft } 100400df344fSAndy Whitcroft } 1005773647a0SAndy Whitcroft 1006fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1007773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1008773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1009113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1010113f04a8SDaniel Walker substr($res, $off, 1, $;); 1011773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1012773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 101300df344fSAndy Whitcroft } else { 1014773647a0SAndy Whitcroft substr($res, $off, 1, $c); 101500df344fSAndy Whitcroft } 1016c2fdda0dSAndy Whitcroft } 1017c2fdda0dSAndy Whitcroft 1018113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1019113f04a8SDaniel Walker $sanitise_quote = ''; 1020113f04a8SDaniel Walker } 1021113f04a8SDaniel Walker 1022c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1023c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1024c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1025c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1026c2fdda0dSAndy Whitcroft 1027c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1028c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1029c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1030c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1031c2fdda0dSAndy Whitcroft } 1032c2fdda0dSAndy Whitcroft 103300df344fSAndy Whitcroft return $res; 103400df344fSAndy Whitcroft} 103500df344fSAndy Whitcroft 1036a6962d72SJoe Perchessub get_quoted_string { 1037a6962d72SJoe Perches my ($line, $rawline) = @_; 1038a6962d72SJoe Perches 103933acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1040a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1041a6962d72SJoe Perches} 1042a6962d72SJoe Perches 10438905a67cSAndy Whitcroftsub ctx_statement_block { 10448905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 10458905a67cSAndy Whitcroft my $line = $linenr - 1; 10468905a67cSAndy Whitcroft my $blk = ''; 10478905a67cSAndy Whitcroft my $soff = $off; 10488905a67cSAndy Whitcroft my $coff = $off - 1; 1049773647a0SAndy Whitcroft my $coff_set = 0; 10508905a67cSAndy Whitcroft 105113214adfSAndy Whitcroft my $loff = 0; 105213214adfSAndy Whitcroft 10538905a67cSAndy Whitcroft my $type = ''; 10548905a67cSAndy Whitcroft my $level = 0; 1055a2750645SAndy Whitcroft my @stack = (); 1056cf655043SAndy Whitcroft my $p; 10578905a67cSAndy Whitcroft my $c; 10588905a67cSAndy Whitcroft my $len = 0; 105913214adfSAndy Whitcroft 106013214adfSAndy Whitcroft my $remainder; 10618905a67cSAndy Whitcroft while (1) { 1062a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1063a2750645SAndy Whitcroft 1064773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 10658905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 10668905a67cSAndy Whitcroft # context. 10678905a67cSAndy Whitcroft if ($off >= $len) { 10688905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1069dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1070c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 10718905a67cSAndy Whitcroft $remain--; 107213214adfSAndy Whitcroft $loff = $len; 1073c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 10748905a67cSAndy Whitcroft $len = length($blk); 10758905a67cSAndy Whitcroft $line++; 10768905a67cSAndy Whitcroft last; 10778905a67cSAndy Whitcroft } 10788905a67cSAndy Whitcroft # Bail if there is no further context. 10798905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 108013214adfSAndy Whitcroft if ($off >= $len) { 10818905a67cSAndy Whitcroft last; 10828905a67cSAndy Whitcroft } 1083f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1084f74bd194SAndy Whitcroft $level++; 1085f74bd194SAndy Whitcroft $type = '#'; 1086f74bd194SAndy Whitcroft } 10878905a67cSAndy Whitcroft } 1088cf655043SAndy Whitcroft $p = $c; 10898905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 109013214adfSAndy Whitcroft $remainder = substr($blk, $off); 10918905a67cSAndy Whitcroft 1092773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 10934635f4fbSAndy Whitcroft 10944635f4fbSAndy Whitcroft # Handle nested #if/#else. 10954635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 10964635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 10974635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 10984635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 10994635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 11004635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 11014635f4fbSAndy Whitcroft } 11024635f4fbSAndy Whitcroft 11038905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 11048905a67cSAndy Whitcroft # outermost level. 11058905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 11068905a67cSAndy Whitcroft last; 11078905a67cSAndy Whitcroft } 11088905a67cSAndy Whitcroft 110913214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1110773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1111773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1112773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1113773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1114773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1115773647a0SAndy Whitcroft $coff_set = 1; 1116773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1117773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 111813214adfSAndy Whitcroft } 111913214adfSAndy Whitcroft 11208905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 11218905a67cSAndy Whitcroft $level++; 11228905a67cSAndy Whitcroft $type = '('; 11238905a67cSAndy Whitcroft } 11248905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 11258905a67cSAndy Whitcroft $level--; 11268905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 11278905a67cSAndy Whitcroft 11288905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 11298905a67cSAndy Whitcroft $coff = $off; 1130773647a0SAndy Whitcroft $coff_set = 1; 1131773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 11328905a67cSAndy Whitcroft } 11338905a67cSAndy Whitcroft } 11348905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 11358905a67cSAndy Whitcroft $level++; 11368905a67cSAndy Whitcroft $type = '{'; 11378905a67cSAndy Whitcroft } 11388905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 11398905a67cSAndy Whitcroft $level--; 11408905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 11418905a67cSAndy Whitcroft 11428905a67cSAndy Whitcroft if ($level == 0) { 1143b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1144b998e001SPatrick Pannuto $off++; 1145b998e001SPatrick Pannuto } 11468905a67cSAndy Whitcroft last; 11478905a67cSAndy Whitcroft } 11488905a67cSAndy Whitcroft } 1149f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1150f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1151f74bd194SAndy Whitcroft $level--; 1152f74bd194SAndy Whitcroft $type = ''; 1153f74bd194SAndy Whitcroft $off++; 1154f74bd194SAndy Whitcroft last; 1155f74bd194SAndy Whitcroft } 11568905a67cSAndy Whitcroft $off++; 11578905a67cSAndy Whitcroft } 1158a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 115913214adfSAndy Whitcroft if ($off == $len) { 1160a3bb97a7SAndy Whitcroft $loff = $len + 1; 116113214adfSAndy Whitcroft $line++; 116213214adfSAndy Whitcroft $remain--; 116313214adfSAndy Whitcroft } 11648905a67cSAndy Whitcroft 11658905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 11668905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 11678905a67cSAndy Whitcroft 11688905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 11698905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 11708905a67cSAndy Whitcroft 1171773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 117213214adfSAndy Whitcroft 117313214adfSAndy Whitcroft return ($statement, $condition, 117413214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 117513214adfSAndy Whitcroft} 117613214adfSAndy Whitcroft 1177cf655043SAndy Whitcroftsub statement_lines { 1178cf655043SAndy Whitcroft my ($stmt) = @_; 1179cf655043SAndy Whitcroft 1180cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1181cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1182cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1183cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1184cf655043SAndy Whitcroft 1185cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1186cf655043SAndy Whitcroft 1187cf655043SAndy Whitcroft return $#stmt_lines + 2; 1188cf655043SAndy Whitcroft} 1189cf655043SAndy Whitcroft 1190cf655043SAndy Whitcroftsub statement_rawlines { 1191cf655043SAndy Whitcroft my ($stmt) = @_; 1192cf655043SAndy Whitcroft 1193cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1194cf655043SAndy Whitcroft 1195cf655043SAndy Whitcroft return $#stmt_lines + 2; 1196cf655043SAndy Whitcroft} 1197cf655043SAndy Whitcroft 1198cf655043SAndy Whitcroftsub statement_block_size { 1199cf655043SAndy Whitcroft my ($stmt) = @_; 1200cf655043SAndy Whitcroft 1201cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1202cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1203cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1204cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1205cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1206cf655043SAndy Whitcroft 1207cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1208cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1209cf655043SAndy Whitcroft 1210cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1211cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1212cf655043SAndy Whitcroft 1213cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1214cf655043SAndy Whitcroft return $stmt_lines; 1215cf655043SAndy Whitcroft } else { 1216cf655043SAndy Whitcroft return $stmt_statements; 1217cf655043SAndy Whitcroft } 1218cf655043SAndy Whitcroft} 1219cf655043SAndy Whitcroft 122013214adfSAndy Whitcroftsub ctx_statement_full { 122113214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 122213214adfSAndy Whitcroft my ($statement, $condition, $level); 122313214adfSAndy Whitcroft 122413214adfSAndy Whitcroft my (@chunks); 122513214adfSAndy Whitcroft 1226cf655043SAndy Whitcroft # Grab the first conditional/block pair. 122713214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 122813214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1229773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 123013214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1231cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1232cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1233cf655043SAndy Whitcroft } 1234cf655043SAndy Whitcroft 1235cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1236cf655043SAndy Whitcroft # could continue the statement. 1237cf655043SAndy Whitcroft for (;;) { 123813214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 123913214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1240cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1241773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1242cf655043SAndy Whitcroft #print "C: push\n"; 1243cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 124413214adfSAndy Whitcroft } 124513214adfSAndy Whitcroft 124613214adfSAndy Whitcroft return ($level, $linenr, @chunks); 12478905a67cSAndy Whitcroft} 12488905a67cSAndy Whitcroft 12494a0df2efSAndy Whitcroftsub ctx_block_get { 1250f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 12514a0df2efSAndy Whitcroft my $line; 12524a0df2efSAndy Whitcroft my $start = $linenr - 1; 12534a0df2efSAndy Whitcroft my $blk = ''; 12544a0df2efSAndy Whitcroft my @o; 12554a0df2efSAndy Whitcroft my @c; 12564a0df2efSAndy Whitcroft my @res = (); 12574a0df2efSAndy Whitcroft 1258f0a594c1SAndy Whitcroft my $level = 0; 12594635f4fbSAndy Whitcroft my @stack = ($level); 126000df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 126100df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 126200df344fSAndy Whitcroft $remain--; 126300df344fSAndy Whitcroft 126400df344fSAndy Whitcroft $blk .= $rawlines[$line]; 12654635f4fbSAndy Whitcroft 12664635f4fbSAndy Whitcroft # Handle nested #if/#else. 126701464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 12684635f4fbSAndy Whitcroft push(@stack, $level); 126901464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 12704635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 127101464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 12724635f4fbSAndy Whitcroft $level = pop(@stack); 12734635f4fbSAndy Whitcroft } 12744635f4fbSAndy Whitcroft 127501464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1276f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1277f0a594c1SAndy Whitcroft if ($off > 0) { 1278f0a594c1SAndy Whitcroft $off--; 1279f0a594c1SAndy Whitcroft next; 1280f0a594c1SAndy Whitcroft } 12814a0df2efSAndy Whitcroft 1282f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1283f0a594c1SAndy Whitcroft $level--; 1284f0a594c1SAndy Whitcroft last if ($level == 0); 1285f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1286f0a594c1SAndy Whitcroft $level++; 1287f0a594c1SAndy Whitcroft } 1288f0a594c1SAndy Whitcroft } 12894a0df2efSAndy Whitcroft 1290f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 129100df344fSAndy Whitcroft push(@res, $rawlines[$line]); 12924a0df2efSAndy Whitcroft } 12934a0df2efSAndy Whitcroft 1294f0a594c1SAndy Whitcroft last if ($level == 0); 12954a0df2efSAndy Whitcroft } 12964a0df2efSAndy Whitcroft 1297f0a594c1SAndy Whitcroft return ($level, @res); 12984a0df2efSAndy Whitcroft} 12994a0df2efSAndy Whitcroftsub ctx_block_outer { 13004a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 13014a0df2efSAndy Whitcroft 1302f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1303f0a594c1SAndy Whitcroft return @r; 13044a0df2efSAndy Whitcroft} 13054a0df2efSAndy Whitcroftsub ctx_block { 13064a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 13074a0df2efSAndy Whitcroft 1308f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1309f0a594c1SAndy Whitcroft return @r; 1310653d4876SAndy Whitcroft} 1311653d4876SAndy Whitcroftsub ctx_statement { 1312f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1313f0a594c1SAndy Whitcroft 1314f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1315f0a594c1SAndy Whitcroft return @r; 1316f0a594c1SAndy Whitcroft} 1317f0a594c1SAndy Whitcroftsub ctx_block_level { 1318653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1319653d4876SAndy Whitcroft 1320f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 13214a0df2efSAndy Whitcroft} 13229c0ca6f9SAndy Whitcroftsub ctx_statement_level { 13239c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 13249c0ca6f9SAndy Whitcroft 13259c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 13269c0ca6f9SAndy Whitcroft} 13274a0df2efSAndy Whitcroft 13284a0df2efSAndy Whitcroftsub ctx_locate_comment { 13294a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 13304a0df2efSAndy Whitcroft 13314a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1332beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 13334a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 13344a0df2efSAndy Whitcroft 13354a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 13364a0df2efSAndy Whitcroft # comment. 13374a0df2efSAndy Whitcroft my $in_comment = 0; 13384a0df2efSAndy Whitcroft $current_comment = ''; 13394a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 134000df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 134100df344fSAndy Whitcroft #warn " $line\n"; 13424a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 13434a0df2efSAndy Whitcroft $in_comment = 1; 13444a0df2efSAndy Whitcroft } 13454a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 13464a0df2efSAndy Whitcroft $in_comment = 1; 13474a0df2efSAndy Whitcroft } 13484a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 13494a0df2efSAndy Whitcroft $current_comment = ''; 13504a0df2efSAndy Whitcroft } 13514a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 13524a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 13534a0df2efSAndy Whitcroft $in_comment = 0; 13544a0df2efSAndy Whitcroft } 13554a0df2efSAndy Whitcroft } 13564a0df2efSAndy Whitcroft 13574a0df2efSAndy Whitcroft chomp($current_comment); 13584a0df2efSAndy Whitcroft return($current_comment); 13594a0df2efSAndy Whitcroft} 13604a0df2efSAndy Whitcroftsub ctx_has_comment { 13614a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 13624a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 13634a0df2efSAndy Whitcroft 136400df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 13654a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 13664a0df2efSAndy Whitcroft 13674a0df2efSAndy Whitcroft return ($cmt ne ''); 13684a0df2efSAndy Whitcroft} 13694a0df2efSAndy Whitcroft 13704d001e4dSAndy Whitcroftsub raw_line { 13714d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 13724d001e4dSAndy Whitcroft 13734d001e4dSAndy Whitcroft my $offset = $linenr - 1; 13744d001e4dSAndy Whitcroft $cnt++; 13754d001e4dSAndy Whitcroft 13764d001e4dSAndy Whitcroft my $line; 13774d001e4dSAndy Whitcroft while ($cnt) { 13784d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 13794d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 13804d001e4dSAndy Whitcroft $cnt--; 13814d001e4dSAndy Whitcroft } 13824d001e4dSAndy Whitcroft 13834d001e4dSAndy Whitcroft return $line; 13844d001e4dSAndy Whitcroft} 13854d001e4dSAndy Whitcroft 13860a920b5bSAndy Whitcroftsub cat_vet { 13870a920b5bSAndy Whitcroft my ($vet) = @_; 13889c0ca6f9SAndy Whitcroft my ($res, $coded); 13890a920b5bSAndy Whitcroft 13909c0ca6f9SAndy Whitcroft $res = ''; 13916c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 13926c72ffaaSAndy Whitcroft $res .= $1; 13936c72ffaaSAndy Whitcroft if ($2 ne '') { 13949c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 13956c72ffaaSAndy Whitcroft $res .= $coded; 13966c72ffaaSAndy Whitcroft } 13979c0ca6f9SAndy Whitcroft } 13989c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 13990a920b5bSAndy Whitcroft 14009c0ca6f9SAndy Whitcroft return $res; 14010a920b5bSAndy Whitcroft} 14020a920b5bSAndy Whitcroft 1403c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1404cf655043SAndy Whitcroftmy $av_pending; 1405c2fdda0dSAndy Whitcroftmy @av_paren_type; 14061f65f947SAndy Whitcroftmy $av_pend_colon; 1407c2fdda0dSAndy Whitcroft 1408c2fdda0dSAndy Whitcroftsub annotate_reset { 1409c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1410cf655043SAndy Whitcroft $av_pending = '_'; 1411cf655043SAndy Whitcroft @av_paren_type = ('E'); 14121f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1413c2fdda0dSAndy Whitcroft} 1414c2fdda0dSAndy Whitcroft 14156c72ffaaSAndy Whitcroftsub annotate_values { 14166c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 14176c72ffaaSAndy Whitcroft 14186c72ffaaSAndy Whitcroft my $res; 14191f65f947SAndy Whitcroft my $var = '_' x length($stream); 14206c72ffaaSAndy Whitcroft my $cur = $stream; 14216c72ffaaSAndy Whitcroft 1422c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 14236c72ffaaSAndy Whitcroft 14246c72ffaaSAndy Whitcroft while (length($cur)) { 1425773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1426cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1427171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 14286c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1429c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1430c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1431cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1432c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 14336c72ffaaSAndy Whitcroft } 14346c72ffaaSAndy Whitcroft 1435c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 14369446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 14379446ef56SAndy Whitcroft push(@av_paren_type, $type); 1438addcdceaSAndy Whitcroft $type = 'c'; 14399446ef56SAndy Whitcroft 1440e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1441c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 14426c72ffaaSAndy Whitcroft $type = 'T'; 14436c72ffaaSAndy Whitcroft 1444389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1445389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1446389a2fe5SAndy Whitcroft $type = 'T'; 1447389a2fe5SAndy Whitcroft 1448c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1449171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1450c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1451171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1452171ae1a4SAndy Whitcroft if ($2 ne '') { 1453cf655043SAndy Whitcroft $av_pending = 'N'; 1454171ae1a4SAndy Whitcroft } 1455171ae1a4SAndy Whitcroft $type = 'E'; 1456171ae1a4SAndy Whitcroft 1457c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1458171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1459171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1460171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 14616c72ffaaSAndy Whitcroft 1462c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1463cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1464c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1465cf655043SAndy Whitcroft 1466cf655043SAndy Whitcroft push(@av_paren_type, $type); 1467cf655043SAndy Whitcroft push(@av_paren_type, $type); 1468171ae1a4SAndy Whitcroft $type = 'E'; 1469cf655043SAndy Whitcroft 1470c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1471cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1472cf655043SAndy Whitcroft $av_preprocessor = 1; 1473cf655043SAndy Whitcroft 1474cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1475cf655043SAndy Whitcroft 1476171ae1a4SAndy Whitcroft $type = 'E'; 1477cf655043SAndy Whitcroft 1478c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1479cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1480cf655043SAndy Whitcroft 1481cf655043SAndy Whitcroft $av_preprocessor = 1; 1482cf655043SAndy Whitcroft 1483cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1484cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1485cf655043SAndy Whitcroft pop(@av_paren_type); 1486cf655043SAndy Whitcroft push(@av_paren_type, $type); 1487171ae1a4SAndy Whitcroft $type = 'E'; 14886c72ffaaSAndy Whitcroft 14896c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1490c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 14916c72ffaaSAndy Whitcroft 1492171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1493171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1494171ae1a4SAndy Whitcroft $av_pending = $type; 1495171ae1a4SAndy Whitcroft $type = 'N'; 1496171ae1a4SAndy Whitcroft 14976c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1498c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 14996c72ffaaSAndy Whitcroft if (defined $2) { 1500cf655043SAndy Whitcroft $av_pending = 'V'; 15016c72ffaaSAndy Whitcroft } 15026c72ffaaSAndy Whitcroft $type = 'N'; 15036c72ffaaSAndy Whitcroft 150414b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1505c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 150614b111c1SAndy Whitcroft $av_pending = 'E'; 15076c72ffaaSAndy Whitcroft $type = 'N'; 15086c72ffaaSAndy Whitcroft 15091f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 15101f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 15111f65f947SAndy Whitcroft $av_pend_colon = 'C'; 15121f65f947SAndy Whitcroft $type = 'N'; 15131f65f947SAndy Whitcroft 151414b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1515c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 15166c72ffaaSAndy Whitcroft $type = 'N'; 15176c72ffaaSAndy Whitcroft 15186c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1519c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1520cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1521cf655043SAndy Whitcroft $av_pending = '_'; 15226c72ffaaSAndy Whitcroft $type = 'N'; 15236c72ffaaSAndy Whitcroft 15246c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1525cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1526cf655043SAndy Whitcroft if ($new_type ne '_') { 1527cf655043SAndy Whitcroft $type = $new_type; 1528c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1529c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 15306c72ffaaSAndy Whitcroft } else { 1531c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 15326c72ffaaSAndy Whitcroft } 15336c72ffaaSAndy Whitcroft 1534c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1535c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1536c8cb2ca3SAndy Whitcroft $type = 'V'; 1537cf655043SAndy Whitcroft $av_pending = 'V'; 15386c72ffaaSAndy Whitcroft 15398e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 15408e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 15411f65f947SAndy Whitcroft $av_pend_colon = 'B'; 15428e761b04SAndy Whitcroft } elsif ($type eq 'E') { 15438e761b04SAndy Whitcroft $av_pend_colon = 'L'; 15441f65f947SAndy Whitcroft } 15451f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 15461f65f947SAndy Whitcroft $type = 'V'; 15471f65f947SAndy Whitcroft 15486c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1549c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 15506c72ffaaSAndy Whitcroft $type = 'V'; 15516c72ffaaSAndy Whitcroft 15526c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1553c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 15546c72ffaaSAndy Whitcroft $type = 'N'; 15556c72ffaaSAndy Whitcroft 1556cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1557c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 155813214adfSAndy Whitcroft $type = 'E'; 15591f65f947SAndy Whitcroft $av_pend_colon = 'O'; 156013214adfSAndy Whitcroft 15618e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 15628e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 15638e761b04SAndy Whitcroft $type = 'C'; 15648e761b04SAndy Whitcroft 15651f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 15661f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 15671f65f947SAndy Whitcroft $type = 'N'; 15681f65f947SAndy Whitcroft 15691f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 15701f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 15711f65f947SAndy Whitcroft 15721f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 15731f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 15741f65f947SAndy Whitcroft $type = 'E'; 15751f65f947SAndy Whitcroft } else { 15761f65f947SAndy Whitcroft $type = 'N'; 15771f65f947SAndy Whitcroft } 15781f65f947SAndy Whitcroft $av_pend_colon = 'O'; 15791f65f947SAndy Whitcroft 15808e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 158113214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 15826c72ffaaSAndy Whitcroft $type = 'N'; 15836c72ffaaSAndy Whitcroft 15840d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 158574048ed8SAndy Whitcroft my $variant; 158674048ed8SAndy Whitcroft 158774048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 158874048ed8SAndy Whitcroft if ($type eq 'V') { 158974048ed8SAndy Whitcroft $variant = 'B'; 159074048ed8SAndy Whitcroft } else { 159174048ed8SAndy Whitcroft $variant = 'U'; 159274048ed8SAndy Whitcroft } 159374048ed8SAndy Whitcroft 159474048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 159574048ed8SAndy Whitcroft $type = 'N'; 159674048ed8SAndy Whitcroft 15976c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1598c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 15996c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 16006c72ffaaSAndy Whitcroft $type = 'N'; 16016c72ffaaSAndy Whitcroft } 16026c72ffaaSAndy Whitcroft 16036c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1604c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 16056c72ffaaSAndy Whitcroft } 16066c72ffaaSAndy Whitcroft if (defined $1) { 16076c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 16086c72ffaaSAndy Whitcroft $res .= $type x length($1); 16096c72ffaaSAndy Whitcroft } 16106c72ffaaSAndy Whitcroft } 16116c72ffaaSAndy Whitcroft 16121f65f947SAndy Whitcroft return ($res, $var); 16136c72ffaaSAndy Whitcroft} 16146c72ffaaSAndy Whitcroft 16158905a67cSAndy Whitcroftsub possible { 161613214adfSAndy Whitcroft my ($possible, $line) = @_; 16179a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 16180776e594SAndy Whitcroft ^(?: 16190776e594SAndy Whitcroft $Modifier| 16200776e594SAndy Whitcroft $Storage| 16210776e594SAndy Whitcroft $Type| 16229a974fdbSAndy Whitcroft DEFINE_\S+ 16239a974fdbSAndy Whitcroft )$| 16249a974fdbSAndy Whitcroft ^(?: 16250776e594SAndy Whitcroft goto| 16260776e594SAndy Whitcroft return| 16270776e594SAndy Whitcroft case| 16280776e594SAndy Whitcroft else| 16290776e594SAndy Whitcroft asm|__asm__| 163089a88353SAndy Whitcroft do| 163189a88353SAndy Whitcroft \#| 163289a88353SAndy Whitcroft \#\#| 16339a974fdbSAndy Whitcroft )(?:\s|$)| 16340776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 16359a974fdbSAndy Whitcroft )}x; 16369a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 16379a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1638c45dcabdSAndy Whitcroft # Check for modifiers. 1639c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1640c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1641c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1642c45dcabdSAndy Whitcroft 1643c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1644c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1645d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 16469a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1647d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1648485ff23eSAlex Dowad push(@modifierListFile, $modifier); 1649d2506586SAndy Whitcroft } 16509a974fdbSAndy Whitcroft } 1651c45dcabdSAndy Whitcroft 1652c45dcabdSAndy Whitcroft } else { 165313214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1654485ff23eSAlex Dowad push(@typeListFile, $possible); 1655c45dcabdSAndy Whitcroft } 16568905a67cSAndy Whitcroft build_types(); 16570776e594SAndy Whitcroft } else { 16580776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 16598905a67cSAndy Whitcroft } 16608905a67cSAndy Whitcroft} 16618905a67cSAndy Whitcroft 16626c72ffaaSAndy Whitcroftmy $prefix = ''; 16636c72ffaaSAndy Whitcroft 1664000d1cc1SJoe Perchessub show_type { 1665cbec18afSJoe Perches my ($type) = @_; 166691bfe484SJoe Perches 1667cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 1668cbec18afSJoe Perches 1669cbec18afSJoe Perches return !defined $ignore_type{$type}; 1670000d1cc1SJoe Perches} 1671000d1cc1SJoe Perches 1672f0a594c1SAndy Whitcroftsub report { 1673cbec18afSJoe Perches my ($level, $type, $msg) = @_; 1674cbec18afSJoe Perches 1675cbec18afSJoe Perches if (!show_type($type) || 1676cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1677773647a0SAndy Whitcroft return 0; 1678773647a0SAndy Whitcroft } 1679*57230297SJoe Perches my $output = ''; 1680*57230297SJoe Perches if (-t STDOUT && $color) { 1681*57230297SJoe Perches if ($level eq 'ERROR') { 1682*57230297SJoe Perches $output .= RED; 1683*57230297SJoe Perches } elsif ($level eq 'WARNING') { 1684*57230297SJoe Perches $output .= YELLOW; 1685000d1cc1SJoe Perches } else { 1686*57230297SJoe Perches $output .= GREEN; 1687000d1cc1SJoe Perches } 1688*57230297SJoe Perches } 1689*57230297SJoe Perches $output .= $prefix . $level . ':'; 1690*57230297SJoe Perches if ($show_types) { 1691*57230297SJoe Perches $output .= BLUE if (-t STDOUT && $color); 1692*57230297SJoe Perches $output .= "$type:"; 1693*57230297SJoe Perches } 1694*57230297SJoe Perches $output .= RESET if (-t STDOUT && $color); 1695*57230297SJoe Perches $output .= ' ' . $msg . "\n"; 1696*57230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 16978905a67cSAndy Whitcroft 1698*57230297SJoe Perches push(our @report, $output); 1699773647a0SAndy Whitcroft 1700773647a0SAndy Whitcroft return 1; 1701f0a594c1SAndy Whitcroft} 1702cbec18afSJoe Perches 1703f0a594c1SAndy Whitcroftsub report_dump { 170413214adfSAndy Whitcroft our @report; 1705f0a594c1SAndy Whitcroft} 1706000d1cc1SJoe Perches 1707d752fcc8SJoe Perchessub fixup_current_range { 1708d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 1709d752fcc8SJoe Perches 1710d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 1711d752fcc8SJoe Perches my $o = $1; 1712d752fcc8SJoe Perches my $l = $2; 1713d752fcc8SJoe Perches my $no = $o + $offset; 1714d752fcc8SJoe Perches my $nl = $l + $length; 1715d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 1716d752fcc8SJoe Perches } 1717d752fcc8SJoe Perches} 1718d752fcc8SJoe Perches 1719d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 1720d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 1721d752fcc8SJoe Perches 1722d752fcc8SJoe Perches my $range_last_linenr = 0; 1723d752fcc8SJoe Perches my $delta_offset = 0; 1724d752fcc8SJoe Perches 1725d752fcc8SJoe Perches my $old_linenr = 0; 1726d752fcc8SJoe Perches my $new_linenr = 0; 1727d752fcc8SJoe Perches 1728d752fcc8SJoe Perches my $next_insert = 0; 1729d752fcc8SJoe Perches my $next_delete = 0; 1730d752fcc8SJoe Perches 1731d752fcc8SJoe Perches my @lines = (); 1732d752fcc8SJoe Perches 1733d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 1734d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 1735d752fcc8SJoe Perches 1736d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 1737d752fcc8SJoe Perches my $save_line = 1; 1738d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 1739323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 1740d752fcc8SJoe Perches $delta_offset = 0; 1741d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 1742d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 1743d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 1744d752fcc8SJoe Perches } 1745d752fcc8SJoe Perches 1746d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 1747d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 1748d752fcc8SJoe Perches $save_line = 0; 1749d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 1750d752fcc8SJoe Perches } 1751d752fcc8SJoe Perches 1752d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 1753d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 1754d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 1755d752fcc8SJoe Perches $new_linenr++; 1756d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 1757d752fcc8SJoe Perches } 1758d752fcc8SJoe Perches 1759d752fcc8SJoe Perches if ($save_line) { 1760d752fcc8SJoe Perches push(@lines, $line); 1761d752fcc8SJoe Perches $new_linenr++; 1762d752fcc8SJoe Perches } 1763d752fcc8SJoe Perches 1764d752fcc8SJoe Perches $old_linenr++; 1765d752fcc8SJoe Perches } 1766d752fcc8SJoe Perches 1767d752fcc8SJoe Perches return @lines; 1768d752fcc8SJoe Perches} 1769d752fcc8SJoe Perches 1770f2d7e4d4SJoe Perchessub fix_insert_line { 1771f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 1772f2d7e4d4SJoe Perches 1773f2d7e4d4SJoe Perches my $inserted = { 1774f2d7e4d4SJoe Perches LINENR => $linenr, 1775f2d7e4d4SJoe Perches LINE => $line, 1776f2d7e4d4SJoe Perches }; 1777f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 1778f2d7e4d4SJoe Perches} 1779f2d7e4d4SJoe Perches 1780f2d7e4d4SJoe Perchessub fix_delete_line { 1781f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 1782f2d7e4d4SJoe Perches 1783f2d7e4d4SJoe Perches my $deleted = { 1784f2d7e4d4SJoe Perches LINENR => $linenr, 1785f2d7e4d4SJoe Perches LINE => $line, 1786f2d7e4d4SJoe Perches }; 1787f2d7e4d4SJoe Perches 1788f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 1789f2d7e4d4SJoe Perches} 1790f2d7e4d4SJoe Perches 1791de7d4f0eSAndy Whitcroftsub ERROR { 1792cbec18afSJoe Perches my ($type, $msg) = @_; 1793cbec18afSJoe Perches 1794cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 1795de7d4f0eSAndy Whitcroft our $clean = 0; 17966c72ffaaSAndy Whitcroft our $cnt_error++; 17973705ce5bSJoe Perches return 1; 1798de7d4f0eSAndy Whitcroft } 17993705ce5bSJoe Perches return 0; 1800773647a0SAndy Whitcroft} 1801de7d4f0eSAndy Whitcroftsub WARN { 1802cbec18afSJoe Perches my ($type, $msg) = @_; 1803cbec18afSJoe Perches 1804cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 1805de7d4f0eSAndy Whitcroft our $clean = 0; 18066c72ffaaSAndy Whitcroft our $cnt_warn++; 18073705ce5bSJoe Perches return 1; 1808de7d4f0eSAndy Whitcroft } 18093705ce5bSJoe Perches return 0; 1810773647a0SAndy Whitcroft} 1811de7d4f0eSAndy Whitcroftsub CHK { 1812cbec18afSJoe Perches my ($type, $msg) = @_; 1813cbec18afSJoe Perches 1814cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 1815de7d4f0eSAndy Whitcroft our $clean = 0; 18166c72ffaaSAndy Whitcroft our $cnt_chk++; 18173705ce5bSJoe Perches return 1; 18186c72ffaaSAndy Whitcroft } 18193705ce5bSJoe Perches return 0; 1820de7d4f0eSAndy Whitcroft} 1821de7d4f0eSAndy Whitcroft 18226ecd9674SAndy Whitcroftsub check_absolute_file { 18236ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 18246ecd9674SAndy Whitcroft my $file = $absolute; 18256ecd9674SAndy Whitcroft 18266ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 18276ecd9674SAndy Whitcroft 18286ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 18296ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 18306ecd9674SAndy Whitcroft if (-f "$root/$file") { 18316ecd9674SAndy Whitcroft ##print "file<$file>\n"; 18326ecd9674SAndy Whitcroft last; 18336ecd9674SAndy Whitcroft } 18346ecd9674SAndy Whitcroft } 18356ecd9674SAndy Whitcroft if (! -f _) { 18366ecd9674SAndy Whitcroft return 0; 18376ecd9674SAndy Whitcroft } 18386ecd9674SAndy Whitcroft 18396ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 18406ecd9674SAndy Whitcroft my $prefix = $absolute; 18416ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 18426ecd9674SAndy Whitcroft 18436ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 18446ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1845000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1846000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 18476ecd9674SAndy Whitcroft } 18486ecd9674SAndy Whitcroft} 18496ecd9674SAndy Whitcroft 18503705ce5bSJoe Perchessub trim { 18513705ce5bSJoe Perches my ($string) = @_; 18523705ce5bSJoe Perches 1853b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 1854b34c648bSJoe Perches 1855b34c648bSJoe Perches return $string; 1856b34c648bSJoe Perches} 1857b34c648bSJoe Perches 1858b34c648bSJoe Perchessub ltrim { 1859b34c648bSJoe Perches my ($string) = @_; 1860b34c648bSJoe Perches 1861b34c648bSJoe Perches $string =~ s/^\s+//; 1862b34c648bSJoe Perches 1863b34c648bSJoe Perches return $string; 1864b34c648bSJoe Perches} 1865b34c648bSJoe Perches 1866b34c648bSJoe Perchessub rtrim { 1867b34c648bSJoe Perches my ($string) = @_; 1868b34c648bSJoe Perches 1869b34c648bSJoe Perches $string =~ s/\s+$//; 18703705ce5bSJoe Perches 18713705ce5bSJoe Perches return $string; 18723705ce5bSJoe Perches} 18733705ce5bSJoe Perches 187452ea8506SJoe Perchessub string_find_replace { 187552ea8506SJoe Perches my ($string, $find, $replace) = @_; 187652ea8506SJoe Perches 187752ea8506SJoe Perches $string =~ s/$find/$replace/g; 187852ea8506SJoe Perches 187952ea8506SJoe Perches return $string; 188052ea8506SJoe Perches} 188152ea8506SJoe Perches 18823705ce5bSJoe Perchessub tabify { 18833705ce5bSJoe Perches my ($leading) = @_; 18843705ce5bSJoe Perches 18853705ce5bSJoe Perches my $source_indent = 8; 18863705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 18873705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 18883705ce5bSJoe Perches 18893705ce5bSJoe Perches #convert leading spaces to tabs 18903705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 18913705ce5bSJoe Perches #Remove spaces before a tab 18923705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 18933705ce5bSJoe Perches 18943705ce5bSJoe Perches return "$leading"; 18953705ce5bSJoe Perches} 18963705ce5bSJoe Perches 1897d1fe9c09SJoe Perchessub pos_last_openparen { 1898d1fe9c09SJoe Perches my ($line) = @_; 1899d1fe9c09SJoe Perches 1900d1fe9c09SJoe Perches my $pos = 0; 1901d1fe9c09SJoe Perches 1902d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1903d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1904d1fe9c09SJoe Perches 1905d1fe9c09SJoe Perches my $last_openparen = 0; 1906d1fe9c09SJoe Perches 1907d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1908d1fe9c09SJoe Perches return -1; 1909d1fe9c09SJoe Perches } 1910d1fe9c09SJoe Perches 1911d1fe9c09SJoe Perches my $len = length($line); 1912d1fe9c09SJoe Perches 1913d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1914d1fe9c09SJoe Perches my $string = substr($line, $pos); 1915d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1916d1fe9c09SJoe Perches $pos += length($1) - 1; 1917d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1918d1fe9c09SJoe Perches $last_openparen = $pos; 1919d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1920d1fe9c09SJoe Perches last; 1921d1fe9c09SJoe Perches } 1922d1fe9c09SJoe Perches } 1923d1fe9c09SJoe Perches 192491cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 1925d1fe9c09SJoe Perches} 1926d1fe9c09SJoe Perches 19270a920b5bSAndy Whitcroftsub process { 19280a920b5bSAndy Whitcroft my $filename = shift; 19290a920b5bSAndy Whitcroft 19300a920b5bSAndy Whitcroft my $linenr=0; 19310a920b5bSAndy Whitcroft my $prevline=""; 1932c2fdda0dSAndy Whitcroft my $prevrawline=""; 19330a920b5bSAndy Whitcroft my $stashline=""; 1934c2fdda0dSAndy Whitcroft my $stashrawline=""; 19350a920b5bSAndy Whitcroft 19364a0df2efSAndy Whitcroft my $length; 19370a920b5bSAndy Whitcroft my $indent; 19380a920b5bSAndy Whitcroft my $previndent=0; 19390a920b5bSAndy Whitcroft my $stashindent=0; 19400a920b5bSAndy Whitcroft 1941de7d4f0eSAndy Whitcroft our $clean = 1; 19420a920b5bSAndy Whitcroft my $signoff = 0; 19430a920b5bSAndy Whitcroft my $is_patch = 0; 19440a920b5bSAndy Whitcroft 194529ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 194615662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 19472a076f40SJoe Perches my $commit_log_long_line = 0; 194813f1937eSJoe Perches my $reported_maintainer_file = 0; 1949fa64205dSPasi Savanainen my $non_utf8_charset = 0; 1950fa64205dSPasi Savanainen 1951365dd4eaSJoe Perches my $last_blank_line = 0; 19525e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 1953365dd4eaSJoe Perches 195413214adfSAndy Whitcroft our @report = (); 19556c72ffaaSAndy Whitcroft our $cnt_lines = 0; 19566c72ffaaSAndy Whitcroft our $cnt_error = 0; 19576c72ffaaSAndy Whitcroft our $cnt_warn = 0; 19586c72ffaaSAndy Whitcroft our $cnt_chk = 0; 19596c72ffaaSAndy Whitcroft 19600a920b5bSAndy Whitcroft # Trace the real file/line as we go. 19610a920b5bSAndy Whitcroft my $realfile = ''; 19620a920b5bSAndy Whitcroft my $realline = 0; 19630a920b5bSAndy Whitcroft my $realcnt = 0; 19640a920b5bSAndy Whitcroft my $here = ''; 19650a920b5bSAndy Whitcroft my $in_comment = 0; 1966c2fdda0dSAndy Whitcroft my $comment_edge = 0; 19670a920b5bSAndy Whitcroft my $first_line = 0; 19681e855726SWolfram Sang my $p1_prefix = ''; 19690a920b5bSAndy Whitcroft 197013214adfSAndy Whitcroft my $prev_values = 'E'; 197113214adfSAndy Whitcroft 197213214adfSAndy Whitcroft # suppression flags 1973773647a0SAndy Whitcroft my %suppress_ifbraces; 1974170d3a22SAndy Whitcroft my %suppress_whiletrailers; 19752b474a1aSAndy Whitcroft my %suppress_export; 19763e469cdcSAndy Whitcroft my $suppress_statement = 0; 1977653d4876SAndy Whitcroft 19787e51f197SJoe Perches my %signatures = (); 1979323c1260SJoe Perches 1980c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1981de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1982c2fdda0dSAndy Whitcroft # 1983de7d4f0eSAndy Whitcroft my @setup_docs = (); 1984de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1985773647a0SAndy Whitcroft 1986d8b07710SJoe Perches my $camelcase_file_seeded = 0; 1987d8b07710SJoe Perches 1988773647a0SAndy Whitcroft sanitise_line_reset(); 1989c2fdda0dSAndy Whitcroft my $line; 1990c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 1991773647a0SAndy Whitcroft $linenr++; 1992773647a0SAndy Whitcroft $line = $rawline; 1993c2fdda0dSAndy Whitcroft 19943705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 19953705ce5bSJoe Perches 1996773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 1997de7d4f0eSAndy Whitcroft $setup_docs = 0; 1998de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 1999de7d4f0eSAndy Whitcroft $setup_docs = 1; 2000de7d4f0eSAndy Whitcroft } 2001773647a0SAndy Whitcroft #next; 2002de7d4f0eSAndy Whitcroft } 2003773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2004773647a0SAndy Whitcroft $realline=$1-1; 2005773647a0SAndy Whitcroft if (defined $2) { 2006773647a0SAndy Whitcroft $realcnt=$3+1; 2007773647a0SAndy Whitcroft } else { 2008773647a0SAndy Whitcroft $realcnt=1+1; 2009773647a0SAndy Whitcroft } 2010c45dcabdSAndy Whitcroft $in_comment = 0; 2011773647a0SAndy Whitcroft 2012773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2013773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2014773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2015773647a0SAndy Whitcroft # at context start. 2016773647a0SAndy Whitcroft my $edge; 201701fa9147SAndy Whitcroft my $cnt = $realcnt; 201801fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 201901fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 202001fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 202101fa9147SAndy Whitcroft $cnt--; 202201fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2023721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2024fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2025fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2026fae17daeSAndy Whitcroft ($edge) = $1; 2027fae17daeSAndy Whitcroft last; 2028fae17daeSAndy Whitcroft } 2029773647a0SAndy Whitcroft } 2030773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2031773647a0SAndy Whitcroft $in_comment = 1; 2032773647a0SAndy Whitcroft } 2033773647a0SAndy Whitcroft 2034773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2035773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2036773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2037773647a0SAndy Whitcroft if (!defined $edge && 203883242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2039773647a0SAndy Whitcroft { 2040773647a0SAndy Whitcroft $in_comment = 1; 2041773647a0SAndy Whitcroft } 2042773647a0SAndy Whitcroft 2043773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2044773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2045773647a0SAndy Whitcroft 2046171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2047773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2048171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2049773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2050773647a0SAndy Whitcroft } 2051773647a0SAndy Whitcroft push(@lines, $line); 2052773647a0SAndy Whitcroft 2053773647a0SAndy Whitcroft if ($realcnt > 1) { 2054773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2055773647a0SAndy Whitcroft } else { 2056773647a0SAndy Whitcroft $realcnt = 0; 2057773647a0SAndy Whitcroft } 2058773647a0SAndy Whitcroft 2059773647a0SAndy Whitcroft #print "==>$rawline\n"; 2060773647a0SAndy Whitcroft #print "-->$line\n"; 2061de7d4f0eSAndy Whitcroft 2062de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2063de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2064de7d4f0eSAndy Whitcroft } 2065de7d4f0eSAndy Whitcroft } 2066de7d4f0eSAndy Whitcroft 20676c72ffaaSAndy Whitcroft $prefix = ''; 20686c72ffaaSAndy Whitcroft 2069773647a0SAndy Whitcroft $realcnt = 0; 2070773647a0SAndy Whitcroft $linenr = 0; 2071194f66fcSJoe Perches $fixlinenr = -1; 20720a920b5bSAndy Whitcroft foreach my $line (@lines) { 20730a920b5bSAndy Whitcroft $linenr++; 2074194f66fcSJoe Perches $fixlinenr++; 20751b5539b1SJoe Perches my $sline = $line; #copy of $line 20761b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 20770a920b5bSAndy Whitcroft 2078c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 20796c72ffaaSAndy Whitcroft 20800a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 20816c72ffaaSAndy Whitcroft if ($line=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 20820a920b5bSAndy Whitcroft $is_patch = 1; 20834a0df2efSAndy Whitcroft $first_line = $linenr + 1; 20840a920b5bSAndy Whitcroft $realline=$1-1; 20850a920b5bSAndy Whitcroft if (defined $2) { 20860a920b5bSAndy Whitcroft $realcnt=$3+1; 20870a920b5bSAndy Whitcroft } else { 20880a920b5bSAndy Whitcroft $realcnt=1+1; 20890a920b5bSAndy Whitcroft } 2090c2fdda0dSAndy Whitcroft annotate_reset(); 209113214adfSAndy Whitcroft $prev_values = 'E'; 209213214adfSAndy Whitcroft 2093773647a0SAndy Whitcroft %suppress_ifbraces = (); 2094170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 20952b474a1aSAndy Whitcroft %suppress_export = (); 20963e469cdcSAndy Whitcroft $suppress_statement = 0; 20970a920b5bSAndy Whitcroft next; 20980a920b5bSAndy Whitcroft 20994a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 21004a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 21014a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2102773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 21030a920b5bSAndy Whitcroft $realline++; 2104d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 21050a920b5bSAndy Whitcroft 21064a0df2efSAndy Whitcroft # Measure the line length and indent. 2107c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 21080a920b5bSAndy Whitcroft 21090a920b5bSAndy Whitcroft # Track the previous line. 21100a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 21110a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2112c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2113c2fdda0dSAndy Whitcroft 2114773647a0SAndy Whitcroft #warn "line<$line>\n"; 21156c72ffaaSAndy Whitcroft 2116d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2117d8aaf121SAndy Whitcroft $realcnt--; 21180a920b5bSAndy Whitcroft } 21190a920b5bSAndy Whitcroft 2120cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2121cc77cdcaSAndy Whitcroft 21220a920b5bSAndy Whitcroft#make up the handle for any error we report on this line 2123773647a0SAndy Whitcroft $prefix = "$filename:$realline: " if ($emacs && $file); 2124773647a0SAndy Whitcroft $prefix = "$filename:$linenr: " if ($emacs && !$file); 2125773647a0SAndy Whitcroft 21266c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 21276c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2128773647a0SAndy Whitcroft 21292ac73b4fSJoe Perches my $found_file = 0; 2130773647a0SAndy Whitcroft # extract the filename as it passes 21313bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 21323bf9a009SRabin Vincent $realfile = $1; 21332b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2134270c49a0SJoe Perches $in_commit_log = 0; 21352ac73b4fSJoe Perches $found_file = 1; 21363bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2137773647a0SAndy Whitcroft $realfile = $1; 21382b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2139270c49a0SJoe Perches $in_commit_log = 0; 21401e855726SWolfram Sang 21411e855726SWolfram Sang $p1_prefix = $1; 2142e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2143e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2144000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2145000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 21461e855726SWolfram Sang } 2147773647a0SAndy Whitcroft 2148c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2149000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2150000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2151773647a0SAndy Whitcroft } 21522ac73b4fSJoe Perches $found_file = 1; 21532ac73b4fSJoe Perches } 21542ac73b4fSJoe Perches 21552ac73b4fSJoe Perches if ($found_file) { 21562ac73b4fSJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@) { 21572ac73b4fSJoe Perches $check = 1; 21582ac73b4fSJoe Perches } else { 21592ac73b4fSJoe Perches $check = $check_orig; 21602ac73b4fSJoe Perches } 2161773647a0SAndy Whitcroft next; 2162773647a0SAndy Whitcroft } 2163773647a0SAndy Whitcroft 2164389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 21650a920b5bSAndy Whitcroft 2166c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2167c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2168c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 21690a920b5bSAndy Whitcroft 21706c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 21716c72ffaaSAndy Whitcroft 21723bf9a009SRabin Vincent# Check for incorrect file permissions 21733bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 21743bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 217504db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 217604db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2177000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2178000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 21793bf9a009SRabin Vincent } 21803bf9a009SRabin Vincent } 21813bf9a009SRabin Vincent 218220112475SJoe Perches# Check the patch for a signoff: 2183d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 21844a0df2efSAndy Whitcroft $signoff++; 218515662b3eSJoe Perches $in_commit_log = 0; 21860a920b5bSAndy Whitcroft } 218720112475SJoe Perches 2188e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2189e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2190e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2191e0d975b1SJoe Perches $reported_maintainer_file = 1; 2192e0d975b1SJoe Perches } 2193e0d975b1SJoe Perches 219420112475SJoe Perches# Check signature styles 2195270c49a0SJoe Perches if (!$in_header_lines && 2196ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 219720112475SJoe Perches my $space_before = $1; 219820112475SJoe Perches my $sign_off = $2; 219920112475SJoe Perches my $space_after = $3; 220020112475SJoe Perches my $email = $4; 220120112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 220220112475SJoe Perches 2203ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2204ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2205ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2206ce0338dfSJoe Perches } 220720112475SJoe Perches if (defined $space_before && $space_before ne "") { 22083705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 22093705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 22103705ce5bSJoe Perches $fix) { 2211194f66fcSJoe Perches $fixed[$fixlinenr] = 22123705ce5bSJoe Perches "$ucfirst_sign_off $email"; 22133705ce5bSJoe Perches } 221420112475SJoe Perches } 221520112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 22163705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 22173705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 22183705ce5bSJoe Perches $fix) { 2219194f66fcSJoe Perches $fixed[$fixlinenr] = 22203705ce5bSJoe Perches "$ucfirst_sign_off $email"; 22213705ce5bSJoe Perches } 22223705ce5bSJoe Perches 222320112475SJoe Perches } 222420112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 22253705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 22263705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 22273705ce5bSJoe Perches $fix) { 2228194f66fcSJoe Perches $fixed[$fixlinenr] = 22293705ce5bSJoe Perches "$ucfirst_sign_off $email"; 22303705ce5bSJoe Perches } 223120112475SJoe Perches } 223220112475SJoe Perches 223320112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 223420112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 223520112475SJoe Perches if ($suggested_email eq "") { 2236000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2237000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 223820112475SJoe Perches } else { 223920112475SJoe Perches my $dequoted = $suggested_email; 224020112475SJoe Perches $dequoted =~ s/^"//; 224120112475SJoe Perches $dequoted =~ s/" </ </; 224220112475SJoe Perches # Don't force email to have quotes 224320112475SJoe Perches # Allow just an angle bracketed address 224420112475SJoe Perches if ("$dequoted$comment" ne $email && 224520112475SJoe Perches "<$email_address>$comment" ne $email && 224620112475SJoe Perches "$suggested_email$comment" ne $email) { 2247000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2248000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 224920112475SJoe Perches } 22500a920b5bSAndy Whitcroft } 22517e51f197SJoe Perches 22527e51f197SJoe Perches# Check for duplicate signatures 22537e51f197SJoe Perches my $sig_nospace = $line; 22547e51f197SJoe Perches $sig_nospace =~ s/\s//g; 22557e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 22567e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 22577e51f197SJoe Perches WARN("BAD_SIGN_OFF", 22587e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 22597e51f197SJoe Perches } else { 22607e51f197SJoe Perches $signatures{$sig_nospace} = 1; 22617e51f197SJoe Perches } 22620a920b5bSAndy Whitcroft } 22630a920b5bSAndy Whitcroft 2264a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2265a2fe16b9SJoe Perches if ($in_header_lines && 2266a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2267a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2268a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2269a2fe16b9SJoe Perches } 2270a2fe16b9SJoe Perches 22719b3189ebSJoe Perches# Check for old stable address 22729b3189ebSJoe Perches if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) { 22739b3189ebSJoe Perches ERROR("STABLE_ADDRESS", 22749b3189ebSJoe Perches "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr); 22759b3189ebSJoe Perches } 22769b3189ebSJoe Perches 22777ebd05efSChristopher Covington# Check for unwanted Gerrit info 22787ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 22797ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 22807ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 22817ebd05efSChristopher Covington } 22827ebd05efSChristopher Covington 22832a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 22842a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 22852a076f40SJoe Perches length($line) > 75) { 22862a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 22872a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 22882a076f40SJoe Perches $commit_log_long_line = 1; 22892a076f40SJoe Perches } 22902a076f40SJoe Perches 22910d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 22920d7835fcSJoe Perches if ($in_commit_log && $line =~ /\b(c)ommit\s+([0-9a-f]{5,})/i) { 2293d311cd44SJoe Perches my $init_char = $1; 2294d311cd44SJoe Perches my $orig_commit = lc($2); 22950d7835fcSJoe Perches my $short = 1; 22960d7835fcSJoe Perches my $long = 0; 22970d7835fcSJoe Perches my $case = 1; 22980d7835fcSJoe Perches my $space = 1; 22990d7835fcSJoe Perches my $hasdesc = 0; 230019c146a6SJoe Perches my $hasparens = 0; 23010d7835fcSJoe Perches my $id = '0123456789ab'; 23020d7835fcSJoe Perches my $orig_desc = "commit description"; 23030d7835fcSJoe Perches my $description = ""; 23040d7835fcSJoe Perches 23050d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 23060d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 23070d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 23080d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 23090d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 23100d7835fcSJoe Perches $orig_desc = $1; 231119c146a6SJoe Perches $hasparens = 1; 23120d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 23130d7835fcSJoe Perches defined $rawlines[$linenr] && 23140d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 23150d7835fcSJoe Perches $orig_desc = $1; 231619c146a6SJoe Perches $hasparens = 1; 2317b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2318b671fde0SJoe Perches defined $rawlines[$linenr] && 2319b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2320b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2321b671fde0SJoe Perches $orig_desc = $1; 2322b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2323b671fde0SJoe Perches $orig_desc .= " " . $1; 232419c146a6SJoe Perches $hasparens = 1; 23250d7835fcSJoe Perches } 23260d7835fcSJoe Perches 23270d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 23280d7835fcSJoe Perches $id, $orig_desc); 23290d7835fcSJoe Perches 233019c146a6SJoe Perches if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) { 2331d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 23320d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 23330d7835fcSJoe Perches } 2334d311cd44SJoe Perches } 2335d311cd44SJoe Perches 233613f1937eSJoe Perches# Check for added, moved or deleted files 233713f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 233813f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 233913f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 234013f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 234113f1937eSJoe Perches (defined($1) || defined($2))))) { 234213f1937eSJoe Perches $reported_maintainer_file = 1; 234313f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 234413f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 234513f1937eSJoe Perches } 234613f1937eSJoe Perches 234700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 23488905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2349000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2350000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 23516c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2352de7d4f0eSAndy Whitcroft } 2353de7d4f0eSAndy Whitcroft 23546ecd9674SAndy Whitcroft# Check for absolute kernel paths. 23556ecd9674SAndy Whitcroft if ($tree) { 23566ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 23576ecd9674SAndy Whitcroft my $file = $1; 23586ecd9674SAndy Whitcroft 23596ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 23606ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 23616ecd9674SAndy Whitcroft # 23626ecd9674SAndy Whitcroft } else { 23636ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 23646ecd9674SAndy Whitcroft } 23656ecd9674SAndy Whitcroft } 23666ecd9674SAndy Whitcroft } 23676ecd9674SAndy Whitcroft 2368de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2369de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2370171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2371171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2372171ae1a4SAndy Whitcroft 2373171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2374171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2375171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2376171ae1a4SAndy Whitcroft 237734d99219SJoe Perches CHK("INVALID_UTF8", 2378000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 237900df344fSAndy Whitcroft } 23800a920b5bSAndy Whitcroft 238115662b3eSJoe Perches# Check if it's the start of a commit log 238215662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 238315662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 238429ee1b0cSJoe Perches !($rawline =~ /^\s+\S/ || 238529ee1b0cSJoe Perches $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { 238615662b3eSJoe Perches $in_header_lines = 0; 238715662b3eSJoe Perches $in_commit_log = 1; 238815662b3eSJoe Perches } 238915662b3eSJoe Perches 2390fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2391fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2392fa64205dSPasi Savanainen if ($in_header_lines && 2393fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2394fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2395fa64205dSPasi Savanainen $non_utf8_charset = 1; 2396fa64205dSPasi Savanainen } 2397fa64205dSPasi Savanainen 2398fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 239915662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2400fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 240115662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 240215662b3eSJoe Perches } 240315662b3eSJoe Perches 240466b47b4aSKees Cook# Check for various typo / spelling mistakes 240566d7a382SJoe Perches if (defined($misspellings) && 240666d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2407ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 240866b47b4aSKees Cook my $typo = $1; 240966b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 241066b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 241166b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 241266b47b4aSKees Cook my $msg_type = \&WARN; 241366b47b4aSKees Cook $msg_type = \&CHK if ($file); 241466b47b4aSKees Cook if (&{$msg_type}("TYPO_SPELLING", 241566b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 241666b47b4aSKees Cook $fix) { 241766b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 241866b47b4aSKees Cook } 241966b47b4aSKees Cook } 242066b47b4aSKees Cook } 242166b47b4aSKees Cook 242230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 242330670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 242400df344fSAndy Whitcroft 24250a920b5bSAndy Whitcroft#trailing whitespace 24269c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2427c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2428d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2429d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2430d5e616fcSJoe Perches $fix) { 2431194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2432d5e616fcSJoe Perches } 2433c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2434c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 24353705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 24363705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 24373705ce5bSJoe Perches $fix) { 2438194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 24393705ce5bSJoe Perches } 24403705ce5bSJoe Perches 2441d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 24420a920b5bSAndy Whitcroft } 24435368df20SAndy Whitcroft 24444783f894SJosh Triplett# Check for FSF mailing addresses. 2445109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 24463e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 24473e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 24484783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 24494783f894SJosh Triplett my $msg_type = \&ERROR; 24504783f894SJosh Triplett $msg_type = \&CHK if ($file); 24514783f894SJosh Triplett &{$msg_type}("FSF_MAILING_ADDRESS", 24524783f894SJosh 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) 24534783f894SJosh Triplett } 24544783f894SJosh Triplett 24553354957aSAndi Kleen# check for Kconfig help text having a real description 24569fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 24579fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 24583354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 24598d73e0e7SJoe Perches $line =~ /^\+\s*config\s+/) { 24603354957aSAndi Kleen my $length = 0; 24619fe287d7SAndy Whitcroft my $cnt = $realcnt; 24629fe287d7SAndy Whitcroft my $ln = $linenr + 1; 24639fe287d7SAndy Whitcroft my $f; 2464a1385803SAndy Whitcroft my $is_start = 0; 24659fe287d7SAndy Whitcroft my $is_end = 0; 2466a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 24679fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 24689fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 24699fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 24709fe287d7SAndy Whitcroft 24719fe287d7SAndy Whitcroft next if ($f =~ /^-/); 24728d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 2473a1385803SAndy Whitcroft 24748d73e0e7SJoe Perches if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) { 2475a1385803SAndy Whitcroft $is_start = 1; 24768d73e0e7SJoe Perches } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { 2477a1385803SAndy Whitcroft $length = -1; 2478a1385803SAndy Whitcroft } 2479a1385803SAndy Whitcroft 24809fe287d7SAndy Whitcroft $f =~ s/^.//; 24813354957aSAndi Kleen $f =~ s/#.*//; 24823354957aSAndi Kleen $f =~ s/^\s+//; 24833354957aSAndi Kleen next if ($f =~ /^$/); 24849fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 24859fe287d7SAndy Whitcroft $is_end = 1; 24869fe287d7SAndy Whitcroft last; 24879fe287d7SAndy Whitcroft } 24883354957aSAndi Kleen $length++; 24893354957aSAndi Kleen } 249056193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 2491000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 249256193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 249356193274SVadim Bendebury } 2494a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 24953354957aSAndi Kleen } 24963354957aSAndi Kleen 24971ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 24981ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 24991ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 25001ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 25011ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 25021ba8dfd1SKees Cook } 25031ba8dfd1SKees Cook 2504327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 2505327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 2506327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 2507327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 2508327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 2509327953e9SChristoph Jaeger } 2510327953e9SChristoph Jaeger 2511c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2512c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2513c68e5878SArnaud Lacombe my $flag = $1; 2514c68e5878SArnaud Lacombe my $replacement = { 2515c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2516c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2517c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2518c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2519c68e5878SArnaud Lacombe }; 2520c68e5878SArnaud Lacombe 2521c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2522c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2523c68e5878SArnaud Lacombe } 2524c68e5878SArnaud Lacombe 2525bff5da43SRob Herring# check for DT compatible documentation 25267dd05b38SFlorian Vaussard if (defined $root && 25277dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 25287dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 25297dd05b38SFlorian Vaussard 2530bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2531bff5da43SRob Herring 2532cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2533cc93319bSFlorian Vaussard my $vp_file = $dt_path . "vendor-prefixes.txt"; 2534cc93319bSFlorian Vaussard 2535bff5da43SRob Herring foreach my $compat (@compats) { 2536bff5da43SRob Herring my $compat2 = $compat; 2537185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 2538185d566bSRob Herring my $compat3 = $compat; 2539185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 2540185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 2541bff5da43SRob Herring if ( $? >> 8 ) { 2542bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2543bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2544bff5da43SRob Herring } 2545bff5da43SRob Herring 25464fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 25474fbf32a6SFlorian Vaussard my $vendor = $1; 2548cc93319bSFlorian Vaussard `grep -Eq "^$vendor\\b" $vp_file`; 2549bff5da43SRob Herring if ( $? >> 8 ) { 2550bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2551cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 2552bff5da43SRob Herring } 2553bff5da43SRob Herring } 2554bff5da43SRob Herring } 2555bff5da43SRob Herring 25565368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 2557de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/); 25585368df20SAndy Whitcroft 255947e0c88bSJoe Perches# line length limit (with some exclusions) 256047e0c88bSJoe Perches# 256147e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 256247e0c88bSJoe Perches# logging functions like pr_info that end in a string 256347e0c88bSJoe Perches# lines with a single string 256447e0c88bSJoe Perches# #defines that are a single string 256547e0c88bSJoe Perches# 256647e0c88bSJoe Perches# There are 3 different line length message types: 256747e0c88bSJoe Perches# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength 256847e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 256947e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 257047e0c88bSJoe Perches# 257147e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 257247e0c88bSJoe Perches# 257347e0c88bSJoe Perches 257447e0c88bSJoe Perches if ($length > $max_line_length) { 257547e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 257647e0c88bSJoe Perches 257747e0c88bSJoe Perches # Check the allowed long line types first 257847e0c88bSJoe Perches 257947e0c88bSJoe Perches # logging functions that end in a string that starts 258047e0c88bSJoe Perches # before $max_line_length 258147e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 258247e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 258347e0c88bSJoe Perches $msg_type = ""; 258447e0c88bSJoe Perches 258547e0c88bSJoe Perches # lines with only strings (w/ possible termination) 258647e0c88bSJoe Perches # #defines with only strings 258747e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 258847e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 258947e0c88bSJoe Perches $msg_type = ""; 259047e0c88bSJoe Perches 259147e0c88bSJoe Perches # Otherwise set the alternate message types 259247e0c88bSJoe Perches 259347e0c88bSJoe Perches # a comment starts before $max_line_length 259447e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 259547e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 259647e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 259747e0c88bSJoe Perches 259847e0c88bSJoe Perches # a quoted string starts before $max_line_length 259947e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 260047e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 260147e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 260247e0c88bSJoe Perches } 260347e0c88bSJoe Perches 260447e0c88bSJoe Perches if ($msg_type ne "" && 260547e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 260647e0c88bSJoe Perches WARN($msg_type, 26076cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 26080a920b5bSAndy Whitcroft } 260947e0c88bSJoe Perches } 26100a920b5bSAndy Whitcroft 26118905a67cSAndy Whitcroft# check for adding lines without a newline. 26128905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2613000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 2614000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 26158905a67cSAndy Whitcroft } 26168905a67cSAndy Whitcroft 261742e41c54SMike Frysinger# Blackfin: use hi/lo macros 261842e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 261942e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 262042e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2621000d1cc1SJoe Perches ERROR("LO_MACRO", 2622000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 262342e41c54SMike Frysinger } 262442e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 262542e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2626000d1cc1SJoe Perches ERROR("HI_MACRO", 2627000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 262842e41c54SMike Frysinger } 262942e41c54SMike Frysinger } 263042e41c54SMike Frysinger 2631b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 2632de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 26330a920b5bSAndy Whitcroft 26340a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 26350a920b5bSAndy Whitcroft# more than 8 must use tabs. 2636c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 2637c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 2638c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2639d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 26403705ce5bSJoe Perches if (ERROR("CODE_INDENT", 26413705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 26423705ce5bSJoe Perches $fix) { 2643194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 26443705ce5bSJoe Perches } 26450a920b5bSAndy Whitcroft } 26460a920b5bSAndy Whitcroft 264708e44365SAlberto Panizzo# check for space before tabs. 264808e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 264908e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 26503705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 26513705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 26523705ce5bSJoe Perches $fix) { 2653194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 2654d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 2655194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 2656c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 26573705ce5bSJoe Perches } 265808e44365SAlberto Panizzo } 265908e44365SAlberto Panizzo 2660d1fe9c09SJoe Perches# check for && or || at the start of a line 2661d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 2662d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 2663d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 2664d1fe9c09SJoe Perches } 2665d1fe9c09SJoe Perches 2666d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 2667d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 266891cb5195SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 2669d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 2670d1fe9c09SJoe Perches my $oldindent = $1; 2671d1fe9c09SJoe Perches my $rest = $2; 2672d1fe9c09SJoe Perches 2673d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 2674d1fe9c09SJoe Perches if ($pos >= 0) { 2675b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 2676b34a26f3SJoe Perches my $newindent = $2; 2677d1fe9c09SJoe Perches 2678d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 2679d1fe9c09SJoe Perches "\t" x ($pos / 8) . 2680d1fe9c09SJoe Perches " " x ($pos % 8); 2681d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 2682d1fe9c09SJoe Perches 2683d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 2684d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 26853705ce5bSJoe Perches 26863705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 26873705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 26883705ce5bSJoe Perches $fix && $line =~ /^\+/) { 2689194f66fcSJoe Perches $fixed[$fixlinenr] =~ 26903705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 26913705ce5bSJoe Perches } 2692d1fe9c09SJoe Perches } 2693d1fe9c09SJoe Perches } 2694d1fe9c09SJoe Perches } 2695d1fe9c09SJoe Perches 26966ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 26976ab3a970SJoe Perches# avoid checking a few false positives: 26986ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 26996ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 27006ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 27016ab3a970SJoe Perches# multiline macros that define functions 27026ab3a970SJoe Perches# known attributes or the __attribute__ keyword 27036ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 27046ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 27053705ce5bSJoe Perches if (CHK("SPACING", 2706f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 27073705ce5bSJoe Perches $fix) { 2708194f66fcSJoe Perches $fixed[$fixlinenr] =~ 2709f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 27103705ce5bSJoe Perches } 2711aad4f614SJoe Perches } 2712aad4f614SJoe Perches 271305880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2714fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 271585ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 271685ad978cSJoe Perches $realline > 2) { 271705880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 271805880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 271905880600SJoe Perches } 272005880600SJoe Perches 272105880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2722a605e32eSJoe Perches $prevrawline =~ /^\+[ \t]*\/\*/ && #starting /* 2723a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 272461135e96SJoe Perches $rawline =~ /^\+/ && #line is new 2725a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 2726a605e32eSJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 2727a605e32eSJoe Perches "networking block comments start with * on subsequent lines\n" . $hereprev); 2728a605e32eSJoe Perches } 2729a605e32eSJoe Perches 2730a605e32eSJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2731c24f9f19SJoe Perches $rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2732c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2733c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2734c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 273505880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 273605880600SJoe Perches "networking block comments put the trailing */ on a separate line\n" . $herecurr); 273705880600SJoe Perches } 273805880600SJoe Perches 27397f619191SJoe Perches# check for missing blank lines after struct/union declarations 27407f619191SJoe Perches# with exceptions for various attributes and macros 27417f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 27427f619191SJoe Perches $line =~ /^\+/ && 27437f619191SJoe Perches !($line =~ /^\+\s*$/ || 27447f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 27457f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 27467f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 27477f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 27487f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 27497f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 27507f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 2751d752fcc8SJoe Perches if (CHK("LINE_SPACING", 2752d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 2753d752fcc8SJoe Perches $fix) { 2754f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 2755d752fcc8SJoe Perches } 27567f619191SJoe Perches } 27577f619191SJoe Perches 2758365dd4eaSJoe Perches# check for multiple consecutive blank lines 2759365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 2760365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 2761365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 2762d752fcc8SJoe Perches if (CHK("LINE_SPACING", 2763d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 2764d752fcc8SJoe Perches $fix) { 2765f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 2766d752fcc8SJoe Perches } 2767d752fcc8SJoe Perches 2768365dd4eaSJoe Perches $last_blank_line = $linenr; 2769365dd4eaSJoe Perches } 2770365dd4eaSJoe Perches 27713b617e3bSJoe Perches# check for missing blank lines after declarations 27723f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 27733f7bac03SJoe Perches # actual declarations 27743f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 27755a4e1fd3SJoe Perches # function pointer declarations 27765a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 27773f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 27783f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 27793f7bac03SJoe Perches # known declaration macros 27803f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 27813f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 27823f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 27833f7bac03SJoe Perches # other possible extensions of declaration lines 27843f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 27853f7bac03SJoe Perches # not starting a section or a macro "\" extended line 27863f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 27873f7bac03SJoe Perches # looks like a declaration 27883f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 27895a4e1fd3SJoe Perches # function pointer declarations 27905a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 27913f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 27923f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 27933f7bac03SJoe Perches # known declaration macros 27943f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 27953f7bac03SJoe Perches # start of struct or union or enum 27963b617e3bSJoe Perches $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || 27973f7bac03SJoe Perches # start or end of block or continuation of declaration 27983f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 27993f7bac03SJoe Perches # bitfield continuation 28003f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 28013f7bac03SJoe Perches # other possible extensions of declaration lines 28023f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 28033f7bac03SJoe Perches # indentation of previous and current line are the same 28043f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 2805d752fcc8SJoe Perches if (WARN("LINE_SPACING", 2806d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 2807d752fcc8SJoe Perches $fix) { 2808f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 2809d752fcc8SJoe Perches } 28103b617e3bSJoe Perches } 28113b617e3bSJoe Perches 28125f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 28136b4c5bebSAndy Whitcroft# Exceptions: 28146b4c5bebSAndy Whitcroft# 1) within comments 28156b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 28166b4c5bebSAndy Whitcroft# 3) hanging labels 28173705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 28185f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28193705ce5bSJoe Perches if (WARN("LEADING_SPACE", 28203705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 28213705ce5bSJoe Perches $fix) { 2822194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 28233705ce5bSJoe Perches } 28245f7ddae6SRaffaele Recalcati } 28255f7ddae6SRaffaele Recalcati 2826b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 2827b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 2828b9ea10d6SAndy Whitcroft 2829032a4c0fSJoe Perches# check indentation of any line with a bare else 2830840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 2831032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 2832032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 2833032a4c0fSJoe Perches my $tabs = length($1) + 1; 2834840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 2835840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 2836840080a0SJoe Perches defined $lines[$linenr] && 2837840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 2838032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 2839032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 2840032a4c0fSJoe Perches } 2841032a4c0fSJoe Perches } 2842032a4c0fSJoe Perches 2843c00df19aSJoe Perches# check indentation of a line with a break; 2844c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 2845c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 2846c00df19aSJoe Perches my $tabs = $1; 2847c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 2848c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 2849c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 2850c00df19aSJoe Perches } 2851c00df19aSJoe Perches } 2852c00df19aSJoe Perches 28531ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 28541ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 28551ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 28561ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 28571ba8dfd1SKees Cook } 28581ba8dfd1SKees Cook 2859c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 2860cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 2861000d1cc1SJoe Perches WARN("CVS_KEYWORD", 2862000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 2863c2fdda0dSAndy Whitcroft } 286422f2a2efSAndy Whitcroft 286542e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 286642e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 286742e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2868000d1cc1SJoe Perches ERROR("CSYNC", 2869000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 287042e41c54SMike Frysinger } 287142e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 287242e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2873000d1cc1SJoe Perches ERROR("SSYNC", 2874000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 287542e41c54SMike Frysinger } 287642e41c54SMike Frysinger 287756e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 287856e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 287956e77d70SJoe Perches WARN("HOTPLUG_SECTION", 288056e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 288156e77d70SJoe Perches } 288256e77d70SJoe Perches 28839c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 28842b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 28852b474a1aSAndy Whitcroft $realline_next); 28863e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 28873e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 28881b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 2889170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2890f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2891171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 2892171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 2893171ae1a4SAndy Whitcroft 28943e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 28953e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 28963e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 28973e469cdcSAndy Whitcroft # until we hit end of it. 28983e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 28993e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 29003e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 29013e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 29023e469cdcSAndy Whitcroft } 2903f74bd194SAndy Whitcroft 29042b474a1aSAndy Whitcroft # Find the real next line. 29052b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 29062b474a1aSAndy Whitcroft if (defined $realline_next && 29072b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 29082b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 29092b474a1aSAndy Whitcroft $realline_next++; 29102b474a1aSAndy Whitcroft } 29112b474a1aSAndy Whitcroft 2912171ae1a4SAndy Whitcroft my $s = $stat; 2913171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 2914cf655043SAndy Whitcroft 2915c2fdda0dSAndy Whitcroft # Ignore goto labels. 2916171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 2917c2fdda0dSAndy Whitcroft 2918c2fdda0dSAndy Whitcroft # Ignore functions being called 2919171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 2920c2fdda0dSAndy Whitcroft 2921463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 2922463f2864SAndy Whitcroft 2923c45dcabdSAndy Whitcroft # declarations always start with types 2924d2506586SAndy 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) { 2925c45dcabdSAndy Whitcroft my $type = $1; 2926c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 2927c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 2928c45dcabdSAndy Whitcroft 29296c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 2930a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 2931c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 2932c2fdda0dSAndy Whitcroft } 29338905a67cSAndy Whitcroft 29346c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 293565863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 2936c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 29379c0ca6f9SAndy Whitcroft } 29388905a67cSAndy Whitcroft 29398905a67cSAndy Whitcroft # Check for any sort of function declaration. 29408905a67cSAndy Whitcroft # int foo(something bar, other baz); 29418905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 2942171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 29438905a67cSAndy Whitcroft my ($name_len) = length($1); 29448905a67cSAndy Whitcroft 2945cf655043SAndy Whitcroft my $ctx = $s; 2946773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 29478905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 2948cf655043SAndy Whitcroft 29498905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 2950c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 29518905a67cSAndy Whitcroft 2952c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 29538905a67cSAndy Whitcroft } 29548905a67cSAndy Whitcroft } 29558905a67cSAndy Whitcroft } 29568905a67cSAndy Whitcroft 29579c0ca6f9SAndy Whitcroft } 29589c0ca6f9SAndy Whitcroft 295900df344fSAndy Whitcroft# 296000df344fSAndy Whitcroft# Checks which may be anchored in the context. 296100df344fSAndy Whitcroft# 296200df344fSAndy Whitcroft 296300df344fSAndy Whitcroft# Check for switch () and associated case and default 296400df344fSAndy Whitcroft# statements should be at the same indent. 296500df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 296600df344fSAndy Whitcroft my $err = ''; 296700df344fSAndy Whitcroft my $sep = ''; 296800df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 296900df344fSAndy Whitcroft shift(@ctx); 297000df344fSAndy Whitcroft for my $ctx (@ctx) { 297100df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 297200df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 297300df344fSAndy Whitcroft $indent != $cindent) { 297400df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 297500df344fSAndy Whitcroft $sep = ''; 297600df344fSAndy Whitcroft } else { 297700df344fSAndy Whitcroft $sep = "[...]\n"; 297800df344fSAndy Whitcroft } 297900df344fSAndy Whitcroft } 298000df344fSAndy Whitcroft if ($err ne '') { 2981000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 2982000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 2983de7d4f0eSAndy Whitcroft } 2984de7d4f0eSAndy Whitcroft } 2985de7d4f0eSAndy Whitcroft 2986de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 2987de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 29880fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 2989773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 2990773647a0SAndy Whitcroft 29919c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 29928eef05ddSJoe Perches 29938eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 29948eef05ddSJoe Perches WARN("DEEP_INDENTATION", 29958eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 29968eef05ddSJoe Perches } 29978eef05ddSJoe Perches 2998de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 2999de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3000de7d4f0eSAndy Whitcroft 3001548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3002548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3003de7d4f0eSAndy Whitcroft 3004548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3005548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3006548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3007548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3008548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3009773647a0SAndy Whitcroft $ctx_ln++; 3010773647a0SAndy Whitcroft } 3011548596d5SAndy Whitcroft 301253210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 301353210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3014773647a0SAndy Whitcroft 3015773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3016000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3017000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 301801464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 301900df344fSAndy Whitcroft } 3020773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3021773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3022773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3023773647a0SAndy Whitcroft { 30249c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 30259c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3026000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3027000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 302801464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 30299c0ca6f9SAndy Whitcroft } 30309c0ca6f9SAndy Whitcroft } 303100df344fSAndy Whitcroft } 303200df344fSAndy Whitcroft 30334d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 30340fe3dc2bSJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 30353e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 30363e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 30373e469cdcSAndy Whitcroft if (!defined $stat); 30384d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 30394d001e4dSAndy Whitcroft 30404d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 30414d001e4dSAndy Whitcroft 30424d001e4dSAndy Whitcroft # Make sure we remove the line prefixes as we have 30434d001e4dSAndy Whitcroft # none on the first line, and are going to readd them 30444d001e4dSAndy Whitcroft # where necessary. 30454d001e4dSAndy Whitcroft $s =~ s/\n./\n/gs; 30464d001e4dSAndy Whitcroft 30474d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 30486f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 30496f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 30504d001e4dSAndy Whitcroft 30514d001e4dSAndy Whitcroft # We want to check the first line inside the block 30524d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 30534d001e4dSAndy Whitcroft # 1) any blank line termination 30544d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 30554d001e4dSAndy Whitcroft # 3) any do (...) { 30564d001e4dSAndy Whitcroft my $continuation = 0; 30574d001e4dSAndy Whitcroft my $check = 0; 30584d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 30594d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 30604d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 30614d001e4dSAndy Whitcroft $continuation = 1; 30624d001e4dSAndy Whitcroft } 30639bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 30644d001e4dSAndy Whitcroft $check = 1; 30654d001e4dSAndy Whitcroft $cond_lines++; 30664d001e4dSAndy Whitcroft } 30674d001e4dSAndy Whitcroft 30684d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 30694d001e4dSAndy Whitcroft # preprocessor statement. 30704d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 30714d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 30724d001e4dSAndy Whitcroft $check = 0; 30734d001e4dSAndy Whitcroft } 30744d001e4dSAndy Whitcroft 30759bd49efeSAndy Whitcroft my $cond_ptr = -1; 3076740504c6SAndy Whitcroft $continuation = 0; 30779bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 30789bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 30794d001e4dSAndy Whitcroft 3080f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3081f16fa28fSAndy Whitcroft # is not linear. 3082f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3083f16fa28fSAndy Whitcroft $check = 0; 3084f16fa28fSAndy Whitcroft } 3085f16fa28fSAndy Whitcroft 30869bd49efeSAndy Whitcroft # Ignore: 30879bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 30889bd49efeSAndy Whitcroft # 2) preprocessor lines, and 30899bd49efeSAndy Whitcroft # 3) labels. 3090740504c6SAndy Whitcroft if ($continuation || 3091740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 30929bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 30939bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3094740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 309530dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 30969bd49efeSAndy Whitcroft $cond_lines++; 30979bd49efeSAndy Whitcroft } 30984d001e4dSAndy Whitcroft } 309930dad6ebSAndy Whitcroft } 31004d001e4dSAndy Whitcroft 31014d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 31024d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 31034d001e4dSAndy Whitcroft 31044d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 31054d001e4dSAndy Whitcroft # this is not this patch's fault. 31064d001e4dSAndy Whitcroft if (!defined($stat_real) || 31074d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 31084d001e4dSAndy Whitcroft $check = 0; 31094d001e4dSAndy Whitcroft } 31104d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 31114d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 31124d001e4dSAndy Whitcroft } 31134d001e4dSAndy Whitcroft 31149bd49efeSAndy 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"; 31154d001e4dSAndy Whitcroft 31164d001e4dSAndy Whitcroft if ($check && (($sindent % 8) != 0 || 31174d001e4dSAndy Whitcroft ($sindent <= $indent && $s ne ''))) { 3118000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3119000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 31204d001e4dSAndy Whitcroft } 31214d001e4dSAndy Whitcroft } 31224d001e4dSAndy Whitcroft 31236c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 31246c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 31251f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 31261f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 31276c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3128c2fdda0dSAndy Whitcroft if ($dbg_values) { 3129c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3130cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3131cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 31321f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3133c2fdda0dSAndy Whitcroft } 31346c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 31356c72ffaaSAndy Whitcroft 313600df344fSAndy Whitcroft#ignore lines not being added 31373705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 313800df344fSAndy Whitcroft 3139653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 31407429c690SAndy Whitcroft if ($dbg_type) { 31417429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3142000d1cc1SJoe Perches ERROR("TEST_TYPE", 3143000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 31447429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3145000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3146000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 31477429c690SAndy Whitcroft } 3148653d4876SAndy Whitcroft next; 3149653d4876SAndy Whitcroft } 3150a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3151a1ef277eSAndy Whitcroft if ($dbg_attr) { 31529360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3153000d1cc1SJoe Perches ERROR("TEST_ATTR", 3154000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 31559360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3156000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3157000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3158a1ef277eSAndy Whitcroft } 3159a1ef277eSAndy Whitcroft next; 3160a1ef277eSAndy Whitcroft } 3161653d4876SAndy Whitcroft 3162f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 316399423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 316499423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3165d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3166d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3167f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3168f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3169f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3170d752fcc8SJoe Perches my $fixedline = $prevrawline; 3171d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3172f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3173d752fcc8SJoe Perches $fixedline = $line; 3174d752fcc8SJoe Perches $fixedline =~ s/^(.\s*){\s*/$1/; 3175f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3176d752fcc8SJoe Perches } 3177f0a594c1SAndy Whitcroft } 3178f0a594c1SAndy Whitcroft 317900df344fSAndy Whitcroft# 318000df344fSAndy Whitcroft# Checks which are anchored on the added line. 318100df344fSAndy Whitcroft# 318200df344fSAndy Whitcroft 3183653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3184c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3185653d4876SAndy Whitcroft my $path = $1; 3186653d4876SAndy Whitcroft if ($path =~ m{//}) { 3187000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3188495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3189495e9d84SJoe Perches } 3190495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3191495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3192495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3193653d4876SAndy Whitcroft } 3194653d4876SAndy Whitcroft } 3195653d4876SAndy Whitcroft 319600df344fSAndy Whitcroft# no C99 // comments 319700df344fSAndy Whitcroft if ($line =~ m{//}) { 31983705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 31993705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 32003705ce5bSJoe Perches $fix) { 3201194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 32023705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 32033705ce5bSJoe Perches my $comment = trim($1); 3204194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 32053705ce5bSJoe Perches } 32063705ce5bSJoe Perches } 320700df344fSAndy Whitcroft } 320800df344fSAndy Whitcroft # Remove C99 comments. 32090a920b5bSAndy Whitcroft $line =~ s@//.*@@; 32106c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 32110a920b5bSAndy Whitcroft 32122b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 32132b474a1aSAndy Whitcroft# the whole statement. 32142b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 32152b474a1aSAndy Whitcroft if (defined $realline_next && 32162b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 32172b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 32182b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 32192b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 32203cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 32213cbf62dfSAndy Whitcroft # a prefix: 32223cbf62dfSAndy Whitcroft # XXX(foo); 32233cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3224653d4876SAndy Whitcroft my $name = $1; 322587a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 32263cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 32273cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 32283cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 32293cbf62dfSAndy Whitcroft 32303cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 32312b474a1aSAndy Whitcroft \n.}\s*$| 323248012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 323348012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 323448012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 32352b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 32362b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 323748012058SAndy Whitcroft )/x) { 32382b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 32392b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 32402b474a1aSAndy Whitcroft } else { 32412b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 32420a920b5bSAndy Whitcroft } 32430a920b5bSAndy Whitcroft } 32442b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 32452b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 32462b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 32472b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 32482b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 32492b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 32502b474a1aSAndy Whitcroft } 32512b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 32522b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3253000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3254000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 32552b474a1aSAndy Whitcroft } 32560a920b5bSAndy Whitcroft 32575150bda4SJoe Eloff# check for global initialisers. 32585129e87cSJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) { 3259d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 3260000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 3261d5e616fcSJoe Perches $herecurr) && 3262d5e616fcSJoe Perches $fix) { 32635129e87cSJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/; 3264d5e616fcSJoe Perches } 3265f0a594c1SAndy Whitcroft } 32660a920b5bSAndy Whitcroft# check for static initialisers. 3267d5e616fcSJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 3268d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 3269000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 3270d5e616fcSJoe Perches $herecurr) && 3271d5e616fcSJoe Perches $fix) { 3272194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; 3273d5e616fcSJoe Perches } 32740a920b5bSAndy Whitcroft } 32750a920b5bSAndy Whitcroft 32761813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 32771813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 32781813087dSJoe Perches my $tmp = trim($1); 32791813087dSJoe Perches WARN("MISORDERED_TYPE", 32801813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 32811813087dSJoe Perches } 32821813087dSJoe Perches 3283cb710ecaSJoe Perches# check for static const char * arrays. 3284cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3285000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3286000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3287cb710ecaSJoe Perches $herecurr); 3288cb710ecaSJoe Perches } 3289cb710ecaSJoe Perches 3290cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3291cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3292000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3293000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 3294cb710ecaSJoe Perches $herecurr); 3295cb710ecaSJoe Perches } 3296cb710ecaSJoe Perches 3297ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 3298ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 3299ab7e23f3SJoe Perches my $found = $1; 3300ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 3301ab7e23f3SJoe Perches WARN("CONST_CONST", 3302ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 3303ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 3304ab7e23f3SJoe Perches WARN("CONST_CONST", 3305ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 3306ab7e23f3SJoe Perches } 3307ab7e23f3SJoe Perches } 3308ab7e23f3SJoe Perches 33099b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 33109b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 33119b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 33129b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 33139b0fa60dSJoe Perches $herecurr); 33149b0fa60dSJoe Perches } 33159b0fa60dSJoe Perches 3316b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 3317b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 3318b598b670SJoe Perches my $array = $1; 3319b598b670SJoe Perches if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) { 3320b598b670SJoe Perches my $array_div = $1; 3321b598b670SJoe Perches if (WARN("ARRAY_SIZE", 3322b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 3323b598b670SJoe Perches $fix) { 3324b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 3325b598b670SJoe Perches } 3326b598b670SJoe Perches } 3327b598b670SJoe Perches } 3328b598b670SJoe Perches 3329b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 3330b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 3331b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 3332b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 3333b36190c5SJoe Perches $fix) { 3334194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 3335b36190c5SJoe Perches } 3336b36190c5SJoe Perches } 3337b36190c5SJoe Perches 333892e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE 333992e112fdSJoe Perches if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { 334092e112fdSJoe Perches if (WARN("DEFINE_PCI_DEVICE_TABLE", 334192e112fdSJoe Perches "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && 334292e112fdSJoe Perches $fix) { 3343194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /; 334492e112fdSJoe Perches } 334593ed0e2dSJoe Perches } 334693ed0e2dSJoe Perches 3347653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 3348653d4876SAndy Whitcroft# make sense. 3349653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 33508054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 3351c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 33528ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 3353653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 3354000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 3355000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 33560a920b5bSAndy Whitcroft } 33570a920b5bSAndy Whitcroft 33580a920b5bSAndy Whitcroft# * goes on variable not on type 335965863862SAndy Whitcroft # (char*[ const]) 3360bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 3361bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 33623705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 3363d8aaf121SAndy Whitcroft 336465863862SAndy Whitcroft # Should start with a space. 336565863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 336665863862SAndy Whitcroft # Should not end with a space. 336765863862SAndy Whitcroft $to =~ s/\s+$//; 336865863862SAndy Whitcroft # '*'s should not have spaces between. 3369f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 337065863862SAndy Whitcroft } 3371d8aaf121SAndy Whitcroft 33723705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 337365863862SAndy Whitcroft if ($from ne $to) { 33743705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 33753705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 33763705ce5bSJoe Perches $fix) { 33773705ce5bSJoe Perches my $sub_from = $ident; 33783705ce5bSJoe Perches my $sub_to = $ident; 33793705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3380194f66fcSJoe Perches $fixed[$fixlinenr] =~ 33813705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 33823705ce5bSJoe Perches } 338365863862SAndy Whitcroft } 3384bfcb2cc7SAndy Whitcroft } 3385bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 3386bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 33873705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 3388d8aaf121SAndy Whitcroft 338965863862SAndy Whitcroft # Should start with a space. 339065863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 339165863862SAndy Whitcroft # Should not end with a space. 339265863862SAndy Whitcroft $to =~ s/\s+$//; 339365863862SAndy Whitcroft # '*'s should not have spaces between. 3394f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 339565863862SAndy Whitcroft } 339665863862SAndy Whitcroft # Modifiers should have spaces. 339765863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 339865863862SAndy Whitcroft 33993705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 3400667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 34013705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 34023705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 34033705ce5bSJoe Perches $fix) { 34043705ce5bSJoe Perches 34053705ce5bSJoe Perches my $sub_from = $match; 34063705ce5bSJoe Perches my $sub_to = $match; 34073705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3408194f66fcSJoe Perches $fixed[$fixlinenr] =~ 34093705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 34103705ce5bSJoe Perches } 341165863862SAndy Whitcroft } 34120a920b5bSAndy Whitcroft } 34130a920b5bSAndy Whitcroft 34140a920b5bSAndy Whitcroft# # no BUG() or BUG_ON() 34150a920b5bSAndy Whitcroft# if ($line =~ /\b(BUG|BUG_ON)\b/) { 34160a920b5bSAndy Whitcroft# print "Try to use WARN_ON & Recovery code rather than BUG() or BUG_ON()\n"; 34170a920b5bSAndy Whitcroft# print "$herecurr"; 34180a920b5bSAndy Whitcroft# $clean = 0; 34190a920b5bSAndy Whitcroft# } 34200a920b5bSAndy Whitcroft 34218905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 3422000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 3423000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 34248905a67cSAndy Whitcroft } 34258905a67cSAndy Whitcroft 342617441227SJoe Perches# check for uses of printk_ratelimit 342717441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 3428000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 3429000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 343017441227SJoe Perches } 343117441227SJoe Perches 343200df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 343300df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 343400df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 343525985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 343600df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 3437f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 343800df344fSAndy Whitcroft my $ok = 0; 343900df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 344000df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 344125985edcSLucas De Marchi # we have a preceding printk if it ends 344200df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 344300df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 344400df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 344500df344fSAndy Whitcroft $ok = 1; 344600df344fSAndy Whitcroft } 344700df344fSAndy Whitcroft last; 344800df344fSAndy Whitcroft } 344900df344fSAndy Whitcroft } 345000df344fSAndy Whitcroft if ($ok == 0) { 3451000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 3452000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 34530a920b5bSAndy Whitcroft } 345400df344fSAndy Whitcroft } 34550a920b5bSAndy Whitcroft 3456243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 3457243f3803SJoe Perches my $orig = $1; 3458243f3803SJoe Perches my $level = lc($orig); 3459243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 34608f26b837SJoe Perches my $level2 = $level; 34618f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 3462243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 3463daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 3464243f3803SJoe Perches } 3465243f3803SJoe Perches 3466243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 3467d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 3468d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 3469d5e616fcSJoe Perches $fix) { 3470194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3471d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 3472d5e616fcSJoe Perches } 3473243f3803SJoe Perches } 3474243f3803SJoe Perches 3475dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 3476dc139313SJoe Perches my $orig = $1; 3477dc139313SJoe Perches my $level = lc($orig); 3478dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 3479dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 3480dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 3481dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 3482dc139313SJoe Perches } 3483dc139313SJoe Perches 348491c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 348591c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 348691c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 348791c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 348891c9afafSAndy Lutomirski WARN("ENOSYS", 348991c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 349091c9afafSAndy Lutomirski } 349191c9afafSAndy Lutomirski 3492653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 3493653d4876SAndy Whitcroft# or if closed on same line 34948d182478SJoe Perches if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and 3495c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 34968d182478SJoe Perches if (ERROR("OPEN_BRACE", 34978d182478SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr) && 34988d182478SJoe Perches $fix) { 34998d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 35008d182478SJoe Perches my $fixed_line = $rawline; 35018d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 35028d182478SJoe Perches my $line1 = $1; 35038d182478SJoe Perches my $line2 = $2; 35048d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 35058d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 35068d182478SJoe Perches if ($line2 !~ /^\s*$/) { 35078d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 35088d182478SJoe Perches } 35098d182478SJoe Perches } 35100a920b5bSAndy Whitcroft } 3511653d4876SAndy Whitcroft 35128905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 35138905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 35148905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 35158d182478SJoe Perches if (ERROR("OPEN_BRACE", 35168d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 35178d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 35188d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 35198d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 35208d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 35218d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 35228d182478SJoe Perches $fixedline = $rawline; 35238d182478SJoe Perches $fixedline =~ s/^(.\s*){\s*/$1\t/; 35248d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 35258d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 35268d182478SJoe Perches } 35278d182478SJoe Perches } 35288905a67cSAndy Whitcroft } 35298905a67cSAndy Whitcroft 35300c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 35313705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 35323705ce5bSJoe Perches if (WARN("SPACING", 35333705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 35343705ce5bSJoe Perches $fix) { 3535194f66fcSJoe Perches $fixed[$fixlinenr] =~ 35363705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 35373705ce5bSJoe Perches } 35380c73b4ebSAndy Whitcroft } 35390c73b4ebSAndy Whitcroft 354031070b5dSJoe Perches# Function pointer declarations 354131070b5dSJoe Perches# check spacing between type, funcptr, and args 354231070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 354391f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 354431070b5dSJoe Perches my $declare = $1; 354531070b5dSJoe Perches my $pre_pointer_space = $2; 354631070b5dSJoe Perches my $post_pointer_space = $3; 354731070b5dSJoe Perches my $funcname = $4; 354831070b5dSJoe Perches my $post_funcname_space = $5; 354931070b5dSJoe Perches my $pre_args_space = $6; 355031070b5dSJoe Perches 355191f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 355291f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 355391f72e9cSJoe Perches# don't need a space so don't warn for those. 355491f72e9cSJoe Perches my $post_declare_space = ""; 355591f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 355691f72e9cSJoe Perches $post_declare_space = $1; 355791f72e9cSJoe Perches $declare = rtrim($declare); 355891f72e9cSJoe Perches } 355991f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 356031070b5dSJoe Perches WARN("SPACING", 356131070b5dSJoe Perches "missing space after return type\n" . $herecurr); 356291f72e9cSJoe Perches $post_declare_space = " "; 356331070b5dSJoe Perches } 356431070b5dSJoe Perches 356531070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 356691f72e9cSJoe Perches# This test is not currently implemented because these declarations are 356791f72e9cSJoe Perches# equivalent to 356891f72e9cSJoe Perches# int foo(int bar, ...) 356991f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 357091f72e9cSJoe Perches# 357191f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 357291f72e9cSJoe Perches# WARN("SPACING", 357391f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 357491f72e9cSJoe Perches# } 357531070b5dSJoe Perches 357631070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 357731070b5dSJoe Perches if (defined $pre_pointer_space && 357831070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 357931070b5dSJoe Perches WARN("SPACING", 358031070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 358131070b5dSJoe Perches } 358231070b5dSJoe Perches 358331070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 358431070b5dSJoe Perches if (defined $post_pointer_space && 358531070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 358631070b5dSJoe Perches WARN("SPACING", 358731070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 358831070b5dSJoe Perches } 358931070b5dSJoe Perches 359031070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 359131070b5dSJoe Perches if (defined $post_funcname_space && 359231070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 359331070b5dSJoe Perches WARN("SPACING", 359431070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 359531070b5dSJoe Perches } 359631070b5dSJoe Perches 359731070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 359831070b5dSJoe Perches if (defined $pre_args_space && 359931070b5dSJoe Perches $pre_args_space =~ /^\s/) { 360031070b5dSJoe Perches WARN("SPACING", 360131070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 360231070b5dSJoe Perches } 360331070b5dSJoe Perches 360431070b5dSJoe Perches if (show_type("SPACING") && $fix) { 3605194f66fcSJoe Perches $fixed[$fixlinenr] =~ 360691f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 360731070b5dSJoe Perches } 360831070b5dSJoe Perches } 360931070b5dSJoe Perches 36108d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 36118d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 3612fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 3613fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 36148d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 36158d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 36168d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 3617fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 3618daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 36193705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 36203705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 36213705ce5bSJoe Perches $fix) { 3622194f66fcSJoe Perches $fixed[$fixlinenr] =~ 36233705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 36243705ce5bSJoe Perches } 36258d31cfceSAndy Whitcroft } 36268d31cfceSAndy Whitcroft } 36278d31cfceSAndy Whitcroft 3628f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 36296c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 3630c2fdda0dSAndy Whitcroft my $name = $1; 3631773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 3632773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 3633c2fdda0dSAndy Whitcroft 3634c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 3635773647a0SAndy Whitcroft if ($name =~ /^(?: 3636773647a0SAndy Whitcroft if|for|while|switch|return|case| 3637773647a0SAndy Whitcroft volatile|__volatile__| 3638773647a0SAndy Whitcroft __attribute__|format|__extension__| 3639773647a0SAndy Whitcroft asm|__asm__)$/x) 3640773647a0SAndy Whitcroft { 3641c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 3642c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 3643c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 3644c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 3645773647a0SAndy Whitcroft 3646773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 3647c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 3648c2fdda0dSAndy Whitcroft 3649c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 3650c2fdda0dSAndy Whitcroft # likely a typedef for a function. 3651773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 3652c2fdda0dSAndy Whitcroft 3653c2fdda0dSAndy Whitcroft } else { 36543705ce5bSJoe Perches if (WARN("SPACING", 36553705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 36563705ce5bSJoe Perches $fix) { 3657194f66fcSJoe Perches $fixed[$fixlinenr] =~ 36583705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 36593705ce5bSJoe Perches } 3660f0a594c1SAndy Whitcroft } 36616c72ffaaSAndy Whitcroft } 36629a4cad4eSEric Nelson 3663653d4876SAndy Whitcroft# Check operator spacing. 36640a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 36653705ce5bSJoe Perches my $fixed_line = ""; 36663705ce5bSJoe Perches my $line_fixed = 0; 36673705ce5bSJoe Perches 36689c0ca6f9SAndy Whitcroft my $ops = qr{ 36699c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 36709c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 36719c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 36721f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 367384731623SJoe Perches \?:|\?|: 36749c0ca6f9SAndy Whitcroft }x; 3675cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 36763705ce5bSJoe Perches 36773705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 36783705ce5bSJoe Perches## foreach my $el (@elements) { 36793705ce5bSJoe Perches## print("el: <$el>\n"); 36803705ce5bSJoe Perches## } 36813705ce5bSJoe Perches 36823705ce5bSJoe Perches my @fix_elements = (); 368300df344fSAndy Whitcroft my $off = 0; 36846c72ffaaSAndy Whitcroft 36853705ce5bSJoe Perches foreach my $el (@elements) { 36863705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 36873705ce5bSJoe Perches $off += length($el); 36883705ce5bSJoe Perches } 36893705ce5bSJoe Perches 36903705ce5bSJoe Perches $off = 0; 36913705ce5bSJoe Perches 36926c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 3693b34c648bSJoe Perches my $last_after = -1; 36946c72ffaaSAndy Whitcroft 36950a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 36963705ce5bSJoe Perches 36973705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 36983705ce5bSJoe Perches 36993705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 37003705ce5bSJoe Perches 37014a0df2efSAndy Whitcroft $off += length($elements[$n]); 37024a0df2efSAndy Whitcroft 370325985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 3704773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 3705773647a0SAndy Whitcroft my $cc = ''; 3706773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 3707773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 3708773647a0SAndy Whitcroft } 3709773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 3710773647a0SAndy Whitcroft 37114a0df2efSAndy Whitcroft my $a = ''; 37124a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 37134a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 3714cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 37154a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 37164a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 3717773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 37184a0df2efSAndy Whitcroft 37190a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 37204a0df2efSAndy Whitcroft 37214a0df2efSAndy Whitcroft my $c = ''; 37220a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 37234a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 37244a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 3725cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 37264a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 37274a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 37288b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 37294a0df2efSAndy Whitcroft } else { 37304a0df2efSAndy Whitcroft $c = 'E'; 37310a920b5bSAndy Whitcroft } 37320a920b5bSAndy Whitcroft 37334a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 37344a0df2efSAndy Whitcroft 37354a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 37364a0df2efSAndy Whitcroft 37376c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 3738de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 37390a920b5bSAndy Whitcroft 374074048ed8SAndy Whitcroft # Pull out the value of this operator. 37416c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 37420a920b5bSAndy Whitcroft 37431f65f947SAndy Whitcroft # Get the full operator variant. 37441f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 37451f65f947SAndy Whitcroft 374613214adfSAndy Whitcroft # Ignore operators passed as parameters. 374713214adfSAndy Whitcroft if ($op_type ne 'V' && 3748d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 374913214adfSAndy Whitcroft 3750cf655043SAndy Whitcroft# # Ignore comments 3751cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 375213214adfSAndy Whitcroft 3753d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 375413214adfSAndy Whitcroft } elsif ($op eq ';') { 3755cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 3756cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 37573705ce5bSJoe Perches if (ERROR("SPACING", 37583705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 3759b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 37603705ce5bSJoe Perches $line_fixed = 1; 37613705ce5bSJoe Perches } 3762d8aaf121SAndy Whitcroft } 3763d8aaf121SAndy Whitcroft 3764d8aaf121SAndy Whitcroft # // is a comment 3765d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 37660a920b5bSAndy Whitcroft 3767b00e4814SJoe Perches # : when part of a bitfield 3768b00e4814SJoe Perches } elsif ($opv eq ':B') { 3769b00e4814SJoe Perches # skip the bitfield test for now 3770b00e4814SJoe Perches 37711f65f947SAndy Whitcroft # No spaces for: 37721f65f947SAndy Whitcroft # -> 3773b00e4814SJoe Perches } elsif ($op eq '->') { 37744a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 37753705ce5bSJoe Perches if (ERROR("SPACING", 37763705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 3777b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 37783705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 37793705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 37803705ce5bSJoe Perches } 3781b34c648bSJoe Perches $line_fixed = 1; 37823705ce5bSJoe Perches } 37830a920b5bSAndy Whitcroft } 37840a920b5bSAndy Whitcroft 37852381097bSJoe Perches # , must not have a space before and must have a space on the right. 37860a920b5bSAndy Whitcroft } elsif ($op eq ',') { 37872381097bSJoe Perches my $rtrim_before = 0; 37882381097bSJoe Perches my $space_after = 0; 37892381097bSJoe Perches if ($ctx =~ /Wx./) { 37902381097bSJoe Perches if (ERROR("SPACING", 37912381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 37922381097bSJoe Perches $line_fixed = 1; 37932381097bSJoe Perches $rtrim_before = 1; 37942381097bSJoe Perches } 37952381097bSJoe Perches } 3796cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 37973705ce5bSJoe Perches if (ERROR("SPACING", 37983705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 37993705ce5bSJoe Perches $line_fixed = 1; 3800b34c648bSJoe Perches $last_after = $n; 38012381097bSJoe Perches $space_after = 1; 38022381097bSJoe Perches } 38032381097bSJoe Perches } 38042381097bSJoe Perches if ($rtrim_before || $space_after) { 38052381097bSJoe Perches if ($rtrim_before) { 38062381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 38072381097bSJoe Perches } else { 38082381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 38092381097bSJoe Perches } 38102381097bSJoe Perches if ($space_after) { 38112381097bSJoe Perches $good .= " "; 38123705ce5bSJoe Perches } 38130a920b5bSAndy Whitcroft } 38140a920b5bSAndy Whitcroft 38159c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 381674048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 38179c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 38189c0ca6f9SAndy Whitcroft 38199c0ca6f9SAndy Whitcroft # unary operators should have a space before and 38209c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 38219c0ca6f9SAndy Whitcroft # unary operator, or a cast 38229c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 382374048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 38240d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 3825cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 38263705ce5bSJoe Perches if (ERROR("SPACING", 38273705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 3828b34c648bSJoe Perches if ($n != $last_after + 2) { 3829b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 38303705ce5bSJoe Perches $line_fixed = 1; 38313705ce5bSJoe Perches } 38320a920b5bSAndy Whitcroft } 3833b34c648bSJoe Perches } 3834a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 3835171ae1a4SAndy Whitcroft # A unary '*' may be const 3836171ae1a4SAndy Whitcroft 3837171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 38383705ce5bSJoe Perches if (ERROR("SPACING", 38393705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3840b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 38413705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 38423705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 38433705ce5bSJoe Perches } 3844b34c648bSJoe Perches $line_fixed = 1; 38453705ce5bSJoe Perches } 38460a920b5bSAndy Whitcroft } 38470a920b5bSAndy Whitcroft 38480a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 38490a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 3850773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 38513705ce5bSJoe Perches if (ERROR("SPACING", 38523705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 3853b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 38543705ce5bSJoe Perches $line_fixed = 1; 38553705ce5bSJoe Perches } 38560a920b5bSAndy Whitcroft } 3857773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 3858773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 38593705ce5bSJoe Perches if (ERROR("SPACING", 38603705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3861b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 38623705ce5bSJoe Perches $line_fixed = 1; 38633705ce5bSJoe Perches } 3864653d4876SAndy Whitcroft } 3865773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 38663705ce5bSJoe Perches if (ERROR("SPACING", 38673705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3868b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 38693705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 38703705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3871773647a0SAndy Whitcroft } 3872b34c648bSJoe Perches $line_fixed = 1; 38733705ce5bSJoe Perches } 38743705ce5bSJoe Perches } 38750a920b5bSAndy Whitcroft 38760a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 38779c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 38789c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 38799c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 3880c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 3881c2fdda0dSAndy Whitcroft $op eq '%') 38820a920b5bSAndy Whitcroft { 3883d2e025f3SJoe Perches if ($check) { 3884d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 3885d2e025f3SJoe Perches if (CHK("SPACING", 3886d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 3887d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3888d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3889d2e025f3SJoe Perches $line_fixed = 1; 3890d2e025f3SJoe Perches } 3891d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 3892d2e025f3SJoe Perches if (CHK("SPACING", 3893d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 3894d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 3895d2e025f3SJoe Perches $line_fixed = 1; 3896d2e025f3SJoe Perches } 3897d2e025f3SJoe Perches } 3898d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 38993705ce5bSJoe Perches if (ERROR("SPACING", 39003705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 3901b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3902b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3903b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3904b34c648bSJoe Perches } 39053705ce5bSJoe Perches $line_fixed = 1; 39063705ce5bSJoe Perches } 39070a920b5bSAndy Whitcroft } 39080a920b5bSAndy Whitcroft 39091f65f947SAndy Whitcroft # A colon needs no spaces before when it is 39101f65f947SAndy Whitcroft # terminating a case value or a label. 39111f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 39121f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 39133705ce5bSJoe Perches if (ERROR("SPACING", 39143705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3915b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 39163705ce5bSJoe Perches $line_fixed = 1; 39173705ce5bSJoe Perches } 39181f65f947SAndy Whitcroft } 39191f65f947SAndy Whitcroft 39200a920b5bSAndy Whitcroft # All the others need spaces both sides. 3921cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 39221f65f947SAndy Whitcroft my $ok = 0; 39231f65f947SAndy Whitcroft 392422f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 39251f65f947SAndy Whitcroft if (($op eq '<' && 39261f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 39271f65f947SAndy Whitcroft ($op eq '>' && 39281f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 39291f65f947SAndy Whitcroft { 39301f65f947SAndy Whitcroft $ok = 1; 39311f65f947SAndy Whitcroft } 39321f65f947SAndy Whitcroft 3933e0df7e1fSJoe Perches # for asm volatile statements 3934e0df7e1fSJoe Perches # ignore a colon with another 3935e0df7e1fSJoe Perches # colon immediately before or after 3936e0df7e1fSJoe Perches if (($op eq ':') && 3937e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 3938e0df7e1fSJoe Perches $ok = 1; 3939e0df7e1fSJoe Perches } 3940e0df7e1fSJoe Perches 394184731623SJoe Perches # messages are ERROR, but ?: are CHK 39421f65f947SAndy Whitcroft if ($ok == 0) { 394384731623SJoe Perches my $msg_type = \&ERROR; 394484731623SJoe Perches $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 394584731623SJoe Perches 394684731623SJoe Perches if (&{$msg_type}("SPACING", 39473705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 3948b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3949b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3950b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3951b34c648bSJoe Perches } 39523705ce5bSJoe Perches $line_fixed = 1; 39533705ce5bSJoe Perches } 39540a920b5bSAndy Whitcroft } 395522f2a2efSAndy Whitcroft } 39564a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 39573705ce5bSJoe Perches 39583705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 39593705ce5bSJoe Perches 39603705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 39610a920b5bSAndy Whitcroft } 39623705ce5bSJoe Perches 39633705ce5bSJoe Perches if (($#elements % 2) == 0) { 39643705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 39653705ce5bSJoe Perches } 39663705ce5bSJoe Perches 3967194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 3968194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 39693705ce5bSJoe Perches } 39703705ce5bSJoe Perches 39713705ce5bSJoe Perches 39720a920b5bSAndy Whitcroft } 39730a920b5bSAndy Whitcroft 3974786b6326SJoe Perches# check for whitespace before a non-naked semicolon 3975d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 3976786b6326SJoe Perches if (WARN("SPACING", 3977786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 3978786b6326SJoe Perches $fix) { 3979194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 3980786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 3981786b6326SJoe Perches } 3982786b6326SJoe Perches } 3983786b6326SJoe Perches 3984f0a594c1SAndy Whitcroft# check for multiple assignments 3985f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 3986000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 3987000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 3988f0a594c1SAndy Whitcroft } 3989f0a594c1SAndy Whitcroft 399022f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 399122f2a2efSAndy Whitcroft## # continuation. 399222f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 399322f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 399422f2a2efSAndy Whitcroft## 399522f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 399622f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 399722f2a2efSAndy Whitcroft## my $ln = $line; 399822f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 399922f2a2efSAndy Whitcroft## } 400022f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4001000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4002000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 400322f2a2efSAndy Whitcroft## } 400422f2a2efSAndy Whitcroft## } 4005f0a594c1SAndy Whitcroft 40060a920b5bSAndy Whitcroft#need space before brace following if, while, etc 400722f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 400822f2a2efSAndy Whitcroft $line =~ /do{/) { 40093705ce5bSJoe Perches if (ERROR("SPACING", 40103705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 40113705ce5bSJoe Perches $fix) { 4012194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/; 40133705ce5bSJoe Perches } 4014de7d4f0eSAndy Whitcroft } 4015de7d4f0eSAndy Whitcroft 4016c4a62ef9SJoe Perches## # check for blank lines before declarations 4017c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4018c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4019c4a62ef9SJoe Perches## WARN("SPACING", 4020c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4021c4a62ef9SJoe Perches## } 4022c4a62ef9SJoe Perches## 4023c4a62ef9SJoe Perches 4024de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4025de7d4f0eSAndy Whitcroft# on the line 4026de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 4027d5e616fcSJoe Perches if (ERROR("SPACING", 4028d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4029d5e616fcSJoe Perches $fix) { 4030194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4031d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4032d5e616fcSJoe Perches } 40330a920b5bSAndy Whitcroft } 40340a920b5bSAndy Whitcroft 403522f2a2efSAndy Whitcroft# check spacing on square brackets 403622f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 40373705ce5bSJoe Perches if (ERROR("SPACING", 40383705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 40393705ce5bSJoe Perches $fix) { 4040194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40413705ce5bSJoe Perches s/\[\s+/\[/; 40423705ce5bSJoe Perches } 404322f2a2efSAndy Whitcroft } 404422f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 40453705ce5bSJoe Perches if (ERROR("SPACING", 40463705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 40473705ce5bSJoe Perches $fix) { 4048194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40493705ce5bSJoe Perches s/\s+\]/\]/; 40503705ce5bSJoe Perches } 405122f2a2efSAndy Whitcroft } 405222f2a2efSAndy Whitcroft 4053c45dcabdSAndy Whitcroft# check spacing on parentheses 40549c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 40559c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 40563705ce5bSJoe Perches if (ERROR("SPACING", 40573705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 40583705ce5bSJoe Perches $fix) { 4059194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40603705ce5bSJoe Perches s/\(\s+/\(/; 40613705ce5bSJoe Perches } 406222f2a2efSAndy Whitcroft } 406313214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4064c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4065c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 40663705ce5bSJoe Perches if (ERROR("SPACING", 40673705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 40683705ce5bSJoe Perches $fix) { 4069194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40703705ce5bSJoe Perches s/\s+\)/\)/; 40713705ce5bSJoe Perches } 407222f2a2efSAndy Whitcroft } 407322f2a2efSAndy Whitcroft 4074e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4075e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4076e2826fd0SJoe Perches 4077e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4078ea4acbb1SJoe Perches my $var = $1; 4079ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4080ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4081ea4acbb1SJoe Perches $fix) { 4082ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4083ea4acbb1SJoe Perches } 4084ea4acbb1SJoe Perches } 4085ea4acbb1SJoe Perches 4086ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4087ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4088ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4089ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4090ea4acbb1SJoe Perches my $var = $2; 4091ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4092ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4093ea4acbb1SJoe Perches $fix) { 4094ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4095ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4096ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4097ea4acbb1SJoe Perches } 4098e2826fd0SJoe Perches } 4099e2826fd0SJoe Perches 41000a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 41014a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 41020a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 41033705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 41043705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 41053705ce5bSJoe Perches $fix) { 4106194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41073705ce5bSJoe Perches s/^(.)\s+/$1/; 41083705ce5bSJoe Perches } 41090a920b5bSAndy Whitcroft } 41100a920b5bSAndy Whitcroft 41115b9553abSJoe Perches# return is not a function 4112507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4113c45dcabdSAndy Whitcroft my $spacing = $1; 4114507e5141SJoe Perches if ($^V && $^V ge 5.10.0 && 41155b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 41165b9553abSJoe Perches my $value = $1; 41175b9553abSJoe Perches $value = deparenthesize($value); 41185b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4119000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4120000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 41215b9553abSJoe Perches } 4122c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4123000d1cc1SJoe Perches ERROR("SPACING", 4124000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4125c45dcabdSAndy Whitcroft } 4126c45dcabdSAndy Whitcroft } 4127507e5141SJoe Perches 4128b43ae21bSJoe Perches# unnecessary return in a void function 4129b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4130b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4131b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4132b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4133b43ae21bSJoe Perches $linenr >= 3 && 4134b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4135b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 41369819cf25SJoe Perches WARN("RETURN_VOID", 4137b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 41389819cf25SJoe Perches } 41399819cf25SJoe Perches 4140189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 4141189248d8SJoe Perches if ($^V && $^V ge 5.10.0 && 4142189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4143189248d8SJoe Perches my $openparens = $1; 4144189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4145189248d8SJoe Perches my $msg = ""; 4146189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4147189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4148189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4149189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4150189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4151189248d8SJoe Perches } 4152189248d8SJoe Perches } 4153189248d8SJoe Perches 4154f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4155f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 415653a3c448SAndy Whitcroft my $name = $1; 415753a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4158000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4159f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 416053a3c448SAndy Whitcroft } 416153a3c448SAndy Whitcroft } 4162c45dcabdSAndy Whitcroft 41630a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 41644a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 41653705ce5bSJoe Perches if (ERROR("SPACING", 41663705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 41673705ce5bSJoe Perches $fix) { 4168194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41693705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 41703705ce5bSJoe Perches } 41710a920b5bSAndy Whitcroft } 41720a920b5bSAndy Whitcroft 4173f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4174f5fe35ddSAndy Whitcroft# statements after the conditional. 4175170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 41763e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 41773e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 41783e469cdcSAndy Whitcroft if (!defined $stat); 4179170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4180170d3a22SAndy Whitcroft $remain_next, $off_next); 4181170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4182170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4183170d3a22SAndy Whitcroft 4184170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4185170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4186170d3a22SAndy Whitcroft # then count those as offsets. 4187170d3a22SAndy Whitcroft my ($whitespace) = 4188170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4189170d3a22SAndy Whitcroft my $offset = 4190170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4191170d3a22SAndy Whitcroft 4192170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4193170d3a22SAndy Whitcroft $offset} = 1; 4194170d3a22SAndy Whitcroft } 4195170d3a22SAndy Whitcroft } 4196170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4197c11230f4SJoe Perches defined($stat) && defined($cond) && 4198170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4199171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 42008905a67cSAndy Whitcroft 4201b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4202000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4203000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 42048905a67cSAndy Whitcroft } 42058905a67cSAndy Whitcroft 42068905a67cSAndy Whitcroft # Find out what is on the end of the line after the 42078905a67cSAndy Whitcroft # conditional. 4208773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 42098905a67cSAndy Whitcroft $s =~ s/\n.*//g; 421013214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 421153210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 421253210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4213773647a0SAndy Whitcroft { 4214bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4215bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4216bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 421742bdf74cSHidetoshi Seto my $stat_real = ''; 4218bb44ad39SAndy Whitcroft 421942bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 422042bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4221bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4222bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4223bb44ad39SAndy Whitcroft } 4224bb44ad39SAndy Whitcroft 4225000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4226000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 42278905a67cSAndy Whitcroft } 42288905a67cSAndy Whitcroft } 42298905a67cSAndy Whitcroft 423013214adfSAndy Whitcroft# Check for bitwise tests written as boolean 423113214adfSAndy Whitcroft if ($line =~ / 423213214adfSAndy Whitcroft (?: 423313214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 423413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 423513214adfSAndy Whitcroft (?:\&\&|\|\|) 423613214adfSAndy Whitcroft | 423713214adfSAndy Whitcroft (?:\&\&|\|\|) 423813214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 423913214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 424013214adfSAndy Whitcroft )/x) 424113214adfSAndy Whitcroft { 4242000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4243000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 424413214adfSAndy Whitcroft } 424513214adfSAndy Whitcroft 42468905a67cSAndy Whitcroft# if and else should not have general statements after it 424713214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 424813214adfSAndy Whitcroft my $s = $1; 424913214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 425013214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4251000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4252000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 42530a920b5bSAndy Whitcroft } 425413214adfSAndy Whitcroft } 425539667782SAndy Whitcroft# if should not continue a brace 425639667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4257000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4258048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 425939667782SAndy Whitcroft $herecurr); 426039667782SAndy Whitcroft } 4261a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4262a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4263a1080bf8SAndy Whitcroft $line !~ /\G(?: 42643fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4265a1080bf8SAndy Whitcroft \s*return\s+ 4266a1080bf8SAndy Whitcroft )/xg) 4267a1080bf8SAndy Whitcroft { 4268000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4269000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4270a1080bf8SAndy Whitcroft } 42710a920b5bSAndy Whitcroft 42720a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 42730a920b5bSAndy Whitcroft # indent level to be relevant to each other. 42748b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 42750a920b5bSAndy Whitcroft $previndent == $indent) { 42768b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 42778b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 42788b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 42798b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 42808b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 42818b8856f4SJoe Perches my $fixedline = $prevrawline; 42828b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 42838b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 42848b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42858b8856f4SJoe Perches } 42868b8856f4SJoe Perches $fixedline = $rawline; 42878b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 42888b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 42898b8856f4SJoe Perches } 42900a920b5bSAndy Whitcroft } 42910a920b5bSAndy Whitcroft 42928b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 4293c2fdda0dSAndy Whitcroft $previndent == $indent) { 4294c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 4295c2fdda0dSAndy Whitcroft 4296c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 4297c2fdda0dSAndy Whitcroft # conditional. 4298773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 4299c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 4300c2fdda0dSAndy Whitcroft 4301c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 43028b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 43038b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 43048b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 43058b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 43068b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 43078b8856f4SJoe Perches my $fixedline = $prevrawline; 43088b8856f4SJoe Perches my $trailing = $rawline; 43098b8856f4SJoe Perches $trailing =~ s/^\+//; 43108b8856f4SJoe Perches $trailing = trim($trailing); 43118b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 43128b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 43138b8856f4SJoe Perches } 4314c2fdda0dSAndy Whitcroft } 4315c2fdda0dSAndy Whitcroft } 4316c2fdda0dSAndy Whitcroft 431795e2c602SJoe Perches#Specific variable tests 4318323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 4319323c1260SJoe Perches my $var = $1; 432095e2c602SJoe Perches 432195e2c602SJoe Perches#gcc binary extension 432295e2c602SJoe Perches if ($var =~ /^$Binary$/) { 4323d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 4324d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 4325d5e616fcSJoe Perches $fix) { 4326d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 4327194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4328d5e616fcSJoe Perches s/\b$var\b/$hexval/; 4329d5e616fcSJoe Perches } 433095e2c602SJoe Perches } 433195e2c602SJoe Perches 433295e2c602SJoe Perches#CamelCase 4333807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 4334be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 433522735ce8SJoe Perches#Ignore Page<foo> variants 4336807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 433722735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 4338f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && 4339f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 4340f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 43417e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 43427e781f67SJoe Perches my $word = $1; 43437e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 4344d8b07710SJoe Perches if ($check) { 4345d8b07710SJoe Perches seed_camelcase_includes(); 4346d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 4347d8b07710SJoe Perches seed_camelcase_file($realfile); 4348d8b07710SJoe Perches $camelcase_file_seeded = 1; 4349d8b07710SJoe Perches } 4350d8b07710SJoe Perches } 43517e781f67SJoe Perches if (!defined $camelcase{$word}) { 43527e781f67SJoe Perches $camelcase{$word} = 1; 4353be79794bSJoe Perches CHK("CAMELCASE", 43547e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 43557e781f67SJoe Perches } 4356323c1260SJoe Perches } 4357323c1260SJoe Perches } 43583445686aSJoe Perches } 43590a920b5bSAndy Whitcroft 43600a920b5bSAndy Whitcroft#no spaces allowed after \ in define 4361d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 4362d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 4363d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 4364d5e616fcSJoe Perches $fix) { 4365194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 4366d5e616fcSJoe Perches } 43670a920b5bSAndy Whitcroft } 43680a920b5bSAndy Whitcroft 43690e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 43700e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 4371c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 4372e09dec48SAndy Whitcroft my $file = "$1.h"; 4373e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 4374e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 4375e09dec48SAndy Whitcroft $realfile ne $checkfile && 43767840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 4377c45dcabdSAndy Whitcroft { 43780e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 43790e212e0aSFabian Frederick if ($asminclude > 0) { 4380e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 4381000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 4382000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4383e09dec48SAndy Whitcroft } else { 4384000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 4385000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4386e09dec48SAndy Whitcroft } 43870a920b5bSAndy Whitcroft } 43880a920b5bSAndy Whitcroft } 43890e212e0aSFabian Frederick } 43900a920b5bSAndy Whitcroft 4391653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 4392653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 4393cf655043SAndy Whitcroft# in a known good container 4394b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 4395b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 4396d8aaf121SAndy Whitcroft my $ln = $linenr; 4397d8aaf121SAndy Whitcroft my $cnt = $realcnt; 4398c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 4399c45dcabdSAndy Whitcroft my $ctx = ''; 440008a2843eSJoe Perches my $has_flow_statement = 0; 440108a2843eSJoe Perches my $has_arg_concat = 0; 4402c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 4403f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 4404f74bd194SAndy Whitcroft $ctx = $dstat; 4405c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4406a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4407c45dcabdSAndy Whitcroft 440808a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 440908a2843eSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/); 441008a2843eSJoe Perches 4411f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 4412292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 4413c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 4414c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 4415c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 4416c45dcabdSAndy Whitcroft 4417c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 4418bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 4419bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 4420c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 4421bf30d6edSAndy Whitcroft { 4422c45dcabdSAndy Whitcroft } 4423c45dcabdSAndy Whitcroft 4424e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 442533acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 442633acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 4427e45bab8eSAndy Whitcroft { 4428e45bab8eSAndy Whitcroft } 4429e45bab8eSAndy Whitcroft 4430c45dcabdSAndy Whitcroft my $exceptions = qr{ 4431c45dcabdSAndy Whitcroft $Declare| 4432c45dcabdSAndy Whitcroft module_param_named| 4433a0a0a7a9SKees Cook MODULE_PARM_DESC| 4434c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 4435c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 4436383099fdSAndy Whitcroft __typeof__\(| 443722fd2d3eSStefani Seibold union| 443822fd2d3eSStefani Seibold struct| 4439ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 4440ea71a0a0SAndy Whitcroft ^\"|\"$ 4441c45dcabdSAndy Whitcroft }x; 44425eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 4443f74bd194SAndy Whitcroft if ($dstat ne '' && 4444f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 4445f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 44463cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 4447356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 4448f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 4449f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 4450e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 445172f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 4452f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 4453f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 4454f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 4455f95a7e6aSJoe Perches $dstat !~ /^\({/ && # ({... 4456f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 4457c45dcabdSAndy Whitcroft { 4458f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 4459f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 4460f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 4461f74bd194SAndy Whitcroft 4462f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 4463f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 4464c45dcabdSAndy Whitcroft } 4465c45dcabdSAndy Whitcroft 4466f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 4467f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 4468f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 4469f74bd194SAndy Whitcroft } else { 4470000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 4471388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 4472d8aaf121SAndy Whitcroft } 44730a920b5bSAndy Whitcroft } 44745023d347SJoe Perches 447508a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 447608a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 447708a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 447808a2843eSJoe Perches my $herectx = $here . "\n"; 447908a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 448008a2843eSJoe Perches 448108a2843eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 448208a2843eSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 448308a2843eSJoe Perches } 448408a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 448508a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 448608a2843eSJoe Perches } 448708a2843eSJoe Perches 4488481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 44895023d347SJoe Perches 44905023d347SJoe Perches } else { 44915023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 4492481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 4493481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 44945023d347SJoe Perches $line =~ /^\+.*\\$/) { 44955023d347SJoe Perches WARN("LINE_CONTINUATIONS", 44965023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 44975023d347SJoe Perches } 4498653d4876SAndy Whitcroft } 44990a920b5bSAndy Whitcroft 4500b13edf7fSJoe Perches# do {} while (0) macro tests: 4501b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 4502b13edf7fSJoe Perches# macro should not end with a semicolon 4503b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 4504b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 4505b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 4506b13edf7fSJoe Perches my $ln = $linenr; 4507b13edf7fSJoe Perches my $cnt = $realcnt; 4508b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 4509b13edf7fSJoe Perches my $ctx = ''; 4510b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 4511b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 4512b13edf7fSJoe Perches $ctx = $dstat; 4513b13edf7fSJoe Perches 4514b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 45151b36b201SJoe Perches $dstat =~ s/$;/ /g; 4516b13edf7fSJoe Perches 4517b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 4518b13edf7fSJoe Perches my $stmts = $2; 4519b13edf7fSJoe Perches my $semis = $3; 4520b13edf7fSJoe Perches 4521b13edf7fSJoe Perches $ctx =~ s/\n*$//; 4522b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 4523b13edf7fSJoe Perches my $herectx = $here . "\n"; 4524b13edf7fSJoe Perches 4525b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4526b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4527b13edf7fSJoe Perches } 4528b13edf7fSJoe Perches 4529ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 4530ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 4531b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 4532b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 4533b13edf7fSJoe Perches } 4534b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 4535b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 4536b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 4537b13edf7fSJoe Perches } 4538f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 4539f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 4540f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 4541f5ef95b1SJoe Perches my $herectx = $here . "\n"; 4542f5ef95b1SJoe Perches 4543f5ef95b1SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4544f5ef95b1SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4545f5ef95b1SJoe Perches } 4546f5ef95b1SJoe Perches 4547f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 4548f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 4549b13edf7fSJoe Perches } 4550b13edf7fSJoe Perches } 4551b13edf7fSJoe Perches 4552080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 4553080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 4554080ba929SMike Frysinger# . 4555080ba929SMike Frysinger# ALIGN(...) 4556080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 4557080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 4558000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 4559000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 4560080ba929SMike Frysinger } 4561080ba929SMike Frysinger 4562f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 456313214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 456413214adfSAndy Whitcroft my ($level, $endln, @chunks) = 4565cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 456613214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 4567cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 4568cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 4569aad4f614SJoe Perches my @allowed = (); 4570aad4f614SJoe Perches my $allow = 0; 457113214adfSAndy Whitcroft my $seen = 0; 4572773647a0SAndy Whitcroft my $herectx = $here . "\n"; 4573cf655043SAndy Whitcroft my $ln = $linenr - 1; 457413214adfSAndy Whitcroft for my $chunk (@chunks) { 457513214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 457613214adfSAndy Whitcroft 4577773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 4578773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 4579773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 4580773647a0SAndy Whitcroft 4581aad4f614SJoe Perches $allowed[$allow] = 0; 4582773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 4583773647a0SAndy Whitcroft 4584773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 4585773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 4586773647a0SAndy Whitcroft 4587773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 4588cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 4589cf655043SAndy Whitcroft 4590773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 459113214adfSAndy Whitcroft 459213214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 459313214adfSAndy Whitcroft 4594aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 4595cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 4596cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 4597aad4f614SJoe Perches $allowed[$allow] = 1; 459813214adfSAndy Whitcroft } 459913214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 4600cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 4601aad4f614SJoe Perches $allowed[$allow] = 1; 460213214adfSAndy Whitcroft } 4603cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 4604cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 4605aad4f614SJoe Perches $allowed[$allow] = 1; 460613214adfSAndy Whitcroft } 4607aad4f614SJoe Perches $allow++; 460813214adfSAndy Whitcroft } 4609aad4f614SJoe Perches if ($seen) { 4610aad4f614SJoe Perches my $sum_allowed = 0; 4611aad4f614SJoe Perches foreach (@allowed) { 4612aad4f614SJoe Perches $sum_allowed += $_; 4613aad4f614SJoe Perches } 4614aad4f614SJoe Perches if ($sum_allowed == 0) { 4615000d1cc1SJoe Perches WARN("BRACES", 4616000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 4617aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 4618aad4f614SJoe Perches $seen != $allow) { 4619aad4f614SJoe Perches CHK("BRACES", 4620aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 4621aad4f614SJoe Perches } 462213214adfSAndy Whitcroft } 462313214adfSAndy Whitcroft } 462413214adfSAndy Whitcroft } 4625773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 462613214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 4627cf655043SAndy Whitcroft my $allowed = 0; 4628f0a594c1SAndy Whitcroft 4629cf655043SAndy Whitcroft # Check the pre-context. 4630cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 4631cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 4632cf655043SAndy Whitcroft $allowed = 1; 4633f0a594c1SAndy Whitcroft } 4634773647a0SAndy Whitcroft 4635773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 4636773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 4637773647a0SAndy Whitcroft 4638cf655043SAndy Whitcroft # Check the condition. 4639cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 4640773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 4641cf655043SAndy Whitcroft if (defined $cond) { 4642773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 4643cf655043SAndy Whitcroft } 4644cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 4645cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 4646cf655043SAndy Whitcroft $allowed = 1; 4647cf655043SAndy Whitcroft } 4648cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 4649cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 4650cf655043SAndy Whitcroft $allowed = 1; 4651cf655043SAndy Whitcroft } 4652cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 4653cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 4654cf655043SAndy Whitcroft $allowed = 1; 4655cf655043SAndy Whitcroft } 4656cf655043SAndy Whitcroft # Check the post-context. 4657cf655043SAndy Whitcroft if (defined $chunks[1]) { 4658cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 4659cf655043SAndy Whitcroft if (defined $cond) { 4660773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 4661cf655043SAndy Whitcroft } 4662cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 4663cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 4664cf655043SAndy Whitcroft $allowed = 1; 4665cf655043SAndy Whitcroft } 4666cf655043SAndy Whitcroft } 4667cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 466869932487SJustin P. Mattock my $herectx = $here . "\n"; 4669f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 4670cf655043SAndy Whitcroft 4671f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 467269932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 4673cf655043SAndy Whitcroft } 4674cf655043SAndy Whitcroft 4675000d1cc1SJoe Perches WARN("BRACES", 4676000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 4677f0a594c1SAndy Whitcroft } 4678f0a594c1SAndy Whitcroft } 4679f0a594c1SAndy Whitcroft 46800979ae66SJoe Perches# check for unnecessary blank lines around braces 468177b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 4682f8e58219SJoe Perches if (CHK("BRACES", 4683f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 4684f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 4685f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4686f8e58219SJoe Perches } 46870979ae66SJoe Perches } 468877b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 4689f8e58219SJoe Perches if (CHK("BRACES", 4690f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 4691f8e58219SJoe Perches $fix) { 4692f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 4693f8e58219SJoe Perches } 46940979ae66SJoe Perches } 46950979ae66SJoe Perches 46964a0df2efSAndy Whitcroft# no volatiles please 46976c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 46986c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 4699000d1cc1SJoe Perches WARN("VOLATILE", 4700000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 47014a0df2efSAndy Whitcroft } 47024a0df2efSAndy Whitcroft 47035e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 47045e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 47055e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 47065e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 470733acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 47085e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 47095e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 47105e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 47115e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 47125e4f6ba5SJoe Perches $fix && 47135e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 47145e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 47155e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 47165e4f6ba5SJoe Perches my $comma_close = ""; 47175e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 47185e4f6ba5SJoe Perches $comma_close = $1; 47195e4f6ba5SJoe Perches } 47205e4f6ba5SJoe Perches 47215e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 47225e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 47235e4f6ba5SJoe Perches my $fixedline = $prevrawline; 47245e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 47255e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 47265e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 47275e4f6ba5SJoe Perches $fixedline = $rawline; 47285e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 47295e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 47305e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 47315e4f6ba5SJoe Perches } 47325e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 47335e4f6ba5SJoe Perches } 47345e4f6ba5SJoe Perches } 47355e4f6ba5SJoe Perches 47365e4f6ba5SJoe Perches# check for missing a space in a string concatenation 47375e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 47385e4f6ba5SJoe Perches WARN('MISSING_SPACE', 47395e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 47405e4f6ba5SJoe Perches } 47415e4f6ba5SJoe Perches 47425e4f6ba5SJoe Perches# check for spaces before a quoted newline 47435e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 47445e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 47455e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 47465e4f6ba5SJoe Perches $fix) { 47475e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 47485e4f6ba5SJoe Perches } 47495e4f6ba5SJoe Perches 47505e4f6ba5SJoe Perches } 47515e4f6ba5SJoe Perches 4752f17dba4fSJoe Perches# concatenated string without spaces between elements 475333acb54aSJoe Perches if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) { 4754f17dba4fSJoe Perches CHK("CONCATENATED_STRING", 4755f17dba4fSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr); 4756f17dba4fSJoe Perches } 4757f17dba4fSJoe Perches 475890ad30e5SJoe Perches# uncoalesced string fragments 475933acb54aSJoe Perches if ($line =~ /$String\s*"/) { 476090ad30e5SJoe Perches WARN("STRING_FRAGMENTS", 476190ad30e5SJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr); 476290ad30e5SJoe Perches } 476390ad30e5SJoe Perches 47645e4f6ba5SJoe Perches# check for %L{u,d,i} in strings 47655e4f6ba5SJoe Perches my $string; 47665e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 47675e4f6ba5SJoe Perches $string = substr($rawline, $-[1], $+[1] - $-[1]); 47685e4f6ba5SJoe Perches $string =~ s/%%/__/g; 47695e4f6ba5SJoe Perches if ($string =~ /(?<!%)%L[udi]/) { 47705e4f6ba5SJoe Perches WARN("PRINTF_L", 47715e4f6ba5SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 47725e4f6ba5SJoe Perches last; 47735e4f6ba5SJoe Perches } 47745e4f6ba5SJoe Perches } 47755e4f6ba5SJoe Perches 47765e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 47775e4f6ba5SJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 47785e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 47795e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 47805e4f6ba5SJoe Perches } 47815e4f6ba5SJoe Perches 478200df344fSAndy Whitcroft# warn about #if 0 4783c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 4784000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 4785000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 4786de7d4f0eSAndy Whitcroft $herecurr); 47874a0df2efSAndy Whitcroft } 47884a0df2efSAndy Whitcroft 478903df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 479003df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 479103df4b51SAndy Whitcroft my $expr = '\s*\(\s*' . quotemeta($1) . '\s*\)\s*;'; 479203df4b51SAndy Whitcroft if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?)$expr/) { 479303df4b51SAndy Whitcroft WARN('NEEDLESS_IF', 479415160f90SFabio Estevam "$1(NULL) is safe and this check is probably not required\n" . $hereprev); 47954c432a8fSGreg Kroah-Hartman } 47964c432a8fSGreg Kroah-Hartman } 4797f0a594c1SAndy Whitcroft 4798ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 4799ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 4800ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 4801ebfdc409SJoe Perches (defined $1 || defined $3) && 4802ebfdc409SJoe Perches $linenr > 3) { 4803ebfdc409SJoe Perches my $testval = $2; 4804ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 4805ebfdc409SJoe Perches 4806ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 4807ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 4808ebfdc409SJoe Perches 4809ebfdc409SJoe 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)/) { 4810ebfdc409SJoe Perches WARN("OOM_MESSAGE", 4811ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 4812ebfdc409SJoe Perches } 4813ebfdc409SJoe Perches } 4814ebfdc409SJoe Perches 4815f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 4816dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 4817f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 4818f78d98f6SJoe Perches my $level = $1; 4819f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 4820f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 4821f78d98f6SJoe Perches $fix) { 4822f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 4823f78d98f6SJoe Perches } 4824f78d98f6SJoe Perches } 4825f78d98f6SJoe Perches 4826abb08a53SJoe Perches# check for mask then right shift without a parentheses 4827abb08a53SJoe Perches if ($^V && $^V ge 5.10.0 && 4828abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 4829abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 4830abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 4831abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 4832abb08a53SJoe Perches } 4833abb08a53SJoe Perches 4834b75ac618SJoe Perches# check for pointer comparisons to NULL 4835b75ac618SJoe Perches if ($^V && $^V ge 5.10.0) { 4836b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 4837b75ac618SJoe Perches my $val = $1; 4838b75ac618SJoe Perches my $equal = "!"; 4839b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 4840b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 4841b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 4842b75ac618SJoe Perches $fix) { 4843b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 4844b75ac618SJoe Perches } 4845b75ac618SJoe Perches } 4846b75ac618SJoe Perches } 4847b75ac618SJoe Perches 48488716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 48498716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 48508716de38SJoe Perches my $attr = $1; 48518716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 48528716de38SJoe Perches my $ptr = $1; 48538716de38SJoe Perches my $var = $2; 48548716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 48558716de38SJoe Perches ERROR("MISPLACED_INIT", 48568716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 48578716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 48588716de38SJoe Perches WARN("MISPLACED_INIT", 48598716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 48608716de38SJoe Perches $fix) { 4861194f66fcSJoe 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; 48628716de38SJoe Perches } 48638716de38SJoe Perches } 48648716de38SJoe Perches } 48658716de38SJoe Perches 4866e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 4867e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 4868e970b884SJoe Perches my $attr = $1; 4869e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 4870e970b884SJoe Perches my $attr_prefix = $1; 4871e970b884SJoe Perches my $attr_type = $2; 4872e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 4873e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 4874e970b884SJoe Perches $fix) { 4875194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4876e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 4877e970b884SJoe Perches } 4878e970b884SJoe Perches } 4879e970b884SJoe Perches 4880e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 4881e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 4882e970b884SJoe Perches my $attr = $1; 4883e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 4884e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 4885e970b884SJoe Perches $fix) { 4886194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 4887e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 4888e970b884SJoe Perches $lead = rtrim($1); 4889e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 4890e970b884SJoe Perches $lead = "${lead}const "; 4891194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 4892e970b884SJoe Perches } 4893e970b884SJoe Perches } 4894e970b884SJoe Perches 4895c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 4896c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 4897c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 4898c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 4899c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 4900c17893c7SJoe Perches $fix) { 4901c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 4902c17893c7SJoe Perches } 4903c17893c7SJoe Perches } 4904c17893c7SJoe Perches 4905fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 4906fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 4907fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 4908fbdb8138SJoe Perches my $constant_func = $1; 4909fbdb8138SJoe Perches my $func = $constant_func; 4910fbdb8138SJoe Perches $func =~ s/^__constant_//; 4911fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 4912fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 4913fbdb8138SJoe Perches $fix) { 4914194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 4915fbdb8138SJoe Perches } 4916fbdb8138SJoe Perches } 4917fbdb8138SJoe Perches 49181a15a250SPatrick Pannuto# prefer usleep_range over udelay 491937581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 492043c1d77cSJoe Perches my $delay = $1; 49211a15a250SPatrick Pannuto # ignore udelay's < 10, however 492243c1d77cSJoe Perches if (! ($delay < 10) ) { 4923000d1cc1SJoe Perches CHK("USLEEP_RANGE", 492443c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 492543c1d77cSJoe Perches } 492643c1d77cSJoe Perches if ($delay > 2000) { 492743c1d77cSJoe Perches WARN("LONG_UDELAY", 492843c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 49291a15a250SPatrick Pannuto } 49301a15a250SPatrick Pannuto } 49311a15a250SPatrick Pannuto 493209ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 493309ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 493409ef8725SPatrick Pannuto if ($1 < 20) { 4935000d1cc1SJoe Perches WARN("MSLEEP", 493643c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 493709ef8725SPatrick Pannuto } 493809ef8725SPatrick Pannuto } 493909ef8725SPatrick Pannuto 494036ec1939SJoe Perches# check for comparisons of jiffies 494136ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 494236ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 494336ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 494436ec1939SJoe Perches } 494536ec1939SJoe Perches 49469d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 49479d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 49489d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 49499d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 49509d7a34a5SJoe Perches } 49519d7a34a5SJoe Perches 495200df344fSAndy Whitcroft# warn about #ifdefs in C files 4953c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 495400df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 495500df344fSAndy Whitcroft# print "$herecurr"; 495600df344fSAndy Whitcroft# $clean = 0; 495700df344fSAndy Whitcroft# } 495800df344fSAndy Whitcroft 495922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 4960c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 49613705ce5bSJoe Perches if (ERROR("SPACING", 49623705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 49633705ce5bSJoe Perches $fix) { 4964194f66fcSJoe Perches $fixed[$fixlinenr] =~ 49653705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 49663705ce5bSJoe Perches } 49673705ce5bSJoe Perches 496822f2a2efSAndy Whitcroft } 496922f2a2efSAndy Whitcroft 49704a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 4971171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 4972171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 49734a0df2efSAndy Whitcroft my $which = $1; 49744a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 4975000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 4976000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 49774a0df2efSAndy Whitcroft } 49784a0df2efSAndy Whitcroft } 49794a0df2efSAndy Whitcroft# check for memory barriers without a comment. 49804a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 49814a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 4982c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 4983000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 49844a0df2efSAndy Whitcroft } 49854a0df2efSAndy Whitcroft } 4986cb426e99SJoe Perches# check for waitqueue_active without a comment. 4987cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 4988cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 4989cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 4990cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 4991cb426e99SJoe Perches } 4992cb426e99SJoe Perches } 49934a0df2efSAndy Whitcroft# check of hardware specific defines 4994c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 4995000d1cc1SJoe Perches CHK("ARCH_DEFINES", 4996000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 49970a920b5bSAndy Whitcroft } 4998653d4876SAndy Whitcroft 4999d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 5000d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 5001000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5002000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 5003d4977c78STobias Klauser } 5004d4977c78STobias Klauser 5005de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5006de7d4f0eSAndy Whitcroft# storage class and type. 50079c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 50089c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5009000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5010000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5011de7d4f0eSAndy Whitcroft } 5012de7d4f0eSAndy Whitcroft 50138905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 50142b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 50152b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5016d5e616fcSJoe Perches if (WARN("INLINE", 5017d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5018d5e616fcSJoe Perches $fix) { 5019194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5020d5e616fcSJoe Perches 5021d5e616fcSJoe Perches } 50228905a67cSAndy Whitcroft } 50238905a67cSAndy Whitcroft 50243d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 50252b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 50262b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5027000d1cc1SJoe Perches WARN("PREFER_PACKED", 5028000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 50293d130fd0SJoe Perches } 50303d130fd0SJoe Perches 503139b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 50322b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 50332b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5034000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5035000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 503639b7e287SJoe Perches } 503739b7e287SJoe Perches 50385f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 50392b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 50402b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5041d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5042d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5043d5e616fcSJoe Perches $fix) { 5044194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5045d5e616fcSJoe Perches 5046d5e616fcSJoe Perches } 50475f14d3bdSJoe Perches } 50485f14d3bdSJoe Perches 50496061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 50502b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 50512b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5052d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5053d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5054d5e616fcSJoe Perches $fix) { 5055194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5056d5e616fcSJoe Perches } 50576061d949SJoe Perches } 50586061d949SJoe Perches 5059619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 5060619a908aSJoe Perches if ($^V && $^V ge 5.10.0 && 5061619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5062619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5063619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5064619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5065619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5066619a908aSJoe Perches } 5067619a908aSJoe Perches 5068e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/ 5069e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5070e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5071e6176fa4SJoe Perches my $type = $1; 5072e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5073e6176fa4SJoe Perches $type = $1; 5074e6176fa4SJoe Perches my $kernel_type = 'u'; 5075e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5076e6176fa4SJoe Perches $type =~ /(\d+)/; 5077e6176fa4SJoe Perches $kernel_type .= $1; 5078e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5079e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5080e6176fa4SJoe Perches $fix) { 5081e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5082e6176fa4SJoe Perches } 5083e6176fa4SJoe Perches } 5084e6176fa4SJoe Perches } 5085e6176fa4SJoe Perches 50868f53a9b8SJoe Perches# check for sizeof(&) 50878f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 5088000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 5089000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 50908f53a9b8SJoe Perches } 50918f53a9b8SJoe Perches 509266c80b60SJoe Perches# check for sizeof without parenthesis 509366c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 5094d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 5095d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 5096d5e616fcSJoe Perches $fix) { 5097194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 5098d5e616fcSJoe Perches } 509966c80b60SJoe Perches } 510066c80b60SJoe Perches 510188982feaSJoe Perches# check for struct spinlock declarations 510288982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 510388982feaSJoe Perches WARN("USE_SPINLOCK_T", 510488982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 510588982feaSJoe Perches } 510688982feaSJoe Perches 5107a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 510806668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 5109a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 5110caac1d5fSHeba Aamer $fmt =~ s/%%//g; 5111caac1d5fSHeba Aamer if ($fmt !~ /%/) { 5112d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 5113d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 5114d5e616fcSJoe Perches $fix) { 5115194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 5116d5e616fcSJoe Perches } 5117a6962d72SJoe Perches } 5118a6962d72SJoe Perches } 5119a6962d72SJoe Perches 5120554e165cSAndy Whitcroft# Check for misused memsets 5121d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 5122d1fe9c09SJoe Perches defined $stat && 5123d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/s) { 5124554e165cSAndy Whitcroft 5125d7c76ba7SJoe Perches my $ms_addr = $2; 5126d1fe9c09SJoe Perches my $ms_val = $7; 5127d1fe9c09SJoe Perches my $ms_size = $12; 5128d7c76ba7SJoe Perches 5129554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 5130554e165cSAndy Whitcroft ERROR("MEMSET", 5131d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 5132554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 5133554e165cSAndy Whitcroft WARN("MEMSET", 5134d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 5135d7c76ba7SJoe Perches } 5136d7c76ba7SJoe Perches } 5137d7c76ba7SJoe Perches 513898a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 513998a9bba5SJoe Perches if ($^V && $^V ge 5.10.0 && 514098a9bba5SJoe Perches $line =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/s) { 514198a9bba5SJoe Perches if (WARN("PREFER_ETHER_ADDR_COPY", 514298a9bba5SJoe Perches "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . $herecurr) && 514398a9bba5SJoe Perches $fix) { 5144194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 514598a9bba5SJoe Perches } 514698a9bba5SJoe Perches } 514798a9bba5SJoe Perches 5148d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 5149d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 5150d1fe9c09SJoe Perches defined $stat && 5151d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 5152d1fe9c09SJoe Perches if (defined $2 || defined $7) { 5153d7c76ba7SJoe Perches my $call = $1; 5154d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 5155d7c76ba7SJoe Perches my $arg1 = $3; 5156d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 5157d1fe9c09SJoe Perches my $arg2 = $8; 5158d7c76ba7SJoe Perches my $cast; 5159d7c76ba7SJoe Perches 5160d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 5161d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 5162d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 5163d7c76ba7SJoe Perches $cast = $cast1; 5164d7c76ba7SJoe Perches } else { 5165d7c76ba7SJoe Perches $cast = $cast2; 5166d7c76ba7SJoe Perches } 5167d7c76ba7SJoe Perches WARN("MINMAX", 5168d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 5169554e165cSAndy Whitcroft } 5170554e165cSAndy Whitcroft } 5171554e165cSAndy Whitcroft 51724a273195SJoe Perches# check usleep_range arguments 51734a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 51744a273195SJoe Perches defined $stat && 51754a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 51764a273195SJoe Perches my $min = $1; 51774a273195SJoe Perches my $max = $7; 51784a273195SJoe Perches if ($min eq $max) { 51794a273195SJoe Perches WARN("USLEEP_RANGE", 51804a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 51814a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 51824a273195SJoe Perches $min > $max) { 51834a273195SJoe Perches WARN("USLEEP_RANGE", 51844a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 51854a273195SJoe Perches } 51864a273195SJoe Perches } 51874a273195SJoe Perches 5188823b794cSJoe Perches# check for naked sscanf 5189823b794cSJoe Perches if ($^V && $^V ge 5.10.0 && 5190823b794cSJoe Perches defined $stat && 51916c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 5192823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 5193823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 5194823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 5195823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 5196823b794cSJoe Perches $lc = $lc + $linenr; 5197823b794cSJoe Perches my $stat_real = raw_line($linenr, 0); 5198823b794cSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 5199823b794cSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 5200823b794cSJoe Perches } 5201823b794cSJoe Perches WARN("NAKED_SSCANF", 5202823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 5203823b794cSJoe Perches } 5204823b794cSJoe Perches 5205afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 5206afc819abSJoe Perches if ($^V && $^V ge 5.10.0 && 5207afc819abSJoe Perches defined $stat && 5208afc819abSJoe Perches $line =~ /\bsscanf\b/) { 5209afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 5210afc819abSJoe Perches $lc = $lc + $linenr; 5211afc819abSJoe Perches my $stat_real = raw_line($linenr, 0); 5212afc819abSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 5213afc819abSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 5214afc819abSJoe Perches } 5215afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 5216afc819abSJoe Perches my $format = $6; 5217afc819abSJoe Perches my $count = $format =~ tr@%@%@; 5218afc819abSJoe Perches if ($count == 1 && 5219afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 5220afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 5221afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 5222afc819abSJoe Perches } 5223afc819abSJoe Perches } 5224afc819abSJoe Perches } 5225afc819abSJoe Perches 522670dc8a48SJoe Perches# check for new externs in .h files. 522770dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 522870dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 5229d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 523070dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 523170dc8a48SJoe Perches $fix) { 5232194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 523370dc8a48SJoe Perches } 523470dc8a48SJoe Perches } 523570dc8a48SJoe Perches 5236de7d4f0eSAndy Whitcroft# check for new externs in .c files. 5237171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 5238c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 5239171ae1a4SAndy Whitcroft { 5240c45dcabdSAndy Whitcroft my $function_name = $1; 5241c45dcabdSAndy Whitcroft my $paren_space = $2; 5242171ae1a4SAndy Whitcroft 5243171ae1a4SAndy Whitcroft my $s = $stat; 5244171ae1a4SAndy Whitcroft if (defined $cond) { 5245171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 5246171ae1a4SAndy Whitcroft } 5247c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 5248c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 5249c45dcabdSAndy Whitcroft { 5250000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 5251000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 5252de7d4f0eSAndy Whitcroft } 5253de7d4f0eSAndy Whitcroft 5254171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 5255000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 5256000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 5257171ae1a4SAndy Whitcroft } 52589c9ba34eSAndy Whitcroft 52599c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 52609c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 52619c9ba34eSAndy Whitcroft { 5262000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 5263000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 5264171ae1a4SAndy Whitcroft } 5265171ae1a4SAndy Whitcroft 5266de7d4f0eSAndy Whitcroft# checks for new __setup's 5267de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 5268de7d4f0eSAndy Whitcroft my $name = $1; 5269de7d4f0eSAndy Whitcroft 5270de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 5271000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 5272000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 5273de7d4f0eSAndy Whitcroft } 5274653d4876SAndy Whitcroft } 52759c0ca6f9SAndy Whitcroft 52769c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 5277caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 5278000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 5279000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 52809c0ca6f9SAndy Whitcroft } 528113214adfSAndy Whitcroft 5282a640d25cSJoe Perches# alloc style 5283a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 5284a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 5285a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 5286a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 5287a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 5288a640d25cSJoe Perches } 5289a640d25cSJoe Perches 529060a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 529160a55369SJoe Perches if ($^V && $^V ge 5.10.0 && 5292e367455aSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 529360a55369SJoe Perches my $oldfunc = $3; 529460a55369SJoe Perches my $a1 = $4; 529560a55369SJoe Perches my $a2 = $10; 529660a55369SJoe Perches my $newfunc = "kmalloc_array"; 529760a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 529860a55369SJoe Perches my $r1 = $a1; 529960a55369SJoe Perches my $r2 = $a2; 530060a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 530160a55369SJoe Perches $r1 = $a2; 530260a55369SJoe Perches $r2 = $a1; 530360a55369SJoe Perches } 5304e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 5305e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 5306e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 5307e367455aSJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) && 5308e367455aSJoe Perches $fix) { 5309194f66fcSJoe 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; 531060a55369SJoe Perches 531160a55369SJoe Perches } 531260a55369SJoe Perches } 531360a55369SJoe Perches } 531460a55369SJoe Perches 5315972fdea2SJoe Perches# check for krealloc arg reuse 5316972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 5317972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 5318972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 5319972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 5320972fdea2SJoe Perches } 5321972fdea2SJoe Perches 53225ce59ae0SJoe Perches# check for alloc argument mismatch 53235ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 53245ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 53255ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 53265ce59ae0SJoe Perches } 53275ce59ae0SJoe Perches 5328caf2a54fSJoe Perches# check for multiple semicolons 5329caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 5330d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 5331d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 5332d5e616fcSJoe Perches $fix) { 5333194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 5334d5e616fcSJoe Perches } 5335d1e2ad07SJoe Perches } 5336d1e2ad07SJoe Perches 53370ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit) 53380ab90191SJoe Perches if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 53390ab90191SJoe Perches my $ull = ""; 53400ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 53410ab90191SJoe Perches if (CHK("BIT_MACRO", 53420ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 53430ab90191SJoe Perches $fix) { 53440ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 53450ab90191SJoe Perches } 53460ab90191SJoe Perches } 53470ab90191SJoe Perches 5348e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 5349c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 5350c34c09a8SJoe Perches my $has_break = 0; 5351c34c09a8SJoe Perches my $has_statement = 0; 5352c34c09a8SJoe Perches my $count = 0; 5353c34c09a8SJoe Perches my $prevline = $linenr; 5354e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 5355c34c09a8SJoe Perches $prevline--; 5356c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 5357c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 5358c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 5359c34c09a8SJoe Perches next if ($fline =~ /^\-/); 5360c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 5361c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 5362c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 5363c34c09a8SJoe Perches $has_statement = 1; 5364c34c09a8SJoe Perches $count++; 5365c34c09a8SJoe Perches $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); 5366c34c09a8SJoe Perches } 5367c34c09a8SJoe Perches if (!$has_break && $has_statement) { 5368c34c09a8SJoe Perches WARN("MISSING_BREAK", 5369c34c09a8SJoe Perches "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); 5370c34c09a8SJoe Perches } 5371c34c09a8SJoe Perches } 5372c34c09a8SJoe Perches 5373d1e2ad07SJoe Perches# check for switch/default statements without a break; 5374d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 5375d1e2ad07SJoe Perches defined $stat && 5376d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 5377d1e2ad07SJoe Perches my $ctx = ''; 5378d1e2ad07SJoe Perches my $herectx = $here . "\n"; 5379d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 5380d1e2ad07SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 5381d1e2ad07SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 5382d1e2ad07SJoe Perches } 5383d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 5384d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 5385caf2a54fSJoe Perches } 5386caf2a54fSJoe Perches 538713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 5388d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 5389d5e616fcSJoe Perches if (WARN("USE_FUNC", 5390d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 5391d5e616fcSJoe Perches $fix) { 5392194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 5393d5e616fcSJoe Perches } 539413214adfSAndy Whitcroft } 5395773647a0SAndy Whitcroft 539662ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 539762ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 539862ec818fSJoe Perches ERROR("DATE_TIME", 539962ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 540062ec818fSJoe Perches } 540162ec818fSJoe Perches 54022c92488aSJoe Perches# check for use of yield() 54032c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 54042c92488aSJoe Perches WARN("YIELD", 54052c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 54062c92488aSJoe Perches } 54072c92488aSJoe Perches 5408179f8f40SJoe Perches# check for comparisons against true and false 5409179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 5410179f8f40SJoe Perches my $lead = $1; 5411179f8f40SJoe Perches my $arg = $2; 5412179f8f40SJoe Perches my $test = $3; 5413179f8f40SJoe Perches my $otype = $4; 5414179f8f40SJoe Perches my $trail = $5; 5415179f8f40SJoe Perches my $op = "!"; 5416179f8f40SJoe Perches 5417179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 5418179f8f40SJoe Perches 5419179f8f40SJoe Perches my $type = lc($otype); 5420179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 5421179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 5422179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 5423179f8f40SJoe Perches $op = ""; 5424179f8f40SJoe Perches } 5425179f8f40SJoe Perches 5426179f8f40SJoe Perches CHK("BOOL_COMPARISON", 5427179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 5428179f8f40SJoe Perches 5429179f8f40SJoe Perches## maybe suggesting a correct construct would better 5430179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 5431179f8f40SJoe Perches 5432179f8f40SJoe Perches } 5433179f8f40SJoe Perches } 5434179f8f40SJoe Perches 54354882720bSThomas Gleixner# check for semaphores initialized locked 54364882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 5437000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 5438000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 5439773647a0SAndy Whitcroft } 54406712d858SJoe Perches 544167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 544267d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 5443000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 544467d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 5445773647a0SAndy Whitcroft } 54466712d858SJoe Perches 5447ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 5448f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 5449000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 5450ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 5451f3db6639SMichael Ellerman } 54526712d858SJoe Perches 54530f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 54540f3c5aabSJoe Perches my $const_structs = qr{ 54550f3c5aabSJoe Perches acpi_dock_ops| 545679404849SEmese Revfy address_space_operations| 545779404849SEmese Revfy backlight_ops| 545879404849SEmese Revfy block_device_operations| 545979404849SEmese Revfy dentry_operations| 546079404849SEmese Revfy dev_pm_ops| 546179404849SEmese Revfy dma_map_ops| 546279404849SEmese Revfy extent_io_ops| 546379404849SEmese Revfy file_lock_operations| 546479404849SEmese Revfy file_operations| 546579404849SEmese Revfy hv_ops| 546679404849SEmese Revfy ide_dma_ops| 546779404849SEmese Revfy intel_dvo_dev_ops| 546879404849SEmese Revfy item_operations| 546979404849SEmese Revfy iwl_ops| 547079404849SEmese Revfy kgdb_arch| 547179404849SEmese Revfy kgdb_io| 547279404849SEmese Revfy kset_uevent_ops| 547379404849SEmese Revfy lock_manager_operations| 547479404849SEmese Revfy microcode_ops| 547579404849SEmese Revfy mtrr_ops| 547679404849SEmese Revfy neigh_ops| 547779404849SEmese Revfy nlmsvc_binding| 54780f3c5aabSJoe Perches of_device_id| 547979404849SEmese Revfy pci_raw_ops| 548079404849SEmese Revfy pipe_buf_operations| 548179404849SEmese Revfy platform_hibernation_ops| 548279404849SEmese Revfy platform_suspend_ops| 548379404849SEmese Revfy proto_ops| 548479404849SEmese Revfy rpc_pipe_ops| 548579404849SEmese Revfy seq_operations| 548679404849SEmese Revfy snd_ac97_build_ops| 548779404849SEmese Revfy soc_pcmcia_socket_ops| 548879404849SEmese Revfy stacktrace_ops| 548979404849SEmese Revfy sysfs_ops| 549079404849SEmese Revfy tty_operations| 54916d07d01bSJoe Perches uart_ops| 549279404849SEmese Revfy usb_mon_operations| 549379404849SEmese Revfy wd_ops}x; 54946903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 54950f3c5aabSJoe Perches $line =~ /\bstruct\s+($const_structs)\b/) { 5496000d1cc1SJoe Perches WARN("CONST_STRUCT", 5497000d1cc1SJoe Perches "struct $1 should normally be const\n" . 54986903ffb2SAndy Whitcroft $herecurr); 54992b6db5cbSAndy Whitcroft } 5500773647a0SAndy Whitcroft 5501773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 5502773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 5503773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 5504c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 5505c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 5506171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 5507171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 5508171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 5509773647a0SAndy Whitcroft { 5510000d1cc1SJoe Perches WARN("NR_CPUS", 5511000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 5512773647a0SAndy Whitcroft } 55139c9ba34eSAndy Whitcroft 551452ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 551552ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 551652ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 551752ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 551852ea8506SJoe Perches } 551952ea8506SJoe Perches 5520acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 5521acd9362cSJoe Perches if ($^V && $^V ge 5.10.0 && 5522acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 5523acd9362cSJoe Perches WARN("LIKELY_MISUSE", 5524acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 5525acd9362cSJoe Perches } 5526acd9362cSJoe Perches 5527691d77b6SAndy Whitcroft# whine mightly about in_atomic 5528691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 5529691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 5530000d1cc1SJoe Perches ERROR("IN_ATOMIC", 5531000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 5532f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 5533000d1cc1SJoe Perches WARN("IN_ATOMIC", 5534000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 5535691d77b6SAndy Whitcroft } 5536691d77b6SAndy Whitcroft } 55371704f47bSPeter Zijlstra 55381704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 55391704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 55401704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 55411704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 55421704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 55431704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 5544000d1cc1SJoe Perches ERROR("LOCKDEP", 5545000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 55461704f47bSPeter Zijlstra } 55471704f47bSPeter Zijlstra } 554888f8831cSDave Jones 5549b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 5550b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 5551000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 5552000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 555388f8831cSDave Jones } 55542435880fSJoe Perches 5555515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 5556515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 5557515a235eSJoe Perches if ($^V && $^V ge 5.10.0 && 5558515a235eSJoe Perches $line =~ /$mode_perms_search/) { 55592435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 55602435880fSJoe Perches my $func = $entry->[0]; 55612435880fSJoe Perches my $arg_pos = $entry->[1]; 55622435880fSJoe Perches 55632435880fSJoe Perches my $skip_args = ""; 55642435880fSJoe Perches if ($arg_pos > 1) { 55652435880fSJoe Perches $arg_pos--; 55662435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 55672435880fSJoe Perches } 55682435880fSJoe Perches my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; 5569515a235eSJoe Perches if ($line =~ /$test/) { 55702435880fSJoe Perches my $val = $1; 55712435880fSJoe Perches $val = $6 if ($skip_args ne ""); 55722435880fSJoe Perches 55731727cc70SJoe Perches if ($val !~ /^0$/ && 55741727cc70SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 55751727cc70SJoe Perches length($val) ne 4)) { 55762435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 55771727cc70SJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); 5578c0a5c898SJoe Perches } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) { 5579c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 5580c0a5c898SJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 55812435880fSJoe Perches } 55822435880fSJoe Perches } 55832435880fSJoe Perches } 558413214adfSAndy Whitcroft } 5585515a235eSJoe Perches } 558613214adfSAndy Whitcroft 558713214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 558813214adfSAndy Whitcroft # so just keep quiet. 558913214adfSAndy Whitcroft if ($#rawlines == -1) { 559013214adfSAndy Whitcroft exit(0); 55910a920b5bSAndy Whitcroft } 55920a920b5bSAndy Whitcroft 55938905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 55948905a67cSAndy Whitcroft # things that appear to be patches. 55958905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 55968905a67cSAndy Whitcroft exit(0); 55978905a67cSAndy Whitcroft } 55988905a67cSAndy Whitcroft 55998905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 56008905a67cSAndy Whitcroft # just keep quiet. 56018905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 56028905a67cSAndy Whitcroft exit(0); 56038905a67cSAndy Whitcroft } 56048905a67cSAndy Whitcroft 56058905a67cSAndy Whitcroft if (!$is_patch) { 5606000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 5607000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 56080a920b5bSAndy Whitcroft } 56090a920b5bSAndy Whitcroft if ($is_patch && $chk_signoff && $signoff == 0) { 5610000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 5611000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 56120a920b5bSAndy Whitcroft } 56130a920b5bSAndy Whitcroft 5614f0a594c1SAndy Whitcroft print report_dump(); 561513214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 561613214adfSAndy Whitcroft print "$filename " if ($summary_file); 56176c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 56186c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 56196c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 56206c72ffaaSAndy Whitcroft } 56218905a67cSAndy Whitcroft 5622d2c0a235SAndy Whitcroft if ($quiet == 0) { 5623d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 5624d2c0a235SAndy Whitcroft # then suggest that. 5625d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 5626b0781216SMike Frysinger $rpt_cleaners = 0; 5627d8469f16SJoe Perches print << "EOM" 5628d8469f16SJoe Perches 5629d8469f16SJoe PerchesNOTE: Whitespace errors detected. 5630d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 5631d8469f16SJoe PerchesEOM 5632d2c0a235SAndy Whitcroft } 5633d2c0a235SAndy Whitcroft } 5634d2c0a235SAndy Whitcroft 563591bfe484SJoe Perches hash_show_words(\%use_type, "Used"); 563691bfe484SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 5637000d1cc1SJoe Perches 5638d752fcc8SJoe Perches if ($clean == 0 && $fix && 5639d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 5640d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 56419624b8d6SJoe Perches my $newfile = $filename; 56429624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 56433705ce5bSJoe Perches my $linecount = 0; 56443705ce5bSJoe Perches my $f; 56453705ce5bSJoe Perches 5646d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 5647d752fcc8SJoe Perches 56483705ce5bSJoe Perches open($f, '>', $newfile) 56493705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 56503705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 56513705ce5bSJoe Perches $linecount++; 56523705ce5bSJoe Perches if ($file) { 56533705ce5bSJoe Perches if ($linecount > 3) { 56543705ce5bSJoe Perches $fixed_line =~ s/^\+//; 56553705ce5bSJoe Perches print $f $fixed_line . "\n"; 56563705ce5bSJoe Perches } 56573705ce5bSJoe Perches } else { 56583705ce5bSJoe Perches print $f $fixed_line . "\n"; 56593705ce5bSJoe Perches } 56603705ce5bSJoe Perches } 56613705ce5bSJoe Perches close($f); 56623705ce5bSJoe Perches 56633705ce5bSJoe Perches if (!$quiet) { 56643705ce5bSJoe Perches print << "EOM"; 5665d8469f16SJoe Perches 56663705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 56673705ce5bSJoe Perches 56683705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 56693705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 56703705ce5bSJoe Perches 56713705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 56723705ce5bSJoe PerchesNo warranties, expressed or implied... 56733705ce5bSJoe PerchesEOM 56743705ce5bSJoe Perches } 56753705ce5bSJoe Perches } 56763705ce5bSJoe Perches 5677d8469f16SJoe Perches if ($quiet == 0) { 5678d8469f16SJoe Perches print "\n"; 5679d8469f16SJoe Perches if ($clean == 1) { 5680d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 5681d8469f16SJoe Perches } else { 5682d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 56830a920b5bSAndy Whitcroft } 56840a920b5bSAndy Whitcroft } 56850a920b5bSAndy Whitcroft return $clean; 56860a920b5bSAndy Whitcroft} 5687