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'; 1257230297SJoe 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; 2834d8815fSJoe Perchesmy $showfile = 0; 296c72ffaaSAndy Whitcroftmy $file = 0; 306c72ffaaSAndy Whitcroftmy $check = 0; 312ac73b4fSJoe Perchesmy $check_orig = 0; 328905a67cSAndy Whitcroftmy $summary = 1; 338905a67cSAndy Whitcroftmy $mailback = 0; 3413214adfSAndy Whitcroftmy $summary_file = 0; 35000d1cc1SJoe Perchesmy $show_types = 0; 363705ce5bSJoe Perchesmy $fix = 0; 379624b8d6SJoe Perchesmy $fix_inplace = 0; 386c72ffaaSAndy Whitcroftmy $root; 39c2fdda0dSAndy Whitcroftmy %debug; 403445686aSJoe Perchesmy %camelcase = (); 4191bfe484SJoe Perchesmy %use_type = (); 4291bfe484SJoe Perchesmy @use = (); 4391bfe484SJoe Perchesmy %ignore_type = (); 44000d1cc1SJoe Perchesmy @ignore = (); 4577f5b10aSHannes Edermy $help = 0; 46000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 476cd7f386SJoe Perchesmy $max_line_length = 80; 48d62a201fSDave Hansenmy $ignore_perl_version = 0; 49d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 5056193274SVadim Bendeburymy $min_conf_desc_length = 4; 5166b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 52ebfd7d62SJoe Perchesmy $codespell = 0; 53f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 5457230297SJoe Perchesmy $color = 1; 5577f5b10aSHannes Eder 5677f5b10aSHannes Edersub help { 5777f5b10aSHannes Eder my ($exitcode) = @_; 5877f5b10aSHannes Eder 5977f5b10aSHannes Eder print << "EOM"; 6077f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 6177f5b10aSHannes EderVersion: $V 6277f5b10aSHannes Eder 6377f5b10aSHannes EderOptions: 6477f5b10aSHannes Eder -q, --quiet quiet 6577f5b10aSHannes Eder --no-tree run without a kernel tree 6677f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 6777f5b10aSHannes Eder --patch treat FILE as patchfile (default) 6877f5b10aSHannes Eder --emacs emacs compile window format 6977f5b10aSHannes Eder --terse one line per report 7034d8815fSJoe Perches --showfile emit diffed file position, not input file position 7177f5b10aSHannes Eder -f, --file treat FILE as regular source file 7277f5b10aSHannes Eder --subjective, --strict enable more subjective tests 7391bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 74000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 756cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 7656193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 77000d1cc1SJoe Perches --show-types show the message "types" in the output 7877f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 7977f5b10aSHannes Eder --no-summary suppress the per-file summary 8077f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 8177f5b10aSHannes Eder --summary-file include the filename in summary 8277f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 8377f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 8477f5b10aSHannes Eder is all off) 8577f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 8677f5b10aSHannes Eder literally 873705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 883705ce5bSJoe Perches If correctable single-line errors exist, create 893705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 903705ce5bSJoe Perches with potential errors corrected to the preferred 913705ce5bSJoe Perches checkpatch style 929624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 939624b8d6SJoe Perches Is the same as --fix, but overwrites the input 949624b8d6SJoe Perches file. It's your fault if there's no backup or git 95d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 96d62a201fSDave Hansen runtime errors. 97ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 98f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 99ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 10057230297SJoe Perches --color Use colors when output is STDOUT (default: on) 10177f5b10aSHannes Eder -h, --help, --version display this help and exit 10277f5b10aSHannes Eder 10377f5b10aSHannes EderWhen FILE is - read standard input. 10477f5b10aSHannes EderEOM 10577f5b10aSHannes Eder 10677f5b10aSHannes Eder exit($exitcode); 10777f5b10aSHannes Eder} 10877f5b10aSHannes Eder 109000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 110000d1cc1SJoe Perchesif (-f $conf) { 111000d1cc1SJoe Perches my @conf_args; 112000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 113000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 114000d1cc1SJoe Perches 115000d1cc1SJoe Perches while (<$conffile>) { 116000d1cc1SJoe Perches my $line = $_; 117000d1cc1SJoe Perches 118000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 119000d1cc1SJoe Perches $line =~ s/^\s*//g; 120000d1cc1SJoe Perches $line =~ s/\s+/ /g; 121000d1cc1SJoe Perches 122000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 123000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 124000d1cc1SJoe Perches 125000d1cc1SJoe Perches my @words = split(" ", $line); 126000d1cc1SJoe Perches foreach my $word (@words) { 127000d1cc1SJoe Perches last if ($word =~ m/^#/); 128000d1cc1SJoe Perches push (@conf_args, $word); 129000d1cc1SJoe Perches } 130000d1cc1SJoe Perches } 131000d1cc1SJoe Perches close($conffile); 132000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 133000d1cc1SJoe Perches} 134000d1cc1SJoe Perches 1350a920b5bSAndy WhitcroftGetOptions( 1366c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1370a920b5bSAndy Whitcroft 'tree!' => \$tree, 1380a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1390a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 1406c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 1418905a67cSAndy Whitcroft 'terse!' => \$terse, 14234d8815fSJoe Perches 'showfile!' => \$showfile, 14377f5b10aSHannes Eder 'f|file!' => \$file, 1446c72ffaaSAndy Whitcroft 'subjective!' => \$check, 1456c72ffaaSAndy Whitcroft 'strict!' => \$check, 146000d1cc1SJoe Perches 'ignore=s' => \@ignore, 14791bfe484SJoe Perches 'types=s' => \@use, 148000d1cc1SJoe Perches 'show-types!' => \$show_types, 1496cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 15056193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 1516c72ffaaSAndy Whitcroft 'root=s' => \$root, 1528905a67cSAndy Whitcroft 'summary!' => \$summary, 1538905a67cSAndy Whitcroft 'mailback!' => \$mailback, 15413214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 1553705ce5bSJoe Perches 'fix!' => \$fix, 1569624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 157d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 158c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 159773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 160ebfd7d62SJoe Perches 'codespell!' => \$codespell, 161ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 16257230297SJoe Perches 'color!' => \$color, 16377f5b10aSHannes Eder 'h|help' => \$help, 16477f5b10aSHannes Eder 'version' => \$help 16577f5b10aSHannes Eder) or help(1); 16677f5b10aSHannes Eder 16777f5b10aSHannes Ederhelp(0) if ($help); 1680a920b5bSAndy Whitcroft 1699624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 1702ac73b4fSJoe Perches$check_orig = $check; 1719624b8d6SJoe Perches 1720a920b5bSAndy Whitcroftmy $exit = 0; 1730a920b5bSAndy Whitcroft 174d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 175d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 176d62a201fSDave Hansen if (!$ignore_perl_version) { 177d62a201fSDave Hansen exit(1); 178d62a201fSDave Hansen } 179d62a201fSDave Hansen} 180d62a201fSDave Hansen 1810a920b5bSAndy Whitcroftif ($#ARGV < 0) { 18277f5b10aSHannes Eder print "$P: no input files\n"; 1830a920b5bSAndy Whitcroft exit(1); 1840a920b5bSAndy Whitcroft} 1850a920b5bSAndy Whitcroft 18691bfe484SJoe Perchessub hash_save_array_words { 18791bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 18891bfe484SJoe Perches 18991bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 19091bfe484SJoe Perches foreach my $word (@array) { 191000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 192000d1cc1SJoe Perches $word =~ s/^\s*//g; 193000d1cc1SJoe Perches $word =~ s/\s+/ /g; 194000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 195000d1cc1SJoe Perches 196000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 197000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 198000d1cc1SJoe Perches 19991bfe484SJoe Perches $hashRef->{$word}++; 200000d1cc1SJoe Perches } 20191bfe484SJoe Perches} 20291bfe484SJoe Perches 20391bfe484SJoe Perchessub hash_show_words { 20491bfe484SJoe Perches my ($hashRef, $prefix) = @_; 20591bfe484SJoe Perches 2063c816e49SJoe Perches if (keys %$hashRef) { 207d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 20858cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 20991bfe484SJoe Perches print " $word"; 21091bfe484SJoe Perches } 211d8469f16SJoe Perches print "\n"; 21291bfe484SJoe Perches } 21391bfe484SJoe Perches} 21491bfe484SJoe Perches 21591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 21691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 217000d1cc1SJoe Perches 218c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 219c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 2207429c690SAndy Whitcroftmy $dbg_type = 0; 221a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 222c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 22321caa13cSAndy Whitcroft ## no critic 22421caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 22521caa13cSAndy Whitcroft die "$@" if ($@); 226c2fdda0dSAndy Whitcroft} 227c2fdda0dSAndy Whitcroft 228d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 229d2c0a235SAndy Whitcroft 2308905a67cSAndy Whitcroftif ($terse) { 2318905a67cSAndy Whitcroft $emacs = 1; 2328905a67cSAndy Whitcroft $quiet++; 2338905a67cSAndy Whitcroft} 2348905a67cSAndy Whitcroft 2356c72ffaaSAndy Whitcroftif ($tree) { 2366c72ffaaSAndy Whitcroft if (defined $root) { 2376c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 2386c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 2396c72ffaaSAndy Whitcroft } 2406c72ffaaSAndy Whitcroft } else { 2416c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 2426c72ffaaSAndy Whitcroft $root = '.'; 2436c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 2446c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 2456c72ffaaSAndy Whitcroft $root = $1; 2466c72ffaaSAndy Whitcroft } 2476c72ffaaSAndy Whitcroft } 2486c72ffaaSAndy Whitcroft 2496c72ffaaSAndy Whitcroft if (!defined $root) { 2500a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 2510a920b5bSAndy Whitcroft exit(2); 2520a920b5bSAndy Whitcroft } 2536c72ffaaSAndy Whitcroft} 2546c72ffaaSAndy Whitcroft 2556c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 2566c72ffaaSAndy Whitcroft 2572ceb532bSAndy Whitcroftour $Ident = qr{ 2582ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 2592ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 2602ceb532bSAndy Whitcroft }x; 2616c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 2626c72ffaaSAndy Whitcroftour $Sparse = qr{ 2636c72ffaaSAndy Whitcroft __user| 2646c72ffaaSAndy Whitcroft __kernel| 2656c72ffaaSAndy Whitcroft __force| 2666c72ffaaSAndy Whitcroft __iomem| 2676c72ffaaSAndy Whitcroft __must_check| 2686c72ffaaSAndy Whitcroft __init_refok| 269417495edSAndy Whitcroft __kprobes| 270165e72a6SSven Eckelmann __ref| 271165e72a6SSven Eckelmann __rcu 2726c72ffaaSAndy Whitcroft }x; 273e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 274e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 275e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 276e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 277e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 2788716de38SJoe Perches 27952131292SWolfram Sang# Notes to $Attribute: 28052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 2816c72ffaaSAndy Whitcroftour $Attribute = qr{ 2826c72ffaaSAndy Whitcroft const| 28303f1df7dSJoe Perches __percpu| 28403f1df7dSJoe Perches __nocast| 28503f1df7dSJoe Perches __safe| 28603f1df7dSJoe Perches __bitwise__| 28703f1df7dSJoe Perches __packed__| 28803f1df7dSJoe Perches __packed2__| 28903f1df7dSJoe Perches __naked| 29003f1df7dSJoe Perches __maybe_unused| 29103f1df7dSJoe Perches __always_unused| 29203f1df7dSJoe Perches __noreturn| 29303f1df7dSJoe Perches __used| 29403f1df7dSJoe Perches __cold| 295e23ef1f3SJoe Perches __pure| 29603f1df7dSJoe Perches __noclone| 29703f1df7dSJoe Perches __deprecated| 2986c72ffaaSAndy Whitcroft __read_mostly| 2996c72ffaaSAndy Whitcroft __kprobes| 3008716de38SJoe Perches $InitAttribute| 30124e1d81aSAndy Whitcroft ____cacheline_aligned| 30224e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 3035fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 3045fe3af11SAndy Whitcroft __weak 3056c72ffaaSAndy Whitcroft }x; 306c45dcabdSAndy Whitcroftour $Modifier; 30791cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 3086c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 3096c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 3106c72ffaaSAndy Whitcroft 31195e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 31295e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 31395e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 31495e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 3152435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 316c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 317326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 318326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 319326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 32074349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 3212435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 322326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 323447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 32423f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 3256c72ffaaSAndy Whitcroftour $Operators = qr{ 3266c72ffaaSAndy Whitcroft <=|>=|==|!=| 3276c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 32823f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 3296c72ffaaSAndy Whitcroft }x; 3306c72ffaaSAndy Whitcroft 33191cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 33291cb5195SJoe Perches 333ab7e23f3SJoe Perchesour $BasicType; 3348905a67cSAndy Whitcroftour $NonptrType; 3351813087dSJoe Perchesour $NonptrTypeMisordered; 3368716de38SJoe Perchesour $NonptrTypeWithAttr; 3378905a67cSAndy Whitcroftour $Type; 3381813087dSJoe Perchesour $TypeMisordered; 3398905a67cSAndy Whitcroftour $Declare; 3401813087dSJoe Perchesour $DeclareMisordered; 3418905a67cSAndy Whitcroft 34215662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 34315662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 344171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 345171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 346171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 347171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 348171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 349171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 350171ae1a4SAndy Whitcroft}x; 351171ae1a4SAndy Whitcroft 35215662b3eSJoe Perchesour $UTF8 = qr{ 35315662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 35415662b3eSJoe Perches | $NON_ASCII_UTF8 35515662b3eSJoe Perches}x; 35615662b3eSJoe Perches 357e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 358021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 359021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 360021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 361021158b4SJoe Perches)}; 362e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 363fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 3648ed22cadSAndy Whitcroft atomic_t 3658ed22cadSAndy Whitcroft)}; 366e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 367e6176fa4SJoe Perches $typeC99Typedefs\b| 368e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 369e6176fa4SJoe Perches $typeKernelTypedefs\b 370e6176fa4SJoe Perches)}; 3718ed22cadSAndy Whitcroft 372691e669bSJoe Perchesour $logFunctions = qr{(?x: 3736e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 3747d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 3756e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 376b0531722SJoe Perches panic| 37706668727SJoe Perches MODULE_[A-Z_]+| 37806668727SJoe Perches seq_vprintf|seq_printf|seq_puts 379691e669bSJoe Perches)}; 380691e669bSJoe Perches 38120112475SJoe Perchesour $signature_tags = qr{(?xi: 38220112475SJoe Perches Signed-off-by:| 38320112475SJoe Perches Acked-by:| 38420112475SJoe Perches Tested-by:| 38520112475SJoe Perches Reviewed-by:| 38620112475SJoe Perches Reported-by:| 3878543ae12SMugunthan V N Suggested-by:| 38820112475SJoe Perches To:| 38920112475SJoe Perches Cc: 39020112475SJoe Perches)}; 39120112475SJoe Perches 3921813087dSJoe Perchesour @typeListMisordered = ( 3931813087dSJoe Perches qr{char\s+(?:un)?signed}, 3941813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 3951813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 3961813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 3971813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 3981813087dSJoe Perches qr{short\s+(?:un)?signed}, 3991813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 4001813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 4011813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 4021813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 4031813087dSJoe Perches qr{int\s+(?:un)?signed}, 4041813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 4051813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 4061813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 4071813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 4081813087dSJoe Perches qr{long\s+(?:un)?signed}, 4091813087dSJoe Perches); 4101813087dSJoe Perches 4118905a67cSAndy Whitcroftour @typeList = ( 4128905a67cSAndy Whitcroft qr{void}, 4130c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 4140c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 4150c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 4160c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 4170c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 4180c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 4190c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 4200c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 4210c773d9dSJoe Perches qr{(?:un)?signed}, 4228905a67cSAndy Whitcroft qr{float}, 4238905a67cSAndy Whitcroft qr{double}, 4248905a67cSAndy Whitcroft qr{bool}, 4258905a67cSAndy Whitcroft qr{struct\s+$Ident}, 4268905a67cSAndy Whitcroft qr{union\s+$Ident}, 4278905a67cSAndy Whitcroft qr{enum\s+$Ident}, 4288905a67cSAndy Whitcroft qr{${Ident}_t}, 4298905a67cSAndy Whitcroft qr{${Ident}_handler}, 4308905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 4311813087dSJoe Perches @typeListMisordered, 4328905a67cSAndy Whitcroft); 433485ff23eSAlex Dowadour @typeListFile = (); 4348716de38SJoe Perchesour @typeListWithAttr = ( 4358716de38SJoe Perches @typeList, 4368716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 4378716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 4388716de38SJoe Perches); 4398716de38SJoe Perches 440c45dcabdSAndy Whitcroftour @modifierList = ( 441c45dcabdSAndy Whitcroft qr{fastcall}, 442c45dcabdSAndy Whitcroft); 443485ff23eSAlex Dowadour @modifierListFile = (); 4448905a67cSAndy Whitcroft 4452435880fSJoe Perchesour @mode_permission_funcs = ( 4462435880fSJoe Perches ["module_param", 3], 4472435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 4482435880fSJoe Perches ["module_param_array_named", 5], 4492435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 4502435880fSJoe Perches ["proc_create(?:_data|)", 2], 4512435880fSJoe Perches ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], 4522435880fSJoe Perches); 4532435880fSJoe Perches 454515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 455515a235eSJoe Perchesour $mode_perms_search = ""; 456515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 457515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 458515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 459515a235eSJoe Perches} 460515a235eSJoe Perches 461b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 462b392c64fSJoe Perches S_IWUGO | 463b392c64fSJoe Perches S_IWOTH | 464b392c64fSJoe Perches S_IRWXUGO | 465b392c64fSJoe Perches S_IALLUGO | 466b392c64fSJoe Perches 0[0-7][0-7][2367] 467b392c64fSJoe Perches}x; 468b392c64fSJoe Perches 4697840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 4707840a94cSWolfram Sang irq| 471cdcee686SSergey Ryazanov memory| 472cdcee686SSergey Ryazanov time| 473cdcee686SSergey Ryazanov reboot 4747840a94cSWolfram Sang)}; 4757840a94cSWolfram Sang# memory.h: ARM has a custom one 4767840a94cSWolfram Sang 47766b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 47866b47b4aSKees Cookmy $misspellings; 47966b47b4aSKees Cookmy %spelling_fix; 48036061e38SJoe Perches 48136061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 48266b47b4aSKees Cook while (<$spelling>) { 48366b47b4aSKees Cook my $line = $_; 48466b47b4aSKees Cook 48566b47b4aSKees Cook $line =~ s/\s*\n?$//g; 48666b47b4aSKees Cook $line =~ s/^\s*//g; 48766b47b4aSKees Cook 48866b47b4aSKees Cook next if ($line =~ m/^\s*#/); 48966b47b4aSKees Cook next if ($line =~ m/^\s*$/); 49066b47b4aSKees Cook 49166b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 49266b47b4aSKees Cook 49366b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 49466b47b4aSKees Cook } 49566b47b4aSKees Cook close($spelling); 49636061e38SJoe Perches} else { 49736061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 49836061e38SJoe Perches} 49966b47b4aSKees Cook 500ebfd7d62SJoe Perchesif ($codespell) { 501ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 502ebfd7d62SJoe Perches while (<$spelling>) { 503ebfd7d62SJoe Perches my $line = $_; 504ebfd7d62SJoe Perches 505ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 506ebfd7d62SJoe Perches $line =~ s/^\s*//g; 507ebfd7d62SJoe Perches 508ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 509ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 510ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 511ebfd7d62SJoe Perches 512ebfd7d62SJoe Perches $line =~ s/,.*$//; 513ebfd7d62SJoe Perches 514ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 515ebfd7d62SJoe Perches 516ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 517ebfd7d62SJoe Perches } 518ebfd7d62SJoe Perches close($spelling); 519ebfd7d62SJoe Perches } else { 520ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 521ebfd7d62SJoe Perches } 522ebfd7d62SJoe Perches} 523ebfd7d62SJoe Perches 524ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 525ebfd7d62SJoe Perches 5268905a67cSAndy Whitcroftsub build_types { 527485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 528485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 5291813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 5308716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 531c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 532ab7e23f3SJoe Perches $BasicType = qr{ 533ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 534ab7e23f3SJoe Perches (?:${all}\b) 535ab7e23f3SJoe Perches }x; 5368905a67cSAndy Whitcroft $NonptrType = qr{ 537d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 538cf655043SAndy Whitcroft (?: 5396b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 5408ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 541c45dcabdSAndy Whitcroft (?:${all}\b) 542cf655043SAndy Whitcroft ) 543c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 5448905a67cSAndy Whitcroft }x; 5451813087dSJoe Perches $NonptrTypeMisordered = qr{ 5461813087dSJoe Perches (?:$Modifier\s+|const\s+)* 5471813087dSJoe Perches (?: 5481813087dSJoe Perches (?:${Misordered}\b) 5491813087dSJoe Perches ) 5501813087dSJoe Perches (?:\s+$Modifier|\s+const)* 5511813087dSJoe Perches }x; 5528716de38SJoe Perches $NonptrTypeWithAttr = qr{ 5538716de38SJoe Perches (?:$Modifier\s+|const\s+)* 5548716de38SJoe Perches (?: 5558716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 5568716de38SJoe Perches (?:$typeTypedefs\b)| 5578716de38SJoe Perches (?:${allWithAttr}\b) 5588716de38SJoe Perches ) 5598716de38SJoe Perches (?:\s+$Modifier|\s+const)* 5608716de38SJoe Perches }x; 5618905a67cSAndy Whitcroft $Type = qr{ 562c45dcabdSAndy Whitcroft $NonptrType 5631574a29fSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 564c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 5658905a67cSAndy Whitcroft }x; 5661813087dSJoe Perches $TypeMisordered = qr{ 5671813087dSJoe Perches $NonptrTypeMisordered 5681813087dSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 5691813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 5701813087dSJoe Perches }x; 57191cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 5721813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 5738905a67cSAndy Whitcroft} 5748905a67cSAndy Whitcroftbuild_types(); 5756c72ffaaSAndy Whitcroft 5767d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 577d1fe9c09SJoe Perches 578d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 579d1fe9c09SJoe Perches# requires at least perl version v5.10.0 580d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 581d1fe9c09SJoe Perches 582d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 5832435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 584c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 5857d2367afSJoe Perches 586f8422308SJoe Perchesour $declaration_macros = qr{(?x: 5873e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 588f8422308SJoe Perches (?:$Storage\s+)?LIST_HEAD\s*\(| 589f8422308SJoe Perches (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\( 590f8422308SJoe Perches)}; 591f8422308SJoe Perches 5927d2367afSJoe Perchessub deparenthesize { 5937d2367afSJoe Perches my ($string) = @_; 5947d2367afSJoe Perches return "" if (!defined($string)); 5955b9553abSJoe Perches 5965b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 5975b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 5985b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 5995b9553abSJoe Perches } 6005b9553abSJoe Perches 6017d2367afSJoe Perches $string =~ s@\s+@ @g; 6025b9553abSJoe Perches 6037d2367afSJoe Perches return $string; 6047d2367afSJoe Perches} 6057d2367afSJoe Perches 6063445686aSJoe Perchessub seed_camelcase_file { 6073445686aSJoe Perches my ($file) = @_; 6083445686aSJoe Perches 6093445686aSJoe Perches return if (!(-f $file)); 6103445686aSJoe Perches 6113445686aSJoe Perches local $/; 6123445686aSJoe Perches 6133445686aSJoe Perches open(my $include_file, '<', "$file") 6143445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 6153445686aSJoe Perches my $text = <$include_file>; 6163445686aSJoe Perches close($include_file); 6173445686aSJoe Perches 6183445686aSJoe Perches my @lines = split('\n', $text); 6193445686aSJoe Perches 6203445686aSJoe Perches foreach my $line (@lines) { 6213445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 6223445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 6233445686aSJoe Perches $camelcase{$1} = 1; 62411ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 62511ea516aSJoe Perches $camelcase{$1} = 1; 62611ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 6273445686aSJoe Perches $camelcase{$1} = 1; 6283445686aSJoe Perches } 6293445686aSJoe Perches } 6303445686aSJoe Perches} 6313445686aSJoe Perches 6323445686aSJoe Perchesmy $camelcase_seeded = 0; 6333445686aSJoe Perchessub seed_camelcase_includes { 6343445686aSJoe Perches return if ($camelcase_seeded); 6353445686aSJoe Perches 6363445686aSJoe Perches my $files; 637c707a81dSJoe Perches my $camelcase_cache = ""; 638c707a81dSJoe Perches my @include_files = (); 639c707a81dSJoe Perches 640c707a81dSJoe Perches $camelcase_seeded = 1; 641351b2a1fSJoe Perches 6423645e328SRichard Genoud if (-e ".git") { 643351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 644351b2a1fSJoe Perches chomp $git_last_include_commit; 645c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 646c707a81dSJoe Perches } else { 647c707a81dSJoe Perches my $last_mod_date = 0; 648c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 649c707a81dSJoe Perches @include_files = split('\n', $files); 650c707a81dSJoe Perches foreach my $file (@include_files) { 651c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 652c707a81dSJoe Perches localtime((stat $file)[9])); 653c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 654c707a81dSJoe Perches } 655c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 656c707a81dSJoe Perches } 657c707a81dSJoe Perches 658c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 659c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 660c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 661351b2a1fSJoe Perches while (<$camelcase_file>) { 662351b2a1fSJoe Perches chomp; 663351b2a1fSJoe Perches $camelcase{$_} = 1; 664351b2a1fSJoe Perches } 665351b2a1fSJoe Perches close($camelcase_file); 666351b2a1fSJoe Perches 667351b2a1fSJoe Perches return; 668351b2a1fSJoe Perches } 669c707a81dSJoe Perches 6703645e328SRichard Genoud if (-e ".git") { 671c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 672c707a81dSJoe Perches @include_files = split('\n', $files); 6733445686aSJoe Perches } 674c707a81dSJoe Perches 6753445686aSJoe Perches foreach my $file (@include_files) { 6763445686aSJoe Perches seed_camelcase_file($file); 6773445686aSJoe Perches } 678351b2a1fSJoe Perches 679c707a81dSJoe Perches if ($camelcase_cache ne "") { 680351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 681c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 682c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 683351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 684351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 685351b2a1fSJoe Perches } 686351b2a1fSJoe Perches close($camelcase_file); 687351b2a1fSJoe Perches } 6883445686aSJoe Perches} 6893445686aSJoe Perches 690d311cd44SJoe Perchessub git_commit_info { 691d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 692d311cd44SJoe Perches 693d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 694d311cd44SJoe Perches 695d311cd44SJoe Perches my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; 696d311cd44SJoe Perches $output =~ s/^\s*//gm; 697d311cd44SJoe Perches my @lines = split("\n", $output); 698d311cd44SJoe Perches 6990d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 7000d7835fcSJoe Perches 701d311cd44SJoe Perches if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { 702d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 703d311cd44SJoe Perches# all matching commit ids, but it's very slow... 704d311cd44SJoe Perches# 705d311cd44SJoe Perches# echo "checking commits $1..." 706d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 707d311cd44SJoe Perches# while read line ; do 708d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 709d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 710d311cd44SJoe Perches# done 711d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 712d311cd44SJoe Perches } else { 713d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 714d311cd44SJoe Perches $desc = substr($lines[0], 41); 715d311cd44SJoe Perches } 716d311cd44SJoe Perches 717d311cd44SJoe Perches return ($id, $desc); 718d311cd44SJoe Perches} 719d311cd44SJoe Perches 7206c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 7210a920b5bSAndy Whitcroft 72200df344fSAndy Whitcroftmy @rawlines = (); 723c2fdda0dSAndy Whitcroftmy @lines = (); 7243705ce5bSJoe Perchesmy @fixed = (); 725d752fcc8SJoe Perchesmy @fixed_inserted = (); 726d752fcc8SJoe Perchesmy @fixed_deleted = (); 727194f66fcSJoe Perchesmy $fixlinenr = -1; 728194f66fcSJoe Perches 729c2fdda0dSAndy Whitcroftmy $vname; 7306c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 73121caa13cSAndy Whitcroft my $FILE; 7326c72ffaaSAndy Whitcroft if ($file) { 73321caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 7346c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 73521caa13cSAndy Whitcroft } elsif ($filename eq '-') { 73621caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 7376c72ffaaSAndy Whitcroft } else { 73821caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 7396c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 7406c72ffaaSAndy Whitcroft } 741c2fdda0dSAndy Whitcroft if ($filename eq '-') { 742c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 743c2fdda0dSAndy Whitcroft } else { 744c2fdda0dSAndy Whitcroft $vname = $filename; 745c2fdda0dSAndy Whitcroft } 74621caa13cSAndy Whitcroft while (<$FILE>) { 7470a920b5bSAndy Whitcroft chomp; 74800df344fSAndy Whitcroft push(@rawlines, $_); 7496c72ffaaSAndy Whitcroft } 75021caa13cSAndy Whitcroft close($FILE); 751d8469f16SJoe Perches 752d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 753d8469f16SJoe Perches print '-' x length($vname) . "\n"; 754d8469f16SJoe Perches print "$vname\n"; 755d8469f16SJoe Perches print '-' x length($vname) . "\n"; 756d8469f16SJoe Perches } 757d8469f16SJoe Perches 758c2fdda0dSAndy Whitcroft if (!process($filename)) { 7590a920b5bSAndy Whitcroft $exit = 1; 7600a920b5bSAndy Whitcroft } 76100df344fSAndy Whitcroft @rawlines = (); 76213214adfSAndy Whitcroft @lines = (); 7633705ce5bSJoe Perches @fixed = (); 764d752fcc8SJoe Perches @fixed_inserted = (); 765d752fcc8SJoe Perches @fixed_deleted = (); 766194f66fcSJoe Perches $fixlinenr = -1; 767485ff23eSAlex Dowad @modifierListFile = (); 768485ff23eSAlex Dowad @typeListFile = (); 769485ff23eSAlex Dowad build_types(); 7700a920b5bSAndy Whitcroft} 7710a920b5bSAndy Whitcroft 772d8469f16SJoe Perchesif (!$quiet) { 7733c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 7743c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 7753c816e49SJoe Perches 776d8469f16SJoe Perches if ($^V lt 5.10.0) { 777d8469f16SJoe Perches print << "EOM" 778d8469f16SJoe Perches 779d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 780d8469f16SJoe Perches An upgrade to at least perl v5.10.0 is suggested. 781d8469f16SJoe PerchesEOM 782d8469f16SJoe Perches } 783d8469f16SJoe Perches if ($exit) { 784d8469f16SJoe Perches print << "EOM" 785d8469f16SJoe Perches 786d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 787d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 788d8469f16SJoe PerchesEOM 789d8469f16SJoe Perches } 790d8469f16SJoe Perches} 791d8469f16SJoe Perches 7920a920b5bSAndy Whitcroftexit($exit); 7930a920b5bSAndy Whitcroft 7940a920b5bSAndy Whitcroftsub top_of_kernel_tree { 7956c72ffaaSAndy Whitcroft my ($root) = @_; 7966c72ffaaSAndy Whitcroft 7976c72ffaaSAndy Whitcroft my @tree_check = ( 7986c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 7996c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 8006c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 8016c72ffaaSAndy Whitcroft ); 8026c72ffaaSAndy Whitcroft 8036c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 8046c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 8050a920b5bSAndy Whitcroft return 0; 8060a920b5bSAndy Whitcroft } 8076c72ffaaSAndy Whitcroft } 8086c72ffaaSAndy Whitcroft return 1; 8096c72ffaaSAndy Whitcroft} 8100a920b5bSAndy Whitcroft 81120112475SJoe Perchessub parse_email { 81220112475SJoe Perches my ($formatted_email) = @_; 81320112475SJoe Perches 81420112475SJoe Perches my $name = ""; 81520112475SJoe Perches my $address = ""; 81620112475SJoe Perches my $comment = ""; 81720112475SJoe Perches 81820112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 81920112475SJoe Perches $name = $1; 82020112475SJoe Perches $address = $2; 82120112475SJoe Perches $comment = $3 if defined $3; 82220112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 82320112475SJoe Perches $address = $1; 82420112475SJoe Perches $comment = $2 if defined $2; 82520112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 82620112475SJoe Perches $address = $1; 82720112475SJoe Perches $comment = $2 if defined $2; 82820112475SJoe Perches $formatted_email =~ s/$address.*$//; 82920112475SJoe Perches $name = $formatted_email; 8303705ce5bSJoe Perches $name = trim($name); 83120112475SJoe Perches $name =~ s/^\"|\"$//g; 83220112475SJoe Perches # If there's a name left after stripping spaces and 83320112475SJoe Perches # leading quotes, and the address doesn't have both 83420112475SJoe Perches # leading and trailing angle brackets, the address 83520112475SJoe Perches # is invalid. ie: 83620112475SJoe Perches # "joe smith [email protected]" bad 83720112475SJoe Perches # "joe smith <[email protected]" bad 83820112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 83920112475SJoe Perches $name = ""; 84020112475SJoe Perches $address = ""; 84120112475SJoe Perches $comment = ""; 84220112475SJoe Perches } 84320112475SJoe Perches } 84420112475SJoe Perches 8453705ce5bSJoe Perches $name = trim($name); 84620112475SJoe Perches $name =~ s/^\"|\"$//g; 8473705ce5bSJoe Perches $address = trim($address); 84820112475SJoe Perches $address =~ s/^\<|\>$//g; 84920112475SJoe Perches 85020112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 85120112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 85220112475SJoe Perches $name = "\"$name\""; 85320112475SJoe Perches } 85420112475SJoe Perches 85520112475SJoe Perches return ($name, $address, $comment); 85620112475SJoe Perches} 85720112475SJoe Perches 85820112475SJoe Perchessub format_email { 85920112475SJoe Perches my ($name, $address) = @_; 86020112475SJoe Perches 86120112475SJoe Perches my $formatted_email; 86220112475SJoe Perches 8633705ce5bSJoe Perches $name = trim($name); 86420112475SJoe Perches $name =~ s/^\"|\"$//g; 8653705ce5bSJoe Perches $address = trim($address); 86620112475SJoe Perches 86720112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 86820112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 86920112475SJoe Perches $name = "\"$name\""; 87020112475SJoe Perches } 87120112475SJoe Perches 87220112475SJoe Perches if ("$name" eq "") { 87320112475SJoe Perches $formatted_email = "$address"; 87420112475SJoe Perches } else { 87520112475SJoe Perches $formatted_email = "$name <$address>"; 87620112475SJoe Perches } 87720112475SJoe Perches 87820112475SJoe Perches return $formatted_email; 87920112475SJoe Perches} 88020112475SJoe Perches 881d311cd44SJoe Perchessub which { 882d311cd44SJoe Perches my ($bin) = @_; 883d311cd44SJoe Perches 884d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 885d311cd44SJoe Perches if (-e "$path/$bin") { 886d311cd44SJoe Perches return "$path/$bin"; 887d311cd44SJoe Perches } 888d311cd44SJoe Perches } 889d311cd44SJoe Perches 890d311cd44SJoe Perches return ""; 891d311cd44SJoe Perches} 892d311cd44SJoe Perches 893000d1cc1SJoe Perchessub which_conf { 894000d1cc1SJoe Perches my ($conf) = @_; 895000d1cc1SJoe Perches 896000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 897000d1cc1SJoe Perches if (-e "$path/$conf") { 898000d1cc1SJoe Perches return "$path/$conf"; 899000d1cc1SJoe Perches } 900000d1cc1SJoe Perches } 901000d1cc1SJoe Perches 902000d1cc1SJoe Perches return ""; 903000d1cc1SJoe Perches} 904000d1cc1SJoe Perches 9050a920b5bSAndy Whitcroftsub expand_tabs { 9060a920b5bSAndy Whitcroft my ($str) = @_; 9070a920b5bSAndy Whitcroft 9080a920b5bSAndy Whitcroft my $res = ''; 9090a920b5bSAndy Whitcroft my $n = 0; 9100a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 9110a920b5bSAndy Whitcroft if ($c eq "\t") { 9120a920b5bSAndy Whitcroft $res .= ' '; 9130a920b5bSAndy Whitcroft $n++; 9140a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 9150a920b5bSAndy Whitcroft $res .= ' '; 9160a920b5bSAndy Whitcroft } 9170a920b5bSAndy Whitcroft next; 9180a920b5bSAndy Whitcroft } 9190a920b5bSAndy Whitcroft $res .= $c; 9200a920b5bSAndy Whitcroft $n++; 9210a920b5bSAndy Whitcroft } 9220a920b5bSAndy Whitcroft 9230a920b5bSAndy Whitcroft return $res; 9240a920b5bSAndy Whitcroft} 9256c72ffaaSAndy Whitcroftsub copy_spacing { 926773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 9276c72ffaaSAndy Whitcroft return $res; 9286c72ffaaSAndy Whitcroft} 9290a920b5bSAndy Whitcroft 9304a0df2efSAndy Whitcroftsub line_stats { 9314a0df2efSAndy Whitcroft my ($line) = @_; 9324a0df2efSAndy Whitcroft 9334a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 9344a0df2efSAndy Whitcroft $line =~ s/^.//; 9354a0df2efSAndy Whitcroft $line = expand_tabs($line); 9364a0df2efSAndy Whitcroft 9374a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 9384a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 9394a0df2efSAndy Whitcroft 9404a0df2efSAndy Whitcroft return (length($line), length($white)); 9414a0df2efSAndy Whitcroft} 9424a0df2efSAndy Whitcroft 943773647a0SAndy Whitcroftmy $sanitise_quote = ''; 944773647a0SAndy Whitcroft 945773647a0SAndy Whitcroftsub sanitise_line_reset { 946773647a0SAndy Whitcroft my ($in_comment) = @_; 947773647a0SAndy Whitcroft 948773647a0SAndy Whitcroft if ($in_comment) { 949773647a0SAndy Whitcroft $sanitise_quote = '*/'; 950773647a0SAndy Whitcroft } else { 951773647a0SAndy Whitcroft $sanitise_quote = ''; 952773647a0SAndy Whitcroft } 953773647a0SAndy Whitcroft} 95400df344fSAndy Whitcroftsub sanitise_line { 95500df344fSAndy Whitcroft my ($line) = @_; 95600df344fSAndy Whitcroft 95700df344fSAndy Whitcroft my $res = ''; 95800df344fSAndy Whitcroft my $l = ''; 95900df344fSAndy Whitcroft 960c2fdda0dSAndy Whitcroft my $qlen = 0; 961773647a0SAndy Whitcroft my $off = 0; 962773647a0SAndy Whitcroft my $c; 96300df344fSAndy Whitcroft 964773647a0SAndy Whitcroft # Always copy over the diff marker. 965773647a0SAndy Whitcroft $res = substr($line, 0, 1); 966773647a0SAndy Whitcroft 967773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 968773647a0SAndy Whitcroft $c = substr($line, $off, 1); 969773647a0SAndy Whitcroft 970773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 971773647a0SAndy Whitcroft # and end, all to $;. 972773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 973773647a0SAndy Whitcroft $sanitise_quote = '*/'; 974773647a0SAndy Whitcroft 975773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 976773647a0SAndy Whitcroft $off++; 97700df344fSAndy Whitcroft next; 978773647a0SAndy Whitcroft } 97981bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 980773647a0SAndy Whitcroft $sanitise_quote = ''; 981773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 982773647a0SAndy Whitcroft $off++; 983773647a0SAndy Whitcroft next; 984773647a0SAndy Whitcroft } 985113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 986113f04a8SDaniel Walker $sanitise_quote = '//'; 987113f04a8SDaniel Walker 988113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 989113f04a8SDaniel Walker $off++; 990113f04a8SDaniel Walker next; 991113f04a8SDaniel Walker } 992773647a0SAndy Whitcroft 993773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 994773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 995773647a0SAndy Whitcroft $c eq "\\") { 996773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 997773647a0SAndy Whitcroft $off++; 998773647a0SAndy Whitcroft next; 999773647a0SAndy Whitcroft } 1000773647a0SAndy Whitcroft # Regular quotes. 1001773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1002773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1003773647a0SAndy Whitcroft $sanitise_quote = $c; 1004773647a0SAndy Whitcroft 1005773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1006773647a0SAndy Whitcroft next; 1007773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1008773647a0SAndy Whitcroft $sanitise_quote = ''; 100900df344fSAndy Whitcroft } 101000df344fSAndy Whitcroft } 1011773647a0SAndy Whitcroft 1012fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1013773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1014773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1015113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1016113f04a8SDaniel Walker substr($res, $off, 1, $;); 1017773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1018773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 101900df344fSAndy Whitcroft } else { 1020773647a0SAndy Whitcroft substr($res, $off, 1, $c); 102100df344fSAndy Whitcroft } 1022c2fdda0dSAndy Whitcroft } 1023c2fdda0dSAndy Whitcroft 1024113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1025113f04a8SDaniel Walker $sanitise_quote = ''; 1026113f04a8SDaniel Walker } 1027113f04a8SDaniel Walker 1028c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1029c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1030c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1031c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1032c2fdda0dSAndy Whitcroft 1033c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1034c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1035c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1036c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1037c2fdda0dSAndy Whitcroft } 1038c2fdda0dSAndy Whitcroft 103900df344fSAndy Whitcroft return $res; 104000df344fSAndy Whitcroft} 104100df344fSAndy Whitcroft 1042a6962d72SJoe Perchessub get_quoted_string { 1043a6962d72SJoe Perches my ($line, $rawline) = @_; 1044a6962d72SJoe Perches 104533acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1046a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1047a6962d72SJoe Perches} 1048a6962d72SJoe Perches 10498905a67cSAndy Whitcroftsub ctx_statement_block { 10508905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 10518905a67cSAndy Whitcroft my $line = $linenr - 1; 10528905a67cSAndy Whitcroft my $blk = ''; 10538905a67cSAndy Whitcroft my $soff = $off; 10548905a67cSAndy Whitcroft my $coff = $off - 1; 1055773647a0SAndy Whitcroft my $coff_set = 0; 10568905a67cSAndy Whitcroft 105713214adfSAndy Whitcroft my $loff = 0; 105813214adfSAndy Whitcroft 10598905a67cSAndy Whitcroft my $type = ''; 10608905a67cSAndy Whitcroft my $level = 0; 1061a2750645SAndy Whitcroft my @stack = (); 1062cf655043SAndy Whitcroft my $p; 10638905a67cSAndy Whitcroft my $c; 10648905a67cSAndy Whitcroft my $len = 0; 106513214adfSAndy Whitcroft 106613214adfSAndy Whitcroft my $remainder; 10678905a67cSAndy Whitcroft while (1) { 1068a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1069a2750645SAndy Whitcroft 1070773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 10718905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 10728905a67cSAndy Whitcroft # context. 10738905a67cSAndy Whitcroft if ($off >= $len) { 10748905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1075dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1076c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 10778905a67cSAndy Whitcroft $remain--; 107813214adfSAndy Whitcroft $loff = $len; 1079c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 10808905a67cSAndy Whitcroft $len = length($blk); 10818905a67cSAndy Whitcroft $line++; 10828905a67cSAndy Whitcroft last; 10838905a67cSAndy Whitcroft } 10848905a67cSAndy Whitcroft # Bail if there is no further context. 10858905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 108613214adfSAndy Whitcroft if ($off >= $len) { 10878905a67cSAndy Whitcroft last; 10888905a67cSAndy Whitcroft } 1089f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1090f74bd194SAndy Whitcroft $level++; 1091f74bd194SAndy Whitcroft $type = '#'; 1092f74bd194SAndy Whitcroft } 10938905a67cSAndy Whitcroft } 1094cf655043SAndy Whitcroft $p = $c; 10958905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 109613214adfSAndy Whitcroft $remainder = substr($blk, $off); 10978905a67cSAndy Whitcroft 1098773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 10994635f4fbSAndy Whitcroft 11004635f4fbSAndy Whitcroft # Handle nested #if/#else. 11014635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 11024635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 11034635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 11044635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 11054635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 11064635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 11074635f4fbSAndy Whitcroft } 11084635f4fbSAndy Whitcroft 11098905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 11108905a67cSAndy Whitcroft # outermost level. 11118905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 11128905a67cSAndy Whitcroft last; 11138905a67cSAndy Whitcroft } 11148905a67cSAndy Whitcroft 111513214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1116773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1117773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1118773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1119773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1120773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1121773647a0SAndy Whitcroft $coff_set = 1; 1122773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1123773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 112413214adfSAndy Whitcroft } 112513214adfSAndy Whitcroft 11268905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 11278905a67cSAndy Whitcroft $level++; 11288905a67cSAndy Whitcroft $type = '('; 11298905a67cSAndy Whitcroft } 11308905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 11318905a67cSAndy Whitcroft $level--; 11328905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 11338905a67cSAndy Whitcroft 11348905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 11358905a67cSAndy Whitcroft $coff = $off; 1136773647a0SAndy Whitcroft $coff_set = 1; 1137773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 11388905a67cSAndy Whitcroft } 11398905a67cSAndy Whitcroft } 11408905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 11418905a67cSAndy Whitcroft $level++; 11428905a67cSAndy Whitcroft $type = '{'; 11438905a67cSAndy Whitcroft } 11448905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 11458905a67cSAndy Whitcroft $level--; 11468905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 11478905a67cSAndy Whitcroft 11488905a67cSAndy Whitcroft if ($level == 0) { 1149b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1150b998e001SPatrick Pannuto $off++; 1151b998e001SPatrick Pannuto } 11528905a67cSAndy Whitcroft last; 11538905a67cSAndy Whitcroft } 11548905a67cSAndy Whitcroft } 1155f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1156f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1157f74bd194SAndy Whitcroft $level--; 1158f74bd194SAndy Whitcroft $type = ''; 1159f74bd194SAndy Whitcroft $off++; 1160f74bd194SAndy Whitcroft last; 1161f74bd194SAndy Whitcroft } 11628905a67cSAndy Whitcroft $off++; 11638905a67cSAndy Whitcroft } 1164a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 116513214adfSAndy Whitcroft if ($off == $len) { 1166a3bb97a7SAndy Whitcroft $loff = $len + 1; 116713214adfSAndy Whitcroft $line++; 116813214adfSAndy Whitcroft $remain--; 116913214adfSAndy Whitcroft } 11708905a67cSAndy Whitcroft 11718905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 11728905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 11738905a67cSAndy Whitcroft 11748905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 11758905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 11768905a67cSAndy Whitcroft 1177773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 117813214adfSAndy Whitcroft 117913214adfSAndy Whitcroft return ($statement, $condition, 118013214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 118113214adfSAndy Whitcroft} 118213214adfSAndy Whitcroft 1183cf655043SAndy Whitcroftsub statement_lines { 1184cf655043SAndy Whitcroft my ($stmt) = @_; 1185cf655043SAndy Whitcroft 1186cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1187cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1188cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1189cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1190cf655043SAndy Whitcroft 1191cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1192cf655043SAndy Whitcroft 1193cf655043SAndy Whitcroft return $#stmt_lines + 2; 1194cf655043SAndy Whitcroft} 1195cf655043SAndy Whitcroft 1196cf655043SAndy Whitcroftsub statement_rawlines { 1197cf655043SAndy Whitcroft my ($stmt) = @_; 1198cf655043SAndy Whitcroft 1199cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1200cf655043SAndy Whitcroft 1201cf655043SAndy Whitcroft return $#stmt_lines + 2; 1202cf655043SAndy Whitcroft} 1203cf655043SAndy Whitcroft 1204cf655043SAndy Whitcroftsub statement_block_size { 1205cf655043SAndy Whitcroft my ($stmt) = @_; 1206cf655043SAndy Whitcroft 1207cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1208cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1209cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1210cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1211cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1212cf655043SAndy Whitcroft 1213cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1214cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1215cf655043SAndy Whitcroft 1216cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1217cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1218cf655043SAndy Whitcroft 1219cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1220cf655043SAndy Whitcroft return $stmt_lines; 1221cf655043SAndy Whitcroft } else { 1222cf655043SAndy Whitcroft return $stmt_statements; 1223cf655043SAndy Whitcroft } 1224cf655043SAndy Whitcroft} 1225cf655043SAndy Whitcroft 122613214adfSAndy Whitcroftsub ctx_statement_full { 122713214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 122813214adfSAndy Whitcroft my ($statement, $condition, $level); 122913214adfSAndy Whitcroft 123013214adfSAndy Whitcroft my (@chunks); 123113214adfSAndy Whitcroft 1232cf655043SAndy Whitcroft # Grab the first conditional/block pair. 123313214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 123413214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1235773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 123613214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1237cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1238cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1239cf655043SAndy Whitcroft } 1240cf655043SAndy Whitcroft 1241cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1242cf655043SAndy Whitcroft # could continue the statement. 1243cf655043SAndy Whitcroft for (;;) { 124413214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 124513214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1246cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1247773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1248cf655043SAndy Whitcroft #print "C: push\n"; 1249cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 125013214adfSAndy Whitcroft } 125113214adfSAndy Whitcroft 125213214adfSAndy Whitcroft return ($level, $linenr, @chunks); 12538905a67cSAndy Whitcroft} 12548905a67cSAndy Whitcroft 12554a0df2efSAndy Whitcroftsub ctx_block_get { 1256f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 12574a0df2efSAndy Whitcroft my $line; 12584a0df2efSAndy Whitcroft my $start = $linenr - 1; 12594a0df2efSAndy Whitcroft my $blk = ''; 12604a0df2efSAndy Whitcroft my @o; 12614a0df2efSAndy Whitcroft my @c; 12624a0df2efSAndy Whitcroft my @res = (); 12634a0df2efSAndy Whitcroft 1264f0a594c1SAndy Whitcroft my $level = 0; 12654635f4fbSAndy Whitcroft my @stack = ($level); 126600df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 126700df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 126800df344fSAndy Whitcroft $remain--; 126900df344fSAndy Whitcroft 127000df344fSAndy Whitcroft $blk .= $rawlines[$line]; 12714635f4fbSAndy Whitcroft 12724635f4fbSAndy Whitcroft # Handle nested #if/#else. 127301464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 12744635f4fbSAndy Whitcroft push(@stack, $level); 127501464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 12764635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 127701464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 12784635f4fbSAndy Whitcroft $level = pop(@stack); 12794635f4fbSAndy Whitcroft } 12804635f4fbSAndy Whitcroft 128101464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1282f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1283f0a594c1SAndy Whitcroft if ($off > 0) { 1284f0a594c1SAndy Whitcroft $off--; 1285f0a594c1SAndy Whitcroft next; 1286f0a594c1SAndy Whitcroft } 12874a0df2efSAndy Whitcroft 1288f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1289f0a594c1SAndy Whitcroft $level--; 1290f0a594c1SAndy Whitcroft last if ($level == 0); 1291f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1292f0a594c1SAndy Whitcroft $level++; 1293f0a594c1SAndy Whitcroft } 1294f0a594c1SAndy Whitcroft } 12954a0df2efSAndy Whitcroft 1296f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 129700df344fSAndy Whitcroft push(@res, $rawlines[$line]); 12984a0df2efSAndy Whitcroft } 12994a0df2efSAndy Whitcroft 1300f0a594c1SAndy Whitcroft last if ($level == 0); 13014a0df2efSAndy Whitcroft } 13024a0df2efSAndy Whitcroft 1303f0a594c1SAndy Whitcroft return ($level, @res); 13044a0df2efSAndy Whitcroft} 13054a0df2efSAndy Whitcroftsub ctx_block_outer { 13064a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 13074a0df2efSAndy Whitcroft 1308f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1309f0a594c1SAndy Whitcroft return @r; 13104a0df2efSAndy Whitcroft} 13114a0df2efSAndy Whitcroftsub ctx_block { 13124a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 13134a0df2efSAndy Whitcroft 1314f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1315f0a594c1SAndy Whitcroft return @r; 1316653d4876SAndy Whitcroft} 1317653d4876SAndy Whitcroftsub ctx_statement { 1318f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1319f0a594c1SAndy Whitcroft 1320f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1321f0a594c1SAndy Whitcroft return @r; 1322f0a594c1SAndy Whitcroft} 1323f0a594c1SAndy Whitcroftsub ctx_block_level { 1324653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1325653d4876SAndy Whitcroft 1326f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 13274a0df2efSAndy Whitcroft} 13289c0ca6f9SAndy Whitcroftsub ctx_statement_level { 13299c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 13309c0ca6f9SAndy Whitcroft 13319c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 13329c0ca6f9SAndy Whitcroft} 13334a0df2efSAndy Whitcroft 13344a0df2efSAndy Whitcroftsub ctx_locate_comment { 13354a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 13364a0df2efSAndy Whitcroft 13374a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1338beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 13394a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 13404a0df2efSAndy Whitcroft 13414a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 13424a0df2efSAndy Whitcroft # comment. 13434a0df2efSAndy Whitcroft my $in_comment = 0; 13444a0df2efSAndy Whitcroft $current_comment = ''; 13454a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 134600df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 134700df344fSAndy Whitcroft #warn " $line\n"; 13484a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 13494a0df2efSAndy Whitcroft $in_comment = 1; 13504a0df2efSAndy Whitcroft } 13514a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 13524a0df2efSAndy Whitcroft $in_comment = 1; 13534a0df2efSAndy Whitcroft } 13544a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 13554a0df2efSAndy Whitcroft $current_comment = ''; 13564a0df2efSAndy Whitcroft } 13574a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 13584a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 13594a0df2efSAndy Whitcroft $in_comment = 0; 13604a0df2efSAndy Whitcroft } 13614a0df2efSAndy Whitcroft } 13624a0df2efSAndy Whitcroft 13634a0df2efSAndy Whitcroft chomp($current_comment); 13644a0df2efSAndy Whitcroft return($current_comment); 13654a0df2efSAndy Whitcroft} 13664a0df2efSAndy Whitcroftsub ctx_has_comment { 13674a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 13684a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 13694a0df2efSAndy Whitcroft 137000df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 13714a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 13724a0df2efSAndy Whitcroft 13734a0df2efSAndy Whitcroft return ($cmt ne ''); 13744a0df2efSAndy Whitcroft} 13754a0df2efSAndy Whitcroft 13764d001e4dSAndy Whitcroftsub raw_line { 13774d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 13784d001e4dSAndy Whitcroft 13794d001e4dSAndy Whitcroft my $offset = $linenr - 1; 13804d001e4dSAndy Whitcroft $cnt++; 13814d001e4dSAndy Whitcroft 13824d001e4dSAndy Whitcroft my $line; 13834d001e4dSAndy Whitcroft while ($cnt) { 13844d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 13854d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 13864d001e4dSAndy Whitcroft $cnt--; 13874d001e4dSAndy Whitcroft } 13884d001e4dSAndy Whitcroft 13894d001e4dSAndy Whitcroft return $line; 13904d001e4dSAndy Whitcroft} 13914d001e4dSAndy Whitcroft 13920a920b5bSAndy Whitcroftsub cat_vet { 13930a920b5bSAndy Whitcroft my ($vet) = @_; 13949c0ca6f9SAndy Whitcroft my ($res, $coded); 13950a920b5bSAndy Whitcroft 13969c0ca6f9SAndy Whitcroft $res = ''; 13976c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 13986c72ffaaSAndy Whitcroft $res .= $1; 13996c72ffaaSAndy Whitcroft if ($2 ne '') { 14009c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 14016c72ffaaSAndy Whitcroft $res .= $coded; 14026c72ffaaSAndy Whitcroft } 14039c0ca6f9SAndy Whitcroft } 14049c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 14050a920b5bSAndy Whitcroft 14069c0ca6f9SAndy Whitcroft return $res; 14070a920b5bSAndy Whitcroft} 14080a920b5bSAndy Whitcroft 1409c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1410cf655043SAndy Whitcroftmy $av_pending; 1411c2fdda0dSAndy Whitcroftmy @av_paren_type; 14121f65f947SAndy Whitcroftmy $av_pend_colon; 1413c2fdda0dSAndy Whitcroft 1414c2fdda0dSAndy Whitcroftsub annotate_reset { 1415c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1416cf655043SAndy Whitcroft $av_pending = '_'; 1417cf655043SAndy Whitcroft @av_paren_type = ('E'); 14181f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1419c2fdda0dSAndy Whitcroft} 1420c2fdda0dSAndy Whitcroft 14216c72ffaaSAndy Whitcroftsub annotate_values { 14226c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 14236c72ffaaSAndy Whitcroft 14246c72ffaaSAndy Whitcroft my $res; 14251f65f947SAndy Whitcroft my $var = '_' x length($stream); 14266c72ffaaSAndy Whitcroft my $cur = $stream; 14276c72ffaaSAndy Whitcroft 1428c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 14296c72ffaaSAndy Whitcroft 14306c72ffaaSAndy Whitcroft while (length($cur)) { 1431773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1432cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1433171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 14346c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1435c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1436c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1437cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1438c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 14396c72ffaaSAndy Whitcroft } 14406c72ffaaSAndy Whitcroft 1441c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 14429446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 14439446ef56SAndy Whitcroft push(@av_paren_type, $type); 1444addcdceaSAndy Whitcroft $type = 'c'; 14459446ef56SAndy Whitcroft 1446e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1447c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 14486c72ffaaSAndy Whitcroft $type = 'T'; 14496c72ffaaSAndy Whitcroft 1450389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1451389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1452389a2fe5SAndy Whitcroft $type = 'T'; 1453389a2fe5SAndy Whitcroft 1454c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1455171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1456c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1457171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1458171ae1a4SAndy Whitcroft if ($2 ne '') { 1459cf655043SAndy Whitcroft $av_pending = 'N'; 1460171ae1a4SAndy Whitcroft } 1461171ae1a4SAndy Whitcroft $type = 'E'; 1462171ae1a4SAndy Whitcroft 1463c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1464171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1465171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1466171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 14676c72ffaaSAndy Whitcroft 1468c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1469cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1470c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1471cf655043SAndy Whitcroft 1472cf655043SAndy Whitcroft push(@av_paren_type, $type); 1473cf655043SAndy Whitcroft push(@av_paren_type, $type); 1474171ae1a4SAndy Whitcroft $type = 'E'; 1475cf655043SAndy Whitcroft 1476c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1477cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1478cf655043SAndy Whitcroft $av_preprocessor = 1; 1479cf655043SAndy Whitcroft 1480cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1481cf655043SAndy Whitcroft 1482171ae1a4SAndy Whitcroft $type = 'E'; 1483cf655043SAndy Whitcroft 1484c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1485cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1486cf655043SAndy Whitcroft 1487cf655043SAndy Whitcroft $av_preprocessor = 1; 1488cf655043SAndy Whitcroft 1489cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1490cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1491cf655043SAndy Whitcroft pop(@av_paren_type); 1492cf655043SAndy Whitcroft push(@av_paren_type, $type); 1493171ae1a4SAndy Whitcroft $type = 'E'; 14946c72ffaaSAndy Whitcroft 14956c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1496c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 14976c72ffaaSAndy Whitcroft 1498171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1499171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1500171ae1a4SAndy Whitcroft $av_pending = $type; 1501171ae1a4SAndy Whitcroft $type = 'N'; 1502171ae1a4SAndy Whitcroft 15036c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1504c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 15056c72ffaaSAndy Whitcroft if (defined $2) { 1506cf655043SAndy Whitcroft $av_pending = 'V'; 15076c72ffaaSAndy Whitcroft } 15086c72ffaaSAndy Whitcroft $type = 'N'; 15096c72ffaaSAndy Whitcroft 151014b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1511c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 151214b111c1SAndy Whitcroft $av_pending = 'E'; 15136c72ffaaSAndy Whitcroft $type = 'N'; 15146c72ffaaSAndy Whitcroft 15151f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 15161f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 15171f65f947SAndy Whitcroft $av_pend_colon = 'C'; 15181f65f947SAndy Whitcroft $type = 'N'; 15191f65f947SAndy Whitcroft 152014b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1521c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 15226c72ffaaSAndy Whitcroft $type = 'N'; 15236c72ffaaSAndy Whitcroft 15246c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1525c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1526cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1527cf655043SAndy Whitcroft $av_pending = '_'; 15286c72ffaaSAndy Whitcroft $type = 'N'; 15296c72ffaaSAndy Whitcroft 15306c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1531cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1532cf655043SAndy Whitcroft if ($new_type ne '_') { 1533cf655043SAndy Whitcroft $type = $new_type; 1534c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1535c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 15366c72ffaaSAndy Whitcroft } else { 1537c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 15386c72ffaaSAndy Whitcroft } 15396c72ffaaSAndy Whitcroft 1540c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1541c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1542c8cb2ca3SAndy Whitcroft $type = 'V'; 1543cf655043SAndy Whitcroft $av_pending = 'V'; 15446c72ffaaSAndy Whitcroft 15458e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 15468e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 15471f65f947SAndy Whitcroft $av_pend_colon = 'B'; 15488e761b04SAndy Whitcroft } elsif ($type eq 'E') { 15498e761b04SAndy Whitcroft $av_pend_colon = 'L'; 15501f65f947SAndy Whitcroft } 15511f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 15521f65f947SAndy Whitcroft $type = 'V'; 15531f65f947SAndy Whitcroft 15546c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1555c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 15566c72ffaaSAndy Whitcroft $type = 'V'; 15576c72ffaaSAndy Whitcroft 15586c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1559c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 15606c72ffaaSAndy Whitcroft $type = 'N'; 15616c72ffaaSAndy Whitcroft 1562cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1563c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 156413214adfSAndy Whitcroft $type = 'E'; 15651f65f947SAndy Whitcroft $av_pend_colon = 'O'; 156613214adfSAndy Whitcroft 15678e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 15688e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 15698e761b04SAndy Whitcroft $type = 'C'; 15708e761b04SAndy Whitcroft 15711f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 15721f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 15731f65f947SAndy Whitcroft $type = 'N'; 15741f65f947SAndy Whitcroft 15751f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 15761f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 15771f65f947SAndy Whitcroft 15781f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 15791f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 15801f65f947SAndy Whitcroft $type = 'E'; 15811f65f947SAndy Whitcroft } else { 15821f65f947SAndy Whitcroft $type = 'N'; 15831f65f947SAndy Whitcroft } 15841f65f947SAndy Whitcroft $av_pend_colon = 'O'; 15851f65f947SAndy Whitcroft 15868e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 158713214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 15886c72ffaaSAndy Whitcroft $type = 'N'; 15896c72ffaaSAndy Whitcroft 15900d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 159174048ed8SAndy Whitcroft my $variant; 159274048ed8SAndy Whitcroft 159374048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 159474048ed8SAndy Whitcroft if ($type eq 'V') { 159574048ed8SAndy Whitcroft $variant = 'B'; 159674048ed8SAndy Whitcroft } else { 159774048ed8SAndy Whitcroft $variant = 'U'; 159874048ed8SAndy Whitcroft } 159974048ed8SAndy Whitcroft 160074048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 160174048ed8SAndy Whitcroft $type = 'N'; 160274048ed8SAndy Whitcroft 16036c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1604c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 16056c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 16066c72ffaaSAndy Whitcroft $type = 'N'; 16076c72ffaaSAndy Whitcroft } 16086c72ffaaSAndy Whitcroft 16096c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1610c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 16116c72ffaaSAndy Whitcroft } 16126c72ffaaSAndy Whitcroft if (defined $1) { 16136c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 16146c72ffaaSAndy Whitcroft $res .= $type x length($1); 16156c72ffaaSAndy Whitcroft } 16166c72ffaaSAndy Whitcroft } 16176c72ffaaSAndy Whitcroft 16181f65f947SAndy Whitcroft return ($res, $var); 16196c72ffaaSAndy Whitcroft} 16206c72ffaaSAndy Whitcroft 16218905a67cSAndy Whitcroftsub possible { 162213214adfSAndy Whitcroft my ($possible, $line) = @_; 16239a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 16240776e594SAndy Whitcroft ^(?: 16250776e594SAndy Whitcroft $Modifier| 16260776e594SAndy Whitcroft $Storage| 16270776e594SAndy Whitcroft $Type| 16289a974fdbSAndy Whitcroft DEFINE_\S+ 16299a974fdbSAndy Whitcroft )$| 16309a974fdbSAndy Whitcroft ^(?: 16310776e594SAndy Whitcroft goto| 16320776e594SAndy Whitcroft return| 16330776e594SAndy Whitcroft case| 16340776e594SAndy Whitcroft else| 16350776e594SAndy Whitcroft asm|__asm__| 163689a88353SAndy Whitcroft do| 163789a88353SAndy Whitcroft \#| 163889a88353SAndy Whitcroft \#\#| 16399a974fdbSAndy Whitcroft )(?:\s|$)| 16400776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 16419a974fdbSAndy Whitcroft )}x; 16429a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 16439a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1644c45dcabdSAndy Whitcroft # Check for modifiers. 1645c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1646c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1647c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1648c45dcabdSAndy Whitcroft 1649c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1650c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1651d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 16529a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1653d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1654485ff23eSAlex Dowad push(@modifierListFile, $modifier); 1655d2506586SAndy Whitcroft } 16569a974fdbSAndy Whitcroft } 1657c45dcabdSAndy Whitcroft 1658c45dcabdSAndy Whitcroft } else { 165913214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1660485ff23eSAlex Dowad push(@typeListFile, $possible); 1661c45dcabdSAndy Whitcroft } 16628905a67cSAndy Whitcroft build_types(); 16630776e594SAndy Whitcroft } else { 16640776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 16658905a67cSAndy Whitcroft } 16668905a67cSAndy Whitcroft} 16678905a67cSAndy Whitcroft 16686c72ffaaSAndy Whitcroftmy $prefix = ''; 16696c72ffaaSAndy Whitcroft 1670000d1cc1SJoe Perchessub show_type { 1671cbec18afSJoe Perches my ($type) = @_; 167291bfe484SJoe Perches 1673cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 1674cbec18afSJoe Perches 1675cbec18afSJoe Perches return !defined $ignore_type{$type}; 1676000d1cc1SJoe Perches} 1677000d1cc1SJoe Perches 1678f0a594c1SAndy Whitcroftsub report { 1679cbec18afSJoe Perches my ($level, $type, $msg) = @_; 1680cbec18afSJoe Perches 1681cbec18afSJoe Perches if (!show_type($type) || 1682cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1683773647a0SAndy Whitcroft return 0; 1684773647a0SAndy Whitcroft } 168557230297SJoe Perches my $output = ''; 168657230297SJoe Perches if (-t STDOUT && $color) { 168757230297SJoe Perches if ($level eq 'ERROR') { 168857230297SJoe Perches $output .= RED; 168957230297SJoe Perches } elsif ($level eq 'WARNING') { 169057230297SJoe Perches $output .= YELLOW; 1691000d1cc1SJoe Perches } else { 169257230297SJoe Perches $output .= GREEN; 1693000d1cc1SJoe Perches } 169457230297SJoe Perches } 169557230297SJoe Perches $output .= $prefix . $level . ':'; 169657230297SJoe Perches if ($show_types) { 169757230297SJoe Perches $output .= BLUE if (-t STDOUT && $color); 169857230297SJoe Perches $output .= "$type:"; 169957230297SJoe Perches } 170057230297SJoe Perches $output .= RESET if (-t STDOUT && $color); 170157230297SJoe Perches $output .= ' ' . $msg . "\n"; 170234d8815fSJoe Perches 170334d8815fSJoe Perches if ($showfile) { 170434d8815fSJoe Perches my @lines = split("\n", $output, -1); 170534d8815fSJoe Perches splice(@lines, 1, 1); 170634d8815fSJoe Perches $output = join("\n", @lines); 170734d8815fSJoe Perches } 170857230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 17098905a67cSAndy Whitcroft 171057230297SJoe Perches push(our @report, $output); 1711773647a0SAndy Whitcroft 1712773647a0SAndy Whitcroft return 1; 1713f0a594c1SAndy Whitcroft} 1714cbec18afSJoe Perches 1715f0a594c1SAndy Whitcroftsub report_dump { 171613214adfSAndy Whitcroft our @report; 1717f0a594c1SAndy Whitcroft} 1718000d1cc1SJoe Perches 1719d752fcc8SJoe Perchessub fixup_current_range { 1720d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 1721d752fcc8SJoe Perches 1722d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 1723d752fcc8SJoe Perches my $o = $1; 1724d752fcc8SJoe Perches my $l = $2; 1725d752fcc8SJoe Perches my $no = $o + $offset; 1726d752fcc8SJoe Perches my $nl = $l + $length; 1727d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 1728d752fcc8SJoe Perches } 1729d752fcc8SJoe Perches} 1730d752fcc8SJoe Perches 1731d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 1732d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 1733d752fcc8SJoe Perches 1734d752fcc8SJoe Perches my $range_last_linenr = 0; 1735d752fcc8SJoe Perches my $delta_offset = 0; 1736d752fcc8SJoe Perches 1737d752fcc8SJoe Perches my $old_linenr = 0; 1738d752fcc8SJoe Perches my $new_linenr = 0; 1739d752fcc8SJoe Perches 1740d752fcc8SJoe Perches my $next_insert = 0; 1741d752fcc8SJoe Perches my $next_delete = 0; 1742d752fcc8SJoe Perches 1743d752fcc8SJoe Perches my @lines = (); 1744d752fcc8SJoe Perches 1745d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 1746d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 1747d752fcc8SJoe Perches 1748d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 1749d752fcc8SJoe Perches my $save_line = 1; 1750d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 1751323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 1752d752fcc8SJoe Perches $delta_offset = 0; 1753d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 1754d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 1755d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 1756d752fcc8SJoe Perches } 1757d752fcc8SJoe Perches 1758d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 1759d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 1760d752fcc8SJoe Perches $save_line = 0; 1761d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 1762d752fcc8SJoe Perches } 1763d752fcc8SJoe Perches 1764d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 1765d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 1766d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 1767d752fcc8SJoe Perches $new_linenr++; 1768d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 1769d752fcc8SJoe Perches } 1770d752fcc8SJoe Perches 1771d752fcc8SJoe Perches if ($save_line) { 1772d752fcc8SJoe Perches push(@lines, $line); 1773d752fcc8SJoe Perches $new_linenr++; 1774d752fcc8SJoe Perches } 1775d752fcc8SJoe Perches 1776d752fcc8SJoe Perches $old_linenr++; 1777d752fcc8SJoe Perches } 1778d752fcc8SJoe Perches 1779d752fcc8SJoe Perches return @lines; 1780d752fcc8SJoe Perches} 1781d752fcc8SJoe Perches 1782f2d7e4d4SJoe Perchessub fix_insert_line { 1783f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 1784f2d7e4d4SJoe Perches 1785f2d7e4d4SJoe Perches my $inserted = { 1786f2d7e4d4SJoe Perches LINENR => $linenr, 1787f2d7e4d4SJoe Perches LINE => $line, 1788f2d7e4d4SJoe Perches }; 1789f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 1790f2d7e4d4SJoe Perches} 1791f2d7e4d4SJoe Perches 1792f2d7e4d4SJoe Perchessub fix_delete_line { 1793f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 1794f2d7e4d4SJoe Perches 1795f2d7e4d4SJoe Perches my $deleted = { 1796f2d7e4d4SJoe Perches LINENR => $linenr, 1797f2d7e4d4SJoe Perches LINE => $line, 1798f2d7e4d4SJoe Perches }; 1799f2d7e4d4SJoe Perches 1800f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 1801f2d7e4d4SJoe Perches} 1802f2d7e4d4SJoe Perches 1803de7d4f0eSAndy Whitcroftsub ERROR { 1804cbec18afSJoe Perches my ($type, $msg) = @_; 1805cbec18afSJoe Perches 1806cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 1807de7d4f0eSAndy Whitcroft our $clean = 0; 18086c72ffaaSAndy Whitcroft our $cnt_error++; 18093705ce5bSJoe Perches return 1; 1810de7d4f0eSAndy Whitcroft } 18113705ce5bSJoe Perches return 0; 1812773647a0SAndy Whitcroft} 1813de7d4f0eSAndy Whitcroftsub WARN { 1814cbec18afSJoe Perches my ($type, $msg) = @_; 1815cbec18afSJoe Perches 1816cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 1817de7d4f0eSAndy Whitcroft our $clean = 0; 18186c72ffaaSAndy Whitcroft our $cnt_warn++; 18193705ce5bSJoe Perches return 1; 1820de7d4f0eSAndy Whitcroft } 18213705ce5bSJoe Perches return 0; 1822773647a0SAndy Whitcroft} 1823de7d4f0eSAndy Whitcroftsub CHK { 1824cbec18afSJoe Perches my ($type, $msg) = @_; 1825cbec18afSJoe Perches 1826cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 1827de7d4f0eSAndy Whitcroft our $clean = 0; 18286c72ffaaSAndy Whitcroft our $cnt_chk++; 18293705ce5bSJoe Perches return 1; 18306c72ffaaSAndy Whitcroft } 18313705ce5bSJoe Perches return 0; 1832de7d4f0eSAndy Whitcroft} 1833de7d4f0eSAndy Whitcroft 18346ecd9674SAndy Whitcroftsub check_absolute_file { 18356ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 18366ecd9674SAndy Whitcroft my $file = $absolute; 18376ecd9674SAndy Whitcroft 18386ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 18396ecd9674SAndy Whitcroft 18406ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 18416ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 18426ecd9674SAndy Whitcroft if (-f "$root/$file") { 18436ecd9674SAndy Whitcroft ##print "file<$file>\n"; 18446ecd9674SAndy Whitcroft last; 18456ecd9674SAndy Whitcroft } 18466ecd9674SAndy Whitcroft } 18476ecd9674SAndy Whitcroft if (! -f _) { 18486ecd9674SAndy Whitcroft return 0; 18496ecd9674SAndy Whitcroft } 18506ecd9674SAndy Whitcroft 18516ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 18526ecd9674SAndy Whitcroft my $prefix = $absolute; 18536ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 18546ecd9674SAndy Whitcroft 18556ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 18566ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1857000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1858000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 18596ecd9674SAndy Whitcroft } 18606ecd9674SAndy Whitcroft} 18616ecd9674SAndy Whitcroft 18623705ce5bSJoe Perchessub trim { 18633705ce5bSJoe Perches my ($string) = @_; 18643705ce5bSJoe Perches 1865b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 1866b34c648bSJoe Perches 1867b34c648bSJoe Perches return $string; 1868b34c648bSJoe Perches} 1869b34c648bSJoe Perches 1870b34c648bSJoe Perchessub ltrim { 1871b34c648bSJoe Perches my ($string) = @_; 1872b34c648bSJoe Perches 1873b34c648bSJoe Perches $string =~ s/^\s+//; 1874b34c648bSJoe Perches 1875b34c648bSJoe Perches return $string; 1876b34c648bSJoe Perches} 1877b34c648bSJoe Perches 1878b34c648bSJoe Perchessub rtrim { 1879b34c648bSJoe Perches my ($string) = @_; 1880b34c648bSJoe Perches 1881b34c648bSJoe Perches $string =~ s/\s+$//; 18823705ce5bSJoe Perches 18833705ce5bSJoe Perches return $string; 18843705ce5bSJoe Perches} 18853705ce5bSJoe Perches 188652ea8506SJoe Perchessub string_find_replace { 188752ea8506SJoe Perches my ($string, $find, $replace) = @_; 188852ea8506SJoe Perches 188952ea8506SJoe Perches $string =~ s/$find/$replace/g; 189052ea8506SJoe Perches 189152ea8506SJoe Perches return $string; 189252ea8506SJoe Perches} 189352ea8506SJoe Perches 18943705ce5bSJoe Perchessub tabify { 18953705ce5bSJoe Perches my ($leading) = @_; 18963705ce5bSJoe Perches 18973705ce5bSJoe Perches my $source_indent = 8; 18983705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 18993705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 19003705ce5bSJoe Perches 19013705ce5bSJoe Perches #convert leading spaces to tabs 19023705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 19033705ce5bSJoe Perches #Remove spaces before a tab 19043705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 19053705ce5bSJoe Perches 19063705ce5bSJoe Perches return "$leading"; 19073705ce5bSJoe Perches} 19083705ce5bSJoe Perches 1909d1fe9c09SJoe Perchessub pos_last_openparen { 1910d1fe9c09SJoe Perches my ($line) = @_; 1911d1fe9c09SJoe Perches 1912d1fe9c09SJoe Perches my $pos = 0; 1913d1fe9c09SJoe Perches 1914d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 1915d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 1916d1fe9c09SJoe Perches 1917d1fe9c09SJoe Perches my $last_openparen = 0; 1918d1fe9c09SJoe Perches 1919d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 1920d1fe9c09SJoe Perches return -1; 1921d1fe9c09SJoe Perches } 1922d1fe9c09SJoe Perches 1923d1fe9c09SJoe Perches my $len = length($line); 1924d1fe9c09SJoe Perches 1925d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 1926d1fe9c09SJoe Perches my $string = substr($line, $pos); 1927d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 1928d1fe9c09SJoe Perches $pos += length($1) - 1; 1929d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 1930d1fe9c09SJoe Perches $last_openparen = $pos; 1931d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 1932d1fe9c09SJoe Perches last; 1933d1fe9c09SJoe Perches } 1934d1fe9c09SJoe Perches } 1935d1fe9c09SJoe Perches 193691cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 1937d1fe9c09SJoe Perches} 1938d1fe9c09SJoe Perches 19390a920b5bSAndy Whitcroftsub process { 19400a920b5bSAndy Whitcroft my $filename = shift; 19410a920b5bSAndy Whitcroft 19420a920b5bSAndy Whitcroft my $linenr=0; 19430a920b5bSAndy Whitcroft my $prevline=""; 1944c2fdda0dSAndy Whitcroft my $prevrawline=""; 19450a920b5bSAndy Whitcroft my $stashline=""; 1946c2fdda0dSAndy Whitcroft my $stashrawline=""; 19470a920b5bSAndy Whitcroft 19484a0df2efSAndy Whitcroft my $length; 19490a920b5bSAndy Whitcroft my $indent; 19500a920b5bSAndy Whitcroft my $previndent=0; 19510a920b5bSAndy Whitcroft my $stashindent=0; 19520a920b5bSAndy Whitcroft 1953de7d4f0eSAndy Whitcroft our $clean = 1; 19540a920b5bSAndy Whitcroft my $signoff = 0; 19550a920b5bSAndy Whitcroft my $is_patch = 0; 19560a920b5bSAndy Whitcroft 195729ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 195815662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 19592a076f40SJoe Perches my $commit_log_long_line = 0; 1960e518e9a5SJoe Perches my $commit_log_has_diff = 0; 196113f1937eSJoe Perches my $reported_maintainer_file = 0; 1962fa64205dSPasi Savanainen my $non_utf8_charset = 0; 1963fa64205dSPasi Savanainen 1964365dd4eaSJoe Perches my $last_blank_line = 0; 19655e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 1966365dd4eaSJoe Perches 196713214adfSAndy Whitcroft our @report = (); 19686c72ffaaSAndy Whitcroft our $cnt_lines = 0; 19696c72ffaaSAndy Whitcroft our $cnt_error = 0; 19706c72ffaaSAndy Whitcroft our $cnt_warn = 0; 19716c72ffaaSAndy Whitcroft our $cnt_chk = 0; 19726c72ffaaSAndy Whitcroft 19730a920b5bSAndy Whitcroft # Trace the real file/line as we go. 19740a920b5bSAndy Whitcroft my $realfile = ''; 19750a920b5bSAndy Whitcroft my $realline = 0; 19760a920b5bSAndy Whitcroft my $realcnt = 0; 19770a920b5bSAndy Whitcroft my $here = ''; 19780a920b5bSAndy Whitcroft my $in_comment = 0; 1979c2fdda0dSAndy Whitcroft my $comment_edge = 0; 19800a920b5bSAndy Whitcroft my $first_line = 0; 19811e855726SWolfram Sang my $p1_prefix = ''; 19820a920b5bSAndy Whitcroft 198313214adfSAndy Whitcroft my $prev_values = 'E'; 198413214adfSAndy Whitcroft 198513214adfSAndy Whitcroft # suppression flags 1986773647a0SAndy Whitcroft my %suppress_ifbraces; 1987170d3a22SAndy Whitcroft my %suppress_whiletrailers; 19882b474a1aSAndy Whitcroft my %suppress_export; 19893e469cdcSAndy Whitcroft my $suppress_statement = 0; 1990653d4876SAndy Whitcroft 19917e51f197SJoe Perches my %signatures = (); 1992323c1260SJoe Perches 1993c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 1994de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 1995c2fdda0dSAndy Whitcroft # 1996de7d4f0eSAndy Whitcroft my @setup_docs = (); 1997de7d4f0eSAndy Whitcroft my $setup_docs = 0; 1998773647a0SAndy Whitcroft 1999d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2000d8b07710SJoe Perches 2001773647a0SAndy Whitcroft sanitise_line_reset(); 2002c2fdda0dSAndy Whitcroft my $line; 2003c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2004773647a0SAndy Whitcroft $linenr++; 2005773647a0SAndy Whitcroft $line = $rawline; 2006c2fdda0dSAndy Whitcroft 20073705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 20083705ce5bSJoe Perches 2009773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2010de7d4f0eSAndy Whitcroft $setup_docs = 0; 2011de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 2012de7d4f0eSAndy Whitcroft $setup_docs = 1; 2013de7d4f0eSAndy Whitcroft } 2014773647a0SAndy Whitcroft #next; 2015de7d4f0eSAndy Whitcroft } 2016773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2017773647a0SAndy Whitcroft $realline=$1-1; 2018773647a0SAndy Whitcroft if (defined $2) { 2019773647a0SAndy Whitcroft $realcnt=$3+1; 2020773647a0SAndy Whitcroft } else { 2021773647a0SAndy Whitcroft $realcnt=1+1; 2022773647a0SAndy Whitcroft } 2023c45dcabdSAndy Whitcroft $in_comment = 0; 2024773647a0SAndy Whitcroft 2025773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2026773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2027773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2028773647a0SAndy Whitcroft # at context start. 2029773647a0SAndy Whitcroft my $edge; 203001fa9147SAndy Whitcroft my $cnt = $realcnt; 203101fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 203201fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 203301fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 203401fa9147SAndy Whitcroft $cnt--; 203501fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2036721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2037fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2038fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2039fae17daeSAndy Whitcroft ($edge) = $1; 2040fae17daeSAndy Whitcroft last; 2041fae17daeSAndy Whitcroft } 2042773647a0SAndy Whitcroft } 2043773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2044773647a0SAndy Whitcroft $in_comment = 1; 2045773647a0SAndy Whitcroft } 2046773647a0SAndy Whitcroft 2047773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2048773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2049773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2050773647a0SAndy Whitcroft if (!defined $edge && 205183242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2052773647a0SAndy Whitcroft { 2053773647a0SAndy Whitcroft $in_comment = 1; 2054773647a0SAndy Whitcroft } 2055773647a0SAndy Whitcroft 2056773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2057773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2058773647a0SAndy Whitcroft 2059171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2060773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2061171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2062773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2063773647a0SAndy Whitcroft } 2064773647a0SAndy Whitcroft push(@lines, $line); 2065773647a0SAndy Whitcroft 2066773647a0SAndy Whitcroft if ($realcnt > 1) { 2067773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2068773647a0SAndy Whitcroft } else { 2069773647a0SAndy Whitcroft $realcnt = 0; 2070773647a0SAndy Whitcroft } 2071773647a0SAndy Whitcroft 2072773647a0SAndy Whitcroft #print "==>$rawline\n"; 2073773647a0SAndy Whitcroft #print "-->$line\n"; 2074de7d4f0eSAndy Whitcroft 2075de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2076de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2077de7d4f0eSAndy Whitcroft } 2078de7d4f0eSAndy Whitcroft } 2079de7d4f0eSAndy Whitcroft 20806c72ffaaSAndy Whitcroft $prefix = ''; 20816c72ffaaSAndy Whitcroft 2082773647a0SAndy Whitcroft $realcnt = 0; 2083773647a0SAndy Whitcroft $linenr = 0; 2084194f66fcSJoe Perches $fixlinenr = -1; 20850a920b5bSAndy Whitcroft foreach my $line (@lines) { 20860a920b5bSAndy Whitcroft $linenr++; 2087194f66fcSJoe Perches $fixlinenr++; 20881b5539b1SJoe Perches my $sline = $line; #copy of $line 20891b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 20900a920b5bSAndy Whitcroft 2091c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 20926c72ffaaSAndy Whitcroft 20930a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2094e518e9a5SJoe Perches if (!$in_commit_log && 2095e518e9a5SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 20960a920b5bSAndy Whitcroft $is_patch = 1; 20974a0df2efSAndy Whitcroft $first_line = $linenr + 1; 20980a920b5bSAndy Whitcroft $realline=$1-1; 20990a920b5bSAndy Whitcroft if (defined $2) { 21000a920b5bSAndy Whitcroft $realcnt=$3+1; 21010a920b5bSAndy Whitcroft } else { 21020a920b5bSAndy Whitcroft $realcnt=1+1; 21030a920b5bSAndy Whitcroft } 2104c2fdda0dSAndy Whitcroft annotate_reset(); 210513214adfSAndy Whitcroft $prev_values = 'E'; 210613214adfSAndy Whitcroft 2107773647a0SAndy Whitcroft %suppress_ifbraces = (); 2108170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 21092b474a1aSAndy Whitcroft %suppress_export = (); 21103e469cdcSAndy Whitcroft $suppress_statement = 0; 21110a920b5bSAndy Whitcroft next; 21120a920b5bSAndy Whitcroft 21134a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 21144a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 21154a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2116773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 21170a920b5bSAndy Whitcroft $realline++; 2118d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 21190a920b5bSAndy Whitcroft 21204a0df2efSAndy Whitcroft # Measure the line length and indent. 2121c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 21220a920b5bSAndy Whitcroft 21230a920b5bSAndy Whitcroft # Track the previous line. 21240a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 21250a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2126c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2127c2fdda0dSAndy Whitcroft 2128773647a0SAndy Whitcroft #warn "line<$line>\n"; 21296c72ffaaSAndy Whitcroft 2130d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2131d8aaf121SAndy Whitcroft $realcnt--; 21320a920b5bSAndy Whitcroft } 21330a920b5bSAndy Whitcroft 2134cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2135cc77cdcaSAndy Whitcroft 21366c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 21376c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2138773647a0SAndy Whitcroft 21392ac73b4fSJoe Perches my $found_file = 0; 2140773647a0SAndy Whitcroft # extract the filename as it passes 21413bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 21423bf9a009SRabin Vincent $realfile = $1; 21432b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2144270c49a0SJoe Perches $in_commit_log = 0; 21452ac73b4fSJoe Perches $found_file = 1; 21463bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2147773647a0SAndy Whitcroft $realfile = $1; 21482b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2149270c49a0SJoe Perches $in_commit_log = 0; 21501e855726SWolfram Sang 21511e855726SWolfram Sang $p1_prefix = $1; 2152e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2153e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2154000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2155000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 21561e855726SWolfram Sang } 2157773647a0SAndy Whitcroft 2158c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2159000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2160000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2161773647a0SAndy Whitcroft } 21622ac73b4fSJoe Perches $found_file = 1; 21632ac73b4fSJoe Perches } 21642ac73b4fSJoe Perches 216534d8815fSJoe Perches#make up the handle for any error we report on this line 216634d8815fSJoe Perches if ($showfile) { 216734d8815fSJoe Perches $prefix = "$realfile:$realline: " 216834d8815fSJoe Perches } elsif ($emacs) { 21697d3a9f67SJoe Perches if ($file) { 21707d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 21717d3a9f67SJoe Perches } else { 217234d8815fSJoe Perches $prefix = "$filename:$linenr: "; 217334d8815fSJoe Perches } 21747d3a9f67SJoe Perches } 217534d8815fSJoe Perches 21762ac73b4fSJoe Perches if ($found_file) { 2177*7bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 21782ac73b4fSJoe Perches $check = 1; 21792ac73b4fSJoe Perches } else { 21802ac73b4fSJoe Perches $check = $check_orig; 21812ac73b4fSJoe Perches } 2182773647a0SAndy Whitcroft next; 2183773647a0SAndy Whitcroft } 2184773647a0SAndy Whitcroft 2185389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 21860a920b5bSAndy Whitcroft 2187c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2188c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2189c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 21900a920b5bSAndy Whitcroft 21916c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 21926c72ffaaSAndy Whitcroft 2193e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2194e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2195e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2196e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2197e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2198e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2199e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2200e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2201e518e9a5SJoe Perches $commit_log_has_diff = 1; 2202e518e9a5SJoe Perches } 2203e518e9a5SJoe Perches 22043bf9a009SRabin Vincent# Check for incorrect file permissions 22053bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 22063bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 220704db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 220804db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2209000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2210000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 22113bf9a009SRabin Vincent } 22123bf9a009SRabin Vincent } 22133bf9a009SRabin Vincent 221420112475SJoe Perches# Check the patch for a signoff: 2215d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 22164a0df2efSAndy Whitcroft $signoff++; 221715662b3eSJoe Perches $in_commit_log = 0; 22180a920b5bSAndy Whitcroft } 221920112475SJoe Perches 2220e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2221e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2222e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2223e0d975b1SJoe Perches $reported_maintainer_file = 1; 2224e0d975b1SJoe Perches } 2225e0d975b1SJoe Perches 222620112475SJoe Perches# Check signature styles 2227270c49a0SJoe Perches if (!$in_header_lines && 2228ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 222920112475SJoe Perches my $space_before = $1; 223020112475SJoe Perches my $sign_off = $2; 223120112475SJoe Perches my $space_after = $3; 223220112475SJoe Perches my $email = $4; 223320112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 223420112475SJoe Perches 2235ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2236ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2237ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2238ce0338dfSJoe Perches } 223920112475SJoe Perches if (defined $space_before && $space_before ne "") { 22403705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 22413705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 22423705ce5bSJoe Perches $fix) { 2243194f66fcSJoe Perches $fixed[$fixlinenr] = 22443705ce5bSJoe Perches "$ucfirst_sign_off $email"; 22453705ce5bSJoe Perches } 224620112475SJoe Perches } 224720112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 22483705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 22493705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 22503705ce5bSJoe Perches $fix) { 2251194f66fcSJoe Perches $fixed[$fixlinenr] = 22523705ce5bSJoe Perches "$ucfirst_sign_off $email"; 22533705ce5bSJoe Perches } 22543705ce5bSJoe Perches 225520112475SJoe Perches } 225620112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 22573705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 22583705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 22593705ce5bSJoe Perches $fix) { 2260194f66fcSJoe Perches $fixed[$fixlinenr] = 22613705ce5bSJoe Perches "$ucfirst_sign_off $email"; 22623705ce5bSJoe Perches } 226320112475SJoe Perches } 226420112475SJoe Perches 226520112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 226620112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 226720112475SJoe Perches if ($suggested_email eq "") { 2268000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2269000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 227020112475SJoe Perches } else { 227120112475SJoe Perches my $dequoted = $suggested_email; 227220112475SJoe Perches $dequoted =~ s/^"//; 227320112475SJoe Perches $dequoted =~ s/" </ </; 227420112475SJoe Perches # Don't force email to have quotes 227520112475SJoe Perches # Allow just an angle bracketed address 227620112475SJoe Perches if ("$dequoted$comment" ne $email && 227720112475SJoe Perches "<$email_address>$comment" ne $email && 227820112475SJoe Perches "$suggested_email$comment" ne $email) { 2279000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2280000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 228120112475SJoe Perches } 22820a920b5bSAndy Whitcroft } 22837e51f197SJoe Perches 22847e51f197SJoe Perches# Check for duplicate signatures 22857e51f197SJoe Perches my $sig_nospace = $line; 22867e51f197SJoe Perches $sig_nospace =~ s/\s//g; 22877e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 22887e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 22897e51f197SJoe Perches WARN("BAD_SIGN_OFF", 22907e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 22917e51f197SJoe Perches } else { 22927e51f197SJoe Perches $signatures{$sig_nospace} = 1; 22937e51f197SJoe Perches } 22940a920b5bSAndy Whitcroft } 22950a920b5bSAndy Whitcroft 2296a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2297a2fe16b9SJoe Perches if ($in_header_lines && 2298a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2299a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2300a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2301a2fe16b9SJoe Perches } 2302a2fe16b9SJoe Perches 23039b3189ebSJoe Perches# Check for old stable address 23049b3189ebSJoe Perches if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) { 23059b3189ebSJoe Perches ERROR("STABLE_ADDRESS", 23069b3189ebSJoe Perches "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr); 23079b3189ebSJoe Perches } 23089b3189ebSJoe Perches 23097ebd05efSChristopher Covington# Check for unwanted Gerrit info 23107ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 23117ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 23127ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 23137ebd05efSChristopher Covington } 23147ebd05efSChristopher Covington 23152a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 23162a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 23172a076f40SJoe Perches length($line) > 75) { 23182a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 23192a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 23202a076f40SJoe Perches $commit_log_long_line = 1; 23212a076f40SJoe Perches } 23222a076f40SJoe Perches 23230d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2324fe043ea1SJoe Perches if ($in_commit_log && 2325fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2326fe043ea1SJoe Perches $line =~ /\b[0-9a-f]{12,40}\b/i)) { 2327fe043ea1SJoe Perches my $init_char = "c"; 2328fe043ea1SJoe Perches my $orig_commit = ""; 23290d7835fcSJoe Perches my $short = 1; 23300d7835fcSJoe Perches my $long = 0; 23310d7835fcSJoe Perches my $case = 1; 23320d7835fcSJoe Perches my $space = 1; 23330d7835fcSJoe Perches my $hasdesc = 0; 233419c146a6SJoe Perches my $hasparens = 0; 23350d7835fcSJoe Perches my $id = '0123456789ab'; 23360d7835fcSJoe Perches my $orig_desc = "commit description"; 23370d7835fcSJoe Perches my $description = ""; 23380d7835fcSJoe Perches 2339fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2340fe043ea1SJoe Perches $init_char = $1; 2341fe043ea1SJoe Perches $orig_commit = lc($2); 2342fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2343fe043ea1SJoe Perches $orig_commit = lc($1); 2344fe043ea1SJoe Perches } 2345fe043ea1SJoe Perches 23460d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 23470d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 23480d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 23490d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 23500d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 23510d7835fcSJoe Perches $orig_desc = $1; 235219c146a6SJoe Perches $hasparens = 1; 23530d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 23540d7835fcSJoe Perches defined $rawlines[$linenr] && 23550d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 23560d7835fcSJoe Perches $orig_desc = $1; 235719c146a6SJoe Perches $hasparens = 1; 2358b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2359b671fde0SJoe Perches defined $rawlines[$linenr] && 2360b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2361b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2362b671fde0SJoe Perches $orig_desc = $1; 2363b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2364b671fde0SJoe Perches $orig_desc .= " " . $1; 236519c146a6SJoe Perches $hasparens = 1; 23660d7835fcSJoe Perches } 23670d7835fcSJoe Perches 23680d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 23690d7835fcSJoe Perches $id, $orig_desc); 23700d7835fcSJoe Perches 237119c146a6SJoe Perches if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) { 2372d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 23730d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 23740d7835fcSJoe Perches } 2375d311cd44SJoe Perches } 2376d311cd44SJoe Perches 237713f1937eSJoe Perches# Check for added, moved or deleted files 237813f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 237913f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 238013f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 238113f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 238213f1937eSJoe Perches (defined($1) || defined($2))))) { 238313f1937eSJoe Perches $reported_maintainer_file = 1; 238413f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 238513f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 238613f1937eSJoe Perches } 238713f1937eSJoe Perches 238800df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 23898905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2390000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2391000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 23926c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2393de7d4f0eSAndy Whitcroft } 2394de7d4f0eSAndy Whitcroft 23956ecd9674SAndy Whitcroft# Check for absolute kernel paths. 23966ecd9674SAndy Whitcroft if ($tree) { 23976ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 23986ecd9674SAndy Whitcroft my $file = $1; 23996ecd9674SAndy Whitcroft 24006ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 24016ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 24026ecd9674SAndy Whitcroft # 24036ecd9674SAndy Whitcroft } else { 24046ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 24056ecd9674SAndy Whitcroft } 24066ecd9674SAndy Whitcroft } 24076ecd9674SAndy Whitcroft } 24086ecd9674SAndy Whitcroft 2409de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2410de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2411171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2412171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2413171ae1a4SAndy Whitcroft 2414171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2415171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2416171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2417171ae1a4SAndy Whitcroft 241834d99219SJoe Perches CHK("INVALID_UTF8", 2419000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 242000df344fSAndy Whitcroft } 24210a920b5bSAndy Whitcroft 242215662b3eSJoe Perches# Check if it's the start of a commit log 242315662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 242415662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 242529ee1b0cSJoe Perches !($rawline =~ /^\s+\S/ || 242629ee1b0cSJoe Perches $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { 242715662b3eSJoe Perches $in_header_lines = 0; 242815662b3eSJoe Perches $in_commit_log = 1; 242915662b3eSJoe Perches } 243015662b3eSJoe Perches 2431fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2432fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2433fa64205dSPasi Savanainen if ($in_header_lines && 2434fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2435fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2436fa64205dSPasi Savanainen $non_utf8_charset = 1; 2437fa64205dSPasi Savanainen } 2438fa64205dSPasi Savanainen 2439fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 244015662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2441fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 244215662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 244315662b3eSJoe Perches } 244415662b3eSJoe Perches 244566b47b4aSKees Cook# Check for various typo / spelling mistakes 244666d7a382SJoe Perches if (defined($misspellings) && 244766d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2448ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 244966b47b4aSKees Cook my $typo = $1; 245066b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 245166b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 245266b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 245366b47b4aSKees Cook my $msg_type = \&WARN; 245466b47b4aSKees Cook $msg_type = \&CHK if ($file); 245566b47b4aSKees Cook if (&{$msg_type}("TYPO_SPELLING", 245666b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 245766b47b4aSKees Cook $fix) { 245866b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 245966b47b4aSKees Cook } 246066b47b4aSKees Cook } 246166b47b4aSKees Cook } 246266b47b4aSKees Cook 246330670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 246430670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 246500df344fSAndy Whitcroft 24660a920b5bSAndy Whitcroft#trailing whitespace 24679c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2468c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2469d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2470d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2471d5e616fcSJoe Perches $fix) { 2472194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2473d5e616fcSJoe Perches } 2474c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2475c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 24763705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 24773705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 24783705ce5bSJoe Perches $fix) { 2479194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 24803705ce5bSJoe Perches } 24813705ce5bSJoe Perches 2482d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 24830a920b5bSAndy Whitcroft } 24845368df20SAndy Whitcroft 24854783f894SJosh Triplett# Check for FSF mailing addresses. 2486109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 24873e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 24883e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 24894783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 24904783f894SJosh Triplett my $msg_type = \&ERROR; 24914783f894SJosh Triplett $msg_type = \&CHK if ($file); 24924783f894SJosh Triplett &{$msg_type}("FSF_MAILING_ADDRESS", 24934783f894SJosh 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) 24944783f894SJosh Triplett } 24954783f894SJosh Triplett 24963354957aSAndi Kleen# check for Kconfig help text having a real description 24979fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 24989fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 24993354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 25008d73e0e7SJoe Perches $line =~ /^\+\s*config\s+/) { 25013354957aSAndi Kleen my $length = 0; 25029fe287d7SAndy Whitcroft my $cnt = $realcnt; 25039fe287d7SAndy Whitcroft my $ln = $linenr + 1; 25049fe287d7SAndy Whitcroft my $f; 2505a1385803SAndy Whitcroft my $is_start = 0; 25069fe287d7SAndy Whitcroft my $is_end = 0; 2507a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 25089fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 25099fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 25109fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 25119fe287d7SAndy Whitcroft 25129fe287d7SAndy Whitcroft next if ($f =~ /^-/); 25138d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 2514a1385803SAndy Whitcroft 25158d73e0e7SJoe Perches if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) { 2516a1385803SAndy Whitcroft $is_start = 1; 25178d73e0e7SJoe Perches } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { 2518a1385803SAndy Whitcroft $length = -1; 2519a1385803SAndy Whitcroft } 2520a1385803SAndy Whitcroft 25219fe287d7SAndy Whitcroft $f =~ s/^.//; 25223354957aSAndi Kleen $f =~ s/#.*//; 25233354957aSAndi Kleen $f =~ s/^\s+//; 25243354957aSAndi Kleen next if ($f =~ /^$/); 25259fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 25269fe287d7SAndy Whitcroft $is_end = 1; 25279fe287d7SAndy Whitcroft last; 25289fe287d7SAndy Whitcroft } 25293354957aSAndi Kleen $length++; 25303354957aSAndi Kleen } 253156193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 2532000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 253356193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 253456193274SVadim Bendebury } 2535a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 25363354957aSAndi Kleen } 25373354957aSAndi Kleen 25381ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 25391ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 25401ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 25411ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 25421ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 25431ba8dfd1SKees Cook } 25441ba8dfd1SKees Cook 2545327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 2546327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 2547327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 2548327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 2549327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 2550327953e9SChristoph Jaeger } 2551327953e9SChristoph Jaeger 2552c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2553c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2554c68e5878SArnaud Lacombe my $flag = $1; 2555c68e5878SArnaud Lacombe my $replacement = { 2556c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2557c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2558c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2559c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2560c68e5878SArnaud Lacombe }; 2561c68e5878SArnaud Lacombe 2562c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2563c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2564c68e5878SArnaud Lacombe } 2565c68e5878SArnaud Lacombe 2566bff5da43SRob Herring# check for DT compatible documentation 25677dd05b38SFlorian Vaussard if (defined $root && 25687dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 25697dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 25707dd05b38SFlorian Vaussard 2571bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2572bff5da43SRob Herring 2573cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2574cc93319bSFlorian Vaussard my $vp_file = $dt_path . "vendor-prefixes.txt"; 2575cc93319bSFlorian Vaussard 2576bff5da43SRob Herring foreach my $compat (@compats) { 2577bff5da43SRob Herring my $compat2 = $compat; 2578185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 2579185d566bSRob Herring my $compat3 = $compat; 2580185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 2581185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 2582bff5da43SRob Herring if ( $? >> 8 ) { 2583bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2584bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2585bff5da43SRob Herring } 2586bff5da43SRob Herring 25874fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 25884fbf32a6SFlorian Vaussard my $vendor = $1; 2589cc93319bSFlorian Vaussard `grep -Eq "^$vendor\\b" $vp_file`; 2590bff5da43SRob Herring if ( $? >> 8 ) { 2591bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2592cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 2593bff5da43SRob Herring } 2594bff5da43SRob Herring } 2595bff5da43SRob Herring } 2596bff5da43SRob Herring 25975368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 2598de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/); 25995368df20SAndy Whitcroft 260047e0c88bSJoe Perches# line length limit (with some exclusions) 260147e0c88bSJoe Perches# 260247e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 260347e0c88bSJoe Perches# logging functions like pr_info that end in a string 260447e0c88bSJoe Perches# lines with a single string 260547e0c88bSJoe Perches# #defines that are a single string 260647e0c88bSJoe Perches# 260747e0c88bSJoe Perches# There are 3 different line length message types: 260847e0c88bSJoe Perches# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength 260947e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 261047e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 261147e0c88bSJoe Perches# 261247e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 261347e0c88bSJoe Perches# 261447e0c88bSJoe Perches 2615b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 261647e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 261747e0c88bSJoe Perches 261847e0c88bSJoe Perches # Check the allowed long line types first 261947e0c88bSJoe Perches 262047e0c88bSJoe Perches # logging functions that end in a string that starts 262147e0c88bSJoe Perches # before $max_line_length 262247e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 262347e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 262447e0c88bSJoe Perches $msg_type = ""; 262547e0c88bSJoe Perches 262647e0c88bSJoe Perches # lines with only strings (w/ possible termination) 262747e0c88bSJoe Perches # #defines with only strings 262847e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 262947e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 263047e0c88bSJoe Perches $msg_type = ""; 263147e0c88bSJoe Perches 263247e0c88bSJoe Perches # Otherwise set the alternate message types 263347e0c88bSJoe Perches 263447e0c88bSJoe Perches # a comment starts before $max_line_length 263547e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 263647e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 263747e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 263847e0c88bSJoe Perches 263947e0c88bSJoe Perches # a quoted string starts before $max_line_length 264047e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 264147e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 264247e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 264347e0c88bSJoe Perches } 264447e0c88bSJoe Perches 264547e0c88bSJoe Perches if ($msg_type ne "" && 264647e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 264747e0c88bSJoe Perches WARN($msg_type, 26486cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 26490a920b5bSAndy Whitcroft } 265047e0c88bSJoe Perches } 26510a920b5bSAndy Whitcroft 26528905a67cSAndy Whitcroft# check for adding lines without a newline. 26538905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2654000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 2655000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 26568905a67cSAndy Whitcroft } 26578905a67cSAndy Whitcroft 265842e41c54SMike Frysinger# Blackfin: use hi/lo macros 265942e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 266042e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 266142e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2662000d1cc1SJoe Perches ERROR("LO_MACRO", 2663000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 266442e41c54SMike Frysinger } 266542e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 266642e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2667000d1cc1SJoe Perches ERROR("HI_MACRO", 2668000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 266942e41c54SMike Frysinger } 267042e41c54SMike Frysinger } 267142e41c54SMike Frysinger 2672b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 2673de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 26740a920b5bSAndy Whitcroft 26750a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 26760a920b5bSAndy Whitcroft# more than 8 must use tabs. 2677c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 2678c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 2679c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2680d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 26813705ce5bSJoe Perches if (ERROR("CODE_INDENT", 26823705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 26833705ce5bSJoe Perches $fix) { 2684194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 26853705ce5bSJoe Perches } 26860a920b5bSAndy Whitcroft } 26870a920b5bSAndy Whitcroft 268808e44365SAlberto Panizzo# check for space before tabs. 268908e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 269008e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 26913705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 26923705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 26933705ce5bSJoe Perches $fix) { 2694194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 2695d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 2696194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 2697c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 26983705ce5bSJoe Perches } 269908e44365SAlberto Panizzo } 270008e44365SAlberto Panizzo 2701d1fe9c09SJoe Perches# check for && or || at the start of a line 2702d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 2703d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 2704d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 2705d1fe9c09SJoe Perches } 2706d1fe9c09SJoe Perches 2707d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 2708d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 270991cb5195SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 2710d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 2711d1fe9c09SJoe Perches my $oldindent = $1; 2712d1fe9c09SJoe Perches my $rest = $2; 2713d1fe9c09SJoe Perches 2714d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 2715d1fe9c09SJoe Perches if ($pos >= 0) { 2716b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 2717b34a26f3SJoe Perches my $newindent = $2; 2718d1fe9c09SJoe Perches 2719d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 2720d1fe9c09SJoe Perches "\t" x ($pos / 8) . 2721d1fe9c09SJoe Perches " " x ($pos % 8); 2722d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 2723d1fe9c09SJoe Perches 2724d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 2725d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 27263705ce5bSJoe Perches 27273705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 27283705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 27293705ce5bSJoe Perches $fix && $line =~ /^\+/) { 2730194f66fcSJoe Perches $fixed[$fixlinenr] =~ 27313705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 27323705ce5bSJoe Perches } 2733d1fe9c09SJoe Perches } 2734d1fe9c09SJoe Perches } 2735d1fe9c09SJoe Perches } 2736d1fe9c09SJoe Perches 27376ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 27386ab3a970SJoe Perches# avoid checking a few false positives: 27396ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 27406ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 27416ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 27426ab3a970SJoe Perches# multiline macros that define functions 27436ab3a970SJoe Perches# known attributes or the __attribute__ keyword 27446ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 27456ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 27463705ce5bSJoe Perches if (CHK("SPACING", 2747f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 27483705ce5bSJoe Perches $fix) { 2749194f66fcSJoe Perches $fixed[$fixlinenr] =~ 2750f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 27513705ce5bSJoe Perches } 2752aad4f614SJoe Perches } 2753aad4f614SJoe Perches 275486406b1cSJoe Perches# Block comment styles 275586406b1cSJoe Perches# Networking with an initial /* 275605880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2757fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 275885ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 275985ad978cSJoe Perches $realline > 2) { 276005880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 276105880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 276205880600SJoe Perches } 276305880600SJoe Perches 276486406b1cSJoe Perches# Block comments use * on subsequent lines 276586406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 276686406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 2767a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 276861135e96SJoe Perches $rawline =~ /^\+/ && #line is new 2769a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 277086406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 277186406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 2772a605e32eSJoe Perches } 2773a605e32eSJoe Perches 277486406b1cSJoe Perches# Block comments use */ on trailing lines 277586406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2776c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2777c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2778c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 277986406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 278086406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 278105880600SJoe Perches } 278205880600SJoe Perches 27837f619191SJoe Perches# check for missing blank lines after struct/union declarations 27847f619191SJoe Perches# with exceptions for various attributes and macros 27857f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 27867f619191SJoe Perches $line =~ /^\+/ && 27877f619191SJoe Perches !($line =~ /^\+\s*$/ || 27887f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 27897f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 27907f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 27917f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 27927f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 27937f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 27947f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 2795d752fcc8SJoe Perches if (CHK("LINE_SPACING", 2796d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 2797d752fcc8SJoe Perches $fix) { 2798f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 2799d752fcc8SJoe Perches } 28007f619191SJoe Perches } 28017f619191SJoe Perches 2802365dd4eaSJoe Perches# check for multiple consecutive blank lines 2803365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 2804365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 2805365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 2806d752fcc8SJoe Perches if (CHK("LINE_SPACING", 2807d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 2808d752fcc8SJoe Perches $fix) { 2809f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 2810d752fcc8SJoe Perches } 2811d752fcc8SJoe Perches 2812365dd4eaSJoe Perches $last_blank_line = $linenr; 2813365dd4eaSJoe Perches } 2814365dd4eaSJoe Perches 28153b617e3bSJoe Perches# check for missing blank lines after declarations 28163f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 28173f7bac03SJoe Perches # actual declarations 28183f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 28195a4e1fd3SJoe Perches # function pointer declarations 28205a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 28213f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 28223f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 28233f7bac03SJoe Perches # known declaration macros 28243f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 28253f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 28263f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 28273f7bac03SJoe Perches # other possible extensions of declaration lines 28283f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 28293f7bac03SJoe Perches # not starting a section or a macro "\" extended line 28303f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 28313f7bac03SJoe Perches # looks like a declaration 28323f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 28335a4e1fd3SJoe Perches # function pointer declarations 28345a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 28353f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 28363f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 28373f7bac03SJoe Perches # known declaration macros 28383f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 28393f7bac03SJoe Perches # start of struct or union or enum 28403b617e3bSJoe Perches $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || 28413f7bac03SJoe Perches # start or end of block or continuation of declaration 28423f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 28433f7bac03SJoe Perches # bitfield continuation 28443f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 28453f7bac03SJoe Perches # other possible extensions of declaration lines 28463f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 28473f7bac03SJoe Perches # indentation of previous and current line are the same 28483f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 2849d752fcc8SJoe Perches if (WARN("LINE_SPACING", 2850d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 2851d752fcc8SJoe Perches $fix) { 2852f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 2853d752fcc8SJoe Perches } 28543b617e3bSJoe Perches } 28553b617e3bSJoe Perches 28565f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 28576b4c5bebSAndy Whitcroft# Exceptions: 28586b4c5bebSAndy Whitcroft# 1) within comments 28596b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 28606b4c5bebSAndy Whitcroft# 3) hanging labels 28613705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 28625f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28633705ce5bSJoe Perches if (WARN("LEADING_SPACE", 28643705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 28653705ce5bSJoe Perches $fix) { 2866194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 28673705ce5bSJoe Perches } 28685f7ddae6SRaffaele Recalcati } 28695f7ddae6SRaffaele Recalcati 2870b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 2871b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 2872b9ea10d6SAndy Whitcroft 2873032a4c0fSJoe Perches# check indentation of any line with a bare else 2874840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 2875032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 2876032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 2877032a4c0fSJoe Perches my $tabs = length($1) + 1; 2878840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 2879840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 2880840080a0SJoe Perches defined $lines[$linenr] && 2881840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 2882032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 2883032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 2884032a4c0fSJoe Perches } 2885032a4c0fSJoe Perches } 2886032a4c0fSJoe Perches 2887c00df19aSJoe Perches# check indentation of a line with a break; 2888c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 2889c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 2890c00df19aSJoe Perches my $tabs = $1; 2891c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 2892c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 2893c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 2894c00df19aSJoe Perches } 2895c00df19aSJoe Perches } 2896c00df19aSJoe Perches 28971ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 28981ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 28991ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 29001ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 29011ba8dfd1SKees Cook } 29021ba8dfd1SKees Cook 2903c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 2904cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 2905000d1cc1SJoe Perches WARN("CVS_KEYWORD", 2906000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 2907c2fdda0dSAndy Whitcroft } 290822f2a2efSAndy Whitcroft 290942e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 291042e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 291142e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2912000d1cc1SJoe Perches ERROR("CSYNC", 2913000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 291442e41c54SMike Frysinger } 291542e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 291642e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2917000d1cc1SJoe Perches ERROR("SSYNC", 2918000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 291942e41c54SMike Frysinger } 292042e41c54SMike Frysinger 292156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 292256e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 292356e77d70SJoe Perches WARN("HOTPLUG_SECTION", 292456e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 292556e77d70SJoe Perches } 292656e77d70SJoe Perches 29279c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 29282b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 29292b474a1aSAndy Whitcroft $realline_next); 29303e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 29313e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 29321b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 2933170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 2934f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 2935171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 2936171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 2937171ae1a4SAndy Whitcroft 29383e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 29393e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 29403e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 29413e469cdcSAndy Whitcroft # until we hit end of it. 29423e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 29433e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 29443e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 29453e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 29463e469cdcSAndy Whitcroft } 2947f74bd194SAndy Whitcroft 29482b474a1aSAndy Whitcroft # Find the real next line. 29492b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 29502b474a1aSAndy Whitcroft if (defined $realline_next && 29512b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 29522b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 29532b474a1aSAndy Whitcroft $realline_next++; 29542b474a1aSAndy Whitcroft } 29552b474a1aSAndy Whitcroft 2956171ae1a4SAndy Whitcroft my $s = $stat; 2957171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 2958cf655043SAndy Whitcroft 2959c2fdda0dSAndy Whitcroft # Ignore goto labels. 2960171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 2961c2fdda0dSAndy Whitcroft 2962c2fdda0dSAndy Whitcroft # Ignore functions being called 2963171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 2964c2fdda0dSAndy Whitcroft 2965463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 2966463f2864SAndy Whitcroft 2967c45dcabdSAndy Whitcroft # declarations always start with types 2968d2506586SAndy 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) { 2969c45dcabdSAndy Whitcroft my $type = $1; 2970c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 2971c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 2972c45dcabdSAndy Whitcroft 29736c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 2974a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 2975c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 2976c2fdda0dSAndy Whitcroft } 29778905a67cSAndy Whitcroft 29786c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 297965863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 2980c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 29819c0ca6f9SAndy Whitcroft } 29828905a67cSAndy Whitcroft 29838905a67cSAndy Whitcroft # Check for any sort of function declaration. 29848905a67cSAndy Whitcroft # int foo(something bar, other baz); 29858905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 2986171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 29878905a67cSAndy Whitcroft my ($name_len) = length($1); 29888905a67cSAndy Whitcroft 2989cf655043SAndy Whitcroft my $ctx = $s; 2990773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 29918905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 2992cf655043SAndy Whitcroft 29938905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 2994c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 29958905a67cSAndy Whitcroft 2996c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 29978905a67cSAndy Whitcroft } 29988905a67cSAndy Whitcroft } 29998905a67cSAndy Whitcroft } 30008905a67cSAndy Whitcroft 30019c0ca6f9SAndy Whitcroft } 30029c0ca6f9SAndy Whitcroft 300300df344fSAndy Whitcroft# 300400df344fSAndy Whitcroft# Checks which may be anchored in the context. 300500df344fSAndy Whitcroft# 300600df344fSAndy Whitcroft 300700df344fSAndy Whitcroft# Check for switch () and associated case and default 300800df344fSAndy Whitcroft# statements should be at the same indent. 300900df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 301000df344fSAndy Whitcroft my $err = ''; 301100df344fSAndy Whitcroft my $sep = ''; 301200df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 301300df344fSAndy Whitcroft shift(@ctx); 301400df344fSAndy Whitcroft for my $ctx (@ctx) { 301500df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 301600df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 301700df344fSAndy Whitcroft $indent != $cindent) { 301800df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 301900df344fSAndy Whitcroft $sep = ''; 302000df344fSAndy Whitcroft } else { 302100df344fSAndy Whitcroft $sep = "[...]\n"; 302200df344fSAndy Whitcroft } 302300df344fSAndy Whitcroft } 302400df344fSAndy Whitcroft if ($err ne '') { 3025000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3026000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3027de7d4f0eSAndy Whitcroft } 3028de7d4f0eSAndy Whitcroft } 3029de7d4f0eSAndy Whitcroft 3030de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3031de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 30320fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3033773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3034773647a0SAndy Whitcroft 30359c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 30368eef05ddSJoe Perches 30378eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 30388eef05ddSJoe Perches WARN("DEEP_INDENTATION", 30398eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 30408eef05ddSJoe Perches } 30418eef05ddSJoe Perches 3042de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3043de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3044de7d4f0eSAndy Whitcroft 3045548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3046548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3047de7d4f0eSAndy Whitcroft 3048548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3049548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3050548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3051548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3052548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3053773647a0SAndy Whitcroft $ctx_ln++; 3054773647a0SAndy Whitcroft } 3055548596d5SAndy Whitcroft 305653210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 305753210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3058773647a0SAndy Whitcroft 3059773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3060000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3061000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 306201464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 306300df344fSAndy Whitcroft } 3064773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3065773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3066773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3067773647a0SAndy Whitcroft { 30689c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 30699c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3070000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3071000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 307201464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 30739c0ca6f9SAndy Whitcroft } 30749c0ca6f9SAndy Whitcroft } 307500df344fSAndy Whitcroft } 307600df344fSAndy Whitcroft 30774d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 30780fe3dc2bSJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 30793e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 30803e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 30813e469cdcSAndy Whitcroft if (!defined $stat); 30824d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 30834d001e4dSAndy Whitcroft 30844d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 30854d001e4dSAndy Whitcroft 30869f5af480SJoe Perches # remove inline comments 30879f5af480SJoe Perches $s =~ s/$;/ /g; 30889f5af480SJoe Perches $c =~ s/$;/ /g; 30894d001e4dSAndy Whitcroft 30904d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 30916f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 30926f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 30934d001e4dSAndy Whitcroft 30949f5af480SJoe Perches # Make sure we remove the line prefixes as we have 30959f5af480SJoe Perches # none on the first line, and are going to readd them 30969f5af480SJoe Perches # where necessary. 30979f5af480SJoe Perches $s =~ s/\n./\n/gs; 30989f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 30999f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 31009f5af480SJoe Perches } 31019f5af480SJoe Perches 31024d001e4dSAndy Whitcroft # We want to check the first line inside the block 31034d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 31044d001e4dSAndy Whitcroft # 1) any blank line termination 31054d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 31064d001e4dSAndy Whitcroft # 3) any do (...) { 31074d001e4dSAndy Whitcroft my $continuation = 0; 31084d001e4dSAndy Whitcroft my $check = 0; 31094d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 31104d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 31114d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 31124d001e4dSAndy Whitcroft $continuation = 1; 31134d001e4dSAndy Whitcroft } 31149bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 31154d001e4dSAndy Whitcroft $check = 1; 31164d001e4dSAndy Whitcroft $cond_lines++; 31174d001e4dSAndy Whitcroft } 31184d001e4dSAndy Whitcroft 31194d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 31204d001e4dSAndy Whitcroft # preprocessor statement. 31214d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 31224d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 31234d001e4dSAndy Whitcroft $check = 0; 31244d001e4dSAndy Whitcroft } 31254d001e4dSAndy Whitcroft 31269bd49efeSAndy Whitcroft my $cond_ptr = -1; 3127740504c6SAndy Whitcroft $continuation = 0; 31289bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 31299bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 31304d001e4dSAndy Whitcroft 3131f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3132f16fa28fSAndy Whitcroft # is not linear. 3133f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3134f16fa28fSAndy Whitcroft $check = 0; 3135f16fa28fSAndy Whitcroft } 3136f16fa28fSAndy Whitcroft 31379bd49efeSAndy Whitcroft # Ignore: 31389bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 31399bd49efeSAndy Whitcroft # 2) preprocessor lines, and 31409bd49efeSAndy Whitcroft # 3) labels. 3141740504c6SAndy Whitcroft if ($continuation || 3142740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 31439bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 31449bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3145740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 314630dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 31479bd49efeSAndy Whitcroft $cond_lines++; 31489bd49efeSAndy Whitcroft } 31494d001e4dSAndy Whitcroft } 315030dad6ebSAndy Whitcroft } 31514d001e4dSAndy Whitcroft 31524d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 31534d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 31544d001e4dSAndy Whitcroft 31554d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 31564d001e4dSAndy Whitcroft # this is not this patch's fault. 31574d001e4dSAndy Whitcroft if (!defined($stat_real) || 31584d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 31594d001e4dSAndy Whitcroft $check = 0; 31604d001e4dSAndy Whitcroft } 31614d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 31624d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 31634d001e4dSAndy Whitcroft } 31644d001e4dSAndy Whitcroft 31659bd49efeSAndy 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"; 31664d001e4dSAndy Whitcroft 31679f5af480SJoe Perches if ($check && $s ne '' && 31689f5af480SJoe Perches (($sindent % 8) != 0 || 31699f5af480SJoe Perches ($sindent < $indent) || 31709f5af480SJoe Perches ($sindent > $indent + 8))) { 3171000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3172000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 31734d001e4dSAndy Whitcroft } 31744d001e4dSAndy Whitcroft } 31754d001e4dSAndy Whitcroft 31766c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 31776c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 31781f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 31791f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 31806c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3181c2fdda0dSAndy Whitcroft if ($dbg_values) { 3182c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3183cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3184cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 31851f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3186c2fdda0dSAndy Whitcroft } 31876c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 31886c72ffaaSAndy Whitcroft 318900df344fSAndy Whitcroft#ignore lines not being added 31903705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 319100df344fSAndy Whitcroft 3192653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 31937429c690SAndy Whitcroft if ($dbg_type) { 31947429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3195000d1cc1SJoe Perches ERROR("TEST_TYPE", 3196000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 31977429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3198000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3199000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 32007429c690SAndy Whitcroft } 3201653d4876SAndy Whitcroft next; 3202653d4876SAndy Whitcroft } 3203a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3204a1ef277eSAndy Whitcroft if ($dbg_attr) { 32059360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3206000d1cc1SJoe Perches ERROR("TEST_ATTR", 3207000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 32089360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3209000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3210000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3211a1ef277eSAndy Whitcroft } 3212a1ef277eSAndy Whitcroft next; 3213a1ef277eSAndy Whitcroft } 3214653d4876SAndy Whitcroft 3215f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 321699423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 321799423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3218d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3219d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3220f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3221f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3222f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3223d752fcc8SJoe Perches my $fixedline = $prevrawline; 3224d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3225f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3226d752fcc8SJoe Perches $fixedline = $line; 3227d752fcc8SJoe Perches $fixedline =~ s/^(.\s*){\s*/$1/; 3228f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3229d752fcc8SJoe Perches } 3230f0a594c1SAndy Whitcroft } 3231f0a594c1SAndy Whitcroft 323200df344fSAndy Whitcroft# 323300df344fSAndy Whitcroft# Checks which are anchored on the added line. 323400df344fSAndy Whitcroft# 323500df344fSAndy Whitcroft 3236653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3237c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3238653d4876SAndy Whitcroft my $path = $1; 3239653d4876SAndy Whitcroft if ($path =~ m{//}) { 3240000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3241495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3242495e9d84SJoe Perches } 3243495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3244495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3245495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3246653d4876SAndy Whitcroft } 3247653d4876SAndy Whitcroft } 3248653d4876SAndy Whitcroft 324900df344fSAndy Whitcroft# no C99 // comments 325000df344fSAndy Whitcroft if ($line =~ m{//}) { 32513705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 32523705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 32533705ce5bSJoe Perches $fix) { 3254194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 32553705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 32563705ce5bSJoe Perches my $comment = trim($1); 3257194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 32583705ce5bSJoe Perches } 32593705ce5bSJoe Perches } 326000df344fSAndy Whitcroft } 326100df344fSAndy Whitcroft # Remove C99 comments. 32620a920b5bSAndy Whitcroft $line =~ s@//.*@@; 32636c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 32640a920b5bSAndy Whitcroft 32652b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 32662b474a1aSAndy Whitcroft# the whole statement. 32672b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 32682b474a1aSAndy Whitcroft if (defined $realline_next && 32692b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 32702b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 32712b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 32722b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 32733cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 32743cbf62dfSAndy Whitcroft # a prefix: 32753cbf62dfSAndy Whitcroft # XXX(foo); 32763cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3277653d4876SAndy Whitcroft my $name = $1; 327887a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 32793cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 32803cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 32813cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 32823cbf62dfSAndy Whitcroft 32833cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 32842b474a1aSAndy Whitcroft \n.}\s*$| 328548012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 328648012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 328748012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 32882b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 32892b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 329048012058SAndy Whitcroft )/x) { 32912b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 32922b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 32932b474a1aSAndy Whitcroft } else { 32942b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 32950a920b5bSAndy Whitcroft } 32960a920b5bSAndy Whitcroft } 32972b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 32982b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 32992b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 33002b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 33012b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 33022b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 33032b474a1aSAndy Whitcroft } 33042b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 33052b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3306000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3307000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 33082b474a1aSAndy Whitcroft } 33090a920b5bSAndy Whitcroft 33105150bda4SJoe Eloff# check for global initialisers. 33115129e87cSJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*(?:0|NULL|false)\s*;/) { 3312d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 3313000d1cc1SJoe Perches "do not initialise globals to 0 or NULL\n" . 3314d5e616fcSJoe Perches $herecurr) && 3315d5e616fcSJoe Perches $fix) { 33165129e87cSJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*(0|NULL|false)\s*;/$1;/; 3317d5e616fcSJoe Perches } 3318f0a594c1SAndy Whitcroft } 33190a920b5bSAndy Whitcroft# check for static initialisers. 3320d5e616fcSJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*(0|NULL|false)\s*;/) { 3321d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 3322000d1cc1SJoe Perches "do not initialise statics to 0 or NULL\n" . 3323d5e616fcSJoe Perches $herecurr) && 3324d5e616fcSJoe Perches $fix) { 3325194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*(0|NULL|false)\s*;/$1;/; 3326d5e616fcSJoe Perches } 33270a920b5bSAndy Whitcroft } 33280a920b5bSAndy Whitcroft 33291813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 33301813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 33311813087dSJoe Perches my $tmp = trim($1); 33321813087dSJoe Perches WARN("MISORDERED_TYPE", 33331813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 33341813087dSJoe Perches } 33351813087dSJoe Perches 3336cb710ecaSJoe Perches# check for static const char * arrays. 3337cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3338000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3339000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3340cb710ecaSJoe Perches $herecurr); 3341cb710ecaSJoe Perches } 3342cb710ecaSJoe Perches 3343cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3344cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3345000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3346000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 3347cb710ecaSJoe Perches $herecurr); 3348cb710ecaSJoe Perches } 3349cb710ecaSJoe Perches 3350ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 3351ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 3352ab7e23f3SJoe Perches my $found = $1; 3353ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 3354ab7e23f3SJoe Perches WARN("CONST_CONST", 3355ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 3356ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 3357ab7e23f3SJoe Perches WARN("CONST_CONST", 3358ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 3359ab7e23f3SJoe Perches } 3360ab7e23f3SJoe Perches } 3361ab7e23f3SJoe Perches 33629b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 33639b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 33649b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 33659b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 33669b0fa60dSJoe Perches $herecurr); 33679b0fa60dSJoe Perches } 33689b0fa60dSJoe Perches 3369b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 3370b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 3371b598b670SJoe Perches my $array = $1; 3372b598b670SJoe 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*\))@) { 3373b598b670SJoe Perches my $array_div = $1; 3374b598b670SJoe Perches if (WARN("ARRAY_SIZE", 3375b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 3376b598b670SJoe Perches $fix) { 3377b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 3378b598b670SJoe Perches } 3379b598b670SJoe Perches } 3380b598b670SJoe Perches } 3381b598b670SJoe Perches 3382b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 3383b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 3384b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 3385b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 3386b36190c5SJoe Perches $fix) { 3387194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 3388b36190c5SJoe Perches } 3389b36190c5SJoe Perches } 3390b36190c5SJoe Perches 339192e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE 339292e112fdSJoe Perches if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { 339392e112fdSJoe Perches if (WARN("DEFINE_PCI_DEVICE_TABLE", 339492e112fdSJoe Perches "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && 339592e112fdSJoe Perches $fix) { 3396194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /; 339792e112fdSJoe Perches } 339893ed0e2dSJoe Perches } 339993ed0e2dSJoe Perches 3400653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 3401653d4876SAndy Whitcroft# make sense. 3402653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 34038054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 3404c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 34058ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 3406653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 3407000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 3408000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 34090a920b5bSAndy Whitcroft } 34100a920b5bSAndy Whitcroft 34110a920b5bSAndy Whitcroft# * goes on variable not on type 341265863862SAndy Whitcroft # (char*[ const]) 3413bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 3414bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 34153705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 3416d8aaf121SAndy Whitcroft 341765863862SAndy Whitcroft # Should start with a space. 341865863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 341965863862SAndy Whitcroft # Should not end with a space. 342065863862SAndy Whitcroft $to =~ s/\s+$//; 342165863862SAndy Whitcroft # '*'s should not have spaces between. 3422f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 342365863862SAndy Whitcroft } 3424d8aaf121SAndy Whitcroft 34253705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 342665863862SAndy Whitcroft if ($from ne $to) { 34273705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 34283705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 34293705ce5bSJoe Perches $fix) { 34303705ce5bSJoe Perches my $sub_from = $ident; 34313705ce5bSJoe Perches my $sub_to = $ident; 34323705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3433194f66fcSJoe Perches $fixed[$fixlinenr] =~ 34343705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 34353705ce5bSJoe Perches } 343665863862SAndy Whitcroft } 3437bfcb2cc7SAndy Whitcroft } 3438bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 3439bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 34403705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 3441d8aaf121SAndy Whitcroft 344265863862SAndy Whitcroft # Should start with a space. 344365863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 344465863862SAndy Whitcroft # Should not end with a space. 344565863862SAndy Whitcroft $to =~ s/\s+$//; 344665863862SAndy Whitcroft # '*'s should not have spaces between. 3447f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 344865863862SAndy Whitcroft } 344965863862SAndy Whitcroft # Modifiers should have spaces. 345065863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 345165863862SAndy Whitcroft 34523705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 3453667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 34543705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 34553705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 34563705ce5bSJoe Perches $fix) { 34573705ce5bSJoe Perches 34583705ce5bSJoe Perches my $sub_from = $match; 34593705ce5bSJoe Perches my $sub_to = $match; 34603705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3461194f66fcSJoe Perches $fixed[$fixlinenr] =~ 34623705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 34633705ce5bSJoe Perches } 346465863862SAndy Whitcroft } 34650a920b5bSAndy Whitcroft } 34660a920b5bSAndy Whitcroft 34679d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 34689d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 34699d3e3c70SJoe Perches my $msg_type = \&WARN; 34709d3e3c70SJoe Perches $msg_type = \&CHK if ($file); 34719d3e3c70SJoe Perches &{$msg_type}("AVOID_BUG", 34729d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 34739d3e3c70SJoe Perches } 34740a920b5bSAndy Whitcroft 34759d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 34768905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 3477000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 3478000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 34798905a67cSAndy Whitcroft } 34808905a67cSAndy Whitcroft 348117441227SJoe Perches# check for uses of printk_ratelimit 348217441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 3483000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 3484000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 348517441227SJoe Perches } 348617441227SJoe Perches 348700df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 348800df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 348900df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 349025985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 349100df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 3492f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 349300df344fSAndy Whitcroft my $ok = 0; 349400df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 349500df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 349625985edcSLucas De Marchi # we have a preceding printk if it ends 349700df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 349800df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 349900df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 350000df344fSAndy Whitcroft $ok = 1; 350100df344fSAndy Whitcroft } 350200df344fSAndy Whitcroft last; 350300df344fSAndy Whitcroft } 350400df344fSAndy Whitcroft } 350500df344fSAndy Whitcroft if ($ok == 0) { 3506000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 3507000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 35080a920b5bSAndy Whitcroft } 350900df344fSAndy Whitcroft } 35100a920b5bSAndy Whitcroft 3511243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 3512243f3803SJoe Perches my $orig = $1; 3513243f3803SJoe Perches my $level = lc($orig); 3514243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 35158f26b837SJoe Perches my $level2 = $level; 35168f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 3517243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 3518daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 3519243f3803SJoe Perches } 3520243f3803SJoe Perches 3521243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 3522d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 3523d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 3524d5e616fcSJoe Perches $fix) { 3525194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3526d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 3527d5e616fcSJoe Perches } 3528243f3803SJoe Perches } 3529243f3803SJoe Perches 3530dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 3531dc139313SJoe Perches my $orig = $1; 3532dc139313SJoe Perches my $level = lc($orig); 3533dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 3534dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 3535dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 3536dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 3537dc139313SJoe Perches } 3538dc139313SJoe Perches 353991c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 354091c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 354191c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 354291c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 354391c9afafSAndy Lutomirski WARN("ENOSYS", 354491c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 354591c9afafSAndy Lutomirski } 354691c9afafSAndy Lutomirski 3547653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 3548653d4876SAndy Whitcroft# or if closed on same line 35498d182478SJoe Perches if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and 3550c45dcabdSAndy Whitcroft !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) { 35518d182478SJoe Perches if (ERROR("OPEN_BRACE", 35528d182478SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr) && 35538d182478SJoe Perches $fix) { 35548d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 35558d182478SJoe Perches my $fixed_line = $rawline; 35568d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 35578d182478SJoe Perches my $line1 = $1; 35588d182478SJoe Perches my $line2 = $2; 35598d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 35608d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 35618d182478SJoe Perches if ($line2 !~ /^\s*$/) { 35628d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 35638d182478SJoe Perches } 35648d182478SJoe Perches } 35650a920b5bSAndy Whitcroft } 3566653d4876SAndy Whitcroft 35678905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 35688905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 35698905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 35708d182478SJoe Perches if (ERROR("OPEN_BRACE", 35718d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 35728d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 35738d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 35748d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 35758d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 35768d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 35778d182478SJoe Perches $fixedline = $rawline; 35788d182478SJoe Perches $fixedline =~ s/^(.\s*){\s*/$1\t/; 35798d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 35808d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 35818d182478SJoe Perches } 35828d182478SJoe Perches } 35838905a67cSAndy Whitcroft } 35848905a67cSAndy Whitcroft 35850c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 35863705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 35873705ce5bSJoe Perches if (WARN("SPACING", 35883705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 35893705ce5bSJoe Perches $fix) { 3590194f66fcSJoe Perches $fixed[$fixlinenr] =~ 35913705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 35923705ce5bSJoe Perches } 35930c73b4ebSAndy Whitcroft } 35940c73b4ebSAndy Whitcroft 359531070b5dSJoe Perches# Function pointer declarations 359631070b5dSJoe Perches# check spacing between type, funcptr, and args 359731070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 359891f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 359931070b5dSJoe Perches my $declare = $1; 360031070b5dSJoe Perches my $pre_pointer_space = $2; 360131070b5dSJoe Perches my $post_pointer_space = $3; 360231070b5dSJoe Perches my $funcname = $4; 360331070b5dSJoe Perches my $post_funcname_space = $5; 360431070b5dSJoe Perches my $pre_args_space = $6; 360531070b5dSJoe Perches 360691f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 360791f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 360891f72e9cSJoe Perches# don't need a space so don't warn for those. 360991f72e9cSJoe Perches my $post_declare_space = ""; 361091f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 361191f72e9cSJoe Perches $post_declare_space = $1; 361291f72e9cSJoe Perches $declare = rtrim($declare); 361391f72e9cSJoe Perches } 361491f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 361531070b5dSJoe Perches WARN("SPACING", 361631070b5dSJoe Perches "missing space after return type\n" . $herecurr); 361791f72e9cSJoe Perches $post_declare_space = " "; 361831070b5dSJoe Perches } 361931070b5dSJoe Perches 362031070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 362191f72e9cSJoe Perches# This test is not currently implemented because these declarations are 362291f72e9cSJoe Perches# equivalent to 362391f72e9cSJoe Perches# int foo(int bar, ...) 362491f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 362591f72e9cSJoe Perches# 362691f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 362791f72e9cSJoe Perches# WARN("SPACING", 362891f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 362991f72e9cSJoe Perches# } 363031070b5dSJoe Perches 363131070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 363231070b5dSJoe Perches if (defined $pre_pointer_space && 363331070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 363431070b5dSJoe Perches WARN("SPACING", 363531070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 363631070b5dSJoe Perches } 363731070b5dSJoe Perches 363831070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 363931070b5dSJoe Perches if (defined $post_pointer_space && 364031070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 364131070b5dSJoe Perches WARN("SPACING", 364231070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 364331070b5dSJoe Perches } 364431070b5dSJoe Perches 364531070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 364631070b5dSJoe Perches if (defined $post_funcname_space && 364731070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 364831070b5dSJoe Perches WARN("SPACING", 364931070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 365031070b5dSJoe Perches } 365131070b5dSJoe Perches 365231070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 365331070b5dSJoe Perches if (defined $pre_args_space && 365431070b5dSJoe Perches $pre_args_space =~ /^\s/) { 365531070b5dSJoe Perches WARN("SPACING", 365631070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 365731070b5dSJoe Perches } 365831070b5dSJoe Perches 365931070b5dSJoe Perches if (show_type("SPACING") && $fix) { 3660194f66fcSJoe Perches $fixed[$fixlinenr] =~ 366191f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 366231070b5dSJoe Perches } 366331070b5dSJoe Perches } 366431070b5dSJoe Perches 36658d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 36668d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 3667fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 3668fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 36698d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 36708d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 36718d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 3672fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 3673daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 36743705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 36753705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 36763705ce5bSJoe Perches $fix) { 3677194f66fcSJoe Perches $fixed[$fixlinenr] =~ 36783705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 36793705ce5bSJoe Perches } 36808d31cfceSAndy Whitcroft } 36818d31cfceSAndy Whitcroft } 36828d31cfceSAndy Whitcroft 3683f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 36846c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 3685c2fdda0dSAndy Whitcroft my $name = $1; 3686773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 3687773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 3688c2fdda0dSAndy Whitcroft 3689c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 3690773647a0SAndy Whitcroft if ($name =~ /^(?: 3691773647a0SAndy Whitcroft if|for|while|switch|return|case| 3692773647a0SAndy Whitcroft volatile|__volatile__| 3693773647a0SAndy Whitcroft __attribute__|format|__extension__| 3694773647a0SAndy Whitcroft asm|__asm__)$/x) 3695773647a0SAndy Whitcroft { 3696c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 3697c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 3698c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 3699c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 3700773647a0SAndy Whitcroft 3701773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 3702c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 3703c2fdda0dSAndy Whitcroft 3704c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 3705c2fdda0dSAndy Whitcroft # likely a typedef for a function. 3706773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 3707c2fdda0dSAndy Whitcroft 3708c2fdda0dSAndy Whitcroft } else { 37093705ce5bSJoe Perches if (WARN("SPACING", 37103705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 37113705ce5bSJoe Perches $fix) { 3712194f66fcSJoe Perches $fixed[$fixlinenr] =~ 37133705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 37143705ce5bSJoe Perches } 3715f0a594c1SAndy Whitcroft } 37166c72ffaaSAndy Whitcroft } 37179a4cad4eSEric Nelson 3718653d4876SAndy Whitcroft# Check operator spacing. 37190a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 37203705ce5bSJoe Perches my $fixed_line = ""; 37213705ce5bSJoe Perches my $line_fixed = 0; 37223705ce5bSJoe Perches 37239c0ca6f9SAndy Whitcroft my $ops = qr{ 37249c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 37259c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 37269c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 37271f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 372884731623SJoe Perches \?:|\?|: 37299c0ca6f9SAndy Whitcroft }x; 3730cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 37313705ce5bSJoe Perches 37323705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 37333705ce5bSJoe Perches## foreach my $el (@elements) { 37343705ce5bSJoe Perches## print("el: <$el>\n"); 37353705ce5bSJoe Perches## } 37363705ce5bSJoe Perches 37373705ce5bSJoe Perches my @fix_elements = (); 373800df344fSAndy Whitcroft my $off = 0; 37396c72ffaaSAndy Whitcroft 37403705ce5bSJoe Perches foreach my $el (@elements) { 37413705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 37423705ce5bSJoe Perches $off += length($el); 37433705ce5bSJoe Perches } 37443705ce5bSJoe Perches 37453705ce5bSJoe Perches $off = 0; 37463705ce5bSJoe Perches 37476c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 3748b34c648bSJoe Perches my $last_after = -1; 37496c72ffaaSAndy Whitcroft 37500a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 37513705ce5bSJoe Perches 37523705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 37533705ce5bSJoe Perches 37543705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 37553705ce5bSJoe Perches 37564a0df2efSAndy Whitcroft $off += length($elements[$n]); 37574a0df2efSAndy Whitcroft 375825985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 3759773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 3760773647a0SAndy Whitcroft my $cc = ''; 3761773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 3762773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 3763773647a0SAndy Whitcroft } 3764773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 3765773647a0SAndy Whitcroft 37664a0df2efSAndy Whitcroft my $a = ''; 37674a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 37684a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 3769cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 37704a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 37714a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 3772773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 37734a0df2efSAndy Whitcroft 37740a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 37754a0df2efSAndy Whitcroft 37764a0df2efSAndy Whitcroft my $c = ''; 37770a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 37784a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 37794a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 3780cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 37814a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 37824a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 37838b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 37844a0df2efSAndy Whitcroft } else { 37854a0df2efSAndy Whitcroft $c = 'E'; 37860a920b5bSAndy Whitcroft } 37870a920b5bSAndy Whitcroft 37884a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 37894a0df2efSAndy Whitcroft 37904a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 37914a0df2efSAndy Whitcroft 37926c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 3793de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 37940a920b5bSAndy Whitcroft 379574048ed8SAndy Whitcroft # Pull out the value of this operator. 37966c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 37970a920b5bSAndy Whitcroft 37981f65f947SAndy Whitcroft # Get the full operator variant. 37991f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 38001f65f947SAndy Whitcroft 380113214adfSAndy Whitcroft # Ignore operators passed as parameters. 380213214adfSAndy Whitcroft if ($op_type ne 'V' && 3803d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 380413214adfSAndy Whitcroft 3805cf655043SAndy Whitcroft# # Ignore comments 3806cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 380713214adfSAndy Whitcroft 3808d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 380913214adfSAndy Whitcroft } elsif ($op eq ';') { 3810cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 3811cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 38123705ce5bSJoe Perches if (ERROR("SPACING", 38133705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 3814b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 38153705ce5bSJoe Perches $line_fixed = 1; 38163705ce5bSJoe Perches } 3817d8aaf121SAndy Whitcroft } 3818d8aaf121SAndy Whitcroft 3819d8aaf121SAndy Whitcroft # // is a comment 3820d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 38210a920b5bSAndy Whitcroft 3822b00e4814SJoe Perches # : when part of a bitfield 3823b00e4814SJoe Perches } elsif ($opv eq ':B') { 3824b00e4814SJoe Perches # skip the bitfield test for now 3825b00e4814SJoe Perches 38261f65f947SAndy Whitcroft # No spaces for: 38271f65f947SAndy Whitcroft # -> 3828b00e4814SJoe Perches } elsif ($op eq '->') { 38294a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 38303705ce5bSJoe Perches if (ERROR("SPACING", 38313705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 3832b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 38333705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 38343705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 38353705ce5bSJoe Perches } 3836b34c648bSJoe Perches $line_fixed = 1; 38373705ce5bSJoe Perches } 38380a920b5bSAndy Whitcroft } 38390a920b5bSAndy Whitcroft 38402381097bSJoe Perches # , must not have a space before and must have a space on the right. 38410a920b5bSAndy Whitcroft } elsif ($op eq ',') { 38422381097bSJoe Perches my $rtrim_before = 0; 38432381097bSJoe Perches my $space_after = 0; 38442381097bSJoe Perches if ($ctx =~ /Wx./) { 38452381097bSJoe Perches if (ERROR("SPACING", 38462381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 38472381097bSJoe Perches $line_fixed = 1; 38482381097bSJoe Perches $rtrim_before = 1; 38492381097bSJoe Perches } 38502381097bSJoe Perches } 3851cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 38523705ce5bSJoe Perches if (ERROR("SPACING", 38533705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 38543705ce5bSJoe Perches $line_fixed = 1; 3855b34c648bSJoe Perches $last_after = $n; 38562381097bSJoe Perches $space_after = 1; 38572381097bSJoe Perches } 38582381097bSJoe Perches } 38592381097bSJoe Perches if ($rtrim_before || $space_after) { 38602381097bSJoe Perches if ($rtrim_before) { 38612381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 38622381097bSJoe Perches } else { 38632381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 38642381097bSJoe Perches } 38652381097bSJoe Perches if ($space_after) { 38662381097bSJoe Perches $good .= " "; 38673705ce5bSJoe Perches } 38680a920b5bSAndy Whitcroft } 38690a920b5bSAndy Whitcroft 38709c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 387174048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 38729c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 38739c0ca6f9SAndy Whitcroft 38749c0ca6f9SAndy Whitcroft # unary operators should have a space before and 38759c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 38769c0ca6f9SAndy Whitcroft # unary operator, or a cast 38779c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 387874048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 38790d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 3880cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 38813705ce5bSJoe Perches if (ERROR("SPACING", 38823705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 3883b34c648bSJoe Perches if ($n != $last_after + 2) { 3884b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 38853705ce5bSJoe Perches $line_fixed = 1; 38863705ce5bSJoe Perches } 38870a920b5bSAndy Whitcroft } 3888b34c648bSJoe Perches } 3889a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 3890171ae1a4SAndy Whitcroft # A unary '*' may be const 3891171ae1a4SAndy Whitcroft 3892171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 38933705ce5bSJoe Perches if (ERROR("SPACING", 38943705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3895b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 38963705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 38973705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 38983705ce5bSJoe Perches } 3899b34c648bSJoe Perches $line_fixed = 1; 39003705ce5bSJoe Perches } 39010a920b5bSAndy Whitcroft } 39020a920b5bSAndy Whitcroft 39030a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 39040a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 3905773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 39063705ce5bSJoe Perches if (ERROR("SPACING", 39073705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 3908b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 39093705ce5bSJoe Perches $line_fixed = 1; 39103705ce5bSJoe Perches } 39110a920b5bSAndy Whitcroft } 3912773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 3913773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 39143705ce5bSJoe Perches if (ERROR("SPACING", 39153705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3916b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 39173705ce5bSJoe Perches $line_fixed = 1; 39183705ce5bSJoe Perches } 3919653d4876SAndy Whitcroft } 3920773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 39213705ce5bSJoe Perches if (ERROR("SPACING", 39223705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 3923b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 39243705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 39253705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3926773647a0SAndy Whitcroft } 3927b34c648bSJoe Perches $line_fixed = 1; 39283705ce5bSJoe Perches } 39293705ce5bSJoe Perches } 39300a920b5bSAndy Whitcroft 39310a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 39329c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 39339c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 39349c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 3935c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 3936c2fdda0dSAndy Whitcroft $op eq '%') 39370a920b5bSAndy Whitcroft { 3938d2e025f3SJoe Perches if ($check) { 3939d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 3940d2e025f3SJoe Perches if (CHK("SPACING", 3941d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 3942d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3943d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3944d2e025f3SJoe Perches $line_fixed = 1; 3945d2e025f3SJoe Perches } 3946d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 3947d2e025f3SJoe Perches if (CHK("SPACING", 3948d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 3949d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 3950d2e025f3SJoe Perches $line_fixed = 1; 3951d2e025f3SJoe Perches } 3952d2e025f3SJoe Perches } 3953d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 39543705ce5bSJoe Perches if (ERROR("SPACING", 39553705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 3956b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 3957b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 3958b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 3959b34c648bSJoe Perches } 39603705ce5bSJoe Perches $line_fixed = 1; 39613705ce5bSJoe Perches } 39620a920b5bSAndy Whitcroft } 39630a920b5bSAndy Whitcroft 39641f65f947SAndy Whitcroft # A colon needs no spaces before when it is 39651f65f947SAndy Whitcroft # terminating a case value or a label. 39661f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 39671f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 39683705ce5bSJoe Perches if (ERROR("SPACING", 39693705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 3970b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 39713705ce5bSJoe Perches $line_fixed = 1; 39723705ce5bSJoe Perches } 39731f65f947SAndy Whitcroft } 39741f65f947SAndy Whitcroft 39750a920b5bSAndy Whitcroft # All the others need spaces both sides. 3976cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 39771f65f947SAndy Whitcroft my $ok = 0; 39781f65f947SAndy Whitcroft 397922f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 39801f65f947SAndy Whitcroft if (($op eq '<' && 39811f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 39821f65f947SAndy Whitcroft ($op eq '>' && 39831f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 39841f65f947SAndy Whitcroft { 39851f65f947SAndy Whitcroft $ok = 1; 39861f65f947SAndy Whitcroft } 39871f65f947SAndy Whitcroft 3988e0df7e1fSJoe Perches # for asm volatile statements 3989e0df7e1fSJoe Perches # ignore a colon with another 3990e0df7e1fSJoe Perches # colon immediately before or after 3991e0df7e1fSJoe Perches if (($op eq ':') && 3992e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 3993e0df7e1fSJoe Perches $ok = 1; 3994e0df7e1fSJoe Perches } 3995e0df7e1fSJoe Perches 399684731623SJoe Perches # messages are ERROR, but ?: are CHK 39971f65f947SAndy Whitcroft if ($ok == 0) { 399884731623SJoe Perches my $msg_type = \&ERROR; 399984731623SJoe Perches $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 400084731623SJoe Perches 400184731623SJoe Perches if (&{$msg_type}("SPACING", 40023705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4003b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4004b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4005b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4006b34c648bSJoe Perches } 40073705ce5bSJoe Perches $line_fixed = 1; 40083705ce5bSJoe Perches } 40090a920b5bSAndy Whitcroft } 401022f2a2efSAndy Whitcroft } 40114a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 40123705ce5bSJoe Perches 40133705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 40143705ce5bSJoe Perches 40153705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 40160a920b5bSAndy Whitcroft } 40173705ce5bSJoe Perches 40183705ce5bSJoe Perches if (($#elements % 2) == 0) { 40193705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 40203705ce5bSJoe Perches } 40213705ce5bSJoe Perches 4022194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4023194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 40243705ce5bSJoe Perches } 40253705ce5bSJoe Perches 40263705ce5bSJoe Perches 40270a920b5bSAndy Whitcroft } 40280a920b5bSAndy Whitcroft 4029786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4030d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4031786b6326SJoe Perches if (WARN("SPACING", 4032786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4033786b6326SJoe Perches $fix) { 4034194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4035786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4036786b6326SJoe Perches } 4037786b6326SJoe Perches } 4038786b6326SJoe Perches 4039f0a594c1SAndy Whitcroft# check for multiple assignments 4040f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4041000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4042000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4043f0a594c1SAndy Whitcroft } 4044f0a594c1SAndy Whitcroft 404522f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 404622f2a2efSAndy Whitcroft## # continuation. 404722f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 404822f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 404922f2a2efSAndy Whitcroft## 405022f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 405122f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 405222f2a2efSAndy Whitcroft## my $ln = $line; 405322f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 405422f2a2efSAndy Whitcroft## } 405522f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4056000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4057000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 405822f2a2efSAndy Whitcroft## } 405922f2a2efSAndy Whitcroft## } 4060f0a594c1SAndy Whitcroft 40610a920b5bSAndy Whitcroft#need space before brace following if, while, etc 406222f2a2efSAndy Whitcroft if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) || 406322f2a2efSAndy Whitcroft $line =~ /do{/) { 40643705ce5bSJoe Perches if (ERROR("SPACING", 40653705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 40663705ce5bSJoe Perches $fix) { 4067194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/; 40683705ce5bSJoe Perches } 4069de7d4f0eSAndy Whitcroft } 4070de7d4f0eSAndy Whitcroft 4071c4a62ef9SJoe Perches## # check for blank lines before declarations 4072c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4073c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4074c4a62ef9SJoe Perches## WARN("SPACING", 4075c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4076c4a62ef9SJoe Perches## } 4077c4a62ef9SJoe Perches## 4078c4a62ef9SJoe Perches 4079de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4080de7d4f0eSAndy Whitcroft# on the line 4081de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 4082d5e616fcSJoe Perches if (ERROR("SPACING", 4083d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4084d5e616fcSJoe Perches $fix) { 4085194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4086d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4087d5e616fcSJoe Perches } 40880a920b5bSAndy Whitcroft } 40890a920b5bSAndy Whitcroft 409022f2a2efSAndy Whitcroft# check spacing on square brackets 409122f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 40923705ce5bSJoe Perches if (ERROR("SPACING", 40933705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 40943705ce5bSJoe Perches $fix) { 4095194f66fcSJoe Perches $fixed[$fixlinenr] =~ 40963705ce5bSJoe Perches s/\[\s+/\[/; 40973705ce5bSJoe Perches } 409822f2a2efSAndy Whitcroft } 409922f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 41003705ce5bSJoe Perches if (ERROR("SPACING", 41013705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 41023705ce5bSJoe Perches $fix) { 4103194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41043705ce5bSJoe Perches s/\s+\]/\]/; 41053705ce5bSJoe Perches } 410622f2a2efSAndy Whitcroft } 410722f2a2efSAndy Whitcroft 4108c45dcabdSAndy Whitcroft# check spacing on parentheses 41099c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 41109c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 41113705ce5bSJoe Perches if (ERROR("SPACING", 41123705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 41133705ce5bSJoe Perches $fix) { 4114194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41153705ce5bSJoe Perches s/\(\s+/\(/; 41163705ce5bSJoe Perches } 411722f2a2efSAndy Whitcroft } 411813214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4119c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4120c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 41213705ce5bSJoe Perches if (ERROR("SPACING", 41223705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 41233705ce5bSJoe Perches $fix) { 4124194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41253705ce5bSJoe Perches s/\s+\)/\)/; 41263705ce5bSJoe Perches } 412722f2a2efSAndy Whitcroft } 412822f2a2efSAndy Whitcroft 4129e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4130e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4131e2826fd0SJoe Perches 4132e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4133ea4acbb1SJoe Perches my $var = $1; 4134ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4135ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4136ea4acbb1SJoe Perches $fix) { 4137ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4138ea4acbb1SJoe Perches } 4139ea4acbb1SJoe Perches } 4140ea4acbb1SJoe Perches 4141ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4142ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4143ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4144ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4145ea4acbb1SJoe Perches my $var = $2; 4146ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4147ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4148ea4acbb1SJoe Perches $fix) { 4149ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4150ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4151ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4152ea4acbb1SJoe Perches } 4153e2826fd0SJoe Perches } 4154e2826fd0SJoe Perches 41550a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 41564a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 41570a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 41583705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 41593705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 41603705ce5bSJoe Perches $fix) { 4161194f66fcSJoe Perches $fixed[$fixlinenr] =~ 41623705ce5bSJoe Perches s/^(.)\s+/$1/; 41633705ce5bSJoe Perches } 41640a920b5bSAndy Whitcroft } 41650a920b5bSAndy Whitcroft 41665b9553abSJoe Perches# return is not a function 4167507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4168c45dcabdSAndy Whitcroft my $spacing = $1; 4169507e5141SJoe Perches if ($^V && $^V ge 5.10.0 && 41705b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 41715b9553abSJoe Perches my $value = $1; 41725b9553abSJoe Perches $value = deparenthesize($value); 41735b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4174000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4175000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 41765b9553abSJoe Perches } 4177c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4178000d1cc1SJoe Perches ERROR("SPACING", 4179000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4180c45dcabdSAndy Whitcroft } 4181c45dcabdSAndy Whitcroft } 4182507e5141SJoe Perches 4183b43ae21bSJoe Perches# unnecessary return in a void function 4184b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4185b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4186b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4187b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4188b43ae21bSJoe Perches $linenr >= 3 && 4189b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4190b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 41919819cf25SJoe Perches WARN("RETURN_VOID", 4192b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 41939819cf25SJoe Perches } 41949819cf25SJoe Perches 4195189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 4196189248d8SJoe Perches if ($^V && $^V ge 5.10.0 && 4197189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4198189248d8SJoe Perches my $openparens = $1; 4199189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4200189248d8SJoe Perches my $msg = ""; 4201189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4202189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4203189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4204189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4205189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4206189248d8SJoe Perches } 4207189248d8SJoe Perches } 4208189248d8SJoe Perches 4209f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4210f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 421153a3c448SAndy Whitcroft my $name = $1; 421253a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4213000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4214f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 421553a3c448SAndy Whitcroft } 421653a3c448SAndy Whitcroft } 4217c45dcabdSAndy Whitcroft 42180a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 42194a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 42203705ce5bSJoe Perches if (ERROR("SPACING", 42213705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 42223705ce5bSJoe Perches $fix) { 4223194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42243705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 42253705ce5bSJoe Perches } 42260a920b5bSAndy Whitcroft } 42270a920b5bSAndy Whitcroft 4228f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4229f5fe35ddSAndy Whitcroft# statements after the conditional. 4230170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 42313e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 42323e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 42333e469cdcSAndy Whitcroft if (!defined $stat); 4234170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4235170d3a22SAndy Whitcroft $remain_next, $off_next); 4236170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4237170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4238170d3a22SAndy Whitcroft 4239170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4240170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4241170d3a22SAndy Whitcroft # then count those as offsets. 4242170d3a22SAndy Whitcroft my ($whitespace) = 4243170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4244170d3a22SAndy Whitcroft my $offset = 4245170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4246170d3a22SAndy Whitcroft 4247170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4248170d3a22SAndy Whitcroft $offset} = 1; 4249170d3a22SAndy Whitcroft } 4250170d3a22SAndy Whitcroft } 4251170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4252c11230f4SJoe Perches defined($stat) && defined($cond) && 4253170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4254171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 42558905a67cSAndy Whitcroft 4256b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4257000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4258000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 42598905a67cSAndy Whitcroft } 42608905a67cSAndy Whitcroft 42618905a67cSAndy Whitcroft # Find out what is on the end of the line after the 42628905a67cSAndy Whitcroft # conditional. 4263773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 42648905a67cSAndy Whitcroft $s =~ s/\n.*//g; 426513214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 426653210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 426753210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4268773647a0SAndy Whitcroft { 4269bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4270bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4271bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 427242bdf74cSHidetoshi Seto my $stat_real = ''; 4273bb44ad39SAndy Whitcroft 427442bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 427542bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4276bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4277bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4278bb44ad39SAndy Whitcroft } 4279bb44ad39SAndy Whitcroft 4280000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4281000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 42828905a67cSAndy Whitcroft } 42838905a67cSAndy Whitcroft } 42848905a67cSAndy Whitcroft 428513214adfSAndy Whitcroft# Check for bitwise tests written as boolean 428613214adfSAndy Whitcroft if ($line =~ / 428713214adfSAndy Whitcroft (?: 428813214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 428913214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 429013214adfSAndy Whitcroft (?:\&\&|\|\|) 429113214adfSAndy Whitcroft | 429213214adfSAndy Whitcroft (?:\&\&|\|\|) 429313214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 429413214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 429513214adfSAndy Whitcroft )/x) 429613214adfSAndy Whitcroft { 4297000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4298000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 429913214adfSAndy Whitcroft } 430013214adfSAndy Whitcroft 43018905a67cSAndy Whitcroft# if and else should not have general statements after it 430213214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 430313214adfSAndy Whitcroft my $s = $1; 430413214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 430513214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4306000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4307000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 43080a920b5bSAndy Whitcroft } 430913214adfSAndy Whitcroft } 431039667782SAndy Whitcroft# if should not continue a brace 431139667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4312000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4313048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 431439667782SAndy Whitcroft $herecurr); 431539667782SAndy Whitcroft } 4316a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4317a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4318a1080bf8SAndy Whitcroft $line !~ /\G(?: 43193fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4320a1080bf8SAndy Whitcroft \s*return\s+ 4321a1080bf8SAndy Whitcroft )/xg) 4322a1080bf8SAndy Whitcroft { 4323000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4324000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4325a1080bf8SAndy Whitcroft } 43260a920b5bSAndy Whitcroft 43270a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 43280a920b5bSAndy Whitcroft # indent level to be relevant to each other. 43298b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 43300a920b5bSAndy Whitcroft $previndent == $indent) { 43318b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 43328b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 43338b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 43348b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 43358b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 43368b8856f4SJoe Perches my $fixedline = $prevrawline; 43378b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 43388b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 43398b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 43408b8856f4SJoe Perches } 43418b8856f4SJoe Perches $fixedline = $rawline; 43428b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 43438b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 43448b8856f4SJoe Perches } 43450a920b5bSAndy Whitcroft } 43460a920b5bSAndy Whitcroft 43478b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 4348c2fdda0dSAndy Whitcroft $previndent == $indent) { 4349c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 4350c2fdda0dSAndy Whitcroft 4351c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 4352c2fdda0dSAndy Whitcroft # conditional. 4353773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 4354c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 4355c2fdda0dSAndy Whitcroft 4356c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 43578b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 43588b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 43598b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 43608b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 43618b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 43628b8856f4SJoe Perches my $fixedline = $prevrawline; 43638b8856f4SJoe Perches my $trailing = $rawline; 43648b8856f4SJoe Perches $trailing =~ s/^\+//; 43658b8856f4SJoe Perches $trailing = trim($trailing); 43668b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 43678b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 43688b8856f4SJoe Perches } 4369c2fdda0dSAndy Whitcroft } 4370c2fdda0dSAndy Whitcroft } 4371c2fdda0dSAndy Whitcroft 437295e2c602SJoe Perches#Specific variable tests 4373323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 4374323c1260SJoe Perches my $var = $1; 437595e2c602SJoe Perches 437695e2c602SJoe Perches#gcc binary extension 437795e2c602SJoe Perches if ($var =~ /^$Binary$/) { 4378d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 4379d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 4380d5e616fcSJoe Perches $fix) { 4381d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 4382194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4383d5e616fcSJoe Perches s/\b$var\b/$hexval/; 4384d5e616fcSJoe Perches } 438595e2c602SJoe Perches } 438695e2c602SJoe Perches 438795e2c602SJoe Perches#CamelCase 4388807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 4389be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 439022735ce8SJoe Perches#Ignore Page<foo> variants 4391807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 439222735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 4393f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && 4394f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 4395f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 43967e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 43977e781f67SJoe Perches my $word = $1; 43987e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 4399d8b07710SJoe Perches if ($check) { 4400d8b07710SJoe Perches seed_camelcase_includes(); 4401d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 4402d8b07710SJoe Perches seed_camelcase_file($realfile); 4403d8b07710SJoe Perches $camelcase_file_seeded = 1; 4404d8b07710SJoe Perches } 4405d8b07710SJoe Perches } 44067e781f67SJoe Perches if (!defined $camelcase{$word}) { 44077e781f67SJoe Perches $camelcase{$word} = 1; 4408be79794bSJoe Perches CHK("CAMELCASE", 44097e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 44107e781f67SJoe Perches } 4411323c1260SJoe Perches } 4412323c1260SJoe Perches } 44133445686aSJoe Perches } 44140a920b5bSAndy Whitcroft 44150a920b5bSAndy Whitcroft#no spaces allowed after \ in define 4416d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 4417d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 4418d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 4419d5e616fcSJoe Perches $fix) { 4420194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 4421d5e616fcSJoe Perches } 44220a920b5bSAndy Whitcroft } 44230a920b5bSAndy Whitcroft 44240e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 44250e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 4426c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 4427e09dec48SAndy Whitcroft my $file = "$1.h"; 4428e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 4429e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 4430e09dec48SAndy Whitcroft $realfile ne $checkfile && 44317840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 4432c45dcabdSAndy Whitcroft { 44330e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 44340e212e0aSFabian Frederick if ($asminclude > 0) { 4435e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 4436000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 4437000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4438e09dec48SAndy Whitcroft } else { 4439000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 4440000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4441e09dec48SAndy Whitcroft } 44420a920b5bSAndy Whitcroft } 44430a920b5bSAndy Whitcroft } 44440e212e0aSFabian Frederick } 44450a920b5bSAndy Whitcroft 4446653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 4447653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 4448cf655043SAndy Whitcroft# in a known good container 4449b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 4450b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 4451d8aaf121SAndy Whitcroft my $ln = $linenr; 4452d8aaf121SAndy Whitcroft my $cnt = $realcnt; 4453c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 4454c45dcabdSAndy Whitcroft my $ctx = ''; 445508a2843eSJoe Perches my $has_flow_statement = 0; 445608a2843eSJoe Perches my $has_arg_concat = 0; 4457c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 4458f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 4459f74bd194SAndy Whitcroft $ctx = $dstat; 4460c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4461a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4462c45dcabdSAndy Whitcroft 446308a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 446408a2843eSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/); 446508a2843eSJoe Perches 4466f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 4467292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 4468c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 4469c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 4470c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 4471c45dcabdSAndy Whitcroft 4472c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 4473bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 4474bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 4475c81769fdSAndy Whitcroft $dstat =~ s/\[[^\[\]]*\]/1/) 4476bf30d6edSAndy Whitcroft { 4477c45dcabdSAndy Whitcroft } 4478c45dcabdSAndy Whitcroft 4479e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 448033acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 448133acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 4482e45bab8eSAndy Whitcroft { 4483e45bab8eSAndy Whitcroft } 4484e45bab8eSAndy Whitcroft 4485c45dcabdSAndy Whitcroft my $exceptions = qr{ 4486c45dcabdSAndy Whitcroft $Declare| 4487c45dcabdSAndy Whitcroft module_param_named| 4488a0a0a7a9SKees Cook MODULE_PARM_DESC| 4489c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 4490c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 4491383099fdSAndy Whitcroft __typeof__\(| 449222fd2d3eSStefani Seibold union| 449322fd2d3eSStefani Seibold struct| 4494ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 4495ea71a0a0SAndy Whitcroft ^\"|\"$ 4496c45dcabdSAndy Whitcroft }x; 44975eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 4498f74bd194SAndy Whitcroft if ($dstat ne '' && 4499f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 4500f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 45013cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 4502356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 4503f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 4504f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 4505e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 450672f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 4507f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 4508f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 4509f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 4510f95a7e6aSJoe Perches $dstat !~ /^\({/ && # ({... 4511f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 4512c45dcabdSAndy Whitcroft { 4513f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 4514f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 4515f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 4516f74bd194SAndy Whitcroft 4517f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 4518f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 4519c45dcabdSAndy Whitcroft } 4520c45dcabdSAndy Whitcroft 4521f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 4522f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 4523f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 4524f74bd194SAndy Whitcroft } else { 4525000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 4526388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 4527d8aaf121SAndy Whitcroft } 45280a920b5bSAndy Whitcroft } 45295023d347SJoe Perches 453008a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 453108a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 453208a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 453308a2843eSJoe Perches my $herectx = $here . "\n"; 453408a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 453508a2843eSJoe Perches 453608a2843eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 453708a2843eSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 453808a2843eSJoe Perches } 453908a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 454008a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 454108a2843eSJoe Perches } 454208a2843eSJoe Perches 4543481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 45445023d347SJoe Perches 45455023d347SJoe Perches } else { 45465023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 4547481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 4548481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 45495023d347SJoe Perches $line =~ /^\+.*\\$/) { 45505023d347SJoe Perches WARN("LINE_CONTINUATIONS", 45515023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 45525023d347SJoe Perches } 4553653d4876SAndy Whitcroft } 45540a920b5bSAndy Whitcroft 4555b13edf7fSJoe Perches# do {} while (0) macro tests: 4556b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 4557b13edf7fSJoe Perches# macro should not end with a semicolon 4558b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 4559b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 4560b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 4561b13edf7fSJoe Perches my $ln = $linenr; 4562b13edf7fSJoe Perches my $cnt = $realcnt; 4563b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 4564b13edf7fSJoe Perches my $ctx = ''; 4565b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 4566b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 4567b13edf7fSJoe Perches $ctx = $dstat; 4568b13edf7fSJoe Perches 4569b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 45701b36b201SJoe Perches $dstat =~ s/$;/ /g; 4571b13edf7fSJoe Perches 4572b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 4573b13edf7fSJoe Perches my $stmts = $2; 4574b13edf7fSJoe Perches my $semis = $3; 4575b13edf7fSJoe Perches 4576b13edf7fSJoe Perches $ctx =~ s/\n*$//; 4577b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 4578b13edf7fSJoe Perches my $herectx = $here . "\n"; 4579b13edf7fSJoe Perches 4580b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4581b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4582b13edf7fSJoe Perches } 4583b13edf7fSJoe Perches 4584ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 4585ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 4586b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 4587b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 4588b13edf7fSJoe Perches } 4589b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 4590b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 4591b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 4592b13edf7fSJoe Perches } 4593f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 4594f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 4595f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 4596f5ef95b1SJoe Perches my $herectx = $here . "\n"; 4597f5ef95b1SJoe Perches 4598f5ef95b1SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4599f5ef95b1SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4600f5ef95b1SJoe Perches } 4601f5ef95b1SJoe Perches 4602f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 4603f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 4604b13edf7fSJoe Perches } 4605b13edf7fSJoe Perches } 4606b13edf7fSJoe Perches 4607080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 4608080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 4609080ba929SMike Frysinger# . 4610080ba929SMike Frysinger# ALIGN(...) 4611080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 4612080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 4613000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 4614000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 4615080ba929SMike Frysinger } 4616080ba929SMike Frysinger 4617f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 461813214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 461913214adfSAndy Whitcroft my ($level, $endln, @chunks) = 4620cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 462113214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 4622cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 4623cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 4624aad4f614SJoe Perches my @allowed = (); 4625aad4f614SJoe Perches my $allow = 0; 462613214adfSAndy Whitcroft my $seen = 0; 4627773647a0SAndy Whitcroft my $herectx = $here . "\n"; 4628cf655043SAndy Whitcroft my $ln = $linenr - 1; 462913214adfSAndy Whitcroft for my $chunk (@chunks) { 463013214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 463113214adfSAndy Whitcroft 4632773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 4633773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 4634773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 4635773647a0SAndy Whitcroft 4636aad4f614SJoe Perches $allowed[$allow] = 0; 4637773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 4638773647a0SAndy Whitcroft 4639773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 4640773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 4641773647a0SAndy Whitcroft 4642773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 4643cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 4644cf655043SAndy Whitcroft 4645773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 464613214adfSAndy Whitcroft 464713214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 464813214adfSAndy Whitcroft 4649aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 4650cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 4651cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 4652aad4f614SJoe Perches $allowed[$allow] = 1; 465313214adfSAndy Whitcroft } 465413214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 4655cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 4656aad4f614SJoe Perches $allowed[$allow] = 1; 465713214adfSAndy Whitcroft } 4658cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 4659cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 4660aad4f614SJoe Perches $allowed[$allow] = 1; 466113214adfSAndy Whitcroft } 4662aad4f614SJoe Perches $allow++; 466313214adfSAndy Whitcroft } 4664aad4f614SJoe Perches if ($seen) { 4665aad4f614SJoe Perches my $sum_allowed = 0; 4666aad4f614SJoe Perches foreach (@allowed) { 4667aad4f614SJoe Perches $sum_allowed += $_; 4668aad4f614SJoe Perches } 4669aad4f614SJoe Perches if ($sum_allowed == 0) { 4670000d1cc1SJoe Perches WARN("BRACES", 4671000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 4672aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 4673aad4f614SJoe Perches $seen != $allow) { 4674aad4f614SJoe Perches CHK("BRACES", 4675aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 4676aad4f614SJoe Perches } 467713214adfSAndy Whitcroft } 467813214adfSAndy Whitcroft } 467913214adfSAndy Whitcroft } 4680773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 468113214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 4682cf655043SAndy Whitcroft my $allowed = 0; 4683f0a594c1SAndy Whitcroft 4684cf655043SAndy Whitcroft # Check the pre-context. 4685cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 4686cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 4687cf655043SAndy Whitcroft $allowed = 1; 4688f0a594c1SAndy Whitcroft } 4689773647a0SAndy Whitcroft 4690773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 4691773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 4692773647a0SAndy Whitcroft 4693cf655043SAndy Whitcroft # Check the condition. 4694cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 4695773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 4696cf655043SAndy Whitcroft if (defined $cond) { 4697773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 4698cf655043SAndy Whitcroft } 4699cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 4700cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 4701cf655043SAndy Whitcroft $allowed = 1; 4702cf655043SAndy Whitcroft } 4703cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 4704cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 4705cf655043SAndy Whitcroft $allowed = 1; 4706cf655043SAndy Whitcroft } 4707cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 4708cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 4709cf655043SAndy Whitcroft $allowed = 1; 4710cf655043SAndy Whitcroft } 4711cf655043SAndy Whitcroft # Check the post-context. 4712cf655043SAndy Whitcroft if (defined $chunks[1]) { 4713cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 4714cf655043SAndy Whitcroft if (defined $cond) { 4715773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 4716cf655043SAndy Whitcroft } 4717cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 4718cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 4719cf655043SAndy Whitcroft $allowed = 1; 4720cf655043SAndy Whitcroft } 4721cf655043SAndy Whitcroft } 4722cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 472369932487SJustin P. Mattock my $herectx = $here . "\n"; 4724f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 4725cf655043SAndy Whitcroft 4726f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 472769932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 4728cf655043SAndy Whitcroft } 4729cf655043SAndy Whitcroft 4730000d1cc1SJoe Perches WARN("BRACES", 4731000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 4732f0a594c1SAndy Whitcroft } 4733f0a594c1SAndy Whitcroft } 4734f0a594c1SAndy Whitcroft 47350979ae66SJoe Perches# check for unnecessary blank lines around braces 473677b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 4737f8e58219SJoe Perches if (CHK("BRACES", 4738f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 4739f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 4740f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4741f8e58219SJoe Perches } 47420979ae66SJoe Perches } 474377b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 4744f8e58219SJoe Perches if (CHK("BRACES", 4745f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 4746f8e58219SJoe Perches $fix) { 4747f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 4748f8e58219SJoe Perches } 47490979ae66SJoe Perches } 47500979ae66SJoe Perches 47514a0df2efSAndy Whitcroft# no volatiles please 47526c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 47536c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 4754000d1cc1SJoe Perches WARN("VOLATILE", 4755000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 47564a0df2efSAndy Whitcroft } 47574a0df2efSAndy Whitcroft 47585e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 47595e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 47605e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 47615e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 476233acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 47635e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 47645e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 47655e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 47665e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 47675e4f6ba5SJoe Perches $fix && 47685e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 47695e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 47705e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 47715e4f6ba5SJoe Perches my $comma_close = ""; 47725e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 47735e4f6ba5SJoe Perches $comma_close = $1; 47745e4f6ba5SJoe Perches } 47755e4f6ba5SJoe Perches 47765e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 47775e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 47785e4f6ba5SJoe Perches my $fixedline = $prevrawline; 47795e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 47805e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 47815e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 47825e4f6ba5SJoe Perches $fixedline = $rawline; 47835e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 47845e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 47855e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 47865e4f6ba5SJoe Perches } 47875e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 47885e4f6ba5SJoe Perches } 47895e4f6ba5SJoe Perches } 47905e4f6ba5SJoe Perches 47915e4f6ba5SJoe Perches# check for missing a space in a string concatenation 47925e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 47935e4f6ba5SJoe Perches WARN('MISSING_SPACE', 47945e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 47955e4f6ba5SJoe Perches } 47965e4f6ba5SJoe Perches 47975e4f6ba5SJoe Perches# check for spaces before a quoted newline 47985e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 47995e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 48005e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 48015e4f6ba5SJoe Perches $fix) { 48025e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 48035e4f6ba5SJoe Perches } 48045e4f6ba5SJoe Perches 48055e4f6ba5SJoe Perches } 48065e4f6ba5SJoe Perches 4807f17dba4fSJoe Perches# concatenated string without spaces between elements 480833acb54aSJoe Perches if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) { 4809f17dba4fSJoe Perches CHK("CONCATENATED_STRING", 4810f17dba4fSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr); 4811f17dba4fSJoe Perches } 4812f17dba4fSJoe Perches 481390ad30e5SJoe Perches# uncoalesced string fragments 481433acb54aSJoe Perches if ($line =~ /$String\s*"/) { 481590ad30e5SJoe Perches WARN("STRING_FRAGMENTS", 481690ad30e5SJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr); 481790ad30e5SJoe Perches } 481890ad30e5SJoe Perches 48195e4f6ba5SJoe Perches# check for %L{u,d,i} in strings 48205e4f6ba5SJoe Perches my $string; 48215e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 48225e4f6ba5SJoe Perches $string = substr($rawline, $-[1], $+[1] - $-[1]); 48235e4f6ba5SJoe Perches $string =~ s/%%/__/g; 48245e4f6ba5SJoe Perches if ($string =~ /(?<!%)%L[udi]/) { 48255e4f6ba5SJoe Perches WARN("PRINTF_L", 48265e4f6ba5SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 48275e4f6ba5SJoe Perches last; 48285e4f6ba5SJoe Perches } 48295e4f6ba5SJoe Perches } 48305e4f6ba5SJoe Perches 48315e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 48325e4f6ba5SJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 48335e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 48345e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 48355e4f6ba5SJoe Perches } 48365e4f6ba5SJoe Perches 483700df344fSAndy Whitcroft# warn about #if 0 4838c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 4839000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 4840000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 4841de7d4f0eSAndy Whitcroft $herecurr); 48424a0df2efSAndy Whitcroft } 48434a0df2efSAndy Whitcroft 484403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 484503df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 4846100425deSJoe Perches my $tested = quotemeta($1); 4847100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 4848100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 4849100425deSJoe Perches my $func = $1; 4850100425deSJoe Perches if (WARN('NEEDLESS_IF', 4851100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 4852100425deSJoe Perches $fix) { 4853100425deSJoe Perches my $do_fix = 1; 4854100425deSJoe Perches my $leading_tabs = ""; 4855100425deSJoe Perches my $new_leading_tabs = ""; 4856100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 4857100425deSJoe Perches $leading_tabs = $1; 4858100425deSJoe Perches } else { 4859100425deSJoe Perches $do_fix = 0; 4860100425deSJoe Perches } 4861100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 4862100425deSJoe Perches $new_leading_tabs = $1; 4863100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 4864100425deSJoe Perches $do_fix = 0; 4865100425deSJoe Perches } 4866100425deSJoe Perches } else { 4867100425deSJoe Perches $do_fix = 0; 4868100425deSJoe Perches } 4869100425deSJoe Perches if ($do_fix) { 4870100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4871100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 4872100425deSJoe Perches } 4873100425deSJoe Perches } 48744c432a8fSGreg Kroah-Hartman } 48754c432a8fSGreg Kroah-Hartman } 4876f0a594c1SAndy Whitcroft 4877ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 4878ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 4879ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 4880ebfdc409SJoe Perches (defined $1 || defined $3) && 4881ebfdc409SJoe Perches $linenr > 3) { 4882ebfdc409SJoe Perches my $testval = $2; 4883ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 4884ebfdc409SJoe Perches 4885ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 4886ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 4887ebfdc409SJoe Perches 4888ebfdc409SJoe 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)/) { 4889ebfdc409SJoe Perches WARN("OOM_MESSAGE", 4890ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 4891ebfdc409SJoe Perches } 4892ebfdc409SJoe Perches } 4893ebfdc409SJoe Perches 4894f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 4895dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 4896f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 4897f78d98f6SJoe Perches my $level = $1; 4898f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 4899f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 4900f78d98f6SJoe Perches $fix) { 4901f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 4902f78d98f6SJoe Perches } 4903f78d98f6SJoe Perches } 4904f78d98f6SJoe Perches 4905abb08a53SJoe Perches# check for mask then right shift without a parentheses 4906abb08a53SJoe Perches if ($^V && $^V ge 5.10.0 && 4907abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 4908abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 4909abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 4910abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 4911abb08a53SJoe Perches } 4912abb08a53SJoe Perches 4913b75ac618SJoe Perches# check for pointer comparisons to NULL 4914b75ac618SJoe Perches if ($^V && $^V ge 5.10.0) { 4915b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 4916b75ac618SJoe Perches my $val = $1; 4917b75ac618SJoe Perches my $equal = "!"; 4918b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 4919b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 4920b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 4921b75ac618SJoe Perches $fix) { 4922b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 4923b75ac618SJoe Perches } 4924b75ac618SJoe Perches } 4925b75ac618SJoe Perches } 4926b75ac618SJoe Perches 49278716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 49288716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 49298716de38SJoe Perches my $attr = $1; 49308716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 49318716de38SJoe Perches my $ptr = $1; 49328716de38SJoe Perches my $var = $2; 49338716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 49348716de38SJoe Perches ERROR("MISPLACED_INIT", 49358716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 49368716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 49378716de38SJoe Perches WARN("MISPLACED_INIT", 49388716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 49398716de38SJoe Perches $fix) { 4940194f66fcSJoe 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; 49418716de38SJoe Perches } 49428716de38SJoe Perches } 49438716de38SJoe Perches } 49448716de38SJoe Perches 4945e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 4946e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 4947e970b884SJoe Perches my $attr = $1; 4948e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 4949e970b884SJoe Perches my $attr_prefix = $1; 4950e970b884SJoe Perches my $attr_type = $2; 4951e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 4952e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 4953e970b884SJoe Perches $fix) { 4954194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4955e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 4956e970b884SJoe Perches } 4957e970b884SJoe Perches } 4958e970b884SJoe Perches 4959e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 4960e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 4961e970b884SJoe Perches my $attr = $1; 4962e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 4963e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 4964e970b884SJoe Perches $fix) { 4965194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 4966e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 4967e970b884SJoe Perches $lead = rtrim($1); 4968e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 4969e970b884SJoe Perches $lead = "${lead}const "; 4970194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 4971e970b884SJoe Perches } 4972e970b884SJoe Perches } 4973e970b884SJoe Perches 4974c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 4975c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 4976c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 4977c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 4978c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 4979c17893c7SJoe Perches $fix) { 4980c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 4981c17893c7SJoe Perches } 4982c17893c7SJoe Perches } 4983c17893c7SJoe Perches 4984fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 4985fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 4986fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 4987fbdb8138SJoe Perches my $constant_func = $1; 4988fbdb8138SJoe Perches my $func = $constant_func; 4989fbdb8138SJoe Perches $func =~ s/^__constant_//; 4990fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 4991fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 4992fbdb8138SJoe Perches $fix) { 4993194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 4994fbdb8138SJoe Perches } 4995fbdb8138SJoe Perches } 4996fbdb8138SJoe Perches 49971a15a250SPatrick Pannuto# prefer usleep_range over udelay 499837581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 499943c1d77cSJoe Perches my $delay = $1; 50001a15a250SPatrick Pannuto # ignore udelay's < 10, however 500143c1d77cSJoe Perches if (! ($delay < 10) ) { 5002000d1cc1SJoe Perches CHK("USLEEP_RANGE", 500343c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 500443c1d77cSJoe Perches } 500543c1d77cSJoe Perches if ($delay > 2000) { 500643c1d77cSJoe Perches WARN("LONG_UDELAY", 500743c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 50081a15a250SPatrick Pannuto } 50091a15a250SPatrick Pannuto } 50101a15a250SPatrick Pannuto 501109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 501209ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 501309ef8725SPatrick Pannuto if ($1 < 20) { 5014000d1cc1SJoe Perches WARN("MSLEEP", 501543c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 501609ef8725SPatrick Pannuto } 501709ef8725SPatrick Pannuto } 501809ef8725SPatrick Pannuto 501936ec1939SJoe Perches# check for comparisons of jiffies 502036ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 502136ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 502236ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 502336ec1939SJoe Perches } 502436ec1939SJoe Perches 50259d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 50269d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 50279d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 50289d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 50299d7a34a5SJoe Perches } 50309d7a34a5SJoe Perches 503100df344fSAndy Whitcroft# warn about #ifdefs in C files 5032c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 503300df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 503400df344fSAndy Whitcroft# print "$herecurr"; 503500df344fSAndy Whitcroft# $clean = 0; 503600df344fSAndy Whitcroft# } 503700df344fSAndy Whitcroft 503822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5039c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 50403705ce5bSJoe Perches if (ERROR("SPACING", 50413705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 50423705ce5bSJoe Perches $fix) { 5043194f66fcSJoe Perches $fixed[$fixlinenr] =~ 50443705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 50453705ce5bSJoe Perches } 50463705ce5bSJoe Perches 504722f2a2efSAndy Whitcroft } 504822f2a2efSAndy Whitcroft 50494a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5050171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5051171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 50524a0df2efSAndy Whitcroft my $which = $1; 50534a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5054000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5055000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 50564a0df2efSAndy Whitcroft } 50574a0df2efSAndy Whitcroft } 50584a0df2efSAndy Whitcroft# check for memory barriers without a comment. 50594a0df2efSAndy Whitcroft if ($line =~ /\b(mb|rmb|wmb|read_barrier_depends|smp_mb|smp_rmb|smp_wmb|smp_read_barrier_depends)\(/) { 50604a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5061c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5062000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 50634a0df2efSAndy Whitcroft } 50644a0df2efSAndy Whitcroft } 50653ad81779SPaul E. McKenney 5066cb426e99SJoe Perches# check for waitqueue_active without a comment. 5067cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5068cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5069cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5070cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5071cb426e99SJoe Perches } 5072cb426e99SJoe Perches } 50733ad81779SPaul E. McKenney 50743ad81779SPaul E. McKenney# Check for expedited grace periods that interrupt non-idle non-nohz 50753ad81779SPaul E. McKenney# online CPUs. These expedited can therefore degrade real-time response 50763ad81779SPaul E. McKenney# if used carelessly, and should be avoided where not absolutely 50773ad81779SPaul E. McKenney# needed. It is always OK to use synchronize_rcu_expedited() and 50783ad81779SPaul E. McKenney# synchronize_sched_expedited() at boot time (before real-time applications 50793ad81779SPaul E. McKenney# start) and in error situations where real-time response is compromised in 50803ad81779SPaul E. McKenney# any case. Note that synchronize_srcu_expedited() does -not- interrupt 50813ad81779SPaul E. McKenney# other CPUs, so don't warn on uses of synchronize_srcu_expedited(). 50823ad81779SPaul E. McKenney# Of course, nothing comes for free, and srcu_read_lock() and 50833ad81779SPaul E. McKenney# srcu_read_unlock() do contain full memory barriers in payment for 50843ad81779SPaul E. McKenney# synchronize_srcu_expedited() non-interruption properties. 50853ad81779SPaul E. McKenney if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) { 50863ad81779SPaul E. McKenney WARN("EXPEDITED_RCU_GRACE_PERIOD", 50873ad81779SPaul E. McKenney "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr); 50883ad81779SPaul E. McKenney 50893ad81779SPaul E. McKenney } 50903ad81779SPaul E. McKenney 50914a0df2efSAndy Whitcroft# check of hardware specific defines 5092c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5093000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5094000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 50950a920b5bSAndy Whitcroft } 5096653d4876SAndy Whitcroft 5097d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 5098d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 5099000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5100000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 5101d4977c78STobias Klauser } 5102d4977c78STobias Klauser 5103de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5104de7d4f0eSAndy Whitcroft# storage class and type. 51059c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 51069c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5107000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5108000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5109de7d4f0eSAndy Whitcroft } 5110de7d4f0eSAndy Whitcroft 51118905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 51122b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 51132b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5114d5e616fcSJoe Perches if (WARN("INLINE", 5115d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5116d5e616fcSJoe Perches $fix) { 5117194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5118d5e616fcSJoe Perches 5119d5e616fcSJoe Perches } 51208905a67cSAndy Whitcroft } 51218905a67cSAndy Whitcroft 51223d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 51232b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 51242b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5125000d1cc1SJoe Perches WARN("PREFER_PACKED", 5126000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 51273d130fd0SJoe Perches } 51283d130fd0SJoe Perches 512939b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 51302b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 51312b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5132000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5133000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 513439b7e287SJoe Perches } 513539b7e287SJoe Perches 51365f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 51372b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 51382b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5139d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5140d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5141d5e616fcSJoe Perches $fix) { 5142194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5143d5e616fcSJoe Perches 5144d5e616fcSJoe Perches } 51455f14d3bdSJoe Perches } 51465f14d3bdSJoe Perches 51476061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 51482b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 51492b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5150d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5151d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5152d5e616fcSJoe Perches $fix) { 5153194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5154d5e616fcSJoe Perches } 51556061d949SJoe Perches } 51566061d949SJoe Perches 5157619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 5158619a908aSJoe Perches if ($^V && $^V ge 5.10.0 && 5159619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5160619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5161619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5162619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5163619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5164619a908aSJoe Perches } 5165619a908aSJoe Perches 5166e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/ 5167e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5168e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5169e6176fa4SJoe Perches my $type = $1; 5170e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5171e6176fa4SJoe Perches $type = $1; 5172e6176fa4SJoe Perches my $kernel_type = 'u'; 5173e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5174e6176fa4SJoe Perches $type =~ /(\d+)/; 5175e6176fa4SJoe Perches $kernel_type .= $1; 5176e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5177e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5178e6176fa4SJoe Perches $fix) { 5179e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5180e6176fa4SJoe Perches } 5181e6176fa4SJoe Perches } 5182e6176fa4SJoe Perches } 5183e6176fa4SJoe Perches 51848f53a9b8SJoe Perches# check for sizeof(&) 51858f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 5186000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 5187000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 51888f53a9b8SJoe Perches } 51898f53a9b8SJoe Perches 519066c80b60SJoe Perches# check for sizeof without parenthesis 519166c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 5192d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 5193d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 5194d5e616fcSJoe Perches $fix) { 5195194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 5196d5e616fcSJoe Perches } 519766c80b60SJoe Perches } 519866c80b60SJoe Perches 519988982feaSJoe Perches# check for struct spinlock declarations 520088982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 520188982feaSJoe Perches WARN("USE_SPINLOCK_T", 520288982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 520388982feaSJoe Perches } 520488982feaSJoe Perches 5205a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 520606668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 5207a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 5208caac1d5fSHeba Aamer $fmt =~ s/%%//g; 5209caac1d5fSHeba Aamer if ($fmt !~ /%/) { 5210d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 5211d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 5212d5e616fcSJoe Perches $fix) { 5213194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 5214d5e616fcSJoe Perches } 5215a6962d72SJoe Perches } 5216a6962d72SJoe Perches } 5217a6962d72SJoe Perches 5218554e165cSAndy Whitcroft# Check for misused memsets 5219d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 5220d1fe9c09SJoe Perches defined $stat && 52219e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 5222554e165cSAndy Whitcroft 5223d7c76ba7SJoe Perches my $ms_addr = $2; 5224d1fe9c09SJoe Perches my $ms_val = $7; 5225d1fe9c09SJoe Perches my $ms_size = $12; 5226d7c76ba7SJoe Perches 5227554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 5228554e165cSAndy Whitcroft ERROR("MEMSET", 5229d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 5230554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 5231554e165cSAndy Whitcroft WARN("MEMSET", 5232d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 5233d7c76ba7SJoe Perches } 5234d7c76ba7SJoe Perches } 5235d7c76ba7SJoe Perches 523698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 523798a9bba5SJoe Perches if ($^V && $^V ge 5.10.0 && 523810895d2cSMateusz Kulikowski defined $stat && 523910895d2cSMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 524098a9bba5SJoe Perches if (WARN("PREFER_ETHER_ADDR_COPY", 524110895d2cSMateusz Kulikowski "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 524298a9bba5SJoe Perches $fix) { 5243194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 524498a9bba5SJoe Perches } 524598a9bba5SJoe Perches } 524698a9bba5SJoe Perches 5247b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 5248b6117d17SMateusz Kulikowski if ($^V && $^V ge 5.10.0 && 5249b6117d17SMateusz Kulikowski defined $stat && 5250b6117d17SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5251b6117d17SMateusz Kulikowski WARN("PREFER_ETHER_ADDR_EQUAL", 5252b6117d17SMateusz Kulikowski "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 5253b6117d17SMateusz Kulikowski } 5254b6117d17SMateusz Kulikowski 52558617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 52568617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 52578617cd09SMateusz Kulikowski if ($^V && $^V ge 5.10.0 && 52588617cd09SMateusz Kulikowski defined $stat && 52598617cd09SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 52608617cd09SMateusz Kulikowski 52618617cd09SMateusz Kulikowski my $ms_val = $7; 52628617cd09SMateusz Kulikowski 52638617cd09SMateusz Kulikowski if ($ms_val =~ /^(?:0x|)0+$/i) { 52648617cd09SMateusz Kulikowski if (WARN("PREFER_ETH_ZERO_ADDR", 52658617cd09SMateusz Kulikowski "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 52668617cd09SMateusz Kulikowski $fix) { 52678617cd09SMateusz Kulikowski $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 52688617cd09SMateusz Kulikowski } 52698617cd09SMateusz Kulikowski } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 52708617cd09SMateusz Kulikowski if (WARN("PREFER_ETH_BROADCAST_ADDR", 52718617cd09SMateusz Kulikowski "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 52728617cd09SMateusz Kulikowski $fix) { 52738617cd09SMateusz Kulikowski $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 52748617cd09SMateusz Kulikowski } 52758617cd09SMateusz Kulikowski } 52768617cd09SMateusz Kulikowski } 52778617cd09SMateusz Kulikowski 5278d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 5279d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 5280d1fe9c09SJoe Perches defined $stat && 5281d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 5282d1fe9c09SJoe Perches if (defined $2 || defined $7) { 5283d7c76ba7SJoe Perches my $call = $1; 5284d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 5285d7c76ba7SJoe Perches my $arg1 = $3; 5286d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 5287d1fe9c09SJoe Perches my $arg2 = $8; 5288d7c76ba7SJoe Perches my $cast; 5289d7c76ba7SJoe Perches 5290d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 5291d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 5292d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 5293d7c76ba7SJoe Perches $cast = $cast1; 5294d7c76ba7SJoe Perches } else { 5295d7c76ba7SJoe Perches $cast = $cast2; 5296d7c76ba7SJoe Perches } 5297d7c76ba7SJoe Perches WARN("MINMAX", 5298d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 5299554e165cSAndy Whitcroft } 5300554e165cSAndy Whitcroft } 5301554e165cSAndy Whitcroft 53024a273195SJoe Perches# check usleep_range arguments 53034a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 53044a273195SJoe Perches defined $stat && 53054a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 53064a273195SJoe Perches my $min = $1; 53074a273195SJoe Perches my $max = $7; 53084a273195SJoe Perches if ($min eq $max) { 53094a273195SJoe Perches WARN("USLEEP_RANGE", 53104a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 53114a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 53124a273195SJoe Perches $min > $max) { 53134a273195SJoe Perches WARN("USLEEP_RANGE", 53144a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 53154a273195SJoe Perches } 53164a273195SJoe Perches } 53174a273195SJoe Perches 5318823b794cSJoe Perches# check for naked sscanf 5319823b794cSJoe Perches if ($^V && $^V ge 5.10.0 && 5320823b794cSJoe Perches defined $stat && 53216c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 5322823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 5323823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 5324823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 5325823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 5326823b794cSJoe Perches $lc = $lc + $linenr; 5327823b794cSJoe Perches my $stat_real = raw_line($linenr, 0); 5328823b794cSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 5329823b794cSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 5330823b794cSJoe Perches } 5331823b794cSJoe Perches WARN("NAKED_SSCANF", 5332823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 5333823b794cSJoe Perches } 5334823b794cSJoe Perches 5335afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 5336afc819abSJoe Perches if ($^V && $^V ge 5.10.0 && 5337afc819abSJoe Perches defined $stat && 5338afc819abSJoe Perches $line =~ /\bsscanf\b/) { 5339afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 5340afc819abSJoe Perches $lc = $lc + $linenr; 5341afc819abSJoe Perches my $stat_real = raw_line($linenr, 0); 5342afc819abSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 5343afc819abSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 5344afc819abSJoe Perches } 5345afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 5346afc819abSJoe Perches my $format = $6; 5347afc819abSJoe Perches my $count = $format =~ tr@%@%@; 5348afc819abSJoe Perches if ($count == 1 && 5349afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 5350afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 5351afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 5352afc819abSJoe Perches } 5353afc819abSJoe Perches } 5354afc819abSJoe Perches } 5355afc819abSJoe Perches 535670dc8a48SJoe Perches# check for new externs in .h files. 535770dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 535870dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 5359d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 536070dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 536170dc8a48SJoe Perches $fix) { 5362194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 536370dc8a48SJoe Perches } 536470dc8a48SJoe Perches } 536570dc8a48SJoe Perches 5366de7d4f0eSAndy Whitcroft# check for new externs in .c files. 5367171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 5368c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 5369171ae1a4SAndy Whitcroft { 5370c45dcabdSAndy Whitcroft my $function_name = $1; 5371c45dcabdSAndy Whitcroft my $paren_space = $2; 5372171ae1a4SAndy Whitcroft 5373171ae1a4SAndy Whitcroft my $s = $stat; 5374171ae1a4SAndy Whitcroft if (defined $cond) { 5375171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 5376171ae1a4SAndy Whitcroft } 5377c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 5378c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 5379c45dcabdSAndy Whitcroft { 5380000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 5381000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 5382de7d4f0eSAndy Whitcroft } 5383de7d4f0eSAndy Whitcroft 5384171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 5385000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 5386000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 5387171ae1a4SAndy Whitcroft } 53889c9ba34eSAndy Whitcroft 53899c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 53909c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 53919c9ba34eSAndy Whitcroft { 5392000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 5393000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 5394171ae1a4SAndy Whitcroft } 5395171ae1a4SAndy Whitcroft 5396de7d4f0eSAndy Whitcroft# checks for new __setup's 5397de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 5398de7d4f0eSAndy Whitcroft my $name = $1; 5399de7d4f0eSAndy Whitcroft 5400de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 5401000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 5402000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 5403de7d4f0eSAndy Whitcroft } 5404653d4876SAndy Whitcroft } 54059c0ca6f9SAndy Whitcroft 54069c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 5407caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 5408000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 5409000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 54109c0ca6f9SAndy Whitcroft } 541113214adfSAndy Whitcroft 5412a640d25cSJoe Perches# alloc style 5413a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 5414a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 5415a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 5416a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 5417a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 5418a640d25cSJoe Perches } 5419a640d25cSJoe Perches 542060a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 542160a55369SJoe Perches if ($^V && $^V ge 5.10.0 && 5422e367455aSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 542360a55369SJoe Perches my $oldfunc = $3; 542460a55369SJoe Perches my $a1 = $4; 542560a55369SJoe Perches my $a2 = $10; 542660a55369SJoe Perches my $newfunc = "kmalloc_array"; 542760a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 542860a55369SJoe Perches my $r1 = $a1; 542960a55369SJoe Perches my $r2 = $a2; 543060a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 543160a55369SJoe Perches $r1 = $a2; 543260a55369SJoe Perches $r2 = $a1; 543360a55369SJoe Perches } 5434e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 5435e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 5436e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 5437e367455aSJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) && 5438e367455aSJoe Perches $fix) { 5439194f66fcSJoe 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; 544060a55369SJoe Perches 544160a55369SJoe Perches } 544260a55369SJoe Perches } 544360a55369SJoe Perches } 544460a55369SJoe Perches 5445972fdea2SJoe Perches# check for krealloc arg reuse 5446972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 5447972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 5448972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 5449972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 5450972fdea2SJoe Perches } 5451972fdea2SJoe Perches 54525ce59ae0SJoe Perches# check for alloc argument mismatch 54535ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 54545ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 54555ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 54565ce59ae0SJoe Perches } 54575ce59ae0SJoe Perches 5458caf2a54fSJoe Perches# check for multiple semicolons 5459caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 5460d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 5461d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 5462d5e616fcSJoe Perches $fix) { 5463194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 5464d5e616fcSJoe Perches } 5465d1e2ad07SJoe Perches } 5466d1e2ad07SJoe Perches 54670ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit) 54680ab90191SJoe Perches if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 54690ab90191SJoe Perches my $ull = ""; 54700ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 54710ab90191SJoe Perches if (CHK("BIT_MACRO", 54720ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 54730ab90191SJoe Perches $fix) { 54740ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 54750ab90191SJoe Perches } 54760ab90191SJoe Perches } 54770ab90191SJoe Perches 5478e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 5479c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 5480c34c09a8SJoe Perches my $has_break = 0; 5481c34c09a8SJoe Perches my $has_statement = 0; 5482c34c09a8SJoe Perches my $count = 0; 5483c34c09a8SJoe Perches my $prevline = $linenr; 5484e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 5485c34c09a8SJoe Perches $prevline--; 5486c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 5487c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 5488c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 5489c34c09a8SJoe Perches next if ($fline =~ /^\-/); 5490c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 5491c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 5492c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 5493c34c09a8SJoe Perches $has_statement = 1; 5494c34c09a8SJoe Perches $count++; 5495c34c09a8SJoe Perches $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); 5496c34c09a8SJoe Perches } 5497c34c09a8SJoe Perches if (!$has_break && $has_statement) { 5498c34c09a8SJoe Perches WARN("MISSING_BREAK", 5499c34c09a8SJoe Perches "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); 5500c34c09a8SJoe Perches } 5501c34c09a8SJoe Perches } 5502c34c09a8SJoe Perches 5503d1e2ad07SJoe Perches# check for switch/default statements without a break; 5504d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 5505d1e2ad07SJoe Perches defined $stat && 5506d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 5507d1e2ad07SJoe Perches my $ctx = ''; 5508d1e2ad07SJoe Perches my $herectx = $here . "\n"; 5509d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 5510d1e2ad07SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 5511d1e2ad07SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 5512d1e2ad07SJoe Perches } 5513d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 5514d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 5515caf2a54fSJoe Perches } 5516caf2a54fSJoe Perches 551713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 5518d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 5519d5e616fcSJoe Perches if (WARN("USE_FUNC", 5520d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 5521d5e616fcSJoe Perches $fix) { 5522194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 5523d5e616fcSJoe Perches } 552413214adfSAndy Whitcroft } 5525773647a0SAndy Whitcroft 552662ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 552762ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 552862ec818fSJoe Perches ERROR("DATE_TIME", 552962ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 553062ec818fSJoe Perches } 553162ec818fSJoe Perches 55322c92488aSJoe Perches# check for use of yield() 55332c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 55342c92488aSJoe Perches WARN("YIELD", 55352c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 55362c92488aSJoe Perches } 55372c92488aSJoe Perches 5538179f8f40SJoe Perches# check for comparisons against true and false 5539179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 5540179f8f40SJoe Perches my $lead = $1; 5541179f8f40SJoe Perches my $arg = $2; 5542179f8f40SJoe Perches my $test = $3; 5543179f8f40SJoe Perches my $otype = $4; 5544179f8f40SJoe Perches my $trail = $5; 5545179f8f40SJoe Perches my $op = "!"; 5546179f8f40SJoe Perches 5547179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 5548179f8f40SJoe Perches 5549179f8f40SJoe Perches my $type = lc($otype); 5550179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 5551179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 5552179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 5553179f8f40SJoe Perches $op = ""; 5554179f8f40SJoe Perches } 5555179f8f40SJoe Perches 5556179f8f40SJoe Perches CHK("BOOL_COMPARISON", 5557179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 5558179f8f40SJoe Perches 5559179f8f40SJoe Perches## maybe suggesting a correct construct would better 5560179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 5561179f8f40SJoe Perches 5562179f8f40SJoe Perches } 5563179f8f40SJoe Perches } 5564179f8f40SJoe Perches 55654882720bSThomas Gleixner# check for semaphores initialized locked 55664882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 5567000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 5568000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 5569773647a0SAndy Whitcroft } 55706712d858SJoe Perches 557167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 557267d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 5573000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 557467d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 5575773647a0SAndy Whitcroft } 55766712d858SJoe Perches 5577ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 5578f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 5579000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 5580ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 5581f3db6639SMichael Ellerman } 55826712d858SJoe Perches 55830f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 55840f3c5aabSJoe Perches my $const_structs = qr{ 55850f3c5aabSJoe Perches acpi_dock_ops| 558679404849SEmese Revfy address_space_operations| 558779404849SEmese Revfy backlight_ops| 558879404849SEmese Revfy block_device_operations| 558979404849SEmese Revfy dentry_operations| 559079404849SEmese Revfy dev_pm_ops| 559179404849SEmese Revfy dma_map_ops| 559279404849SEmese Revfy extent_io_ops| 559379404849SEmese Revfy file_lock_operations| 559479404849SEmese Revfy file_operations| 559579404849SEmese Revfy hv_ops| 559679404849SEmese Revfy ide_dma_ops| 559779404849SEmese Revfy intel_dvo_dev_ops| 559879404849SEmese Revfy item_operations| 559979404849SEmese Revfy iwl_ops| 560079404849SEmese Revfy kgdb_arch| 560179404849SEmese Revfy kgdb_io| 560279404849SEmese Revfy kset_uevent_ops| 560379404849SEmese Revfy lock_manager_operations| 560479404849SEmese Revfy microcode_ops| 560579404849SEmese Revfy mtrr_ops| 560679404849SEmese Revfy neigh_ops| 560779404849SEmese Revfy nlmsvc_binding| 56080f3c5aabSJoe Perches of_device_id| 560979404849SEmese Revfy pci_raw_ops| 561079404849SEmese Revfy pipe_buf_operations| 561179404849SEmese Revfy platform_hibernation_ops| 561279404849SEmese Revfy platform_suspend_ops| 561379404849SEmese Revfy proto_ops| 561479404849SEmese Revfy rpc_pipe_ops| 561579404849SEmese Revfy seq_operations| 561679404849SEmese Revfy snd_ac97_build_ops| 561779404849SEmese Revfy soc_pcmcia_socket_ops| 561879404849SEmese Revfy stacktrace_ops| 561979404849SEmese Revfy sysfs_ops| 562079404849SEmese Revfy tty_operations| 56216d07d01bSJoe Perches uart_ops| 562279404849SEmese Revfy usb_mon_operations| 562379404849SEmese Revfy wd_ops}x; 56246903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 56250f3c5aabSJoe Perches $line =~ /\bstruct\s+($const_structs)\b/) { 5626000d1cc1SJoe Perches WARN("CONST_STRUCT", 5627000d1cc1SJoe Perches "struct $1 should normally be const\n" . 56286903ffb2SAndy Whitcroft $herecurr); 56292b6db5cbSAndy Whitcroft } 5630773647a0SAndy Whitcroft 5631773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 5632773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 5633773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 5634c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 5635c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 5636171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 5637171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 5638171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 5639773647a0SAndy Whitcroft { 5640000d1cc1SJoe Perches WARN("NR_CPUS", 5641000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 5642773647a0SAndy Whitcroft } 56439c9ba34eSAndy Whitcroft 564452ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 564552ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 564652ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 564752ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 564852ea8506SJoe Perches } 564952ea8506SJoe Perches 5650acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 5651acd9362cSJoe Perches if ($^V && $^V ge 5.10.0 && 5652acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 5653acd9362cSJoe Perches WARN("LIKELY_MISUSE", 5654acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 5655acd9362cSJoe Perches } 5656acd9362cSJoe Perches 5657691d77b6SAndy Whitcroft# whine mightly about in_atomic 5658691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 5659691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 5660000d1cc1SJoe Perches ERROR("IN_ATOMIC", 5661000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 5662f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 5663000d1cc1SJoe Perches WARN("IN_ATOMIC", 5664000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 5665691d77b6SAndy Whitcroft } 5666691d77b6SAndy Whitcroft } 56671704f47bSPeter Zijlstra 56681704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 56691704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 56701704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 56711704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 56721704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 56731704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 5674000d1cc1SJoe Perches ERROR("LOCKDEP", 5675000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 56761704f47bSPeter Zijlstra } 56771704f47bSPeter Zijlstra } 567888f8831cSDave Jones 5679b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 5680b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 5681000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 5682000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 568388f8831cSDave Jones } 56842435880fSJoe Perches 5685515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 5686515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 5687515a235eSJoe Perches if ($^V && $^V ge 5.10.0 && 5688515a235eSJoe Perches $line =~ /$mode_perms_search/) { 56892435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 56902435880fSJoe Perches my $func = $entry->[0]; 56912435880fSJoe Perches my $arg_pos = $entry->[1]; 56922435880fSJoe Perches 56932435880fSJoe Perches my $skip_args = ""; 56942435880fSJoe Perches if ($arg_pos > 1) { 56952435880fSJoe Perches $arg_pos--; 56962435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 56972435880fSJoe Perches } 56982435880fSJoe Perches my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; 5699515a235eSJoe Perches if ($line =~ /$test/) { 57002435880fSJoe Perches my $val = $1; 57012435880fSJoe Perches $val = $6 if ($skip_args ne ""); 57022435880fSJoe Perches 57031727cc70SJoe Perches if ($val !~ /^0$/ && 57041727cc70SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 57051727cc70SJoe Perches length($val) ne 4)) { 57062435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 57071727cc70SJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); 5708c0a5c898SJoe Perches } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) { 5709c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 5710c0a5c898SJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 57112435880fSJoe Perches } 57122435880fSJoe Perches } 57132435880fSJoe Perches } 571413214adfSAndy Whitcroft } 57155a6d20ceSBjorn Andersson 57165a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 57175a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 57185a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 57195a6d20ceSBjorn Andersson my $valid_licenses = qr{ 57205a6d20ceSBjorn Andersson GPL| 57215a6d20ceSBjorn Andersson GPL\ v2| 57225a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 57235a6d20ceSBjorn Andersson Dual\ BSD/GPL| 57245a6d20ceSBjorn Andersson Dual\ MIT/GPL| 57255a6d20ceSBjorn Andersson Dual\ MPL/GPL| 57265a6d20ceSBjorn Andersson Proprietary 57275a6d20ceSBjorn Andersson }x; 57285a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 57295a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 57305a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 57315a6d20ceSBjorn Andersson } 57325a6d20ceSBjorn Andersson } 5733515a235eSJoe Perches } 573413214adfSAndy Whitcroft 573513214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 573613214adfSAndy Whitcroft # so just keep quiet. 573713214adfSAndy Whitcroft if ($#rawlines == -1) { 573813214adfSAndy Whitcroft exit(0); 57390a920b5bSAndy Whitcroft } 57400a920b5bSAndy Whitcroft 57418905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 57428905a67cSAndy Whitcroft # things that appear to be patches. 57438905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 57448905a67cSAndy Whitcroft exit(0); 57458905a67cSAndy Whitcroft } 57468905a67cSAndy Whitcroft 57478905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 57488905a67cSAndy Whitcroft # just keep quiet. 57498905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 57508905a67cSAndy Whitcroft exit(0); 57518905a67cSAndy Whitcroft } 57528905a67cSAndy Whitcroft 575306330fc4SJoe Perches if (!$is_patch && $file !~ /cover-letter\.patch$/) { 5754000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 5755000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 57560a920b5bSAndy Whitcroft } 575734d8815fSJoe Perches if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) { 5758000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 5759000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 57600a920b5bSAndy Whitcroft } 57610a920b5bSAndy Whitcroft 5762f0a594c1SAndy Whitcroft print report_dump(); 576313214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 576413214adfSAndy Whitcroft print "$filename " if ($summary_file); 57656c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 57666c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 57676c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 57686c72ffaaSAndy Whitcroft } 57698905a67cSAndy Whitcroft 5770d2c0a235SAndy Whitcroft if ($quiet == 0) { 5771d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 5772d2c0a235SAndy Whitcroft # then suggest that. 5773d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 5774b0781216SMike Frysinger $rpt_cleaners = 0; 5775d8469f16SJoe Perches print << "EOM" 5776d8469f16SJoe Perches 5777d8469f16SJoe PerchesNOTE: Whitespace errors detected. 5778d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 5779d8469f16SJoe PerchesEOM 5780d2c0a235SAndy Whitcroft } 5781d2c0a235SAndy Whitcroft } 5782d2c0a235SAndy Whitcroft 5783d752fcc8SJoe Perches if ($clean == 0 && $fix && 5784d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 5785d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 57869624b8d6SJoe Perches my $newfile = $filename; 57879624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 57883705ce5bSJoe Perches my $linecount = 0; 57893705ce5bSJoe Perches my $f; 57903705ce5bSJoe Perches 5791d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 5792d752fcc8SJoe Perches 57933705ce5bSJoe Perches open($f, '>', $newfile) 57943705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 57953705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 57963705ce5bSJoe Perches $linecount++; 57973705ce5bSJoe Perches if ($file) { 57983705ce5bSJoe Perches if ($linecount > 3) { 57993705ce5bSJoe Perches $fixed_line =~ s/^\+//; 58003705ce5bSJoe Perches print $f $fixed_line . "\n"; 58013705ce5bSJoe Perches } 58023705ce5bSJoe Perches } else { 58033705ce5bSJoe Perches print $f $fixed_line . "\n"; 58043705ce5bSJoe Perches } 58053705ce5bSJoe Perches } 58063705ce5bSJoe Perches close($f); 58073705ce5bSJoe Perches 58083705ce5bSJoe Perches if (!$quiet) { 58093705ce5bSJoe Perches print << "EOM"; 5810d8469f16SJoe Perches 58113705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 58123705ce5bSJoe Perches 58133705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 58143705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 58153705ce5bSJoe Perches 58163705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 58173705ce5bSJoe PerchesNo warranties, expressed or implied... 58183705ce5bSJoe PerchesEOM 58193705ce5bSJoe Perches } 58203705ce5bSJoe Perches } 58213705ce5bSJoe Perches 5822d8469f16SJoe Perches if ($quiet == 0) { 5823d8469f16SJoe Perches print "\n"; 5824d8469f16SJoe Perches if ($clean == 1) { 5825d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 5826d8469f16SJoe Perches } else { 5827d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 58280a920b5bSAndy Whitcroft } 58290a920b5bSAndy Whitcroft } 58300a920b5bSAndy Whitcroft return $clean; 58310a920b5bSAndy Whitcroft} 5832