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; 304a593c34SDu, Changbinmy $git = 0; 31*0dea9f1eSJoe Perchesmy %git_commits = (); 326c72ffaaSAndy Whitcroftmy $check = 0; 332ac73b4fSJoe Perchesmy $check_orig = 0; 348905a67cSAndy Whitcroftmy $summary = 1; 358905a67cSAndy Whitcroftmy $mailback = 0; 3613214adfSAndy Whitcroftmy $summary_file = 0; 37000d1cc1SJoe Perchesmy $show_types = 0; 383beb42ecSJoe Perchesmy $list_types = 0; 393705ce5bSJoe Perchesmy $fix = 0; 409624b8d6SJoe Perchesmy $fix_inplace = 0; 416c72ffaaSAndy Whitcroftmy $root; 42c2fdda0dSAndy Whitcroftmy %debug; 433445686aSJoe Perchesmy %camelcase = (); 4491bfe484SJoe Perchesmy %use_type = (); 4591bfe484SJoe Perchesmy @use = (); 4691bfe484SJoe Perchesmy %ignore_type = (); 47000d1cc1SJoe Perchesmy @ignore = (); 4877f5b10aSHannes Edermy $help = 0; 49000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf"; 506cd7f386SJoe Perchesmy $max_line_length = 80; 51d62a201fSDave Hansenmy $ignore_perl_version = 0; 52d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0; 5356193274SVadim Bendeburymy $min_conf_desc_length = 4; 5466b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt"; 55ebfd7d62SJoe Perchesmy $codespell = 0; 56f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt"; 5757230297SJoe Perchesmy $color = 1; 5877f5b10aSHannes Eder 5977f5b10aSHannes Edersub help { 6077f5b10aSHannes Eder my ($exitcode) = @_; 6177f5b10aSHannes Eder 6277f5b10aSHannes Eder print << "EOM"; 6377f5b10aSHannes EderUsage: $P [OPTION]... [FILE]... 6477f5b10aSHannes EderVersion: $V 6577f5b10aSHannes Eder 6677f5b10aSHannes EderOptions: 6777f5b10aSHannes Eder -q, --quiet quiet 6877f5b10aSHannes Eder --no-tree run without a kernel tree 6977f5b10aSHannes Eder --no-signoff do not check for 'Signed-off-by' line 7077f5b10aSHannes Eder --patch treat FILE as patchfile (default) 7177f5b10aSHannes Eder --emacs emacs compile window format 7277f5b10aSHannes Eder --terse one line per report 7334d8815fSJoe Perches --showfile emit diffed file position, not input file position 744a593c34SDu, Changbin -g, --git treat FILE as a single commit or git revision range 754a593c34SDu, Changbin single git commit with: 764a593c34SDu, Changbin <rev> 774a593c34SDu, Changbin <rev>^ 784a593c34SDu, Changbin <rev>~n 794a593c34SDu, Changbin multiple git commits with: 804a593c34SDu, Changbin <rev1>..<rev2> 814a593c34SDu, Changbin <rev1>...<rev2> 824a593c34SDu, Changbin <rev>-<count> 834a593c34SDu, Changbin git merges are ignored 8477f5b10aSHannes Eder -f, --file treat FILE as regular source file 8577f5b10aSHannes Eder --subjective, --strict enable more subjective tests 863beb42ecSJoe Perches --list-types list the possible message types 8791bfe484SJoe Perches --types TYPE(,TYPE2...) show only these comma separated message types 88000d1cc1SJoe Perches --ignore TYPE(,TYPE2...) ignore various comma separated message types 893beb42ecSJoe Perches --show-types show the specific message type in the output 906cd7f386SJoe Perches --max-line-length=n set the maximum line length, if exceeded, warn 9156193274SVadim Bendebury --min-conf-desc-length=n set the min description length, if shorter, warn 9277f5b10aSHannes Eder --root=PATH PATH to the kernel tree root 9377f5b10aSHannes Eder --no-summary suppress the per-file summary 9477f5b10aSHannes Eder --mailback only produce a report in case of warnings/errors 9577f5b10aSHannes Eder --summary-file include the filename in summary 9677f5b10aSHannes Eder --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of 9777f5b10aSHannes Eder 'values', 'possible', 'type', and 'attr' (default 9877f5b10aSHannes Eder is all off) 9977f5b10aSHannes Eder --test-only=WORD report only warnings/errors containing WORD 10077f5b10aSHannes Eder literally 1013705ce5bSJoe Perches --fix EXPERIMENTAL - may create horrible results 1023705ce5bSJoe Perches If correctable single-line errors exist, create 1033705ce5bSJoe Perches "<inputfile>.EXPERIMENTAL-checkpatch-fixes" 1043705ce5bSJoe Perches with potential errors corrected to the preferred 1053705ce5bSJoe Perches checkpatch style 1069624b8d6SJoe Perches --fix-inplace EXPERIMENTAL - may create horrible results 1079624b8d6SJoe Perches Is the same as --fix, but overwrites the input 1089624b8d6SJoe Perches file. It's your fault if there's no backup or git 109d62a201fSDave Hansen --ignore-perl-version override checking of perl version. expect 110d62a201fSDave Hansen runtime errors. 111ebfd7d62SJoe Perches --codespell Use the codespell dictionary for spelling/typos 112f1a63678SMaxim Uvarov (default:/usr/share/codespell/dictionary.txt) 113ebfd7d62SJoe Perches --codespellfile Use this codespell dictionary 11457230297SJoe Perches --color Use colors when output is STDOUT (default: on) 11577f5b10aSHannes Eder -h, --help, --version display this help and exit 11677f5b10aSHannes Eder 11777f5b10aSHannes EderWhen FILE is - read standard input. 11877f5b10aSHannes EderEOM 11977f5b10aSHannes Eder 12077f5b10aSHannes Eder exit($exitcode); 12177f5b10aSHannes Eder} 12277f5b10aSHannes Eder 1233beb42ecSJoe Perchessub uniq { 1243beb42ecSJoe Perches my %seen; 1253beb42ecSJoe Perches return grep { !$seen{$_}++ } @_; 1263beb42ecSJoe Perches} 1273beb42ecSJoe Perches 1283beb42ecSJoe Perchessub list_types { 1293beb42ecSJoe Perches my ($exitcode) = @_; 1303beb42ecSJoe Perches 1313beb42ecSJoe Perches my $count = 0; 1323beb42ecSJoe Perches 1333beb42ecSJoe Perches local $/ = undef; 1343beb42ecSJoe Perches 1353beb42ecSJoe Perches open(my $script, '<', abs_path($P)) or 1363beb42ecSJoe Perches die "$P: Can't read '$P' $!\n"; 1373beb42ecSJoe Perches 1383beb42ecSJoe Perches my $text = <$script>; 1393beb42ecSJoe Perches close($script); 1403beb42ecSJoe Perches 1413beb42ecSJoe Perches my @types = (); 1423beb42ecSJoe Perches for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) { 1433beb42ecSJoe Perches push (@types, $_); 1443beb42ecSJoe Perches } 1453beb42ecSJoe Perches @types = sort(uniq(@types)); 1463beb42ecSJoe Perches print("#\tMessage type\n\n"); 1473beb42ecSJoe Perches foreach my $type (@types) { 1483beb42ecSJoe Perches print(++$count . "\t" . $type . "\n"); 1493beb42ecSJoe Perches } 1503beb42ecSJoe Perches 1513beb42ecSJoe Perches exit($exitcode); 1523beb42ecSJoe Perches} 1533beb42ecSJoe Perches 154000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file); 155000d1cc1SJoe Perchesif (-f $conf) { 156000d1cc1SJoe Perches my @conf_args; 157000d1cc1SJoe Perches open(my $conffile, '<', "$conf") 158000d1cc1SJoe Perches or warn "$P: Can't find a readable $configuration_file file $!\n"; 159000d1cc1SJoe Perches 160000d1cc1SJoe Perches while (<$conffile>) { 161000d1cc1SJoe Perches my $line = $_; 162000d1cc1SJoe Perches 163000d1cc1SJoe Perches $line =~ s/\s*\n?$//g; 164000d1cc1SJoe Perches $line =~ s/^\s*//g; 165000d1cc1SJoe Perches $line =~ s/\s+/ /g; 166000d1cc1SJoe Perches 167000d1cc1SJoe Perches next if ($line =~ m/^\s*#/); 168000d1cc1SJoe Perches next if ($line =~ m/^\s*$/); 169000d1cc1SJoe Perches 170000d1cc1SJoe Perches my @words = split(" ", $line); 171000d1cc1SJoe Perches foreach my $word (@words) { 172000d1cc1SJoe Perches last if ($word =~ m/^#/); 173000d1cc1SJoe Perches push (@conf_args, $word); 174000d1cc1SJoe Perches } 175000d1cc1SJoe Perches } 176000d1cc1SJoe Perches close($conffile); 177000d1cc1SJoe Perches unshift(@ARGV, @conf_args) if @conf_args; 178000d1cc1SJoe Perches} 179000d1cc1SJoe Perches 1800a920b5bSAndy WhitcroftGetOptions( 1816c72ffaaSAndy Whitcroft 'q|quiet+' => \$quiet, 1820a920b5bSAndy Whitcroft 'tree!' => \$tree, 1830a920b5bSAndy Whitcroft 'signoff!' => \$chk_signoff, 1840a920b5bSAndy Whitcroft 'patch!' => \$chk_patch, 1856c72ffaaSAndy Whitcroft 'emacs!' => \$emacs, 1868905a67cSAndy Whitcroft 'terse!' => \$terse, 18734d8815fSJoe Perches 'showfile!' => \$showfile, 18877f5b10aSHannes Eder 'f|file!' => \$file, 1894a593c34SDu, Changbin 'g|git!' => \$git, 1906c72ffaaSAndy Whitcroft 'subjective!' => \$check, 1916c72ffaaSAndy Whitcroft 'strict!' => \$check, 192000d1cc1SJoe Perches 'ignore=s' => \@ignore, 19391bfe484SJoe Perches 'types=s' => \@use, 194000d1cc1SJoe Perches 'show-types!' => \$show_types, 1953beb42ecSJoe Perches 'list-types!' => \$list_types, 1966cd7f386SJoe Perches 'max-line-length=i' => \$max_line_length, 19756193274SVadim Bendebury 'min-conf-desc-length=i' => \$min_conf_desc_length, 1986c72ffaaSAndy Whitcroft 'root=s' => \$root, 1998905a67cSAndy Whitcroft 'summary!' => \$summary, 2008905a67cSAndy Whitcroft 'mailback!' => \$mailback, 20113214adfSAndy Whitcroft 'summary-file!' => \$summary_file, 2023705ce5bSJoe Perches 'fix!' => \$fix, 2039624b8d6SJoe Perches 'fix-inplace!' => \$fix_inplace, 204d62a201fSDave Hansen 'ignore-perl-version!' => \$ignore_perl_version, 205c2fdda0dSAndy Whitcroft 'debug=s' => \%debug, 206773647a0SAndy Whitcroft 'test-only=s' => \$tst_only, 207ebfd7d62SJoe Perches 'codespell!' => \$codespell, 208ebfd7d62SJoe Perches 'codespellfile=s' => \$codespellfile, 20957230297SJoe Perches 'color!' => \$color, 21077f5b10aSHannes Eder 'h|help' => \$help, 21177f5b10aSHannes Eder 'version' => \$help 21277f5b10aSHannes Eder) or help(1); 21377f5b10aSHannes Eder 21477f5b10aSHannes Ederhelp(0) if ($help); 2150a920b5bSAndy Whitcroft 2163beb42ecSJoe Percheslist_types(0) if ($list_types); 2173beb42ecSJoe Perches 2189624b8d6SJoe Perches$fix = 1 if ($fix_inplace); 2192ac73b4fSJoe Perches$check_orig = $check; 2209624b8d6SJoe Perches 2210a920b5bSAndy Whitcroftmy $exit = 0; 2220a920b5bSAndy Whitcroft 223d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) { 224d62a201fSDave Hansen printf "$P: requires at least perl version %vd\n", $minimum_perl_version; 225d62a201fSDave Hansen if (!$ignore_perl_version) { 226d62a201fSDave Hansen exit(1); 227d62a201fSDave Hansen } 228d62a201fSDave Hansen} 229d62a201fSDave Hansen 2300a920b5bSAndy Whitcroftif ($#ARGV < 0) { 23177f5b10aSHannes Eder print "$P: no input files\n"; 2320a920b5bSAndy Whitcroft exit(1); 2330a920b5bSAndy Whitcroft} 2340a920b5bSAndy Whitcroft 23591bfe484SJoe Perchessub hash_save_array_words { 23691bfe484SJoe Perches my ($hashRef, $arrayRef) = @_; 23791bfe484SJoe Perches 23891bfe484SJoe Perches my @array = split(/,/, join(',', @$arrayRef)); 23991bfe484SJoe Perches foreach my $word (@array) { 240000d1cc1SJoe Perches $word =~ s/\s*\n?$//g; 241000d1cc1SJoe Perches $word =~ s/^\s*//g; 242000d1cc1SJoe Perches $word =~ s/\s+/ /g; 243000d1cc1SJoe Perches $word =~ tr/[a-z]/[A-Z]/; 244000d1cc1SJoe Perches 245000d1cc1SJoe Perches next if ($word =~ m/^\s*#/); 246000d1cc1SJoe Perches next if ($word =~ m/^\s*$/); 247000d1cc1SJoe Perches 24891bfe484SJoe Perches $hashRef->{$word}++; 249000d1cc1SJoe Perches } 25091bfe484SJoe Perches} 25191bfe484SJoe Perches 25291bfe484SJoe Perchessub hash_show_words { 25391bfe484SJoe Perches my ($hashRef, $prefix) = @_; 25491bfe484SJoe Perches 2553c816e49SJoe Perches if (keys %$hashRef) { 256d8469f16SJoe Perches print "\nNOTE: $prefix message types:"; 25758cb3cf6SJoe Perches foreach my $word (sort keys %$hashRef) { 25891bfe484SJoe Perches print " $word"; 25991bfe484SJoe Perches } 260d8469f16SJoe Perches print "\n"; 26191bfe484SJoe Perches } 26291bfe484SJoe Perches} 26391bfe484SJoe Perches 26491bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore); 26591bfe484SJoe Percheshash_save_array_words(\%use_type, \@use); 266000d1cc1SJoe Perches 267c2fdda0dSAndy Whitcroftmy $dbg_values = 0; 268c2fdda0dSAndy Whitcroftmy $dbg_possible = 0; 2697429c690SAndy Whitcroftmy $dbg_type = 0; 270a1ef277eSAndy Whitcroftmy $dbg_attr = 0; 271c2fdda0dSAndy Whitcroftfor my $key (keys %debug) { 27221caa13cSAndy Whitcroft ## no critic 27321caa13cSAndy Whitcroft eval "\${dbg_$key} = '$debug{$key}';"; 27421caa13cSAndy Whitcroft die "$@" if ($@); 275c2fdda0dSAndy Whitcroft} 276c2fdda0dSAndy Whitcroft 277d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0; 278d2c0a235SAndy Whitcroft 2798905a67cSAndy Whitcroftif ($terse) { 2808905a67cSAndy Whitcroft $emacs = 1; 2818905a67cSAndy Whitcroft $quiet++; 2828905a67cSAndy Whitcroft} 2838905a67cSAndy Whitcroft 2846c72ffaaSAndy Whitcroftif ($tree) { 2856c72ffaaSAndy Whitcroft if (defined $root) { 2866c72ffaaSAndy Whitcroft if (!top_of_kernel_tree($root)) { 2876c72ffaaSAndy Whitcroft die "$P: $root: --root does not point at a valid tree\n"; 2886c72ffaaSAndy Whitcroft } 2896c72ffaaSAndy Whitcroft } else { 2906c72ffaaSAndy Whitcroft if (top_of_kernel_tree('.')) { 2916c72ffaaSAndy Whitcroft $root = '.'; 2926c72ffaaSAndy Whitcroft } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ && 2936c72ffaaSAndy Whitcroft top_of_kernel_tree($1)) { 2946c72ffaaSAndy Whitcroft $root = $1; 2956c72ffaaSAndy Whitcroft } 2966c72ffaaSAndy Whitcroft } 2976c72ffaaSAndy Whitcroft 2986c72ffaaSAndy Whitcroft if (!defined $root) { 2990a920b5bSAndy Whitcroft print "Must be run from the top-level dir. of a kernel tree\n"; 3000a920b5bSAndy Whitcroft exit(2); 3010a920b5bSAndy Whitcroft } 3026c72ffaaSAndy Whitcroft} 3036c72ffaaSAndy Whitcroft 3046c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0; 3056c72ffaaSAndy Whitcroft 3062ceb532bSAndy Whitcroftour $Ident = qr{ 3072ceb532bSAndy Whitcroft [A-Za-z_][A-Za-z\d_]* 3082ceb532bSAndy Whitcroft (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)* 3092ceb532bSAndy Whitcroft }x; 3106c72ffaaSAndy Whitcroftour $Storage = qr{extern|static|asmlinkage}; 3116c72ffaaSAndy Whitcroftour $Sparse = qr{ 3126c72ffaaSAndy Whitcroft __user| 3136c72ffaaSAndy Whitcroft __kernel| 3146c72ffaaSAndy Whitcroft __force| 3156c72ffaaSAndy Whitcroft __iomem| 31654507b51SJoe Perches __pmem| 3176c72ffaaSAndy Whitcroft __must_check| 3186c72ffaaSAndy Whitcroft __init_refok| 319417495edSAndy Whitcroft __kprobes| 320165e72a6SSven Eckelmann __ref| 321ad315455SBoqun Feng __rcu| 322ad315455SBoqun Feng __private 3236c72ffaaSAndy Whitcroft }x; 324e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)}; 325e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)}; 326e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)}; 327e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)}; 328e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit}; 3298716de38SJoe Perches 33052131292SWolfram Sang# Notes to $Attribute: 33152131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check 3326c72ffaaSAndy Whitcroftour $Attribute = qr{ 3336c72ffaaSAndy Whitcroft const| 33403f1df7dSJoe Perches __percpu| 33503f1df7dSJoe Perches __nocast| 33603f1df7dSJoe Perches __safe| 33703f1df7dSJoe Perches __bitwise__| 33803f1df7dSJoe Perches __packed__| 33903f1df7dSJoe Perches __packed2__| 34003f1df7dSJoe Perches __naked| 34103f1df7dSJoe Perches __maybe_unused| 34203f1df7dSJoe Perches __always_unused| 34303f1df7dSJoe Perches __noreturn| 34403f1df7dSJoe Perches __used| 34503f1df7dSJoe Perches __cold| 346e23ef1f3SJoe Perches __pure| 34703f1df7dSJoe Perches __noclone| 34803f1df7dSJoe Perches __deprecated| 3496c72ffaaSAndy Whitcroft __read_mostly| 3506c72ffaaSAndy Whitcroft __kprobes| 3518716de38SJoe Perches $InitAttribute| 35224e1d81aSAndy Whitcroft ____cacheline_aligned| 35324e1d81aSAndy Whitcroft ____cacheline_aligned_in_smp| 3545fe3af11SAndy Whitcroft ____cacheline_internodealigned_in_smp| 3555fe3af11SAndy Whitcroft __weak 3566c72ffaaSAndy Whitcroft }x; 357c45dcabdSAndy Whitcroftour $Modifier; 35891cb5195SJoe Perchesour $Inline = qr{inline|__always_inline|noinline|__inline|__inline__}; 3596c72ffaaSAndy Whitcroftour $Member = qr{->$Ident|\.$Ident|\[[^]]*\]}; 3606c72ffaaSAndy Whitcroftour $Lval = qr{$Ident(?:$Member)*}; 3616c72ffaaSAndy Whitcroft 36295e2c602SJoe Perchesour $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u}; 36395e2c602SJoe Perchesour $Binary = qr{(?i)0b[01]+$Int_type?}; 36495e2c602SJoe Perchesour $Hex = qr{(?i)0x[0-9a-f]+$Int_type?}; 36595e2c602SJoe Perchesour $Int = qr{[0-9]+$Int_type?}; 3662435880fSJoe Perchesour $Octal = qr{0[0-7]+$Int_type?}; 367c0a5c898SJoe Perchesour $String = qr{"[X\t]*"}; 368326b1ffcSJoe Perchesour $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?}; 369326b1ffcSJoe Perchesour $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?}; 370326b1ffcSJoe Perchesour $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?}; 37174349bccSJoe Perchesour $Float = qr{$Float_hex|$Float_dec|$Float_int}; 3722435880fSJoe Perchesour $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int}; 373326b1ffcSJoe Perchesour $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=}; 374447432f3SJoe Perchesour $Compare = qr{<=|>=|==|!=|<|(?<!-)>}; 37523f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%}; 3766c72ffaaSAndy Whitcroftour $Operators = qr{ 3776c72ffaaSAndy Whitcroft <=|>=|==|!=| 3786c72ffaaSAndy Whitcroft =>|->|<<|>>|<|>|!|~| 37923f780c9SJoe Perches &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic 3806c72ffaaSAndy Whitcroft }x; 3816c72ffaaSAndy Whitcroft 38291cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x; 38391cb5195SJoe Perches 384ab7e23f3SJoe Perchesour $BasicType; 3858905a67cSAndy Whitcroftour $NonptrType; 3861813087dSJoe Perchesour $NonptrTypeMisordered; 3878716de38SJoe Perchesour $NonptrTypeWithAttr; 3888905a67cSAndy Whitcroftour $Type; 3891813087dSJoe Perchesour $TypeMisordered; 3908905a67cSAndy Whitcroftour $Declare; 3911813087dSJoe Perchesour $DeclareMisordered; 3928905a67cSAndy Whitcroft 39315662b3eSJoe Perchesour $NON_ASCII_UTF8 = qr{ 39415662b3eSJoe Perches [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 395171ae1a4SAndy Whitcroft | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 396171ae1a4SAndy Whitcroft | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 397171ae1a4SAndy Whitcroft | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 398171ae1a4SAndy Whitcroft | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 399171ae1a4SAndy Whitcroft | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 400171ae1a4SAndy Whitcroft | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 401171ae1a4SAndy Whitcroft}x; 402171ae1a4SAndy Whitcroft 40315662b3eSJoe Perchesour $UTF8 = qr{ 40415662b3eSJoe Perches [\x09\x0A\x0D\x20-\x7E] # ASCII 40515662b3eSJoe Perches | $NON_ASCII_UTF8 40615662b3eSJoe Perches}x; 40715662b3eSJoe Perches 408e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t}; 409021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x: 410021158b4SJoe Perches u_(?:char|short|int|long) | # bsd 411021158b4SJoe Perches u(?:nchar|short|int|long) # sysv 412021158b4SJoe Perches)}; 413e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x: 414fb9e9096SAndy Whitcroft (?:__)?(?:u|s|be|le)(?:8|16|32|64)| 4158ed22cadSAndy Whitcroft atomic_t 4168ed22cadSAndy Whitcroft)}; 417e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x: 418e6176fa4SJoe Perches $typeC99Typedefs\b| 419e6176fa4SJoe Perches $typeOtherOSTypedefs\b| 420e6176fa4SJoe Perches $typeKernelTypedefs\b 421e6176fa4SJoe Perches)}; 4228ed22cadSAndy Whitcroft 4236d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b}; 4246d32f7a3SJoe Perches 425691e669bSJoe Perchesour $logFunctions = qr{(?x: 4266e60c02eSJoe Perches printk(?:_ratelimited|_once|)| 4277d0b6594SJacob Keller (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)| 4286e60c02eSJoe Perches WARN(?:_RATELIMIT|_ONCE|)| 429b0531722SJoe Perches panic| 43006668727SJoe Perches MODULE_[A-Z_]+| 43106668727SJoe Perches seq_vprintf|seq_printf|seq_puts 432691e669bSJoe Perches)}; 433691e669bSJoe Perches 43420112475SJoe Perchesour $signature_tags = qr{(?xi: 43520112475SJoe Perches Signed-off-by:| 43620112475SJoe Perches Acked-by:| 43720112475SJoe Perches Tested-by:| 43820112475SJoe Perches Reviewed-by:| 43920112475SJoe Perches Reported-by:| 4408543ae12SMugunthan V N Suggested-by:| 44120112475SJoe Perches To:| 44220112475SJoe Perches Cc: 44320112475SJoe Perches)}; 44420112475SJoe Perches 4451813087dSJoe Perchesour @typeListMisordered = ( 4461813087dSJoe Perches qr{char\s+(?:un)?signed}, 4471813087dSJoe Perches qr{int\s+(?:(?:un)?signed\s+)?short\s}, 4481813087dSJoe Perches qr{int\s+short(?:\s+(?:un)?signed)}, 4491813087dSJoe Perches qr{short\s+int(?:\s+(?:un)?signed)}, 4501813087dSJoe Perches qr{(?:un)?signed\s+int\s+short}, 4511813087dSJoe Perches qr{short\s+(?:un)?signed}, 4521813087dSJoe Perches qr{long\s+int\s+(?:un)?signed}, 4531813087dSJoe Perches qr{int\s+long\s+(?:un)?signed}, 4541813087dSJoe Perches qr{long\s+(?:un)?signed\s+int}, 4551813087dSJoe Perches qr{int\s+(?:un)?signed\s+long}, 4561813087dSJoe Perches qr{int\s+(?:un)?signed}, 4571813087dSJoe Perches qr{int\s+long\s+long\s+(?:un)?signed}, 4581813087dSJoe Perches qr{long\s+long\s+int\s+(?:un)?signed}, 4591813087dSJoe Perches qr{long\s+long\s+(?:un)?signed\s+int}, 4601813087dSJoe Perches qr{long\s+long\s+(?:un)?signed}, 4611813087dSJoe Perches qr{long\s+(?:un)?signed}, 4621813087dSJoe Perches); 4631813087dSJoe Perches 4648905a67cSAndy Whitcroftour @typeList = ( 4658905a67cSAndy Whitcroft qr{void}, 4660c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?char}, 4670c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short\s+int}, 4680c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?short}, 4690c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?int}, 4700c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+int}, 4710c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long\s+int}, 4720c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long\s+long}, 4730c773d9dSJoe Perches qr{(?:(?:un)?signed\s+)?long}, 4740c773d9dSJoe Perches qr{(?:un)?signed}, 4758905a67cSAndy Whitcroft qr{float}, 4768905a67cSAndy Whitcroft qr{double}, 4778905a67cSAndy Whitcroft qr{bool}, 4788905a67cSAndy Whitcroft qr{struct\s+$Ident}, 4798905a67cSAndy Whitcroft qr{union\s+$Ident}, 4808905a67cSAndy Whitcroft qr{enum\s+$Ident}, 4818905a67cSAndy Whitcroft qr{${Ident}_t}, 4828905a67cSAndy Whitcroft qr{${Ident}_handler}, 4838905a67cSAndy Whitcroft qr{${Ident}_handler_fn}, 4841813087dSJoe Perches @typeListMisordered, 4858905a67cSAndy Whitcroft); 486938224b5SJoe Perches 487938224b5SJoe Perchesour $C90_int_types = qr{(?x: 488938224b5SJoe Perches long\s+long\s+int\s+(?:un)?signed| 489938224b5SJoe Perches long\s+long\s+(?:un)?signed\s+int| 490938224b5SJoe Perches long\s+long\s+(?:un)?signed| 491938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long\s+int| 492938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+long| 493938224b5SJoe Perches int\s+long\s+long\s+(?:un)?signed| 494938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long\s+long| 495938224b5SJoe Perches 496938224b5SJoe Perches long\s+int\s+(?:un)?signed| 497938224b5SJoe Perches long\s+(?:un)?signed\s+int| 498938224b5SJoe Perches long\s+(?:un)?signed| 499938224b5SJoe Perches (?:(?:un)?signed\s+)?long\s+int| 500938224b5SJoe Perches (?:(?:un)?signed\s+)?long| 501938224b5SJoe Perches int\s+long\s+(?:un)?signed| 502938224b5SJoe Perches int\s+(?:(?:un)?signed\s+)?long| 503938224b5SJoe Perches 504938224b5SJoe Perches int\s+(?:un)?signed| 505938224b5SJoe Perches (?:(?:un)?signed\s+)?int 506938224b5SJoe Perches)}; 507938224b5SJoe Perches 508485ff23eSAlex Dowadour @typeListFile = (); 5098716de38SJoe Perchesour @typeListWithAttr = ( 5108716de38SJoe Perches @typeList, 5118716de38SJoe Perches qr{struct\s+$InitAttribute\s+$Ident}, 5128716de38SJoe Perches qr{union\s+$InitAttribute\s+$Ident}, 5138716de38SJoe Perches); 5148716de38SJoe Perches 515c45dcabdSAndy Whitcroftour @modifierList = ( 516c45dcabdSAndy Whitcroft qr{fastcall}, 517c45dcabdSAndy Whitcroft); 518485ff23eSAlex Dowadour @modifierListFile = (); 5198905a67cSAndy Whitcroft 5202435880fSJoe Perchesour @mode_permission_funcs = ( 5212435880fSJoe Perches ["module_param", 3], 5222435880fSJoe Perches ["module_param_(?:array|named|string)", 4], 5232435880fSJoe Perches ["module_param_array_named", 5], 5242435880fSJoe Perches ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2], 5252435880fSJoe Perches ["proc_create(?:_data|)", 2], 5262435880fSJoe Perches ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2], 5272435880fSJoe Perches); 5282435880fSJoe Perches 529515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below 530515a235eSJoe Perchesour $mode_perms_search = ""; 531515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) { 532515a235eSJoe Perches $mode_perms_search .= '|' if ($mode_perms_search ne ""); 533515a235eSJoe Perches $mode_perms_search .= $entry->[0]; 534515a235eSJoe Perches} 535515a235eSJoe Perches 536b392c64fSJoe Perchesour $mode_perms_world_writable = qr{ 537b392c64fSJoe Perches S_IWUGO | 538b392c64fSJoe Perches S_IWOTH | 539b392c64fSJoe Perches S_IRWXUGO | 540b392c64fSJoe Perches S_IALLUGO | 541b392c64fSJoe Perches 0[0-7][0-7][2367] 542b392c64fSJoe Perches}x; 543b392c64fSJoe Perches 5447840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x: 5457840a94cSWolfram Sang irq| 546cdcee686SSergey Ryazanov memory| 547cdcee686SSergey Ryazanov time| 548cdcee686SSergey Ryazanov reboot 5497840a94cSWolfram Sang)}; 5507840a94cSWolfram Sang# memory.h: ARM has a custom one 5517840a94cSWolfram Sang 55266b47b4aSKees Cook# Load common spelling mistakes and build regular expression list. 55366b47b4aSKees Cookmy $misspellings; 55466b47b4aSKees Cookmy %spelling_fix; 55536061e38SJoe Perches 55636061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) { 55766b47b4aSKees Cook while (<$spelling>) { 55866b47b4aSKees Cook my $line = $_; 55966b47b4aSKees Cook 56066b47b4aSKees Cook $line =~ s/\s*\n?$//g; 56166b47b4aSKees Cook $line =~ s/^\s*//g; 56266b47b4aSKees Cook 56366b47b4aSKees Cook next if ($line =~ m/^\s*#/); 56466b47b4aSKees Cook next if ($line =~ m/^\s*$/); 56566b47b4aSKees Cook 56666b47b4aSKees Cook my ($suspect, $fix) = split(/\|\|/, $line); 56766b47b4aSKees Cook 56866b47b4aSKees Cook $spelling_fix{$suspect} = $fix; 56966b47b4aSKees Cook } 57066b47b4aSKees Cook close($spelling); 57136061e38SJoe Perches} else { 57236061e38SJoe Perches warn "No typos will be found - file '$spelling_file': $!\n"; 57336061e38SJoe Perches} 57466b47b4aSKees Cook 575ebfd7d62SJoe Perchesif ($codespell) { 576ebfd7d62SJoe Perches if (open(my $spelling, '<', $codespellfile)) { 577ebfd7d62SJoe Perches while (<$spelling>) { 578ebfd7d62SJoe Perches my $line = $_; 579ebfd7d62SJoe Perches 580ebfd7d62SJoe Perches $line =~ s/\s*\n?$//g; 581ebfd7d62SJoe Perches $line =~ s/^\s*//g; 582ebfd7d62SJoe Perches 583ebfd7d62SJoe Perches next if ($line =~ m/^\s*#/); 584ebfd7d62SJoe Perches next if ($line =~ m/^\s*$/); 585ebfd7d62SJoe Perches next if ($line =~ m/, disabled/i); 586ebfd7d62SJoe Perches 587ebfd7d62SJoe Perches $line =~ s/,.*$//; 588ebfd7d62SJoe Perches 589ebfd7d62SJoe Perches my ($suspect, $fix) = split(/->/, $line); 590ebfd7d62SJoe Perches 591ebfd7d62SJoe Perches $spelling_fix{$suspect} = $fix; 592ebfd7d62SJoe Perches } 593ebfd7d62SJoe Perches close($spelling); 594ebfd7d62SJoe Perches } else { 595ebfd7d62SJoe Perches warn "No codespell typos will be found - file '$codespellfile': $!\n"; 596ebfd7d62SJoe Perches } 597ebfd7d62SJoe Perches} 598ebfd7d62SJoe Perches 599ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix; 600ebfd7d62SJoe Perches 6018905a67cSAndy Whitcroftsub build_types { 602485ff23eSAlex Dowad my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)"; 603485ff23eSAlex Dowad my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)"; 6041813087dSJoe Perches my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)"; 6058716de38SJoe Perches my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)"; 606c8cb2ca3SAndy Whitcroft $Modifier = qr{(?:$Attribute|$Sparse|$mods)}; 607ab7e23f3SJoe Perches $BasicType = qr{ 608ab7e23f3SJoe Perches (?:$typeTypedefs\b)| 609ab7e23f3SJoe Perches (?:${all}\b) 610ab7e23f3SJoe Perches }x; 6118905a67cSAndy Whitcroft $NonptrType = qr{ 612d2172eb5SAndy Whitcroft (?:$Modifier\s+|const\s+)* 613cf655043SAndy Whitcroft (?: 6146b48db24SAndy Whitcroft (?:typeof|__typeof__)\s*\([^\)]*\)| 6158ed22cadSAndy Whitcroft (?:$typeTypedefs\b)| 616c45dcabdSAndy Whitcroft (?:${all}\b) 617cf655043SAndy Whitcroft ) 618c8cb2ca3SAndy Whitcroft (?:\s+$Modifier|\s+const)* 6198905a67cSAndy Whitcroft }x; 6201813087dSJoe Perches $NonptrTypeMisordered = qr{ 6211813087dSJoe Perches (?:$Modifier\s+|const\s+)* 6221813087dSJoe Perches (?: 6231813087dSJoe Perches (?:${Misordered}\b) 6241813087dSJoe Perches ) 6251813087dSJoe Perches (?:\s+$Modifier|\s+const)* 6261813087dSJoe Perches }x; 6278716de38SJoe Perches $NonptrTypeWithAttr = qr{ 6288716de38SJoe Perches (?:$Modifier\s+|const\s+)* 6298716de38SJoe Perches (?: 6308716de38SJoe Perches (?:typeof|__typeof__)\s*\([^\)]*\)| 6318716de38SJoe Perches (?:$typeTypedefs\b)| 6328716de38SJoe Perches (?:${allWithAttr}\b) 6338716de38SJoe Perches ) 6348716de38SJoe Perches (?:\s+$Modifier|\s+const)* 6358716de38SJoe Perches }x; 6368905a67cSAndy Whitcroft $Type = qr{ 637c45dcabdSAndy Whitcroft $NonptrType 6381574a29fSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 639c8cb2ca3SAndy Whitcroft (?:\s+$Inline|\s+$Modifier)* 6408905a67cSAndy Whitcroft }x; 6411813087dSJoe Perches $TypeMisordered = qr{ 6421813087dSJoe Perches $NonptrTypeMisordered 6431813087dSJoe Perches (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)? 6441813087dSJoe Perches (?:\s+$Inline|\s+$Modifier)* 6451813087dSJoe Perches }x; 64691cb5195SJoe Perches $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type}; 6471813087dSJoe Perches $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered}; 6488905a67cSAndy Whitcroft} 6498905a67cSAndy Whitcroftbuild_types(); 6506c72ffaaSAndy Whitcroft 6517d2367afSJoe Perchesour $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*}; 652d1fe9c09SJoe Perches 653d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg 654d1fe9c09SJoe Perches# requires at least perl version v5.10.0 655d1fe9c09SJoe Perches# Any use must be runtime checked with $^V 656d1fe9c09SJoe Perches 657d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/; 6582435880fSJoe Perchesour $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*}; 659c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)}; 6607d2367afSJoe Perches 661f8422308SJoe Perchesour $declaration_macros = qr{(?x: 6623e838b6cSJoe Perches (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(| 663f8422308SJoe Perches (?:$Storage\s+)?LIST_HEAD\s*\(| 664f8422308SJoe Perches (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\( 665f8422308SJoe Perches)}; 666f8422308SJoe Perches 6677d2367afSJoe Perchessub deparenthesize { 6687d2367afSJoe Perches my ($string) = @_; 6697d2367afSJoe Perches return "" if (!defined($string)); 6705b9553abSJoe Perches 6715b9553abSJoe Perches while ($string =~ /^\s*\(.*\)\s*$/) { 6725b9553abSJoe Perches $string =~ s@^\s*\(\s*@@; 6735b9553abSJoe Perches $string =~ s@\s*\)\s*$@@; 6745b9553abSJoe Perches } 6755b9553abSJoe Perches 6767d2367afSJoe Perches $string =~ s@\s+@ @g; 6775b9553abSJoe Perches 6787d2367afSJoe Perches return $string; 6797d2367afSJoe Perches} 6807d2367afSJoe Perches 6813445686aSJoe Perchessub seed_camelcase_file { 6823445686aSJoe Perches my ($file) = @_; 6833445686aSJoe Perches 6843445686aSJoe Perches return if (!(-f $file)); 6853445686aSJoe Perches 6863445686aSJoe Perches local $/; 6873445686aSJoe Perches 6883445686aSJoe Perches open(my $include_file, '<', "$file") 6893445686aSJoe Perches or warn "$P: Can't read '$file' $!\n"; 6903445686aSJoe Perches my $text = <$include_file>; 6913445686aSJoe Perches close($include_file); 6923445686aSJoe Perches 6933445686aSJoe Perches my @lines = split('\n', $text); 6943445686aSJoe Perches 6953445686aSJoe Perches foreach my $line (@lines) { 6963445686aSJoe Perches next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/); 6973445686aSJoe Perches if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) { 6983445686aSJoe Perches $camelcase{$1} = 1; 69911ea516aSJoe Perches } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) { 70011ea516aSJoe Perches $camelcase{$1} = 1; 70111ea516aSJoe Perches } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) { 7023445686aSJoe Perches $camelcase{$1} = 1; 7033445686aSJoe Perches } 7043445686aSJoe Perches } 7053445686aSJoe Perches} 7063445686aSJoe Perches 7073445686aSJoe Perchesmy $camelcase_seeded = 0; 7083445686aSJoe Perchessub seed_camelcase_includes { 7093445686aSJoe Perches return if ($camelcase_seeded); 7103445686aSJoe Perches 7113445686aSJoe Perches my $files; 712c707a81dSJoe Perches my $camelcase_cache = ""; 713c707a81dSJoe Perches my @include_files = (); 714c707a81dSJoe Perches 715c707a81dSJoe Perches $camelcase_seeded = 1; 716351b2a1fSJoe Perches 7173645e328SRichard Genoud if (-e ".git") { 718351b2a1fSJoe Perches my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`; 719351b2a1fSJoe Perches chomp $git_last_include_commit; 720c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit"; 721c707a81dSJoe Perches } else { 722c707a81dSJoe Perches my $last_mod_date = 0; 723c707a81dSJoe Perches $files = `find $root/include -name "*.h"`; 724c707a81dSJoe Perches @include_files = split('\n', $files); 725c707a81dSJoe Perches foreach my $file (@include_files) { 726c707a81dSJoe Perches my $date = POSIX::strftime("%Y%m%d%H%M", 727c707a81dSJoe Perches localtime((stat $file)[9])); 728c707a81dSJoe Perches $last_mod_date = $date if ($last_mod_date < $date); 729c707a81dSJoe Perches } 730c707a81dSJoe Perches $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date"; 731c707a81dSJoe Perches } 732c707a81dSJoe Perches 733c707a81dSJoe Perches if ($camelcase_cache ne "" && -f $camelcase_cache) { 734c707a81dSJoe Perches open(my $camelcase_file, '<', "$camelcase_cache") 735c707a81dSJoe Perches or warn "$P: Can't read '$camelcase_cache' $!\n"; 736351b2a1fSJoe Perches while (<$camelcase_file>) { 737351b2a1fSJoe Perches chomp; 738351b2a1fSJoe Perches $camelcase{$_} = 1; 739351b2a1fSJoe Perches } 740351b2a1fSJoe Perches close($camelcase_file); 741351b2a1fSJoe Perches 742351b2a1fSJoe Perches return; 743351b2a1fSJoe Perches } 744c707a81dSJoe Perches 7453645e328SRichard Genoud if (-e ".git") { 746c707a81dSJoe Perches $files = `git ls-files "include/*.h"`; 747c707a81dSJoe Perches @include_files = split('\n', $files); 7483445686aSJoe Perches } 749c707a81dSJoe Perches 7503445686aSJoe Perches foreach my $file (@include_files) { 7513445686aSJoe Perches seed_camelcase_file($file); 7523445686aSJoe Perches } 753351b2a1fSJoe Perches 754c707a81dSJoe Perches if ($camelcase_cache ne "") { 755351b2a1fSJoe Perches unlink glob ".checkpatch-camelcase.*"; 756c707a81dSJoe Perches open(my $camelcase_file, '>', "$camelcase_cache") 757c707a81dSJoe Perches or warn "$P: Can't write '$camelcase_cache' $!\n"; 758351b2a1fSJoe Perches foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) { 759351b2a1fSJoe Perches print $camelcase_file ("$_\n"); 760351b2a1fSJoe Perches } 761351b2a1fSJoe Perches close($camelcase_file); 762351b2a1fSJoe Perches } 7633445686aSJoe Perches} 7643445686aSJoe Perches 765d311cd44SJoe Perchessub git_commit_info { 766d311cd44SJoe Perches my ($commit, $id, $desc) = @_; 767d311cd44SJoe Perches 768d311cd44SJoe Perches return ($id, $desc) if ((which("git") eq "") || !(-e ".git")); 769d311cd44SJoe Perches 770d311cd44SJoe Perches my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`; 771d311cd44SJoe Perches $output =~ s/^\s*//gm; 772d311cd44SJoe Perches my @lines = split("\n", $output); 773d311cd44SJoe Perches 7740d7835fcSJoe Perches return ($id, $desc) if ($#lines < 0); 7750d7835fcSJoe Perches 776d311cd44SJoe Perches if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) { 777d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns 778d311cd44SJoe Perches# all matching commit ids, but it's very slow... 779d311cd44SJoe Perches# 780d311cd44SJoe Perches# echo "checking commits $1..." 781d311cd44SJoe Perches# git rev-list --remotes | grep -i "^$1" | 782d311cd44SJoe Perches# while read line ; do 783d311cd44SJoe Perches# git log --format='%H %s' -1 $line | 784d311cd44SJoe Perches# echo "commit $(cut -c 1-12,41-)" 785d311cd44SJoe Perches# done 786d311cd44SJoe Perches } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) { 787d311cd44SJoe Perches } else { 788d311cd44SJoe Perches $id = substr($lines[0], 0, 12); 789d311cd44SJoe Perches $desc = substr($lines[0], 41); 790d311cd44SJoe Perches } 791d311cd44SJoe Perches 792d311cd44SJoe Perches return ($id, $desc); 793d311cd44SJoe Perches} 794d311cd44SJoe Perches 7956c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file); 7960a920b5bSAndy Whitcroft 79700df344fSAndy Whitcroftmy @rawlines = (); 798c2fdda0dSAndy Whitcroftmy @lines = (); 7993705ce5bSJoe Perchesmy @fixed = (); 800d752fcc8SJoe Perchesmy @fixed_inserted = (); 801d752fcc8SJoe Perchesmy @fixed_deleted = (); 802194f66fcSJoe Perchesmy $fixlinenr = -1; 803194f66fcSJoe Perches 8044a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions. 8054a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'. 8064a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git"); 8074a593c34SDu, Changbin 8084a593c34SDu, Changbinif ($git) { 8094a593c34SDu, Changbin my @commits = (); 810*0dea9f1eSJoe Perches foreach my $commit_expr (@ARGV) { 8114a593c34SDu, Changbin my $git_range; 8124a593c34SDu, Changbin if ($commit_expr =~ m/-/) { 8134a593c34SDu, Changbin my @tmp = split(/-/, $commit_expr); 814*0dea9f1eSJoe Perches die "$P: incorrect git commit expression '$commit_expr' $!\n" 8154a593c34SDu, Changbin if (@tmp != 2); 8164a593c34SDu, Changbin $git_range = "-$tmp[1] $tmp[0]"; 8174a593c34SDu, Changbin } elsif ($commit_expr =~ m/\.\./) { 8184a593c34SDu, Changbin $git_range = "$commit_expr"; 8194a593c34SDu, Changbin } else { 820*0dea9f1eSJoe Perches $git_range = "-1 $commit_expr"; 821*0dea9f1eSJoe Perches } 822*0dea9f1eSJoe Perches my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`; 823*0dea9f1eSJoe Perches foreach my $line (split(/\n/, $lines)) { 824*0dea9f1eSJoe Perches $line =~ /(^\w+) (.*)/; 825*0dea9f1eSJoe Perches my $sha1 = $1; 826*0dea9f1eSJoe Perches my $subject = $2; 827*0dea9f1eSJoe Perches unshift(@commits, $sha1); 828*0dea9f1eSJoe Perches $git_commits{$sha1} = $subject; 8294a593c34SDu, Changbin } 8304a593c34SDu, Changbin } 8314a593c34SDu, Changbin die "$P: no git commits after extraction!\n" if (@commits == 0); 8324a593c34SDu, Changbin @ARGV = @commits; 8334a593c34SDu, Changbin} 8344a593c34SDu, Changbin 835c2fdda0dSAndy Whitcroftmy $vname; 8366c72ffaaSAndy Whitcroftfor my $filename (@ARGV) { 83721caa13cSAndy Whitcroft my $FILE; 8384a593c34SDu, Changbin if ($git) { 8394a593c34SDu, Changbin open($FILE, '-|', "git format-patch -M --stdout -1 $filename") || 8404a593c34SDu, Changbin die "$P: $filename: git format-patch failed - $!\n"; 8414a593c34SDu, Changbin } elsif ($file) { 84221caa13cSAndy Whitcroft open($FILE, '-|', "diff -u /dev/null $filename") || 8436c72ffaaSAndy Whitcroft die "$P: $filename: diff failed - $!\n"; 84421caa13cSAndy Whitcroft } elsif ($filename eq '-') { 84521caa13cSAndy Whitcroft open($FILE, '<&STDIN'); 8466c72ffaaSAndy Whitcroft } else { 84721caa13cSAndy Whitcroft open($FILE, '<', "$filename") || 8486c72ffaaSAndy Whitcroft die "$P: $filename: open failed - $!\n"; 8496c72ffaaSAndy Whitcroft } 850c2fdda0dSAndy Whitcroft if ($filename eq '-') { 851c2fdda0dSAndy Whitcroft $vname = 'Your patch'; 8524a593c34SDu, Changbin } elsif ($git) { 853*0dea9f1eSJoe Perches $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")'; 854c2fdda0dSAndy Whitcroft } else { 855c2fdda0dSAndy Whitcroft $vname = $filename; 856c2fdda0dSAndy Whitcroft } 85721caa13cSAndy Whitcroft while (<$FILE>) { 8580a920b5bSAndy Whitcroft chomp; 85900df344fSAndy Whitcroft push(@rawlines, $_); 8606c72ffaaSAndy Whitcroft } 86121caa13cSAndy Whitcroft close($FILE); 862d8469f16SJoe Perches 863d8469f16SJoe Perches if ($#ARGV > 0 && $quiet == 0) { 864d8469f16SJoe Perches print '-' x length($vname) . "\n"; 865d8469f16SJoe Perches print "$vname\n"; 866d8469f16SJoe Perches print '-' x length($vname) . "\n"; 867d8469f16SJoe Perches } 868d8469f16SJoe Perches 869c2fdda0dSAndy Whitcroft if (!process($filename)) { 8700a920b5bSAndy Whitcroft $exit = 1; 8710a920b5bSAndy Whitcroft } 87200df344fSAndy Whitcroft @rawlines = (); 87313214adfSAndy Whitcroft @lines = (); 8743705ce5bSJoe Perches @fixed = (); 875d752fcc8SJoe Perches @fixed_inserted = (); 876d752fcc8SJoe Perches @fixed_deleted = (); 877194f66fcSJoe Perches $fixlinenr = -1; 878485ff23eSAlex Dowad @modifierListFile = (); 879485ff23eSAlex Dowad @typeListFile = (); 880485ff23eSAlex Dowad build_types(); 8810a920b5bSAndy Whitcroft} 8820a920b5bSAndy Whitcroft 883d8469f16SJoe Perchesif (!$quiet) { 8843c816e49SJoe Perches hash_show_words(\%use_type, "Used"); 8853c816e49SJoe Perches hash_show_words(\%ignore_type, "Ignored"); 8863c816e49SJoe Perches 887d8469f16SJoe Perches if ($^V lt 5.10.0) { 888d8469f16SJoe Perches print << "EOM" 889d8469f16SJoe Perches 890d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues. 891d8469f16SJoe Perches An upgrade to at least perl v5.10.0 is suggested. 892d8469f16SJoe PerchesEOM 893d8469f16SJoe Perches } 894d8469f16SJoe Perches if ($exit) { 895d8469f16SJoe Perches print << "EOM" 896d8469f16SJoe Perches 897d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report 898d8469f16SJoe Perches them to the maintainer, see CHECKPATCH in MAINTAINERS. 899d8469f16SJoe PerchesEOM 900d8469f16SJoe Perches } 901d8469f16SJoe Perches} 902d8469f16SJoe Perches 9030a920b5bSAndy Whitcroftexit($exit); 9040a920b5bSAndy Whitcroft 9050a920b5bSAndy Whitcroftsub top_of_kernel_tree { 9066c72ffaaSAndy Whitcroft my ($root) = @_; 9076c72ffaaSAndy Whitcroft 9086c72ffaaSAndy Whitcroft my @tree_check = ( 9096c72ffaaSAndy Whitcroft "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile", 9106c72ffaaSAndy Whitcroft "README", "Documentation", "arch", "include", "drivers", 9116c72ffaaSAndy Whitcroft "fs", "init", "ipc", "kernel", "lib", "scripts", 9126c72ffaaSAndy Whitcroft ); 9136c72ffaaSAndy Whitcroft 9146c72ffaaSAndy Whitcroft foreach my $check (@tree_check) { 9156c72ffaaSAndy Whitcroft if (! -e $root . '/' . $check) { 9160a920b5bSAndy Whitcroft return 0; 9170a920b5bSAndy Whitcroft } 9186c72ffaaSAndy Whitcroft } 9196c72ffaaSAndy Whitcroft return 1; 9206c72ffaaSAndy Whitcroft} 9210a920b5bSAndy Whitcroft 92220112475SJoe Perchessub parse_email { 92320112475SJoe Perches my ($formatted_email) = @_; 92420112475SJoe Perches 92520112475SJoe Perches my $name = ""; 92620112475SJoe Perches my $address = ""; 92720112475SJoe Perches my $comment = ""; 92820112475SJoe Perches 92920112475SJoe Perches if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) { 93020112475SJoe Perches $name = $1; 93120112475SJoe Perches $address = $2; 93220112475SJoe Perches $comment = $3 if defined $3; 93320112475SJoe Perches } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) { 93420112475SJoe Perches $address = $1; 93520112475SJoe Perches $comment = $2 if defined $2; 93620112475SJoe Perches } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) { 93720112475SJoe Perches $address = $1; 93820112475SJoe Perches $comment = $2 if defined $2; 93920112475SJoe Perches $formatted_email =~ s/$address.*$//; 94020112475SJoe Perches $name = $formatted_email; 9413705ce5bSJoe Perches $name = trim($name); 94220112475SJoe Perches $name =~ s/^\"|\"$//g; 94320112475SJoe Perches # If there's a name left after stripping spaces and 94420112475SJoe Perches # leading quotes, and the address doesn't have both 94520112475SJoe Perches # leading and trailing angle brackets, the address 94620112475SJoe Perches # is invalid. ie: 94720112475SJoe Perches # "joe smith [email protected]" bad 94820112475SJoe Perches # "joe smith <[email protected]" bad 94920112475SJoe Perches if ($name ne "" && $address !~ /^<[^>]+>$/) { 95020112475SJoe Perches $name = ""; 95120112475SJoe Perches $address = ""; 95220112475SJoe Perches $comment = ""; 95320112475SJoe Perches } 95420112475SJoe Perches } 95520112475SJoe Perches 9563705ce5bSJoe Perches $name = trim($name); 95720112475SJoe Perches $name =~ s/^\"|\"$//g; 9583705ce5bSJoe Perches $address = trim($address); 95920112475SJoe Perches $address =~ s/^\<|\>$//g; 96020112475SJoe Perches 96120112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 96220112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 96320112475SJoe Perches $name = "\"$name\""; 96420112475SJoe Perches } 96520112475SJoe Perches 96620112475SJoe Perches return ($name, $address, $comment); 96720112475SJoe Perches} 96820112475SJoe Perches 96920112475SJoe Perchessub format_email { 97020112475SJoe Perches my ($name, $address) = @_; 97120112475SJoe Perches 97220112475SJoe Perches my $formatted_email; 97320112475SJoe Perches 9743705ce5bSJoe Perches $name = trim($name); 97520112475SJoe Perches $name =~ s/^\"|\"$//g; 9763705ce5bSJoe Perches $address = trim($address); 97720112475SJoe Perches 97820112475SJoe Perches if ($name =~ /[^\w \-]/i) { ##has "must quote" chars 97920112475SJoe Perches $name =~ s/(?<!\\)"/\\"/g; ##escape quotes 98020112475SJoe Perches $name = "\"$name\""; 98120112475SJoe Perches } 98220112475SJoe Perches 98320112475SJoe Perches if ("$name" eq "") { 98420112475SJoe Perches $formatted_email = "$address"; 98520112475SJoe Perches } else { 98620112475SJoe Perches $formatted_email = "$name <$address>"; 98720112475SJoe Perches } 98820112475SJoe Perches 98920112475SJoe Perches return $formatted_email; 99020112475SJoe Perches} 99120112475SJoe Perches 992d311cd44SJoe Perchessub which { 993d311cd44SJoe Perches my ($bin) = @_; 994d311cd44SJoe Perches 995d311cd44SJoe Perches foreach my $path (split(/:/, $ENV{PATH})) { 996d311cd44SJoe Perches if (-e "$path/$bin") { 997d311cd44SJoe Perches return "$path/$bin"; 998d311cd44SJoe Perches } 999d311cd44SJoe Perches } 1000d311cd44SJoe Perches 1001d311cd44SJoe Perches return ""; 1002d311cd44SJoe Perches} 1003d311cd44SJoe Perches 1004000d1cc1SJoe Perchessub which_conf { 1005000d1cc1SJoe Perches my ($conf) = @_; 1006000d1cc1SJoe Perches 1007000d1cc1SJoe Perches foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) { 1008000d1cc1SJoe Perches if (-e "$path/$conf") { 1009000d1cc1SJoe Perches return "$path/$conf"; 1010000d1cc1SJoe Perches } 1011000d1cc1SJoe Perches } 1012000d1cc1SJoe Perches 1013000d1cc1SJoe Perches return ""; 1014000d1cc1SJoe Perches} 1015000d1cc1SJoe Perches 10160a920b5bSAndy Whitcroftsub expand_tabs { 10170a920b5bSAndy Whitcroft my ($str) = @_; 10180a920b5bSAndy Whitcroft 10190a920b5bSAndy Whitcroft my $res = ''; 10200a920b5bSAndy Whitcroft my $n = 0; 10210a920b5bSAndy Whitcroft for my $c (split(//, $str)) { 10220a920b5bSAndy Whitcroft if ($c eq "\t") { 10230a920b5bSAndy Whitcroft $res .= ' '; 10240a920b5bSAndy Whitcroft $n++; 10250a920b5bSAndy Whitcroft for (; ($n % 8) != 0; $n++) { 10260a920b5bSAndy Whitcroft $res .= ' '; 10270a920b5bSAndy Whitcroft } 10280a920b5bSAndy Whitcroft next; 10290a920b5bSAndy Whitcroft } 10300a920b5bSAndy Whitcroft $res .= $c; 10310a920b5bSAndy Whitcroft $n++; 10320a920b5bSAndy Whitcroft } 10330a920b5bSAndy Whitcroft 10340a920b5bSAndy Whitcroft return $res; 10350a920b5bSAndy Whitcroft} 10366c72ffaaSAndy Whitcroftsub copy_spacing { 1037773647a0SAndy Whitcroft (my $res = shift) =~ tr/\t/ /c; 10386c72ffaaSAndy Whitcroft return $res; 10396c72ffaaSAndy Whitcroft} 10400a920b5bSAndy Whitcroft 10414a0df2efSAndy Whitcroftsub line_stats { 10424a0df2efSAndy Whitcroft my ($line) = @_; 10434a0df2efSAndy Whitcroft 10444a0df2efSAndy Whitcroft # Drop the diff line leader and expand tabs 10454a0df2efSAndy Whitcroft $line =~ s/^.//; 10464a0df2efSAndy Whitcroft $line = expand_tabs($line); 10474a0df2efSAndy Whitcroft 10484a0df2efSAndy Whitcroft # Pick the indent from the front of the line. 10494a0df2efSAndy Whitcroft my ($white) = ($line =~ /^(\s*)/); 10504a0df2efSAndy Whitcroft 10514a0df2efSAndy Whitcroft return (length($line), length($white)); 10524a0df2efSAndy Whitcroft} 10534a0df2efSAndy Whitcroft 1054773647a0SAndy Whitcroftmy $sanitise_quote = ''; 1055773647a0SAndy Whitcroft 1056773647a0SAndy Whitcroftsub sanitise_line_reset { 1057773647a0SAndy Whitcroft my ($in_comment) = @_; 1058773647a0SAndy Whitcroft 1059773647a0SAndy Whitcroft if ($in_comment) { 1060773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1061773647a0SAndy Whitcroft } else { 1062773647a0SAndy Whitcroft $sanitise_quote = ''; 1063773647a0SAndy Whitcroft } 1064773647a0SAndy Whitcroft} 106500df344fSAndy Whitcroftsub sanitise_line { 106600df344fSAndy Whitcroft my ($line) = @_; 106700df344fSAndy Whitcroft 106800df344fSAndy Whitcroft my $res = ''; 106900df344fSAndy Whitcroft my $l = ''; 107000df344fSAndy Whitcroft 1071c2fdda0dSAndy Whitcroft my $qlen = 0; 1072773647a0SAndy Whitcroft my $off = 0; 1073773647a0SAndy Whitcroft my $c; 107400df344fSAndy Whitcroft 1075773647a0SAndy Whitcroft # Always copy over the diff marker. 1076773647a0SAndy Whitcroft $res = substr($line, 0, 1); 1077773647a0SAndy Whitcroft 1078773647a0SAndy Whitcroft for ($off = 1; $off < length($line); $off++) { 1079773647a0SAndy Whitcroft $c = substr($line, $off, 1); 1080773647a0SAndy Whitcroft 1081773647a0SAndy Whitcroft # Comments we are wacking completly including the begin 1082773647a0SAndy Whitcroft # and end, all to $;. 1083773647a0SAndy Whitcroft if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') { 1084773647a0SAndy Whitcroft $sanitise_quote = '*/'; 1085773647a0SAndy Whitcroft 1086773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1087773647a0SAndy Whitcroft $off++; 108800df344fSAndy Whitcroft next; 1089773647a0SAndy Whitcroft } 109081bc0e02SAndy Whitcroft if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') { 1091773647a0SAndy Whitcroft $sanitise_quote = ''; 1092773647a0SAndy Whitcroft substr($res, $off, 2, "$;$;"); 1093773647a0SAndy Whitcroft $off++; 1094773647a0SAndy Whitcroft next; 1095773647a0SAndy Whitcroft } 1096113f04a8SDaniel Walker if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') { 1097113f04a8SDaniel Walker $sanitise_quote = '//'; 1098113f04a8SDaniel Walker 1099113f04a8SDaniel Walker substr($res, $off, 2, $sanitise_quote); 1100113f04a8SDaniel Walker $off++; 1101113f04a8SDaniel Walker next; 1102113f04a8SDaniel Walker } 1103773647a0SAndy Whitcroft 1104773647a0SAndy Whitcroft # A \ in a string means ignore the next character. 1105773647a0SAndy Whitcroft if (($sanitise_quote eq "'" || $sanitise_quote eq '"') && 1106773647a0SAndy Whitcroft $c eq "\\") { 1107773647a0SAndy Whitcroft substr($res, $off, 2, 'XX'); 1108773647a0SAndy Whitcroft $off++; 1109773647a0SAndy Whitcroft next; 1110773647a0SAndy Whitcroft } 1111773647a0SAndy Whitcroft # Regular quotes. 1112773647a0SAndy Whitcroft if ($c eq "'" || $c eq '"') { 1113773647a0SAndy Whitcroft if ($sanitise_quote eq '') { 1114773647a0SAndy Whitcroft $sanitise_quote = $c; 1115773647a0SAndy Whitcroft 1116773647a0SAndy Whitcroft substr($res, $off, 1, $c); 1117773647a0SAndy Whitcroft next; 1118773647a0SAndy Whitcroft } elsif ($sanitise_quote eq $c) { 1119773647a0SAndy Whitcroft $sanitise_quote = ''; 112000df344fSAndy Whitcroft } 112100df344fSAndy Whitcroft } 1122773647a0SAndy Whitcroft 1123fae17daeSAndy Whitcroft #print "c<$c> SQ<$sanitise_quote>\n"; 1124773647a0SAndy Whitcroft if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") { 1125773647a0SAndy Whitcroft substr($res, $off, 1, $;); 1126113f04a8SDaniel Walker } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") { 1127113f04a8SDaniel Walker substr($res, $off, 1, $;); 1128773647a0SAndy Whitcroft } elsif ($off != 0 && $sanitise_quote && $c ne "\t") { 1129773647a0SAndy Whitcroft substr($res, $off, 1, 'X'); 113000df344fSAndy Whitcroft } else { 1131773647a0SAndy Whitcroft substr($res, $off, 1, $c); 113200df344fSAndy Whitcroft } 1133c2fdda0dSAndy Whitcroft } 1134c2fdda0dSAndy Whitcroft 1135113f04a8SDaniel Walker if ($sanitise_quote eq '//') { 1136113f04a8SDaniel Walker $sanitise_quote = ''; 1137113f04a8SDaniel Walker } 1138113f04a8SDaniel Walker 1139c2fdda0dSAndy Whitcroft # The pathname on a #include may be surrounded by '<' and '>'. 1140c45dcabdSAndy Whitcroft if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) { 1141c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1142c2fdda0dSAndy Whitcroft $res =~ s@\<.*\>@<$clean>@; 1143c2fdda0dSAndy Whitcroft 1144c2fdda0dSAndy Whitcroft # The whole of a #error is a string. 1145c45dcabdSAndy Whitcroft } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) { 1146c2fdda0dSAndy Whitcroft my $clean = 'X' x length($1); 1147c45dcabdSAndy Whitcroft $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@; 1148c2fdda0dSAndy Whitcroft } 1149c2fdda0dSAndy Whitcroft 115000df344fSAndy Whitcroft return $res; 115100df344fSAndy Whitcroft} 115200df344fSAndy Whitcroft 1153a6962d72SJoe Perchessub get_quoted_string { 1154a6962d72SJoe Perches my ($line, $rawline) = @_; 1155a6962d72SJoe Perches 115633acb54aSJoe Perches return "" if ($line !~ m/($String)/g); 1157a6962d72SJoe Perches return substr($rawline, $-[0], $+[0] - $-[0]); 1158a6962d72SJoe Perches} 1159a6962d72SJoe Perches 11608905a67cSAndy Whitcroftsub ctx_statement_block { 11618905a67cSAndy Whitcroft my ($linenr, $remain, $off) = @_; 11628905a67cSAndy Whitcroft my $line = $linenr - 1; 11638905a67cSAndy Whitcroft my $blk = ''; 11648905a67cSAndy Whitcroft my $soff = $off; 11658905a67cSAndy Whitcroft my $coff = $off - 1; 1166773647a0SAndy Whitcroft my $coff_set = 0; 11678905a67cSAndy Whitcroft 116813214adfSAndy Whitcroft my $loff = 0; 116913214adfSAndy Whitcroft 11708905a67cSAndy Whitcroft my $type = ''; 11718905a67cSAndy Whitcroft my $level = 0; 1172a2750645SAndy Whitcroft my @stack = (); 1173cf655043SAndy Whitcroft my $p; 11748905a67cSAndy Whitcroft my $c; 11758905a67cSAndy Whitcroft my $len = 0; 117613214adfSAndy Whitcroft 117713214adfSAndy Whitcroft my $remainder; 11788905a67cSAndy Whitcroft while (1) { 1179a2750645SAndy Whitcroft @stack = (['', 0]) if ($#stack == -1); 1180a2750645SAndy Whitcroft 1181773647a0SAndy Whitcroft #warn "CSB: blk<$blk> remain<$remain>\n"; 11828905a67cSAndy Whitcroft # If we are about to drop off the end, pull in more 11838905a67cSAndy Whitcroft # context. 11848905a67cSAndy Whitcroft if ($off >= $len) { 11858905a67cSAndy Whitcroft for (; $remain > 0; $line++) { 1186dea33496SAndy Whitcroft last if (!defined $lines[$line]); 1187c2fdda0dSAndy Whitcroft next if ($lines[$line] =~ /^-/); 11888905a67cSAndy Whitcroft $remain--; 118913214adfSAndy Whitcroft $loff = $len; 1190c2fdda0dSAndy Whitcroft $blk .= $lines[$line] . "\n"; 11918905a67cSAndy Whitcroft $len = length($blk); 11928905a67cSAndy Whitcroft $line++; 11938905a67cSAndy Whitcroft last; 11948905a67cSAndy Whitcroft } 11958905a67cSAndy Whitcroft # Bail if there is no further context. 11968905a67cSAndy Whitcroft #warn "CSB: blk<$blk> off<$off> len<$len>\n"; 119713214adfSAndy Whitcroft if ($off >= $len) { 11988905a67cSAndy Whitcroft last; 11998905a67cSAndy Whitcroft } 1200f74bd194SAndy Whitcroft if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) { 1201f74bd194SAndy Whitcroft $level++; 1202f74bd194SAndy Whitcroft $type = '#'; 1203f74bd194SAndy Whitcroft } 12048905a67cSAndy Whitcroft } 1205cf655043SAndy Whitcroft $p = $c; 12068905a67cSAndy Whitcroft $c = substr($blk, $off, 1); 120713214adfSAndy Whitcroft $remainder = substr($blk, $off); 12088905a67cSAndy Whitcroft 1209773647a0SAndy Whitcroft #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n"; 12104635f4fbSAndy Whitcroft 12114635f4fbSAndy Whitcroft # Handle nested #if/#else. 12124635f4fbSAndy Whitcroft if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) { 12134635f4fbSAndy Whitcroft push(@stack, [ $type, $level ]); 12144635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) { 12154635f4fbSAndy Whitcroft ($type, $level) = @{$stack[$#stack - 1]}; 12164635f4fbSAndy Whitcroft } elsif ($remainder =~ /^#\s*endif\b/) { 12174635f4fbSAndy Whitcroft ($type, $level) = @{pop(@stack)}; 12184635f4fbSAndy Whitcroft } 12194635f4fbSAndy Whitcroft 12208905a67cSAndy Whitcroft # Statement ends at the ';' or a close '}' at the 12218905a67cSAndy Whitcroft # outermost level. 12228905a67cSAndy Whitcroft if ($level == 0 && $c eq ';') { 12238905a67cSAndy Whitcroft last; 12248905a67cSAndy Whitcroft } 12258905a67cSAndy Whitcroft 122613214adfSAndy Whitcroft # An else is really a conditional as long as its not else if 1227773647a0SAndy Whitcroft if ($level == 0 && $coff_set == 0 && 1228773647a0SAndy Whitcroft (!defined($p) || $p =~ /(?:\s|\}|\+)/) && 1229773647a0SAndy Whitcroft $remainder =~ /^(else)(?:\s|{)/ && 1230773647a0SAndy Whitcroft $remainder !~ /^else\s+if\b/) { 1231773647a0SAndy Whitcroft $coff = $off + length($1) - 1; 1232773647a0SAndy Whitcroft $coff_set = 1; 1233773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n"; 1234773647a0SAndy Whitcroft #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n"; 123513214adfSAndy Whitcroft } 123613214adfSAndy Whitcroft 12378905a67cSAndy Whitcroft if (($type eq '' || $type eq '(') && $c eq '(') { 12388905a67cSAndy Whitcroft $level++; 12398905a67cSAndy Whitcroft $type = '('; 12408905a67cSAndy Whitcroft } 12418905a67cSAndy Whitcroft if ($type eq '(' && $c eq ')') { 12428905a67cSAndy Whitcroft $level--; 12438905a67cSAndy Whitcroft $type = ($level != 0)? '(' : ''; 12448905a67cSAndy Whitcroft 12458905a67cSAndy Whitcroft if ($level == 0 && $coff < $soff) { 12468905a67cSAndy Whitcroft $coff = $off; 1247773647a0SAndy Whitcroft $coff_set = 1; 1248773647a0SAndy Whitcroft #warn "CSB: mark coff<$coff>\n"; 12498905a67cSAndy Whitcroft } 12508905a67cSAndy Whitcroft } 12518905a67cSAndy Whitcroft if (($type eq '' || $type eq '{') && $c eq '{') { 12528905a67cSAndy Whitcroft $level++; 12538905a67cSAndy Whitcroft $type = '{'; 12548905a67cSAndy Whitcroft } 12558905a67cSAndy Whitcroft if ($type eq '{' && $c eq '}') { 12568905a67cSAndy Whitcroft $level--; 12578905a67cSAndy Whitcroft $type = ($level != 0)? '{' : ''; 12588905a67cSAndy Whitcroft 12598905a67cSAndy Whitcroft if ($level == 0) { 1260b998e001SPatrick Pannuto if (substr($blk, $off + 1, 1) eq ';') { 1261b998e001SPatrick Pannuto $off++; 1262b998e001SPatrick Pannuto } 12638905a67cSAndy Whitcroft last; 12648905a67cSAndy Whitcroft } 12658905a67cSAndy Whitcroft } 1266f74bd194SAndy Whitcroft # Preprocessor commands end at the newline unless escaped. 1267f74bd194SAndy Whitcroft if ($type eq '#' && $c eq "\n" && $p ne "\\") { 1268f74bd194SAndy Whitcroft $level--; 1269f74bd194SAndy Whitcroft $type = ''; 1270f74bd194SAndy Whitcroft $off++; 1271f74bd194SAndy Whitcroft last; 1272f74bd194SAndy Whitcroft } 12738905a67cSAndy Whitcroft $off++; 12748905a67cSAndy Whitcroft } 1275a3bb97a7SAndy Whitcroft # We are truly at the end, so shuffle to the next line. 127613214adfSAndy Whitcroft if ($off == $len) { 1277a3bb97a7SAndy Whitcroft $loff = $len + 1; 127813214adfSAndy Whitcroft $line++; 127913214adfSAndy Whitcroft $remain--; 128013214adfSAndy Whitcroft } 12818905a67cSAndy Whitcroft 12828905a67cSAndy Whitcroft my $statement = substr($blk, $soff, $off - $soff + 1); 12838905a67cSAndy Whitcroft my $condition = substr($blk, $soff, $coff - $soff + 1); 12848905a67cSAndy Whitcroft 12858905a67cSAndy Whitcroft #warn "STATEMENT<$statement>\n"; 12868905a67cSAndy Whitcroft #warn "CONDITION<$condition>\n"; 12878905a67cSAndy Whitcroft 1288773647a0SAndy Whitcroft #print "coff<$coff> soff<$off> loff<$loff>\n"; 128913214adfSAndy Whitcroft 129013214adfSAndy Whitcroft return ($statement, $condition, 129113214adfSAndy Whitcroft $line, $remain + 1, $off - $loff + 1, $level); 129213214adfSAndy Whitcroft} 129313214adfSAndy Whitcroft 1294cf655043SAndy Whitcroftsub statement_lines { 1295cf655043SAndy Whitcroft my ($stmt) = @_; 1296cf655043SAndy Whitcroft 1297cf655043SAndy Whitcroft # Strip the diff line prefixes and rip blank lines at start and end. 1298cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1299cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1300cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1301cf655043SAndy Whitcroft 1302cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1303cf655043SAndy Whitcroft 1304cf655043SAndy Whitcroft return $#stmt_lines + 2; 1305cf655043SAndy Whitcroft} 1306cf655043SAndy Whitcroft 1307cf655043SAndy Whitcroftsub statement_rawlines { 1308cf655043SAndy Whitcroft my ($stmt) = @_; 1309cf655043SAndy Whitcroft 1310cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1311cf655043SAndy Whitcroft 1312cf655043SAndy Whitcroft return $#stmt_lines + 2; 1313cf655043SAndy Whitcroft} 1314cf655043SAndy Whitcroft 1315cf655043SAndy Whitcroftsub statement_block_size { 1316cf655043SAndy Whitcroft my ($stmt) = @_; 1317cf655043SAndy Whitcroft 1318cf655043SAndy Whitcroft $stmt =~ s/(^|\n)./$1/g; 1319cf655043SAndy Whitcroft $stmt =~ s/^\s*{//; 1320cf655043SAndy Whitcroft $stmt =~ s/}\s*$//; 1321cf655043SAndy Whitcroft $stmt =~ s/^\s*//; 1322cf655043SAndy Whitcroft $stmt =~ s/\s*$//; 1323cf655043SAndy Whitcroft 1324cf655043SAndy Whitcroft my @stmt_lines = ($stmt =~ /\n/g); 1325cf655043SAndy Whitcroft my @stmt_statements = ($stmt =~ /;/g); 1326cf655043SAndy Whitcroft 1327cf655043SAndy Whitcroft my $stmt_lines = $#stmt_lines + 2; 1328cf655043SAndy Whitcroft my $stmt_statements = $#stmt_statements + 1; 1329cf655043SAndy Whitcroft 1330cf655043SAndy Whitcroft if ($stmt_lines > $stmt_statements) { 1331cf655043SAndy Whitcroft return $stmt_lines; 1332cf655043SAndy Whitcroft } else { 1333cf655043SAndy Whitcroft return $stmt_statements; 1334cf655043SAndy Whitcroft } 1335cf655043SAndy Whitcroft} 1336cf655043SAndy Whitcroft 133713214adfSAndy Whitcroftsub ctx_statement_full { 133813214adfSAndy Whitcroft my ($linenr, $remain, $off) = @_; 133913214adfSAndy Whitcroft my ($statement, $condition, $level); 134013214adfSAndy Whitcroft 134113214adfSAndy Whitcroft my (@chunks); 134213214adfSAndy Whitcroft 1343cf655043SAndy Whitcroft # Grab the first conditional/block pair. 134413214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 134513214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1346773647a0SAndy Whitcroft #print "F: c<$condition> s<$statement> remain<$remain>\n"; 134713214adfSAndy Whitcroft push(@chunks, [ $condition, $statement ]); 1348cf655043SAndy Whitcroft if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) { 1349cf655043SAndy Whitcroft return ($level, $linenr, @chunks); 1350cf655043SAndy Whitcroft } 1351cf655043SAndy Whitcroft 1352cf655043SAndy Whitcroft # Pull in the following conditional/block pairs and see if they 1353cf655043SAndy Whitcroft # could continue the statement. 1354cf655043SAndy Whitcroft for (;;) { 135513214adfSAndy Whitcroft ($statement, $condition, $linenr, $remain, $off, $level) = 135613214adfSAndy Whitcroft ctx_statement_block($linenr, $remain, $off); 1357cf655043SAndy Whitcroft #print "C: c<$condition> s<$statement> remain<$remain>\n"; 1358773647a0SAndy Whitcroft last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s)); 1359cf655043SAndy Whitcroft #print "C: push\n"; 1360cf655043SAndy Whitcroft push(@chunks, [ $condition, $statement ]); 136113214adfSAndy Whitcroft } 136213214adfSAndy Whitcroft 136313214adfSAndy Whitcroft return ($level, $linenr, @chunks); 13648905a67cSAndy Whitcroft} 13658905a67cSAndy Whitcroft 13664a0df2efSAndy Whitcroftsub ctx_block_get { 1367f0a594c1SAndy Whitcroft my ($linenr, $remain, $outer, $open, $close, $off) = @_; 13684a0df2efSAndy Whitcroft my $line; 13694a0df2efSAndy Whitcroft my $start = $linenr - 1; 13704a0df2efSAndy Whitcroft my $blk = ''; 13714a0df2efSAndy Whitcroft my @o; 13724a0df2efSAndy Whitcroft my @c; 13734a0df2efSAndy Whitcroft my @res = (); 13744a0df2efSAndy Whitcroft 1375f0a594c1SAndy Whitcroft my $level = 0; 13764635f4fbSAndy Whitcroft my @stack = ($level); 137700df344fSAndy Whitcroft for ($line = $start; $remain > 0; $line++) { 137800df344fSAndy Whitcroft next if ($rawlines[$line] =~ /^-/); 137900df344fSAndy Whitcroft $remain--; 138000df344fSAndy Whitcroft 138100df344fSAndy Whitcroft $blk .= $rawlines[$line]; 13824635f4fbSAndy Whitcroft 13834635f4fbSAndy Whitcroft # Handle nested #if/#else. 138401464f30SAndy Whitcroft if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) { 13854635f4fbSAndy Whitcroft push(@stack, $level); 138601464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) { 13874635f4fbSAndy Whitcroft $level = $stack[$#stack - 1]; 138801464f30SAndy Whitcroft } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) { 13894635f4fbSAndy Whitcroft $level = pop(@stack); 13904635f4fbSAndy Whitcroft } 13914635f4fbSAndy Whitcroft 139201464f30SAndy Whitcroft foreach my $c (split(//, $lines[$line])) { 1393f0a594c1SAndy Whitcroft ##print "C<$c>L<$level><$open$close>O<$off>\n"; 1394f0a594c1SAndy Whitcroft if ($off > 0) { 1395f0a594c1SAndy Whitcroft $off--; 1396f0a594c1SAndy Whitcroft next; 1397f0a594c1SAndy Whitcroft } 13984a0df2efSAndy Whitcroft 1399f0a594c1SAndy Whitcroft if ($c eq $close && $level > 0) { 1400f0a594c1SAndy Whitcroft $level--; 1401f0a594c1SAndy Whitcroft last if ($level == 0); 1402f0a594c1SAndy Whitcroft } elsif ($c eq $open) { 1403f0a594c1SAndy Whitcroft $level++; 1404f0a594c1SAndy Whitcroft } 1405f0a594c1SAndy Whitcroft } 14064a0df2efSAndy Whitcroft 1407f0a594c1SAndy Whitcroft if (!$outer || $level <= 1) { 140800df344fSAndy Whitcroft push(@res, $rawlines[$line]); 14094a0df2efSAndy Whitcroft } 14104a0df2efSAndy Whitcroft 1411f0a594c1SAndy Whitcroft last if ($level == 0); 14124a0df2efSAndy Whitcroft } 14134a0df2efSAndy Whitcroft 1414f0a594c1SAndy Whitcroft return ($level, @res); 14154a0df2efSAndy Whitcroft} 14164a0df2efSAndy Whitcroftsub ctx_block_outer { 14174a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 14184a0df2efSAndy Whitcroft 1419f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0); 1420f0a594c1SAndy Whitcroft return @r; 14214a0df2efSAndy Whitcroft} 14224a0df2efSAndy Whitcroftsub ctx_block { 14234a0df2efSAndy Whitcroft my ($linenr, $remain) = @_; 14244a0df2efSAndy Whitcroft 1425f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0); 1426f0a594c1SAndy Whitcroft return @r; 1427653d4876SAndy Whitcroft} 1428653d4876SAndy Whitcroftsub ctx_statement { 1429f0a594c1SAndy Whitcroft my ($linenr, $remain, $off) = @_; 1430f0a594c1SAndy Whitcroft 1431f0a594c1SAndy Whitcroft my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off); 1432f0a594c1SAndy Whitcroft return @r; 1433f0a594c1SAndy Whitcroft} 1434f0a594c1SAndy Whitcroftsub ctx_block_level { 1435653d4876SAndy Whitcroft my ($linenr, $remain) = @_; 1436653d4876SAndy Whitcroft 1437f0a594c1SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '{', '}', 0); 14384a0df2efSAndy Whitcroft} 14399c0ca6f9SAndy Whitcroftsub ctx_statement_level { 14409c0ca6f9SAndy Whitcroft my ($linenr, $remain, $off) = @_; 14419c0ca6f9SAndy Whitcroft 14429c0ca6f9SAndy Whitcroft return ctx_block_get($linenr, $remain, 0, '(', ')', $off); 14439c0ca6f9SAndy Whitcroft} 14444a0df2efSAndy Whitcroft 14454a0df2efSAndy Whitcroftsub ctx_locate_comment { 14464a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 14474a0df2efSAndy Whitcroft 14484a0df2efSAndy Whitcroft # Catch a comment on the end of the line itself. 1449beae6332SAndy Whitcroft my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@); 14504a0df2efSAndy Whitcroft return $current_comment if (defined $current_comment); 14514a0df2efSAndy Whitcroft 14524a0df2efSAndy Whitcroft # Look through the context and try and figure out if there is a 14534a0df2efSAndy Whitcroft # comment. 14544a0df2efSAndy Whitcroft my $in_comment = 0; 14554a0df2efSAndy Whitcroft $current_comment = ''; 14564a0df2efSAndy Whitcroft for (my $linenr = $first_line; $linenr < $end_line; $linenr++) { 145700df344fSAndy Whitcroft my $line = $rawlines[$linenr - 1]; 145800df344fSAndy Whitcroft #warn " $line\n"; 14594a0df2efSAndy Whitcroft if ($linenr == $first_line and $line =~ m@^.\s*\*@) { 14604a0df2efSAndy Whitcroft $in_comment = 1; 14614a0df2efSAndy Whitcroft } 14624a0df2efSAndy Whitcroft if ($line =~ m@/\*@) { 14634a0df2efSAndy Whitcroft $in_comment = 1; 14644a0df2efSAndy Whitcroft } 14654a0df2efSAndy Whitcroft if (!$in_comment && $current_comment ne '') { 14664a0df2efSAndy Whitcroft $current_comment = ''; 14674a0df2efSAndy Whitcroft } 14684a0df2efSAndy Whitcroft $current_comment .= $line . "\n" if ($in_comment); 14694a0df2efSAndy Whitcroft if ($line =~ m@\*/@) { 14704a0df2efSAndy Whitcroft $in_comment = 0; 14714a0df2efSAndy Whitcroft } 14724a0df2efSAndy Whitcroft } 14734a0df2efSAndy Whitcroft 14744a0df2efSAndy Whitcroft chomp($current_comment); 14754a0df2efSAndy Whitcroft return($current_comment); 14764a0df2efSAndy Whitcroft} 14774a0df2efSAndy Whitcroftsub ctx_has_comment { 14784a0df2efSAndy Whitcroft my ($first_line, $end_line) = @_; 14794a0df2efSAndy Whitcroft my $cmt = ctx_locate_comment($first_line, $end_line); 14804a0df2efSAndy Whitcroft 148100df344fSAndy Whitcroft ##print "LINE: $rawlines[$end_line - 1 ]\n"; 14824a0df2efSAndy Whitcroft ##print "CMMT: $cmt\n"; 14834a0df2efSAndy Whitcroft 14844a0df2efSAndy Whitcroft return ($cmt ne ''); 14854a0df2efSAndy Whitcroft} 14864a0df2efSAndy Whitcroft 14874d001e4dSAndy Whitcroftsub raw_line { 14884d001e4dSAndy Whitcroft my ($linenr, $cnt) = @_; 14894d001e4dSAndy Whitcroft 14904d001e4dSAndy Whitcroft my $offset = $linenr - 1; 14914d001e4dSAndy Whitcroft $cnt++; 14924d001e4dSAndy Whitcroft 14934d001e4dSAndy Whitcroft my $line; 14944d001e4dSAndy Whitcroft while ($cnt) { 14954d001e4dSAndy Whitcroft $line = $rawlines[$offset++]; 14964d001e4dSAndy Whitcroft next if (defined($line) && $line =~ /^-/); 14974d001e4dSAndy Whitcroft $cnt--; 14984d001e4dSAndy Whitcroft } 14994d001e4dSAndy Whitcroft 15004d001e4dSAndy Whitcroft return $line; 15014d001e4dSAndy Whitcroft} 15024d001e4dSAndy Whitcroft 15030a920b5bSAndy Whitcroftsub cat_vet { 15040a920b5bSAndy Whitcroft my ($vet) = @_; 15059c0ca6f9SAndy Whitcroft my ($res, $coded); 15060a920b5bSAndy Whitcroft 15079c0ca6f9SAndy Whitcroft $res = ''; 15086c72ffaaSAndy Whitcroft while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) { 15096c72ffaaSAndy Whitcroft $res .= $1; 15106c72ffaaSAndy Whitcroft if ($2 ne '') { 15119c0ca6f9SAndy Whitcroft $coded = sprintf("^%c", unpack('C', $2) + 64); 15126c72ffaaSAndy Whitcroft $res .= $coded; 15136c72ffaaSAndy Whitcroft } 15149c0ca6f9SAndy Whitcroft } 15159c0ca6f9SAndy Whitcroft $res =~ s/$/\$/; 15160a920b5bSAndy Whitcroft 15179c0ca6f9SAndy Whitcroft return $res; 15180a920b5bSAndy Whitcroft} 15190a920b5bSAndy Whitcroft 1520c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0; 1521cf655043SAndy Whitcroftmy $av_pending; 1522c2fdda0dSAndy Whitcroftmy @av_paren_type; 15231f65f947SAndy Whitcroftmy $av_pend_colon; 1524c2fdda0dSAndy Whitcroft 1525c2fdda0dSAndy Whitcroftsub annotate_reset { 1526c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 1527cf655043SAndy Whitcroft $av_pending = '_'; 1528cf655043SAndy Whitcroft @av_paren_type = ('E'); 15291f65f947SAndy Whitcroft $av_pend_colon = 'O'; 1530c2fdda0dSAndy Whitcroft} 1531c2fdda0dSAndy Whitcroft 15326c72ffaaSAndy Whitcroftsub annotate_values { 15336c72ffaaSAndy Whitcroft my ($stream, $type) = @_; 15346c72ffaaSAndy Whitcroft 15356c72ffaaSAndy Whitcroft my $res; 15361f65f947SAndy Whitcroft my $var = '_' x length($stream); 15376c72ffaaSAndy Whitcroft my $cur = $stream; 15386c72ffaaSAndy Whitcroft 1539c2fdda0dSAndy Whitcroft print "$stream\n" if ($dbg_values > 1); 15406c72ffaaSAndy Whitcroft 15416c72ffaaSAndy Whitcroft while (length($cur)) { 1542773647a0SAndy Whitcroft @av_paren_type = ('E') if ($#av_paren_type < 0); 1543cf655043SAndy Whitcroft print " <" . join('', @av_paren_type) . 1544171ae1a4SAndy Whitcroft "> <$type> <$av_pending>" if ($dbg_values > 1); 15456c72ffaaSAndy Whitcroft if ($cur =~ /^(\s+)/o) { 1546c2fdda0dSAndy Whitcroft print "WS($1)\n" if ($dbg_values > 1); 1547c2fdda0dSAndy Whitcroft if ($1 =~ /\n/ && $av_preprocessor) { 1548cf655043SAndy Whitcroft $type = pop(@av_paren_type); 1549c2fdda0dSAndy Whitcroft $av_preprocessor = 0; 15506c72ffaaSAndy Whitcroft } 15516c72ffaaSAndy Whitcroft 1552c023e473SFlorian Mickler } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') { 15539446ef56SAndy Whitcroft print "CAST($1)\n" if ($dbg_values > 1); 15549446ef56SAndy Whitcroft push(@av_paren_type, $type); 1555addcdceaSAndy Whitcroft $type = 'c'; 15569446ef56SAndy Whitcroft 1557e91b6e26SAndy Whitcroft } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) { 1558c2fdda0dSAndy Whitcroft print "DECLARE($1)\n" if ($dbg_values > 1); 15596c72ffaaSAndy Whitcroft $type = 'T'; 15606c72ffaaSAndy Whitcroft 1561389a2fe5SAndy Whitcroft } elsif ($cur =~ /^($Modifier)\s*/) { 1562389a2fe5SAndy Whitcroft print "MODIFIER($1)\n" if ($dbg_values > 1); 1563389a2fe5SAndy Whitcroft $type = 'T'; 1564389a2fe5SAndy Whitcroft 1565c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) { 1566171ae1a4SAndy Whitcroft print "DEFINE($1,$2)\n" if ($dbg_values > 1); 1567c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1568171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 1569171ae1a4SAndy Whitcroft if ($2 ne '') { 1570cf655043SAndy Whitcroft $av_pending = 'N'; 1571171ae1a4SAndy Whitcroft } 1572171ae1a4SAndy Whitcroft $type = 'E'; 1573171ae1a4SAndy Whitcroft 1574c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) { 1575171ae1a4SAndy Whitcroft print "UNDEF($1)\n" if ($dbg_values > 1); 1576171ae1a4SAndy Whitcroft $av_preprocessor = 1; 1577171ae1a4SAndy Whitcroft push(@av_paren_type, $type); 15786c72ffaaSAndy Whitcroft 1579c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) { 1580cf655043SAndy Whitcroft print "PRE_START($1)\n" if ($dbg_values > 1); 1581c2fdda0dSAndy Whitcroft $av_preprocessor = 1; 1582cf655043SAndy Whitcroft 1583cf655043SAndy Whitcroft push(@av_paren_type, $type); 1584cf655043SAndy Whitcroft push(@av_paren_type, $type); 1585171ae1a4SAndy Whitcroft $type = 'E'; 1586cf655043SAndy Whitcroft 1587c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) { 1588cf655043SAndy Whitcroft print "PRE_RESTART($1)\n" if ($dbg_values > 1); 1589cf655043SAndy Whitcroft $av_preprocessor = 1; 1590cf655043SAndy Whitcroft 1591cf655043SAndy Whitcroft push(@av_paren_type, $av_paren_type[$#av_paren_type]); 1592cf655043SAndy Whitcroft 1593171ae1a4SAndy Whitcroft $type = 'E'; 1594cf655043SAndy Whitcroft 1595c45dcabdSAndy Whitcroft } elsif ($cur =~ /^(\#\s*(?:endif))/o) { 1596cf655043SAndy Whitcroft print "PRE_END($1)\n" if ($dbg_values > 1); 1597cf655043SAndy Whitcroft 1598cf655043SAndy Whitcroft $av_preprocessor = 1; 1599cf655043SAndy Whitcroft 1600cf655043SAndy Whitcroft # Assume all arms of the conditional end as this 1601cf655043SAndy Whitcroft # one does, and continue as if the #endif was not here. 1602cf655043SAndy Whitcroft pop(@av_paren_type); 1603cf655043SAndy Whitcroft push(@av_paren_type, $type); 1604171ae1a4SAndy Whitcroft $type = 'E'; 16056c72ffaaSAndy Whitcroft 16066c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\\\n)/o) { 1607c2fdda0dSAndy Whitcroft print "PRECONT($1)\n" if ($dbg_values > 1); 16086c72ffaaSAndy Whitcroft 1609171ae1a4SAndy Whitcroft } elsif ($cur =~ /^(__attribute__)\s*\(?/o) { 1610171ae1a4SAndy Whitcroft print "ATTR($1)\n" if ($dbg_values > 1); 1611171ae1a4SAndy Whitcroft $av_pending = $type; 1612171ae1a4SAndy Whitcroft $type = 'N'; 1613171ae1a4SAndy Whitcroft 16146c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(sizeof)\s*(\()?/o) { 1615c2fdda0dSAndy Whitcroft print "SIZEOF($1)\n" if ($dbg_values > 1); 16166c72ffaaSAndy Whitcroft if (defined $2) { 1617cf655043SAndy Whitcroft $av_pending = 'V'; 16186c72ffaaSAndy Whitcroft } 16196c72ffaaSAndy Whitcroft $type = 'N'; 16206c72ffaaSAndy Whitcroft 162114b111c1SAndy Whitcroft } elsif ($cur =~ /^(if|while|for)\b/o) { 1622c2fdda0dSAndy Whitcroft print "COND($1)\n" if ($dbg_values > 1); 162314b111c1SAndy Whitcroft $av_pending = 'E'; 16246c72ffaaSAndy Whitcroft $type = 'N'; 16256c72ffaaSAndy Whitcroft 16261f65f947SAndy Whitcroft } elsif ($cur =~/^(case)/o) { 16271f65f947SAndy Whitcroft print "CASE($1)\n" if ($dbg_values > 1); 16281f65f947SAndy Whitcroft $av_pend_colon = 'C'; 16291f65f947SAndy Whitcroft $type = 'N'; 16301f65f947SAndy Whitcroft 163114b111c1SAndy Whitcroft } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) { 1632c2fdda0dSAndy Whitcroft print "KEYWORD($1)\n" if ($dbg_values > 1); 16336c72ffaaSAndy Whitcroft $type = 'N'; 16346c72ffaaSAndy Whitcroft 16356c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\()/o) { 1636c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 1637cf655043SAndy Whitcroft push(@av_paren_type, $av_pending); 1638cf655043SAndy Whitcroft $av_pending = '_'; 16396c72ffaaSAndy Whitcroft $type = 'N'; 16406c72ffaaSAndy Whitcroft 16416c72ffaaSAndy Whitcroft } elsif ($cur =~ /^(\))/o) { 1642cf655043SAndy Whitcroft my $new_type = pop(@av_paren_type); 1643cf655043SAndy Whitcroft if ($new_type ne '_') { 1644cf655043SAndy Whitcroft $type = $new_type; 1645c2fdda0dSAndy Whitcroft print "PAREN('$1') -> $type\n" 1646c2fdda0dSAndy Whitcroft if ($dbg_values > 1); 16476c72ffaaSAndy Whitcroft } else { 1648c2fdda0dSAndy Whitcroft print "PAREN('$1')\n" if ($dbg_values > 1); 16496c72ffaaSAndy Whitcroft } 16506c72ffaaSAndy Whitcroft 1651c8cb2ca3SAndy Whitcroft } elsif ($cur =~ /^($Ident)\s*\(/o) { 1652c2fdda0dSAndy Whitcroft print "FUNC($1)\n" if ($dbg_values > 1); 1653c8cb2ca3SAndy Whitcroft $type = 'V'; 1654cf655043SAndy Whitcroft $av_pending = 'V'; 16556c72ffaaSAndy Whitcroft 16568e761b04SAndy Whitcroft } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) { 16578e761b04SAndy Whitcroft if (defined $2 && $type eq 'C' || $type eq 'T') { 16581f65f947SAndy Whitcroft $av_pend_colon = 'B'; 16598e761b04SAndy Whitcroft } elsif ($type eq 'E') { 16608e761b04SAndy Whitcroft $av_pend_colon = 'L'; 16611f65f947SAndy Whitcroft } 16621f65f947SAndy Whitcroft print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1); 16631f65f947SAndy Whitcroft $type = 'V'; 16641f65f947SAndy Whitcroft 16656c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Ident|$Constant)/o) { 1666c2fdda0dSAndy Whitcroft print "IDENT($1)\n" if ($dbg_values > 1); 16676c72ffaaSAndy Whitcroft $type = 'V'; 16686c72ffaaSAndy Whitcroft 16696c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Assignment)/o) { 1670c2fdda0dSAndy Whitcroft print "ASSIGN($1)\n" if ($dbg_values > 1); 16716c72ffaaSAndy Whitcroft $type = 'N'; 16726c72ffaaSAndy Whitcroft 1673cf655043SAndy Whitcroft } elsif ($cur =~/^(;|{|})/) { 1674c2fdda0dSAndy Whitcroft print "END($1)\n" if ($dbg_values > 1); 167513214adfSAndy Whitcroft $type = 'E'; 16761f65f947SAndy Whitcroft $av_pend_colon = 'O'; 167713214adfSAndy Whitcroft 16788e761b04SAndy Whitcroft } elsif ($cur =~/^(,)/) { 16798e761b04SAndy Whitcroft print "COMMA($1)\n" if ($dbg_values > 1); 16808e761b04SAndy Whitcroft $type = 'C'; 16818e761b04SAndy Whitcroft 16821f65f947SAndy Whitcroft } elsif ($cur =~ /^(\?)/o) { 16831f65f947SAndy Whitcroft print "QUESTION($1)\n" if ($dbg_values > 1); 16841f65f947SAndy Whitcroft $type = 'N'; 16851f65f947SAndy Whitcroft 16861f65f947SAndy Whitcroft } elsif ($cur =~ /^(:)/o) { 16871f65f947SAndy Whitcroft print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1); 16881f65f947SAndy Whitcroft 16891f65f947SAndy Whitcroft substr($var, length($res), 1, $av_pend_colon); 16901f65f947SAndy Whitcroft if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') { 16911f65f947SAndy Whitcroft $type = 'E'; 16921f65f947SAndy Whitcroft } else { 16931f65f947SAndy Whitcroft $type = 'N'; 16941f65f947SAndy Whitcroft } 16951f65f947SAndy Whitcroft $av_pend_colon = 'O'; 16961f65f947SAndy Whitcroft 16978e761b04SAndy Whitcroft } elsif ($cur =~ /^(\[)/o) { 169813214adfSAndy Whitcroft print "CLOSE($1)\n" if ($dbg_values > 1); 16996c72ffaaSAndy Whitcroft $type = 'N'; 17006c72ffaaSAndy Whitcroft 17010d413866SAndy Whitcroft } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) { 170274048ed8SAndy Whitcroft my $variant; 170374048ed8SAndy Whitcroft 170474048ed8SAndy Whitcroft print "OPV($1)\n" if ($dbg_values > 1); 170574048ed8SAndy Whitcroft if ($type eq 'V') { 170674048ed8SAndy Whitcroft $variant = 'B'; 170774048ed8SAndy Whitcroft } else { 170874048ed8SAndy Whitcroft $variant = 'U'; 170974048ed8SAndy Whitcroft } 171074048ed8SAndy Whitcroft 171174048ed8SAndy Whitcroft substr($var, length($res), 1, $variant); 171274048ed8SAndy Whitcroft $type = 'N'; 171374048ed8SAndy Whitcroft 17146c72ffaaSAndy Whitcroft } elsif ($cur =~ /^($Operators)/o) { 1715c2fdda0dSAndy Whitcroft print "OP($1)\n" if ($dbg_values > 1); 17166c72ffaaSAndy Whitcroft if ($1 ne '++' && $1 ne '--') { 17176c72ffaaSAndy Whitcroft $type = 'N'; 17186c72ffaaSAndy Whitcroft } 17196c72ffaaSAndy Whitcroft 17206c72ffaaSAndy Whitcroft } elsif ($cur =~ /(^.)/o) { 1721c2fdda0dSAndy Whitcroft print "C($1)\n" if ($dbg_values > 1); 17226c72ffaaSAndy Whitcroft } 17236c72ffaaSAndy Whitcroft if (defined $1) { 17246c72ffaaSAndy Whitcroft $cur = substr($cur, length($1)); 17256c72ffaaSAndy Whitcroft $res .= $type x length($1); 17266c72ffaaSAndy Whitcroft } 17276c72ffaaSAndy Whitcroft } 17286c72ffaaSAndy Whitcroft 17291f65f947SAndy Whitcroft return ($res, $var); 17306c72ffaaSAndy Whitcroft} 17316c72ffaaSAndy Whitcroft 17328905a67cSAndy Whitcroftsub possible { 173313214adfSAndy Whitcroft my ($possible, $line) = @_; 17349a974fdbSAndy Whitcroft my $notPermitted = qr{(?: 17350776e594SAndy Whitcroft ^(?: 17360776e594SAndy Whitcroft $Modifier| 17370776e594SAndy Whitcroft $Storage| 17380776e594SAndy Whitcroft $Type| 17399a974fdbSAndy Whitcroft DEFINE_\S+ 17409a974fdbSAndy Whitcroft )$| 17419a974fdbSAndy Whitcroft ^(?: 17420776e594SAndy Whitcroft goto| 17430776e594SAndy Whitcroft return| 17440776e594SAndy Whitcroft case| 17450776e594SAndy Whitcroft else| 17460776e594SAndy Whitcroft asm|__asm__| 174789a88353SAndy Whitcroft do| 174889a88353SAndy Whitcroft \#| 174989a88353SAndy Whitcroft \#\#| 17509a974fdbSAndy Whitcroft )(?:\s|$)| 17510776e594SAndy Whitcroft ^(?:typedef|struct|enum)\b 17529a974fdbSAndy Whitcroft )}x; 17539a974fdbSAndy Whitcroft warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2); 17549a974fdbSAndy Whitcroft if ($possible !~ $notPermitted) { 1755c45dcabdSAndy Whitcroft # Check for modifiers. 1756c45dcabdSAndy Whitcroft $possible =~ s/\s*$Storage\s*//g; 1757c45dcabdSAndy Whitcroft $possible =~ s/\s*$Sparse\s*//g; 1758c45dcabdSAndy Whitcroft if ($possible =~ /^\s*$/) { 1759c45dcabdSAndy Whitcroft 1760c45dcabdSAndy Whitcroft } elsif ($possible =~ /\s/) { 1761c45dcabdSAndy Whitcroft $possible =~ s/\s*$Type\s*//g; 1762d2506586SAndy Whitcroft for my $modifier (split(' ', $possible)) { 17639a974fdbSAndy Whitcroft if ($modifier !~ $notPermitted) { 1764d2506586SAndy Whitcroft warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible); 1765485ff23eSAlex Dowad push(@modifierListFile, $modifier); 1766d2506586SAndy Whitcroft } 17679a974fdbSAndy Whitcroft } 1768c45dcabdSAndy Whitcroft 1769c45dcabdSAndy Whitcroft } else { 177013214adfSAndy Whitcroft warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible); 1771485ff23eSAlex Dowad push(@typeListFile, $possible); 1772c45dcabdSAndy Whitcroft } 17738905a67cSAndy Whitcroft build_types(); 17740776e594SAndy Whitcroft } else { 17750776e594SAndy Whitcroft warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1); 17768905a67cSAndy Whitcroft } 17778905a67cSAndy Whitcroft} 17788905a67cSAndy Whitcroft 17796c72ffaaSAndy Whitcroftmy $prefix = ''; 17806c72ffaaSAndy Whitcroft 1781000d1cc1SJoe Perchessub show_type { 1782cbec18afSJoe Perches my ($type) = @_; 178391bfe484SJoe Perches 1784cbec18afSJoe Perches return defined $use_type{$type} if (scalar keys %use_type > 0); 1785cbec18afSJoe Perches 1786cbec18afSJoe Perches return !defined $ignore_type{$type}; 1787000d1cc1SJoe Perches} 1788000d1cc1SJoe Perches 1789f0a594c1SAndy Whitcroftsub report { 1790cbec18afSJoe Perches my ($level, $type, $msg) = @_; 1791cbec18afSJoe Perches 1792cbec18afSJoe Perches if (!show_type($type) || 1793cbec18afSJoe Perches (defined $tst_only && $msg !~ /\Q$tst_only\E/)) { 1794773647a0SAndy Whitcroft return 0; 1795773647a0SAndy Whitcroft } 179657230297SJoe Perches my $output = ''; 179757230297SJoe Perches if (-t STDOUT && $color) { 179857230297SJoe Perches if ($level eq 'ERROR') { 179957230297SJoe Perches $output .= RED; 180057230297SJoe Perches } elsif ($level eq 'WARNING') { 180157230297SJoe Perches $output .= YELLOW; 1802000d1cc1SJoe Perches } else { 180357230297SJoe Perches $output .= GREEN; 1804000d1cc1SJoe Perches } 180557230297SJoe Perches } 180657230297SJoe Perches $output .= $prefix . $level . ':'; 180757230297SJoe Perches if ($show_types) { 180857230297SJoe Perches $output .= BLUE if (-t STDOUT && $color); 180957230297SJoe Perches $output .= "$type:"; 181057230297SJoe Perches } 181157230297SJoe Perches $output .= RESET if (-t STDOUT && $color); 181257230297SJoe Perches $output .= ' ' . $msg . "\n"; 181334d8815fSJoe Perches 181434d8815fSJoe Perches if ($showfile) { 181534d8815fSJoe Perches my @lines = split("\n", $output, -1); 181634d8815fSJoe Perches splice(@lines, 1, 1); 181734d8815fSJoe Perches $output = join("\n", @lines); 181834d8815fSJoe Perches } 181957230297SJoe Perches $output = (split('\n', $output))[0] . "\n" if ($terse); 18208905a67cSAndy Whitcroft 182157230297SJoe Perches push(our @report, $output); 1822773647a0SAndy Whitcroft 1823773647a0SAndy Whitcroft return 1; 1824f0a594c1SAndy Whitcroft} 1825cbec18afSJoe Perches 1826f0a594c1SAndy Whitcroftsub report_dump { 182713214adfSAndy Whitcroft our @report; 1828f0a594c1SAndy Whitcroft} 1829000d1cc1SJoe Perches 1830d752fcc8SJoe Perchessub fixup_current_range { 1831d752fcc8SJoe Perches my ($lineRef, $offset, $length) = @_; 1832d752fcc8SJoe Perches 1833d752fcc8SJoe Perches if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) { 1834d752fcc8SJoe Perches my $o = $1; 1835d752fcc8SJoe Perches my $l = $2; 1836d752fcc8SJoe Perches my $no = $o + $offset; 1837d752fcc8SJoe Perches my $nl = $l + $length; 1838d752fcc8SJoe Perches $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/; 1839d752fcc8SJoe Perches } 1840d752fcc8SJoe Perches} 1841d752fcc8SJoe Perches 1842d752fcc8SJoe Perchessub fix_inserted_deleted_lines { 1843d752fcc8SJoe Perches my ($linesRef, $insertedRef, $deletedRef) = @_; 1844d752fcc8SJoe Perches 1845d752fcc8SJoe Perches my $range_last_linenr = 0; 1846d752fcc8SJoe Perches my $delta_offset = 0; 1847d752fcc8SJoe Perches 1848d752fcc8SJoe Perches my $old_linenr = 0; 1849d752fcc8SJoe Perches my $new_linenr = 0; 1850d752fcc8SJoe Perches 1851d752fcc8SJoe Perches my $next_insert = 0; 1852d752fcc8SJoe Perches my $next_delete = 0; 1853d752fcc8SJoe Perches 1854d752fcc8SJoe Perches my @lines = (); 1855d752fcc8SJoe Perches 1856d752fcc8SJoe Perches my $inserted = @{$insertedRef}[$next_insert++]; 1857d752fcc8SJoe Perches my $deleted = @{$deletedRef}[$next_delete++]; 1858d752fcc8SJoe Perches 1859d752fcc8SJoe Perches foreach my $old_line (@{$linesRef}) { 1860d752fcc8SJoe Perches my $save_line = 1; 1861d752fcc8SJoe Perches my $line = $old_line; #don't modify the array 1862323b267fSJoe Perches if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename 1863d752fcc8SJoe Perches $delta_offset = 0; 1864d752fcc8SJoe Perches } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk 1865d752fcc8SJoe Perches $range_last_linenr = $new_linenr; 1866d752fcc8SJoe Perches fixup_current_range(\$line, $delta_offset, 0); 1867d752fcc8SJoe Perches } 1868d752fcc8SJoe Perches 1869d752fcc8SJoe Perches while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) { 1870d752fcc8SJoe Perches $deleted = @{$deletedRef}[$next_delete++]; 1871d752fcc8SJoe Perches $save_line = 0; 1872d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1); 1873d752fcc8SJoe Perches } 1874d752fcc8SJoe Perches 1875d752fcc8SJoe Perches while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) { 1876d752fcc8SJoe Perches push(@lines, ${$inserted}{'LINE'}); 1877d752fcc8SJoe Perches $inserted = @{$insertedRef}[$next_insert++]; 1878d752fcc8SJoe Perches $new_linenr++; 1879d752fcc8SJoe Perches fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1); 1880d752fcc8SJoe Perches } 1881d752fcc8SJoe Perches 1882d752fcc8SJoe Perches if ($save_line) { 1883d752fcc8SJoe Perches push(@lines, $line); 1884d752fcc8SJoe Perches $new_linenr++; 1885d752fcc8SJoe Perches } 1886d752fcc8SJoe Perches 1887d752fcc8SJoe Perches $old_linenr++; 1888d752fcc8SJoe Perches } 1889d752fcc8SJoe Perches 1890d752fcc8SJoe Perches return @lines; 1891d752fcc8SJoe Perches} 1892d752fcc8SJoe Perches 1893f2d7e4d4SJoe Perchessub fix_insert_line { 1894f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 1895f2d7e4d4SJoe Perches 1896f2d7e4d4SJoe Perches my $inserted = { 1897f2d7e4d4SJoe Perches LINENR => $linenr, 1898f2d7e4d4SJoe Perches LINE => $line, 1899f2d7e4d4SJoe Perches }; 1900f2d7e4d4SJoe Perches push(@fixed_inserted, $inserted); 1901f2d7e4d4SJoe Perches} 1902f2d7e4d4SJoe Perches 1903f2d7e4d4SJoe Perchessub fix_delete_line { 1904f2d7e4d4SJoe Perches my ($linenr, $line) = @_; 1905f2d7e4d4SJoe Perches 1906f2d7e4d4SJoe Perches my $deleted = { 1907f2d7e4d4SJoe Perches LINENR => $linenr, 1908f2d7e4d4SJoe Perches LINE => $line, 1909f2d7e4d4SJoe Perches }; 1910f2d7e4d4SJoe Perches 1911f2d7e4d4SJoe Perches push(@fixed_deleted, $deleted); 1912f2d7e4d4SJoe Perches} 1913f2d7e4d4SJoe Perches 1914de7d4f0eSAndy Whitcroftsub ERROR { 1915cbec18afSJoe Perches my ($type, $msg) = @_; 1916cbec18afSJoe Perches 1917cbec18afSJoe Perches if (report("ERROR", $type, $msg)) { 1918de7d4f0eSAndy Whitcroft our $clean = 0; 19196c72ffaaSAndy Whitcroft our $cnt_error++; 19203705ce5bSJoe Perches return 1; 1921de7d4f0eSAndy Whitcroft } 19223705ce5bSJoe Perches return 0; 1923773647a0SAndy Whitcroft} 1924de7d4f0eSAndy Whitcroftsub WARN { 1925cbec18afSJoe Perches my ($type, $msg) = @_; 1926cbec18afSJoe Perches 1927cbec18afSJoe Perches if (report("WARNING", $type, $msg)) { 1928de7d4f0eSAndy Whitcroft our $clean = 0; 19296c72ffaaSAndy Whitcroft our $cnt_warn++; 19303705ce5bSJoe Perches return 1; 1931de7d4f0eSAndy Whitcroft } 19323705ce5bSJoe Perches return 0; 1933773647a0SAndy Whitcroft} 1934de7d4f0eSAndy Whitcroftsub CHK { 1935cbec18afSJoe Perches my ($type, $msg) = @_; 1936cbec18afSJoe Perches 1937cbec18afSJoe Perches if ($check && report("CHECK", $type, $msg)) { 1938de7d4f0eSAndy Whitcroft our $clean = 0; 19396c72ffaaSAndy Whitcroft our $cnt_chk++; 19403705ce5bSJoe Perches return 1; 19416c72ffaaSAndy Whitcroft } 19423705ce5bSJoe Perches return 0; 1943de7d4f0eSAndy Whitcroft} 1944de7d4f0eSAndy Whitcroft 19456ecd9674SAndy Whitcroftsub check_absolute_file { 19466ecd9674SAndy Whitcroft my ($absolute, $herecurr) = @_; 19476ecd9674SAndy Whitcroft my $file = $absolute; 19486ecd9674SAndy Whitcroft 19496ecd9674SAndy Whitcroft ##print "absolute<$absolute>\n"; 19506ecd9674SAndy Whitcroft 19516ecd9674SAndy Whitcroft # See if any suffix of this path is a path within the tree. 19526ecd9674SAndy Whitcroft while ($file =~ s@^[^/]*/@@) { 19536ecd9674SAndy Whitcroft if (-f "$root/$file") { 19546ecd9674SAndy Whitcroft ##print "file<$file>\n"; 19556ecd9674SAndy Whitcroft last; 19566ecd9674SAndy Whitcroft } 19576ecd9674SAndy Whitcroft } 19586ecd9674SAndy Whitcroft if (! -f _) { 19596ecd9674SAndy Whitcroft return 0; 19606ecd9674SAndy Whitcroft } 19616ecd9674SAndy Whitcroft 19626ecd9674SAndy Whitcroft # It is, so see if the prefix is acceptable. 19636ecd9674SAndy Whitcroft my $prefix = $absolute; 19646ecd9674SAndy Whitcroft substr($prefix, -length($file)) = ''; 19656ecd9674SAndy Whitcroft 19666ecd9674SAndy Whitcroft ##print "prefix<$prefix>\n"; 19676ecd9674SAndy Whitcroft if ($prefix ne ".../") { 1968000d1cc1SJoe Perches WARN("USE_RELATIVE_PATH", 1969000d1cc1SJoe Perches "use relative pathname instead of absolute in changelog text\n" . $herecurr); 19706ecd9674SAndy Whitcroft } 19716ecd9674SAndy Whitcroft} 19726ecd9674SAndy Whitcroft 19733705ce5bSJoe Perchessub trim { 19743705ce5bSJoe Perches my ($string) = @_; 19753705ce5bSJoe Perches 1976b34c648bSJoe Perches $string =~ s/^\s+|\s+$//g; 1977b34c648bSJoe Perches 1978b34c648bSJoe Perches return $string; 1979b34c648bSJoe Perches} 1980b34c648bSJoe Perches 1981b34c648bSJoe Perchessub ltrim { 1982b34c648bSJoe Perches my ($string) = @_; 1983b34c648bSJoe Perches 1984b34c648bSJoe Perches $string =~ s/^\s+//; 1985b34c648bSJoe Perches 1986b34c648bSJoe Perches return $string; 1987b34c648bSJoe Perches} 1988b34c648bSJoe Perches 1989b34c648bSJoe Perchessub rtrim { 1990b34c648bSJoe Perches my ($string) = @_; 1991b34c648bSJoe Perches 1992b34c648bSJoe Perches $string =~ s/\s+$//; 19933705ce5bSJoe Perches 19943705ce5bSJoe Perches return $string; 19953705ce5bSJoe Perches} 19963705ce5bSJoe Perches 199752ea8506SJoe Perchessub string_find_replace { 199852ea8506SJoe Perches my ($string, $find, $replace) = @_; 199952ea8506SJoe Perches 200052ea8506SJoe Perches $string =~ s/$find/$replace/g; 200152ea8506SJoe Perches 200252ea8506SJoe Perches return $string; 200352ea8506SJoe Perches} 200452ea8506SJoe Perches 20053705ce5bSJoe Perchessub tabify { 20063705ce5bSJoe Perches my ($leading) = @_; 20073705ce5bSJoe Perches 20083705ce5bSJoe Perches my $source_indent = 8; 20093705ce5bSJoe Perches my $max_spaces_before_tab = $source_indent - 1; 20103705ce5bSJoe Perches my $spaces_to_tab = " " x $source_indent; 20113705ce5bSJoe Perches 20123705ce5bSJoe Perches #convert leading spaces to tabs 20133705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g; 20143705ce5bSJoe Perches #Remove spaces before a tab 20153705ce5bSJoe Perches 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g; 20163705ce5bSJoe Perches 20173705ce5bSJoe Perches return "$leading"; 20183705ce5bSJoe Perches} 20193705ce5bSJoe Perches 2020d1fe9c09SJoe Perchessub pos_last_openparen { 2021d1fe9c09SJoe Perches my ($line) = @_; 2022d1fe9c09SJoe Perches 2023d1fe9c09SJoe Perches my $pos = 0; 2024d1fe9c09SJoe Perches 2025d1fe9c09SJoe Perches my $opens = $line =~ tr/\(/\(/; 2026d1fe9c09SJoe Perches my $closes = $line =~ tr/\)/\)/; 2027d1fe9c09SJoe Perches 2028d1fe9c09SJoe Perches my $last_openparen = 0; 2029d1fe9c09SJoe Perches 2030d1fe9c09SJoe Perches if (($opens == 0) || ($closes >= $opens)) { 2031d1fe9c09SJoe Perches return -1; 2032d1fe9c09SJoe Perches } 2033d1fe9c09SJoe Perches 2034d1fe9c09SJoe Perches my $len = length($line); 2035d1fe9c09SJoe Perches 2036d1fe9c09SJoe Perches for ($pos = 0; $pos < $len; $pos++) { 2037d1fe9c09SJoe Perches my $string = substr($line, $pos); 2038d1fe9c09SJoe Perches if ($string =~ /^($FuncArg|$balanced_parens)/) { 2039d1fe9c09SJoe Perches $pos += length($1) - 1; 2040d1fe9c09SJoe Perches } elsif (substr($line, $pos, 1) eq '(') { 2041d1fe9c09SJoe Perches $last_openparen = $pos; 2042d1fe9c09SJoe Perches } elsif (index($string, '(') == -1) { 2043d1fe9c09SJoe Perches last; 2044d1fe9c09SJoe Perches } 2045d1fe9c09SJoe Perches } 2046d1fe9c09SJoe Perches 204791cb5195SJoe Perches return length(expand_tabs(substr($line, 0, $last_openparen))) + 1; 2048d1fe9c09SJoe Perches} 2049d1fe9c09SJoe Perches 20500a920b5bSAndy Whitcroftsub process { 20510a920b5bSAndy Whitcroft my $filename = shift; 20520a920b5bSAndy Whitcroft 20530a920b5bSAndy Whitcroft my $linenr=0; 20540a920b5bSAndy Whitcroft my $prevline=""; 2055c2fdda0dSAndy Whitcroft my $prevrawline=""; 20560a920b5bSAndy Whitcroft my $stashline=""; 2057c2fdda0dSAndy Whitcroft my $stashrawline=""; 20580a920b5bSAndy Whitcroft 20594a0df2efSAndy Whitcroft my $length; 20600a920b5bSAndy Whitcroft my $indent; 20610a920b5bSAndy Whitcroft my $previndent=0; 20620a920b5bSAndy Whitcroft my $stashindent=0; 20630a920b5bSAndy Whitcroft 2064de7d4f0eSAndy Whitcroft our $clean = 1; 20650a920b5bSAndy Whitcroft my $signoff = 0; 20660a920b5bSAndy Whitcroft my $is_patch = 0; 206729ee1b0cSJoe Perches my $in_header_lines = $file ? 0 : 1; 206815662b3eSJoe Perches my $in_commit_log = 0; #Scanning lines before patch 2069bf4daf12SJoe Perches my $commit_log_possible_stack_dump = 0; 20702a076f40SJoe Perches my $commit_log_long_line = 0; 2071e518e9a5SJoe Perches my $commit_log_has_diff = 0; 207213f1937eSJoe Perches my $reported_maintainer_file = 0; 2073fa64205dSPasi Savanainen my $non_utf8_charset = 0; 2074fa64205dSPasi Savanainen 2075365dd4eaSJoe Perches my $last_blank_line = 0; 20765e4f6ba5SJoe Perches my $last_coalesced_string_linenr = -1; 2077365dd4eaSJoe Perches 207813214adfSAndy Whitcroft our @report = (); 20796c72ffaaSAndy Whitcroft our $cnt_lines = 0; 20806c72ffaaSAndy Whitcroft our $cnt_error = 0; 20816c72ffaaSAndy Whitcroft our $cnt_warn = 0; 20826c72ffaaSAndy Whitcroft our $cnt_chk = 0; 20836c72ffaaSAndy Whitcroft 20840a920b5bSAndy Whitcroft # Trace the real file/line as we go. 20850a920b5bSAndy Whitcroft my $realfile = ''; 20860a920b5bSAndy Whitcroft my $realline = 0; 20870a920b5bSAndy Whitcroft my $realcnt = 0; 20880a920b5bSAndy Whitcroft my $here = ''; 20890a920b5bSAndy Whitcroft my $in_comment = 0; 2090c2fdda0dSAndy Whitcroft my $comment_edge = 0; 20910a920b5bSAndy Whitcroft my $first_line = 0; 20921e855726SWolfram Sang my $p1_prefix = ''; 20930a920b5bSAndy Whitcroft 209413214adfSAndy Whitcroft my $prev_values = 'E'; 209513214adfSAndy Whitcroft 209613214adfSAndy Whitcroft # suppression flags 2097773647a0SAndy Whitcroft my %suppress_ifbraces; 2098170d3a22SAndy Whitcroft my %suppress_whiletrailers; 20992b474a1aSAndy Whitcroft my %suppress_export; 21003e469cdcSAndy Whitcroft my $suppress_statement = 0; 2101653d4876SAndy Whitcroft 21027e51f197SJoe Perches my %signatures = (); 2103323c1260SJoe Perches 2104c2fdda0dSAndy Whitcroft # Pre-scan the patch sanitizing the lines. 2105de7d4f0eSAndy Whitcroft # Pre-scan the patch looking for any __setup documentation. 2106c2fdda0dSAndy Whitcroft # 2107de7d4f0eSAndy Whitcroft my @setup_docs = (); 2108de7d4f0eSAndy Whitcroft my $setup_docs = 0; 2109773647a0SAndy Whitcroft 2110d8b07710SJoe Perches my $camelcase_file_seeded = 0; 2111d8b07710SJoe Perches 2112773647a0SAndy Whitcroft sanitise_line_reset(); 2113c2fdda0dSAndy Whitcroft my $line; 2114c2fdda0dSAndy Whitcroft foreach my $rawline (@rawlines) { 2115773647a0SAndy Whitcroft $linenr++; 2116773647a0SAndy Whitcroft $line = $rawline; 2117c2fdda0dSAndy Whitcroft 21183705ce5bSJoe Perches push(@fixed, $rawline) if ($fix); 21193705ce5bSJoe Perches 2120773647a0SAndy Whitcroft if ($rawline=~/^\+\+\+\s+(\S+)/) { 2121de7d4f0eSAndy Whitcroft $setup_docs = 0; 2122de7d4f0eSAndy Whitcroft if ($1 =~ m@Documentation/kernel-parameters.txt$@) { 2123de7d4f0eSAndy Whitcroft $setup_docs = 1; 2124de7d4f0eSAndy Whitcroft } 2125773647a0SAndy Whitcroft #next; 2126de7d4f0eSAndy Whitcroft } 2127773647a0SAndy Whitcroft if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 2128773647a0SAndy Whitcroft $realline=$1-1; 2129773647a0SAndy Whitcroft if (defined $2) { 2130773647a0SAndy Whitcroft $realcnt=$3+1; 2131773647a0SAndy Whitcroft } else { 2132773647a0SAndy Whitcroft $realcnt=1+1; 2133773647a0SAndy Whitcroft } 2134c45dcabdSAndy Whitcroft $in_comment = 0; 2135773647a0SAndy Whitcroft 2136773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. Run 2137773647a0SAndy Whitcroft # the context looking for a comment "edge". If this 2138773647a0SAndy Whitcroft # edge is a close comment then we must be in a comment 2139773647a0SAndy Whitcroft # at context start. 2140773647a0SAndy Whitcroft my $edge; 214101fa9147SAndy Whitcroft my $cnt = $realcnt; 214201fa9147SAndy Whitcroft for (my $ln = $linenr + 1; $cnt > 0; $ln++) { 214301fa9147SAndy Whitcroft next if (defined $rawlines[$ln - 1] && 214401fa9147SAndy Whitcroft $rawlines[$ln - 1] =~ /^-/); 214501fa9147SAndy Whitcroft $cnt--; 214601fa9147SAndy Whitcroft #print "RAW<$rawlines[$ln - 1]>\n"; 2147721c1cb6SAndy Whitcroft last if (!defined $rawlines[$ln - 1]); 2148fae17daeSAndy Whitcroft if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ && 2149fae17daeSAndy Whitcroft $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) { 2150fae17daeSAndy Whitcroft ($edge) = $1; 2151fae17daeSAndy Whitcroft last; 2152fae17daeSAndy Whitcroft } 2153773647a0SAndy Whitcroft } 2154773647a0SAndy Whitcroft if (defined $edge && $edge eq '*/') { 2155773647a0SAndy Whitcroft $in_comment = 1; 2156773647a0SAndy Whitcroft } 2157773647a0SAndy Whitcroft 2158773647a0SAndy Whitcroft # Guestimate if this is a continuing comment. If this 2159773647a0SAndy Whitcroft # is the start of a diff block and this line starts 2160773647a0SAndy Whitcroft # ' *' then it is very likely a comment. 2161773647a0SAndy Whitcroft if (!defined $edge && 216283242e0cSAndy Whitcroft $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@) 2163773647a0SAndy Whitcroft { 2164773647a0SAndy Whitcroft $in_comment = 1; 2165773647a0SAndy Whitcroft } 2166773647a0SAndy Whitcroft 2167773647a0SAndy Whitcroft ##print "COMMENT:$in_comment edge<$edge> $rawline\n"; 2168773647a0SAndy Whitcroft sanitise_line_reset($in_comment); 2169773647a0SAndy Whitcroft 2170171ae1a4SAndy Whitcroft } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) { 2171773647a0SAndy Whitcroft # Standardise the strings and chars within the input to 2172171ae1a4SAndy Whitcroft # simplify matching -- only bother with positive lines. 2173773647a0SAndy Whitcroft $line = sanitise_line($rawline); 2174773647a0SAndy Whitcroft } 2175773647a0SAndy Whitcroft push(@lines, $line); 2176773647a0SAndy Whitcroft 2177773647a0SAndy Whitcroft if ($realcnt > 1) { 2178773647a0SAndy Whitcroft $realcnt-- if ($line =~ /^(?:\+| |$)/); 2179773647a0SAndy Whitcroft } else { 2180773647a0SAndy Whitcroft $realcnt = 0; 2181773647a0SAndy Whitcroft } 2182773647a0SAndy Whitcroft 2183773647a0SAndy Whitcroft #print "==>$rawline\n"; 2184773647a0SAndy Whitcroft #print "-->$line\n"; 2185de7d4f0eSAndy Whitcroft 2186de7d4f0eSAndy Whitcroft if ($setup_docs && $line =~ /^\+/) { 2187de7d4f0eSAndy Whitcroft push(@setup_docs, $line); 2188de7d4f0eSAndy Whitcroft } 2189de7d4f0eSAndy Whitcroft } 2190de7d4f0eSAndy Whitcroft 21916c72ffaaSAndy Whitcroft $prefix = ''; 21926c72ffaaSAndy Whitcroft 2193773647a0SAndy Whitcroft $realcnt = 0; 2194773647a0SAndy Whitcroft $linenr = 0; 2195194f66fcSJoe Perches $fixlinenr = -1; 21960a920b5bSAndy Whitcroft foreach my $line (@lines) { 21970a920b5bSAndy Whitcroft $linenr++; 2198194f66fcSJoe Perches $fixlinenr++; 21991b5539b1SJoe Perches my $sline = $line; #copy of $line 22001b5539b1SJoe Perches $sline =~ s/$;/ /g; #with comments as spaces 22010a920b5bSAndy Whitcroft 2202c2fdda0dSAndy Whitcroft my $rawline = $rawlines[$linenr - 1]; 22036c72ffaaSAndy Whitcroft 22040a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied 2205e518e9a5SJoe Perches if (!$in_commit_log && 2206e518e9a5SJoe Perches $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) { 22070a920b5bSAndy Whitcroft $is_patch = 1; 22084a0df2efSAndy Whitcroft $first_line = $linenr + 1; 22090a920b5bSAndy Whitcroft $realline=$1-1; 22100a920b5bSAndy Whitcroft if (defined $2) { 22110a920b5bSAndy Whitcroft $realcnt=$3+1; 22120a920b5bSAndy Whitcroft } else { 22130a920b5bSAndy Whitcroft $realcnt=1+1; 22140a920b5bSAndy Whitcroft } 2215c2fdda0dSAndy Whitcroft annotate_reset(); 221613214adfSAndy Whitcroft $prev_values = 'E'; 221713214adfSAndy Whitcroft 2218773647a0SAndy Whitcroft %suppress_ifbraces = (); 2219170d3a22SAndy Whitcroft %suppress_whiletrailers = (); 22202b474a1aSAndy Whitcroft %suppress_export = (); 22213e469cdcSAndy Whitcroft $suppress_statement = 0; 22220a920b5bSAndy Whitcroft next; 22230a920b5bSAndy Whitcroft 22244a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that 22254a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely 22264a0df2efSAndy Whitcroft# blank context lines so we need to count that too. 2227773647a0SAndy Whitcroft } elsif ($line =~ /^( |\+|$)/) { 22280a920b5bSAndy Whitcroft $realline++; 2229d8aaf121SAndy Whitcroft $realcnt-- if ($realcnt != 0); 22300a920b5bSAndy Whitcroft 22314a0df2efSAndy Whitcroft # Measure the line length and indent. 2232c2fdda0dSAndy Whitcroft ($length, $indent) = line_stats($rawline); 22330a920b5bSAndy Whitcroft 22340a920b5bSAndy Whitcroft # Track the previous line. 22350a920b5bSAndy Whitcroft ($prevline, $stashline) = ($stashline, $line); 22360a920b5bSAndy Whitcroft ($previndent, $stashindent) = ($stashindent, $indent); 2237c2fdda0dSAndy Whitcroft ($prevrawline, $stashrawline) = ($stashrawline, $rawline); 2238c2fdda0dSAndy Whitcroft 2239773647a0SAndy Whitcroft #warn "line<$line>\n"; 22406c72ffaaSAndy Whitcroft 2241d8aaf121SAndy Whitcroft } elsif ($realcnt == 1) { 2242d8aaf121SAndy Whitcroft $realcnt--; 22430a920b5bSAndy Whitcroft } 22440a920b5bSAndy Whitcroft 2245cc77cdcaSAndy Whitcroft my $hunk_line = ($realcnt != 0); 2246cc77cdcaSAndy Whitcroft 22476c72ffaaSAndy Whitcroft $here = "#$linenr: " if (!$file); 22486c72ffaaSAndy Whitcroft $here = "#$realline: " if ($file); 2249773647a0SAndy Whitcroft 22502ac73b4fSJoe Perches my $found_file = 0; 2251773647a0SAndy Whitcroft # extract the filename as it passes 22523bf9a009SRabin Vincent if ($line =~ /^diff --git.*?(\S+)$/) { 22533bf9a009SRabin Vincent $realfile = $1; 22542b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2255270c49a0SJoe Perches $in_commit_log = 0; 22562ac73b4fSJoe Perches $found_file = 1; 22573bf9a009SRabin Vincent } elsif ($line =~ /^\+\+\+\s+(\S+)/) { 2258773647a0SAndy Whitcroft $realfile = $1; 22592b7ab453SJoe Perches $realfile =~ s@^([^/]*)/@@ if (!$file); 2260270c49a0SJoe Perches $in_commit_log = 0; 22611e855726SWolfram Sang 22621e855726SWolfram Sang $p1_prefix = $1; 2263e2f7aa4bSAndy Whitcroft if (!$file && $tree && $p1_prefix ne '' && 2264e2f7aa4bSAndy Whitcroft -e "$root/$p1_prefix") { 2265000d1cc1SJoe Perches WARN("PATCH_PREFIX", 2266000d1cc1SJoe Perches "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n"); 22671e855726SWolfram Sang } 2268773647a0SAndy Whitcroft 2269c1ab3326SAndy Whitcroft if ($realfile =~ m@^include/asm/@) { 2270000d1cc1SJoe Perches ERROR("MODIFIED_INCLUDE_ASM", 2271000d1cc1SJoe Perches "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n"); 2272773647a0SAndy Whitcroft } 22732ac73b4fSJoe Perches $found_file = 1; 22742ac73b4fSJoe Perches } 22752ac73b4fSJoe Perches 227634d8815fSJoe Perches#make up the handle for any error we report on this line 227734d8815fSJoe Perches if ($showfile) { 227834d8815fSJoe Perches $prefix = "$realfile:$realline: " 227934d8815fSJoe Perches } elsif ($emacs) { 22807d3a9f67SJoe Perches if ($file) { 22817d3a9f67SJoe Perches $prefix = "$filename:$realline: "; 22827d3a9f67SJoe Perches } else { 228334d8815fSJoe Perches $prefix = "$filename:$linenr: "; 228434d8815fSJoe Perches } 22857d3a9f67SJoe Perches } 228634d8815fSJoe Perches 22872ac73b4fSJoe Perches if ($found_file) { 22887bd7e483SJoe Perches if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) { 22892ac73b4fSJoe Perches $check = 1; 22902ac73b4fSJoe Perches } else { 22912ac73b4fSJoe Perches $check = $check_orig; 22922ac73b4fSJoe Perches } 2293773647a0SAndy Whitcroft next; 2294773647a0SAndy Whitcroft } 2295773647a0SAndy Whitcroft 2296389834b6SRandy Dunlap $here .= "FILE: $realfile:$realline:" if ($realcnt != 0); 22970a920b5bSAndy Whitcroft 2298c2fdda0dSAndy Whitcroft my $hereline = "$here\n$rawline\n"; 2299c2fdda0dSAndy Whitcroft my $herecurr = "$here\n$rawline\n"; 2300c2fdda0dSAndy Whitcroft my $hereprev = "$here\n$prevrawline\n$rawline\n"; 23010a920b5bSAndy Whitcroft 23026c72ffaaSAndy Whitcroft $cnt_lines++ if ($realcnt != 0); 23036c72ffaaSAndy Whitcroft 2304e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch 2305e518e9a5SJoe Perches if ($in_commit_log && !$commit_log_has_diff && 2306e518e9a5SJoe Perches (($line =~ m@^\s+diff\b.*a/[\w/]+@ && 2307e518e9a5SJoe Perches $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) || 2308e518e9a5SJoe Perches $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ || 2309e518e9a5SJoe Perches $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) { 2310e518e9a5SJoe Perches ERROR("DIFF_IN_COMMIT_MSG", 2311e518e9a5SJoe Perches "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr); 2312e518e9a5SJoe Perches $commit_log_has_diff = 1; 2313e518e9a5SJoe Perches } 2314e518e9a5SJoe Perches 23153bf9a009SRabin Vincent# Check for incorrect file permissions 23163bf9a009SRabin Vincent if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) { 23173bf9a009SRabin Vincent my $permhere = $here . "FILE: $realfile\n"; 231804db4d25SJoe Perches if ($realfile !~ m@scripts/@ && 231904db4d25SJoe Perches $realfile !~ /\.(py|pl|awk|sh)$/) { 2320000d1cc1SJoe Perches ERROR("EXECUTE_PERMISSIONS", 2321000d1cc1SJoe Perches "do not set execute permissions for source files\n" . $permhere); 23223bf9a009SRabin Vincent } 23233bf9a009SRabin Vincent } 23243bf9a009SRabin Vincent 232520112475SJoe Perches# Check the patch for a signoff: 2326d8aaf121SAndy Whitcroft if ($line =~ /^\s*signed-off-by:/i) { 23274a0df2efSAndy Whitcroft $signoff++; 232815662b3eSJoe Perches $in_commit_log = 0; 23290a920b5bSAndy Whitcroft } 233020112475SJoe Perches 2331e0d975b1SJoe Perches# Check if MAINTAINERS is being updated. If so, there's probably no need to 2332e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete 2333e0d975b1SJoe Perches if ($line =~ /^\s*MAINTAINERS\s*\|/) { 2334e0d975b1SJoe Perches $reported_maintainer_file = 1; 2335e0d975b1SJoe Perches } 2336e0d975b1SJoe Perches 233720112475SJoe Perches# Check signature styles 2338270c49a0SJoe Perches if (!$in_header_lines && 2339ce0338dfSJoe Perches $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) { 234020112475SJoe Perches my $space_before = $1; 234120112475SJoe Perches my $sign_off = $2; 234220112475SJoe Perches my $space_after = $3; 234320112475SJoe Perches my $email = $4; 234420112475SJoe Perches my $ucfirst_sign_off = ucfirst(lc($sign_off)); 234520112475SJoe Perches 2346ce0338dfSJoe Perches if ($sign_off !~ /$signature_tags/) { 2347ce0338dfSJoe Perches WARN("BAD_SIGN_OFF", 2348ce0338dfSJoe Perches "Non-standard signature: $sign_off\n" . $herecurr); 2349ce0338dfSJoe Perches } 235020112475SJoe Perches if (defined $space_before && $space_before ne "") { 23513705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 23523705ce5bSJoe Perches "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) && 23533705ce5bSJoe Perches $fix) { 2354194f66fcSJoe Perches $fixed[$fixlinenr] = 23553705ce5bSJoe Perches "$ucfirst_sign_off $email"; 23563705ce5bSJoe Perches } 235720112475SJoe Perches } 235820112475SJoe Perches if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) { 23593705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 23603705ce5bSJoe Perches "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) && 23613705ce5bSJoe Perches $fix) { 2362194f66fcSJoe Perches $fixed[$fixlinenr] = 23633705ce5bSJoe Perches "$ucfirst_sign_off $email"; 23643705ce5bSJoe Perches } 23653705ce5bSJoe Perches 236620112475SJoe Perches } 236720112475SJoe Perches if (!defined $space_after || $space_after ne " ") { 23683705ce5bSJoe Perches if (WARN("BAD_SIGN_OFF", 23693705ce5bSJoe Perches "Use a single space after $ucfirst_sign_off\n" . $herecurr) && 23703705ce5bSJoe Perches $fix) { 2371194f66fcSJoe Perches $fixed[$fixlinenr] = 23723705ce5bSJoe Perches "$ucfirst_sign_off $email"; 23733705ce5bSJoe Perches } 237420112475SJoe Perches } 237520112475SJoe Perches 237620112475SJoe Perches my ($email_name, $email_address, $comment) = parse_email($email); 237720112475SJoe Perches my $suggested_email = format_email(($email_name, $email_address)); 237820112475SJoe Perches if ($suggested_email eq "") { 2379000d1cc1SJoe Perches ERROR("BAD_SIGN_OFF", 2380000d1cc1SJoe Perches "Unrecognized email address: '$email'\n" . $herecurr); 238120112475SJoe Perches } else { 238220112475SJoe Perches my $dequoted = $suggested_email; 238320112475SJoe Perches $dequoted =~ s/^"//; 238420112475SJoe Perches $dequoted =~ s/" </ </; 238520112475SJoe Perches # Don't force email to have quotes 238620112475SJoe Perches # Allow just an angle bracketed address 238720112475SJoe Perches if ("$dequoted$comment" ne $email && 238820112475SJoe Perches "<$email_address>$comment" ne $email && 238920112475SJoe Perches "$suggested_email$comment" ne $email) { 2390000d1cc1SJoe Perches WARN("BAD_SIGN_OFF", 2391000d1cc1SJoe Perches "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr); 239220112475SJoe Perches } 23930a920b5bSAndy Whitcroft } 23947e51f197SJoe Perches 23957e51f197SJoe Perches# Check for duplicate signatures 23967e51f197SJoe Perches my $sig_nospace = $line; 23977e51f197SJoe Perches $sig_nospace =~ s/\s//g; 23987e51f197SJoe Perches $sig_nospace = lc($sig_nospace); 23997e51f197SJoe Perches if (defined $signatures{$sig_nospace}) { 24007e51f197SJoe Perches WARN("BAD_SIGN_OFF", 24017e51f197SJoe Perches "Duplicate signature\n" . $herecurr); 24027e51f197SJoe Perches } else { 24037e51f197SJoe Perches $signatures{$sig_nospace} = 1; 24047e51f197SJoe Perches } 24050a920b5bSAndy Whitcroft } 24060a920b5bSAndy Whitcroft 2407a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned 2408a2fe16b9SJoe Perches if ($in_header_lines && 2409a2fe16b9SJoe Perches $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) { 2410a2fe16b9SJoe Perches WARN("EMAIL_SUBJECT", 2411a2fe16b9SJoe Perches "A patch subject line should describe the change not the tool that found it\n" . $herecurr); 2412a2fe16b9SJoe Perches } 2413a2fe16b9SJoe Perches 24149b3189ebSJoe Perches# Check for old stable address 24159b3189ebSJoe Perches if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) { 24169b3189ebSJoe Perches ERROR("STABLE_ADDRESS", 24179b3189ebSJoe Perches "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr); 24189b3189ebSJoe Perches } 24199b3189ebSJoe Perches 24207ebd05efSChristopher Covington# Check for unwanted Gerrit info 24217ebd05efSChristopher Covington if ($in_commit_log && $line =~ /^\s*change-id:/i) { 24227ebd05efSChristopher Covington ERROR("GERRIT_CHANGE_ID", 24237ebd05efSChristopher Covington "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr); 24247ebd05efSChristopher Covington } 24257ebd05efSChristopher Covington 2426369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump 2427369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2428369c8dd3SJoe Perches ($line =~ /^\s*(?:WARNING:|BUG:)/ || 2429369c8dd3SJoe Perches $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ || 2430369c8dd3SJoe Perches # timestamp 2431369c8dd3SJoe Perches $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) { 2432369c8dd3SJoe Perches # stack dump address 2433369c8dd3SJoe Perches $commit_log_possible_stack_dump = 1; 2434369c8dd3SJoe Perches } 2435369c8dd3SJoe Perches 24362a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once 24372a076f40SJoe Perches if ($in_commit_log && !$commit_log_long_line && 2438bf4daf12SJoe Perches length($line) > 75 && 2439bf4daf12SJoe Perches !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ || 2440bf4daf12SJoe Perches # file delta changes 2441bf4daf12SJoe Perches $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ || 2442bf4daf12SJoe Perches # filename then : 2443bf4daf12SJoe Perches $line =~ /^\s*(?:Fixes:|Link:)/i || 2444bf4daf12SJoe Perches # A Fixes: or Link: line 2445bf4daf12SJoe Perches $commit_log_possible_stack_dump)) { 24462a076f40SJoe Perches WARN("COMMIT_LOG_LONG_LINE", 24472a076f40SJoe Perches "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr); 24482a076f40SJoe Perches $commit_log_long_line = 1; 24492a076f40SJoe Perches } 24502a076f40SJoe Perches 2451bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found 2452bf4daf12SJoe Perches if ($in_commit_log && $commit_log_possible_stack_dump && 2453bf4daf12SJoe Perches $line =~ /^\s*$/) { 2454bf4daf12SJoe Perches $commit_log_possible_stack_dump = 0; 2455bf4daf12SJoe Perches } 2456bf4daf12SJoe Perches 24570d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions 2458369c8dd3SJoe Perches if ($in_commit_log && !$commit_log_possible_stack_dump && 2459fe043ea1SJoe Perches ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i || 2460bf4daf12SJoe Perches ($line =~ /\b[0-9a-f]{12,40}\b/i && 2461369c8dd3SJoe Perches $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i && 2462bf4daf12SJoe Perches $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) { 2463fe043ea1SJoe Perches my $init_char = "c"; 2464fe043ea1SJoe Perches my $orig_commit = ""; 24650d7835fcSJoe Perches my $short = 1; 24660d7835fcSJoe Perches my $long = 0; 24670d7835fcSJoe Perches my $case = 1; 24680d7835fcSJoe Perches my $space = 1; 24690d7835fcSJoe Perches my $hasdesc = 0; 247019c146a6SJoe Perches my $hasparens = 0; 24710d7835fcSJoe Perches my $id = '0123456789ab'; 24720d7835fcSJoe Perches my $orig_desc = "commit description"; 24730d7835fcSJoe Perches my $description = ""; 24740d7835fcSJoe Perches 2475fe043ea1SJoe Perches if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) { 2476fe043ea1SJoe Perches $init_char = $1; 2477fe043ea1SJoe Perches $orig_commit = lc($2); 2478fe043ea1SJoe Perches } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) { 2479fe043ea1SJoe Perches $orig_commit = lc($1); 2480fe043ea1SJoe Perches } 2481fe043ea1SJoe Perches 24820d7835fcSJoe Perches $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i); 24830d7835fcSJoe Perches $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i); 24840d7835fcSJoe Perches $space = 0 if ($line =~ /\bcommit [0-9a-f]/i); 24850d7835fcSJoe Perches $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/); 24860d7835fcSJoe Perches if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) { 24870d7835fcSJoe Perches $orig_desc = $1; 248819c146a6SJoe Perches $hasparens = 1; 24890d7835fcSJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i && 24900d7835fcSJoe Perches defined $rawlines[$linenr] && 24910d7835fcSJoe Perches $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) { 24920d7835fcSJoe Perches $orig_desc = $1; 249319c146a6SJoe Perches $hasparens = 1; 2494b671fde0SJoe Perches } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i && 2495b671fde0SJoe Perches defined $rawlines[$linenr] && 2496b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*[^"]+"\)/) { 2497b671fde0SJoe Perches $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i; 2498b671fde0SJoe Perches $orig_desc = $1; 2499b671fde0SJoe Perches $rawlines[$linenr] =~ /^\s*([^"]+)"\)/; 2500b671fde0SJoe Perches $orig_desc .= " " . $1; 250119c146a6SJoe Perches $hasparens = 1; 25020d7835fcSJoe Perches } 25030d7835fcSJoe Perches 25040d7835fcSJoe Perches ($id, $description) = git_commit_info($orig_commit, 25050d7835fcSJoe Perches $id, $orig_desc); 25060d7835fcSJoe Perches 250719c146a6SJoe Perches if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) { 2508d311cd44SJoe Perches ERROR("GIT_COMMIT_ID", 25090d7835fcSJoe Perches "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr); 25100d7835fcSJoe Perches } 2511d311cd44SJoe Perches } 2512d311cd44SJoe Perches 251313f1937eSJoe Perches# Check for added, moved or deleted files 251413f1937eSJoe Perches if (!$reported_maintainer_file && !$in_commit_log && 251513f1937eSJoe Perches ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ || 251613f1937eSJoe Perches $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ || 251713f1937eSJoe Perches ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ && 251813f1937eSJoe Perches (defined($1) || defined($2))))) { 251913f1937eSJoe Perches $reported_maintainer_file = 1; 252013f1937eSJoe Perches WARN("FILE_PATH_CHANGES", 252113f1937eSJoe Perches "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr); 252213f1937eSJoe Perches } 252313f1937eSJoe Perches 252400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file 25258905a67cSAndy Whitcroft if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) { 2526000d1cc1SJoe Perches ERROR("CORRUPTED_PATCH", 2527000d1cc1SJoe Perches "patch seems to be corrupt (line wrapped?)\n" . 25286c72ffaaSAndy Whitcroft $herecurr) if (!$emitted_corrupt++); 2529de7d4f0eSAndy Whitcroft } 2530de7d4f0eSAndy Whitcroft 25316ecd9674SAndy Whitcroft# Check for absolute kernel paths. 25326ecd9674SAndy Whitcroft if ($tree) { 25336ecd9674SAndy Whitcroft while ($line =~ m{(?:^|\s)(/\S*)}g) { 25346ecd9674SAndy Whitcroft my $file = $1; 25356ecd9674SAndy Whitcroft 25366ecd9674SAndy Whitcroft if ($file =~ m{^(.*?)(?::\d+)+:?$} && 25376ecd9674SAndy Whitcroft check_absolute_file($1, $herecurr)) { 25386ecd9674SAndy Whitcroft # 25396ecd9674SAndy Whitcroft } else { 25406ecd9674SAndy Whitcroft check_absolute_file($file, $herecurr); 25416ecd9674SAndy Whitcroft } 25426ecd9674SAndy Whitcroft } 25436ecd9674SAndy Whitcroft } 25446ecd9674SAndy Whitcroft 2545de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php 2546de7d4f0eSAndy Whitcroft if (($realfile =~ /^$/ || $line =~ /^\+/) && 2547171ae1a4SAndy Whitcroft $rawline !~ m/^$UTF8*$/) { 2548171ae1a4SAndy Whitcroft my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/); 2549171ae1a4SAndy Whitcroft 2550171ae1a4SAndy Whitcroft my $blank = copy_spacing($rawline); 2551171ae1a4SAndy Whitcroft my $ptr = substr($blank, 0, length($utf8_prefix)) . "^"; 2552171ae1a4SAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 2553171ae1a4SAndy Whitcroft 255434d99219SJoe Perches CHK("INVALID_UTF8", 2555000d1cc1SJoe Perches "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr); 255600df344fSAndy Whitcroft } 25570a920b5bSAndy Whitcroft 255815662b3eSJoe Perches# Check if it's the start of a commit log 255915662b3eSJoe Perches# (not a header line and we haven't seen the patch filename) 256015662b3eSJoe Perches if ($in_header_lines && $realfile =~ /^$/ && 256129ee1b0cSJoe Perches !($rawline =~ /^\s+\S/ || 256229ee1b0cSJoe Perches $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) { 256315662b3eSJoe Perches $in_header_lines = 0; 256415662b3eSJoe Perches $in_commit_log = 1; 256515662b3eSJoe Perches } 256615662b3eSJoe Perches 2567fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly 2568fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing. 2569fa64205dSPasi Savanainen if ($in_header_lines && 2570fa64205dSPasi Savanainen $rawline =~ /^Content-Type:.+charset="(.+)".*$/ && 2571fa64205dSPasi Savanainen $1 !~ /utf-8/i) { 2572fa64205dSPasi Savanainen $non_utf8_charset = 1; 2573fa64205dSPasi Savanainen } 2574fa64205dSPasi Savanainen 2575fa64205dSPasi Savanainen if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ && 257615662b3eSJoe Perches $rawline =~ /$NON_ASCII_UTF8/) { 2577fa64205dSPasi Savanainen WARN("UTF8_BEFORE_PATCH", 257815662b3eSJoe Perches "8-bit UTF-8 used in possible commit log\n" . $herecurr); 257915662b3eSJoe Perches } 258015662b3eSJoe Perches 258166b47b4aSKees Cook# Check for various typo / spelling mistakes 258266d7a382SJoe Perches if (defined($misspellings) && 258366d7a382SJoe Perches ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) { 2584ebfd7d62SJoe Perches while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) { 258566b47b4aSKees Cook my $typo = $1; 258666b47b4aSKees Cook my $typo_fix = $spelling_fix{lc($typo)}; 258766b47b4aSKees Cook $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/); 258866b47b4aSKees Cook $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/); 258966b47b4aSKees Cook my $msg_type = \&WARN; 259066b47b4aSKees Cook $msg_type = \&CHK if ($file); 259166b47b4aSKees Cook if (&{$msg_type}("TYPO_SPELLING", 259266b47b4aSKees Cook "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) && 259366b47b4aSKees Cook $fix) { 259466b47b4aSKees Cook $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/; 259566b47b4aSKees Cook } 259666b47b4aSKees Cook } 259766b47b4aSKees Cook } 259866b47b4aSKees Cook 259930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed 260030670854SAndy Whitcroft next if (!$hunk_line || $line =~ /^-/); 260100df344fSAndy Whitcroft 26020a920b5bSAndy Whitcroft#trailing whitespace 26039c0ca6f9SAndy Whitcroft if ($line =~ /^\+.*\015/) { 2604c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2605d5e616fcSJoe Perches if (ERROR("DOS_LINE_ENDINGS", 2606d5e616fcSJoe Perches "DOS line endings\n" . $herevet) && 2607d5e616fcSJoe Perches $fix) { 2608194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/[\s\015]+$//; 2609d5e616fcSJoe Perches } 2610c2fdda0dSAndy Whitcroft } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) { 2611c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 26123705ce5bSJoe Perches if (ERROR("TRAILING_WHITESPACE", 26133705ce5bSJoe Perches "trailing whitespace\n" . $herevet) && 26143705ce5bSJoe Perches $fix) { 2615194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 26163705ce5bSJoe Perches } 26173705ce5bSJoe Perches 2618d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 26190a920b5bSAndy Whitcroft } 26205368df20SAndy Whitcroft 26214783f894SJosh Triplett# Check for FSF mailing addresses. 2622109d8cb2SAlexander Duyck if ($rawline =~ /\bwrite to the Free/i || 26233e2232f2SJoe Perches $rawline =~ /\b59\s+Temple\s+Pl/i || 26243e2232f2SJoe Perches $rawline =~ /\b51\s+Franklin\s+St/i) { 26254783f894SJosh Triplett my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 26264783f894SJosh Triplett my $msg_type = \&ERROR; 26274783f894SJosh Triplett $msg_type = \&CHK if ($file); 26284783f894SJosh Triplett &{$msg_type}("FSF_MAILING_ADDRESS", 26294783f894SJosh 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) 26304783f894SJosh Triplett } 26314783f894SJosh Triplett 26323354957aSAndi Kleen# check for Kconfig help text having a real description 26339fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have 26349fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough. 26353354957aSAndi Kleen if ($realfile =~ /Kconfig/ && 26368d73e0e7SJoe Perches $line =~ /^\+\s*config\s+/) { 26373354957aSAndi Kleen my $length = 0; 26389fe287d7SAndy Whitcroft my $cnt = $realcnt; 26399fe287d7SAndy Whitcroft my $ln = $linenr + 1; 26409fe287d7SAndy Whitcroft my $f; 2641a1385803SAndy Whitcroft my $is_start = 0; 26429fe287d7SAndy Whitcroft my $is_end = 0; 2643a1385803SAndy Whitcroft for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) { 26449fe287d7SAndy Whitcroft $f = $lines[$ln - 1]; 26459fe287d7SAndy Whitcroft $cnt-- if ($lines[$ln - 1] !~ /^-/); 26469fe287d7SAndy Whitcroft $is_end = $lines[$ln - 1] =~ /^\+/; 26479fe287d7SAndy Whitcroft 26489fe287d7SAndy Whitcroft next if ($f =~ /^-/); 26498d73e0e7SJoe Perches last if (!$file && $f =~ /^\@\@/); 2650a1385803SAndy Whitcroft 26518d73e0e7SJoe Perches if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) { 2652a1385803SAndy Whitcroft $is_start = 1; 26538d73e0e7SJoe Perches } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) { 2654a1385803SAndy Whitcroft $length = -1; 2655a1385803SAndy Whitcroft } 2656a1385803SAndy Whitcroft 26579fe287d7SAndy Whitcroft $f =~ s/^.//; 26583354957aSAndi Kleen $f =~ s/#.*//; 26593354957aSAndi Kleen $f =~ s/^\s+//; 26603354957aSAndi Kleen next if ($f =~ /^$/); 26619fe287d7SAndy Whitcroft if ($f =~ /^\s*config\s/) { 26629fe287d7SAndy Whitcroft $is_end = 1; 26639fe287d7SAndy Whitcroft last; 26649fe287d7SAndy Whitcroft } 26653354957aSAndi Kleen $length++; 26663354957aSAndi Kleen } 266756193274SVadim Bendebury if ($is_start && $is_end && $length < $min_conf_desc_length) { 2668000d1cc1SJoe Perches WARN("CONFIG_DESCRIPTION", 266956193274SVadim Bendebury "please write a paragraph that describes the config symbol fully\n" . $herecurr); 267056193274SVadim Bendebury } 2671a1385803SAndy Whitcroft #print "is_start<$is_start> is_end<$is_end> length<$length>\n"; 26723354957aSAndi Kleen } 26733354957aSAndi Kleen 26741ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig. 26751ba8dfd1SKees Cook if ($realfile =~ /Kconfig/ && 26761ba8dfd1SKees Cook $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) { 26771ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 26781ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 26791ba8dfd1SKees Cook } 26801ba8dfd1SKees Cook 2681327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options 2682327953e9SChristoph Jaeger if ($realfile =~ /Kconfig/ && 2683327953e9SChristoph Jaeger $line =~ /^\+\s*\bboolean\b/) { 2684327953e9SChristoph Jaeger WARN("CONFIG_TYPE_BOOLEAN", 2685327953e9SChristoph Jaeger "Use of boolean is deprecated, please use bool instead.\n" . $herecurr); 2686327953e9SChristoph Jaeger } 2687327953e9SChristoph Jaeger 2688c68e5878SArnaud Lacombe if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) && 2689c68e5878SArnaud Lacombe ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) { 2690c68e5878SArnaud Lacombe my $flag = $1; 2691c68e5878SArnaud Lacombe my $replacement = { 2692c68e5878SArnaud Lacombe 'EXTRA_AFLAGS' => 'asflags-y', 2693c68e5878SArnaud Lacombe 'EXTRA_CFLAGS' => 'ccflags-y', 2694c68e5878SArnaud Lacombe 'EXTRA_CPPFLAGS' => 'cppflags-y', 2695c68e5878SArnaud Lacombe 'EXTRA_LDFLAGS' => 'ldflags-y', 2696c68e5878SArnaud Lacombe }; 2697c68e5878SArnaud Lacombe 2698c68e5878SArnaud Lacombe WARN("DEPRECATED_VARIABLE", 2699c68e5878SArnaud Lacombe "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag}); 2700c68e5878SArnaud Lacombe } 2701c68e5878SArnaud Lacombe 2702bff5da43SRob Herring# check for DT compatible documentation 27037dd05b38SFlorian Vaussard if (defined $root && 27047dd05b38SFlorian Vaussard (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) || 27057dd05b38SFlorian Vaussard ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) { 27067dd05b38SFlorian Vaussard 2707bff5da43SRob Herring my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g; 2708bff5da43SRob Herring 2709cc93319bSFlorian Vaussard my $dt_path = $root . "/Documentation/devicetree/bindings/"; 2710cc93319bSFlorian Vaussard my $vp_file = $dt_path . "vendor-prefixes.txt"; 2711cc93319bSFlorian Vaussard 2712bff5da43SRob Herring foreach my $compat (@compats) { 2713bff5da43SRob Herring my $compat2 = $compat; 2714185d566bSRob Herring $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/; 2715185d566bSRob Herring my $compat3 = $compat; 2716185d566bSRob Herring $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/; 2717185d566bSRob Herring `grep -Erq "$compat|$compat2|$compat3" $dt_path`; 2718bff5da43SRob Herring if ( $? >> 8 ) { 2719bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2720bff5da43SRob Herring "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr); 2721bff5da43SRob Herring } 2722bff5da43SRob Herring 27234fbf32a6SFlorian Vaussard next if $compat !~ /^([a-zA-Z0-9\-]+)\,/; 27244fbf32a6SFlorian Vaussard my $vendor = $1; 2725cc93319bSFlorian Vaussard `grep -Eq "^$vendor\\b" $vp_file`; 2726bff5da43SRob Herring if ( $? >> 8 ) { 2727bff5da43SRob Herring WARN("UNDOCUMENTED_DT_STRING", 2728cc93319bSFlorian Vaussard "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr); 2729bff5da43SRob Herring } 2730bff5da43SRob Herring } 2731bff5da43SRob Herring } 2732bff5da43SRob Herring 27335368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk 2734de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/); 27355368df20SAndy Whitcroft 273647e0c88bSJoe Perches# line length limit (with some exclusions) 273747e0c88bSJoe Perches# 273847e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length: 273947e0c88bSJoe Perches# logging functions like pr_info that end in a string 274047e0c88bSJoe Perches# lines with a single string 274147e0c88bSJoe Perches# #defines that are a single string 274247e0c88bSJoe Perches# 274347e0c88bSJoe Perches# There are 3 different line length message types: 274447e0c88bSJoe Perches# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength 274547e0c88bSJoe Perches# LONG_LINE_STRING a string starts before but extends beyond $max_line_length 274647e0c88bSJoe Perches# LONG_LINE all other lines longer than $max_line_length 274747e0c88bSJoe Perches# 274847e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored 274947e0c88bSJoe Perches# 275047e0c88bSJoe Perches 2751b4749e96SJoe Perches if ($line =~ /^\+/ && $length > $max_line_length) { 275247e0c88bSJoe Perches my $msg_type = "LONG_LINE"; 275347e0c88bSJoe Perches 275447e0c88bSJoe Perches # Check the allowed long line types first 275547e0c88bSJoe Perches 275647e0c88bSJoe Perches # logging functions that end in a string that starts 275747e0c88bSJoe Perches # before $max_line_length 275847e0c88bSJoe Perches if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ && 275947e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 276047e0c88bSJoe Perches $msg_type = ""; 276147e0c88bSJoe Perches 276247e0c88bSJoe Perches # lines with only strings (w/ possible termination) 276347e0c88bSJoe Perches # #defines with only strings 276447e0c88bSJoe Perches } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ || 276547e0c88bSJoe Perches $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) { 276647e0c88bSJoe Perches $msg_type = ""; 276747e0c88bSJoe Perches 276847e0c88bSJoe Perches # Otherwise set the alternate message types 276947e0c88bSJoe Perches 277047e0c88bSJoe Perches # a comment starts before $max_line_length 277147e0c88bSJoe Perches } elsif ($line =~ /($;[\s$;]*)$/ && 277247e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 277347e0c88bSJoe Perches $msg_type = "LONG_LINE_COMMENT" 277447e0c88bSJoe Perches 277547e0c88bSJoe Perches # a quoted string starts before $max_line_length 277647e0c88bSJoe Perches } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ && 277747e0c88bSJoe Perches length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) { 277847e0c88bSJoe Perches $msg_type = "LONG_LINE_STRING" 277947e0c88bSJoe Perches } 278047e0c88bSJoe Perches 278147e0c88bSJoe Perches if ($msg_type ne "" && 278247e0c88bSJoe Perches (show_type("LONG_LINE") || show_type($msg_type))) { 278347e0c88bSJoe Perches WARN($msg_type, 27846cd7f386SJoe Perches "line over $max_line_length characters\n" . $herecurr); 27850a920b5bSAndy Whitcroft } 278647e0c88bSJoe Perches } 27870a920b5bSAndy Whitcroft 27888905a67cSAndy Whitcroft# check for adding lines without a newline. 27898905a67cSAndy Whitcroft if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) { 2790000d1cc1SJoe Perches WARN("MISSING_EOF_NEWLINE", 2791000d1cc1SJoe Perches "adding a line without newline at end of file\n" . $herecurr); 27928905a67cSAndy Whitcroft } 27938905a67cSAndy Whitcroft 279442e41c54SMike Frysinger# Blackfin: use hi/lo macros 279542e41c54SMike Frysinger if ($realfile =~ m@arch/blackfin/.*\.S$@) { 279642e41c54SMike Frysinger if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) { 279742e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2798000d1cc1SJoe Perches ERROR("LO_MACRO", 2799000d1cc1SJoe Perches "use the LO() macro, not (... & 0xFFFF)\n" . $herevet); 280042e41c54SMike Frysinger } 280142e41c54SMike Frysinger if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) { 280242e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 2803000d1cc1SJoe Perches ERROR("HI_MACRO", 2804000d1cc1SJoe Perches "use the HI() macro, not (... >> 16)\n" . $herevet); 280542e41c54SMike Frysinger } 280642e41c54SMike Frysinger } 280742e41c54SMike Frysinger 2808b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk 2809de4c924cSGeert Uytterhoeven next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/); 28100a920b5bSAndy Whitcroft 28110a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything 28120a920b5bSAndy Whitcroft# more than 8 must use tabs. 2813c2fdda0dSAndy Whitcroft if ($rawline =~ /^\+\s* \t\s*\S/ || 2814c2fdda0dSAndy Whitcroft $rawline =~ /^\+\s* \s*/) { 2815c2fdda0dSAndy Whitcroft my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 2816d2c0a235SAndy Whitcroft $rpt_cleaners = 1; 28173705ce5bSJoe Perches if (ERROR("CODE_INDENT", 28183705ce5bSJoe Perches "code indent should use tabs where possible\n" . $herevet) && 28193705ce5bSJoe Perches $fix) { 2820194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 28213705ce5bSJoe Perches } 28220a920b5bSAndy Whitcroft } 28230a920b5bSAndy Whitcroft 282408e44365SAlberto Panizzo# check for space before tabs. 282508e44365SAlberto Panizzo if ($rawline =~ /^\+/ && $rawline =~ / \t/) { 282608e44365SAlberto Panizzo my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 28273705ce5bSJoe Perches if (WARN("SPACE_BEFORE_TAB", 28283705ce5bSJoe Perches "please, no space before tabs\n" . $herevet) && 28293705ce5bSJoe Perches $fix) { 2830194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 2831d2207ccbSJoe Perches s/(^\+.*) {8,8}\t/$1\t\t/) {} 2832194f66fcSJoe Perches while ($fixed[$fixlinenr] =~ 2833c76f4cb3SJoe Perches s/(^\+.*) +\t/$1\t/) {} 28343705ce5bSJoe Perches } 283508e44365SAlberto Panizzo } 283608e44365SAlberto Panizzo 2837d1fe9c09SJoe Perches# check for && or || at the start of a line 2838d1fe9c09SJoe Perches if ($rawline =~ /^\+\s*(&&|\|\|)/) { 2839d1fe9c09SJoe Perches CHK("LOGICAL_CONTINUATIONS", 2840d1fe9c09SJoe Perches "Logical continuations should be on the previous line\n" . $hereprev); 2841d1fe9c09SJoe Perches } 2842d1fe9c09SJoe Perches 2843a91e8994SJoe Perches# check indentation starts on a tab stop 2844a91e8994SJoe Perches if ($^V && $^V ge 5.10.0 && 2845a91e8994SJoe Perches $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) { 2846a91e8994SJoe Perches my $indent = length($1); 2847a91e8994SJoe Perches if ($indent % 8) { 2848a91e8994SJoe Perches if (WARN("TABSTOP", 2849a91e8994SJoe Perches "Statements should start on a tabstop\n" . $herecurr) && 2850a91e8994SJoe Perches $fix) { 2851a91e8994SJoe Perches $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e; 2852a91e8994SJoe Perches } 2853a91e8994SJoe Perches } 2854a91e8994SJoe Perches } 2855a91e8994SJoe Perches 2856d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line 2857d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 285891cb5195SJoe Perches $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) { 2859d1fe9c09SJoe Perches $prevline =~ /^\+(\t*)(.*)$/; 2860d1fe9c09SJoe Perches my $oldindent = $1; 2861d1fe9c09SJoe Perches my $rest = $2; 2862d1fe9c09SJoe Perches 2863d1fe9c09SJoe Perches my $pos = pos_last_openparen($rest); 2864d1fe9c09SJoe Perches if ($pos >= 0) { 2865b34a26f3SJoe Perches $line =~ /^(\+| )([ \t]*)/; 2866b34a26f3SJoe Perches my $newindent = $2; 2867d1fe9c09SJoe Perches 2868d1fe9c09SJoe Perches my $goodtabindent = $oldindent . 2869d1fe9c09SJoe Perches "\t" x ($pos / 8) . 2870d1fe9c09SJoe Perches " " x ($pos % 8); 2871d1fe9c09SJoe Perches my $goodspaceindent = $oldindent . " " x $pos; 2872d1fe9c09SJoe Perches 2873d1fe9c09SJoe Perches if ($newindent ne $goodtabindent && 2874d1fe9c09SJoe Perches $newindent ne $goodspaceindent) { 28753705ce5bSJoe Perches 28763705ce5bSJoe Perches if (CHK("PARENTHESIS_ALIGNMENT", 28773705ce5bSJoe Perches "Alignment should match open parenthesis\n" . $hereprev) && 28783705ce5bSJoe Perches $fix && $line =~ /^\+/) { 2879194f66fcSJoe Perches $fixed[$fixlinenr] =~ 28803705ce5bSJoe Perches s/^\+[ \t]*/\+$goodtabindent/; 28813705ce5bSJoe Perches } 2882d1fe9c09SJoe Perches } 2883d1fe9c09SJoe Perches } 2884d1fe9c09SJoe Perches } 2885d1fe9c09SJoe Perches 28866ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar" 28876ab3a970SJoe Perches# avoid checking a few false positives: 28886ab3a970SJoe Perches# "sizeof(<type>)" or "__alignof__(<type>)" 28896ab3a970SJoe Perches# function pointer declarations like "(*foo)(int) = bar;" 28906ab3a970SJoe Perches# structure definitions like "(struct foo) { 0 };" 28916ab3a970SJoe Perches# multiline macros that define functions 28926ab3a970SJoe Perches# known attributes or the __attribute__ keyword 28936ab3a970SJoe Perches if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ && 28946ab3a970SJoe Perches (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) { 28953705ce5bSJoe Perches if (CHK("SPACING", 2896f27c95dbSJoe Perches "No space is necessary after a cast\n" . $herecurr) && 28973705ce5bSJoe Perches $fix) { 2898194f66fcSJoe Perches $fixed[$fixlinenr] =~ 2899f27c95dbSJoe Perches s/(\(\s*$Type\s*\))[ \t]+/$1/; 29003705ce5bSJoe Perches } 2901aad4f614SJoe Perches } 2902aad4f614SJoe Perches 290386406b1cSJoe Perches# Block comment styles 290486406b1cSJoe Perches# Networking with an initial /* 290505880600SJoe Perches if ($realfile =~ m@^(drivers/net/|net/)@ && 2906fdb4bcd6SJoe Perches $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ && 290785ad978cSJoe Perches $rawline =~ /^\+[ \t]*\*/ && 290885ad978cSJoe Perches $realline > 2) { 290905880600SJoe Perches WARN("NETWORKING_BLOCK_COMMENT_STYLE", 291005880600SJoe Perches "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev); 291105880600SJoe Perches } 291205880600SJoe Perches 291386406b1cSJoe Perches# Block comments use * on subsequent lines 291486406b1cSJoe Perches if ($prevline =~ /$;[ \t]*$/ && #ends in comment 291586406b1cSJoe Perches $prevrawline =~ /^\+.*?\/\*/ && #starting /* 2916a605e32eSJoe Perches $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */ 291761135e96SJoe Perches $rawline =~ /^\+/ && #line is new 2918a605e32eSJoe Perches $rawline !~ /^\+[ \t]*\*/) { #no leading * 291986406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 292086406b1cSJoe Perches "Block comments use * on subsequent lines\n" . $hereprev); 2921a605e32eSJoe Perches } 2922a605e32eSJoe Perches 292386406b1cSJoe Perches# Block comments use */ on trailing lines 292486406b1cSJoe Perches if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */ 2925c24f9f19SJoe Perches $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/ 2926c24f9f19SJoe Perches $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/ 2927c24f9f19SJoe Perches $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */ 292886406b1cSJoe Perches WARN("BLOCK_COMMENT_STYLE", 292986406b1cSJoe Perches "Block comments use a trailing */ on a separate line\n" . $herecurr); 293005880600SJoe Perches } 293105880600SJoe Perches 29327f619191SJoe Perches# check for missing blank lines after struct/union declarations 29337f619191SJoe Perches# with exceptions for various attributes and macros 29347f619191SJoe Perches if ($prevline =~ /^[\+ ]};?\s*$/ && 29357f619191SJoe Perches $line =~ /^\+/ && 29367f619191SJoe Perches !($line =~ /^\+\s*$/ || 29377f619191SJoe Perches $line =~ /^\+\s*EXPORT_SYMBOL/ || 29387f619191SJoe Perches $line =~ /^\+\s*MODULE_/i || 29397f619191SJoe Perches $line =~ /^\+\s*\#\s*(?:end|elif|else)/ || 29407f619191SJoe Perches $line =~ /^\+[a-z_]*init/ || 29417f619191SJoe Perches $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ || 29427f619191SJoe Perches $line =~ /^\+\s*DECLARE/ || 29437f619191SJoe Perches $line =~ /^\+\s*__setup/)) { 2944d752fcc8SJoe Perches if (CHK("LINE_SPACING", 2945d752fcc8SJoe Perches "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) && 2946d752fcc8SJoe Perches $fix) { 2947f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 2948d752fcc8SJoe Perches } 29497f619191SJoe Perches } 29507f619191SJoe Perches 2951365dd4eaSJoe Perches# check for multiple consecutive blank lines 2952365dd4eaSJoe Perches if ($prevline =~ /^[\+ ]\s*$/ && 2953365dd4eaSJoe Perches $line =~ /^\+\s*$/ && 2954365dd4eaSJoe Perches $last_blank_line != ($linenr - 1)) { 2955d752fcc8SJoe Perches if (CHK("LINE_SPACING", 2956d752fcc8SJoe Perches "Please don't use multiple blank lines\n" . $hereprev) && 2957d752fcc8SJoe Perches $fix) { 2958f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 2959d752fcc8SJoe Perches } 2960d752fcc8SJoe Perches 2961365dd4eaSJoe Perches $last_blank_line = $linenr; 2962365dd4eaSJoe Perches } 2963365dd4eaSJoe Perches 29643b617e3bSJoe Perches# check for missing blank lines after declarations 29653f7bac03SJoe Perches if ($sline =~ /^\+\s+\S/ && #Not at char 1 29663f7bac03SJoe Perches # actual declarations 29673f7bac03SJoe Perches ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 29685a4e1fd3SJoe Perches # function pointer declarations 29695a4e1fd3SJoe Perches $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 29703f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 29713f7bac03SJoe Perches $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 29723f7bac03SJoe Perches # known declaration macros 29733f7bac03SJoe Perches $prevline =~ /^\+\s+$declaration_macros/) && 29743f7bac03SJoe Perches # for "else if" which can look like "$Ident $Ident" 29753f7bac03SJoe Perches !($prevline =~ /^\+\s+$c90_Keywords\b/ || 29763f7bac03SJoe Perches # other possible extensions of declaration lines 29773f7bac03SJoe Perches $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ || 29783f7bac03SJoe Perches # not starting a section or a macro "\" extended line 29793f7bac03SJoe Perches $prevline =~ /(?:\{\s*|\\)$/) && 29803f7bac03SJoe Perches # looks like a declaration 29813f7bac03SJoe Perches !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ || 29825a4e1fd3SJoe Perches # function pointer declarations 29835a4e1fd3SJoe Perches $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ || 29843f7bac03SJoe Perches # foo bar; where foo is some local typedef or #define 29853f7bac03SJoe Perches $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ || 29863f7bac03SJoe Perches # known declaration macros 29873f7bac03SJoe Perches $sline =~ /^\+\s+$declaration_macros/ || 29883f7bac03SJoe Perches # start of struct or union or enum 29893b617e3bSJoe Perches $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ || 29903f7bac03SJoe Perches # start or end of block or continuation of declaration 29913f7bac03SJoe Perches $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ || 29923f7bac03SJoe Perches # bitfield continuation 29933f7bac03SJoe Perches $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ || 29943f7bac03SJoe Perches # other possible extensions of declaration lines 29953f7bac03SJoe Perches $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) && 29963f7bac03SJoe Perches # indentation of previous and current line are the same 29973f7bac03SJoe Perches (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) { 2998d752fcc8SJoe Perches if (WARN("LINE_SPACING", 2999d752fcc8SJoe Perches "Missing a blank line after declarations\n" . $hereprev) && 3000d752fcc8SJoe Perches $fix) { 3001f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, "\+"); 3002d752fcc8SJoe Perches } 30033b617e3bSJoe Perches } 30043b617e3bSJoe Perches 30055f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line. 30066b4c5bebSAndy Whitcroft# Exceptions: 30076b4c5bebSAndy Whitcroft# 1) within comments 30086b4c5bebSAndy Whitcroft# 2) indented preprocessor commands 30096b4c5bebSAndy Whitcroft# 3) hanging labels 30103705ce5bSJoe Perches if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) { 30115f7ddae6SRaffaele Recalcati my $herevet = "$here\n" . cat_vet($rawline) . "\n"; 30123705ce5bSJoe Perches if (WARN("LEADING_SPACE", 30133705ce5bSJoe Perches "please, no spaces at the start of a line\n" . $herevet) && 30143705ce5bSJoe Perches $fix) { 3015194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e; 30163705ce5bSJoe Perches } 30175f7ddae6SRaffaele Recalcati } 30185f7ddae6SRaffaele Recalcati 3019b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk 3020b9ea10d6SAndy Whitcroft next if ($realfile !~ /\.(h|c)$/); 3021b9ea10d6SAndy Whitcroft 3022032a4c0fSJoe Perches# check indentation of any line with a bare else 3023840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;") 3024032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more... 3025032a4c0fSJoe Perches if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) { 3026032a4c0fSJoe Perches my $tabs = length($1) + 1; 3027840080a0SJoe Perches if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ || 3028840080a0SJoe Perches ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ && 3029840080a0SJoe Perches defined $lines[$linenr] && 3030840080a0SJoe Perches $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) { 3031032a4c0fSJoe Perches WARN("UNNECESSARY_ELSE", 3032032a4c0fSJoe Perches "else is not generally useful after a break or return\n" . $hereprev); 3033032a4c0fSJoe Perches } 3034032a4c0fSJoe Perches } 3035032a4c0fSJoe Perches 3036c00df19aSJoe Perches# check indentation of a line with a break; 3037c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs 3038c00df19aSJoe Perches if ($sline =~ /^\+([\t]+)break\s*;\s*$/) { 3039c00df19aSJoe Perches my $tabs = $1; 3040c00df19aSJoe Perches if ($prevline =~ /^\+$tabs(?:goto|return)\b/) { 3041c00df19aSJoe Perches WARN("UNNECESSARY_BREAK", 3042c00df19aSJoe Perches "break is not useful after a goto or return\n" . $hereprev); 3043c00df19aSJoe Perches } 3044c00df19aSJoe Perches } 3045c00df19aSJoe Perches 30461ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def). 30471ba8dfd1SKees Cook if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) { 30481ba8dfd1SKees Cook WARN("CONFIG_EXPERIMENTAL", 30491ba8dfd1SKees Cook "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n"); 30501ba8dfd1SKees Cook } 30511ba8dfd1SKees Cook 3052c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers 3053cf655043SAndy Whitcroft if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) { 3054000d1cc1SJoe Perches WARN("CVS_KEYWORD", 3055000d1cc1SJoe Perches "CVS style keyword markers, these will _not_ be updated\n". $herecurr); 3056c2fdda0dSAndy Whitcroft } 305722f2a2efSAndy Whitcroft 305842e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync 305942e41c54SMike Frysinger if ($line =~ /__builtin_bfin_csync/) { 306042e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 3061000d1cc1SJoe Perches ERROR("CSYNC", 3062000d1cc1SJoe Perches "use the CSYNC() macro in asm/blackfin.h\n" . $herevet); 306342e41c54SMike Frysinger } 306442e41c54SMike Frysinger if ($line =~ /__builtin_bfin_ssync/) { 306542e41c54SMike Frysinger my $herevet = "$here\n" . cat_vet($line) . "\n"; 3066000d1cc1SJoe Perches ERROR("SSYNC", 3067000d1cc1SJoe Perches "use the SSYNC() macro in asm/blackfin.h\n" . $herevet); 306842e41c54SMike Frysinger } 306942e41c54SMike Frysinger 307056e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings 307156e77d70SJoe Perches if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) { 307256e77d70SJoe Perches WARN("HOTPLUG_SECTION", 307356e77d70SJoe Perches "Using $1 is unnecessary\n" . $herecurr); 307456e77d70SJoe Perches } 307556e77d70SJoe Perches 30769c0ca6f9SAndy Whitcroft# Check for potential 'bare' types 30772b474a1aSAndy Whitcroft my ($stat, $cond, $line_nr_next, $remain_next, $off_next, 30782b474a1aSAndy Whitcroft $realline_next); 30793e469cdcSAndy Whitcroft#print "LINE<$line>\n"; 30803e469cdcSAndy Whitcroft if ($linenr >= $suppress_statement && 30811b5539b1SJoe Perches $realcnt && $sline =~ /.\s*\S/) { 3082170d3a22SAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 3083f5fe35ddSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 3084171ae1a4SAndy Whitcroft $stat =~ s/\n./\n /g; 3085171ae1a4SAndy Whitcroft $cond =~ s/\n./\n /g; 3086171ae1a4SAndy Whitcroft 30873e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n"; 30883e469cdcSAndy Whitcroft # If this statement has no statement boundaries within 30893e469cdcSAndy Whitcroft # it there is no point in retrying a statement scan 30903e469cdcSAndy Whitcroft # until we hit end of it. 30913e469cdcSAndy Whitcroft my $frag = $stat; $frag =~ s/;+\s*$//; 30923e469cdcSAndy Whitcroft if ($frag !~ /(?:{|;)/) { 30933e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n"; 30943e469cdcSAndy Whitcroft $suppress_statement = $line_nr_next; 30953e469cdcSAndy Whitcroft } 3096f74bd194SAndy Whitcroft 30972b474a1aSAndy Whitcroft # Find the real next line. 30982b474a1aSAndy Whitcroft $realline_next = $line_nr_next; 30992b474a1aSAndy Whitcroft if (defined $realline_next && 31002b474a1aSAndy Whitcroft (!defined $lines[$realline_next - 1] || 31012b474a1aSAndy Whitcroft substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) { 31022b474a1aSAndy Whitcroft $realline_next++; 31032b474a1aSAndy Whitcroft } 31042b474a1aSAndy Whitcroft 3105171ae1a4SAndy Whitcroft my $s = $stat; 3106171ae1a4SAndy Whitcroft $s =~ s/{.*$//s; 3107cf655043SAndy Whitcroft 3108c2fdda0dSAndy Whitcroft # Ignore goto labels. 3109171ae1a4SAndy Whitcroft if ($s =~ /$Ident:\*$/s) { 3110c2fdda0dSAndy Whitcroft 3111c2fdda0dSAndy Whitcroft # Ignore functions being called 3112171ae1a4SAndy Whitcroft } elsif ($s =~ /^.\s*$Ident\s*\(/s) { 3113c2fdda0dSAndy Whitcroft 3114463f2864SAndy Whitcroft } elsif ($s =~ /^.\s*else\b/s) { 3115463f2864SAndy Whitcroft 3116c45dcabdSAndy Whitcroft # declarations always start with types 3117d2506586SAndy 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) { 3118c45dcabdSAndy Whitcroft my $type = $1; 3119c45dcabdSAndy Whitcroft $type =~ s/\s+/ /g; 3120c45dcabdSAndy Whitcroft possible($type, "A:" . $s); 3121c45dcabdSAndy Whitcroft 31226c72ffaaSAndy Whitcroft # definitions in global scope can only start with types 3123a6a84062SAndy Whitcroft } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) { 3124c45dcabdSAndy Whitcroft possible($1, "B:" . $s); 3125c2fdda0dSAndy Whitcroft } 31268905a67cSAndy Whitcroft 31276c72ffaaSAndy Whitcroft # any (foo ... *) is a pointer cast, and foo is a type 312865863862SAndy Whitcroft while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) { 3129c45dcabdSAndy Whitcroft possible($1, "C:" . $s); 31309c0ca6f9SAndy Whitcroft } 31318905a67cSAndy Whitcroft 31328905a67cSAndy Whitcroft # Check for any sort of function declaration. 31338905a67cSAndy Whitcroft # int foo(something bar, other baz); 31348905a67cSAndy Whitcroft # void (*store_gdt)(x86_descr_ptr *); 3135171ae1a4SAndy Whitcroft if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) { 31368905a67cSAndy Whitcroft my ($name_len) = length($1); 31378905a67cSAndy Whitcroft 3138cf655043SAndy Whitcroft my $ctx = $s; 3139773647a0SAndy Whitcroft substr($ctx, 0, $name_len + 1, ''); 31408905a67cSAndy Whitcroft $ctx =~ s/\)[^\)]*$//; 3141cf655043SAndy Whitcroft 31428905a67cSAndy Whitcroft for my $arg (split(/\s*,\s*/, $ctx)) { 3143c45dcabdSAndy Whitcroft if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) { 31448905a67cSAndy Whitcroft 3145c45dcabdSAndy Whitcroft possible($1, "D:" . $s); 31468905a67cSAndy Whitcroft } 31478905a67cSAndy Whitcroft } 31488905a67cSAndy Whitcroft } 31498905a67cSAndy Whitcroft 31509c0ca6f9SAndy Whitcroft } 31519c0ca6f9SAndy Whitcroft 315200df344fSAndy Whitcroft# 315300df344fSAndy Whitcroft# Checks which may be anchored in the context. 315400df344fSAndy Whitcroft# 315500df344fSAndy Whitcroft 315600df344fSAndy Whitcroft# Check for switch () and associated case and default 315700df344fSAndy Whitcroft# statements should be at the same indent. 315800df344fSAndy Whitcroft if ($line=~/\bswitch\s*\(.*\)/) { 315900df344fSAndy Whitcroft my $err = ''; 316000df344fSAndy Whitcroft my $sep = ''; 316100df344fSAndy Whitcroft my @ctx = ctx_block_outer($linenr, $realcnt); 316200df344fSAndy Whitcroft shift(@ctx); 316300df344fSAndy Whitcroft for my $ctx (@ctx) { 316400df344fSAndy Whitcroft my ($clen, $cindent) = line_stats($ctx); 316500df344fSAndy Whitcroft if ($ctx =~ /^\+\s*(case\s+|default:)/ && 316600df344fSAndy Whitcroft $indent != $cindent) { 316700df344fSAndy Whitcroft $err .= "$sep$ctx\n"; 316800df344fSAndy Whitcroft $sep = ''; 316900df344fSAndy Whitcroft } else { 317000df344fSAndy Whitcroft $sep = "[...]\n"; 317100df344fSAndy Whitcroft } 317200df344fSAndy Whitcroft } 317300df344fSAndy Whitcroft if ($err ne '') { 3174000d1cc1SJoe Perches ERROR("SWITCH_CASE_INDENT_LEVEL", 3175000d1cc1SJoe Perches "switch and case should be at the same indent\n$hereline$err"); 3176de7d4f0eSAndy Whitcroft } 3177de7d4f0eSAndy Whitcroft } 3178de7d4f0eSAndy Whitcroft 3179de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop, 3180de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else 31810fe3dc2bSJoe Perches if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) { 3182773647a0SAndy Whitcroft my $pre_ctx = "$1$2"; 3183773647a0SAndy Whitcroft 31849c0ca6f9SAndy Whitcroft my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0); 31858eef05ddSJoe Perches 31868eef05ddSJoe Perches if ($line =~ /^\+\t{6,}/) { 31878eef05ddSJoe Perches WARN("DEEP_INDENTATION", 31888eef05ddSJoe Perches "Too many leading tabs - consider code refactoring\n" . $herecurr); 31898eef05ddSJoe Perches } 31908eef05ddSJoe Perches 3191de7d4f0eSAndy Whitcroft my $ctx_cnt = $realcnt - $#ctx - 1; 3192de7d4f0eSAndy Whitcroft my $ctx = join("\n", @ctx); 3193de7d4f0eSAndy Whitcroft 3194548596d5SAndy Whitcroft my $ctx_ln = $linenr; 3195548596d5SAndy Whitcroft my $ctx_skip = $realcnt; 3196de7d4f0eSAndy Whitcroft 3197548596d5SAndy Whitcroft while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt && 3198548596d5SAndy Whitcroft defined $lines[$ctx_ln - 1] && 3199548596d5SAndy Whitcroft $lines[$ctx_ln - 1] =~ /^-/)) { 3200548596d5SAndy Whitcroft ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n"; 3201548596d5SAndy Whitcroft $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/); 3202773647a0SAndy Whitcroft $ctx_ln++; 3203773647a0SAndy Whitcroft } 3204548596d5SAndy Whitcroft 320553210168SAndy Whitcroft #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n"; 320653210168SAndy Whitcroft #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n"; 3207773647a0SAndy Whitcroft 3208773647a0SAndy Whitcroft if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) { 3209000d1cc1SJoe Perches ERROR("OPEN_BRACE", 3210000d1cc1SJoe Perches "that open brace { should be on the previous line\n" . 321101464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 321200df344fSAndy Whitcroft } 3213773647a0SAndy Whitcroft if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ && 3214773647a0SAndy Whitcroft $ctx =~ /\)\s*\;\s*$/ && 3215773647a0SAndy Whitcroft defined $lines[$ctx_ln - 1]) 3216773647a0SAndy Whitcroft { 32179c0ca6f9SAndy Whitcroft my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]); 32189c0ca6f9SAndy Whitcroft if ($nindent > $indent) { 3219000d1cc1SJoe Perches WARN("TRAILING_SEMICOLON", 3220000d1cc1SJoe Perches "trailing semicolon indicates no statements, indent implies otherwise\n" . 322101464f30SAndy Whitcroft "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n"); 32229c0ca6f9SAndy Whitcroft } 32239c0ca6f9SAndy Whitcroft } 322400df344fSAndy Whitcroft } 322500df344fSAndy Whitcroft 32264d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks. 32270fe3dc2bSJoe Perches if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) { 32283e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 32293e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 32303e469cdcSAndy Whitcroft if (!defined $stat); 32314d001e4dSAndy Whitcroft my ($s, $c) = ($stat, $cond); 32324d001e4dSAndy Whitcroft 32334d001e4dSAndy Whitcroft substr($s, 0, length($c), ''); 32344d001e4dSAndy Whitcroft 32359f5af480SJoe Perches # remove inline comments 32369f5af480SJoe Perches $s =~ s/$;/ /g; 32379f5af480SJoe Perches $c =~ s/$;/ /g; 32384d001e4dSAndy Whitcroft 32394d001e4dSAndy Whitcroft # Find out how long the conditional actually is. 32406f779c18SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 32416f779c18SAndy Whitcroft my $cond_lines = 1 + $#newlines; 32424d001e4dSAndy Whitcroft 32439f5af480SJoe Perches # Make sure we remove the line prefixes as we have 32449f5af480SJoe Perches # none on the first line, and are going to readd them 32459f5af480SJoe Perches # where necessary. 32469f5af480SJoe Perches $s =~ s/\n./\n/gs; 32479f5af480SJoe Perches while ($s =~ /\n\s+\\\n/) { 32489f5af480SJoe Perches $cond_lines += $s =~ s/\n\s+\\\n/\n/g; 32499f5af480SJoe Perches } 32509f5af480SJoe Perches 32514d001e4dSAndy Whitcroft # We want to check the first line inside the block 32524d001e4dSAndy Whitcroft # starting at the end of the conditional, so remove: 32534d001e4dSAndy Whitcroft # 1) any blank line termination 32544d001e4dSAndy Whitcroft # 2) any opening brace { on end of the line 32554d001e4dSAndy Whitcroft # 3) any do (...) { 32564d001e4dSAndy Whitcroft my $continuation = 0; 32574d001e4dSAndy Whitcroft my $check = 0; 32584d001e4dSAndy Whitcroft $s =~ s/^.*\bdo\b//; 32594d001e4dSAndy Whitcroft $s =~ s/^\s*{//; 32604d001e4dSAndy Whitcroft if ($s =~ s/^\s*\\//) { 32614d001e4dSAndy Whitcroft $continuation = 1; 32624d001e4dSAndy Whitcroft } 32639bd49efeSAndy Whitcroft if ($s =~ s/^\s*?\n//) { 32644d001e4dSAndy Whitcroft $check = 1; 32654d001e4dSAndy Whitcroft $cond_lines++; 32664d001e4dSAndy Whitcroft } 32674d001e4dSAndy Whitcroft 32684d001e4dSAndy Whitcroft # Also ignore a loop construct at the end of a 32694d001e4dSAndy Whitcroft # preprocessor statement. 32704d001e4dSAndy Whitcroft if (($prevline =~ /^.\s*#\s*define\s/ || 32714d001e4dSAndy Whitcroft $prevline =~ /\\\s*$/) && $continuation == 0) { 32724d001e4dSAndy Whitcroft $check = 0; 32734d001e4dSAndy Whitcroft } 32744d001e4dSAndy Whitcroft 32759bd49efeSAndy Whitcroft my $cond_ptr = -1; 3276740504c6SAndy Whitcroft $continuation = 0; 32779bd49efeSAndy Whitcroft while ($cond_ptr != $cond_lines) { 32789bd49efeSAndy Whitcroft $cond_ptr = $cond_lines; 32794d001e4dSAndy Whitcroft 3280f16fa28fSAndy Whitcroft # If we see an #else/#elif then the code 3281f16fa28fSAndy Whitcroft # is not linear. 3282f16fa28fSAndy Whitcroft if ($s =~ /^\s*\#\s*(?:else|elif)/) { 3283f16fa28fSAndy Whitcroft $check = 0; 3284f16fa28fSAndy Whitcroft } 3285f16fa28fSAndy Whitcroft 32869bd49efeSAndy Whitcroft # Ignore: 32879bd49efeSAndy Whitcroft # 1) blank lines, they should be at 0, 32889bd49efeSAndy Whitcroft # 2) preprocessor lines, and 32899bd49efeSAndy Whitcroft # 3) labels. 3290740504c6SAndy Whitcroft if ($continuation || 3291740504c6SAndy Whitcroft $s =~ /^\s*?\n/ || 32929bd49efeSAndy Whitcroft $s =~ /^\s*#\s*?/ || 32939bd49efeSAndy Whitcroft $s =~ /^\s*$Ident\s*:/) { 3294740504c6SAndy Whitcroft $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0; 329530dad6ebSAndy Whitcroft if ($s =~ s/^.*?\n//) { 32969bd49efeSAndy Whitcroft $cond_lines++; 32979bd49efeSAndy Whitcroft } 32984d001e4dSAndy Whitcroft } 329930dad6ebSAndy Whitcroft } 33004d001e4dSAndy Whitcroft 33014d001e4dSAndy Whitcroft my (undef, $sindent) = line_stats("+" . $s); 33024d001e4dSAndy Whitcroft my $stat_real = raw_line($linenr, $cond_lines); 33034d001e4dSAndy Whitcroft 33044d001e4dSAndy Whitcroft # Check if either of these lines are modified, else 33054d001e4dSAndy Whitcroft # this is not this patch's fault. 33064d001e4dSAndy Whitcroft if (!defined($stat_real) || 33074d001e4dSAndy Whitcroft $stat !~ /^\+/ && $stat_real !~ /^\+/) { 33084d001e4dSAndy Whitcroft $check = 0; 33094d001e4dSAndy Whitcroft } 33104d001e4dSAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 33114d001e4dSAndy Whitcroft $stat_real = "[...]\n$stat_real"; 33124d001e4dSAndy Whitcroft } 33134d001e4dSAndy Whitcroft 33149bd49efeSAndy 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"; 33154d001e4dSAndy Whitcroft 33169f5af480SJoe Perches if ($check && $s ne '' && 33179f5af480SJoe Perches (($sindent % 8) != 0 || 33189f5af480SJoe Perches ($sindent < $indent) || 33199f5af480SJoe Perches ($sindent > $indent + 8))) { 3320000d1cc1SJoe Perches WARN("SUSPECT_CODE_INDENT", 3321000d1cc1SJoe Perches "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n"); 33224d001e4dSAndy Whitcroft } 33234d001e4dSAndy Whitcroft } 33244d001e4dSAndy Whitcroft 33256c72ffaaSAndy Whitcroft # Track the 'values' across context and added lines. 33266c72ffaaSAndy Whitcroft my $opline = $line; $opline =~ s/^./ /; 33271f65f947SAndy Whitcroft my ($curr_values, $curr_vars) = 33281f65f947SAndy Whitcroft annotate_values($opline . "\n", $prev_values); 33296c72ffaaSAndy Whitcroft $curr_values = $prev_values . $curr_values; 3330c2fdda0dSAndy Whitcroft if ($dbg_values) { 3331c2fdda0dSAndy Whitcroft my $outline = $opline; $outline =~ s/\t/ /g; 3332cf655043SAndy Whitcroft print "$linenr > .$outline\n"; 3333cf655043SAndy Whitcroft print "$linenr > $curr_values\n"; 33341f65f947SAndy Whitcroft print "$linenr > $curr_vars\n"; 3335c2fdda0dSAndy Whitcroft } 33366c72ffaaSAndy Whitcroft $prev_values = substr($curr_values, -1); 33376c72ffaaSAndy Whitcroft 333800df344fSAndy Whitcroft#ignore lines not being added 33393705ce5bSJoe Perches next if ($line =~ /^[^\+]/); 334000df344fSAndy Whitcroft 3341a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int 3342207a8e84SJoe Perches while ($line =~ m{($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) { 3343a1ce18e4SJoe Perches my $type = $1; 3344a1ce18e4SJoe Perches my $var = $2; 3345207a8e84SJoe Perches $var = "" if (!defined $var); 3346207a8e84SJoe Perches if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) { 3347a1ce18e4SJoe Perches my $sign = $1; 3348a1ce18e4SJoe Perches my $pointer = $2; 3349a1ce18e4SJoe Perches 3350a1ce18e4SJoe Perches $pointer = "" if (!defined $pointer); 3351a1ce18e4SJoe Perches 3352a1ce18e4SJoe Perches if (WARN("UNSPECIFIED_INT", 3353a1ce18e4SJoe Perches "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) && 3354a1ce18e4SJoe Perches $fix) { 3355a1ce18e4SJoe Perches my $decl = trim($sign) . " int "; 3356207a8e84SJoe Perches my $comp_pointer = $pointer; 3357207a8e84SJoe Perches $comp_pointer =~ s/\s//g; 3358207a8e84SJoe Perches $decl .= $comp_pointer; 3359207a8e84SJoe Perches $decl = rtrim($decl) if ($var eq ""); 3360207a8e84SJoe Perches $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@; 3361a1ce18e4SJoe Perches } 3362a1ce18e4SJoe Perches } 3363a1ce18e4SJoe Perches } 3364a1ce18e4SJoe Perches 3365653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher. 33667429c690SAndy Whitcroft if ($dbg_type) { 33677429c690SAndy Whitcroft if ($line =~ /^.\s*$Declare\s*$/) { 3368000d1cc1SJoe Perches ERROR("TEST_TYPE", 3369000d1cc1SJoe Perches "TEST: is type\n" . $herecurr); 33707429c690SAndy Whitcroft } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) { 3371000d1cc1SJoe Perches ERROR("TEST_NOT_TYPE", 3372000d1cc1SJoe Perches "TEST: is not type ($1 is)\n". $herecurr); 33737429c690SAndy Whitcroft } 3374653d4876SAndy Whitcroft next; 3375653d4876SAndy Whitcroft } 3376a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher. 3377a1ef277eSAndy Whitcroft if ($dbg_attr) { 33789360b0e5SAndy Whitcroft if ($line =~ /^.\s*$Modifier\s*$/) { 3379000d1cc1SJoe Perches ERROR("TEST_ATTR", 3380000d1cc1SJoe Perches "TEST: is attr\n" . $herecurr); 33819360b0e5SAndy Whitcroft } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) { 3382000d1cc1SJoe Perches ERROR("TEST_NOT_ATTR", 3383000d1cc1SJoe Perches "TEST: is not attr ($1 is)\n". $herecurr); 3384a1ef277eSAndy Whitcroft } 3385a1ef277eSAndy Whitcroft next; 3386a1ef277eSAndy Whitcroft } 3387653d4876SAndy Whitcroft 3388f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line 338999423c20SAndy Whitcroft if ($line =~ /^.\s*{/ && 339099423c20SAndy Whitcroft $prevline =~ /(?:^|[^=])=\s*$/) { 3391d752fcc8SJoe Perches if (ERROR("OPEN_BRACE", 3392d752fcc8SJoe Perches "that open brace { should be on the previous line\n" . $hereprev) && 3393f2d7e4d4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 3394f2d7e4d4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 3395f2d7e4d4SJoe Perches fix_delete_line($fixlinenr, $rawline); 3396d752fcc8SJoe Perches my $fixedline = $prevrawline; 3397d752fcc8SJoe Perches $fixedline =~ s/\s*=\s*$/ = {/; 3398f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3399d752fcc8SJoe Perches $fixedline = $line; 3400d752fcc8SJoe Perches $fixedline =~ s/^(.\s*){\s*/$1/; 3401f2d7e4d4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 3402d752fcc8SJoe Perches } 3403f0a594c1SAndy Whitcroft } 3404f0a594c1SAndy Whitcroft 340500df344fSAndy Whitcroft# 340600df344fSAndy Whitcroft# Checks which are anchored on the added line. 340700df344fSAndy Whitcroft# 340800df344fSAndy Whitcroft 3409653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line) 3410c45dcabdSAndy Whitcroft if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) { 3411653d4876SAndy Whitcroft my $path = $1; 3412653d4876SAndy Whitcroft if ($path =~ m{//}) { 3413000d1cc1SJoe Perches ERROR("MALFORMED_INCLUDE", 3414495e9d84SJoe Perches "malformed #include filename\n" . $herecurr); 3415495e9d84SJoe Perches } 3416495e9d84SJoe Perches if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) { 3417495e9d84SJoe Perches ERROR("UAPI_INCLUDE", 3418495e9d84SJoe Perches "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr); 3419653d4876SAndy Whitcroft } 3420653d4876SAndy Whitcroft } 3421653d4876SAndy Whitcroft 342200df344fSAndy Whitcroft# no C99 // comments 342300df344fSAndy Whitcroft if ($line =~ m{//}) { 34243705ce5bSJoe Perches if (ERROR("C99_COMMENTS", 34253705ce5bSJoe Perches "do not use C99 // comments\n" . $herecurr) && 34263705ce5bSJoe Perches $fix) { 3427194f66fcSJoe Perches my $line = $fixed[$fixlinenr]; 34283705ce5bSJoe Perches if ($line =~ /\/\/(.*)$/) { 34293705ce5bSJoe Perches my $comment = trim($1); 3430194f66fcSJoe Perches $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@; 34313705ce5bSJoe Perches } 34323705ce5bSJoe Perches } 343300df344fSAndy Whitcroft } 343400df344fSAndy Whitcroft # Remove C99 comments. 34350a920b5bSAndy Whitcroft $line =~ s@//.*@@; 34366c72ffaaSAndy Whitcroft $opline =~ s@//.*@@; 34370a920b5bSAndy Whitcroft 34382b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider 34392b474a1aSAndy Whitcroft# the whole statement. 34402b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n"; 34412b474a1aSAndy Whitcroft if (defined $realline_next && 34422b474a1aSAndy Whitcroft exists $lines[$realline_next - 1] && 34432b474a1aSAndy Whitcroft !defined $suppress_export{$realline_next} && 34442b474a1aSAndy Whitcroft ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ || 34452b474a1aSAndy Whitcroft $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 34463cbf62dfSAndy Whitcroft # Handle definitions which produce identifiers with 34473cbf62dfSAndy Whitcroft # a prefix: 34483cbf62dfSAndy Whitcroft # XXX(foo); 34493cbf62dfSAndy Whitcroft # EXPORT_SYMBOL(something_foo); 3450653d4876SAndy Whitcroft my $name = $1; 345187a53877SAndy Whitcroft if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ && 34523cbf62dfSAndy Whitcroft $name =~ /^${Ident}_$2/) { 34533cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n"; 34543cbf62dfSAndy Whitcroft $suppress_export{$realline_next} = 1; 34553cbf62dfSAndy Whitcroft 34563cbf62dfSAndy Whitcroft } elsif ($stat !~ /(?: 34572b474a1aSAndy Whitcroft \n.}\s*$| 345848012058SAndy Whitcroft ^.DEFINE_$Ident\(\Q$name\E\)| 345948012058SAndy Whitcroft ^.DECLARE_$Ident\(\Q$name\E\)| 346048012058SAndy Whitcroft ^.LIST_HEAD\(\Q$name\E\)| 34612b474a1aSAndy Whitcroft ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(| 34622b474a1aSAndy Whitcroft \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\() 346348012058SAndy Whitcroft )/x) { 34642b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n"; 34652b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 2; 34662b474a1aSAndy Whitcroft } else { 34672b474a1aSAndy Whitcroft $suppress_export{$realline_next} = 1; 34680a920b5bSAndy Whitcroft } 34690a920b5bSAndy Whitcroft } 34702b474a1aSAndy Whitcroft if (!defined $suppress_export{$linenr} && 34712b474a1aSAndy Whitcroft $prevline =~ /^.\s*$/ && 34722b474a1aSAndy Whitcroft ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ || 34732b474a1aSAndy Whitcroft $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) { 34742b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n"; 34752b474a1aSAndy Whitcroft $suppress_export{$linenr} = 2; 34762b474a1aSAndy Whitcroft } 34772b474a1aSAndy Whitcroft if (defined $suppress_export{$linenr} && 34782b474a1aSAndy Whitcroft $suppress_export{$linenr} == 2) { 3479000d1cc1SJoe Perches WARN("EXPORT_SYMBOL", 3480000d1cc1SJoe Perches "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr); 34812b474a1aSAndy Whitcroft } 34820a920b5bSAndy Whitcroft 34835150bda4SJoe Eloff# check for global initialisers. 34846d32f7a3SJoe Perches if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) { 3485d5e616fcSJoe Perches if (ERROR("GLOBAL_INITIALISERS", 34866d32f7a3SJoe Perches "do not initialise globals to $1\n" . $herecurr) && 3487d5e616fcSJoe Perches $fix) { 34886d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/; 3489d5e616fcSJoe Perches } 3490f0a594c1SAndy Whitcroft } 34910a920b5bSAndy Whitcroft# check for static initialisers. 34926d32f7a3SJoe Perches if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) { 3493d5e616fcSJoe Perches if (ERROR("INITIALISED_STATIC", 34946d32f7a3SJoe Perches "do not initialise statics to $1\n" . 3495d5e616fcSJoe Perches $herecurr) && 3496d5e616fcSJoe Perches $fix) { 34976d32f7a3SJoe Perches $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/; 3498d5e616fcSJoe Perches } 34990a920b5bSAndy Whitcroft } 35000a920b5bSAndy Whitcroft 35011813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned 35021813087dSJoe Perches while ($sline =~ m{(\b$TypeMisordered\b)}g) { 35031813087dSJoe Perches my $tmp = trim($1); 35041813087dSJoe Perches WARN("MISORDERED_TYPE", 35051813087dSJoe Perches "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr); 35061813087dSJoe Perches } 35071813087dSJoe Perches 3508cb710ecaSJoe Perches# check for static const char * arrays. 3509cb710ecaSJoe Perches if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) { 3510000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3511000d1cc1SJoe Perches "static const char * array should probably be static const char * const\n" . 3512cb710ecaSJoe Perches $herecurr); 3513cb710ecaSJoe Perches } 3514cb710ecaSJoe Perches 3515cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations. 3516cb710ecaSJoe Perches if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) { 3517000d1cc1SJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 3518000d1cc1SJoe Perches "static char array declaration should probably be static const char\n" . 3519cb710ecaSJoe Perches $herecurr); 3520cb710ecaSJoe Perches } 3521cb710ecaSJoe Perches 3522ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type 3523ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) { 3524ab7e23f3SJoe Perches my $found = $1; 3525ab7e23f3SJoe Perches if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) { 3526ab7e23f3SJoe Perches WARN("CONST_CONST", 3527ab7e23f3SJoe Perches "'const $found const *' should probably be 'const $found * const'\n" . $herecurr); 3528ab7e23f3SJoe Perches } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) { 3529ab7e23f3SJoe Perches WARN("CONST_CONST", 3530ab7e23f3SJoe Perches "'const $found const' should probably be 'const $found'\n" . $herecurr); 3531ab7e23f3SJoe Perches } 3532ab7e23f3SJoe Perches } 3533ab7e23f3SJoe Perches 35349b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations. 35359b0fa60dSJoe Perches if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) { 35369b0fa60dSJoe Perches WARN("STATIC_CONST_CHAR_ARRAY", 35379b0fa60dSJoe Perches "char * array declaration might be better as static const\n" . 35389b0fa60dSJoe Perches $herecurr); 35399b0fa60dSJoe Perches } 35409b0fa60dSJoe Perches 3541b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo) 3542b598b670SJoe Perches if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) { 3543b598b670SJoe Perches my $array = $1; 3544b598b670SJoe 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*\))@) { 3545b598b670SJoe Perches my $array_div = $1; 3546b598b670SJoe Perches if (WARN("ARRAY_SIZE", 3547b598b670SJoe Perches "Prefer ARRAY_SIZE($array)\n" . $herecurr) && 3548b598b670SJoe Perches $fix) { 3549b598b670SJoe Perches $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/; 3550b598b670SJoe Perches } 3551b598b670SJoe Perches } 3552b598b670SJoe Perches } 3553b598b670SJoe Perches 3554b36190c5SJoe Perches# check for function declarations without arguments like "int foo()" 3555b36190c5SJoe Perches if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) { 3556b36190c5SJoe Perches if (ERROR("FUNCTION_WITHOUT_ARGS", 3557b36190c5SJoe Perches "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) && 3558b36190c5SJoe Perches $fix) { 3559194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/; 3560b36190c5SJoe Perches } 3561b36190c5SJoe Perches } 3562b36190c5SJoe Perches 356392e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE 356492e112fdSJoe Perches if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) { 356592e112fdSJoe Perches if (WARN("DEFINE_PCI_DEVICE_TABLE", 356692e112fdSJoe Perches "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) && 356792e112fdSJoe Perches $fix) { 3568194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /; 356992e112fdSJoe Perches } 357093ed0e2dSJoe Perches } 357193ed0e2dSJoe Perches 3572653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations 3573653d4876SAndy Whitcroft# make sense. 3574653d4876SAndy Whitcroft if ($line =~ /\btypedef\s/ && 35758054576dSAndy Whitcroft $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ && 3576c45dcabdSAndy Whitcroft $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ && 35778ed22cadSAndy Whitcroft $line !~ /\b$typeTypedefs\b/ && 3578653d4876SAndy Whitcroft $line !~ /\b__bitwise(?:__|)\b/) { 3579000d1cc1SJoe Perches WARN("NEW_TYPEDEFS", 3580000d1cc1SJoe Perches "do not add new typedefs\n" . $herecurr); 35810a920b5bSAndy Whitcroft } 35820a920b5bSAndy Whitcroft 35830a920b5bSAndy Whitcroft# * goes on variable not on type 358465863862SAndy Whitcroft # (char*[ const]) 3585bfcb2cc7SAndy Whitcroft while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) { 3586bfcb2cc7SAndy Whitcroft #print "AA<$1>\n"; 35873705ce5bSJoe Perches my ($ident, $from, $to) = ($1, $2, $2); 3588d8aaf121SAndy Whitcroft 358965863862SAndy Whitcroft # Should start with a space. 359065863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 359165863862SAndy Whitcroft # Should not end with a space. 359265863862SAndy Whitcroft $to =~ s/\s+$//; 359365863862SAndy Whitcroft # '*'s should not have spaces between. 3594f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 359565863862SAndy Whitcroft } 3596d8aaf121SAndy Whitcroft 35973705ce5bSJoe Perches## print "1: from<$from> to<$to> ident<$ident>\n"; 359865863862SAndy Whitcroft if ($from ne $to) { 35993705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 36003705ce5bSJoe Perches "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) && 36013705ce5bSJoe Perches $fix) { 36023705ce5bSJoe Perches my $sub_from = $ident; 36033705ce5bSJoe Perches my $sub_to = $ident; 36043705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3605194f66fcSJoe Perches $fixed[$fixlinenr] =~ 36063705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 36073705ce5bSJoe Perches } 360865863862SAndy Whitcroft } 3609bfcb2cc7SAndy Whitcroft } 3610bfcb2cc7SAndy Whitcroft while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) { 3611bfcb2cc7SAndy Whitcroft #print "BB<$1>\n"; 36123705ce5bSJoe Perches my ($match, $from, $to, $ident) = ($1, $2, $2, $3); 3613d8aaf121SAndy Whitcroft 361465863862SAndy Whitcroft # Should start with a space. 361565863862SAndy Whitcroft $to =~ s/^(\S)/ $1/; 361665863862SAndy Whitcroft # Should not end with a space. 361765863862SAndy Whitcroft $to =~ s/\s+$//; 361865863862SAndy Whitcroft # '*'s should not have spaces between. 3619f9a0b3d1SAndy Whitcroft while ($to =~ s/\*\s+\*/\*\*/) { 362065863862SAndy Whitcroft } 362165863862SAndy Whitcroft # Modifiers should have spaces. 362265863862SAndy Whitcroft $to =~ s/(\b$Modifier$)/$1 /; 362365863862SAndy Whitcroft 36243705ce5bSJoe Perches## print "2: from<$from> to<$to> ident<$ident>\n"; 3625667026e7SAndy Whitcroft if ($from ne $to && $ident !~ /^$Modifier$/) { 36263705ce5bSJoe Perches if (ERROR("POINTER_LOCATION", 36273705ce5bSJoe Perches "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) && 36283705ce5bSJoe Perches $fix) { 36293705ce5bSJoe Perches 36303705ce5bSJoe Perches my $sub_from = $match; 36313705ce5bSJoe Perches my $sub_to = $match; 36323705ce5bSJoe Perches $sub_to =~ s/\Q$from\E/$to/; 3633194f66fcSJoe Perches $fixed[$fixlinenr] =~ 36343705ce5bSJoe Perches s@\Q$sub_from\E@$sub_to@; 36353705ce5bSJoe Perches } 363665863862SAndy Whitcroft } 36370a920b5bSAndy Whitcroft } 36380a920b5bSAndy Whitcroft 36399d3e3c70SJoe Perches# avoid BUG() or BUG_ON() 36409d3e3c70SJoe Perches if ($line =~ /\b(?:BUG|BUG_ON)\b/) { 36419d3e3c70SJoe Perches my $msg_type = \&WARN; 36429d3e3c70SJoe Perches $msg_type = \&CHK if ($file); 36439d3e3c70SJoe Perches &{$msg_type}("AVOID_BUG", 36449d3e3c70SJoe Perches "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr); 36459d3e3c70SJoe Perches } 36460a920b5bSAndy Whitcroft 36479d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE 36488905a67cSAndy Whitcroft if ($line =~ /\bLINUX_VERSION_CODE\b/) { 3649000d1cc1SJoe Perches WARN("LINUX_VERSION_CODE", 3650000d1cc1SJoe Perches "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr); 36518905a67cSAndy Whitcroft } 36528905a67cSAndy Whitcroft 365317441227SJoe Perches# check for uses of printk_ratelimit 365417441227SJoe Perches if ($line =~ /\bprintk_ratelimit\s*\(/) { 3655000d1cc1SJoe Perches WARN("PRINTK_RATELIMITED", 3656000d1cc1SJoe Perches "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr); 365717441227SJoe Perches } 365817441227SJoe Perches 365900df344fSAndy Whitcroft# printk should use KERN_* levels. Note that follow on printk's on the 366000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context 366100df344fSAndy Whitcroft# to try and find and validate the current printk. In summary the current 366225985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end. 366300df344fSAndy Whitcroft# we assume the first bad printk is the one to report. 3664f0a594c1SAndy Whitcroft if ($line =~ /\bprintk\((?!KERN_)\s*"/) { 366500df344fSAndy Whitcroft my $ok = 0; 366600df344fSAndy Whitcroft for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) { 366700df344fSAndy Whitcroft #print "CHECK<$lines[$ln - 1]\n"; 366825985edcSLucas De Marchi # we have a preceding printk if it ends 366900df344fSAndy Whitcroft # with "\n" ignore it, else it is to blame 367000df344fSAndy Whitcroft if ($lines[$ln - 1] =~ m{\bprintk\(}) { 367100df344fSAndy Whitcroft if ($rawlines[$ln - 1] !~ m{\\n"}) { 367200df344fSAndy Whitcroft $ok = 1; 367300df344fSAndy Whitcroft } 367400df344fSAndy Whitcroft last; 367500df344fSAndy Whitcroft } 367600df344fSAndy Whitcroft } 367700df344fSAndy Whitcroft if ($ok == 0) { 3678000d1cc1SJoe Perches WARN("PRINTK_WITHOUT_KERN_LEVEL", 3679000d1cc1SJoe Perches "printk() should include KERN_ facility level\n" . $herecurr); 36800a920b5bSAndy Whitcroft } 368100df344fSAndy Whitcroft } 36820a920b5bSAndy Whitcroft 3683243f3803SJoe Perches if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) { 3684243f3803SJoe Perches my $orig = $1; 3685243f3803SJoe Perches my $level = lc($orig); 3686243f3803SJoe Perches $level = "warn" if ($level eq "warning"); 36878f26b837SJoe Perches my $level2 = $level; 36888f26b837SJoe Perches $level2 = "dbg" if ($level eq "debug"); 3689243f3803SJoe Perches WARN("PREFER_PR_LEVEL", 3690daa8b059SYogesh Chaudhari "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr); 3691243f3803SJoe Perches } 3692243f3803SJoe Perches 3693243f3803SJoe Perches if ($line =~ /\bpr_warning\s*\(/) { 3694d5e616fcSJoe Perches if (WARN("PREFER_PR_LEVEL", 3695d5e616fcSJoe Perches "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) && 3696d5e616fcSJoe Perches $fix) { 3697194f66fcSJoe Perches $fixed[$fixlinenr] =~ 3698d5e616fcSJoe Perches s/\bpr_warning\b/pr_warn/; 3699d5e616fcSJoe Perches } 3700243f3803SJoe Perches } 3701243f3803SJoe Perches 3702dc139313SJoe Perches if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) { 3703dc139313SJoe Perches my $orig = $1; 3704dc139313SJoe Perches my $level = lc($orig); 3705dc139313SJoe Perches $level = "warn" if ($level eq "warning"); 3706dc139313SJoe Perches $level = "dbg" if ($level eq "debug"); 3707dc139313SJoe Perches WARN("PREFER_DEV_LEVEL", 3708dc139313SJoe Perches "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr); 3709dc139313SJoe Perches } 3710dc139313SJoe Perches 371191c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else. This will have a small 371291c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at 371391c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning. 371491c9afafSAndy Lutomirski if ($line =~ /\bENOSYS\b/) { 371591c9afafSAndy Lutomirski WARN("ENOSYS", 371691c9afafSAndy Lutomirski "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr); 371791c9afafSAndy Lutomirski } 371891c9afafSAndy Lutomirski 3719653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while, 3720653d4876SAndy Whitcroft# or if closed on same line 37218d182478SJoe Perches if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and 37224e5d56bdSEddie Kovsky !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) { 37238d182478SJoe Perches if (ERROR("OPEN_BRACE", 37248d182478SJoe Perches "open brace '{' following function declarations go on the next line\n" . $herecurr) && 37258d182478SJoe Perches $fix) { 37268d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 37278d182478SJoe Perches my $fixed_line = $rawline; 37288d182478SJoe Perches $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/; 37298d182478SJoe Perches my $line1 = $1; 37308d182478SJoe Perches my $line2 = $2; 37318d182478SJoe Perches fix_insert_line($fixlinenr, ltrim($line1)); 37328d182478SJoe Perches fix_insert_line($fixlinenr, "\+{"); 37338d182478SJoe Perches if ($line2 !~ /^\s*$/) { 37348d182478SJoe Perches fix_insert_line($fixlinenr, "\+\t" . trim($line2)); 37358d182478SJoe Perches } 37368d182478SJoe Perches } 37370a920b5bSAndy Whitcroft } 3738653d4876SAndy Whitcroft 37398905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line. 37408905a67cSAndy Whitcroft if ($line =~ /^.\s*{/ && 37418905a67cSAndy Whitcroft $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) { 37428d182478SJoe Perches if (ERROR("OPEN_BRACE", 37438d182478SJoe Perches "open brace '{' following $1 go on the same line\n" . $hereprev) && 37448d182478SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 37458d182478SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 37468d182478SJoe Perches fix_delete_line($fixlinenr, $rawline); 37478d182478SJoe Perches my $fixedline = rtrim($prevrawline) . " {"; 37488d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 37498d182478SJoe Perches $fixedline = $rawline; 37508d182478SJoe Perches $fixedline =~ s/^(.\s*){\s*/$1\t/; 37518d182478SJoe Perches if ($fixedline !~ /^\+\s*$/) { 37528d182478SJoe Perches fix_insert_line($fixlinenr, $fixedline); 37538d182478SJoe Perches } 37548d182478SJoe Perches } 37558905a67cSAndy Whitcroft } 37568905a67cSAndy Whitcroft 37570c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition 37583705ce5bSJoe Perches if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) { 37593705ce5bSJoe Perches if (WARN("SPACING", 37603705ce5bSJoe Perches "missing space after $1 definition\n" . $herecurr) && 37613705ce5bSJoe Perches $fix) { 3762194f66fcSJoe Perches $fixed[$fixlinenr] =~ 37633705ce5bSJoe Perches s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/; 37643705ce5bSJoe Perches } 37650c73b4ebSAndy Whitcroft } 37660c73b4ebSAndy Whitcroft 376731070b5dSJoe Perches# Function pointer declarations 376831070b5dSJoe Perches# check spacing between type, funcptr, and args 376931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)" 377091f72e9cSJoe Perches if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) { 377131070b5dSJoe Perches my $declare = $1; 377231070b5dSJoe Perches my $pre_pointer_space = $2; 377331070b5dSJoe Perches my $post_pointer_space = $3; 377431070b5dSJoe Perches my $funcname = $4; 377531070b5dSJoe Perches my $post_funcname_space = $5; 377631070b5dSJoe Perches my $pre_args_space = $6; 377731070b5dSJoe Perches 377891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type 377991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types 378091f72e9cSJoe Perches# don't need a space so don't warn for those. 378191f72e9cSJoe Perches my $post_declare_space = ""; 378291f72e9cSJoe Perches if ($declare =~ /(\s+)$/) { 378391f72e9cSJoe Perches $post_declare_space = $1; 378491f72e9cSJoe Perches $declare = rtrim($declare); 378591f72e9cSJoe Perches } 378691f72e9cSJoe Perches if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) { 378731070b5dSJoe Perches WARN("SPACING", 378831070b5dSJoe Perches "missing space after return type\n" . $herecurr); 378991f72e9cSJoe Perches $post_declare_space = " "; 379031070b5dSJoe Perches } 379131070b5dSJoe Perches 379231070b5dSJoe Perches# unnecessary space "type (*funcptr)(args...)" 379391f72e9cSJoe Perches# This test is not currently implemented because these declarations are 379491f72e9cSJoe Perches# equivalent to 379591f72e9cSJoe Perches# int foo(int bar, ...) 379691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning. 379791f72e9cSJoe Perches# 379891f72e9cSJoe Perches# elsif ($declare =~ /\s{2,}$/) { 379991f72e9cSJoe Perches# WARN("SPACING", 380091f72e9cSJoe Perches# "Multiple spaces after return type\n" . $herecurr); 380191f72e9cSJoe Perches# } 380231070b5dSJoe Perches 380331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)" 380431070b5dSJoe Perches if (defined $pre_pointer_space && 380531070b5dSJoe Perches $pre_pointer_space =~ /^\s/) { 380631070b5dSJoe Perches WARN("SPACING", 380731070b5dSJoe Perches "Unnecessary space after function pointer open parenthesis\n" . $herecurr); 380831070b5dSJoe Perches } 380931070b5dSJoe Perches 381031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)" 381131070b5dSJoe Perches if (defined $post_pointer_space && 381231070b5dSJoe Perches $post_pointer_space =~ /^\s/) { 381331070b5dSJoe Perches WARN("SPACING", 381431070b5dSJoe Perches "Unnecessary space before function pointer name\n" . $herecurr); 381531070b5dSJoe Perches } 381631070b5dSJoe Perches 381731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)" 381831070b5dSJoe Perches if (defined $post_funcname_space && 381931070b5dSJoe Perches $post_funcname_space =~ /^\s/) { 382031070b5dSJoe Perches WARN("SPACING", 382131070b5dSJoe Perches "Unnecessary space after function pointer name\n" . $herecurr); 382231070b5dSJoe Perches } 382331070b5dSJoe Perches 382431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)" 382531070b5dSJoe Perches if (defined $pre_args_space && 382631070b5dSJoe Perches $pre_args_space =~ /^\s/) { 382731070b5dSJoe Perches WARN("SPACING", 382831070b5dSJoe Perches "Unnecessary space before function pointer arguments\n" . $herecurr); 382931070b5dSJoe Perches } 383031070b5dSJoe Perches 383131070b5dSJoe Perches if (show_type("SPACING") && $fix) { 3832194f66fcSJoe Perches $fixed[$fixlinenr] =~ 383391f72e9cSJoe Perches s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex; 383431070b5dSJoe Perches } 383531070b5dSJoe Perches } 383631070b5dSJoe Perches 38378d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed: 38388d31cfceSAndy Whitcroft# 1. with a type on the left -- int [] a; 3839fe2a7dbcSAndy Whitcroft# 2. at the beginning of a line for slice initialisers -- [0...10] = 5, 3840fe2a7dbcSAndy Whitcroft# 3. inside a curly brace -- = { [0...10] = 5 } 38418d31cfceSAndy Whitcroft while ($line =~ /(.*?\s)\[/g) { 38428d31cfceSAndy Whitcroft my ($where, $prefix) = ($-[1], $1); 38438d31cfceSAndy Whitcroft if ($prefix !~ /$Type\s+$/ && 3844fe2a7dbcSAndy Whitcroft ($where != 0 || $prefix !~ /^.\s+$/) && 3845daebc534SAndy Whitcroft $prefix !~ /[{,]\s+$/) { 38463705ce5bSJoe Perches if (ERROR("BRACKET_SPACE", 38473705ce5bSJoe Perches "space prohibited before open square bracket '['\n" . $herecurr) && 38483705ce5bSJoe Perches $fix) { 3849194f66fcSJoe Perches $fixed[$fixlinenr] =~ 38503705ce5bSJoe Perches s/^(\+.*?)\s+\[/$1\[/; 38513705ce5bSJoe Perches } 38528d31cfceSAndy Whitcroft } 38538d31cfceSAndy Whitcroft } 38548d31cfceSAndy Whitcroft 3855f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses. 38566c72ffaaSAndy Whitcroft while ($line =~ /($Ident)\s+\(/g) { 3857c2fdda0dSAndy Whitcroft my $name = $1; 3858773647a0SAndy Whitcroft my $ctx_before = substr($line, 0, $-[1]); 3859773647a0SAndy Whitcroft my $ctx = "$ctx_before$name"; 3860c2fdda0dSAndy Whitcroft 3861c2fdda0dSAndy Whitcroft # Ignore those directives where spaces _are_ permitted. 3862773647a0SAndy Whitcroft if ($name =~ /^(?: 3863773647a0SAndy Whitcroft if|for|while|switch|return|case| 3864773647a0SAndy Whitcroft volatile|__volatile__| 3865773647a0SAndy Whitcroft __attribute__|format|__extension__| 3866773647a0SAndy Whitcroft asm|__asm__)$/x) 3867773647a0SAndy Whitcroft { 3868c2fdda0dSAndy Whitcroft # cpp #define statements have non-optional spaces, ie 3869c2fdda0dSAndy Whitcroft # if there is a space between the name and the open 3870c2fdda0dSAndy Whitcroft # parenthesis it is simply not a parameter group. 3871c45dcabdSAndy Whitcroft } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) { 3872773647a0SAndy Whitcroft 3873773647a0SAndy Whitcroft # cpp #elif statement condition may start with a ( 3874c45dcabdSAndy Whitcroft } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) { 3875c2fdda0dSAndy Whitcroft 3876c2fdda0dSAndy Whitcroft # If this whole things ends with a type its most 3877c2fdda0dSAndy Whitcroft # likely a typedef for a function. 3878773647a0SAndy Whitcroft } elsif ($ctx =~ /$Type$/) { 3879c2fdda0dSAndy Whitcroft 3880c2fdda0dSAndy Whitcroft } else { 38813705ce5bSJoe Perches if (WARN("SPACING", 38823705ce5bSJoe Perches "space prohibited between function name and open parenthesis '('\n" . $herecurr) && 38833705ce5bSJoe Perches $fix) { 3884194f66fcSJoe Perches $fixed[$fixlinenr] =~ 38853705ce5bSJoe Perches s/\b$name\s+\(/$name\(/; 38863705ce5bSJoe Perches } 3887f0a594c1SAndy Whitcroft } 38886c72ffaaSAndy Whitcroft } 38899a4cad4eSEric Nelson 3890653d4876SAndy Whitcroft# Check operator spacing. 38910a920b5bSAndy Whitcroft if (!($line=~/\#\s*include/)) { 38923705ce5bSJoe Perches my $fixed_line = ""; 38933705ce5bSJoe Perches my $line_fixed = 0; 38943705ce5bSJoe Perches 38959c0ca6f9SAndy Whitcroft my $ops = qr{ 38969c0ca6f9SAndy Whitcroft <<=|>>=|<=|>=|==|!=| 38979c0ca6f9SAndy Whitcroft \+=|-=|\*=|\/=|%=|\^=|\|=|&=| 38989c0ca6f9SAndy Whitcroft =>|->|<<|>>|<|>|=|!|~| 38991f65f947SAndy Whitcroft &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%| 390084731623SJoe Perches \?:|\?|: 39019c0ca6f9SAndy Whitcroft }x; 3902cf655043SAndy Whitcroft my @elements = split(/($ops|;)/, $opline); 39033705ce5bSJoe Perches 39043705ce5bSJoe Perches## print("element count: <" . $#elements . ">\n"); 39053705ce5bSJoe Perches## foreach my $el (@elements) { 39063705ce5bSJoe Perches## print("el: <$el>\n"); 39073705ce5bSJoe Perches## } 39083705ce5bSJoe Perches 39093705ce5bSJoe Perches my @fix_elements = (); 391000df344fSAndy Whitcroft my $off = 0; 39116c72ffaaSAndy Whitcroft 39123705ce5bSJoe Perches foreach my $el (@elements) { 39133705ce5bSJoe Perches push(@fix_elements, substr($rawline, $off, length($el))); 39143705ce5bSJoe Perches $off += length($el); 39153705ce5bSJoe Perches } 39163705ce5bSJoe Perches 39173705ce5bSJoe Perches $off = 0; 39183705ce5bSJoe Perches 39196c72ffaaSAndy Whitcroft my $blank = copy_spacing($opline); 3920b34c648bSJoe Perches my $last_after = -1; 39216c72ffaaSAndy Whitcroft 39220a920b5bSAndy Whitcroft for (my $n = 0; $n < $#elements; $n += 2) { 39233705ce5bSJoe Perches 39243705ce5bSJoe Perches my $good = $fix_elements[$n] . $fix_elements[$n + 1]; 39253705ce5bSJoe Perches 39263705ce5bSJoe Perches## print("n: <$n> good: <$good>\n"); 39273705ce5bSJoe Perches 39284a0df2efSAndy Whitcroft $off += length($elements[$n]); 39294a0df2efSAndy Whitcroft 393025985edcSLucas De Marchi # Pick up the preceding and succeeding characters. 3931773647a0SAndy Whitcroft my $ca = substr($opline, 0, $off); 3932773647a0SAndy Whitcroft my $cc = ''; 3933773647a0SAndy Whitcroft if (length($opline) >= ($off + length($elements[$n + 1]))) { 3934773647a0SAndy Whitcroft $cc = substr($opline, $off + length($elements[$n + 1])); 3935773647a0SAndy Whitcroft } 3936773647a0SAndy Whitcroft my $cb = "$ca$;$cc"; 3937773647a0SAndy Whitcroft 39384a0df2efSAndy Whitcroft my $a = ''; 39394a0df2efSAndy Whitcroft $a = 'V' if ($elements[$n] ne ''); 39404a0df2efSAndy Whitcroft $a = 'W' if ($elements[$n] =~ /\s$/); 3941cf655043SAndy Whitcroft $a = 'C' if ($elements[$n] =~ /$;$/); 39424a0df2efSAndy Whitcroft $a = 'B' if ($elements[$n] =~ /(\[|\()$/); 39434a0df2efSAndy Whitcroft $a = 'O' if ($elements[$n] eq ''); 3944773647a0SAndy Whitcroft $a = 'E' if ($ca =~ /^\s*$/); 39454a0df2efSAndy Whitcroft 39460a920b5bSAndy Whitcroft my $op = $elements[$n + 1]; 39474a0df2efSAndy Whitcroft 39484a0df2efSAndy Whitcroft my $c = ''; 39490a920b5bSAndy Whitcroft if (defined $elements[$n + 2]) { 39504a0df2efSAndy Whitcroft $c = 'V' if ($elements[$n + 2] ne ''); 39514a0df2efSAndy Whitcroft $c = 'W' if ($elements[$n + 2] =~ /^\s/); 3952cf655043SAndy Whitcroft $c = 'C' if ($elements[$n + 2] =~ /^$;/); 39534a0df2efSAndy Whitcroft $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/); 39544a0df2efSAndy Whitcroft $c = 'O' if ($elements[$n + 2] eq ''); 39558b1b3378SAndy Whitcroft $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/); 39564a0df2efSAndy Whitcroft } else { 39574a0df2efSAndy Whitcroft $c = 'E'; 39580a920b5bSAndy Whitcroft } 39590a920b5bSAndy Whitcroft 39604a0df2efSAndy Whitcroft my $ctx = "${a}x${c}"; 39614a0df2efSAndy Whitcroft 39624a0df2efSAndy Whitcroft my $at = "(ctx:$ctx)"; 39634a0df2efSAndy Whitcroft 39646c72ffaaSAndy Whitcroft my $ptr = substr($blank, 0, $off) . "^"; 3965de7d4f0eSAndy Whitcroft my $hereptr = "$hereline$ptr\n"; 39660a920b5bSAndy Whitcroft 396774048ed8SAndy Whitcroft # Pull out the value of this operator. 39686c72ffaaSAndy Whitcroft my $op_type = substr($curr_values, $off + 1, 1); 39690a920b5bSAndy Whitcroft 39701f65f947SAndy Whitcroft # Get the full operator variant. 39711f65f947SAndy Whitcroft my $opv = $op . substr($curr_vars, $off, 1); 39721f65f947SAndy Whitcroft 397313214adfSAndy Whitcroft # Ignore operators passed as parameters. 397413214adfSAndy Whitcroft if ($op_type ne 'V' && 3975d7fe8065SSam Bobroff $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) { 397613214adfSAndy Whitcroft 3977cf655043SAndy Whitcroft# # Ignore comments 3978cf655043SAndy Whitcroft# } elsif ($op =~ /^$;+$/) { 397913214adfSAndy Whitcroft 3980d8aaf121SAndy Whitcroft # ; should have either the end of line or a space or \ after it 398113214adfSAndy Whitcroft } elsif ($op eq ';') { 3982cf655043SAndy Whitcroft if ($ctx !~ /.x[WEBC]/ && 3983cf655043SAndy Whitcroft $cc !~ /^\\/ && $cc !~ /^;/) { 39843705ce5bSJoe Perches if (ERROR("SPACING", 39853705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 3986b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 39873705ce5bSJoe Perches $line_fixed = 1; 39883705ce5bSJoe Perches } 3989d8aaf121SAndy Whitcroft } 3990d8aaf121SAndy Whitcroft 3991d8aaf121SAndy Whitcroft # // is a comment 3992d8aaf121SAndy Whitcroft } elsif ($op eq '//') { 39930a920b5bSAndy Whitcroft 3994b00e4814SJoe Perches # : when part of a bitfield 3995b00e4814SJoe Perches } elsif ($opv eq ':B') { 3996b00e4814SJoe Perches # skip the bitfield test for now 3997b00e4814SJoe Perches 39981f65f947SAndy Whitcroft # No spaces for: 39991f65f947SAndy Whitcroft # -> 4000b00e4814SJoe Perches } elsif ($op eq '->') { 40014a0df2efSAndy Whitcroft if ($ctx =~ /Wx.|.xW/) { 40023705ce5bSJoe Perches if (ERROR("SPACING", 40033705ce5bSJoe Perches "spaces prohibited around that '$op' $at\n" . $hereptr)) { 4004b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 40053705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 40063705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 40073705ce5bSJoe Perches } 4008b34c648bSJoe Perches $line_fixed = 1; 40093705ce5bSJoe Perches } 40100a920b5bSAndy Whitcroft } 40110a920b5bSAndy Whitcroft 40122381097bSJoe Perches # , must not have a space before and must have a space on the right. 40130a920b5bSAndy Whitcroft } elsif ($op eq ',') { 40142381097bSJoe Perches my $rtrim_before = 0; 40152381097bSJoe Perches my $space_after = 0; 40162381097bSJoe Perches if ($ctx =~ /Wx./) { 40172381097bSJoe Perches if (ERROR("SPACING", 40182381097bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 40192381097bSJoe Perches $line_fixed = 1; 40202381097bSJoe Perches $rtrim_before = 1; 40212381097bSJoe Perches } 40222381097bSJoe Perches } 4023cf655043SAndy Whitcroft if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) { 40243705ce5bSJoe Perches if (ERROR("SPACING", 40253705ce5bSJoe Perches "space required after that '$op' $at\n" . $hereptr)) { 40263705ce5bSJoe Perches $line_fixed = 1; 4027b34c648bSJoe Perches $last_after = $n; 40282381097bSJoe Perches $space_after = 1; 40292381097bSJoe Perches } 40302381097bSJoe Perches } 40312381097bSJoe Perches if ($rtrim_before || $space_after) { 40322381097bSJoe Perches if ($rtrim_before) { 40332381097bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 40342381097bSJoe Perches } else { 40352381097bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 40362381097bSJoe Perches } 40372381097bSJoe Perches if ($space_after) { 40382381097bSJoe Perches $good .= " "; 40393705ce5bSJoe Perches } 40400a920b5bSAndy Whitcroft } 40410a920b5bSAndy Whitcroft 40429c0ca6f9SAndy Whitcroft # '*' as part of a type definition -- reported already. 404374048ed8SAndy Whitcroft } elsif ($opv eq '*_') { 40449c0ca6f9SAndy Whitcroft #warn "'*' is part of type\n"; 40459c0ca6f9SAndy Whitcroft 40469c0ca6f9SAndy Whitcroft # unary operators should have a space before and 40479c0ca6f9SAndy Whitcroft # none after. May be left adjacent to another 40489c0ca6f9SAndy Whitcroft # unary operator, or a cast 40499c0ca6f9SAndy Whitcroft } elsif ($op eq '!' || $op eq '~' || 405074048ed8SAndy Whitcroft $opv eq '*U' || $opv eq '-U' || 40510d413866SAndy Whitcroft $opv eq '&U' || $opv eq '&&U') { 4052cf655043SAndy Whitcroft if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) { 40533705ce5bSJoe Perches if (ERROR("SPACING", 40543705ce5bSJoe Perches "space required before that '$op' $at\n" . $hereptr)) { 4055b34c648bSJoe Perches if ($n != $last_after + 2) { 4056b34c648bSJoe Perches $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]); 40573705ce5bSJoe Perches $line_fixed = 1; 40583705ce5bSJoe Perches } 40590a920b5bSAndy Whitcroft } 4060b34c648bSJoe Perches } 4061a3340b35SAndy Whitcroft if ($op eq '*' && $cc =~/\s*$Modifier\b/) { 4062171ae1a4SAndy Whitcroft # A unary '*' may be const 4063171ae1a4SAndy Whitcroft 4064171ae1a4SAndy Whitcroft } elsif ($ctx =~ /.xW/) { 40653705ce5bSJoe Perches if (ERROR("SPACING", 40663705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4067b34c648bSJoe Perches $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]); 40683705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 40693705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 40703705ce5bSJoe Perches } 4071b34c648bSJoe Perches $line_fixed = 1; 40723705ce5bSJoe Perches } 40730a920b5bSAndy Whitcroft } 40740a920b5bSAndy Whitcroft 40750a920b5bSAndy Whitcroft # unary ++ and unary -- are allowed no space on one side. 40760a920b5bSAndy Whitcroft } elsif ($op eq '++' or $op eq '--') { 4077773647a0SAndy Whitcroft if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) { 40783705ce5bSJoe Perches if (ERROR("SPACING", 40793705ce5bSJoe Perches "space required one side of that '$op' $at\n" . $hereptr)) { 4080b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " "; 40813705ce5bSJoe Perches $line_fixed = 1; 40823705ce5bSJoe Perches } 40830a920b5bSAndy Whitcroft } 4084773647a0SAndy Whitcroft if ($ctx =~ /Wx[BE]/ || 4085773647a0SAndy Whitcroft ($ctx =~ /Wx./ && $cc =~ /^;/)) { 40863705ce5bSJoe Perches if (ERROR("SPACING", 40873705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4088b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 40893705ce5bSJoe Perches $line_fixed = 1; 40903705ce5bSJoe Perches } 4091653d4876SAndy Whitcroft } 4092773647a0SAndy Whitcroft if ($ctx =~ /ExW/) { 40933705ce5bSJoe Perches if (ERROR("SPACING", 40943705ce5bSJoe Perches "space prohibited after that '$op' $at\n" . $hereptr)) { 4095b34c648bSJoe Perches $good = $fix_elements[$n] . trim($fix_elements[$n + 1]); 40963705ce5bSJoe Perches if (defined $fix_elements[$n + 2]) { 40973705ce5bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4098773647a0SAndy Whitcroft } 4099b34c648bSJoe Perches $line_fixed = 1; 41003705ce5bSJoe Perches } 41013705ce5bSJoe Perches } 41020a920b5bSAndy Whitcroft 41030a920b5bSAndy Whitcroft # << and >> may either have or not have spaces both sides 41049c0ca6f9SAndy Whitcroft } elsif ($op eq '<<' or $op eq '>>' or 41059c0ca6f9SAndy Whitcroft $op eq '&' or $op eq '^' or $op eq '|' or 41069c0ca6f9SAndy Whitcroft $op eq '+' or $op eq '-' or 4107c2fdda0dSAndy Whitcroft $op eq '*' or $op eq '/' or 4108c2fdda0dSAndy Whitcroft $op eq '%') 41090a920b5bSAndy Whitcroft { 4110d2e025f3SJoe Perches if ($check) { 4111d2e025f3SJoe Perches if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) { 4112d2e025f3SJoe Perches if (CHK("SPACING", 4113d2e025f3SJoe Perches "spaces preferred around that '$op' $at\n" . $hereptr)) { 4114d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4115d2e025f3SJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4116d2e025f3SJoe Perches $line_fixed = 1; 4117d2e025f3SJoe Perches } 4118d2e025f3SJoe Perches } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) { 4119d2e025f3SJoe Perches if (CHK("SPACING", 4120d2e025f3SJoe Perches "space preferred before that '$op' $at\n" . $hereptr)) { 4121d2e025f3SJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]); 4122d2e025f3SJoe Perches $line_fixed = 1; 4123d2e025f3SJoe Perches } 4124d2e025f3SJoe Perches } 4125d2e025f3SJoe Perches } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) { 41263705ce5bSJoe Perches if (ERROR("SPACING", 41273705ce5bSJoe Perches "need consistent spacing around '$op' $at\n" . $hereptr)) { 4128b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4129b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4130b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4131b34c648bSJoe Perches } 41323705ce5bSJoe Perches $line_fixed = 1; 41333705ce5bSJoe Perches } 41340a920b5bSAndy Whitcroft } 41350a920b5bSAndy Whitcroft 41361f65f947SAndy Whitcroft # A colon needs no spaces before when it is 41371f65f947SAndy Whitcroft # terminating a case value or a label. 41381f65f947SAndy Whitcroft } elsif ($opv eq ':C' || $opv eq ':L') { 41391f65f947SAndy Whitcroft if ($ctx =~ /Wx./) { 41403705ce5bSJoe Perches if (ERROR("SPACING", 41413705ce5bSJoe Perches "space prohibited before that '$op' $at\n" . $hereptr)) { 4142b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]); 41433705ce5bSJoe Perches $line_fixed = 1; 41443705ce5bSJoe Perches } 41451f65f947SAndy Whitcroft } 41461f65f947SAndy Whitcroft 41470a920b5bSAndy Whitcroft # All the others need spaces both sides. 4148cf655043SAndy Whitcroft } elsif ($ctx !~ /[EWC]x[CWE]/) { 41491f65f947SAndy Whitcroft my $ok = 0; 41501f65f947SAndy Whitcroft 415122f2a2efSAndy Whitcroft # Ignore email addresses <foo@bar> 41521f65f947SAndy Whitcroft if (($op eq '<' && 41531f65f947SAndy Whitcroft $cc =~ /^\S+\@\S+>/) || 41541f65f947SAndy Whitcroft ($op eq '>' && 41551f65f947SAndy Whitcroft $ca =~ /<\S+\@\S+$/)) 41561f65f947SAndy Whitcroft { 41571f65f947SAndy Whitcroft $ok = 1; 41581f65f947SAndy Whitcroft } 41591f65f947SAndy Whitcroft 4160e0df7e1fSJoe Perches # for asm volatile statements 4161e0df7e1fSJoe Perches # ignore a colon with another 4162e0df7e1fSJoe Perches # colon immediately before or after 4163e0df7e1fSJoe Perches if (($op eq ':') && 4164e0df7e1fSJoe Perches ($ca =~ /:$/ || $cc =~ /^:/)) { 4165e0df7e1fSJoe Perches $ok = 1; 4166e0df7e1fSJoe Perches } 4167e0df7e1fSJoe Perches 416884731623SJoe Perches # messages are ERROR, but ?: are CHK 41691f65f947SAndy Whitcroft if ($ok == 0) { 417084731623SJoe Perches my $msg_type = \&ERROR; 417184731623SJoe Perches $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/); 417284731623SJoe Perches 417384731623SJoe Perches if (&{$msg_type}("SPACING", 41743705ce5bSJoe Perches "spaces required around that '$op' $at\n" . $hereptr)) { 4175b34c648bSJoe Perches $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " "; 4176b34c648bSJoe Perches if (defined $fix_elements[$n + 2]) { 4177b34c648bSJoe Perches $fix_elements[$n + 2] =~ s/^\s+//; 4178b34c648bSJoe Perches } 41793705ce5bSJoe Perches $line_fixed = 1; 41803705ce5bSJoe Perches } 41810a920b5bSAndy Whitcroft } 418222f2a2efSAndy Whitcroft } 41834a0df2efSAndy Whitcroft $off += length($elements[$n + 1]); 41843705ce5bSJoe Perches 41853705ce5bSJoe Perches## print("n: <$n> GOOD: <$good>\n"); 41863705ce5bSJoe Perches 41873705ce5bSJoe Perches $fixed_line = $fixed_line . $good; 41880a920b5bSAndy Whitcroft } 41893705ce5bSJoe Perches 41903705ce5bSJoe Perches if (($#elements % 2) == 0) { 41913705ce5bSJoe Perches $fixed_line = $fixed_line . $fix_elements[$#elements]; 41923705ce5bSJoe Perches } 41933705ce5bSJoe Perches 4194194f66fcSJoe Perches if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) { 4195194f66fcSJoe Perches $fixed[$fixlinenr] = $fixed_line; 41963705ce5bSJoe Perches } 41973705ce5bSJoe Perches 41983705ce5bSJoe Perches 41990a920b5bSAndy Whitcroft } 42000a920b5bSAndy Whitcroft 4201786b6326SJoe Perches# check for whitespace before a non-naked semicolon 4202d2e248e7SJoe Perches if ($line =~ /^\+.*\S\s+;\s*$/) { 4203786b6326SJoe Perches if (WARN("SPACING", 4204786b6326SJoe Perches "space prohibited before semicolon\n" . $herecurr) && 4205786b6326SJoe Perches $fix) { 4206194f66fcSJoe Perches 1 while $fixed[$fixlinenr] =~ 4207786b6326SJoe Perches s/^(\+.*\S)\s+;/$1;/; 4208786b6326SJoe Perches } 4209786b6326SJoe Perches } 4210786b6326SJoe Perches 4211f0a594c1SAndy Whitcroft# check for multiple assignments 4212f0a594c1SAndy Whitcroft if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) { 4213000d1cc1SJoe Perches CHK("MULTIPLE_ASSIGNMENTS", 4214000d1cc1SJoe Perches "multiple assignments should be avoided\n" . $herecurr); 4215f0a594c1SAndy Whitcroft } 4216f0a594c1SAndy Whitcroft 421722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration 421822f2a2efSAndy Whitcroft## # continuation. 421922f2a2efSAndy Whitcroft## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ && 422022f2a2efSAndy Whitcroft## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) { 422122f2a2efSAndy Whitcroft## 422222f2a2efSAndy Whitcroft## # Remove any bracketed sections to ensure we do not 422322f2a2efSAndy Whitcroft## # falsly report the parameters of functions. 422422f2a2efSAndy Whitcroft## my $ln = $line; 422522f2a2efSAndy Whitcroft## while ($ln =~ s/\([^\(\)]*\)//g) { 422622f2a2efSAndy Whitcroft## } 422722f2a2efSAndy Whitcroft## if ($ln =~ /,/) { 4228000d1cc1SJoe Perches## WARN("MULTIPLE_DECLARATION", 4229000d1cc1SJoe Perches## "declaring multiple variables together should be avoided\n" . $herecurr); 423022f2a2efSAndy Whitcroft## } 423122f2a2efSAndy Whitcroft## } 4232f0a594c1SAndy Whitcroft 42330a920b5bSAndy Whitcroft#need space before brace following if, while, etc 42346b8c69e4SGeyslan G. Bem if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) || 42354e5d56bdSEddie Kovsky $line =~ /do\{/) { 42363705ce5bSJoe Perches if (ERROR("SPACING", 42373705ce5bSJoe Perches "space required before the open brace '{'\n" . $herecurr) && 42383705ce5bSJoe Perches $fix) { 4239194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/; 42403705ce5bSJoe Perches } 4241de7d4f0eSAndy Whitcroft } 4242de7d4f0eSAndy Whitcroft 4243c4a62ef9SJoe Perches## # check for blank lines before declarations 4244c4a62ef9SJoe Perches## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ && 4245c4a62ef9SJoe Perches## $prevrawline =~ /^.\s*$/) { 4246c4a62ef9SJoe Perches## WARN("SPACING", 4247c4a62ef9SJoe Perches## "No blank lines before declarations\n" . $hereprev); 4248c4a62ef9SJoe Perches## } 4249c4a62ef9SJoe Perches## 4250c4a62ef9SJoe Perches 4251de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything 4252de7d4f0eSAndy Whitcroft# on the line 4253de7d4f0eSAndy Whitcroft if ($line =~ /}(?!(?:,|;|\)))\S/) { 4254d5e616fcSJoe Perches if (ERROR("SPACING", 4255d5e616fcSJoe Perches "space required after that close brace '}'\n" . $herecurr) && 4256d5e616fcSJoe Perches $fix) { 4257194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4258d5e616fcSJoe Perches s/}((?!(?:,|;|\)))\S)/} $1/; 4259d5e616fcSJoe Perches } 42600a920b5bSAndy Whitcroft } 42610a920b5bSAndy Whitcroft 426222f2a2efSAndy Whitcroft# check spacing on square brackets 426322f2a2efSAndy Whitcroft if ($line =~ /\[\s/ && $line !~ /\[\s*$/) { 42643705ce5bSJoe Perches if (ERROR("SPACING", 42653705ce5bSJoe Perches "space prohibited after that open square bracket '['\n" . $herecurr) && 42663705ce5bSJoe Perches $fix) { 4267194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42683705ce5bSJoe Perches s/\[\s+/\[/; 42693705ce5bSJoe Perches } 427022f2a2efSAndy Whitcroft } 427122f2a2efSAndy Whitcroft if ($line =~ /\s\]/) { 42723705ce5bSJoe Perches if (ERROR("SPACING", 42733705ce5bSJoe Perches "space prohibited before that close square bracket ']'\n" . $herecurr) && 42743705ce5bSJoe Perches $fix) { 4275194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42763705ce5bSJoe Perches s/\s+\]/\]/; 42773705ce5bSJoe Perches } 427822f2a2efSAndy Whitcroft } 427922f2a2efSAndy Whitcroft 4280c45dcabdSAndy Whitcroft# check spacing on parentheses 42819c0ca6f9SAndy Whitcroft if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ && 42829c0ca6f9SAndy Whitcroft $line !~ /for\s*\(\s+;/) { 42833705ce5bSJoe Perches if (ERROR("SPACING", 42843705ce5bSJoe Perches "space prohibited after that open parenthesis '('\n" . $herecurr) && 42853705ce5bSJoe Perches $fix) { 4286194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42873705ce5bSJoe Perches s/\(\s+/\(/; 42883705ce5bSJoe Perches } 428922f2a2efSAndy Whitcroft } 429013214adfSAndy Whitcroft if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ && 4291c45dcabdSAndy Whitcroft $line !~ /for\s*\(.*;\s+\)/ && 4292c45dcabdSAndy Whitcroft $line !~ /:\s+\)/) { 42933705ce5bSJoe Perches if (ERROR("SPACING", 42943705ce5bSJoe Perches "space prohibited before that close parenthesis ')'\n" . $herecurr) && 42953705ce5bSJoe Perches $fix) { 4296194f66fcSJoe Perches $fixed[$fixlinenr] =~ 42973705ce5bSJoe Perches s/\s+\)/\)/; 42983705ce5bSJoe Perches } 429922f2a2efSAndy Whitcroft } 430022f2a2efSAndy Whitcroft 4301e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals 4302e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar 4303e2826fd0SJoe Perches 4304e2826fd0SJoe Perches while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) { 4305ea4acbb1SJoe Perches my $var = $1; 4306ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4307ea4acbb1SJoe Perches "Unnecessary parentheses around $var\n" . $herecurr) && 4308ea4acbb1SJoe Perches $fix) { 4309ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/; 4310ea4acbb1SJoe Perches } 4311ea4acbb1SJoe Perches } 4312ea4acbb1SJoe Perches 4313ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses 4314ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar(); 4315ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives 4316ea4acbb1SJoe Perches if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) { 4317ea4acbb1SJoe Perches my $var = $2; 4318ea4acbb1SJoe Perches if (CHK("UNNECESSARY_PARENTHESES", 4319ea4acbb1SJoe Perches "Unnecessary parentheses around function pointer $var\n" . $herecurr) && 4320ea4acbb1SJoe Perches $fix) { 4321ea4acbb1SJoe Perches my $var2 = deparenthesize($var); 4322ea4acbb1SJoe Perches $var2 =~ s/\s//g; 4323ea4acbb1SJoe Perches $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/; 4324ea4acbb1SJoe Perches } 4325e2826fd0SJoe Perches } 4326e2826fd0SJoe Perches 43270a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however 43284a0df2efSAndy Whitcroft if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and 43290a920b5bSAndy Whitcroft !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) { 43303705ce5bSJoe Perches if (WARN("INDENTED_LABEL", 43313705ce5bSJoe Perches "labels should not be indented\n" . $herecurr) && 43323705ce5bSJoe Perches $fix) { 4333194f66fcSJoe Perches $fixed[$fixlinenr] =~ 43343705ce5bSJoe Perches s/^(.)\s+/$1/; 43353705ce5bSJoe Perches } 43360a920b5bSAndy Whitcroft } 43370a920b5bSAndy Whitcroft 43385b9553abSJoe Perches# return is not a function 4339507e5141SJoe Perches if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) { 4340c45dcabdSAndy Whitcroft my $spacing = $1; 4341507e5141SJoe Perches if ($^V && $^V ge 5.10.0 && 43425b9553abSJoe Perches $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) { 43435b9553abSJoe Perches my $value = $1; 43445b9553abSJoe Perches $value = deparenthesize($value); 43455b9553abSJoe Perches if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) { 4346000d1cc1SJoe Perches ERROR("RETURN_PARENTHESES", 4347000d1cc1SJoe Perches "return is not a function, parentheses are not required\n" . $herecurr); 43485b9553abSJoe Perches } 4349c45dcabdSAndy Whitcroft } elsif ($spacing !~ /\s+/) { 4350000d1cc1SJoe Perches ERROR("SPACING", 4351000d1cc1SJoe Perches "space required before the open parenthesis '('\n" . $herecurr); 4352c45dcabdSAndy Whitcroft } 4353c45dcabdSAndy Whitcroft } 4354507e5141SJoe Perches 4355b43ae21bSJoe Perches# unnecessary return in a void function 4356b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return; 4357b43ae21bSJoe Perches# and the line before that not a goto label target like "out:" 4358b43ae21bSJoe Perches if ($sline =~ /^[ \+]}\s*$/ && 4359b43ae21bSJoe Perches $prevline =~ /^\+\treturn\s*;\s*$/ && 4360b43ae21bSJoe Perches $linenr >= 3 && 4361b43ae21bSJoe Perches $lines[$linenr - 3] =~ /^[ +]/ && 4362b43ae21bSJoe Perches $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) { 43639819cf25SJoe Perches WARN("RETURN_VOID", 4364b43ae21bSJoe Perches "void function return statements are not generally useful\n" . $hereprev); 43659819cf25SJoe Perches } 43669819cf25SJoe Perches 4367189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar)) 4368189248d8SJoe Perches if ($^V && $^V ge 5.10.0 && 4369189248d8SJoe Perches $line =~ /\bif\s*((?:\(\s*){2,})/) { 4370189248d8SJoe Perches my $openparens = $1; 4371189248d8SJoe Perches my $count = $openparens =~ tr@\(@\(@; 4372189248d8SJoe Perches my $msg = ""; 4373189248d8SJoe Perches if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) { 4374189248d8SJoe Perches my $comp = $4; #Not $1 because of $LvalOrFunc 4375189248d8SJoe Perches $msg = " - maybe == should be = ?" if ($comp eq "=="); 4376189248d8SJoe Perches WARN("UNNECESSARY_PARENTHESES", 4377189248d8SJoe Perches "Unnecessary parentheses$msg\n" . $herecurr); 4378189248d8SJoe Perches } 4379189248d8SJoe Perches } 4380189248d8SJoe Perches 4381c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left 4382c5595fa2SJoe Perches# avoid cases like "foo + BAR < baz" 4383c5595fa2SJoe Perches# only fix matches surrounded by parentheses to avoid incorrect 4384c5595fa2SJoe Perches# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5" 4385c5595fa2SJoe Perches if ($^V && $^V ge 5.10.0 && 4386c5595fa2SJoe Perches $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) { 4387c5595fa2SJoe Perches my $lead = $1; 4388c5595fa2SJoe Perches my $const = $2; 4389c5595fa2SJoe Perches my $comp = $3; 4390c5595fa2SJoe Perches my $to = $4; 4391c5595fa2SJoe Perches my $newcomp = $comp; 4392f39e1769SJoe Perches if ($lead !~ /(?:$Operators|\.)\s*$/ && 4393c5595fa2SJoe Perches $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ && 4394c5595fa2SJoe Perches WARN("CONSTANT_COMPARISON", 4395c5595fa2SJoe Perches "Comparisons should place the constant on the right side of the test\n" . $herecurr) && 4396c5595fa2SJoe Perches $fix) { 4397c5595fa2SJoe Perches if ($comp eq "<") { 4398c5595fa2SJoe Perches $newcomp = ">"; 4399c5595fa2SJoe Perches } elsif ($comp eq "<=") { 4400c5595fa2SJoe Perches $newcomp = ">="; 4401c5595fa2SJoe Perches } elsif ($comp eq ">") { 4402c5595fa2SJoe Perches $newcomp = "<"; 4403c5595fa2SJoe Perches } elsif ($comp eq ">=") { 4404c5595fa2SJoe Perches $newcomp = "<="; 4405c5595fa2SJoe Perches } 4406c5595fa2SJoe Perches $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/; 4407c5595fa2SJoe Perches } 4408c5595fa2SJoe Perches } 4409c5595fa2SJoe Perches 4410f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative 4411f34e4a4fSJoe Perches if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) { 441253a3c448SAndy Whitcroft my $name = $1; 441353a3c448SAndy Whitcroft if ($name ne 'EOF' && $name ne 'ERROR') { 4414000d1cc1SJoe Perches WARN("USE_NEGATIVE_ERRNO", 4415f34e4a4fSJoe Perches "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr); 441653a3c448SAndy Whitcroft } 441753a3c448SAndy Whitcroft } 4418c45dcabdSAndy Whitcroft 44190a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc 44204a0df2efSAndy Whitcroft if ($line =~ /\b(if|while|for|switch)\(/) { 44213705ce5bSJoe Perches if (ERROR("SPACING", 44223705ce5bSJoe Perches "space required before the open parenthesis '('\n" . $herecurr) && 44233705ce5bSJoe Perches $fix) { 4424194f66fcSJoe Perches $fixed[$fixlinenr] =~ 44253705ce5bSJoe Perches s/\b(if|while|for|switch)\(/$1 \(/; 44263705ce5bSJoe Perches } 44270a920b5bSAndy Whitcroft } 44280a920b5bSAndy Whitcroft 4429f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing 4430f5fe35ddSAndy Whitcroft# statements after the conditional. 4431170d3a22SAndy Whitcroft if ($line =~ /do\s*(?!{)/) { 44323e469cdcSAndy Whitcroft ($stat, $cond, $line_nr_next, $remain_next, $off_next) = 44333e469cdcSAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0) 44343e469cdcSAndy Whitcroft if (!defined $stat); 4435170d3a22SAndy Whitcroft my ($stat_next) = ctx_statement_block($line_nr_next, 4436170d3a22SAndy Whitcroft $remain_next, $off_next); 4437170d3a22SAndy Whitcroft $stat_next =~ s/\n./\n /g; 4438170d3a22SAndy Whitcroft ##print "stat<$stat> stat_next<$stat_next>\n"; 4439170d3a22SAndy Whitcroft 4440170d3a22SAndy Whitcroft if ($stat_next =~ /^\s*while\b/) { 4441170d3a22SAndy Whitcroft # If the statement carries leading newlines, 4442170d3a22SAndy Whitcroft # then count those as offsets. 4443170d3a22SAndy Whitcroft my ($whitespace) = 4444170d3a22SAndy Whitcroft ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s); 4445170d3a22SAndy Whitcroft my $offset = 4446170d3a22SAndy Whitcroft statement_rawlines($whitespace) - 1; 4447170d3a22SAndy Whitcroft 4448170d3a22SAndy Whitcroft $suppress_whiletrailers{$line_nr_next + 4449170d3a22SAndy Whitcroft $offset} = 1; 4450170d3a22SAndy Whitcroft } 4451170d3a22SAndy Whitcroft } 4452170d3a22SAndy Whitcroft if (!defined $suppress_whiletrailers{$linenr} && 4453c11230f4SJoe Perches defined($stat) && defined($cond) && 4454170d3a22SAndy Whitcroft $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) { 4455171ae1a4SAndy Whitcroft my ($s, $c) = ($stat, $cond); 44568905a67cSAndy Whitcroft 4457b53c8e10SAndy Whitcroft if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) { 4458000d1cc1SJoe Perches ERROR("ASSIGN_IN_IF", 4459000d1cc1SJoe Perches "do not use assignment in if condition\n" . $herecurr); 44608905a67cSAndy Whitcroft } 44618905a67cSAndy Whitcroft 44628905a67cSAndy Whitcroft # Find out what is on the end of the line after the 44638905a67cSAndy Whitcroft # conditional. 4464773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 44658905a67cSAndy Whitcroft $s =~ s/\n.*//g; 446613214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 446753210168SAndy Whitcroft if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ && 446853210168SAndy Whitcroft $c !~ /}\s*while\s*/) 4469773647a0SAndy Whitcroft { 4470bb44ad39SAndy Whitcroft # Find out how long the conditional actually is. 4471bb44ad39SAndy Whitcroft my @newlines = ($c =~ /\n/gs); 4472bb44ad39SAndy Whitcroft my $cond_lines = 1 + $#newlines; 447342bdf74cSHidetoshi Seto my $stat_real = ''; 4474bb44ad39SAndy Whitcroft 447542bdf74cSHidetoshi Seto $stat_real = raw_line($linenr, $cond_lines) 447642bdf74cSHidetoshi Seto . "\n" if ($cond_lines); 4477bb44ad39SAndy Whitcroft if (defined($stat_real) && $cond_lines > 1) { 4478bb44ad39SAndy Whitcroft $stat_real = "[...]\n$stat_real"; 4479bb44ad39SAndy Whitcroft } 4480bb44ad39SAndy Whitcroft 4481000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4482000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr . $stat_real); 44838905a67cSAndy Whitcroft } 44848905a67cSAndy Whitcroft } 44858905a67cSAndy Whitcroft 448613214adfSAndy Whitcroft# Check for bitwise tests written as boolean 448713214adfSAndy Whitcroft if ($line =~ / 448813214adfSAndy Whitcroft (?: 448913214adfSAndy Whitcroft (?:\[|\(|\&\&|\|\|) 449013214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 449113214adfSAndy Whitcroft (?:\&\&|\|\|) 449213214adfSAndy Whitcroft | 449313214adfSAndy Whitcroft (?:\&\&|\|\|) 449413214adfSAndy Whitcroft \s*0[xX][0-9]+\s* 449513214adfSAndy Whitcroft (?:\&\&|\|\||\)|\]) 449613214adfSAndy Whitcroft )/x) 449713214adfSAndy Whitcroft { 4498000d1cc1SJoe Perches WARN("HEXADECIMAL_BOOLEAN_TEST", 4499000d1cc1SJoe Perches "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr); 450013214adfSAndy Whitcroft } 450113214adfSAndy Whitcroft 45028905a67cSAndy Whitcroft# if and else should not have general statements after it 450313214adfSAndy Whitcroft if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) { 450413214adfSAndy Whitcroft my $s = $1; 450513214adfSAndy Whitcroft $s =~ s/$;//g; # Remove any comments 450613214adfSAndy Whitcroft if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) { 4507000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4508000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 45090a920b5bSAndy Whitcroft } 451013214adfSAndy Whitcroft } 451139667782SAndy Whitcroft# if should not continue a brace 451239667782SAndy Whitcroft if ($line =~ /}\s*if\b/) { 4513000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4514048b123fSRasmus Villemoes "trailing statements should be on next line (or did you mean 'else if'?)\n" . 451539667782SAndy Whitcroft $herecurr); 451639667782SAndy Whitcroft } 4517a1080bf8SAndy Whitcroft# case and default should not have general statements after them 4518a1080bf8SAndy Whitcroft if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g && 4519a1080bf8SAndy Whitcroft $line !~ /\G(?: 45203fef12d6SAndy Whitcroft (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$| 4521a1080bf8SAndy Whitcroft \s*return\s+ 4522a1080bf8SAndy Whitcroft )/xg) 4523a1080bf8SAndy Whitcroft { 4524000d1cc1SJoe Perches ERROR("TRAILING_STATEMENTS", 4525000d1cc1SJoe Perches "trailing statements should be on next line\n" . $herecurr); 4526a1080bf8SAndy Whitcroft } 45270a920b5bSAndy Whitcroft 45280a920b5bSAndy Whitcroft # Check for }<nl>else {, these must be at the same 45290a920b5bSAndy Whitcroft # indent level to be relevant to each other. 45308b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ && 45310a920b5bSAndy Whitcroft $previndent == $indent) { 45328b8856f4SJoe Perches if (ERROR("ELSE_AFTER_BRACE", 45338b8856f4SJoe Perches "else should follow close brace '}'\n" . $hereprev) && 45348b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 45358b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 45368b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 45378b8856f4SJoe Perches my $fixedline = $prevrawline; 45388b8856f4SJoe Perches $fixedline =~ s/}\s*$//; 45398b8856f4SJoe Perches if ($fixedline !~ /^\+\s*$/) { 45408b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 45418b8856f4SJoe Perches } 45428b8856f4SJoe Perches $fixedline = $rawline; 45438b8856f4SJoe Perches $fixedline =~ s/^(.\s*)else/$1} else/; 45448b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 45458b8856f4SJoe Perches } 45460a920b5bSAndy Whitcroft } 45470a920b5bSAndy Whitcroft 45488b8856f4SJoe Perches if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ && 4549c2fdda0dSAndy Whitcroft $previndent == $indent) { 4550c2fdda0dSAndy Whitcroft my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0); 4551c2fdda0dSAndy Whitcroft 4552c2fdda0dSAndy Whitcroft # Find out what is on the end of the line after the 4553c2fdda0dSAndy Whitcroft # conditional. 4554773647a0SAndy Whitcroft substr($s, 0, length($c), ''); 4555c2fdda0dSAndy Whitcroft $s =~ s/\n.*//g; 4556c2fdda0dSAndy Whitcroft 4557c2fdda0dSAndy Whitcroft if ($s =~ /^\s*;/) { 45588b8856f4SJoe Perches if (ERROR("WHILE_AFTER_BRACE", 45598b8856f4SJoe Perches "while should follow close brace '}'\n" . $hereprev) && 45608b8856f4SJoe Perches $fix && $prevline =~ /^\+/ && $line =~ /^\+/) { 45618b8856f4SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 45628b8856f4SJoe Perches fix_delete_line($fixlinenr, $rawline); 45638b8856f4SJoe Perches my $fixedline = $prevrawline; 45648b8856f4SJoe Perches my $trailing = $rawline; 45658b8856f4SJoe Perches $trailing =~ s/^\+//; 45668b8856f4SJoe Perches $trailing = trim($trailing); 45678b8856f4SJoe Perches $fixedline =~ s/}\s*$/} $trailing/; 45688b8856f4SJoe Perches fix_insert_line($fixlinenr, $fixedline); 45698b8856f4SJoe Perches } 4570c2fdda0dSAndy Whitcroft } 4571c2fdda0dSAndy Whitcroft } 4572c2fdda0dSAndy Whitcroft 457395e2c602SJoe Perches#Specific variable tests 4574323c1260SJoe Perches while ($line =~ m{($Constant|$Lval)}g) { 4575323c1260SJoe Perches my $var = $1; 457695e2c602SJoe Perches 457795e2c602SJoe Perches#gcc binary extension 457895e2c602SJoe Perches if ($var =~ /^$Binary$/) { 4579d5e616fcSJoe Perches if (WARN("GCC_BINARY_CONSTANT", 4580d5e616fcSJoe Perches "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) && 4581d5e616fcSJoe Perches $fix) { 4582d5e616fcSJoe Perches my $hexval = sprintf("0x%x", oct($var)); 4583194f66fcSJoe Perches $fixed[$fixlinenr] =~ 4584d5e616fcSJoe Perches s/\b$var\b/$hexval/; 4585d5e616fcSJoe Perches } 458695e2c602SJoe Perches } 458795e2c602SJoe Perches 458895e2c602SJoe Perches#CamelCase 4589807bd26cSJoe Perches if ($var !~ /^$Constant$/ && 4590be79794bSJoe Perches $var =~ /[A-Z][a-z]|[a-z][A-Z]/ && 459122735ce8SJoe Perches#Ignore Page<foo> variants 4592807bd26cSJoe Perches $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ && 459322735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show) 4594f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ && 4595f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz 4596f5123576SJulius Werner $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) { 45977e781f67SJoe Perches while ($var =~ m{($Ident)}g) { 45987e781f67SJoe Perches my $word = $1; 45997e781f67SJoe Perches next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/); 4600d8b07710SJoe Perches if ($check) { 4601d8b07710SJoe Perches seed_camelcase_includes(); 4602d8b07710SJoe Perches if (!$file && !$camelcase_file_seeded) { 4603d8b07710SJoe Perches seed_camelcase_file($realfile); 4604d8b07710SJoe Perches $camelcase_file_seeded = 1; 4605d8b07710SJoe Perches } 4606d8b07710SJoe Perches } 46077e781f67SJoe Perches if (!defined $camelcase{$word}) { 46087e781f67SJoe Perches $camelcase{$word} = 1; 4609be79794bSJoe Perches CHK("CAMELCASE", 46107e781f67SJoe Perches "Avoid CamelCase: <$word>\n" . $herecurr); 46117e781f67SJoe Perches } 4612323c1260SJoe Perches } 4613323c1260SJoe Perches } 46143445686aSJoe Perches } 46150a920b5bSAndy Whitcroft 46160a920b5bSAndy Whitcroft#no spaces allowed after \ in define 4617d5e616fcSJoe Perches if ($line =~ /\#\s*define.*\\\s+$/) { 4618d5e616fcSJoe Perches if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION", 4619d5e616fcSJoe Perches "Whitespace after \\ makes next lines useless\n" . $herecurr) && 4620d5e616fcSJoe Perches $fix) { 4621194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\s+$//; 4622d5e616fcSJoe Perches } 46230a920b5bSAndy Whitcroft } 46240a920b5bSAndy Whitcroft 46250e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes 46260e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line) 4627c45dcabdSAndy Whitcroft if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) { 4628e09dec48SAndy Whitcroft my $file = "$1.h"; 4629e09dec48SAndy Whitcroft my $checkfile = "include/linux/$file"; 4630e09dec48SAndy Whitcroft if (-f "$root/$checkfile" && 4631e09dec48SAndy Whitcroft $realfile ne $checkfile && 46327840a94cSWolfram Sang $1 !~ /$allowed_asm_includes/) 4633c45dcabdSAndy Whitcroft { 46340e212e0aSFabian Frederick my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`; 46350e212e0aSFabian Frederick if ($asminclude > 0) { 4636e09dec48SAndy Whitcroft if ($realfile =~ m{^arch/}) { 4637000d1cc1SJoe Perches CHK("ARCH_INCLUDE_LINUX", 4638000d1cc1SJoe Perches "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4639e09dec48SAndy Whitcroft } else { 4640000d1cc1SJoe Perches WARN("INCLUDE_LINUX", 4641000d1cc1SJoe Perches "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr); 4642e09dec48SAndy Whitcroft } 46430a920b5bSAndy Whitcroft } 46440a920b5bSAndy Whitcroft } 46450e212e0aSFabian Frederick } 46460a920b5bSAndy Whitcroft 4647653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the 4648653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed 4649cf655043SAndy Whitcroft# in a known good container 4650b8f96a31SAndy Whitcroft if ($realfile !~ m@/vmlinux.lds.h$@ && 4651b8f96a31SAndy Whitcroft $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) { 4652d8aaf121SAndy Whitcroft my $ln = $linenr; 4653d8aaf121SAndy Whitcroft my $cnt = $realcnt; 4654c45dcabdSAndy Whitcroft my ($off, $dstat, $dcond, $rest); 4655c45dcabdSAndy Whitcroft my $ctx = ''; 465608a2843eSJoe Perches my $has_flow_statement = 0; 465708a2843eSJoe Perches my $has_arg_concat = 0; 4658c45dcabdSAndy Whitcroft ($dstat, $dcond, $ln, $cnt, $off) = 4659f74bd194SAndy Whitcroft ctx_statement_block($linenr, $realcnt, 0); 4660f74bd194SAndy Whitcroft $ctx = $dstat; 4661c45dcabdSAndy Whitcroft #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n"; 4662a3bb97a7SAndy Whitcroft #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n"; 4663c45dcabdSAndy Whitcroft 466408a2843eSJoe Perches $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/); 466562e15a6dSJoe Perches $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/); 466608a2843eSJoe Perches 4667f74bd194SAndy Whitcroft $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//; 4668292f1a9bSAndy Whitcroft $dstat =~ s/$;//g; 4669c45dcabdSAndy Whitcroft $dstat =~ s/\\\n.//g; 4670c45dcabdSAndy Whitcroft $dstat =~ s/^\s*//s; 4671c45dcabdSAndy Whitcroft $dstat =~ s/\s*$//s; 4672c45dcabdSAndy Whitcroft 4673c45dcabdSAndy Whitcroft # Flatten any parentheses and braces 4674bf30d6edSAndy Whitcroft while ($dstat =~ s/\([^\(\)]*\)/1/ || 4675bf30d6edSAndy Whitcroft $dstat =~ s/\{[^\{\}]*\}/1/ || 46766b10df42SVladimir Zapolskiy $dstat =~ s/.\[[^\[\]]*\]/1/) 4677bf30d6edSAndy Whitcroft { 4678c45dcabdSAndy Whitcroft } 4679c45dcabdSAndy Whitcroft 4680e45bab8eSAndy Whitcroft # Flatten any obvious string concatentation. 468133acb54aSJoe Perches while ($dstat =~ s/($String)\s*$Ident/$1/ || 468233acb54aSJoe Perches $dstat =~ s/$Ident\s*($String)/$1/) 4683e45bab8eSAndy Whitcroft { 4684e45bab8eSAndy Whitcroft } 4685e45bab8eSAndy Whitcroft 468642e15293SJoe Perches # Make asm volatile uses seem like a generic function 468742e15293SJoe Perches $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g; 468842e15293SJoe Perches 4689c45dcabdSAndy Whitcroft my $exceptions = qr{ 4690c45dcabdSAndy Whitcroft $Declare| 4691c45dcabdSAndy Whitcroft module_param_named| 4692a0a0a7a9SKees Cook MODULE_PARM_DESC| 4693c45dcabdSAndy Whitcroft DECLARE_PER_CPU| 4694c45dcabdSAndy Whitcroft DEFINE_PER_CPU| 4695383099fdSAndy Whitcroft __typeof__\(| 469622fd2d3eSStefani Seibold union| 469722fd2d3eSStefani Seibold struct| 4698ea71a0a0SAndy Whitcroft \.$Ident\s*=\s*| 46996b10df42SVladimir Zapolskiy ^\"|\"$| 47006b10df42SVladimir Zapolskiy ^\[ 4701c45dcabdSAndy Whitcroft }x; 47025eaa20b9SAndy Whitcroft #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n"; 4703f74bd194SAndy Whitcroft if ($dstat ne '' && 4704f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(), 4705f74bd194SAndy Whitcroft $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo(); 47063cc4b1c3SJoe Perches $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz 4707356fd398SJoe Perches $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants 4708f74bd194SAndy Whitcroft $dstat !~ /$exceptions/ && 4709f74bd194SAndy Whitcroft $dstat !~ /^\.$Ident\s*=/ && # .foo = 4710e942e2c3SJoe Perches $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo 471172f115f9SAndy Whitcroft $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...) 4712f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant$/ && # for (...) 4713f74bd194SAndy Whitcroft $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar() 4714f74bd194SAndy Whitcroft $dstat !~ /^do\s*{/ && # do {... 47154e5d56bdSEddie Kovsky $dstat !~ /^\(\{/ && # ({... 4716f95a7e6aSJoe Perches $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/) 4717c45dcabdSAndy Whitcroft { 4718f74bd194SAndy Whitcroft $ctx =~ s/\n*$//; 4719f74bd194SAndy Whitcroft my $herectx = $here . "\n"; 4720f74bd194SAndy Whitcroft my $cnt = statement_rawlines($ctx); 4721f74bd194SAndy Whitcroft 4722f74bd194SAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 4723f74bd194SAndy Whitcroft $herectx .= raw_line($linenr, $n) . "\n"; 4724c45dcabdSAndy Whitcroft } 4725c45dcabdSAndy Whitcroft 4726f74bd194SAndy Whitcroft if ($dstat =~ /;/) { 4727f74bd194SAndy Whitcroft ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE", 4728f74bd194SAndy Whitcroft "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx"); 4729f74bd194SAndy Whitcroft } else { 4730000d1cc1SJoe Perches ERROR("COMPLEX_MACRO", 4731388982b5SAndrew Morton "Macros with complex values should be enclosed in parentheses\n" . "$herectx"); 4732d8aaf121SAndy Whitcroft } 47330a920b5bSAndy Whitcroft } 47345023d347SJoe Perches 473508a2843eSJoe Perches# check for macros with flow control, but without ## concatenation 473608a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those 473708a2843eSJoe Perches if ($has_flow_statement && !$has_arg_concat) { 473808a2843eSJoe Perches my $herectx = $here . "\n"; 473908a2843eSJoe Perches my $cnt = statement_rawlines($ctx); 474008a2843eSJoe Perches 474108a2843eSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 474208a2843eSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 474308a2843eSJoe Perches } 474408a2843eSJoe Perches WARN("MACRO_WITH_FLOW_CONTROL", 474508a2843eSJoe Perches "Macros with flow control statements should be avoided\n" . "$herectx"); 474608a2843eSJoe Perches } 474708a2843eSJoe Perches 4748481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm 47495023d347SJoe Perches 47505023d347SJoe Perches } else { 47515023d347SJoe Perches if ($prevline !~ /^..*\\$/ && 4752481eb486SJoe Perches $line !~ /^\+\s*\#.*\\$/ && # preprocessor 4753481eb486SJoe Perches $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm 47545023d347SJoe Perches $line =~ /^\+.*\\$/) { 47555023d347SJoe Perches WARN("LINE_CONTINUATIONS", 47565023d347SJoe Perches "Avoid unnecessary line continuations\n" . $herecurr); 47575023d347SJoe Perches } 4758653d4876SAndy Whitcroft } 47590a920b5bSAndy Whitcroft 4760b13edf7fSJoe Perches# do {} while (0) macro tests: 4761b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop, 4762b13edf7fSJoe Perches# macro should not end with a semicolon 4763b13edf7fSJoe Perches if ($^V && $^V ge 5.10.0 && 4764b13edf7fSJoe Perches $realfile !~ m@/vmlinux.lds.h$@ && 4765b13edf7fSJoe Perches $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) { 4766b13edf7fSJoe Perches my $ln = $linenr; 4767b13edf7fSJoe Perches my $cnt = $realcnt; 4768b13edf7fSJoe Perches my ($off, $dstat, $dcond, $rest); 4769b13edf7fSJoe Perches my $ctx = ''; 4770b13edf7fSJoe Perches ($dstat, $dcond, $ln, $cnt, $off) = 4771b13edf7fSJoe Perches ctx_statement_block($linenr, $realcnt, 0); 4772b13edf7fSJoe Perches $ctx = $dstat; 4773b13edf7fSJoe Perches 4774b13edf7fSJoe Perches $dstat =~ s/\\\n.//g; 47751b36b201SJoe Perches $dstat =~ s/$;/ /g; 4776b13edf7fSJoe Perches 4777b13edf7fSJoe Perches if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) { 4778b13edf7fSJoe Perches my $stmts = $2; 4779b13edf7fSJoe Perches my $semis = $3; 4780b13edf7fSJoe Perches 4781b13edf7fSJoe Perches $ctx =~ s/\n*$//; 4782b13edf7fSJoe Perches my $cnt = statement_rawlines($ctx); 4783b13edf7fSJoe Perches my $herectx = $here . "\n"; 4784b13edf7fSJoe Perches 4785b13edf7fSJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4786b13edf7fSJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4787b13edf7fSJoe Perches } 4788b13edf7fSJoe Perches 4789ac8e97f8SJoe Perches if (($stmts =~ tr/;/;/) == 1 && 4790ac8e97f8SJoe Perches $stmts !~ /^\s*(if|while|for|switch)\b/) { 4791b13edf7fSJoe Perches WARN("SINGLE_STATEMENT_DO_WHILE_MACRO", 4792b13edf7fSJoe Perches "Single statement macros should not use a do {} while (0) loop\n" . "$herectx"); 4793b13edf7fSJoe Perches } 4794b13edf7fSJoe Perches if (defined $semis && $semis ne "") { 4795b13edf7fSJoe Perches WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON", 4796b13edf7fSJoe Perches "do {} while (0) macros should not be semicolon terminated\n" . "$herectx"); 4797b13edf7fSJoe Perches } 4798f5ef95b1SJoe Perches } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) { 4799f5ef95b1SJoe Perches $ctx =~ s/\n*$//; 4800f5ef95b1SJoe Perches my $cnt = statement_rawlines($ctx); 4801f5ef95b1SJoe Perches my $herectx = $here . "\n"; 4802f5ef95b1SJoe Perches 4803f5ef95b1SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 4804f5ef95b1SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 4805f5ef95b1SJoe Perches } 4806f5ef95b1SJoe Perches 4807f5ef95b1SJoe Perches WARN("TRAILING_SEMICOLON", 4808f5ef95b1SJoe Perches "macros should not use a trailing semicolon\n" . "$herectx"); 4809b13edf7fSJoe Perches } 4810b13edf7fSJoe Perches } 4811b13edf7fSJoe Perches 4812080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ... 4813080ba929SMike Frysinger# all assignments may have only one of the following with an assignment: 4814080ba929SMike Frysinger# . 4815080ba929SMike Frysinger# ALIGN(...) 4816080ba929SMike Frysinger# VMLINUX_SYMBOL(...) 4817080ba929SMike Frysinger if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) { 4818000d1cc1SJoe Perches WARN("MISSING_VMLINUX_SYMBOL", 4819000d1cc1SJoe Perches "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr); 4820080ba929SMike Frysinger } 4821080ba929SMike Frysinger 4822f0a594c1SAndy Whitcroft# check for redundant bracing round if etc 482313214adfSAndy Whitcroft if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) { 482413214adfSAndy Whitcroft my ($level, $endln, @chunks) = 4825cf655043SAndy Whitcroft ctx_statement_full($linenr, $realcnt, 1); 482613214adfSAndy Whitcroft #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n"; 4827cf655043SAndy Whitcroft #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n"; 4828cf655043SAndy Whitcroft if ($#chunks > 0 && $level == 0) { 4829aad4f614SJoe Perches my @allowed = (); 4830aad4f614SJoe Perches my $allow = 0; 483113214adfSAndy Whitcroft my $seen = 0; 4832773647a0SAndy Whitcroft my $herectx = $here . "\n"; 4833cf655043SAndy Whitcroft my $ln = $linenr - 1; 483413214adfSAndy Whitcroft for my $chunk (@chunks) { 483513214adfSAndy Whitcroft my ($cond, $block) = @{$chunk}; 483613214adfSAndy Whitcroft 4837773647a0SAndy Whitcroft # If the condition carries leading newlines, then count those as offsets. 4838773647a0SAndy Whitcroft my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s); 4839773647a0SAndy Whitcroft my $offset = statement_rawlines($whitespace) - 1; 4840773647a0SAndy Whitcroft 4841aad4f614SJoe Perches $allowed[$allow] = 0; 4842773647a0SAndy Whitcroft #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n"; 4843773647a0SAndy Whitcroft 4844773647a0SAndy Whitcroft # We have looked at and allowed this specific line. 4845773647a0SAndy Whitcroft $suppress_ifbraces{$ln + $offset} = 1; 4846773647a0SAndy Whitcroft 4847773647a0SAndy Whitcroft $herectx .= "$rawlines[$ln + $offset]\n[...]\n"; 4848cf655043SAndy Whitcroft $ln += statement_rawlines($block) - 1; 4849cf655043SAndy Whitcroft 4850773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 485113214adfSAndy Whitcroft 485213214adfSAndy Whitcroft $seen++ if ($block =~ /^\s*{/); 485313214adfSAndy Whitcroft 4854aad4f614SJoe Perches #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n"; 4855cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 4856cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 4857aad4f614SJoe Perches $allowed[$allow] = 1; 485813214adfSAndy Whitcroft } 485913214adfSAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 4860cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 4861aad4f614SJoe Perches $allowed[$allow] = 1; 486213214adfSAndy Whitcroft } 4863cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 4864cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 4865aad4f614SJoe Perches $allowed[$allow] = 1; 486613214adfSAndy Whitcroft } 4867aad4f614SJoe Perches $allow++; 486813214adfSAndy Whitcroft } 4869aad4f614SJoe Perches if ($seen) { 4870aad4f614SJoe Perches my $sum_allowed = 0; 4871aad4f614SJoe Perches foreach (@allowed) { 4872aad4f614SJoe Perches $sum_allowed += $_; 4873aad4f614SJoe Perches } 4874aad4f614SJoe Perches if ($sum_allowed == 0) { 4875000d1cc1SJoe Perches WARN("BRACES", 4876000d1cc1SJoe Perches "braces {} are not necessary for any arm of this statement\n" . $herectx); 4877aad4f614SJoe Perches } elsif ($sum_allowed != $allow && 4878aad4f614SJoe Perches $seen != $allow) { 4879aad4f614SJoe Perches CHK("BRACES", 4880aad4f614SJoe Perches "braces {} should be used on all arms of this statement\n" . $herectx); 4881aad4f614SJoe Perches } 488213214adfSAndy Whitcroft } 488313214adfSAndy Whitcroft } 488413214adfSAndy Whitcroft } 4885773647a0SAndy Whitcroft if (!defined $suppress_ifbraces{$linenr - 1} && 488613214adfSAndy Whitcroft $line =~ /\b(if|while|for|else)\b/) { 4887cf655043SAndy Whitcroft my $allowed = 0; 4888f0a594c1SAndy Whitcroft 4889cf655043SAndy Whitcroft # Check the pre-context. 4890cf655043SAndy Whitcroft if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) { 4891cf655043SAndy Whitcroft #print "APW: ALLOWED: pre<$1>\n"; 4892cf655043SAndy Whitcroft $allowed = 1; 4893f0a594c1SAndy Whitcroft } 4894773647a0SAndy Whitcroft 4895773647a0SAndy Whitcroft my ($level, $endln, @chunks) = 4896773647a0SAndy Whitcroft ctx_statement_full($linenr, $realcnt, $-[0]); 4897773647a0SAndy Whitcroft 4898cf655043SAndy Whitcroft # Check the condition. 4899cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[0]}; 4900773647a0SAndy Whitcroft #print "CHECKING<$linenr> cond<$cond> block<$block>\n"; 4901cf655043SAndy Whitcroft if (defined $cond) { 4902773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 4903cf655043SAndy Whitcroft } 4904cf655043SAndy Whitcroft if (statement_lines($cond) > 1) { 4905cf655043SAndy Whitcroft #print "APW: ALLOWED: cond<$cond>\n"; 4906cf655043SAndy Whitcroft $allowed = 1; 4907cf655043SAndy Whitcroft } 4908cf655043SAndy Whitcroft if ($block =~/\b(?:if|for|while)\b/) { 4909cf655043SAndy Whitcroft #print "APW: ALLOWED: block<$block>\n"; 4910cf655043SAndy Whitcroft $allowed = 1; 4911cf655043SAndy Whitcroft } 4912cf655043SAndy Whitcroft if (statement_block_size($block) > 1) { 4913cf655043SAndy Whitcroft #print "APW: ALLOWED: lines block<$block>\n"; 4914cf655043SAndy Whitcroft $allowed = 1; 4915cf655043SAndy Whitcroft } 4916cf655043SAndy Whitcroft # Check the post-context. 4917cf655043SAndy Whitcroft if (defined $chunks[1]) { 4918cf655043SAndy Whitcroft my ($cond, $block) = @{$chunks[1]}; 4919cf655043SAndy Whitcroft if (defined $cond) { 4920773647a0SAndy Whitcroft substr($block, 0, length($cond), ''); 4921cf655043SAndy Whitcroft } 4922cf655043SAndy Whitcroft if ($block =~ /^\s*\{/) { 4923cf655043SAndy Whitcroft #print "APW: ALLOWED: chunk-1 block<$block>\n"; 4924cf655043SAndy Whitcroft $allowed = 1; 4925cf655043SAndy Whitcroft } 4926cf655043SAndy Whitcroft } 4927cf655043SAndy Whitcroft if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) { 492869932487SJustin P. Mattock my $herectx = $here . "\n"; 4929f055663cSAndy Whitcroft my $cnt = statement_rawlines($block); 4930cf655043SAndy Whitcroft 4931f055663cSAndy Whitcroft for (my $n = 0; $n < $cnt; $n++) { 493269932487SJustin P. Mattock $herectx .= raw_line($linenr, $n) . "\n"; 4933cf655043SAndy Whitcroft } 4934cf655043SAndy Whitcroft 4935000d1cc1SJoe Perches WARN("BRACES", 4936000d1cc1SJoe Perches "braces {} are not necessary for single statement blocks\n" . $herectx); 4937f0a594c1SAndy Whitcroft } 4938f0a594c1SAndy Whitcroft } 4939f0a594c1SAndy Whitcroft 49400979ae66SJoe Perches# check for unnecessary blank lines around braces 494177b9a53aSJoe Perches if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) { 4942f8e58219SJoe Perches if (CHK("BRACES", 4943f8e58219SJoe Perches "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) && 4944f8e58219SJoe Perches $fix && $prevrawline =~ /^\+/) { 4945f8e58219SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 4946f8e58219SJoe Perches } 49470979ae66SJoe Perches } 494877b9a53aSJoe Perches if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) { 4949f8e58219SJoe Perches if (CHK("BRACES", 4950f8e58219SJoe Perches "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) && 4951f8e58219SJoe Perches $fix) { 4952f8e58219SJoe Perches fix_delete_line($fixlinenr, $rawline); 4953f8e58219SJoe Perches } 49540979ae66SJoe Perches } 49550979ae66SJoe Perches 49564a0df2efSAndy Whitcroft# no volatiles please 49576c72ffaaSAndy Whitcroft my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b}; 49586c72ffaaSAndy Whitcroft if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) { 4959000d1cc1SJoe Perches WARN("VOLATILE", 4960000d1cc1SJoe Perches "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr); 49614a0df2efSAndy Whitcroft } 49624a0df2efSAndy Whitcroft 49635e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability 49645e4f6ba5SJoe Perches# to grep for the string. Make exceptions when the previous string ends in a 49655e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{' 49665e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value 496733acb54aSJoe Perches if ($line =~ /^\+\s*$String/ && 49685e4f6ba5SJoe Perches $prevline =~ /"\s*$/ && 49695e4f6ba5SJoe Perches $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) { 49705e4f6ba5SJoe Perches if (WARN("SPLIT_STRING", 49715e4f6ba5SJoe Perches "quoted string split across lines\n" . $hereprev) && 49725e4f6ba5SJoe Perches $fix && 49735e4f6ba5SJoe Perches $prevrawline =~ /^\+.*"\s*$/ && 49745e4f6ba5SJoe Perches $last_coalesced_string_linenr != $linenr - 1) { 49755e4f6ba5SJoe Perches my $extracted_string = get_quoted_string($line, $rawline); 49765e4f6ba5SJoe Perches my $comma_close = ""; 49775e4f6ba5SJoe Perches if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) { 49785e4f6ba5SJoe Perches $comma_close = $1; 49795e4f6ba5SJoe Perches } 49805e4f6ba5SJoe Perches 49815e4f6ba5SJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 49825e4f6ba5SJoe Perches fix_delete_line($fixlinenr, $rawline); 49835e4f6ba5SJoe Perches my $fixedline = $prevrawline; 49845e4f6ba5SJoe Perches $fixedline =~ s/"\s*$//; 49855e4f6ba5SJoe Perches $fixedline .= substr($extracted_string, 1) . trim($comma_close); 49865e4f6ba5SJoe Perches fix_insert_line($fixlinenr - 1, $fixedline); 49875e4f6ba5SJoe Perches $fixedline = $rawline; 49885e4f6ba5SJoe Perches $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//; 49895e4f6ba5SJoe Perches if ($fixedline !~ /\+\s*$/) { 49905e4f6ba5SJoe Perches fix_insert_line($fixlinenr, $fixedline); 49915e4f6ba5SJoe Perches } 49925e4f6ba5SJoe Perches $last_coalesced_string_linenr = $linenr; 49935e4f6ba5SJoe Perches } 49945e4f6ba5SJoe Perches } 49955e4f6ba5SJoe Perches 49965e4f6ba5SJoe Perches# check for missing a space in a string concatenation 49975e4f6ba5SJoe Perches if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) { 49985e4f6ba5SJoe Perches WARN('MISSING_SPACE', 49995e4f6ba5SJoe Perches "break quoted strings at a space character\n" . $hereprev); 50005e4f6ba5SJoe Perches } 50015e4f6ba5SJoe Perches 50025e4f6ba5SJoe Perches# check for spaces before a quoted newline 50035e4f6ba5SJoe Perches if ($rawline =~ /^.*\".*\s\\n/) { 50045e4f6ba5SJoe Perches if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE", 50055e4f6ba5SJoe Perches "unnecessary whitespace before a quoted newline\n" . $herecurr) && 50065e4f6ba5SJoe Perches $fix) { 50075e4f6ba5SJoe Perches $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/; 50085e4f6ba5SJoe Perches } 50095e4f6ba5SJoe Perches 50105e4f6ba5SJoe Perches } 50115e4f6ba5SJoe Perches 5012f17dba4fSJoe Perches# concatenated string without spaces between elements 501333acb54aSJoe Perches if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) { 5014f17dba4fSJoe Perches CHK("CONCATENATED_STRING", 5015f17dba4fSJoe Perches "Concatenated strings should use spaces between elements\n" . $herecurr); 5016f17dba4fSJoe Perches } 5017f17dba4fSJoe Perches 501890ad30e5SJoe Perches# uncoalesced string fragments 501933acb54aSJoe Perches if ($line =~ /$String\s*"/) { 502090ad30e5SJoe Perches WARN("STRING_FRAGMENTS", 502190ad30e5SJoe Perches "Consecutive strings are generally better as a single string\n" . $herecurr); 502290ad30e5SJoe Perches } 502390ad30e5SJoe Perches 50246e300757SJoe Perches# check for %L{u,d,i} and 0x%[udi] in strings 50255e4f6ba5SJoe Perches my $string; 50265e4f6ba5SJoe Perches while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) { 50275e4f6ba5SJoe Perches $string = substr($rawline, $-[1], $+[1] - $-[1]); 50285e4f6ba5SJoe Perches $string =~ s/%%/__/g; 50296e300757SJoe Perches if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) { 50305e4f6ba5SJoe Perches WARN("PRINTF_L", 50315e4f6ba5SJoe Perches "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr); 50325e4f6ba5SJoe Perches last; 50335e4f6ba5SJoe Perches } 50346e300757SJoe Perches if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) { 50356e300757SJoe Perches ERROR("PRINTF_0xDECIMAL", 50366e300757SJoe Perches "Prefixing 0x with decimal output is defective\n" . $herecurr); 50376e300757SJoe Perches } 50385e4f6ba5SJoe Perches } 50395e4f6ba5SJoe Perches 50405e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of " 50415e4f6ba5SJoe Perches if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) { 50425e4f6ba5SJoe Perches WARN("LINE_CONTINUATIONS", 50435e4f6ba5SJoe Perches "Avoid line continuations in quoted strings\n" . $herecurr); 50445e4f6ba5SJoe Perches } 50455e4f6ba5SJoe Perches 504600df344fSAndy Whitcroft# warn about #if 0 5047c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*if\s+0\b/) { 5048000d1cc1SJoe Perches CHK("REDUNDANT_CODE", 5049000d1cc1SJoe Perches "if this code is redundant consider removing it\n" . 5050de7d4f0eSAndy Whitcroft $herecurr); 50514a0df2efSAndy Whitcroft } 50524a0df2efSAndy Whitcroft 505303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses 505403df4b51SAndy Whitcroft if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) { 5055100425deSJoe Perches my $tested = quotemeta($1); 5056100425deSJoe Perches my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;'; 5057100425deSJoe Perches if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) { 5058100425deSJoe Perches my $func = $1; 5059100425deSJoe Perches if (WARN('NEEDLESS_IF', 5060100425deSJoe Perches "$func(NULL) is safe and this check is probably not required\n" . $hereprev) && 5061100425deSJoe Perches $fix) { 5062100425deSJoe Perches my $do_fix = 1; 5063100425deSJoe Perches my $leading_tabs = ""; 5064100425deSJoe Perches my $new_leading_tabs = ""; 5065100425deSJoe Perches if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) { 5066100425deSJoe Perches $leading_tabs = $1; 5067100425deSJoe Perches } else { 5068100425deSJoe Perches $do_fix = 0; 5069100425deSJoe Perches } 5070100425deSJoe Perches if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) { 5071100425deSJoe Perches $new_leading_tabs = $1; 5072100425deSJoe Perches if (length($leading_tabs) + 1 ne length($new_leading_tabs)) { 5073100425deSJoe Perches $do_fix = 0; 5074100425deSJoe Perches } 5075100425deSJoe Perches } else { 5076100425deSJoe Perches $do_fix = 0; 5077100425deSJoe Perches } 5078100425deSJoe Perches if ($do_fix) { 5079100425deSJoe Perches fix_delete_line($fixlinenr - 1, $prevrawline); 5080100425deSJoe Perches $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/; 5081100425deSJoe Perches } 5082100425deSJoe Perches } 50834c432a8fSGreg Kroah-Hartman } 50844c432a8fSGreg Kroah-Hartman } 5085f0a594c1SAndy Whitcroft 5086ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages 5087ebfdc409SJoe Perches if ($line =~ /^\+.*\b$logFunctions\s*\(/ && 5088ebfdc409SJoe Perches $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ && 5089ebfdc409SJoe Perches (defined $1 || defined $3) && 5090ebfdc409SJoe Perches $linenr > 3) { 5091ebfdc409SJoe Perches my $testval = $2; 5092ebfdc409SJoe Perches my $testline = $lines[$linenr - 3]; 5093ebfdc409SJoe Perches 5094ebfdc409SJoe Perches my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0); 5095ebfdc409SJoe Perches# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n"); 5096ebfdc409SJoe Perches 5097ebfdc409SJoe 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)/) { 5098ebfdc409SJoe Perches WARN("OOM_MESSAGE", 5099ebfdc409SJoe Perches "Possible unnecessary 'out of memory' message\n" . $hereprev); 5100ebfdc409SJoe Perches } 5101ebfdc409SJoe Perches } 5102ebfdc409SJoe Perches 5103f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL> 5104dcaf1123SPaolo Bonzini if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ && 5105f78d98f6SJoe Perches $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) { 5106f78d98f6SJoe Perches my $level = $1; 5107f78d98f6SJoe Perches if (WARN("UNNECESSARY_KERN_LEVEL", 5108f78d98f6SJoe Perches "Possible unnecessary $level\n" . $herecurr) && 5109f78d98f6SJoe Perches $fix) { 5110f78d98f6SJoe Perches $fixed[$fixlinenr] =~ s/\s*$level\s*//; 5111f78d98f6SJoe Perches } 5112f78d98f6SJoe Perches } 5113f78d98f6SJoe Perches 5114abb08a53SJoe Perches# check for mask then right shift without a parentheses 5115abb08a53SJoe Perches if ($^V && $^V ge 5.10.0 && 5116abb08a53SJoe Perches $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ && 5117abb08a53SJoe Perches $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so 5118abb08a53SJoe Perches WARN("MASK_THEN_SHIFT", 5119abb08a53SJoe Perches "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr); 5120abb08a53SJoe Perches } 5121abb08a53SJoe Perches 5122b75ac618SJoe Perches# check for pointer comparisons to NULL 5123b75ac618SJoe Perches if ($^V && $^V ge 5.10.0) { 5124b75ac618SJoe Perches while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) { 5125b75ac618SJoe Perches my $val = $1; 5126b75ac618SJoe Perches my $equal = "!"; 5127b75ac618SJoe Perches $equal = "" if ($4 eq "!="); 5128b75ac618SJoe Perches if (CHK("COMPARISON_TO_NULL", 5129b75ac618SJoe Perches "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) && 5130b75ac618SJoe Perches $fix) { 5131b75ac618SJoe Perches $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/; 5132b75ac618SJoe Perches } 5133b75ac618SJoe Perches } 5134b75ac618SJoe Perches } 5135b75ac618SJoe Perches 51368716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata) 51378716de38SJoe Perches if ($line =~ /(\b$InitAttribute\b)/) { 51388716de38SJoe Perches my $attr = $1; 51398716de38SJoe Perches if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) { 51408716de38SJoe Perches my $ptr = $1; 51418716de38SJoe Perches my $var = $2; 51428716de38SJoe Perches if ((($ptr =~ /\b(union|struct)\s+$attr\b/ && 51438716de38SJoe Perches ERROR("MISPLACED_INIT", 51448716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr)) || 51458716de38SJoe Perches ($ptr !~ /\b(union|struct)\s+$attr\b/ && 51468716de38SJoe Perches WARN("MISPLACED_INIT", 51478716de38SJoe Perches "$attr should be placed after $var\n" . $herecurr))) && 51488716de38SJoe Perches $fix) { 5149194f66fcSJoe 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; 51508716de38SJoe Perches } 51518716de38SJoe Perches } 51528716de38SJoe Perches } 51538716de38SJoe Perches 5154e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const 5155e970b884SJoe Perches if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) { 5156e970b884SJoe Perches my $attr = $1; 5157e970b884SJoe Perches $attr =~ /($InitAttributePrefix)(.*)/; 5158e970b884SJoe Perches my $attr_prefix = $1; 5159e970b884SJoe Perches my $attr_type = $2; 5160e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5161e970b884SJoe Perches "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) && 5162e970b884SJoe Perches $fix) { 5163194f66fcSJoe Perches $fixed[$fixlinenr] =~ 5164e970b884SJoe Perches s/$InitAttributeData/${attr_prefix}initconst/; 5165e970b884SJoe Perches } 5166e970b884SJoe Perches } 5167e970b884SJoe Perches 5168e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const 5169e970b884SJoe Perches if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) { 5170e970b884SJoe Perches my $attr = $1; 5171e970b884SJoe Perches if (ERROR("INIT_ATTRIBUTE", 5172e970b884SJoe Perches "Use of $attr requires a separate use of const\n" . $herecurr) && 5173e970b884SJoe Perches $fix) { 5174194f66fcSJoe Perches my $lead = $fixed[$fixlinenr] =~ 5175e970b884SJoe Perches /(^\+\s*(?:static\s+))/; 5176e970b884SJoe Perches $lead = rtrim($1); 5177e970b884SJoe Perches $lead = "$lead " if ($lead !~ /^\+$/); 5178e970b884SJoe Perches $lead = "${lead}const "; 5179194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/; 5180e970b884SJoe Perches } 5181e970b884SJoe Perches } 5182e970b884SJoe Perches 5183c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const) 5184c17893c7SJoe Perches if ($line =~ /\b__read_mostly\b/ && 5185c17893c7SJoe Perches $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) { 5186c17893c7SJoe Perches if (ERROR("CONST_READ_MOSTLY", 5187c17893c7SJoe Perches "Invalid use of __read_mostly with const type\n" . $herecurr) && 5188c17893c7SJoe Perches $fix) { 5189c17893c7SJoe Perches $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//; 5190c17893c7SJoe Perches } 5191c17893c7SJoe Perches } 5192c17893c7SJoe Perches 5193fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/ 5194fbdb8138SJoe Perches if ($realfile !~ m@^include/uapi/@ && 5195fbdb8138SJoe Perches $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) { 5196fbdb8138SJoe Perches my $constant_func = $1; 5197fbdb8138SJoe Perches my $func = $constant_func; 5198fbdb8138SJoe Perches $func =~ s/^__constant_//; 5199fbdb8138SJoe Perches if (WARN("CONSTANT_CONVERSION", 5200fbdb8138SJoe Perches "$constant_func should be $func\n" . $herecurr) && 5201fbdb8138SJoe Perches $fix) { 5202194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g; 5203fbdb8138SJoe Perches } 5204fbdb8138SJoe Perches } 5205fbdb8138SJoe Perches 52061a15a250SPatrick Pannuto# prefer usleep_range over udelay 520737581c28SBruce Allan if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) { 520843c1d77cSJoe Perches my $delay = $1; 52091a15a250SPatrick Pannuto # ignore udelay's < 10, however 521043c1d77cSJoe Perches if (! ($delay < 10) ) { 5211000d1cc1SJoe Perches CHK("USLEEP_RANGE", 521243c1d77cSJoe Perches "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr); 521343c1d77cSJoe Perches } 521443c1d77cSJoe Perches if ($delay > 2000) { 521543c1d77cSJoe Perches WARN("LONG_UDELAY", 521643c1d77cSJoe Perches "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr); 52171a15a250SPatrick Pannuto } 52181a15a250SPatrick Pannuto } 52191a15a250SPatrick Pannuto 522009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's 522109ef8725SPatrick Pannuto if ($line =~ /\bmsleep\s*\((\d+)\);/) { 522209ef8725SPatrick Pannuto if ($1 < 20) { 5223000d1cc1SJoe Perches WARN("MSLEEP", 522443c1d77cSJoe Perches "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr); 522509ef8725SPatrick Pannuto } 522609ef8725SPatrick Pannuto } 522709ef8725SPatrick Pannuto 522836ec1939SJoe Perches# check for comparisons of jiffies 522936ec1939SJoe Perches if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) { 523036ec1939SJoe Perches WARN("JIFFIES_COMPARISON", 523136ec1939SJoe Perches "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr); 523236ec1939SJoe Perches } 523336ec1939SJoe Perches 52349d7a34a5SJoe Perches# check for comparisons of get_jiffies_64() 52359d7a34a5SJoe Perches if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) { 52369d7a34a5SJoe Perches WARN("JIFFIES_COMPARISON", 52379d7a34a5SJoe Perches "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr); 52389d7a34a5SJoe Perches } 52399d7a34a5SJoe Perches 524000df344fSAndy Whitcroft# warn about #ifdefs in C files 5241c45dcabdSAndy Whitcroft# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) { 524200df344fSAndy Whitcroft# print "#ifdef in C files should be avoided\n"; 524300df344fSAndy Whitcroft# print "$herecurr"; 524400df344fSAndy Whitcroft# $clean = 0; 524500df344fSAndy Whitcroft# } 524600df344fSAndy Whitcroft 524722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs 5248c45dcabdSAndy Whitcroft if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) { 52493705ce5bSJoe Perches if (ERROR("SPACING", 52503705ce5bSJoe Perches "exactly one space required after that #$1\n" . $herecurr) && 52513705ce5bSJoe Perches $fix) { 5252194f66fcSJoe Perches $fixed[$fixlinenr] =~ 52533705ce5bSJoe Perches s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /; 52543705ce5bSJoe Perches } 52553705ce5bSJoe Perches 525622f2a2efSAndy Whitcroft } 525722f2a2efSAndy Whitcroft 52584a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment. 5259171ae1a4SAndy Whitcroft if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ || 5260171ae1a4SAndy Whitcroft $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) { 52614a0df2efSAndy Whitcroft my $which = $1; 52624a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5263000d1cc1SJoe Perches CHK("UNCOMMENTED_DEFINITION", 5264000d1cc1SJoe Perches "$1 definition without comment\n" . $herecurr); 52654a0df2efSAndy Whitcroft } 52664a0df2efSAndy Whitcroft } 52674a0df2efSAndy Whitcroft# check for memory barriers without a comment. 5268402c2553SMichael S. Tsirkin 5269402c2553SMichael S. Tsirkin my $barriers = qr{ 5270402c2553SMichael S. Tsirkin mb| 5271402c2553SMichael S. Tsirkin rmb| 5272402c2553SMichael S. Tsirkin wmb| 5273402c2553SMichael S. Tsirkin read_barrier_depends 5274402c2553SMichael S. Tsirkin }x; 5275402c2553SMichael S. Tsirkin my $barrier_stems = qr{ 5276402c2553SMichael S. Tsirkin mb__before_atomic| 5277402c2553SMichael S. Tsirkin mb__after_atomic| 5278402c2553SMichael S. Tsirkin store_release| 5279402c2553SMichael S. Tsirkin load_acquire| 5280402c2553SMichael S. Tsirkin store_mb| 5281402c2553SMichael S. Tsirkin (?:$barriers) 5282402c2553SMichael S. Tsirkin }x; 5283402c2553SMichael S. Tsirkin my $all_barriers = qr{ 5284402c2553SMichael S. Tsirkin (?:$barriers)| 528543e361f2SMichael S. Tsirkin smp_(?:$barrier_stems)| 528643e361f2SMichael S. Tsirkin virt_(?:$barrier_stems) 5287402c2553SMichael S. Tsirkin }x; 5288402c2553SMichael S. Tsirkin 5289402c2553SMichael S. Tsirkin if ($line =~ /\b(?:$all_barriers)\s*\(/) { 52904a0df2efSAndy Whitcroft if (!ctx_has_comment($first_line, $linenr)) { 5291c1fd7bb9SJoe Perches WARN("MEMORY_BARRIER", 5292000d1cc1SJoe Perches "memory barrier without comment\n" . $herecurr); 52934a0df2efSAndy Whitcroft } 52944a0df2efSAndy Whitcroft } 52953ad81779SPaul E. McKenney 5296f4073b0fSMichael S. Tsirkin my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x; 5297f4073b0fSMichael S. Tsirkin 5298f4073b0fSMichael S. Tsirkin if ($realfile !~ m@^include/asm-generic/@ && 5299f4073b0fSMichael S. Tsirkin $realfile !~ m@/barrier\.h$@ && 5300f4073b0fSMichael S. Tsirkin $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ && 5301f4073b0fSMichael S. Tsirkin $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) { 5302f4073b0fSMichael S. Tsirkin WARN("MEMORY_BARRIER", 5303f4073b0fSMichael S. Tsirkin "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr); 5304f4073b0fSMichael S. Tsirkin } 5305f4073b0fSMichael S. Tsirkin 5306cb426e99SJoe Perches# check for waitqueue_active without a comment. 5307cb426e99SJoe Perches if ($line =~ /\bwaitqueue_active\s*\(/) { 5308cb426e99SJoe Perches if (!ctx_has_comment($first_line, $linenr)) { 5309cb426e99SJoe Perches WARN("WAITQUEUE_ACTIVE", 5310cb426e99SJoe Perches "waitqueue_active without comment\n" . $herecurr); 5311cb426e99SJoe Perches } 5312cb426e99SJoe Perches } 53133ad81779SPaul E. McKenney 53143ad81779SPaul E. McKenney# Check for expedited grace periods that interrupt non-idle non-nohz 53153ad81779SPaul E. McKenney# online CPUs. These expedited can therefore degrade real-time response 53163ad81779SPaul E. McKenney# if used carelessly, and should be avoided where not absolutely 53173ad81779SPaul E. McKenney# needed. It is always OK to use synchronize_rcu_expedited() and 53183ad81779SPaul E. McKenney# synchronize_sched_expedited() at boot time (before real-time applications 53193ad81779SPaul E. McKenney# start) and in error situations where real-time response is compromised in 53203ad81779SPaul E. McKenney# any case. Note that synchronize_srcu_expedited() does -not- interrupt 53213ad81779SPaul E. McKenney# other CPUs, so don't warn on uses of synchronize_srcu_expedited(). 53223ad81779SPaul E. McKenney# Of course, nothing comes for free, and srcu_read_lock() and 53233ad81779SPaul E. McKenney# srcu_read_unlock() do contain full memory barriers in payment for 53243ad81779SPaul E. McKenney# synchronize_srcu_expedited() non-interruption properties. 53253ad81779SPaul E. McKenney if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) { 53263ad81779SPaul E. McKenney WARN("EXPEDITED_RCU_GRACE_PERIOD", 53273ad81779SPaul E. McKenney "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr); 53283ad81779SPaul E. McKenney 53293ad81779SPaul E. McKenney } 53303ad81779SPaul E. McKenney 53314a0df2efSAndy Whitcroft# check of hardware specific defines 5332c45dcabdSAndy Whitcroft if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) { 5333000d1cc1SJoe Perches CHK("ARCH_DEFINES", 5334000d1cc1SJoe Perches "architecture specific defines should be avoided\n" . $herecurr); 53350a920b5bSAndy Whitcroft } 5336653d4876SAndy Whitcroft 5337d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration 5338d4977c78STobias Klauser if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) { 5339000d1cc1SJoe Perches WARN("STORAGE_CLASS", 5340000d1cc1SJoe Perches "storage class should be at the beginning of the declaration\n" . $herecurr) 5341d4977c78STobias Klauser } 5342d4977c78STobias Klauser 5343de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between 5344de7d4f0eSAndy Whitcroft# storage class and type. 53459c0ca6f9SAndy Whitcroft if ($line =~ /\b$Type\s+$Inline\b/ || 53469c0ca6f9SAndy Whitcroft $line =~ /\b$Inline\s+$Storage\b/) { 5347000d1cc1SJoe Perches ERROR("INLINE_LOCATION", 5348000d1cc1SJoe Perches "inline keyword should sit between storage class and type\n" . $herecurr); 5349de7d4f0eSAndy Whitcroft } 5350de7d4f0eSAndy Whitcroft 53518905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline 53522b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 53532b7ab453SJoe Perches $line =~ /\b(__inline__|__inline)\b/) { 5354d5e616fcSJoe Perches if (WARN("INLINE", 5355d5e616fcSJoe Perches "plain inline is preferred over $1\n" . $herecurr) && 5356d5e616fcSJoe Perches $fix) { 5357194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/; 5358d5e616fcSJoe Perches 5359d5e616fcSJoe Perches } 53608905a67cSAndy Whitcroft } 53618905a67cSAndy Whitcroft 53623d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed 53632b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 53642b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) { 5365000d1cc1SJoe Perches WARN("PREFER_PACKED", 5366000d1cc1SJoe Perches "__packed is preferred over __attribute__((packed))\n" . $herecurr); 53673d130fd0SJoe Perches } 53683d130fd0SJoe Perches 536939b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned 53702b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 53712b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) { 5372000d1cc1SJoe Perches WARN("PREFER_ALIGNED", 5373000d1cc1SJoe Perches "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr); 537439b7e287SJoe Perches } 537539b7e287SJoe Perches 53765f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf 53772b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 53782b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) { 5379d5e616fcSJoe Perches if (WARN("PREFER_PRINTF", 5380d5e616fcSJoe Perches "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) && 5381d5e616fcSJoe Perches $fix) { 5382194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex; 5383d5e616fcSJoe Perches 5384d5e616fcSJoe Perches } 53855f14d3bdSJoe Perches } 53865f14d3bdSJoe Perches 53876061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf 53882b7ab453SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 53892b7ab453SJoe Perches $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) { 5390d5e616fcSJoe Perches if (WARN("PREFER_SCANF", 5391d5e616fcSJoe Perches "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) && 5392d5e616fcSJoe Perches $fix) { 5393194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex; 5394d5e616fcSJoe Perches } 53956061d949SJoe Perches } 53966061d949SJoe Perches 5397619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues) 5398619a908aSJoe Perches if ($^V && $^V ge 5.10.0 && 5399619a908aSJoe Perches $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ && 5400619a908aSJoe Perches ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ || 5401619a908aSJoe Perches $line =~ /\b__weak\b/)) { 5402619a908aSJoe Perches ERROR("WEAK_DECLARATION", 5403619a908aSJoe Perches "Using weak declarations can have unintended link defects\n" . $herecurr); 5404619a908aSJoe Perches } 5405619a908aSJoe Perches 5406e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/ 5407e6176fa4SJoe Perches if ($realfile !~ m@\binclude/uapi/@ && 5408e6176fa4SJoe Perches $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) { 5409e6176fa4SJoe Perches my $type = $1; 5410e6176fa4SJoe Perches if ($type =~ /\b($typeC99Typedefs)\b/) { 5411e6176fa4SJoe Perches $type = $1; 5412e6176fa4SJoe Perches my $kernel_type = 'u'; 5413e6176fa4SJoe Perches $kernel_type = 's' if ($type =~ /^_*[si]/); 5414e6176fa4SJoe Perches $type =~ /(\d+)/; 5415e6176fa4SJoe Perches $kernel_type .= $1; 5416e6176fa4SJoe Perches if (CHK("PREFER_KERNEL_TYPES", 5417e6176fa4SJoe Perches "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) && 5418e6176fa4SJoe Perches $fix) { 5419e6176fa4SJoe Perches $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/; 5420e6176fa4SJoe Perches } 5421e6176fa4SJoe Perches } 5422e6176fa4SJoe Perches } 5423e6176fa4SJoe Perches 5424938224b5SJoe Perches# check for cast of C90 native int or longer types constants 5425938224b5SJoe Perches if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) { 5426938224b5SJoe Perches my $cast = $1; 5427938224b5SJoe Perches my $const = $2; 5428938224b5SJoe Perches if (WARN("TYPECAST_INT_CONSTANT", 5429938224b5SJoe Perches "Unnecessary typecast of c90 int constant\n" . $herecurr) && 5430938224b5SJoe Perches $fix) { 5431938224b5SJoe Perches my $suffix = ""; 5432938224b5SJoe Perches my $newconst = $const; 5433938224b5SJoe Perches $newconst =~ s/${Int_type}$//; 5434938224b5SJoe Perches $suffix .= 'U' if ($cast =~ /\bunsigned\b/); 5435938224b5SJoe Perches if ($cast =~ /\blong\s+long\b/) { 5436938224b5SJoe Perches $suffix .= 'LL'; 5437938224b5SJoe Perches } elsif ($cast =~ /\blong\b/) { 5438938224b5SJoe Perches $suffix .= 'L'; 5439938224b5SJoe Perches } 5440938224b5SJoe Perches $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/; 5441938224b5SJoe Perches } 5442938224b5SJoe Perches } 5443938224b5SJoe Perches 54448f53a9b8SJoe Perches# check for sizeof(&) 54458f53a9b8SJoe Perches if ($line =~ /\bsizeof\s*\(\s*\&/) { 5446000d1cc1SJoe Perches WARN("SIZEOF_ADDRESS", 5447000d1cc1SJoe Perches "sizeof(& should be avoided\n" . $herecurr); 54488f53a9b8SJoe Perches } 54498f53a9b8SJoe Perches 545066c80b60SJoe Perches# check for sizeof without parenthesis 545166c80b60SJoe Perches if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) { 5452d5e616fcSJoe Perches if (WARN("SIZEOF_PARENTHESIS", 5453d5e616fcSJoe Perches "sizeof $1 should be sizeof($1)\n" . $herecurr) && 5454d5e616fcSJoe Perches $fix) { 5455194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex; 5456d5e616fcSJoe Perches } 545766c80b60SJoe Perches } 545866c80b60SJoe Perches 545988982feaSJoe Perches# check for struct spinlock declarations 546088982feaSJoe Perches if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) { 546188982feaSJoe Perches WARN("USE_SPINLOCK_T", 546288982feaSJoe Perches "struct spinlock should be spinlock_t\n" . $herecurr); 546388982feaSJoe Perches } 546488982feaSJoe Perches 5465a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts 546606668727SJoe Perches if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) { 5467a6962d72SJoe Perches my $fmt = get_quoted_string($line, $rawline); 5468caac1d5fSHeba Aamer $fmt =~ s/%%//g; 5469caac1d5fSHeba Aamer if ($fmt !~ /%/) { 5470d5e616fcSJoe Perches if (WARN("PREFER_SEQ_PUTS", 5471d5e616fcSJoe Perches "Prefer seq_puts to seq_printf\n" . $herecurr) && 5472d5e616fcSJoe Perches $fix) { 5473194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/; 5474d5e616fcSJoe Perches } 5475a6962d72SJoe Perches } 5476a6962d72SJoe Perches } 5477a6962d72SJoe Perches 5478554e165cSAndy Whitcroft# Check for misused memsets 5479d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 5480d1fe9c09SJoe Perches defined $stat && 54819e20a853SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) { 5482554e165cSAndy Whitcroft 5483d7c76ba7SJoe Perches my $ms_addr = $2; 5484d1fe9c09SJoe Perches my $ms_val = $7; 5485d1fe9c09SJoe Perches my $ms_size = $12; 5486d7c76ba7SJoe Perches 5487554e165cSAndy Whitcroft if ($ms_size =~ /^(0x|)0$/i) { 5488554e165cSAndy Whitcroft ERROR("MEMSET", 5489d7c76ba7SJoe Perches "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n"); 5490554e165cSAndy Whitcroft } elsif ($ms_size =~ /^(0x|)1$/i) { 5491554e165cSAndy Whitcroft WARN("MEMSET", 5492d7c76ba7SJoe Perches "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n"); 5493d7c76ba7SJoe Perches } 5494d7c76ba7SJoe Perches } 5495d7c76ba7SJoe Perches 549698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar) 549798a9bba5SJoe Perches if ($^V && $^V ge 5.10.0 && 549810895d2cSMateusz Kulikowski defined $stat && 549910895d2cSMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 550098a9bba5SJoe Perches if (WARN("PREFER_ETHER_ADDR_COPY", 550110895d2cSMateusz Kulikowski "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") && 550298a9bba5SJoe Perches $fix) { 5503194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/; 550498a9bba5SJoe Perches } 550598a9bba5SJoe Perches } 550698a9bba5SJoe Perches 5507b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar) 5508b6117d17SMateusz Kulikowski if ($^V && $^V ge 5.10.0 && 5509b6117d17SMateusz Kulikowski defined $stat && 5510b6117d17SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 5511b6117d17SMateusz Kulikowski WARN("PREFER_ETHER_ADDR_EQUAL", 5512b6117d17SMateusz Kulikowski "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n") 5513b6117d17SMateusz Kulikowski } 5514b6117d17SMateusz Kulikowski 55158617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr 55168617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr 55178617cd09SMateusz Kulikowski if ($^V && $^V ge 5.10.0 && 55188617cd09SMateusz Kulikowski defined $stat && 55198617cd09SMateusz Kulikowski $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) { 55208617cd09SMateusz Kulikowski 55218617cd09SMateusz Kulikowski my $ms_val = $7; 55228617cd09SMateusz Kulikowski 55238617cd09SMateusz Kulikowski if ($ms_val =~ /^(?:0x|)0+$/i) { 55248617cd09SMateusz Kulikowski if (WARN("PREFER_ETH_ZERO_ADDR", 55258617cd09SMateusz Kulikowski "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") && 55268617cd09SMateusz Kulikowski $fix) { 55278617cd09SMateusz Kulikowski $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/; 55288617cd09SMateusz Kulikowski } 55298617cd09SMateusz Kulikowski } elsif ($ms_val =~ /^(?:0xff|255)$/i) { 55308617cd09SMateusz Kulikowski if (WARN("PREFER_ETH_BROADCAST_ADDR", 55318617cd09SMateusz Kulikowski "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") && 55328617cd09SMateusz Kulikowski $fix) { 55338617cd09SMateusz Kulikowski $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/; 55348617cd09SMateusz Kulikowski } 55358617cd09SMateusz Kulikowski } 55368617cd09SMateusz Kulikowski } 55378617cd09SMateusz Kulikowski 5538d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t 5539d1fe9c09SJoe Perches if ($^V && $^V ge 5.10.0 && 5540d1fe9c09SJoe Perches defined $stat && 5541d7c76ba7SJoe Perches $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) { 5542d1fe9c09SJoe Perches if (defined $2 || defined $7) { 5543d7c76ba7SJoe Perches my $call = $1; 5544d7c76ba7SJoe Perches my $cast1 = deparenthesize($2); 5545d7c76ba7SJoe Perches my $arg1 = $3; 5546d1fe9c09SJoe Perches my $cast2 = deparenthesize($7); 5547d1fe9c09SJoe Perches my $arg2 = $8; 5548d7c76ba7SJoe Perches my $cast; 5549d7c76ba7SJoe Perches 5550d1fe9c09SJoe Perches if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) { 5551d7c76ba7SJoe Perches $cast = "$cast1 or $cast2"; 5552d7c76ba7SJoe Perches } elsif ($cast1 ne "") { 5553d7c76ba7SJoe Perches $cast = $cast1; 5554d7c76ba7SJoe Perches } else { 5555d7c76ba7SJoe Perches $cast = $cast2; 5556d7c76ba7SJoe Perches } 5557d7c76ba7SJoe Perches WARN("MINMAX", 5558d7c76ba7SJoe Perches "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n"); 5559554e165cSAndy Whitcroft } 5560554e165cSAndy Whitcroft } 5561554e165cSAndy Whitcroft 55624a273195SJoe Perches# check usleep_range arguments 55634a273195SJoe Perches if ($^V && $^V ge 5.10.0 && 55644a273195SJoe Perches defined $stat && 55654a273195SJoe Perches $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) { 55664a273195SJoe Perches my $min = $1; 55674a273195SJoe Perches my $max = $7; 55684a273195SJoe Perches if ($min eq $max) { 55694a273195SJoe Perches WARN("USLEEP_RANGE", 55704a273195SJoe Perches "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 55714a273195SJoe Perches } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ && 55724a273195SJoe Perches $min > $max) { 55734a273195SJoe Perches WARN("USLEEP_RANGE", 55744a273195SJoe Perches "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n"); 55754a273195SJoe Perches } 55764a273195SJoe Perches } 55774a273195SJoe Perches 5578823b794cSJoe Perches# check for naked sscanf 5579823b794cSJoe Perches if ($^V && $^V ge 5.10.0 && 5580823b794cSJoe Perches defined $stat && 55816c8bd707SJoe Perches $line =~ /\bsscanf\b/ && 5582823b794cSJoe Perches ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ && 5583823b794cSJoe Perches $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ && 5584823b794cSJoe Perches $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) { 5585823b794cSJoe Perches my $lc = $stat =~ tr@\n@@; 5586823b794cSJoe Perches $lc = $lc + $linenr; 5587823b794cSJoe Perches my $stat_real = raw_line($linenr, 0); 5588823b794cSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 5589823b794cSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 5590823b794cSJoe Perches } 5591823b794cSJoe Perches WARN("NAKED_SSCANF", 5592823b794cSJoe Perches "unchecked sscanf return value\n" . "$here\n$stat_real\n"); 5593823b794cSJoe Perches } 5594823b794cSJoe Perches 5595afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo> 5596afc819abSJoe Perches if ($^V && $^V ge 5.10.0 && 5597afc819abSJoe Perches defined $stat && 5598afc819abSJoe Perches $line =~ /\bsscanf\b/) { 5599afc819abSJoe Perches my $lc = $stat =~ tr@\n@@; 5600afc819abSJoe Perches $lc = $lc + $linenr; 5601afc819abSJoe Perches my $stat_real = raw_line($linenr, 0); 5602afc819abSJoe Perches for (my $count = $linenr + 1; $count <= $lc; $count++) { 5603afc819abSJoe Perches $stat_real = $stat_real . "\n" . raw_line($count, 0); 5604afc819abSJoe Perches } 5605afc819abSJoe Perches if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) { 5606afc819abSJoe Perches my $format = $6; 5607afc819abSJoe Perches my $count = $format =~ tr@%@%@; 5608afc819abSJoe Perches if ($count == 1 && 5609afc819abSJoe Perches $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) { 5610afc819abSJoe Perches WARN("SSCANF_TO_KSTRTO", 5611afc819abSJoe Perches "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n"); 5612afc819abSJoe Perches } 5613afc819abSJoe Perches } 5614afc819abSJoe Perches } 5615afc819abSJoe Perches 561670dc8a48SJoe Perches# check for new externs in .h files. 561770dc8a48SJoe Perches if ($realfile =~ /\.h$/ && 561870dc8a48SJoe Perches $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) { 5619d1d85780SJoe Perches if (CHK("AVOID_EXTERNS", 562070dc8a48SJoe Perches "extern prototypes should be avoided in .h files\n" . $herecurr) && 562170dc8a48SJoe Perches $fix) { 5622194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/; 562370dc8a48SJoe Perches } 562470dc8a48SJoe Perches } 562570dc8a48SJoe Perches 5626de7d4f0eSAndy Whitcroft# check for new externs in .c files. 5627171ae1a4SAndy Whitcroft if ($realfile =~ /\.c$/ && defined $stat && 5628c45dcabdSAndy Whitcroft $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s) 5629171ae1a4SAndy Whitcroft { 5630c45dcabdSAndy Whitcroft my $function_name = $1; 5631c45dcabdSAndy Whitcroft my $paren_space = $2; 5632171ae1a4SAndy Whitcroft 5633171ae1a4SAndy Whitcroft my $s = $stat; 5634171ae1a4SAndy Whitcroft if (defined $cond) { 5635171ae1a4SAndy Whitcroft substr($s, 0, length($cond), ''); 5636171ae1a4SAndy Whitcroft } 5637c45dcabdSAndy Whitcroft if ($s =~ /^\s*;/ && 5638c45dcabdSAndy Whitcroft $function_name ne 'uninitialized_var') 5639c45dcabdSAndy Whitcroft { 5640000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 5641000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 5642de7d4f0eSAndy Whitcroft } 5643de7d4f0eSAndy Whitcroft 5644171ae1a4SAndy Whitcroft if ($paren_space =~ /\n/) { 5645000d1cc1SJoe Perches WARN("FUNCTION_ARGUMENTS", 5646000d1cc1SJoe Perches "arguments for function declarations should follow identifier\n" . $herecurr); 5647171ae1a4SAndy Whitcroft } 56489c9ba34eSAndy Whitcroft 56499c9ba34eSAndy Whitcroft } elsif ($realfile =~ /\.c$/ && defined $stat && 56509c9ba34eSAndy Whitcroft $stat =~ /^.\s*extern\s+/) 56519c9ba34eSAndy Whitcroft { 5652000d1cc1SJoe Perches WARN("AVOID_EXTERNS", 5653000d1cc1SJoe Perches "externs should be avoided in .c files\n" . $herecurr); 5654171ae1a4SAndy Whitcroft } 5655171ae1a4SAndy Whitcroft 5656de7d4f0eSAndy Whitcroft# checks for new __setup's 5657de7d4f0eSAndy Whitcroft if ($rawline =~ /\b__setup\("([^"]*)"/) { 5658de7d4f0eSAndy Whitcroft my $name = $1; 5659de7d4f0eSAndy Whitcroft 5660de7d4f0eSAndy Whitcroft if (!grep(/$name/, @setup_docs)) { 5661000d1cc1SJoe Perches CHK("UNDOCUMENTED_SETUP", 5662000d1cc1SJoe Perches "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr); 5663de7d4f0eSAndy Whitcroft } 5664653d4876SAndy Whitcroft } 56659c0ca6f9SAndy Whitcroft 56669c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return 5667caf2a54fSJoe Perches if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) { 5668000d1cc1SJoe Perches WARN("UNNECESSARY_CASTS", 5669000d1cc1SJoe Perches "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr); 56709c0ca6f9SAndy Whitcroft } 567113214adfSAndy Whitcroft 5672a640d25cSJoe Perches# alloc style 5673a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...) 5674a640d25cSJoe Perches if ($^V && $^V ge 5.10.0 && 5675a640d25cSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) { 5676a640d25cSJoe Perches CHK("ALLOC_SIZEOF_STRUCT", 5677a640d25cSJoe Perches "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr); 5678a640d25cSJoe Perches } 5679a640d25cSJoe Perches 568060a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc 568160a55369SJoe Perches if ($^V && $^V ge 5.10.0 && 5682e367455aSJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) { 568360a55369SJoe Perches my $oldfunc = $3; 568460a55369SJoe Perches my $a1 = $4; 568560a55369SJoe Perches my $a2 = $10; 568660a55369SJoe Perches my $newfunc = "kmalloc_array"; 568760a55369SJoe Perches $newfunc = "kcalloc" if ($oldfunc eq "kzalloc"); 568860a55369SJoe Perches my $r1 = $a1; 568960a55369SJoe Perches my $r2 = $a2; 569060a55369SJoe Perches if ($a1 =~ /^sizeof\s*\S/) { 569160a55369SJoe Perches $r1 = $a2; 569260a55369SJoe Perches $r2 = $a1; 569360a55369SJoe Perches } 5694e367455aSJoe Perches if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ && 5695e367455aSJoe Perches !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) { 5696e367455aSJoe Perches if (WARN("ALLOC_WITH_MULTIPLY", 5697e367455aSJoe Perches "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) && 5698e367455aSJoe Perches $fix) { 5699194f66fcSJoe 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; 570060a55369SJoe Perches 570160a55369SJoe Perches } 570260a55369SJoe Perches } 570360a55369SJoe Perches } 570460a55369SJoe Perches 5705972fdea2SJoe Perches# check for krealloc arg reuse 5706972fdea2SJoe Perches if ($^V && $^V ge 5.10.0 && 5707972fdea2SJoe Perches $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) { 5708972fdea2SJoe Perches WARN("KREALLOC_ARG_REUSE", 5709972fdea2SJoe Perches "Reusing the krealloc arg is almost always a bug\n" . $herecurr); 5710972fdea2SJoe Perches } 5711972fdea2SJoe Perches 57125ce59ae0SJoe Perches# check for alloc argument mismatch 57135ce59ae0SJoe Perches if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) { 57145ce59ae0SJoe Perches WARN("ALLOC_ARRAY_ARGS", 57155ce59ae0SJoe Perches "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr); 57165ce59ae0SJoe Perches } 57175ce59ae0SJoe Perches 5718caf2a54fSJoe Perches# check for multiple semicolons 5719caf2a54fSJoe Perches if ($line =~ /;\s*;\s*$/) { 5720d5e616fcSJoe Perches if (WARN("ONE_SEMICOLON", 5721d5e616fcSJoe Perches "Statements terminations use 1 semicolon\n" . $herecurr) && 5722d5e616fcSJoe Perches $fix) { 5723194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g; 5724d5e616fcSJoe Perches } 5725d1e2ad07SJoe Perches } 5726d1e2ad07SJoe Perches 57270ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit) 57280ab90191SJoe Perches if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) { 57290ab90191SJoe Perches my $ull = ""; 57300ab90191SJoe Perches $ull = "_ULL" if (defined($1) && $1 =~ /ll/i); 57310ab90191SJoe Perches if (CHK("BIT_MACRO", 57320ab90191SJoe Perches "Prefer using the BIT$ull macro\n" . $herecurr) && 57330ab90191SJoe Perches $fix) { 57340ab90191SJoe Perches $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/; 57350ab90191SJoe Perches } 57360ab90191SJoe Perches } 57370ab90191SJoe Perches 57382d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE 57392d632745SJoe Perches if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) { 57402d632745SJoe Perches my $config = $1; 57412d632745SJoe Perches if (WARN("PREFER_IS_ENABLED", 57422d632745SJoe Perches "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) && 57432d632745SJoe Perches $fix) { 57442d632745SJoe Perches $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)"; 57452d632745SJoe Perches } 57462d632745SJoe Perches } 57472d632745SJoe Perches 5748e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch 5749c34c09a8SJoe Perches if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) { 5750c34c09a8SJoe Perches my $has_break = 0; 5751c34c09a8SJoe Perches my $has_statement = 0; 5752c34c09a8SJoe Perches my $count = 0; 5753c34c09a8SJoe Perches my $prevline = $linenr; 5754e81f239bSJoe Perches while ($prevline > 1 && ($file || $count < 3) && !$has_break) { 5755c34c09a8SJoe Perches $prevline--; 5756c34c09a8SJoe Perches my $rline = $rawlines[$prevline - 1]; 5757c34c09a8SJoe Perches my $fline = $lines[$prevline - 1]; 5758c34c09a8SJoe Perches last if ($fline =~ /^\@\@/); 5759c34c09a8SJoe Perches next if ($fline =~ /^\-/); 5760c34c09a8SJoe Perches next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/); 5761c34c09a8SJoe Perches $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i); 5762c34c09a8SJoe Perches next if ($fline =~ /^.[\s$;]*$/); 5763c34c09a8SJoe Perches $has_statement = 1; 5764c34c09a8SJoe Perches $count++; 5765c34c09a8SJoe Perches $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/); 5766c34c09a8SJoe Perches } 5767c34c09a8SJoe Perches if (!$has_break && $has_statement) { 5768c34c09a8SJoe Perches WARN("MISSING_BREAK", 5769c34c09a8SJoe Perches "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr); 5770c34c09a8SJoe Perches } 5771c34c09a8SJoe Perches } 5772c34c09a8SJoe Perches 5773d1e2ad07SJoe Perches# check for switch/default statements without a break; 5774d1e2ad07SJoe Perches if ($^V && $^V ge 5.10.0 && 5775d1e2ad07SJoe Perches defined $stat && 5776d1e2ad07SJoe Perches $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) { 5777d1e2ad07SJoe Perches my $ctx = ''; 5778d1e2ad07SJoe Perches my $herectx = $here . "\n"; 5779d1e2ad07SJoe Perches my $cnt = statement_rawlines($stat); 5780d1e2ad07SJoe Perches for (my $n = 0; $n < $cnt; $n++) { 5781d1e2ad07SJoe Perches $herectx .= raw_line($linenr, $n) . "\n"; 5782d1e2ad07SJoe Perches } 5783d1e2ad07SJoe Perches WARN("DEFAULT_NO_BREAK", 5784d1e2ad07SJoe Perches "switch default: should use break\n" . $herectx); 5785caf2a54fSJoe Perches } 5786caf2a54fSJoe Perches 578713214adfSAndy Whitcroft# check for gcc specific __FUNCTION__ 5788d5e616fcSJoe Perches if ($line =~ /\b__FUNCTION__\b/) { 5789d5e616fcSJoe Perches if (WARN("USE_FUNC", 5790d5e616fcSJoe Perches "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) && 5791d5e616fcSJoe Perches $fix) { 5792194f66fcSJoe Perches $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g; 5793d5e616fcSJoe Perches } 579413214adfSAndy Whitcroft } 5795773647a0SAndy Whitcroft 579662ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__ 579762ec818fSJoe Perches while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) { 579862ec818fSJoe Perches ERROR("DATE_TIME", 579962ec818fSJoe Perches "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr); 580062ec818fSJoe Perches } 580162ec818fSJoe Perches 58022c92488aSJoe Perches# check for use of yield() 58032c92488aSJoe Perches if ($line =~ /\byield\s*\(\s*\)/) { 58042c92488aSJoe Perches WARN("YIELD", 58052c92488aSJoe Perches "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr); 58062c92488aSJoe Perches } 58072c92488aSJoe Perches 5808179f8f40SJoe Perches# check for comparisons against true and false 5809179f8f40SJoe Perches if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) { 5810179f8f40SJoe Perches my $lead = $1; 5811179f8f40SJoe Perches my $arg = $2; 5812179f8f40SJoe Perches my $test = $3; 5813179f8f40SJoe Perches my $otype = $4; 5814179f8f40SJoe Perches my $trail = $5; 5815179f8f40SJoe Perches my $op = "!"; 5816179f8f40SJoe Perches 5817179f8f40SJoe Perches ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i); 5818179f8f40SJoe Perches 5819179f8f40SJoe Perches my $type = lc($otype); 5820179f8f40SJoe Perches if ($type =~ /^(?:true|false)$/) { 5821179f8f40SJoe Perches if (("$test" eq "==" && "$type" eq "true") || 5822179f8f40SJoe Perches ("$test" eq "!=" && "$type" eq "false")) { 5823179f8f40SJoe Perches $op = ""; 5824179f8f40SJoe Perches } 5825179f8f40SJoe Perches 5826179f8f40SJoe Perches CHK("BOOL_COMPARISON", 5827179f8f40SJoe Perches "Using comparison to $otype is error prone\n" . $herecurr); 5828179f8f40SJoe Perches 5829179f8f40SJoe Perches## maybe suggesting a correct construct would better 5830179f8f40SJoe Perches## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr); 5831179f8f40SJoe Perches 5832179f8f40SJoe Perches } 5833179f8f40SJoe Perches } 5834179f8f40SJoe Perches 58354882720bSThomas Gleixner# check for semaphores initialized locked 58364882720bSThomas Gleixner if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) { 5837000d1cc1SJoe Perches WARN("CONSIDER_COMPLETION", 5838000d1cc1SJoe Perches "consider using a completion\n" . $herecurr); 5839773647a0SAndy Whitcroft } 58406712d858SJoe Perches 584167d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto* 584267d0a075SJoe Perches if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) { 5843000d1cc1SJoe Perches WARN("CONSIDER_KSTRTO", 584467d0a075SJoe Perches "$1 is obsolete, use k$3 instead\n" . $herecurr); 5845773647a0SAndy Whitcroft } 58466712d858SJoe Perches 5847ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please 5848f3db6639SMichael Ellerman if ($line =~ /^.\s*__initcall\s*\(/) { 5849000d1cc1SJoe Perches WARN("USE_DEVICE_INITCALL", 5850ae3ccc46SFabian Frederick "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr); 5851f3db6639SMichael Ellerman } 58526712d858SJoe Perches 58530f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree) 58540f3c5aabSJoe Perches my $const_structs = qr{ 58550f3c5aabSJoe Perches acpi_dock_ops| 585679404849SEmese Revfy address_space_operations| 585779404849SEmese Revfy backlight_ops| 585879404849SEmese Revfy block_device_operations| 585979404849SEmese Revfy dentry_operations| 586079404849SEmese Revfy dev_pm_ops| 586179404849SEmese Revfy dma_map_ops| 586279404849SEmese Revfy extent_io_ops| 586379404849SEmese Revfy file_lock_operations| 586479404849SEmese Revfy file_operations| 586579404849SEmese Revfy hv_ops| 586679404849SEmese Revfy ide_dma_ops| 586779404849SEmese Revfy intel_dvo_dev_ops| 586879404849SEmese Revfy item_operations| 586979404849SEmese Revfy iwl_ops| 587079404849SEmese Revfy kgdb_arch| 587179404849SEmese Revfy kgdb_io| 587279404849SEmese Revfy kset_uevent_ops| 587379404849SEmese Revfy lock_manager_operations| 587479404849SEmese Revfy microcode_ops| 587579404849SEmese Revfy mtrr_ops| 587679404849SEmese Revfy neigh_ops| 587779404849SEmese Revfy nlmsvc_binding| 58780f3c5aabSJoe Perches of_device_id| 587979404849SEmese Revfy pci_raw_ops| 588079404849SEmese Revfy pipe_buf_operations| 588179404849SEmese Revfy platform_hibernation_ops| 588279404849SEmese Revfy platform_suspend_ops| 588379404849SEmese Revfy proto_ops| 588479404849SEmese Revfy rpc_pipe_ops| 588579404849SEmese Revfy seq_operations| 588679404849SEmese Revfy snd_ac97_build_ops| 588779404849SEmese Revfy soc_pcmcia_socket_ops| 588879404849SEmese Revfy stacktrace_ops| 588979404849SEmese Revfy sysfs_ops| 589079404849SEmese Revfy tty_operations| 58916d07d01bSJoe Perches uart_ops| 589279404849SEmese Revfy usb_mon_operations| 589379404849SEmese Revfy wd_ops}x; 58946903ffb2SAndy Whitcroft if ($line !~ /\bconst\b/ && 58950f3c5aabSJoe Perches $line =~ /\bstruct\s+($const_structs)\b/) { 5896000d1cc1SJoe Perches WARN("CONST_STRUCT", 5897000d1cc1SJoe Perches "struct $1 should normally be const\n" . 58986903ffb2SAndy Whitcroft $herecurr); 58992b6db5cbSAndy Whitcroft } 5900773647a0SAndy Whitcroft 5901773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong 5902773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right 5903773647a0SAndy Whitcroft if ($line =~ /\bNR_CPUS\b/ && 5904c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ && 5905c45dcabdSAndy Whitcroft $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ && 5906171ae1a4SAndy Whitcroft $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ && 5907171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ && 5908171ae1a4SAndy Whitcroft $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/) 5909773647a0SAndy Whitcroft { 5910000d1cc1SJoe Perches WARN("NR_CPUS", 5911000d1cc1SJoe Perches "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr); 5912773647a0SAndy Whitcroft } 59139c9ba34eSAndy Whitcroft 591452ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong. 591552ea8506SJoe Perches if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) { 591652ea8506SJoe Perches ERROR("DEFINE_ARCH_HAS", 591752ea8506SJoe Perches "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr); 591852ea8506SJoe Perches } 591952ea8506SJoe Perches 5920acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)" 5921acd9362cSJoe Perches if ($^V && $^V ge 5.10.0 && 5922acd9362cSJoe Perches $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) { 5923acd9362cSJoe Perches WARN("LIKELY_MISUSE", 5924acd9362cSJoe Perches "Using $1 should generally have parentheses around the comparison\n" . $herecurr); 5925acd9362cSJoe Perches } 5926acd9362cSJoe Perches 5927691d77b6SAndy Whitcroft# whine mightly about in_atomic 5928691d77b6SAndy Whitcroft if ($line =~ /\bin_atomic\s*\(/) { 5929691d77b6SAndy Whitcroft if ($realfile =~ m@^drivers/@) { 5930000d1cc1SJoe Perches ERROR("IN_ATOMIC", 5931000d1cc1SJoe Perches "do not use in_atomic in drivers\n" . $herecurr); 5932f4a87736SAndy Whitcroft } elsif ($realfile !~ m@^kernel/@) { 5933000d1cc1SJoe Perches WARN("IN_ATOMIC", 5934000d1cc1SJoe Perches "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr); 5935691d77b6SAndy Whitcroft } 5936691d77b6SAndy Whitcroft } 59371704f47bSPeter Zijlstra 5938481aea5cSJoe Perches# whine about ACCESS_ONCE 5939481aea5cSJoe Perches if ($^V && $^V ge 5.10.0 && 5940481aea5cSJoe Perches $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) { 5941481aea5cSJoe Perches my $par = $1; 5942481aea5cSJoe Perches my $eq = $2; 5943481aea5cSJoe Perches my $fun = $3; 5944481aea5cSJoe Perches $par =~ s/^\(\s*(.*)\s*\)$/$1/; 5945481aea5cSJoe Perches if (defined($eq)) { 5946481aea5cSJoe Perches if (WARN("PREFER_WRITE_ONCE", 5947481aea5cSJoe Perches "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) && 5948481aea5cSJoe Perches $fix) { 5949481aea5cSJoe Perches $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/; 5950481aea5cSJoe Perches } 5951481aea5cSJoe Perches } else { 5952481aea5cSJoe Perches if (WARN("PREFER_READ_ONCE", 5953481aea5cSJoe Perches "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) && 5954481aea5cSJoe Perches $fix) { 5955481aea5cSJoe Perches $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/; 5956481aea5cSJoe Perches } 5957481aea5cSJoe Perches } 5958481aea5cSJoe Perches } 5959481aea5cSJoe Perches 59601704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class 59611704f47bSPeter Zijlstra if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ || 59621704f47bSPeter Zijlstra $line =~ /__lockdep_no_validate__\s*\)/ ) { 59631704f47bSPeter Zijlstra if ($realfile !~ m@^kernel/lockdep@ && 59641704f47bSPeter Zijlstra $realfile !~ m@^include/linux/lockdep@ && 59651704f47bSPeter Zijlstra $realfile !~ m@^drivers/base/core@) { 5966000d1cc1SJoe Perches ERROR("LOCKDEP", 5967000d1cc1SJoe Perches "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr); 59681704f47bSPeter Zijlstra } 59691704f47bSPeter Zijlstra } 597088f8831cSDave Jones 5971b392c64fSJoe Perches if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ || 5972b392c64fSJoe Perches $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) { 5973000d1cc1SJoe Perches WARN("EXPORTED_WORLD_WRITABLE", 5974000d1cc1SJoe Perches "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 597588f8831cSDave Jones } 59762435880fSJoe Perches 5977515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal 5978515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop 5979515a235eSJoe Perches if ($^V && $^V ge 5.10.0 && 5980515a235eSJoe Perches $line =~ /$mode_perms_search/) { 59812435880fSJoe Perches foreach my $entry (@mode_permission_funcs) { 59822435880fSJoe Perches my $func = $entry->[0]; 59832435880fSJoe Perches my $arg_pos = $entry->[1]; 59842435880fSJoe Perches 59852435880fSJoe Perches my $skip_args = ""; 59862435880fSJoe Perches if ($arg_pos > 1) { 59872435880fSJoe Perches $arg_pos--; 59882435880fSJoe Perches $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}"; 59892435880fSJoe Perches } 59902435880fSJoe Perches my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]"; 5991515a235eSJoe Perches if ($line =~ /$test/) { 59922435880fSJoe Perches my $val = $1; 59932435880fSJoe Perches $val = $6 if ($skip_args ne ""); 59942435880fSJoe Perches 59951727cc70SJoe Perches if ($val !~ /^0$/ && 59961727cc70SJoe Perches (($val =~ /^$Int$/ && $val !~ /^$Octal$/) || 59971727cc70SJoe Perches length($val) ne 4)) { 59982435880fSJoe Perches ERROR("NON_OCTAL_PERMISSIONS", 59991727cc70SJoe Perches "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr); 6000c0a5c898SJoe Perches } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) { 6001c0a5c898SJoe Perches ERROR("EXPORTED_WORLD_WRITABLE", 6002c0a5c898SJoe Perches "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr); 60032435880fSJoe Perches } 60042435880fSJoe Perches } 60052435880fSJoe Perches } 600613214adfSAndy Whitcroft } 60075a6d20ceSBjorn Andersson 60085a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h 60095a6d20ceSBjorn Andersson if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) { 60105a6d20ceSBjorn Andersson my $extracted_string = get_quoted_string($line, $rawline); 60115a6d20ceSBjorn Andersson my $valid_licenses = qr{ 60125a6d20ceSBjorn Andersson GPL| 60135a6d20ceSBjorn Andersson GPL\ v2| 60145a6d20ceSBjorn Andersson GPL\ and\ additional\ rights| 60155a6d20ceSBjorn Andersson Dual\ BSD/GPL| 60165a6d20ceSBjorn Andersson Dual\ MIT/GPL| 60175a6d20ceSBjorn Andersson Dual\ MPL/GPL| 60185a6d20ceSBjorn Andersson Proprietary 60195a6d20ceSBjorn Andersson }x; 60205a6d20ceSBjorn Andersson if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) { 60215a6d20ceSBjorn Andersson WARN("MODULE_LICENSE", 60225a6d20ceSBjorn Andersson "unknown module license " . $extracted_string . "\n" . $herecurr); 60235a6d20ceSBjorn Andersson } 60245a6d20ceSBjorn Andersson } 6025515a235eSJoe Perches } 602613214adfSAndy Whitcroft 602713214adfSAndy Whitcroft # If we have no input at all, then there is nothing to report on 602813214adfSAndy Whitcroft # so just keep quiet. 602913214adfSAndy Whitcroft if ($#rawlines == -1) { 603013214adfSAndy Whitcroft exit(0); 60310a920b5bSAndy Whitcroft } 60320a920b5bSAndy Whitcroft 60338905a67cSAndy Whitcroft # In mailback mode only produce a report in the negative, for 60348905a67cSAndy Whitcroft # things that appear to be patches. 60358905a67cSAndy Whitcroft if ($mailback && ($clean == 1 || !$is_patch)) { 60368905a67cSAndy Whitcroft exit(0); 60378905a67cSAndy Whitcroft } 60388905a67cSAndy Whitcroft 60398905a67cSAndy Whitcroft # This is not a patch, and we are are in 'no-patch' mode so 60408905a67cSAndy Whitcroft # just keep quiet. 60418905a67cSAndy Whitcroft if (!$chk_patch && !$is_patch) { 60428905a67cSAndy Whitcroft exit(0); 60438905a67cSAndy Whitcroft } 60448905a67cSAndy Whitcroft 604506330fc4SJoe Perches if (!$is_patch && $file !~ /cover-letter\.patch$/) { 6046000d1cc1SJoe Perches ERROR("NOT_UNIFIED_DIFF", 6047000d1cc1SJoe Perches "Does not appear to be a unified-diff format patch\n"); 60480a920b5bSAndy Whitcroft } 604934d8815fSJoe Perches if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) { 6050000d1cc1SJoe Perches ERROR("MISSING_SIGN_OFF", 6051000d1cc1SJoe Perches "Missing Signed-off-by: line(s)\n"); 60520a920b5bSAndy Whitcroft } 60530a920b5bSAndy Whitcroft 6054f0a594c1SAndy Whitcroft print report_dump(); 605513214adfSAndy Whitcroft if ($summary && !($clean == 1 && $quiet == 1)) { 605613214adfSAndy Whitcroft print "$filename " if ($summary_file); 60576c72ffaaSAndy Whitcroft print "total: $cnt_error errors, $cnt_warn warnings, " . 60586c72ffaaSAndy Whitcroft (($check)? "$cnt_chk checks, " : "") . 60596c72ffaaSAndy Whitcroft "$cnt_lines lines checked\n"; 60606c72ffaaSAndy Whitcroft } 60618905a67cSAndy Whitcroft 6062d2c0a235SAndy Whitcroft if ($quiet == 0) { 6063ef212196SJoe Perches # If there were any defects found and not already fixing them 6064ef212196SJoe Perches if (!$clean and !$fix) { 6065ef212196SJoe Perches print << "EOM" 6066ef212196SJoe Perches 6067ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to 6068ef212196SJoe Perches mechanically convert to the typical style using --fix or --fix-inplace. 6069ef212196SJoe PerchesEOM 6070ef212196SJoe Perches } 6071d2c0a235SAndy Whitcroft # If there were whitespace errors which cleanpatch can fix 6072d2c0a235SAndy Whitcroft # then suggest that. 6073d2c0a235SAndy Whitcroft if ($rpt_cleaners) { 6074b0781216SMike Frysinger $rpt_cleaners = 0; 6075d8469f16SJoe Perches print << "EOM" 6076d8469f16SJoe Perches 6077d8469f16SJoe PerchesNOTE: Whitespace errors detected. 6078d8469f16SJoe Perches You may wish to use scripts/cleanpatch or scripts/cleanfile 6079d8469f16SJoe PerchesEOM 6080d2c0a235SAndy Whitcroft } 6081d2c0a235SAndy Whitcroft } 6082d2c0a235SAndy Whitcroft 6083d752fcc8SJoe Perches if ($clean == 0 && $fix && 6084d752fcc8SJoe Perches ("@rawlines" ne "@fixed" || 6085d752fcc8SJoe Perches $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) { 60869624b8d6SJoe Perches my $newfile = $filename; 60879624b8d6SJoe Perches $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace); 60883705ce5bSJoe Perches my $linecount = 0; 60893705ce5bSJoe Perches my $f; 60903705ce5bSJoe Perches 6091d752fcc8SJoe Perches @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted); 6092d752fcc8SJoe Perches 60933705ce5bSJoe Perches open($f, '>', $newfile) 60943705ce5bSJoe Perches or die "$P: Can't open $newfile for write\n"; 60953705ce5bSJoe Perches foreach my $fixed_line (@fixed) { 60963705ce5bSJoe Perches $linecount++; 60973705ce5bSJoe Perches if ($file) { 60983705ce5bSJoe Perches if ($linecount > 3) { 60993705ce5bSJoe Perches $fixed_line =~ s/^\+//; 61003705ce5bSJoe Perches print $f $fixed_line . "\n"; 61013705ce5bSJoe Perches } 61023705ce5bSJoe Perches } else { 61033705ce5bSJoe Perches print $f $fixed_line . "\n"; 61043705ce5bSJoe Perches } 61053705ce5bSJoe Perches } 61063705ce5bSJoe Perches close($f); 61073705ce5bSJoe Perches 61083705ce5bSJoe Perches if (!$quiet) { 61093705ce5bSJoe Perches print << "EOM"; 6110d8469f16SJoe Perches 61113705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile' 61123705ce5bSJoe Perches 61133705ce5bSJoe PerchesDo _NOT_ trust the results written to this file. 61143705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness. 61153705ce5bSJoe Perches 61163705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches. 61173705ce5bSJoe PerchesNo warranties, expressed or implied... 61183705ce5bSJoe PerchesEOM 61193705ce5bSJoe Perches } 61203705ce5bSJoe Perches } 61213705ce5bSJoe Perches 6122d8469f16SJoe Perches if ($quiet == 0) { 6123d8469f16SJoe Perches print "\n"; 6124d8469f16SJoe Perches if ($clean == 1) { 6125d8469f16SJoe Perches print "$vname has no obvious style problems and is ready for submission.\n"; 6126d8469f16SJoe Perches } else { 6127d8469f16SJoe Perches print "$vname has style problems, please review.\n"; 61280a920b5bSAndy Whitcroft } 61290a920b5bSAndy Whitcroft } 61300a920b5bSAndy Whitcroft return $clean; 61310a920b5bSAndy Whitcroft} 6132