xref: /linux-6.15/scripts/checkpatch.pl (revision ffbce897)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0
3882ea1d6SJoe Perches#
4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]>
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftuse strict;
11cb77f0d6SKamil Rytarowskiuse warnings;
12c707a81dSJoe Perchesuse POSIX;
1336061e38SJoe Perchesuse File::Basename;
1436061e38SJoe Perchesuse Cwd 'abs_path';
1557230297SJoe Perchesuse Term::ANSIColor qw(:constants);
16cd261496SGeert Uytterhoevenuse Encode qw(decode encode);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $P = $0;
1936061e38SJoe Perchesmy $D = dirname(abs_path($P));
200a920b5bSAndy Whitcroft
21000d1cc1SJoe Perchesmy $V = '0.32';
220a920b5bSAndy Whitcroft
230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
240a920b5bSAndy Whitcroft
250a920b5bSAndy Whitcroftmy $quiet = 0;
260a920b5bSAndy Whitcroftmy $tree = 1;
270a920b5bSAndy Whitcroftmy $chk_signoff = 1;
280a920b5bSAndy Whitcroftmy $chk_patch = 1;
29773647a0SAndy Whitcroftmy $tst_only;
306c72ffaaSAndy Whitcroftmy $emacs = 0;
318905a67cSAndy Whitcroftmy $terse = 0;
3234d8815fSJoe Perchesmy $showfile = 0;
336c72ffaaSAndy Whitcroftmy $file = 0;
344a593c34SDu, Changbinmy $git = 0;
350dea9f1eSJoe Perchesmy %git_commits = ();
366c72ffaaSAndy Whitcroftmy $check = 0;
372ac73b4fSJoe Perchesmy $check_orig = 0;
388905a67cSAndy Whitcroftmy $summary = 1;
398905a67cSAndy Whitcroftmy $mailback = 0;
4013214adfSAndy Whitcroftmy $summary_file = 0;
41000d1cc1SJoe Perchesmy $show_types = 0;
423beb42ecSJoe Perchesmy $list_types = 0;
433705ce5bSJoe Perchesmy $fix = 0;
449624b8d6SJoe Perchesmy $fix_inplace = 0;
456c72ffaaSAndy Whitcroftmy $root;
46c2fdda0dSAndy Whitcroftmy %debug;
473445686aSJoe Perchesmy %camelcase = ();
4891bfe484SJoe Perchesmy %use_type = ();
4991bfe484SJoe Perchesmy @use = ();
5091bfe484SJoe Perchesmy %ignore_type = ();
51000d1cc1SJoe Perchesmy @ignore = ();
5277f5b10aSHannes Edermy $help = 0;
53000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
546cd7f386SJoe Perchesmy $max_line_length = 80;
55d62a201fSDave Hansenmy $ignore_perl_version = 0;
56d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5756193274SVadim Bendeburymy $min_conf_desc_length = 4;
5866b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
59ebfd7d62SJoe Perchesmy $codespell = 0;
60f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
61bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
6275ad8c57SJerome Forissiermy $typedefsfile = "";
63737c0767SJohn Brooksmy $color = "auto";
6498005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
6577f5b10aSHannes Eder
6677f5b10aSHannes Edersub help {
6777f5b10aSHannes Eder	my ($exitcode) = @_;
6877f5b10aSHannes Eder
6977f5b10aSHannes Eder	print << "EOM";
7077f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7177f5b10aSHannes EderVersion: $V
7277f5b10aSHannes Eder
7377f5b10aSHannes EderOptions:
7477f5b10aSHannes Eder  -q, --quiet                quiet
7577f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7677f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
7777f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
7877f5b10aSHannes Eder  --emacs                    emacs compile window format
7977f5b10aSHannes Eder  --terse                    one line per report
8034d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
814a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
824a593c34SDu, Changbin                             single git commit with:
834a593c34SDu, Changbin                               <rev>
844a593c34SDu, Changbin                               <rev>^
854a593c34SDu, Changbin                               <rev>~n
864a593c34SDu, Changbin                             multiple git commits with:
874a593c34SDu, Changbin                               <rev1>..<rev2>
884a593c34SDu, Changbin                               <rev1>...<rev2>
894a593c34SDu, Changbin                               <rev>-<count>
904a593c34SDu, Changbin                             git merges are ignored
9177f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9277f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
933beb42ecSJoe Perches  --list-types               list the possible message types
9491bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
95000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
963beb42ecSJoe Perches  --show-types               show the specific message type in the output
976cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
9856193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
9977f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10077f5b10aSHannes Eder  --no-summary               suppress the per-file summary
10177f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
10277f5b10aSHannes Eder  --summary-file             include the filename in summary
10377f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
10477f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
10577f5b10aSHannes Eder                             is all off)
10677f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
10777f5b10aSHannes Eder                             literally
1083705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1093705ce5bSJoe Perches                             If correctable single-line errors exist, create
1103705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1113705ce5bSJoe Perches                             with potential errors corrected to the preferred
1123705ce5bSJoe Perches                             checkpatch style
1139624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1149624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1159624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
116d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
117d62a201fSDave Hansen                             runtime errors.
118ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
119f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
120ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
12175ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
122737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
123737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
12477f5b10aSHannes Eder  -h, --help, --version      display this help and exit
12577f5b10aSHannes Eder
12677f5b10aSHannes EderWhen FILE is - read standard input.
12777f5b10aSHannes EderEOM
12877f5b10aSHannes Eder
12977f5b10aSHannes Eder	exit($exitcode);
13077f5b10aSHannes Eder}
13177f5b10aSHannes Eder
1323beb42ecSJoe Perchessub uniq {
1333beb42ecSJoe Perches	my %seen;
1343beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1353beb42ecSJoe Perches}
1363beb42ecSJoe Perches
1373beb42ecSJoe Perchessub list_types {
1383beb42ecSJoe Perches	my ($exitcode) = @_;
1393beb42ecSJoe Perches
1403beb42ecSJoe Perches	my $count = 0;
1413beb42ecSJoe Perches
1423beb42ecSJoe Perches	local $/ = undef;
1433beb42ecSJoe Perches
1443beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1453beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1463beb42ecSJoe Perches
1473beb42ecSJoe Perches	my $text = <$script>;
1483beb42ecSJoe Perches	close($script);
1493beb42ecSJoe Perches
1503beb42ecSJoe Perches	my @types = ();
1510547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1520547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1533beb42ecSJoe Perches		push (@types, $_);
1543beb42ecSJoe Perches	}
1553beb42ecSJoe Perches	@types = sort(uniq(@types));
1563beb42ecSJoe Perches	print("#\tMessage type\n\n");
1573beb42ecSJoe Perches	foreach my $type (@types) {
1583beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1593beb42ecSJoe Perches	}
1603beb42ecSJoe Perches
1613beb42ecSJoe Perches	exit($exitcode);
1623beb42ecSJoe Perches}
1633beb42ecSJoe Perches
164000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
165000d1cc1SJoe Perchesif (-f $conf) {
166000d1cc1SJoe Perches	my @conf_args;
167000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
168000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
169000d1cc1SJoe Perches
170000d1cc1SJoe Perches	while (<$conffile>) {
171000d1cc1SJoe Perches		my $line = $_;
172000d1cc1SJoe Perches
173000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
174000d1cc1SJoe Perches		$line =~ s/^\s*//g;
175000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
176000d1cc1SJoe Perches
177000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
178000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
179000d1cc1SJoe Perches
180000d1cc1SJoe Perches		my @words = split(" ", $line);
181000d1cc1SJoe Perches		foreach my $word (@words) {
182000d1cc1SJoe Perches			last if ($word =~ m/^#/);
183000d1cc1SJoe Perches			push (@conf_args, $word);
184000d1cc1SJoe Perches		}
185000d1cc1SJoe Perches	}
186000d1cc1SJoe Perches	close($conffile);
187000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
188000d1cc1SJoe Perches}
189000d1cc1SJoe Perches
190737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
191737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
192737c0767SJohn Brooksforeach (@ARGV) {
193737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
194737c0767SJohn Brooks		$_ = "--color=$color";
195737c0767SJohn Brooks	}
196737c0767SJohn Brooks}
197737c0767SJohn Brooks
1980a920b5bSAndy WhitcroftGetOptions(
1996c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2000a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2010a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2020a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2036c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2048905a67cSAndy Whitcroft	'terse!'	=> \$terse,
20534d8815fSJoe Perches	'showfile!'	=> \$showfile,
20677f5b10aSHannes Eder	'f|file!'	=> \$file,
2074a593c34SDu, Changbin	'g|git!'	=> \$git,
2086c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2096c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
210000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
21191bfe484SJoe Perches	'types=s'	=> \@use,
212000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2133beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2146cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
21556193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
2166c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2178905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2188905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
21913214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2203705ce5bSJoe Perches	'fix!'		=> \$fix,
2219624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
222d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
223c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
224773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
225ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
226ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
22775ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
228737c0767SJohn Brooks	'color=s'	=> \$color,
229737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
230737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
23177f5b10aSHannes Eder	'h|help'	=> \$help,
23277f5b10aSHannes Eder	'version'	=> \$help
23377f5b10aSHannes Eder) or help(1);
23477f5b10aSHannes Eder
23577f5b10aSHannes Ederhelp(0) if ($help);
2360a920b5bSAndy Whitcroft
2373beb42ecSJoe Percheslist_types(0) if ($list_types);
2383beb42ecSJoe Perches
2399624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2402ac73b4fSJoe Perches$check_orig = $check;
2419624b8d6SJoe Perches
2420a920b5bSAndy Whitcroftmy $exit = 0;
2430a920b5bSAndy Whitcroft
2445b57980dSJoe Perchesmy $perl_version_ok = 1;
245d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2465b57980dSJoe Perches	$perl_version_ok = 0;
247d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2485b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
249d62a201fSDave Hansen}
250d62a201fSDave Hansen
25145107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2520a920b5bSAndy Whitcroftif ($#ARGV < 0) {
25345107ff6SAllen Hubbe	push(@ARGV, '-');
2540a920b5bSAndy Whitcroft}
2550a920b5bSAndy Whitcroft
256737c0767SJohn Brooksif ($color =~ /^[01]$/) {
257737c0767SJohn Brooks	$color = !$color;
258737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
259737c0767SJohn Brooks	$color = 1;
260737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
261737c0767SJohn Brooks	$color = 0;
262737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
263737c0767SJohn Brooks	$color = (-t STDOUT);
264737c0767SJohn Brooks} else {
265737c0767SJohn Brooks	die "Invalid color mode: $color\n";
266737c0767SJohn Brooks}
267737c0767SJohn Brooks
26891bfe484SJoe Perchessub hash_save_array_words {
26991bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
27091bfe484SJoe Perches
27191bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
27291bfe484SJoe Perches	foreach my $word (@array) {
273000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
274000d1cc1SJoe Perches		$word =~ s/^\s*//g;
275000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
276000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
277000d1cc1SJoe Perches
278000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
279000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
280000d1cc1SJoe Perches
28191bfe484SJoe Perches		$hashRef->{$word}++;
282000d1cc1SJoe Perches	}
28391bfe484SJoe Perches}
28491bfe484SJoe Perches
28591bfe484SJoe Perchessub hash_show_words {
28691bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
28791bfe484SJoe Perches
2883c816e49SJoe Perches	if (keys %$hashRef) {
289d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
29058cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
29191bfe484SJoe Perches			print " $word";
29291bfe484SJoe Perches		}
293d8469f16SJoe Perches		print "\n";
29491bfe484SJoe Perches	}
29591bfe484SJoe Perches}
29691bfe484SJoe Perches
29791bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
29891bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
299000d1cc1SJoe Perches
300c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
301c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3027429c690SAndy Whitcroftmy $dbg_type = 0;
303a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
304c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
30521caa13cSAndy Whitcroft	## no critic
30621caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
30721caa13cSAndy Whitcroft	die "$@" if ($@);
308c2fdda0dSAndy Whitcroft}
309c2fdda0dSAndy Whitcroft
310d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
311d2c0a235SAndy Whitcroft
3128905a67cSAndy Whitcroftif ($terse) {
3138905a67cSAndy Whitcroft	$emacs = 1;
3148905a67cSAndy Whitcroft	$quiet++;
3158905a67cSAndy Whitcroft}
3168905a67cSAndy Whitcroft
3176c72ffaaSAndy Whitcroftif ($tree) {
3186c72ffaaSAndy Whitcroft	if (defined $root) {
3196c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3206c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3216c72ffaaSAndy Whitcroft		}
3226c72ffaaSAndy Whitcroft	} else {
3236c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3246c72ffaaSAndy Whitcroft			$root = '.';
3256c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3266c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3276c72ffaaSAndy Whitcroft			$root = $1;
3286c72ffaaSAndy Whitcroft		}
3296c72ffaaSAndy Whitcroft	}
3306c72ffaaSAndy Whitcroft
3316c72ffaaSAndy Whitcroft	if (!defined $root) {
3320a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3330a920b5bSAndy Whitcroft		exit(2);
3340a920b5bSAndy Whitcroft	}
3356c72ffaaSAndy Whitcroft}
3366c72ffaaSAndy Whitcroft
3376c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3386c72ffaaSAndy Whitcroft
3392ceb532bSAndy Whitcroftour $Ident	= qr{
3402ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3412ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3422ceb532bSAndy Whitcroft		}x;
3436c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3446c72ffaaSAndy Whitcroftour $Sparse	= qr{
3456c72ffaaSAndy Whitcroft			__user|
3466c72ffaaSAndy Whitcroft			__kernel|
3476c72ffaaSAndy Whitcroft			__force|
3486c72ffaaSAndy Whitcroft			__iomem|
3496c72ffaaSAndy Whitcroft			__must_check|
350417495edSAndy Whitcroft			__kprobes|
351165e72a6SSven Eckelmann			__ref|
35233aa4597SGeert Uytterhoeven			__refconst|
35333aa4597SGeert Uytterhoeven			__refdata|
354ad315455SBoqun Feng			__rcu|
355ad315455SBoqun Feng			__private
3566c72ffaaSAndy Whitcroft		}x;
357e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
358e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
359e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
360e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
361e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3628716de38SJoe Perches
36352131292SWolfram Sang# Notes to $Attribute:
36452131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3656c72ffaaSAndy Whitcroftour $Attribute	= qr{
3666c72ffaaSAndy Whitcroft			const|
36703f1df7dSJoe Perches			__percpu|
36803f1df7dSJoe Perches			__nocast|
36903f1df7dSJoe Perches			__safe|
37046d832f5SMichael S. Tsirkin			__bitwise|
37103f1df7dSJoe Perches			__packed__|
37203f1df7dSJoe Perches			__packed2__|
37303f1df7dSJoe Perches			__naked|
37403f1df7dSJoe Perches			__maybe_unused|
37503f1df7dSJoe Perches			__always_unused|
37603f1df7dSJoe Perches			__noreturn|
37703f1df7dSJoe Perches			__used|
37803f1df7dSJoe Perches			__cold|
379e23ef1f3SJoe Perches			__pure|
38003f1df7dSJoe Perches			__noclone|
38103f1df7dSJoe Perches			__deprecated|
3826c72ffaaSAndy Whitcroft			__read_mostly|
383c5967e98SJoe Perches			__ro_after_init|
3846c72ffaaSAndy Whitcroft			__kprobes|
3858716de38SJoe Perches			$InitAttribute|
38624e1d81aSAndy Whitcroft			____cacheline_aligned|
38724e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3885fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3895fe3af11SAndy Whitcroft			__weak
3906c72ffaaSAndy Whitcroft		  }x;
391c45dcabdSAndy Whitcroftour $Modifier;
39291cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3936c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3946c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3956c72ffaaSAndy Whitcroft
39695e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
39795e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
39895e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
39995e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4002435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
401c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
402326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
403326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
404326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
40574349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4062435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
407326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
408447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
40923f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4106c72ffaaSAndy Whitcroftour $Operators	= qr{
4116c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4126c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
41323f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4146c72ffaaSAndy Whitcroft		  }x;
4156c72ffaaSAndy Whitcroft
41691cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
41791cb5195SJoe Perches
418ab7e23f3SJoe Perchesour $BasicType;
4198905a67cSAndy Whitcroftour $NonptrType;
4201813087dSJoe Perchesour $NonptrTypeMisordered;
4218716de38SJoe Perchesour $NonptrTypeWithAttr;
4228905a67cSAndy Whitcroftour $Type;
4231813087dSJoe Perchesour $TypeMisordered;
4248905a67cSAndy Whitcroftour $Declare;
4251813087dSJoe Perchesour $DeclareMisordered;
4268905a67cSAndy Whitcroft
42715662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
42815662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
429171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
430171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
431171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
432171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
433171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
434171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
435171ae1a4SAndy Whitcroft}x;
436171ae1a4SAndy Whitcroft
43715662b3eSJoe Perchesour $UTF8	= qr{
43815662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
43915662b3eSJoe Perches	| $NON_ASCII_UTF8
44015662b3eSJoe Perches}x;
44115662b3eSJoe Perches
442e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
443021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
444021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
445021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
446021158b4SJoe Perches)};
447e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
448fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4498ed22cadSAndy Whitcroft	atomic_t
4508ed22cadSAndy Whitcroft)};
451e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
452e6176fa4SJoe Perches	$typeC99Typedefs\b|
453e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
454e6176fa4SJoe Perches	$typeKernelTypedefs\b
455e6176fa4SJoe Perches)};
4568ed22cadSAndy Whitcroft
4576d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4586d32f7a3SJoe Perches
459691e669bSJoe Perchesour $logFunctions = qr{(?x:
460758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4617d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
46287bd499aSJoe Perches	TP_printk|
4636e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
464b0531722SJoe Perches	panic|
46506668727SJoe Perches	MODULE_[A-Z_]+|
46606668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
467691e669bSJoe Perches)};
468691e669bSJoe Perches
469e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
470e29a70f1SJoe Perches	(?:(?:devm_)?
471e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
472e29a70f1SJoe Perches		kstrdup(?:_const)? |
473e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
474e29a70f1SJoe Perches	(?:\w+)?alloc_skb(?:ip_align)? |
475e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
476e29a70f1SJoe Perches	dma_alloc_coherent
477e29a70f1SJoe Perches)};
478e29a70f1SJoe Perches
47920112475SJoe Perchesour $signature_tags = qr{(?xi:
48020112475SJoe Perches	Signed-off-by:|
481d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
48220112475SJoe Perches	Acked-by:|
48320112475SJoe Perches	Tested-by:|
48420112475SJoe Perches	Reviewed-by:|
48520112475SJoe Perches	Reported-by:|
4868543ae12SMugunthan V N	Suggested-by:|
48720112475SJoe Perches	To:|
48820112475SJoe Perches	Cc:
48920112475SJoe Perches)};
49020112475SJoe Perches
4911813087dSJoe Perchesour @typeListMisordered = (
4921813087dSJoe Perches	qr{char\s+(?:un)?signed},
4931813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4941813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4951813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4961813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4971813087dSJoe Perches	qr{short\s+(?:un)?signed},
4981813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4991813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5001813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5011813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5021813087dSJoe Perches	qr{int\s+(?:un)?signed},
5031813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5041813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5051813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5061813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5071813087dSJoe Perches	qr{long\s+(?:un)?signed},
5081813087dSJoe Perches);
5091813087dSJoe Perches
5108905a67cSAndy Whitcroftour @typeList = (
5118905a67cSAndy Whitcroft	qr{void},
5120c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5130c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5140c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5150c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5160c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5170c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5180c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5190c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5200c773d9dSJoe Perches	qr{(?:un)?signed},
5218905a67cSAndy Whitcroft	qr{float},
5228905a67cSAndy Whitcroft	qr{double},
5238905a67cSAndy Whitcroft	qr{bool},
5248905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5258905a67cSAndy Whitcroft	qr{union\s+$Ident},
5268905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5278905a67cSAndy Whitcroft	qr{${Ident}_t},
5288905a67cSAndy Whitcroft	qr{${Ident}_handler},
5298905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5301813087dSJoe Perches	@typeListMisordered,
5318905a67cSAndy Whitcroft);
532938224b5SJoe Perches
533938224b5SJoe Perchesour $C90_int_types = qr{(?x:
534938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
535938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
536938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
537938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
538938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
539938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
540938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
541938224b5SJoe Perches
542938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
543938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
544938224b5SJoe Perches	long\s+(?:un)?signed|
545938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
546938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
547938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
548938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
549938224b5SJoe Perches
550938224b5SJoe Perches	int\s+(?:un)?signed|
551938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
552938224b5SJoe Perches)};
553938224b5SJoe Perches
554485ff23eSAlex Dowadour @typeListFile = ();
5558716de38SJoe Perchesour @typeListWithAttr = (
5568716de38SJoe Perches	@typeList,
5578716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5588716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5598716de38SJoe Perches);
5608716de38SJoe Perches
561c45dcabdSAndy Whitcroftour @modifierList = (
562c45dcabdSAndy Whitcroft	qr{fastcall},
563c45dcabdSAndy Whitcroft);
564485ff23eSAlex Dowadour @modifierListFile = ();
5658905a67cSAndy Whitcroft
5662435880fSJoe Perchesour @mode_permission_funcs = (
5672435880fSJoe Perches	["module_param", 3],
5682435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5692435880fSJoe Perches	["module_param_array_named", 5],
5702435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5712435880fSJoe Perches	["proc_create(?:_data|)", 2],
572459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
573459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
574459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
575459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
576459cf0aeSJoe Perches	["__ATTR", 2],
5772435880fSJoe Perches);
5782435880fSJoe Perches
579515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
580515a235eSJoe Perchesour $mode_perms_search = "";
581515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
582515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
583515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
584515a235eSJoe Perches}
58500180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
586515a235eSJoe Perches
5879189c7e7SJoe Perchesour %deprecated_apis = (
5889189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
5899189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
5909189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
5919189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
5929189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
5939189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
5949189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
5959189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
5969189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
5979189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
5989189c7e7SJoe Perches);
5999189c7e7SJoe Perches
6009189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6019189c7e7SJoe Perchesour $deprecated_apis_search = "";
6029189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6039189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6049189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6059189c7e7SJoe Perches}
6069189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6079189c7e7SJoe Perches
608b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
609b392c64fSJoe Perches	S_IWUGO		|
610b392c64fSJoe Perches	S_IWOTH		|
611b392c64fSJoe Perches	S_IRWXUGO	|
612b392c64fSJoe Perches	S_IALLUGO	|
613b392c64fSJoe Perches	0[0-7][0-7][2367]
614b392c64fSJoe Perches}x;
615b392c64fSJoe Perches
616f90774e1SJoe Perchesour %mode_permission_string_types = (
617f90774e1SJoe Perches	"S_IRWXU" => 0700,
618f90774e1SJoe Perches	"S_IRUSR" => 0400,
619f90774e1SJoe Perches	"S_IWUSR" => 0200,
620f90774e1SJoe Perches	"S_IXUSR" => 0100,
621f90774e1SJoe Perches	"S_IRWXG" => 0070,
622f90774e1SJoe Perches	"S_IRGRP" => 0040,
623f90774e1SJoe Perches	"S_IWGRP" => 0020,
624f90774e1SJoe Perches	"S_IXGRP" => 0010,
625f90774e1SJoe Perches	"S_IRWXO" => 0007,
626f90774e1SJoe Perches	"S_IROTH" => 0004,
627f90774e1SJoe Perches	"S_IWOTH" => 0002,
628f90774e1SJoe Perches	"S_IXOTH" => 0001,
629f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
630f90774e1SJoe Perches	"S_IRUGO" => 0444,
631f90774e1SJoe Perches	"S_IWUGO" => 0222,
632f90774e1SJoe Perches	"S_IXUGO" => 0111,
633f90774e1SJoe Perches);
634f90774e1SJoe Perches
635f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
636f90774e1SJoe Perchesour $mode_perms_string_search = "";
637f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
638f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
639f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
640f90774e1SJoe Perches}
64100180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
64200180468SJoe Perchesour $multi_mode_perms_string_search = qr{
64300180468SJoe Perches	${single_mode_perms_string_search}
64400180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
64500180468SJoe Perches}x;
64600180468SJoe Perches
64700180468SJoe Perchessub perms_to_octal {
64800180468SJoe Perches	my ($string) = @_;
64900180468SJoe Perches
65000180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
65100180468SJoe Perches
65200180468SJoe Perches	my $val = "";
65300180468SJoe Perches	my $oval = "";
65400180468SJoe Perches	my $to = 0;
65500180468SJoe Perches	my $curpos = 0;
65600180468SJoe Perches	my $lastpos = 0;
65700180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
65800180468SJoe Perches		$curpos = pos($string);
65900180468SJoe Perches		my $match = $2;
66000180468SJoe Perches		my $omatch = $1;
66100180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
66200180468SJoe Perches		$lastpos = $curpos;
66300180468SJoe Perches		$to |= $mode_permission_string_types{$match};
66400180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
66500180468SJoe Perches		$val .= $match;
66600180468SJoe Perches		$oval .= $omatch;
66700180468SJoe Perches	}
66800180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
66900180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
67000180468SJoe Perches	return sprintf("%04o", $to);
67100180468SJoe Perches}
672f90774e1SJoe Perches
6737840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6747840a94cSWolfram Sang	irq|
675cdcee686SSergey Ryazanov	memory|
676cdcee686SSergey Ryazanov	time|
677cdcee686SSergey Ryazanov	reboot
6787840a94cSWolfram Sang)};
6797840a94cSWolfram Sang# memory.h: ARM has a custom one
6807840a94cSWolfram Sang
68166b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
68266b47b4aSKees Cookmy $misspellings;
68366b47b4aSKees Cookmy %spelling_fix;
68436061e38SJoe Perches
68536061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
68666b47b4aSKees Cook	while (<$spelling>) {
68766b47b4aSKees Cook		my $line = $_;
68866b47b4aSKees Cook
68966b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
69066b47b4aSKees Cook		$line =~ s/^\s*//g;
69166b47b4aSKees Cook
69266b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
69366b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
69466b47b4aSKees Cook
69566b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
69666b47b4aSKees Cook
69766b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
69866b47b4aSKees Cook	}
69966b47b4aSKees Cook	close($spelling);
70036061e38SJoe Perches} else {
70136061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
70236061e38SJoe Perches}
70366b47b4aSKees Cook
704ebfd7d62SJoe Perchesif ($codespell) {
705ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
706ebfd7d62SJoe Perches		while (<$spelling>) {
707ebfd7d62SJoe Perches			my $line = $_;
708ebfd7d62SJoe Perches
709ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
710ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
711ebfd7d62SJoe Perches
712ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
713ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
714ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
715ebfd7d62SJoe Perches
716ebfd7d62SJoe Perches			$line =~ s/,.*$//;
717ebfd7d62SJoe Perches
718ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
719ebfd7d62SJoe Perches
720ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
721ebfd7d62SJoe Perches		}
722ebfd7d62SJoe Perches		close($spelling);
723ebfd7d62SJoe Perches	} else {
724ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
725ebfd7d62SJoe Perches	}
726ebfd7d62SJoe Perches}
727ebfd7d62SJoe Perches
728ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
729ebfd7d62SJoe Perches
73075ad8c57SJerome Forissiersub read_words {
73175ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
73275ad8c57SJerome Forissier
73375ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
73475ad8c57SJerome Forissier		while (<$words>) {
735bf1fa1daSJoe Perches			my $line = $_;
736bf1fa1daSJoe Perches
737bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
738bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
739bf1fa1daSJoe Perches
740bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
741bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
742bf1fa1daSJoe Perches			if ($line =~ /\s/) {
74375ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
744bf1fa1daSJoe Perches				next;
745bf1fa1daSJoe Perches			}
746bf1fa1daSJoe Perches
74775ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
74875ad8c57SJerome Forissier			$$wordsRef .= $line;
749bf1fa1daSJoe Perches		}
75075ad8c57SJerome Forissier		close($file);
75175ad8c57SJerome Forissier		return 1;
752bf1fa1daSJoe Perches	}
753bf1fa1daSJoe Perches
75475ad8c57SJerome Forissier	return 0;
75575ad8c57SJerome Forissier}
75675ad8c57SJerome Forissier
75775ad8c57SJerome Forissiermy $const_structs = "";
75875ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
75975ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
76075ad8c57SJerome Forissier
76175ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
76275ad8c57SJerome Forissierif (length($typedefsfile)) {
76375ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
76475ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
76575ad8c57SJerome Forissier}
76675ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
76775ad8c57SJerome Forissier
7688905a67cSAndy Whitcroftsub build_types {
769485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
770485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7711813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7728716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
773c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
774ab7e23f3SJoe Perches	$BasicType	= qr{
775ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
776ab7e23f3SJoe Perches				(?:${all}\b)
777ab7e23f3SJoe Perches		}x;
7788905a67cSAndy Whitcroft	$NonptrType	= qr{
779d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
780cf655043SAndy Whitcroft			(?:
7816b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7828ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
783c45dcabdSAndy Whitcroft				(?:${all}\b)
784cf655043SAndy Whitcroft			)
785c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7868905a67cSAndy Whitcroft		  }x;
7871813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7881813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7891813087dSJoe Perches			(?:
7901813087dSJoe Perches				(?:${Misordered}\b)
7911813087dSJoe Perches			)
7921813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7931813087dSJoe Perches		  }x;
7948716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7958716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7968716de38SJoe Perches			(?:
7978716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
7988716de38SJoe Perches				(?:$typeTypedefs\b)|
7998716de38SJoe Perches				(?:${allWithAttr}\b)
8008716de38SJoe Perches			)
8018716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8028716de38SJoe Perches		  }x;
8038905a67cSAndy Whitcroft	$Type	= qr{
804c45dcabdSAndy Whitcroft			$NonptrType
8051574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
806c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8078905a67cSAndy Whitcroft		  }x;
8081813087dSJoe Perches	$TypeMisordered	= qr{
8091813087dSJoe Perches			$NonptrTypeMisordered
8101813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
8111813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8121813087dSJoe Perches		  }x;
81391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8141813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8158905a67cSAndy Whitcroft}
8168905a67cSAndy Whitcroftbuild_types();
8176c72ffaaSAndy Whitcroft
8187d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
819d1fe9c09SJoe Perches
820d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
821d1fe9c09SJoe Perches# requires at least perl version v5.10.0
822d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
823d1fe9c09SJoe Perches
824d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
8252435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
826c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
8277d2367afSJoe Perches
828f8422308SJoe Perchesour $declaration_macros = qr{(?x:
8293e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
830fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
8313d102fc0SGilad Ben-Yossef	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
8323d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
833f8422308SJoe Perches)};
834f8422308SJoe Perches
8357d2367afSJoe Perchessub deparenthesize {
8367d2367afSJoe Perches	my ($string) = @_;
8377d2367afSJoe Perches	return "" if (!defined($string));
8385b9553abSJoe Perches
8395b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8405b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8415b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8425b9553abSJoe Perches	}
8435b9553abSJoe Perches
8447d2367afSJoe Perches	$string =~ s@\s+@ @g;
8455b9553abSJoe Perches
8467d2367afSJoe Perches	return $string;
8477d2367afSJoe Perches}
8487d2367afSJoe Perches
8493445686aSJoe Perchessub seed_camelcase_file {
8503445686aSJoe Perches	my ($file) = @_;
8513445686aSJoe Perches
8523445686aSJoe Perches	return if (!(-f $file));
8533445686aSJoe Perches
8543445686aSJoe Perches	local $/;
8553445686aSJoe Perches
8563445686aSJoe Perches	open(my $include_file, '<', "$file")
8573445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8583445686aSJoe Perches	my $text = <$include_file>;
8593445686aSJoe Perches	close($include_file);
8603445686aSJoe Perches
8613445686aSJoe Perches	my @lines = split('\n', $text);
8623445686aSJoe Perches
8633445686aSJoe Perches	foreach my $line (@lines) {
8643445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8653445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8663445686aSJoe Perches			$camelcase{$1} = 1;
86711ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
86811ea516aSJoe Perches			$camelcase{$1} = 1;
86911ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8703445686aSJoe Perches			$camelcase{$1} = 1;
8713445686aSJoe Perches		}
8723445686aSJoe Perches	}
8733445686aSJoe Perches}
8743445686aSJoe Perches
87585b0ee18SJoe Perchessub is_maintained_obsolete {
87685b0ee18SJoe Perches	my ($filename) = @_;
87785b0ee18SJoe Perches
878f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
87985b0ee18SJoe Perches
8800616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
88185b0ee18SJoe Perches
88285b0ee18SJoe Perches	return $status =~ /obsolete/i;
88385b0ee18SJoe Perches}
88485b0ee18SJoe Perches
8853b6e8ac9SJoe Perchessub is_SPDX_License_valid {
8863b6e8ac9SJoe Perches	my ($license) = @_;
8873b6e8ac9SJoe Perches
88856294112SJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
8893b6e8ac9SJoe Perches
89056294112SJoe Perches	my $root_path = abs_path($root);
89156294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
8923b6e8ac9SJoe Perches	return 0 if ($status ne "");
8933b6e8ac9SJoe Perches	return 1;
8943b6e8ac9SJoe Perches}
8953b6e8ac9SJoe Perches
8963445686aSJoe Perchesmy $camelcase_seeded = 0;
8973445686aSJoe Perchessub seed_camelcase_includes {
8983445686aSJoe Perches	return if ($camelcase_seeded);
8993445686aSJoe Perches
9003445686aSJoe Perches	my $files;
901c707a81dSJoe Perches	my $camelcase_cache = "";
902c707a81dSJoe Perches	my @include_files = ();
903c707a81dSJoe Perches
904c707a81dSJoe Perches	$camelcase_seeded = 1;
905351b2a1fSJoe Perches
9063645e328SRichard Genoud	if (-e ".git") {
907351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
908351b2a1fSJoe Perches		chomp $git_last_include_commit;
909c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
910c707a81dSJoe Perches	} else {
911c707a81dSJoe Perches		my $last_mod_date = 0;
912c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
913c707a81dSJoe Perches		@include_files = split('\n', $files);
914c707a81dSJoe Perches		foreach my $file (@include_files) {
915c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
916c707a81dSJoe Perches						   localtime((stat $file)[9]));
917c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
918c707a81dSJoe Perches		}
919c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
920c707a81dSJoe Perches	}
921c707a81dSJoe Perches
922c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
923c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
924c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
925351b2a1fSJoe Perches		while (<$camelcase_file>) {
926351b2a1fSJoe Perches			chomp;
927351b2a1fSJoe Perches			$camelcase{$_} = 1;
928351b2a1fSJoe Perches		}
929351b2a1fSJoe Perches		close($camelcase_file);
930351b2a1fSJoe Perches
931351b2a1fSJoe Perches		return;
932351b2a1fSJoe Perches	}
933c707a81dSJoe Perches
9343645e328SRichard Genoud	if (-e ".git") {
935c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
936c707a81dSJoe Perches		@include_files = split('\n', $files);
9373445686aSJoe Perches	}
938c707a81dSJoe Perches
9393445686aSJoe Perches	foreach my $file (@include_files) {
9403445686aSJoe Perches		seed_camelcase_file($file);
9413445686aSJoe Perches	}
942351b2a1fSJoe Perches
943c707a81dSJoe Perches	if ($camelcase_cache ne "") {
944351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
945c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
946c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
947351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
948351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
949351b2a1fSJoe Perches		}
950351b2a1fSJoe Perches		close($camelcase_file);
951351b2a1fSJoe Perches	}
9523445686aSJoe Perches}
9533445686aSJoe Perches
954d311cd44SJoe Perchessub git_commit_info {
955d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
956d311cd44SJoe Perches
957d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
958d311cd44SJoe Perches
959d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
960d311cd44SJoe Perches	$output =~ s/^\s*//gm;
961d311cd44SJoe Perches	my @lines = split("\n", $output);
962d311cd44SJoe Perches
9630d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9640d7835fcSJoe Perches
965d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
966d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
967d311cd44SJoe Perches# all matching commit ids, but it's very slow...
968d311cd44SJoe Perches#
969d311cd44SJoe Perches#		echo "checking commits $1..."
970d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
971d311cd44SJoe Perches#		while read line ; do
972d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
973d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
974d311cd44SJoe Perches#		done
975d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
976948b133aSHeinrich Schuchardt		$id = undef;
977d311cd44SJoe Perches	} else {
978d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
979d311cd44SJoe Perches		$desc = substr($lines[0], 41);
980d311cd44SJoe Perches	}
981d311cd44SJoe Perches
982d311cd44SJoe Perches	return ($id, $desc);
983d311cd44SJoe Perches}
984d311cd44SJoe Perches
9856c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9860a920b5bSAndy Whitcroft
98700df344fSAndy Whitcroftmy @rawlines = ();
988c2fdda0dSAndy Whitcroftmy @lines = ();
9893705ce5bSJoe Perchesmy @fixed = ();
990d752fcc8SJoe Perchesmy @fixed_inserted = ();
991d752fcc8SJoe Perchesmy @fixed_deleted = ();
992194f66fcSJoe Perchesmy $fixlinenr = -1;
993194f66fcSJoe Perches
9944a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
9954a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
9964a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
9974a593c34SDu, Changbin
9984a593c34SDu, Changbinif ($git) {
9994a593c34SDu, Changbin	my @commits = ();
10000dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
10014a593c34SDu, Changbin		my $git_range;
100228898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
100328898fd1SJoe Perches			$git_range = "-$2 $1";
10044a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
10054a593c34SDu, Changbin			$git_range = "$commit_expr";
10064a593c34SDu, Changbin		} else {
10070dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
10080dea9f1eSJoe Perches		}
10090dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
10100dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
101128898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
101228898fd1SJoe Perches			next if (!defined($1) || !defined($2));
10130dea9f1eSJoe Perches			my $sha1 = $1;
10140dea9f1eSJoe Perches			my $subject = $2;
10150dea9f1eSJoe Perches			unshift(@commits, $sha1);
10160dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
10174a593c34SDu, Changbin		}
10184a593c34SDu, Changbin	}
10194a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
10204a593c34SDu, Changbin	@ARGV = @commits;
10214a593c34SDu, Changbin}
10224a593c34SDu, Changbin
1023c2fdda0dSAndy Whitcroftmy $vname;
102498005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
10256c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
102621caa13cSAndy Whitcroft	my $FILE;
10274a593c34SDu, Changbin	if ($git) {
10284a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
10294a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
10304a593c34SDu, Changbin	} elsif ($file) {
103121caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
10326c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
103321caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
103421caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10356c72ffaaSAndy Whitcroft	} else {
103621caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10376c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10386c72ffaaSAndy Whitcroft	}
1039c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1040c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10414a593c34SDu, Changbin	} elsif ($git) {
10420dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1043c2fdda0dSAndy Whitcroft	} else {
1044c2fdda0dSAndy Whitcroft		$vname = $filename;
1045c2fdda0dSAndy Whitcroft	}
104621caa13cSAndy Whitcroft	while (<$FILE>) {
10470a920b5bSAndy Whitcroft		chomp;
104800df344fSAndy Whitcroft		push(@rawlines, $_);
10496c72ffaaSAndy Whitcroft	}
105021caa13cSAndy Whitcroft	close($FILE);
1051d8469f16SJoe Perches
1052d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1053d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1054d8469f16SJoe Perches		print "$vname\n";
1055d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1056d8469f16SJoe Perches	}
1057d8469f16SJoe Perches
1058c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10590a920b5bSAndy Whitcroft		$exit = 1;
10600a920b5bSAndy Whitcroft	}
106100df344fSAndy Whitcroft	@rawlines = ();
106213214adfSAndy Whitcroft	@lines = ();
10633705ce5bSJoe Perches	@fixed = ();
1064d752fcc8SJoe Perches	@fixed_inserted = ();
1065d752fcc8SJoe Perches	@fixed_deleted = ();
1066194f66fcSJoe Perches	$fixlinenr = -1;
1067485ff23eSAlex Dowad	@modifierListFile = ();
1068485ff23eSAlex Dowad	@typeListFile = ();
1069485ff23eSAlex Dowad	build_types();
10700a920b5bSAndy Whitcroft}
10710a920b5bSAndy Whitcroft
1072d8469f16SJoe Perchesif (!$quiet) {
10733c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10743c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10753c816e49SJoe Perches
10765b57980dSJoe Perches	if (!$perl_version_ok) {
1077d8469f16SJoe Perches		print << "EOM"
1078d8469f16SJoe Perches
1079d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
10805b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1081d8469f16SJoe PerchesEOM
1082d8469f16SJoe Perches	}
1083d8469f16SJoe Perches	if ($exit) {
1084d8469f16SJoe Perches		print << "EOM"
1085d8469f16SJoe Perches
1086d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1087d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1088d8469f16SJoe PerchesEOM
1089d8469f16SJoe Perches	}
1090d8469f16SJoe Perches}
1091d8469f16SJoe Perches
10920a920b5bSAndy Whitcroftexit($exit);
10930a920b5bSAndy Whitcroft
10940a920b5bSAndy Whitcroftsub top_of_kernel_tree {
10956c72ffaaSAndy Whitcroft	my ($root) = @_;
10966c72ffaaSAndy Whitcroft
10976c72ffaaSAndy Whitcroft	my @tree_check = (
10986c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
10996c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
11006c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
11016c72ffaaSAndy Whitcroft	);
11026c72ffaaSAndy Whitcroft
11036c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
11046c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
11050a920b5bSAndy Whitcroft			return 0;
11060a920b5bSAndy Whitcroft		}
11076c72ffaaSAndy Whitcroft	}
11086c72ffaaSAndy Whitcroft	return 1;
11096c72ffaaSAndy Whitcroft}
11100a920b5bSAndy Whitcroft
111120112475SJoe Perchessub parse_email {
111220112475SJoe Perches	my ($formatted_email) = @_;
111320112475SJoe Perches
111420112475SJoe Perches	my $name = "";
111520112475SJoe Perches	my $address = "";
111620112475SJoe Perches	my $comment = "";
111720112475SJoe Perches
111820112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
111920112475SJoe Perches		$name = $1;
112020112475SJoe Perches		$address = $2;
112120112475SJoe Perches		$comment = $3 if defined $3;
112220112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
112320112475SJoe Perches		$address = $1;
112420112475SJoe Perches		$comment = $2 if defined $2;
112520112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
112620112475SJoe Perches		$address = $1;
112720112475SJoe Perches		$comment = $2 if defined $2;
112885e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
112920112475SJoe Perches		$name = $formatted_email;
11303705ce5bSJoe Perches		$name = trim($name);
113120112475SJoe Perches		$name =~ s/^\"|\"$//g;
113220112475SJoe Perches		# If there's a name left after stripping spaces and
113320112475SJoe Perches		# leading quotes, and the address doesn't have both
113420112475SJoe Perches		# leading and trailing angle brackets, the address
113520112475SJoe Perches		# is invalid. ie:
113620112475SJoe Perches		#   "joe smith [email protected]" bad
113720112475SJoe Perches		#   "joe smith <[email protected]" bad
113820112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
113920112475SJoe Perches			$name = "";
114020112475SJoe Perches			$address = "";
114120112475SJoe Perches			$comment = "";
114220112475SJoe Perches		}
114320112475SJoe Perches	}
114420112475SJoe Perches
11453705ce5bSJoe Perches	$name = trim($name);
114620112475SJoe Perches	$name =~ s/^\"|\"$//g;
11473705ce5bSJoe Perches	$address = trim($address);
114820112475SJoe Perches	$address =~ s/^\<|\>$//g;
114920112475SJoe Perches
115020112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
115120112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
115220112475SJoe Perches		$name = "\"$name\"";
115320112475SJoe Perches	}
115420112475SJoe Perches
115520112475SJoe Perches	return ($name, $address, $comment);
115620112475SJoe Perches}
115720112475SJoe Perches
115820112475SJoe Perchessub format_email {
115920112475SJoe Perches	my ($name, $address) = @_;
116020112475SJoe Perches
116120112475SJoe Perches	my $formatted_email;
116220112475SJoe Perches
11633705ce5bSJoe Perches	$name = trim($name);
116420112475SJoe Perches	$name =~ s/^\"|\"$//g;
11653705ce5bSJoe Perches	$address = trim($address);
116620112475SJoe Perches
116720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
116820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
116920112475SJoe Perches		$name = "\"$name\"";
117020112475SJoe Perches	}
117120112475SJoe Perches
117220112475SJoe Perches	if ("$name" eq "") {
117320112475SJoe Perches		$formatted_email = "$address";
117420112475SJoe Perches	} else {
117520112475SJoe Perches		$formatted_email = "$name <$address>";
117620112475SJoe Perches	}
117720112475SJoe Perches
117820112475SJoe Perches	return $formatted_email;
117920112475SJoe Perches}
118020112475SJoe Perches
1181d311cd44SJoe Perchessub which {
1182d311cd44SJoe Perches	my ($bin) = @_;
1183d311cd44SJoe Perches
1184d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1185d311cd44SJoe Perches		if (-e "$path/$bin") {
1186d311cd44SJoe Perches			return "$path/$bin";
1187d311cd44SJoe Perches		}
1188d311cd44SJoe Perches	}
1189d311cd44SJoe Perches
1190d311cd44SJoe Perches	return "";
1191d311cd44SJoe Perches}
1192d311cd44SJoe Perches
1193000d1cc1SJoe Perchessub which_conf {
1194000d1cc1SJoe Perches	my ($conf) = @_;
1195000d1cc1SJoe Perches
1196000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1197000d1cc1SJoe Perches		if (-e "$path/$conf") {
1198000d1cc1SJoe Perches			return "$path/$conf";
1199000d1cc1SJoe Perches		}
1200000d1cc1SJoe Perches	}
1201000d1cc1SJoe Perches
1202000d1cc1SJoe Perches	return "";
1203000d1cc1SJoe Perches}
1204000d1cc1SJoe Perches
12050a920b5bSAndy Whitcroftsub expand_tabs {
12060a920b5bSAndy Whitcroft	my ($str) = @_;
12070a920b5bSAndy Whitcroft
12080a920b5bSAndy Whitcroft	my $res = '';
12090a920b5bSAndy Whitcroft	my $n = 0;
12100a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
12110a920b5bSAndy Whitcroft		if ($c eq "\t") {
12120a920b5bSAndy Whitcroft			$res .= ' ';
12130a920b5bSAndy Whitcroft			$n++;
12140a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
12150a920b5bSAndy Whitcroft				$res .= ' ';
12160a920b5bSAndy Whitcroft			}
12170a920b5bSAndy Whitcroft			next;
12180a920b5bSAndy Whitcroft		}
12190a920b5bSAndy Whitcroft		$res .= $c;
12200a920b5bSAndy Whitcroft		$n++;
12210a920b5bSAndy Whitcroft	}
12220a920b5bSAndy Whitcroft
12230a920b5bSAndy Whitcroft	return $res;
12240a920b5bSAndy Whitcroft}
12256c72ffaaSAndy Whitcroftsub copy_spacing {
1226773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
12276c72ffaaSAndy Whitcroft	return $res;
12286c72ffaaSAndy Whitcroft}
12290a920b5bSAndy Whitcroft
12304a0df2efSAndy Whitcroftsub line_stats {
12314a0df2efSAndy Whitcroft	my ($line) = @_;
12324a0df2efSAndy Whitcroft
12334a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
12344a0df2efSAndy Whitcroft	$line =~ s/^.//;
12354a0df2efSAndy Whitcroft	$line = expand_tabs($line);
12364a0df2efSAndy Whitcroft
12374a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
12384a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
12394a0df2efSAndy Whitcroft
12404a0df2efSAndy Whitcroft	return (length($line), length($white));
12414a0df2efSAndy Whitcroft}
12424a0df2efSAndy Whitcroft
1243773647a0SAndy Whitcroftmy $sanitise_quote = '';
1244773647a0SAndy Whitcroft
1245773647a0SAndy Whitcroftsub sanitise_line_reset {
1246773647a0SAndy Whitcroft	my ($in_comment) = @_;
1247773647a0SAndy Whitcroft
1248773647a0SAndy Whitcroft	if ($in_comment) {
1249773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1250773647a0SAndy Whitcroft	} else {
1251773647a0SAndy Whitcroft		$sanitise_quote = '';
1252773647a0SAndy Whitcroft	}
1253773647a0SAndy Whitcroft}
125400df344fSAndy Whitcroftsub sanitise_line {
125500df344fSAndy Whitcroft	my ($line) = @_;
125600df344fSAndy Whitcroft
125700df344fSAndy Whitcroft	my $res = '';
125800df344fSAndy Whitcroft	my $l = '';
125900df344fSAndy Whitcroft
1260c2fdda0dSAndy Whitcroft	my $qlen = 0;
1261773647a0SAndy Whitcroft	my $off = 0;
1262773647a0SAndy Whitcroft	my $c;
126300df344fSAndy Whitcroft
1264773647a0SAndy Whitcroft	# Always copy over the diff marker.
1265773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1266773647a0SAndy Whitcroft
1267773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1268773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1269773647a0SAndy Whitcroft
12708d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1271773647a0SAndy Whitcroft		# and end, all to $;.
1272773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1273773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1274773647a0SAndy Whitcroft
1275773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1276773647a0SAndy Whitcroft			$off++;
127700df344fSAndy Whitcroft			next;
1278773647a0SAndy Whitcroft		}
127981bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1280773647a0SAndy Whitcroft			$sanitise_quote = '';
1281773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1282773647a0SAndy Whitcroft			$off++;
1283773647a0SAndy Whitcroft			next;
1284773647a0SAndy Whitcroft		}
1285113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1286113f04a8SDaniel Walker			$sanitise_quote = '//';
1287113f04a8SDaniel Walker
1288113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1289113f04a8SDaniel Walker			$off++;
1290113f04a8SDaniel Walker			next;
1291113f04a8SDaniel Walker		}
1292773647a0SAndy Whitcroft
1293773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1294773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1295773647a0SAndy Whitcroft		    $c eq "\\") {
1296773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1297773647a0SAndy Whitcroft			$off++;
1298773647a0SAndy Whitcroft			next;
1299773647a0SAndy Whitcroft		}
1300773647a0SAndy Whitcroft		# Regular quotes.
1301773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1302773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1303773647a0SAndy Whitcroft				$sanitise_quote = $c;
1304773647a0SAndy Whitcroft
1305773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1306773647a0SAndy Whitcroft				next;
1307773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1308773647a0SAndy Whitcroft				$sanitise_quote = '';
130900df344fSAndy Whitcroft			}
131000df344fSAndy Whitcroft		}
1311773647a0SAndy Whitcroft
1312fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1313773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1314773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1315113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1316113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1317773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1318773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
131900df344fSAndy Whitcroft		} else {
1320773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
132100df344fSAndy Whitcroft		}
1322c2fdda0dSAndy Whitcroft	}
1323c2fdda0dSAndy Whitcroft
1324113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1325113f04a8SDaniel Walker		$sanitise_quote = '';
1326113f04a8SDaniel Walker	}
1327113f04a8SDaniel Walker
1328c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1329c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1330c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1331c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1332c2fdda0dSAndy Whitcroft
1333c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1334c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1335c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1336c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1337c2fdda0dSAndy Whitcroft	}
1338c2fdda0dSAndy Whitcroft
1339dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1340dadf680dSJoe Perches		my $match = $1;
1341dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1342dadf680dSJoe Perches	}
1343dadf680dSJoe Perches
134400df344fSAndy Whitcroft	return $res;
134500df344fSAndy Whitcroft}
134600df344fSAndy Whitcroft
1347a6962d72SJoe Perchessub get_quoted_string {
1348a6962d72SJoe Perches	my ($line, $rawline) = @_;
1349a6962d72SJoe Perches
1350478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
135133acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1352a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1353a6962d72SJoe Perches}
1354a6962d72SJoe Perches
13558905a67cSAndy Whitcroftsub ctx_statement_block {
13568905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13578905a67cSAndy Whitcroft	my $line = $linenr - 1;
13588905a67cSAndy Whitcroft	my $blk = '';
13598905a67cSAndy Whitcroft	my $soff = $off;
13608905a67cSAndy Whitcroft	my $coff = $off - 1;
1361773647a0SAndy Whitcroft	my $coff_set = 0;
13628905a67cSAndy Whitcroft
136313214adfSAndy Whitcroft	my $loff = 0;
136413214adfSAndy Whitcroft
13658905a67cSAndy Whitcroft	my $type = '';
13668905a67cSAndy Whitcroft	my $level = 0;
1367a2750645SAndy Whitcroft	my @stack = ();
1368cf655043SAndy Whitcroft	my $p;
13698905a67cSAndy Whitcroft	my $c;
13708905a67cSAndy Whitcroft	my $len = 0;
137113214adfSAndy Whitcroft
137213214adfSAndy Whitcroft	my $remainder;
13738905a67cSAndy Whitcroft	while (1) {
1374a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1375a2750645SAndy Whitcroft
1376773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
13778905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
13788905a67cSAndy Whitcroft		# context.
13798905a67cSAndy Whitcroft		if ($off >= $len) {
13808905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1381dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1382c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
13838905a67cSAndy Whitcroft				$remain--;
138413214adfSAndy Whitcroft				$loff = $len;
1385c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13868905a67cSAndy Whitcroft				$len = length($blk);
13878905a67cSAndy Whitcroft				$line++;
13888905a67cSAndy Whitcroft				last;
13898905a67cSAndy Whitcroft			}
13908905a67cSAndy Whitcroft			# Bail if there is no further context.
13918905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
139213214adfSAndy Whitcroft			if ($off >= $len) {
13938905a67cSAndy Whitcroft				last;
13948905a67cSAndy Whitcroft			}
1395f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1396f74bd194SAndy Whitcroft				$level++;
1397f74bd194SAndy Whitcroft				$type = '#';
1398f74bd194SAndy Whitcroft			}
13998905a67cSAndy Whitcroft		}
1400cf655043SAndy Whitcroft		$p = $c;
14018905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
140213214adfSAndy Whitcroft		$remainder = substr($blk, $off);
14038905a67cSAndy Whitcroft
1404773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
14054635f4fbSAndy Whitcroft
14064635f4fbSAndy Whitcroft		# Handle nested #if/#else.
14074635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
14084635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
14094635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
14104635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
14114635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
14124635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
14134635f4fbSAndy Whitcroft		}
14144635f4fbSAndy Whitcroft
14158905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
14168905a67cSAndy Whitcroft		# outermost level.
14178905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
14188905a67cSAndy Whitcroft			last;
14198905a67cSAndy Whitcroft		}
14208905a67cSAndy Whitcroft
142113214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1422773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1423773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1424773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1425773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1426773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1427773647a0SAndy Whitcroft			$coff_set = 1;
1428773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1429773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
143013214adfSAndy Whitcroft		}
143113214adfSAndy Whitcroft
14328905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
14338905a67cSAndy Whitcroft			$level++;
14348905a67cSAndy Whitcroft			$type = '(';
14358905a67cSAndy Whitcroft		}
14368905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
14378905a67cSAndy Whitcroft			$level--;
14388905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
14398905a67cSAndy Whitcroft
14408905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
14418905a67cSAndy Whitcroft				$coff = $off;
1442773647a0SAndy Whitcroft				$coff_set = 1;
1443773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
14448905a67cSAndy Whitcroft			}
14458905a67cSAndy Whitcroft		}
14468905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
14478905a67cSAndy Whitcroft			$level++;
14488905a67cSAndy Whitcroft			$type = '{';
14498905a67cSAndy Whitcroft		}
14508905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14518905a67cSAndy Whitcroft			$level--;
14528905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14538905a67cSAndy Whitcroft
14548905a67cSAndy Whitcroft			if ($level == 0) {
1455b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1456b998e001SPatrick Pannuto					$off++;
1457b998e001SPatrick Pannuto				}
14588905a67cSAndy Whitcroft				last;
14598905a67cSAndy Whitcroft			}
14608905a67cSAndy Whitcroft		}
1461f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1462f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1463f74bd194SAndy Whitcroft			$level--;
1464f74bd194SAndy Whitcroft			$type = '';
1465f74bd194SAndy Whitcroft			$off++;
1466f74bd194SAndy Whitcroft			last;
1467f74bd194SAndy Whitcroft		}
14688905a67cSAndy Whitcroft		$off++;
14698905a67cSAndy Whitcroft	}
1470a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
147113214adfSAndy Whitcroft	if ($off == $len) {
1472a3bb97a7SAndy Whitcroft		$loff = $len + 1;
147313214adfSAndy Whitcroft		$line++;
147413214adfSAndy Whitcroft		$remain--;
147513214adfSAndy Whitcroft	}
14768905a67cSAndy Whitcroft
14778905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
14788905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
14798905a67cSAndy Whitcroft
14808905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
14818905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
14828905a67cSAndy Whitcroft
1483773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
148413214adfSAndy Whitcroft
148513214adfSAndy Whitcroft	return ($statement, $condition,
148613214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
148713214adfSAndy Whitcroft}
148813214adfSAndy Whitcroft
1489cf655043SAndy Whitcroftsub statement_lines {
1490cf655043SAndy Whitcroft	my ($stmt) = @_;
1491cf655043SAndy Whitcroft
1492cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1493cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1494cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1495cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1496cf655043SAndy Whitcroft
1497cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1498cf655043SAndy Whitcroft
1499cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1500cf655043SAndy Whitcroft}
1501cf655043SAndy Whitcroft
1502cf655043SAndy Whitcroftsub statement_rawlines {
1503cf655043SAndy Whitcroft	my ($stmt) = @_;
1504cf655043SAndy Whitcroft
1505cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1506cf655043SAndy Whitcroft
1507cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1508cf655043SAndy Whitcroft}
1509cf655043SAndy Whitcroft
1510cf655043SAndy Whitcroftsub statement_block_size {
1511cf655043SAndy Whitcroft	my ($stmt) = @_;
1512cf655043SAndy Whitcroft
1513cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1514cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1515cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1516cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1517cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1518cf655043SAndy Whitcroft
1519cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1520cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1521cf655043SAndy Whitcroft
1522cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1523cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1524cf655043SAndy Whitcroft
1525cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1526cf655043SAndy Whitcroft		return $stmt_lines;
1527cf655043SAndy Whitcroft	} else {
1528cf655043SAndy Whitcroft		return $stmt_statements;
1529cf655043SAndy Whitcroft	}
1530cf655043SAndy Whitcroft}
1531cf655043SAndy Whitcroft
153213214adfSAndy Whitcroftsub ctx_statement_full {
153313214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
153413214adfSAndy Whitcroft	my ($statement, $condition, $level);
153513214adfSAndy Whitcroft
153613214adfSAndy Whitcroft	my (@chunks);
153713214adfSAndy Whitcroft
1538cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
153913214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
154013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1541773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
154213214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1543cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1544cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1545cf655043SAndy Whitcroft	}
1546cf655043SAndy Whitcroft
1547cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1548cf655043SAndy Whitcroft	# could continue the statement.
1549cf655043SAndy Whitcroft	for (;;) {
155013214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
155113214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1552cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1553773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1554cf655043SAndy Whitcroft		#print "C: push\n";
1555cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
155613214adfSAndy Whitcroft	}
155713214adfSAndy Whitcroft
155813214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
15598905a67cSAndy Whitcroft}
15608905a67cSAndy Whitcroft
15614a0df2efSAndy Whitcroftsub ctx_block_get {
1562f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
15634a0df2efSAndy Whitcroft	my $line;
15644a0df2efSAndy Whitcroft	my $start = $linenr - 1;
15654a0df2efSAndy Whitcroft	my $blk = '';
15664a0df2efSAndy Whitcroft	my @o;
15674a0df2efSAndy Whitcroft	my @c;
15684a0df2efSAndy Whitcroft	my @res = ();
15694a0df2efSAndy Whitcroft
1570f0a594c1SAndy Whitcroft	my $level = 0;
15714635f4fbSAndy Whitcroft	my @stack = ($level);
157200df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
157300df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
157400df344fSAndy Whitcroft		$remain--;
157500df344fSAndy Whitcroft
157600df344fSAndy Whitcroft		$blk .= $rawlines[$line];
15774635f4fbSAndy Whitcroft
15784635f4fbSAndy Whitcroft		# Handle nested #if/#else.
157901464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
15804635f4fbSAndy Whitcroft			push(@stack, $level);
158101464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
15824635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
158301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
15844635f4fbSAndy Whitcroft			$level = pop(@stack);
15854635f4fbSAndy Whitcroft		}
15864635f4fbSAndy Whitcroft
158701464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1588f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1589f0a594c1SAndy Whitcroft			if ($off > 0) {
1590f0a594c1SAndy Whitcroft				$off--;
1591f0a594c1SAndy Whitcroft				next;
1592f0a594c1SAndy Whitcroft			}
15934a0df2efSAndy Whitcroft
1594f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1595f0a594c1SAndy Whitcroft				$level--;
1596f0a594c1SAndy Whitcroft				last if ($level == 0);
1597f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1598f0a594c1SAndy Whitcroft				$level++;
1599f0a594c1SAndy Whitcroft			}
1600f0a594c1SAndy Whitcroft		}
16014a0df2efSAndy Whitcroft
1602f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
160300df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
16044a0df2efSAndy Whitcroft		}
16054a0df2efSAndy Whitcroft
1606f0a594c1SAndy Whitcroft		last if ($level == 0);
16074a0df2efSAndy Whitcroft	}
16084a0df2efSAndy Whitcroft
1609f0a594c1SAndy Whitcroft	return ($level, @res);
16104a0df2efSAndy Whitcroft}
16114a0df2efSAndy Whitcroftsub ctx_block_outer {
16124a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16134a0df2efSAndy Whitcroft
1614f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1615f0a594c1SAndy Whitcroft	return @r;
16164a0df2efSAndy Whitcroft}
16174a0df2efSAndy Whitcroftsub ctx_block {
16184a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16194a0df2efSAndy Whitcroft
1620f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1621f0a594c1SAndy Whitcroft	return @r;
1622653d4876SAndy Whitcroft}
1623653d4876SAndy Whitcroftsub ctx_statement {
1624f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1625f0a594c1SAndy Whitcroft
1626f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1627f0a594c1SAndy Whitcroft	return @r;
1628f0a594c1SAndy Whitcroft}
1629f0a594c1SAndy Whitcroftsub ctx_block_level {
1630653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1631653d4876SAndy Whitcroft
1632f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
16334a0df2efSAndy Whitcroft}
16349c0ca6f9SAndy Whitcroftsub ctx_statement_level {
16359c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16369c0ca6f9SAndy Whitcroft
16379c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
16389c0ca6f9SAndy Whitcroft}
16394a0df2efSAndy Whitcroft
16404a0df2efSAndy Whitcroftsub ctx_locate_comment {
16414a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16424a0df2efSAndy Whitcroft
16434a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1644beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
16454a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
16464a0df2efSAndy Whitcroft
16474a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
16484a0df2efSAndy Whitcroft	# comment.
16494a0df2efSAndy Whitcroft	my $in_comment = 0;
16504a0df2efSAndy Whitcroft	$current_comment = '';
16514a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
165200df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
165300df344fSAndy Whitcroft		#warn "           $line\n";
16544a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
16554a0df2efSAndy Whitcroft			$in_comment = 1;
16564a0df2efSAndy Whitcroft		}
16574a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
16584a0df2efSAndy Whitcroft			$in_comment = 1;
16594a0df2efSAndy Whitcroft		}
16604a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
16614a0df2efSAndy Whitcroft			$current_comment = '';
16624a0df2efSAndy Whitcroft		}
16634a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
16644a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
16654a0df2efSAndy Whitcroft			$in_comment = 0;
16664a0df2efSAndy Whitcroft		}
16674a0df2efSAndy Whitcroft	}
16684a0df2efSAndy Whitcroft
16694a0df2efSAndy Whitcroft	chomp($current_comment);
16704a0df2efSAndy Whitcroft	return($current_comment);
16714a0df2efSAndy Whitcroft}
16724a0df2efSAndy Whitcroftsub ctx_has_comment {
16734a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16744a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
16754a0df2efSAndy Whitcroft
167600df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
16774a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
16784a0df2efSAndy Whitcroft
16794a0df2efSAndy Whitcroft	return ($cmt ne '');
16804a0df2efSAndy Whitcroft}
16814a0df2efSAndy Whitcroft
16824d001e4dSAndy Whitcroftsub raw_line {
16834d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
16844d001e4dSAndy Whitcroft
16854d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16864d001e4dSAndy Whitcroft	$cnt++;
16874d001e4dSAndy Whitcroft
16884d001e4dSAndy Whitcroft	my $line;
16894d001e4dSAndy Whitcroft	while ($cnt) {
16904d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16914d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16924d001e4dSAndy Whitcroft		$cnt--;
16934d001e4dSAndy Whitcroft	}
16944d001e4dSAndy Whitcroft
16954d001e4dSAndy Whitcroft	return $line;
16964d001e4dSAndy Whitcroft}
16974d001e4dSAndy Whitcroft
16982a9f9d85STobin C. Hardingsub get_stat_real {
16992a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
17002a9f9d85STobin C. Harding
17012a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
17022a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
17032a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
17042a9f9d85STobin C. Harding	}
17052a9f9d85STobin C. Harding
17062a9f9d85STobin C. Harding	return $stat_real;
17072a9f9d85STobin C. Harding}
17082a9f9d85STobin C. Harding
1709e3d95a2aSTobin C. Hardingsub get_stat_here {
1710e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1711e3d95a2aSTobin C. Harding
1712e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1713e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1714e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1715e3d95a2aSTobin C. Harding	}
1716e3d95a2aSTobin C. Harding
1717e3d95a2aSTobin C. Harding	return $herectx;
1718e3d95a2aSTobin C. Harding}
1719e3d95a2aSTobin C. Harding
17200a920b5bSAndy Whitcroftsub cat_vet {
17210a920b5bSAndy Whitcroft	my ($vet) = @_;
17229c0ca6f9SAndy Whitcroft	my ($res, $coded);
17230a920b5bSAndy Whitcroft
17249c0ca6f9SAndy Whitcroft	$res = '';
17256c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
17266c72ffaaSAndy Whitcroft		$res .= $1;
17276c72ffaaSAndy Whitcroft		if ($2 ne '') {
17289c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
17296c72ffaaSAndy Whitcroft			$res .= $coded;
17306c72ffaaSAndy Whitcroft		}
17319c0ca6f9SAndy Whitcroft	}
17329c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
17330a920b5bSAndy Whitcroft
17349c0ca6f9SAndy Whitcroft	return $res;
17350a920b5bSAndy Whitcroft}
17360a920b5bSAndy Whitcroft
1737c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1738cf655043SAndy Whitcroftmy $av_pending;
1739c2fdda0dSAndy Whitcroftmy @av_paren_type;
17401f65f947SAndy Whitcroftmy $av_pend_colon;
1741c2fdda0dSAndy Whitcroft
1742c2fdda0dSAndy Whitcroftsub annotate_reset {
1743c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1744cf655043SAndy Whitcroft	$av_pending = '_';
1745cf655043SAndy Whitcroft	@av_paren_type = ('E');
17461f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1747c2fdda0dSAndy Whitcroft}
1748c2fdda0dSAndy Whitcroft
17496c72ffaaSAndy Whitcroftsub annotate_values {
17506c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
17516c72ffaaSAndy Whitcroft
17526c72ffaaSAndy Whitcroft	my $res;
17531f65f947SAndy Whitcroft	my $var = '_' x length($stream);
17546c72ffaaSAndy Whitcroft	my $cur = $stream;
17556c72ffaaSAndy Whitcroft
1756c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
17576c72ffaaSAndy Whitcroft
17586c72ffaaSAndy Whitcroft	while (length($cur)) {
1759773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1760cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1761171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
17626c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1763c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1764c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1765cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1766c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
17676c72ffaaSAndy Whitcroft			}
17686c72ffaaSAndy Whitcroft
1769c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
17709446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
17719446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1772addcdceaSAndy Whitcroft			$type = 'c';
17739446ef56SAndy Whitcroft
1774e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1775c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
17766c72ffaaSAndy Whitcroft			$type = 'T';
17776c72ffaaSAndy Whitcroft
1778389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1779389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1780389a2fe5SAndy Whitcroft			$type = 'T';
1781389a2fe5SAndy Whitcroft
1782c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1783171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1784c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1785171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1786171ae1a4SAndy Whitcroft			if ($2 ne '') {
1787cf655043SAndy Whitcroft				$av_pending = 'N';
1788171ae1a4SAndy Whitcroft			}
1789171ae1a4SAndy Whitcroft			$type = 'E';
1790171ae1a4SAndy Whitcroft
1791c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1792171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1793171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1794171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
17956c72ffaaSAndy Whitcroft
1796c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1797cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1798c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1799cf655043SAndy Whitcroft
1800cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1801cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1802171ae1a4SAndy Whitcroft			$type = 'E';
1803cf655043SAndy Whitcroft
1804c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1805cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1806cf655043SAndy Whitcroft			$av_preprocessor = 1;
1807cf655043SAndy Whitcroft
1808cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1809cf655043SAndy Whitcroft
1810171ae1a4SAndy Whitcroft			$type = 'E';
1811cf655043SAndy Whitcroft
1812c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1813cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1814cf655043SAndy Whitcroft
1815cf655043SAndy Whitcroft			$av_preprocessor = 1;
1816cf655043SAndy Whitcroft
1817cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1818cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1819cf655043SAndy Whitcroft			pop(@av_paren_type);
1820cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1821171ae1a4SAndy Whitcroft			$type = 'E';
18226c72ffaaSAndy Whitcroft
18236c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1824c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
18256c72ffaaSAndy Whitcroft
1826171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1827171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1828171ae1a4SAndy Whitcroft			$av_pending = $type;
1829171ae1a4SAndy Whitcroft			$type = 'N';
1830171ae1a4SAndy Whitcroft
18316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1832c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
18336c72ffaaSAndy Whitcroft			if (defined $2) {
1834cf655043SAndy Whitcroft				$av_pending = 'V';
18356c72ffaaSAndy Whitcroft			}
18366c72ffaaSAndy Whitcroft			$type = 'N';
18376c72ffaaSAndy Whitcroft
183814b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1839c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
184014b111c1SAndy Whitcroft			$av_pending = 'E';
18416c72ffaaSAndy Whitcroft			$type = 'N';
18426c72ffaaSAndy Whitcroft
18431f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
18441f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
18451f65f947SAndy Whitcroft			$av_pend_colon = 'C';
18461f65f947SAndy Whitcroft			$type = 'N';
18471f65f947SAndy Whitcroft
184814b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1849c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
18506c72ffaaSAndy Whitcroft			$type = 'N';
18516c72ffaaSAndy Whitcroft
18526c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1853c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1854cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1855cf655043SAndy Whitcroft			$av_pending = '_';
18566c72ffaaSAndy Whitcroft			$type = 'N';
18576c72ffaaSAndy Whitcroft
18586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1859cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1860cf655043SAndy Whitcroft			if ($new_type ne '_') {
1861cf655043SAndy Whitcroft				$type = $new_type;
1862c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1863c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
18646c72ffaaSAndy Whitcroft			} else {
1865c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
18666c72ffaaSAndy Whitcroft			}
18676c72ffaaSAndy Whitcroft
1868c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1869c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1870c8cb2ca3SAndy Whitcroft			$type = 'V';
1871cf655043SAndy Whitcroft			$av_pending = 'V';
18726c72ffaaSAndy Whitcroft
18738e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
18748e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
18751f65f947SAndy Whitcroft				$av_pend_colon = 'B';
18768e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
18778e761b04SAndy Whitcroft				$av_pend_colon = 'L';
18781f65f947SAndy Whitcroft			}
18791f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
18801f65f947SAndy Whitcroft			$type = 'V';
18811f65f947SAndy Whitcroft
18826c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1883c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
18846c72ffaaSAndy Whitcroft			$type = 'V';
18856c72ffaaSAndy Whitcroft
18866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1887c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
18886c72ffaaSAndy Whitcroft			$type = 'N';
18896c72ffaaSAndy Whitcroft
1890cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1891c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
189213214adfSAndy Whitcroft			$type = 'E';
18931f65f947SAndy Whitcroft			$av_pend_colon = 'O';
189413214adfSAndy Whitcroft
18958e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
18968e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
18978e761b04SAndy Whitcroft			$type = 'C';
18988e761b04SAndy Whitcroft
18991f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
19001f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
19011f65f947SAndy Whitcroft			$type = 'N';
19021f65f947SAndy Whitcroft
19031f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
19041f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
19051f65f947SAndy Whitcroft
19061f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
19071f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
19081f65f947SAndy Whitcroft				$type = 'E';
19091f65f947SAndy Whitcroft			} else {
19101f65f947SAndy Whitcroft				$type = 'N';
19111f65f947SAndy Whitcroft			}
19121f65f947SAndy Whitcroft			$av_pend_colon = 'O';
19131f65f947SAndy Whitcroft
19148e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
191513214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
19166c72ffaaSAndy Whitcroft			$type = 'N';
19176c72ffaaSAndy Whitcroft
19180d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
191974048ed8SAndy Whitcroft			my $variant;
192074048ed8SAndy Whitcroft
192174048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
192274048ed8SAndy Whitcroft			if ($type eq 'V') {
192374048ed8SAndy Whitcroft				$variant = 'B';
192474048ed8SAndy Whitcroft			} else {
192574048ed8SAndy Whitcroft				$variant = 'U';
192674048ed8SAndy Whitcroft			}
192774048ed8SAndy Whitcroft
192874048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
192974048ed8SAndy Whitcroft			$type = 'N';
193074048ed8SAndy Whitcroft
19316c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1932c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
19336c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
19346c72ffaaSAndy Whitcroft				$type = 'N';
19356c72ffaaSAndy Whitcroft			}
19366c72ffaaSAndy Whitcroft
19376c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1938c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
19396c72ffaaSAndy Whitcroft		}
19406c72ffaaSAndy Whitcroft		if (defined $1) {
19416c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
19426c72ffaaSAndy Whitcroft			$res .= $type x length($1);
19436c72ffaaSAndy Whitcroft		}
19446c72ffaaSAndy Whitcroft	}
19456c72ffaaSAndy Whitcroft
19461f65f947SAndy Whitcroft	return ($res, $var);
19476c72ffaaSAndy Whitcroft}
19486c72ffaaSAndy Whitcroft
19498905a67cSAndy Whitcroftsub possible {
195013214adfSAndy Whitcroft	my ($possible, $line) = @_;
19519a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
19520776e594SAndy Whitcroft		^(?:
19530776e594SAndy Whitcroft			$Modifier|
19540776e594SAndy Whitcroft			$Storage|
19550776e594SAndy Whitcroft			$Type|
19569a974fdbSAndy Whitcroft			DEFINE_\S+
19579a974fdbSAndy Whitcroft		)$|
19589a974fdbSAndy Whitcroft		^(?:
19590776e594SAndy Whitcroft			goto|
19600776e594SAndy Whitcroft			return|
19610776e594SAndy Whitcroft			case|
19620776e594SAndy Whitcroft			else|
19630776e594SAndy Whitcroft			asm|__asm__|
196489a88353SAndy Whitcroft			do|
196589a88353SAndy Whitcroft			\#|
196689a88353SAndy Whitcroft			\#\#|
19679a974fdbSAndy Whitcroft		)(?:\s|$)|
19680776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
19699a974fdbSAndy Whitcroft	    )}x;
19709a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
19719a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1972c45dcabdSAndy Whitcroft		# Check for modifiers.
1973c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1974c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1975c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1976c45dcabdSAndy Whitcroft
1977c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1978c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1979d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
19809a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1981d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1982485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1983d2506586SAndy Whitcroft				}
19849a974fdbSAndy Whitcroft			}
1985c45dcabdSAndy Whitcroft
1986c45dcabdSAndy Whitcroft		} else {
198713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1988485ff23eSAlex Dowad			push(@typeListFile, $possible);
1989c45dcabdSAndy Whitcroft		}
19908905a67cSAndy Whitcroft		build_types();
19910776e594SAndy Whitcroft	} else {
19920776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
19938905a67cSAndy Whitcroft	}
19948905a67cSAndy Whitcroft}
19958905a67cSAndy Whitcroft
19966c72ffaaSAndy Whitcroftmy $prefix = '';
19976c72ffaaSAndy Whitcroft
1998000d1cc1SJoe Perchessub show_type {
1999cbec18afSJoe Perches	my ($type) = @_;
200091bfe484SJoe Perches
2001522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2002522b837cSAlexey Dobriyan
2003cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2004cbec18afSJoe Perches
2005cbec18afSJoe Perches	return !defined $ignore_type{$type};
2006000d1cc1SJoe Perches}
2007000d1cc1SJoe Perches
2008f0a594c1SAndy Whitcroftsub report {
2009cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2010cbec18afSJoe Perches
2011cbec18afSJoe Perches	if (!show_type($type) ||
2012cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2013773647a0SAndy Whitcroft		return 0;
2014773647a0SAndy Whitcroft	}
201557230297SJoe Perches	my $output = '';
2016737c0767SJohn Brooks	if ($color) {
201757230297SJoe Perches		if ($level eq 'ERROR') {
201857230297SJoe Perches			$output .= RED;
201957230297SJoe Perches		} elsif ($level eq 'WARNING') {
202057230297SJoe Perches			$output .= YELLOW;
2021000d1cc1SJoe Perches		} else {
202257230297SJoe Perches			$output .= GREEN;
2023000d1cc1SJoe Perches		}
202457230297SJoe Perches	}
202557230297SJoe Perches	$output .= $prefix . $level . ':';
202657230297SJoe Perches	if ($show_types) {
2027737c0767SJohn Brooks		$output .= BLUE if ($color);
202857230297SJoe Perches		$output .= "$type:";
202957230297SJoe Perches	}
2030737c0767SJohn Brooks	$output .= RESET if ($color);
203157230297SJoe Perches	$output .= ' ' . $msg . "\n";
203234d8815fSJoe Perches
203334d8815fSJoe Perches	if ($showfile) {
203434d8815fSJoe Perches		my @lines = split("\n", $output, -1);
203534d8815fSJoe Perches		splice(@lines, 1, 1);
203634d8815fSJoe Perches		$output = join("\n", @lines);
203734d8815fSJoe Perches	}
203857230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
20398905a67cSAndy Whitcroft
204057230297SJoe Perches	push(our @report, $output);
2041773647a0SAndy Whitcroft
2042773647a0SAndy Whitcroft	return 1;
2043f0a594c1SAndy Whitcroft}
2044cbec18afSJoe Perches
2045f0a594c1SAndy Whitcroftsub report_dump {
204613214adfSAndy Whitcroft	our @report;
2047f0a594c1SAndy Whitcroft}
2048000d1cc1SJoe Perches
2049d752fcc8SJoe Perchessub fixup_current_range {
2050d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2051d752fcc8SJoe Perches
2052d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2053d752fcc8SJoe Perches		my $o = $1;
2054d752fcc8SJoe Perches		my $l = $2;
2055d752fcc8SJoe Perches		my $no = $o + $offset;
2056d752fcc8SJoe Perches		my $nl = $l + $length;
2057d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2058d752fcc8SJoe Perches	}
2059d752fcc8SJoe Perches}
2060d752fcc8SJoe Perches
2061d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2062d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2063d752fcc8SJoe Perches
2064d752fcc8SJoe Perches	my $range_last_linenr = 0;
2065d752fcc8SJoe Perches	my $delta_offset = 0;
2066d752fcc8SJoe Perches
2067d752fcc8SJoe Perches	my $old_linenr = 0;
2068d752fcc8SJoe Perches	my $new_linenr = 0;
2069d752fcc8SJoe Perches
2070d752fcc8SJoe Perches	my $next_insert = 0;
2071d752fcc8SJoe Perches	my $next_delete = 0;
2072d752fcc8SJoe Perches
2073d752fcc8SJoe Perches	my @lines = ();
2074d752fcc8SJoe Perches
2075d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2076d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2077d752fcc8SJoe Perches
2078d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2079d752fcc8SJoe Perches		my $save_line = 1;
2080d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2081323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2082d752fcc8SJoe Perches			$delta_offset = 0;
2083d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2084d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2085d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2086d752fcc8SJoe Perches		}
2087d752fcc8SJoe Perches
2088d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2089d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2090d752fcc8SJoe Perches			$save_line = 0;
2091d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2092d752fcc8SJoe Perches		}
2093d752fcc8SJoe Perches
2094d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2095d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2096d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2097d752fcc8SJoe Perches			$new_linenr++;
2098d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2099d752fcc8SJoe Perches		}
2100d752fcc8SJoe Perches
2101d752fcc8SJoe Perches		if ($save_line) {
2102d752fcc8SJoe Perches			push(@lines, $line);
2103d752fcc8SJoe Perches			$new_linenr++;
2104d752fcc8SJoe Perches		}
2105d752fcc8SJoe Perches
2106d752fcc8SJoe Perches		$old_linenr++;
2107d752fcc8SJoe Perches	}
2108d752fcc8SJoe Perches
2109d752fcc8SJoe Perches	return @lines;
2110d752fcc8SJoe Perches}
2111d752fcc8SJoe Perches
2112f2d7e4d4SJoe Perchessub fix_insert_line {
2113f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2114f2d7e4d4SJoe Perches
2115f2d7e4d4SJoe Perches	my $inserted = {
2116f2d7e4d4SJoe Perches		LINENR => $linenr,
2117f2d7e4d4SJoe Perches		LINE => $line,
2118f2d7e4d4SJoe Perches	};
2119f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2120f2d7e4d4SJoe Perches}
2121f2d7e4d4SJoe Perches
2122f2d7e4d4SJoe Perchessub fix_delete_line {
2123f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2124f2d7e4d4SJoe Perches
2125f2d7e4d4SJoe Perches	my $deleted = {
2126f2d7e4d4SJoe Perches		LINENR => $linenr,
2127f2d7e4d4SJoe Perches		LINE => $line,
2128f2d7e4d4SJoe Perches	};
2129f2d7e4d4SJoe Perches
2130f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2131f2d7e4d4SJoe Perches}
2132f2d7e4d4SJoe Perches
2133de7d4f0eSAndy Whitcroftsub ERROR {
2134cbec18afSJoe Perches	my ($type, $msg) = @_;
2135cbec18afSJoe Perches
2136cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2137de7d4f0eSAndy Whitcroft		our $clean = 0;
21386c72ffaaSAndy Whitcroft		our $cnt_error++;
21393705ce5bSJoe Perches		return 1;
2140de7d4f0eSAndy Whitcroft	}
21413705ce5bSJoe Perches	return 0;
2142773647a0SAndy Whitcroft}
2143de7d4f0eSAndy Whitcroftsub WARN {
2144cbec18afSJoe Perches	my ($type, $msg) = @_;
2145cbec18afSJoe Perches
2146cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2147de7d4f0eSAndy Whitcroft		our $clean = 0;
21486c72ffaaSAndy Whitcroft		our $cnt_warn++;
21493705ce5bSJoe Perches		return 1;
2150de7d4f0eSAndy Whitcroft	}
21513705ce5bSJoe Perches	return 0;
2152773647a0SAndy Whitcroft}
2153de7d4f0eSAndy Whitcroftsub CHK {
2154cbec18afSJoe Perches	my ($type, $msg) = @_;
2155cbec18afSJoe Perches
2156cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2157de7d4f0eSAndy Whitcroft		our $clean = 0;
21586c72ffaaSAndy Whitcroft		our $cnt_chk++;
21593705ce5bSJoe Perches		return 1;
21606c72ffaaSAndy Whitcroft	}
21613705ce5bSJoe Perches	return 0;
2162de7d4f0eSAndy Whitcroft}
2163de7d4f0eSAndy Whitcroft
21646ecd9674SAndy Whitcroftsub check_absolute_file {
21656ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
21666ecd9674SAndy Whitcroft	my $file = $absolute;
21676ecd9674SAndy Whitcroft
21686ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
21696ecd9674SAndy Whitcroft
21706ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
21716ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
21726ecd9674SAndy Whitcroft		if (-f "$root/$file") {
21736ecd9674SAndy Whitcroft			##print "file<$file>\n";
21746ecd9674SAndy Whitcroft			last;
21756ecd9674SAndy Whitcroft		}
21766ecd9674SAndy Whitcroft	}
21776ecd9674SAndy Whitcroft	if (! -f _)  {
21786ecd9674SAndy Whitcroft		return 0;
21796ecd9674SAndy Whitcroft	}
21806ecd9674SAndy Whitcroft
21816ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
21826ecd9674SAndy Whitcroft	my $prefix = $absolute;
21836ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
21846ecd9674SAndy Whitcroft
21856ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
21866ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2187000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2188000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
21896ecd9674SAndy Whitcroft	}
21906ecd9674SAndy Whitcroft}
21916ecd9674SAndy Whitcroft
21923705ce5bSJoe Perchessub trim {
21933705ce5bSJoe Perches	my ($string) = @_;
21943705ce5bSJoe Perches
2195b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2196b34c648bSJoe Perches
2197b34c648bSJoe Perches	return $string;
2198b34c648bSJoe Perches}
2199b34c648bSJoe Perches
2200b34c648bSJoe Perchessub ltrim {
2201b34c648bSJoe Perches	my ($string) = @_;
2202b34c648bSJoe Perches
2203b34c648bSJoe Perches	$string =~ s/^\s+//;
2204b34c648bSJoe Perches
2205b34c648bSJoe Perches	return $string;
2206b34c648bSJoe Perches}
2207b34c648bSJoe Perches
2208b34c648bSJoe Perchessub rtrim {
2209b34c648bSJoe Perches	my ($string) = @_;
2210b34c648bSJoe Perches
2211b34c648bSJoe Perches	$string =~ s/\s+$//;
22123705ce5bSJoe Perches
22133705ce5bSJoe Perches	return $string;
22143705ce5bSJoe Perches}
22153705ce5bSJoe Perches
221652ea8506SJoe Perchessub string_find_replace {
221752ea8506SJoe Perches	my ($string, $find, $replace) = @_;
221852ea8506SJoe Perches
221952ea8506SJoe Perches	$string =~ s/$find/$replace/g;
222052ea8506SJoe Perches
222152ea8506SJoe Perches	return $string;
222252ea8506SJoe Perches}
222352ea8506SJoe Perches
22243705ce5bSJoe Perchessub tabify {
22253705ce5bSJoe Perches	my ($leading) = @_;
22263705ce5bSJoe Perches
22273705ce5bSJoe Perches	my $source_indent = 8;
22283705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
22293705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
22303705ce5bSJoe Perches
22313705ce5bSJoe Perches	#convert leading spaces to tabs
22323705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
22333705ce5bSJoe Perches	#Remove spaces before a tab
22343705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
22353705ce5bSJoe Perches
22363705ce5bSJoe Perches	return "$leading";
22373705ce5bSJoe Perches}
22383705ce5bSJoe Perches
2239d1fe9c09SJoe Perchessub pos_last_openparen {
2240d1fe9c09SJoe Perches	my ($line) = @_;
2241d1fe9c09SJoe Perches
2242d1fe9c09SJoe Perches	my $pos = 0;
2243d1fe9c09SJoe Perches
2244d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2245d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2246d1fe9c09SJoe Perches
2247d1fe9c09SJoe Perches	my $last_openparen = 0;
2248d1fe9c09SJoe Perches
2249d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2250d1fe9c09SJoe Perches		return -1;
2251d1fe9c09SJoe Perches	}
2252d1fe9c09SJoe Perches
2253d1fe9c09SJoe Perches	my $len = length($line);
2254d1fe9c09SJoe Perches
2255d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2256d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2257d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2258d1fe9c09SJoe Perches			$pos += length($1) - 1;
2259d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2260d1fe9c09SJoe Perches			$last_openparen = $pos;
2261d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2262d1fe9c09SJoe Perches			last;
2263d1fe9c09SJoe Perches		}
2264d1fe9c09SJoe Perches	}
2265d1fe9c09SJoe Perches
226691cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2267d1fe9c09SJoe Perches}
2268d1fe9c09SJoe Perches
22690a920b5bSAndy Whitcroftsub process {
22700a920b5bSAndy Whitcroft	my $filename = shift;
22710a920b5bSAndy Whitcroft
22720a920b5bSAndy Whitcroft	my $linenr=0;
22730a920b5bSAndy Whitcroft	my $prevline="";
2274c2fdda0dSAndy Whitcroft	my $prevrawline="";
22750a920b5bSAndy Whitcroft	my $stashline="";
2276c2fdda0dSAndy Whitcroft	my $stashrawline="";
22770a920b5bSAndy Whitcroft
22784a0df2efSAndy Whitcroft	my $length;
22790a920b5bSAndy Whitcroft	my $indent;
22800a920b5bSAndy Whitcroft	my $previndent=0;
22810a920b5bSAndy Whitcroft	my $stashindent=0;
22820a920b5bSAndy Whitcroft
2283de7d4f0eSAndy Whitcroft	our $clean = 1;
22840a920b5bSAndy Whitcroft	my $signoff = 0;
2285cd261496SGeert Uytterhoeven	my $author = '';
2286cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
22870a920b5bSAndy Whitcroft	my $is_patch = 0;
2288133712a2SRob Herring	my $is_binding_patch = -1;
228929ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
229015662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2291ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2292490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2293bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
22942a076f40SJoe Perches	my $commit_log_long_line = 0;
2295e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
229613f1937eSJoe Perches	my $reported_maintainer_file = 0;
2297fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2298fa64205dSPasi Savanainen
2299365dd4eaSJoe Perches	my $last_blank_line = 0;
23005e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2301365dd4eaSJoe Perches
230213214adfSAndy Whitcroft	our @report = ();
23036c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
23046c72ffaaSAndy Whitcroft	our $cnt_error = 0;
23056c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
23066c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
23076c72ffaaSAndy Whitcroft
23080a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
23090a920b5bSAndy Whitcroft	my $realfile = '';
23100a920b5bSAndy Whitcroft	my $realline = 0;
23110a920b5bSAndy Whitcroft	my $realcnt = 0;
23120a920b5bSAndy Whitcroft	my $here = '';
231377cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
23140a920b5bSAndy Whitcroft	my $in_comment = 0;
2315c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
23160a920b5bSAndy Whitcroft	my $first_line = 0;
23171e855726SWolfram Sang	my $p1_prefix = '';
23180a920b5bSAndy Whitcroft
231913214adfSAndy Whitcroft	my $prev_values = 'E';
232013214adfSAndy Whitcroft
232113214adfSAndy Whitcroft	# suppression flags
2322773647a0SAndy Whitcroft	my %suppress_ifbraces;
2323170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
23242b474a1aSAndy Whitcroft	my %suppress_export;
23253e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2326653d4876SAndy Whitcroft
23277e51f197SJoe Perches	my %signatures = ();
2328323c1260SJoe Perches
2329c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2330de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2331c2fdda0dSAndy Whitcroft	#
2332de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2333de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2334773647a0SAndy Whitcroft
2335d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2336d8b07710SJoe Perches
23379f3a8992SRob Herring	my $checklicenseline = 1;
23389f3a8992SRob Herring
2339773647a0SAndy Whitcroft	sanitise_line_reset();
2340c2fdda0dSAndy Whitcroft	my $line;
2341c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2342773647a0SAndy Whitcroft		$linenr++;
2343773647a0SAndy Whitcroft		$line = $rawline;
2344c2fdda0dSAndy Whitcroft
23453705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
23463705ce5bSJoe Perches
2347773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2348de7d4f0eSAndy Whitcroft			$setup_docs = 0;
23498c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2350de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2351de7d4f0eSAndy Whitcroft			}
2352773647a0SAndy Whitcroft			#next;
2353de7d4f0eSAndy Whitcroft		}
235474fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2355773647a0SAndy Whitcroft			$realline=$1-1;
2356773647a0SAndy Whitcroft			if (defined $2) {
2357773647a0SAndy Whitcroft				$realcnt=$3+1;
2358773647a0SAndy Whitcroft			} else {
2359773647a0SAndy Whitcroft				$realcnt=1+1;
2360773647a0SAndy Whitcroft			}
2361c45dcabdSAndy Whitcroft			$in_comment = 0;
2362773647a0SAndy Whitcroft
2363773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2364773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2365773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2366773647a0SAndy Whitcroft			# at context start.
2367773647a0SAndy Whitcroft			my $edge;
236801fa9147SAndy Whitcroft			my $cnt = $realcnt;
236901fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
237001fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
237101fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
237201fa9147SAndy Whitcroft				$cnt--;
237301fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2374721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2375fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2376fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2377fae17daeSAndy Whitcroft					($edge) = $1;
2378fae17daeSAndy Whitcroft					last;
2379fae17daeSAndy Whitcroft				}
2380773647a0SAndy Whitcroft			}
2381773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2382773647a0SAndy Whitcroft				$in_comment = 1;
2383773647a0SAndy Whitcroft			}
2384773647a0SAndy Whitcroft
2385773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2386773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2387773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2388773647a0SAndy Whitcroft			if (!defined $edge &&
238983242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2390773647a0SAndy Whitcroft			{
2391773647a0SAndy Whitcroft				$in_comment = 1;
2392773647a0SAndy Whitcroft			}
2393773647a0SAndy Whitcroft
2394773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2395773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2396773647a0SAndy Whitcroft
2397171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2398773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2399171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2400773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2401773647a0SAndy Whitcroft		}
2402773647a0SAndy Whitcroft		push(@lines, $line);
2403773647a0SAndy Whitcroft
2404773647a0SAndy Whitcroft		if ($realcnt > 1) {
2405773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2406773647a0SAndy Whitcroft		} else {
2407773647a0SAndy Whitcroft			$realcnt = 0;
2408773647a0SAndy Whitcroft		}
2409773647a0SAndy Whitcroft
2410773647a0SAndy Whitcroft		#print "==>$rawline\n";
2411773647a0SAndy Whitcroft		#print "-->$line\n";
2412de7d4f0eSAndy Whitcroft
2413de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2414de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2415de7d4f0eSAndy Whitcroft		}
2416de7d4f0eSAndy Whitcroft	}
2417de7d4f0eSAndy Whitcroft
24186c72ffaaSAndy Whitcroft	$prefix = '';
24196c72ffaaSAndy Whitcroft
2420773647a0SAndy Whitcroft	$realcnt = 0;
2421773647a0SAndy Whitcroft	$linenr = 0;
2422194f66fcSJoe Perches	$fixlinenr = -1;
24230a920b5bSAndy Whitcroft	foreach my $line (@lines) {
24240a920b5bSAndy Whitcroft		$linenr++;
2425194f66fcSJoe Perches		$fixlinenr++;
24261b5539b1SJoe Perches		my $sline = $line;	#copy of $line
24271b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
24280a920b5bSAndy Whitcroft
2429c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
24306c72ffaaSAndy Whitcroft
243112c253abSJoe Perches# check if it's a mode change, rename or start of a patch
243212c253abSJoe Perches		if (!$in_commit_log &&
243312c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
243412c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
243512c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
243612c253abSJoe Perches			$is_patch = 1;
243712c253abSJoe Perches		}
243812c253abSJoe Perches
24390a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2440e518e9a5SJoe Perches		if (!$in_commit_log &&
244174fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
244274fd4f34SJoe Perches			my $context = $4;
24430a920b5bSAndy Whitcroft			$is_patch = 1;
24444a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
24450a920b5bSAndy Whitcroft			$realline=$1-1;
24460a920b5bSAndy Whitcroft			if (defined $2) {
24470a920b5bSAndy Whitcroft				$realcnt=$3+1;
24480a920b5bSAndy Whitcroft			} else {
24490a920b5bSAndy Whitcroft				$realcnt=1+1;
24500a920b5bSAndy Whitcroft			}
2451c2fdda0dSAndy Whitcroft			annotate_reset();
245213214adfSAndy Whitcroft			$prev_values = 'E';
245313214adfSAndy Whitcroft
2454773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2455170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
24562b474a1aSAndy Whitcroft			%suppress_export = ();
24573e469cdcSAndy Whitcroft			$suppress_statement = 0;
245874fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
245974fd4f34SJoe Perches				$context_function = $1;
246074fd4f34SJoe Perches			} else {
246174fd4f34SJoe Perches				undef $context_function;
246274fd4f34SJoe Perches			}
24630a920b5bSAndy Whitcroft			next;
24640a920b5bSAndy Whitcroft
24654a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
24664a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
24674a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2468773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
24690a920b5bSAndy Whitcroft			$realline++;
2470d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
24710a920b5bSAndy Whitcroft
24724a0df2efSAndy Whitcroft			# Measure the line length and indent.
2473c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
24740a920b5bSAndy Whitcroft
24750a920b5bSAndy Whitcroft			# Track the previous line.
24760a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
24770a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2478c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2479c2fdda0dSAndy Whitcroft
2480773647a0SAndy Whitcroft			#warn "line<$line>\n";
24816c72ffaaSAndy Whitcroft
2482d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2483d8aaf121SAndy Whitcroft			$realcnt--;
24840a920b5bSAndy Whitcroft		}
24850a920b5bSAndy Whitcroft
2486cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2487cc77cdcaSAndy Whitcroft
24886c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
24896c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2490773647a0SAndy Whitcroft
24912ac73b4fSJoe Perches		my $found_file = 0;
2492773647a0SAndy Whitcroft		# extract the filename as it passes
24933bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
24943bf9a009SRabin Vincent			$realfile = $1;
24952b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2496270c49a0SJoe Perches			$in_commit_log = 0;
24972ac73b4fSJoe Perches			$found_file = 1;
24983bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2499773647a0SAndy Whitcroft			$realfile = $1;
25002b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2501270c49a0SJoe Perches			$in_commit_log = 0;
25021e855726SWolfram Sang
25031e855726SWolfram Sang			$p1_prefix = $1;
2504e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2505e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2506000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2507000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
25081e855726SWolfram Sang			}
2509773647a0SAndy Whitcroft
2510c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2511000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2512000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2513773647a0SAndy Whitcroft			}
25142ac73b4fSJoe Perches			$found_file = 1;
25152ac73b4fSJoe Perches		}
25162ac73b4fSJoe Perches
251734d8815fSJoe Perches#make up the handle for any error we report on this line
251834d8815fSJoe Perches		if ($showfile) {
251934d8815fSJoe Perches			$prefix = "$realfile:$realline: "
252034d8815fSJoe Perches		} elsif ($emacs) {
25217d3a9f67SJoe Perches			if ($file) {
25227d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
25237d3a9f67SJoe Perches			} else {
252434d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
252534d8815fSJoe Perches			}
25267d3a9f67SJoe Perches		}
252734d8815fSJoe Perches
25282ac73b4fSJoe Perches		if ($found_file) {
252985b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
253085b0ee18SJoe Perches				WARN("OBSOLETE",
253185b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
253285b0ee18SJoe Perches			}
25337bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
25342ac73b4fSJoe Perches				$check = 1;
25352ac73b4fSJoe Perches			} else {
25362ac73b4fSJoe Perches				$check = $check_orig;
25372ac73b4fSJoe Perches			}
25389f3a8992SRob Herring			$checklicenseline = 1;
2539133712a2SRob Herring
2540133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2541133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2542133712a2SRob Herring
2543133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2544133712a2SRob Herring
2545133712a2SRob Herring				if (($last_binding_patch != -1) &&
2546133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2547133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2548133712a2SRob Herring					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2549133712a2SRob Herring				}
2550133712a2SRob Herring			}
2551133712a2SRob Herring
2552773647a0SAndy Whitcroft			next;
2553773647a0SAndy Whitcroft		}
2554773647a0SAndy Whitcroft
2555389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
25560a920b5bSAndy Whitcroft
2557c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2558c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2559c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
25600a920b5bSAndy Whitcroft
25616c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
25626c72ffaaSAndy Whitcroft
2563490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2564490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2565490b292cSJoe Perches		if ($in_commit_log) {
2566490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2567490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2568490b292cSJoe Perches			}
2569490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2570490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2571490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2572490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2573490b292cSJoe Perches		}
2574490b292cSJoe Perches
2575e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2576e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2577e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2578e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2579e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2580e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2581e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2582e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2583e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2584e518e9a5SJoe Perches		}
2585e518e9a5SJoe Perches
25863bf9a009SRabin Vincent# Check for incorrect file permissions
25873bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
25883bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
258904db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
259004db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2591000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2592000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
25933bf9a009SRabin Vincent			}
25943bf9a009SRabin Vincent		}
25953bf9a009SRabin Vincent
2596cd261496SGeert Uytterhoeven# Check the patch for a From:
2597cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2598cd261496SGeert Uytterhoeven			$author = $1;
2599cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2600cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2601cd261496SGeert Uytterhoeven		}
2602cd261496SGeert Uytterhoeven
260320112475SJoe Perches# Check the patch for a signoff:
2604d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
26054a0df2efSAndy Whitcroft			$signoff++;
260615662b3eSJoe Perches			$in_commit_log = 0;
2607cd261496SGeert Uytterhoeven			if ($author ne '') {
2608cd261496SGeert Uytterhoeven				my $l = $line;
2609cd261496SGeert Uytterhoeven				$l =~ s/"//g;
2610cd261496SGeert Uytterhoeven				if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2611cd261496SGeert Uytterhoeven				    $authorsignoff = 1;
2612cd261496SGeert Uytterhoeven				}
2613cd261496SGeert Uytterhoeven			}
26140a920b5bSAndy Whitcroft		}
261520112475SJoe Perches
2616e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2617e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2618e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2619e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2620e0d975b1SJoe Perches		}
2621e0d975b1SJoe Perches
262220112475SJoe Perches# Check signature styles
2623270c49a0SJoe Perches		if (!$in_header_lines &&
2624ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
262520112475SJoe Perches			my $space_before = $1;
262620112475SJoe Perches			my $sign_off = $2;
262720112475SJoe Perches			my $space_after = $3;
262820112475SJoe Perches			my $email = $4;
262920112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
263020112475SJoe Perches
2631ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2632ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2633ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2634ce0338dfSJoe Perches			}
263520112475SJoe Perches			if (defined $space_before && $space_before ne "") {
26363705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26373705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
26383705ce5bSJoe Perches				    $fix) {
2639194f66fcSJoe Perches					$fixed[$fixlinenr] =
26403705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26413705ce5bSJoe Perches				}
264220112475SJoe Perches			}
264320112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
26443705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26453705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
26463705ce5bSJoe Perches				    $fix) {
2647194f66fcSJoe Perches					$fixed[$fixlinenr] =
26483705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26493705ce5bSJoe Perches				}
26503705ce5bSJoe Perches
265120112475SJoe Perches			}
265220112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
26533705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26543705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
26553705ce5bSJoe Perches				    $fix) {
2656194f66fcSJoe Perches					$fixed[$fixlinenr] =
26573705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26583705ce5bSJoe Perches				}
265920112475SJoe Perches			}
266020112475SJoe Perches
266120112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
266220112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
266320112475SJoe Perches			if ($suggested_email eq "") {
2664000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2665000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
266620112475SJoe Perches			} else {
266720112475SJoe Perches				my $dequoted = $suggested_email;
266820112475SJoe Perches				$dequoted =~ s/^"//;
266920112475SJoe Perches				$dequoted =~ s/" </ </;
267020112475SJoe Perches				# Don't force email to have quotes
267120112475SJoe Perches				# Allow just an angle bracketed address
267220112475SJoe Perches				if ("$dequoted$comment" ne $email &&
267320112475SJoe Perches				    "<$email_address>$comment" ne $email &&
267420112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2675000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2676000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
267720112475SJoe Perches				}
26780a920b5bSAndy Whitcroft			}
26797e51f197SJoe Perches
26807e51f197SJoe Perches# Check for duplicate signatures
26817e51f197SJoe Perches			my $sig_nospace = $line;
26827e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
26837e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
26847e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
26857e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
26867e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
26877e51f197SJoe Perches			} else {
26887e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
26897e51f197SJoe Perches			}
26906c5d24eeSSean Christopherson
26916c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
26926c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
26936c5d24eeSSean Christopherson				if ($email eq $author) {
26946c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
26956c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
26966c5d24eeSSean Christopherson				}
26976c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
26986c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
26996c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
27006c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
27016c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27026c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27036c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
27046c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27056c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27066c5d24eeSSean Christopherson				}
27076c5d24eeSSean Christopherson			}
27080a920b5bSAndy Whitcroft		}
27090a920b5bSAndy Whitcroft
2710a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2711a2fe16b9SJoe Perches		if ($in_header_lines &&
2712a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2713a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2714a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2715a2fe16b9SJoe Perches		}
2716a2fe16b9SJoe Perches
27177ebd05efSChristopher Covington# Check for unwanted Gerrit info
27187ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
27197ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
27207ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
27217ebd05efSChristopher Covington		}
27227ebd05efSChristopher Covington
2723369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2724369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2725369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2726369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2727369c8dd3SJoe Perches					# timestamp
2728634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2729634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2730634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2731634cffccSJoe Perches					# stack dump address styles
2732369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2733369c8dd3SJoe Perches		}
2734369c8dd3SJoe Perches
27352a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
27362a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2737bf4daf12SJoe Perches		    length($line) > 75 &&
2738bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2739bf4daf12SJoe Perches					# file delta changes
2740bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2741bf4daf12SJoe Perches					# filename then :
2742bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2743bf4daf12SJoe Perches					# A Fixes: or Link: line
2744bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
27452a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
27462a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
27472a076f40SJoe Perches			$commit_log_long_line = 1;
27482a076f40SJoe Perches		}
27492a076f40SJoe Perches
2750bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2751bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2752bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2753bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2754bf4daf12SJoe Perches		}
2755bf4daf12SJoe Perches
27560d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2757369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2758aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2759e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2760fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2761aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2762369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2763bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2764fe043ea1SJoe Perches			my $init_char = "c";
2765fe043ea1SJoe Perches			my $orig_commit = "";
27660d7835fcSJoe Perches			my $short = 1;
27670d7835fcSJoe Perches			my $long = 0;
27680d7835fcSJoe Perches			my $case = 1;
27690d7835fcSJoe Perches			my $space = 1;
27700d7835fcSJoe Perches			my $hasdesc = 0;
277119c146a6SJoe Perches			my $hasparens = 0;
27720d7835fcSJoe Perches			my $id = '0123456789ab';
27730d7835fcSJoe Perches			my $orig_desc = "commit description";
27740d7835fcSJoe Perches			my $description = "";
27750d7835fcSJoe Perches
2776fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2777fe043ea1SJoe Perches				$init_char = $1;
2778fe043ea1SJoe Perches				$orig_commit = lc($2);
2779fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2780fe043ea1SJoe Perches				$orig_commit = lc($1);
2781fe043ea1SJoe Perches			}
2782fe043ea1SJoe Perches
27830d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
27840d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
27850d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
27860d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
27870d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
27880d7835fcSJoe Perches				$orig_desc = $1;
278919c146a6SJoe Perches				$hasparens = 1;
27900d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
27910d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
27920d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
27930d7835fcSJoe Perches				$orig_desc = $1;
279419c146a6SJoe Perches				$hasparens = 1;
2795b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2796b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2797b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2798b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2799b671fde0SJoe Perches				$orig_desc = $1;
2800b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2801b671fde0SJoe Perches				$orig_desc .= " " . $1;
280219c146a6SJoe Perches				$hasparens = 1;
28030d7835fcSJoe Perches			}
28040d7835fcSJoe Perches
28050d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
28060d7835fcSJoe Perches							      $id, $orig_desc);
28070d7835fcSJoe Perches
2808948b133aSHeinrich Schuchardt			if (defined($id) &&
2809948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2810d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
28110d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
28120d7835fcSJoe Perches			}
2813d311cd44SJoe Perches		}
2814d311cd44SJoe Perches
281513f1937eSJoe Perches# Check for added, moved or deleted files
281613f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
281713f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
281813f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
281913f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
282013f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2821a82603a8SAndrew Jeffery			$is_patch = 1;
282213f1937eSJoe Perches			$reported_maintainer_file = 1;
282313f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
282413f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
282513f1937eSJoe Perches		}
282613f1937eSJoe Perches
282700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
28288905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2829000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2830000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
28316c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2832de7d4f0eSAndy Whitcroft		}
2833de7d4f0eSAndy Whitcroft
2834de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2835de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2836171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2837171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2838171ae1a4SAndy Whitcroft
2839171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2840171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2841171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2842171ae1a4SAndy Whitcroft
284334d99219SJoe Perches			CHK("INVALID_UTF8",
2844000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
284500df344fSAndy Whitcroft		}
28460a920b5bSAndy Whitcroft
284715662b3eSJoe Perches# Check if it's the start of a commit log
284815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
284915662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2850eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2851eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
285215662b3eSJoe Perches			$in_header_lines = 0;
285315662b3eSJoe Perches			$in_commit_log = 1;
2854ed43c4e5SAllen Hubbe			$has_commit_log = 1;
285515662b3eSJoe Perches		}
285615662b3eSJoe Perches
2857fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2858fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2859fa64205dSPasi Savanainen		if ($in_header_lines &&
2860fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2861fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2862fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2863fa64205dSPasi Savanainen		}
2864fa64205dSPasi Savanainen
2865fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
286615662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2867fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
286815662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
286915662b3eSJoe Perches		}
287015662b3eSJoe Perches
2871d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2872d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2873d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2874d6430f71SJoe Perches				my $file = $1;
2875d6430f71SJoe Perches
2876d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2877d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2878d6430f71SJoe Perches					#
2879d6430f71SJoe Perches				} else {
2880d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2881d6430f71SJoe Perches				}
2882d6430f71SJoe Perches			}
2883d6430f71SJoe Perches		}
2884d6430f71SJoe Perches
288566b47b4aSKees Cook# Check for various typo / spelling mistakes
288666d7a382SJoe Perches		if (defined($misspellings) &&
288766d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2888ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
288966b47b4aSKees Cook				my $typo = $1;
289066b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
289166b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
289266b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
28930675a8fbSJean Delvare				my $msg_level = \&WARN;
28940675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
28950675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
289666b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
289766b47b4aSKees Cook				    $fix) {
289866b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
289966b47b4aSKees Cook				}
290066b47b4aSKees Cook			}
290166b47b4aSKees Cook		}
290266b47b4aSKees Cook
290330670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
290430670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
290500df344fSAndy Whitcroft
29060a920b5bSAndy Whitcroft#trailing whitespace
29079c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2908c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2909d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2910d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2911d5e616fcSJoe Perches			    $fix) {
2912194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2913d5e616fcSJoe Perches			}
2914c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2915c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29163705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
29173705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
29183705ce5bSJoe Perches			    $fix) {
2919194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
29203705ce5bSJoe Perches			}
29213705ce5bSJoe Perches
2922d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29230a920b5bSAndy Whitcroft		}
29245368df20SAndy Whitcroft
29254783f894SJosh Triplett# Check for FSF mailing addresses.
2926109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
29271bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
29283e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
29293e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
29304783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29310675a8fbSJean Delvare			my $msg_level = \&ERROR;
29320675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
29330675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
29344783f894SJosh 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)
29354783f894SJosh Triplett		}
29364783f894SJosh Triplett
29373354957aSAndi Kleen# check for Kconfig help text having a real description
29389fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
29399fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
29403354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
2941678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
2942678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
2943678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
2944678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
29453354957aSAndi Kleen			my $length = 0;
29469fe287d7SAndy Whitcroft			my $cnt = $realcnt;
29479fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
29489fe287d7SAndy Whitcroft			my $f;
2949a1385803SAndy Whitcroft			my $is_start = 0;
29509fe287d7SAndy Whitcroft			my $is_end = 0;
2951a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
29529fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
29539fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
29549fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
29559fe287d7SAndy Whitcroft
29569fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
29578d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2958a1385803SAndy Whitcroft
295986adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
2960a1385803SAndy Whitcroft					$is_start = 1;
296184af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
296284af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
296384af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
296484af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
296584af7a61SUlf Magnusson					}
2966a1385803SAndy Whitcroft					$length = -1;
2967a1385803SAndy Whitcroft				}
2968a1385803SAndy Whitcroft
29699fe287d7SAndy Whitcroft				$f =~ s/^.//;
29703354957aSAndi Kleen				$f =~ s/#.*//;
29713354957aSAndi Kleen				$f =~ s/^\s+//;
29723354957aSAndi Kleen				next if ($f =~ /^$/);
2973678ae162SUlf Magnusson
2974678ae162SUlf Magnusson				# This only checks context lines in the patch
2975678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
2976678ae162SUlf Magnusson				# positives, even though some of these are
2977678ae162SUlf Magnusson				# common words in help texts
2978678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
2979678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
29809fe287d7SAndy Whitcroft					$is_end = 1;
29819fe287d7SAndy Whitcroft					last;
29829fe287d7SAndy Whitcroft				}
29833354957aSAndi Kleen				$length++;
29843354957aSAndi Kleen			}
298556193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2986000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
298756193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
298856193274SVadim Bendebury			}
2989a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
29903354957aSAndi Kleen		}
29913354957aSAndi Kleen
2992628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
2993628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
2994628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
2995628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
2996628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
2997628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2998628f91a2SJoe Perches			    $fix) {
2999628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3000628f91a2SJoe Perches			}
3001628f91a2SJoe Perches		}
3002628f91a2SJoe Perches
3003327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3004327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3005327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3006327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3007327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3008327953e9SChristoph Jaeger		}
3009327953e9SChristoph Jaeger
3010c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3011c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3012c68e5878SArnaud Lacombe			my $flag = $1;
3013c68e5878SArnaud Lacombe			my $replacement = {
3014c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3015c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3016c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3017c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3018c68e5878SArnaud Lacombe			};
3019c68e5878SArnaud Lacombe
3020c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3021c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3022c68e5878SArnaud Lacombe		}
3023c68e5878SArnaud Lacombe
3024bff5da43SRob Herring# check for DT compatible documentation
30257dd05b38SFlorian Vaussard		if (defined $root &&
30267dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
30277dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
30287dd05b38SFlorian Vaussard
3029bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3030bff5da43SRob Herring
3031cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3032852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3033cc93319bSFlorian Vaussard
3034bff5da43SRob Herring			foreach my $compat (@compats) {
3035bff5da43SRob Herring				my $compat2 = $compat;
3036185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3037185d566bSRob Herring				my $compat3 = $compat;
3038185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3039185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3040bff5da43SRob Herring				if ( $? >> 8 ) {
3041bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3042bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3043bff5da43SRob Herring				}
3044bff5da43SRob Herring
30454fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
30464fbf32a6SFlorian Vaussard				my $vendor = $1;
3047852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3048bff5da43SRob Herring				if ( $? >> 8 ) {
3049bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3050cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3051bff5da43SRob Herring				}
3052bff5da43SRob Herring			}
3053bff5da43SRob Herring		}
3054bff5da43SRob Herring
30559f3a8992SRob Herring# check for using SPDX license tag at beginning of files
30569f3a8992SRob Herring		if ($realline == $checklicenseline) {
30579f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
30589f3a8992SRob Herring				$checklicenseline = 2;
30599f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
30609f3a8992SRob Herring				my $comment = "";
30619f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
30629f3a8992SRob Herring					$comment = '/*';
30639f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
30649f3a8992SRob Herring					$comment = '//';
30659f3a8992SRob Herring				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
30669f3a8992SRob Herring					$comment = '#';
30679f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
30689f3a8992SRob Herring					$comment = '..';
30699f3a8992SRob Herring				}
30709f3a8992SRob Herring
3071fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3072fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3073fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3074*ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3075fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3076fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3077fdf13693SJoe Perches				}
3078fdf13693SJoe Perches
30799f3a8992SRob Herring				if ($comment !~ /^$/ &&
3080*ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
30819f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
30829f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
30833b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
30843b6e8ac9SJoe Perches					my $spdx_license = $1;
30853b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
30863b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
30873b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
30883b6e8ac9SJoe Perches					}
30899f3a8992SRob Herring				}
30909f3a8992SRob Herring			}
30919f3a8992SRob Herring		}
30929f3a8992SRob Herring
30935368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3094d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
30955368df20SAndy Whitcroft
3096a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3097a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3098a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3099a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3100a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3101a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3102a8da38a9SJoe Perches		}
3103a8da38a9SJoe Perches
310447e0c88bSJoe Perches# line length limit (with some exclusions)
310547e0c88bSJoe Perches#
310647e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
310747e0c88bSJoe Perches#	logging functions like pr_info that end in a string
310847e0c88bSJoe Perches#	lines with a single string
310947e0c88bSJoe Perches#	#defines that are a single string
31102e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
311147e0c88bSJoe Perches#
311247e0c88bSJoe Perches# There are 3 different line length message types:
3113ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
311447e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
311547e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
311647e0c88bSJoe Perches#
311747e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
311847e0c88bSJoe Perches#
311947e0c88bSJoe Perches
3120b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
312147e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
312247e0c88bSJoe Perches
312347e0c88bSJoe Perches			# Check the allowed long line types first
312447e0c88bSJoe Perches
312547e0c88bSJoe Perches			# logging functions that end in a string that starts
312647e0c88bSJoe Perches			# before $max_line_length
312747e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
312847e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
312947e0c88bSJoe Perches				$msg_type = "";
313047e0c88bSJoe Perches
313147e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
313247e0c88bSJoe Perches			# #defines with only strings
313347e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
313447e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
313547e0c88bSJoe Perches				$msg_type = "";
313647e0c88bSJoe Perches
3137cc147506SJoe Perches			# More special cases
3138cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3139cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3140d560a5f8SJoe Perches				$msg_type = "";
3141d560a5f8SJoe Perches
31422e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
31432e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
31442e4bbbc5SAndreas Brauchli				$msg_type = "";
31452e4bbbc5SAndreas Brauchli
314647e0c88bSJoe Perches			# Otherwise set the alternate message types
314747e0c88bSJoe Perches
314847e0c88bSJoe Perches			# a comment starts before $max_line_length
314947e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
315047e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
315147e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
315247e0c88bSJoe Perches
315347e0c88bSJoe Perches			# a quoted string starts before $max_line_length
315447e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
315547e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
315647e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
315747e0c88bSJoe Perches			}
315847e0c88bSJoe Perches
315947e0c88bSJoe Perches			if ($msg_type ne "" &&
316047e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
316147e0c88bSJoe Perches				WARN($msg_type,
31626cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
31630a920b5bSAndy Whitcroft			}
316447e0c88bSJoe Perches		}
31650a920b5bSAndy Whitcroft
31668905a67cSAndy Whitcroft# check for adding lines without a newline.
31678905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3168000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3169000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
31708905a67cSAndy Whitcroft		}
31718905a67cSAndy Whitcroft
3172b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3173de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
31740a920b5bSAndy Whitcroft
31750a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
31760a920b5bSAndy Whitcroft# more than 8 must use tabs.
3177c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3178c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3179c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3180d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
31813705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
31823705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
31833705ce5bSJoe Perches			    $fix) {
3184194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
31853705ce5bSJoe Perches			}
31860a920b5bSAndy Whitcroft		}
31870a920b5bSAndy Whitcroft
318808e44365SAlberto Panizzo# check for space before tabs.
318908e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
319008e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
31913705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
31923705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
31933705ce5bSJoe Perches			    $fix) {
3194194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3195d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
3196194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3197c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
31983705ce5bSJoe Perches			}
319908e44365SAlberto Panizzo		}
320008e44365SAlberto Panizzo
32016a487211SJoe Perches# check for assignments on the start of a line
32026a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
32036a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
32046a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
32056a487211SJoe Perches		}
32066a487211SJoe Perches
3207d1fe9c09SJoe Perches# check for && or || at the start of a line
3208d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3209d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3210d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3211d1fe9c09SJoe Perches		}
3212d1fe9c09SJoe Perches
3213a91e8994SJoe Perches# check indentation starts on a tab stop
32145b57980dSJoe Perches		if ($perl_version_ok &&
3215bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3216a91e8994SJoe Perches			my $indent = length($1);
3217a91e8994SJoe Perches			if ($indent % 8) {
3218a91e8994SJoe Perches				if (WARN("TABSTOP",
3219a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3220a91e8994SJoe Perches				    $fix) {
3221a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3222a91e8994SJoe Perches				}
3223a91e8994SJoe Perches			}
3224a91e8994SJoe Perches		}
3225a91e8994SJoe Perches
3226d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
32275b57980dSJoe Perches		if ($perl_version_ok &&
3228fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3229d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3230d1fe9c09SJoe Perches			my $oldindent = $1;
3231d1fe9c09SJoe Perches			my $rest = $2;
3232d1fe9c09SJoe Perches
3233d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3234d1fe9c09SJoe Perches			if ($pos >= 0) {
3235b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3236b34a26f3SJoe Perches				my $newindent = $2;
3237d1fe9c09SJoe Perches
3238d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3239d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3240d1fe9c09SJoe Perches					" "  x ($pos % 8);
3241d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3242d1fe9c09SJoe Perches
3243d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3244d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
32453705ce5bSJoe Perches
32463705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
32473705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
32483705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3249194f66fcSJoe Perches						$fixed[$fixlinenr] =~
32503705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
32513705ce5bSJoe Perches					}
3252d1fe9c09SJoe Perches				}
3253d1fe9c09SJoe Perches			}
3254d1fe9c09SJoe Perches		}
3255d1fe9c09SJoe Perches
32566ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
32576ab3a970SJoe Perches# avoid checking a few false positives:
32586ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
32596ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
32606ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
32616ab3a970SJoe Perches#   multiline macros that define functions
32626ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
32636ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
32646ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
32653705ce5bSJoe Perches			if (CHK("SPACING",
3266f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
32673705ce5bSJoe Perches			    $fix) {
3268194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3269f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
32703705ce5bSJoe Perches			}
3271aad4f614SJoe Perches		}
3272aad4f614SJoe Perches
327386406b1cSJoe Perches# Block comment styles
327486406b1cSJoe Perches# Networking with an initial /*
327505880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3276fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
327785ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
327885ad978cSJoe Perches		    $realline > 2) {
327905880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
328005880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
328105880600SJoe Perches		}
328205880600SJoe Perches
328386406b1cSJoe Perches# Block comments use * on subsequent lines
328486406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
328586406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3286a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
328761135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3288a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
328986406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
329086406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3291a605e32eSJoe Perches		}
3292a605e32eSJoe Perches
329386406b1cSJoe Perches# Block comments use */ on trailing lines
329486406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3295c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3296c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3297c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
329886406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
329986406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
330005880600SJoe Perches		}
330105880600SJoe Perches
330208eb9b80SJoe Perches# Block comment * alignment
330308eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3304af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3305af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3306af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
330708eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3308af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3309af207524SJoe Perches			my $oldindent;
331008eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3311af207524SJoe Perches			if (defined($1)) {
3312af207524SJoe Perches				$oldindent = expand_tabs($1);
3313af207524SJoe Perches			} else {
3314af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3315af207524SJoe Perches				$oldindent = expand_tabs($1);
3316af207524SJoe Perches			}
331708eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
331808eb9b80SJoe Perches			my $newindent = $1;
331908eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3320af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
332108eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
332208eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
332308eb9b80SJoe Perches			}
332408eb9b80SJoe Perches		}
332508eb9b80SJoe Perches
33267f619191SJoe Perches# check for missing blank lines after struct/union declarations
33277f619191SJoe Perches# with exceptions for various attributes and macros
33287f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
33297f619191SJoe Perches		    $line =~ /^\+/ &&
33307f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
33317f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
33327f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
33337f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
33347f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
33357f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
33367f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
33370bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
33387f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3339d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3340d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3341d752fcc8SJoe Perches			    $fix) {
3342f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3343d752fcc8SJoe Perches			}
33447f619191SJoe Perches		}
33457f619191SJoe Perches
3346365dd4eaSJoe Perches# check for multiple consecutive blank lines
3347365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3348365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3349365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3350d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3351d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3352d752fcc8SJoe Perches			    $fix) {
3353f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3354d752fcc8SJoe Perches			}
3355d752fcc8SJoe Perches
3356365dd4eaSJoe Perches			$last_blank_line = $linenr;
3357365dd4eaSJoe Perches		}
3358365dd4eaSJoe Perches
33593b617e3bSJoe Perches# check for missing blank lines after declarations
33603f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
33613f7bac03SJoe Perches			# actual declarations
33623f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
33635a4e1fd3SJoe Perches			# function pointer declarations
33645a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
33653f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
33663f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
33673f7bac03SJoe Perches			# known declaration macros
33683f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
33693f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
33703f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
33713f7bac03SJoe Perches			# other possible extensions of declaration lines
33723f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
33733f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
33743f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
33753f7bac03SJoe Perches			# looks like a declaration
33763f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
33775a4e1fd3SJoe Perches			# function pointer declarations
33785a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
33793f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
33803f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
33813f7bac03SJoe Perches			# known declaration macros
33823f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
33833f7bac03SJoe Perches			# start of struct or union or enum
3384328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
33853f7bac03SJoe Perches			# start or end of block or continuation of declaration
33863f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
33873f7bac03SJoe Perches			# bitfield continuation
33883f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
33893f7bac03SJoe Perches			# other possible extensions of declaration lines
33903f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
33913f7bac03SJoe Perches			# indentation of previous and current line are the same
33923f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3393d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3394d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3395d752fcc8SJoe Perches			    $fix) {
3396f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3397d752fcc8SJoe Perches			}
33983b617e3bSJoe Perches		}
33993b617e3bSJoe Perches
34005f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
34016b4c5bebSAndy Whitcroft# Exceptions:
34026b4c5bebSAndy Whitcroft#  1) within comments
34036b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
34046b4c5bebSAndy Whitcroft#  3) hanging labels
34053705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
34065f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
34073705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
34083705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
34093705ce5bSJoe Perches			    $fix) {
3410194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
34113705ce5bSJoe Perches			}
34125f7ddae6SRaffaele Recalcati		}
34135f7ddae6SRaffaele Recalcati
3414b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3415b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3416b9ea10d6SAndy Whitcroft
34175751a24eSJoe Perches# check for unusual line ending [ or (
34185751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
34195751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
34205751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
34215751a24eSJoe Perches		}
34225751a24eSJoe Perches
34234dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
34244dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
34254dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
34264dbed76fSJoe Perches			$context_function = $1;
34274dbed76fSJoe Perches		}
34284dbed76fSJoe Perches
34294dbed76fSJoe Perches# check if this appears to be the end of function declaration
34304dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
34314dbed76fSJoe Perches			undef $context_function;
34324dbed76fSJoe Perches		}
34334dbed76fSJoe Perches
3434032a4c0fSJoe Perches# check indentation of any line with a bare else
3435840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3436032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3437032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3438032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3439840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3440840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3441840080a0SJoe Perches			     defined $lines[$linenr] &&
3442840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3443032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3444032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3445032a4c0fSJoe Perches			}
3446032a4c0fSJoe Perches		}
3447032a4c0fSJoe Perches
3448c00df19aSJoe Perches# check indentation of a line with a break;
3449c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3450c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3451c00df19aSJoe Perches			my $tabs = $1;
3452c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3453c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3454c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3455c00df19aSJoe Perches			}
3456c00df19aSJoe Perches		}
3457c00df19aSJoe Perches
3458c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3459cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3460000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3461000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3462c2fdda0dSAndy Whitcroft		}
346322f2a2efSAndy Whitcroft
346456e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
346556e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
346656e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
346756e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
346856e77d70SJoe Perches		}
346956e77d70SJoe Perches
34709c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
34712b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
34722b474a1aSAndy Whitcroft		    $realline_next);
34733e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3474ca819864SJoe Perches		if ($linenr > $suppress_statement &&
34751b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3476170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3477f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3478171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3479171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3480171ae1a4SAndy Whitcroft
34813e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
34823e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
34833e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
34843e469cdcSAndy Whitcroft			# until we hit end of it.
34853e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
34863e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
34873e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
34883e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
34893e469cdcSAndy Whitcroft			}
3490f74bd194SAndy Whitcroft
34912b474a1aSAndy Whitcroft			# Find the real next line.
34922b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
34932b474a1aSAndy Whitcroft			if (defined $realline_next &&
34942b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
34952b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
34962b474a1aSAndy Whitcroft				$realline_next++;
34972b474a1aSAndy Whitcroft			}
34982b474a1aSAndy Whitcroft
3499171ae1a4SAndy Whitcroft			my $s = $stat;
3500171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3501cf655043SAndy Whitcroft
3502c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3503171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3504c2fdda0dSAndy Whitcroft
3505c2fdda0dSAndy Whitcroft			# Ignore functions being called
3506171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3507c2fdda0dSAndy Whitcroft
3508463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3509463f2864SAndy Whitcroft
3510c45dcabdSAndy Whitcroft			# declarations always start with types
3511d2506586SAndy 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) {
3512c45dcabdSAndy Whitcroft				my $type = $1;
3513c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3514c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3515c45dcabdSAndy Whitcroft
35166c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3517a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3518c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3519c2fdda0dSAndy Whitcroft			}
35208905a67cSAndy Whitcroft
35216c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
352265863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3523c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
35249c0ca6f9SAndy Whitcroft			}
35258905a67cSAndy Whitcroft
35268905a67cSAndy Whitcroft			# Check for any sort of function declaration.
35278905a67cSAndy Whitcroft			# int foo(something bar, other baz);
35288905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3529171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
35308905a67cSAndy Whitcroft				my ($name_len) = length($1);
35318905a67cSAndy Whitcroft
3532cf655043SAndy Whitcroft				my $ctx = $s;
3533773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
35348905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3535cf655043SAndy Whitcroft
35368905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3537c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
35388905a67cSAndy Whitcroft
3539c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
35408905a67cSAndy Whitcroft					}
35418905a67cSAndy Whitcroft				}
35428905a67cSAndy Whitcroft			}
35438905a67cSAndy Whitcroft
35449c0ca6f9SAndy Whitcroft		}
35459c0ca6f9SAndy Whitcroft
354600df344fSAndy Whitcroft#
354700df344fSAndy Whitcroft# Checks which may be anchored in the context.
354800df344fSAndy Whitcroft#
354900df344fSAndy Whitcroft
355000df344fSAndy Whitcroft# Check for switch () and associated case and default
355100df344fSAndy Whitcroft# statements should be at the same indent.
355200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
355300df344fSAndy Whitcroft			my $err = '';
355400df344fSAndy Whitcroft			my $sep = '';
355500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
355600df344fSAndy Whitcroft			shift(@ctx);
355700df344fSAndy Whitcroft			for my $ctx (@ctx) {
355800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
355900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
356000df344fSAndy Whitcroft							$indent != $cindent) {
356100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
356200df344fSAndy Whitcroft					$sep = '';
356300df344fSAndy Whitcroft				} else {
356400df344fSAndy Whitcroft					$sep = "[...]\n";
356500df344fSAndy Whitcroft				}
356600df344fSAndy Whitcroft			}
356700df344fSAndy Whitcroft			if ($err ne '') {
3568000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3569000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3570de7d4f0eSAndy Whitcroft			}
3571de7d4f0eSAndy Whitcroft		}
3572de7d4f0eSAndy Whitcroft
3573de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3574de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
35750fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3576773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3577773647a0SAndy Whitcroft
35789c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
35798eef05ddSJoe Perches
35808eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
35818eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
35828eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
35838eef05ddSJoe Perches			}
35848eef05ddSJoe Perches
3585de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3586de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3587de7d4f0eSAndy Whitcroft
3588548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3589548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3590de7d4f0eSAndy Whitcroft
3591548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3592548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3593548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3594548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3595548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3596773647a0SAndy Whitcroft				$ctx_ln++;
3597773647a0SAndy Whitcroft			}
3598548596d5SAndy Whitcroft
359953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
360053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3601773647a0SAndy Whitcroft
3602773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3603000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3604000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
360501464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
360600df344fSAndy Whitcroft			}
3607773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3608773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3609773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3610773647a0SAndy Whitcroft			{
36119c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
36129c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3613000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3614000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
361501464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
36169c0ca6f9SAndy Whitcroft				}
36179c0ca6f9SAndy Whitcroft			}
361800df344fSAndy Whitcroft		}
361900df344fSAndy Whitcroft
36204d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3621f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
36223e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
36233e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
36243e469cdcSAndy Whitcroft					if (!defined $stat);
36254d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
36264d001e4dSAndy Whitcroft
36274d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
36284d001e4dSAndy Whitcroft
36299f5af480SJoe Perches			# remove inline comments
36309f5af480SJoe Perches			$s =~ s/$;/ /g;
36319f5af480SJoe Perches			$c =~ s/$;/ /g;
36324d001e4dSAndy Whitcroft
36334d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
36346f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
36356f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
36364d001e4dSAndy Whitcroft
36379f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
36389f5af480SJoe Perches			# none on the first line, and are going to readd them
36399f5af480SJoe Perches			# where necessary.
36409f5af480SJoe Perches			$s =~ s/\n./\n/gs;
36419f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
36429f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
36439f5af480SJoe Perches			}
36449f5af480SJoe Perches
36454d001e4dSAndy Whitcroft			# We want to check the first line inside the block
36464d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
36474d001e4dSAndy Whitcroft			#  1) any blank line termination
36484d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
36494d001e4dSAndy Whitcroft			#  3) any do (...) {
36504d001e4dSAndy Whitcroft			my $continuation = 0;
36514d001e4dSAndy Whitcroft			my $check = 0;
36524d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
36534d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
36544d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
36554d001e4dSAndy Whitcroft				$continuation = 1;
36564d001e4dSAndy Whitcroft			}
36579bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
36584d001e4dSAndy Whitcroft				$check = 1;
36594d001e4dSAndy Whitcroft				$cond_lines++;
36604d001e4dSAndy Whitcroft			}
36614d001e4dSAndy Whitcroft
36624d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
36634d001e4dSAndy Whitcroft			# preprocessor statement.
36644d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
36654d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
36664d001e4dSAndy Whitcroft				$check = 0;
36674d001e4dSAndy Whitcroft			}
36684d001e4dSAndy Whitcroft
36699bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3670740504c6SAndy Whitcroft			$continuation = 0;
36719bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
36729bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
36734d001e4dSAndy Whitcroft
3674f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3675f16fa28fSAndy Whitcroft				# is not linear.
3676f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3677f16fa28fSAndy Whitcroft					$check = 0;
3678f16fa28fSAndy Whitcroft				}
3679f16fa28fSAndy Whitcroft
36809bd49efeSAndy Whitcroft				# Ignore:
36819bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
36829bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
36839bd49efeSAndy Whitcroft				#  3) labels.
3684740504c6SAndy Whitcroft				if ($continuation ||
3685740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
36869bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
36879bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3688740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
368930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
36909bd49efeSAndy Whitcroft						$cond_lines++;
36919bd49efeSAndy Whitcroft					}
36924d001e4dSAndy Whitcroft				}
369330dad6ebSAndy Whitcroft			}
36944d001e4dSAndy Whitcroft
36954d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
36964d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
36974d001e4dSAndy Whitcroft
36984d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
36994d001e4dSAndy Whitcroft			# this is not this patch's fault.
37004d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
37014d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
37024d001e4dSAndy Whitcroft				$check = 0;
37034d001e4dSAndy Whitcroft			}
37044d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
37054d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
37064d001e4dSAndy Whitcroft			}
37074d001e4dSAndy Whitcroft
37089bd49efeSAndy 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";
37094d001e4dSAndy Whitcroft
37109f5af480SJoe Perches			if ($check && $s ne '' &&
37119f5af480SJoe Perches			    (($sindent % 8) != 0 ||
37129f5af480SJoe Perches			     ($sindent < $indent) ||
3713f6950a73SJoe Perches			     ($sindent == $indent &&
3714f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
37159f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3716000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3717000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
37184d001e4dSAndy Whitcroft			}
37194d001e4dSAndy Whitcroft		}
37204d001e4dSAndy Whitcroft
37216c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
37226c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
37231f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
37241f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
37256c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3726c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3727c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3728cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3729cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
37301f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3731c2fdda0dSAndy Whitcroft		}
37326c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
37336c72ffaaSAndy Whitcroft
373400df344fSAndy Whitcroft#ignore lines not being added
37353705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
373600df344fSAndy Whitcroft
373711ca40a0SJoe Perches# check for dereferences that span multiple lines
373811ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
373911ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
374011ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
374111ca40a0SJoe Perches			my $ref = $1;
374211ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
374311ca40a0SJoe Perches			$ref .= $1;
374411ca40a0SJoe Perches			$ref =~ s/\s//g;
374511ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
374611ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
374711ca40a0SJoe Perches		}
374811ca40a0SJoe Perches
3749a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3750c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3751a1ce18e4SJoe Perches			my $type = $1;
3752a1ce18e4SJoe Perches			my $var = $2;
3753207a8e84SJoe Perches			$var = "" if (!defined $var);
3754207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3755a1ce18e4SJoe Perches				my $sign = $1;
3756a1ce18e4SJoe Perches				my $pointer = $2;
3757a1ce18e4SJoe Perches
3758a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3759a1ce18e4SJoe Perches
3760a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3761a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3762a1ce18e4SJoe Perches				    $fix) {
3763a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3764207a8e84SJoe Perches					my $comp_pointer = $pointer;
3765207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3766207a8e84SJoe Perches					$decl .= $comp_pointer;
3767207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3768207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3769a1ce18e4SJoe Perches				}
3770a1ce18e4SJoe Perches			}
3771a1ce18e4SJoe Perches		}
3772a1ce18e4SJoe Perches
3773653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
37747429c690SAndy Whitcroft		if ($dbg_type) {
37757429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3776000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3777000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
37787429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3779000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3780000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
37817429c690SAndy Whitcroft			}
3782653d4876SAndy Whitcroft			next;
3783653d4876SAndy Whitcroft		}
3784a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3785a1ef277eSAndy Whitcroft		if ($dbg_attr) {
37869360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3787000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3788000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
37899360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3790000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3791000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3792a1ef277eSAndy Whitcroft			}
3793a1ef277eSAndy Whitcroft			next;
3794a1ef277eSAndy Whitcroft		}
3795653d4876SAndy Whitcroft
3796f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
379799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
379899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3799d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3800d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3801f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3802f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3803f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3804d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3805d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3806f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3807d752fcc8SJoe Perches				$fixedline = $line;
38088d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3809f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3810d752fcc8SJoe Perches			}
3811f0a594c1SAndy Whitcroft		}
3812f0a594c1SAndy Whitcroft
381300df344fSAndy Whitcroft#
381400df344fSAndy Whitcroft# Checks which are anchored on the added line.
381500df344fSAndy Whitcroft#
381600df344fSAndy Whitcroft
3817653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3818c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3819653d4876SAndy Whitcroft			my $path = $1;
3820653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3821000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3822495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3823495e9d84SJoe Perches			}
3824495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3825495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3826495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3827653d4876SAndy Whitcroft			}
3828653d4876SAndy Whitcroft		}
3829653d4876SAndy Whitcroft
383000df344fSAndy Whitcroft# no C99 // comments
383100df344fSAndy Whitcroft		if ($line =~ m{//}) {
38323705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
38333705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
38343705ce5bSJoe Perches			    $fix) {
3835194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
38363705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
38373705ce5bSJoe Perches					my $comment = trim($1);
3838194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
38393705ce5bSJoe Perches				}
38403705ce5bSJoe Perches			}
384100df344fSAndy Whitcroft		}
384200df344fSAndy Whitcroft		# Remove C99 comments.
38430a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
38446c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
38450a920b5bSAndy Whitcroft
38462b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
38472b474a1aSAndy Whitcroft# the whole statement.
38482b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
38492b474a1aSAndy Whitcroft		if (defined $realline_next &&
38502b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
38512b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
38522b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
38532b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
38543cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
38553cbf62dfSAndy Whitcroft			# a prefix:
38563cbf62dfSAndy Whitcroft			#   XXX(foo);
38573cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3858653d4876SAndy Whitcroft			my $name = $1;
385987a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
38603cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
38613cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
38623cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
38633cbf62dfSAndy Whitcroft
38643cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
38652b474a1aSAndy Whitcroft				\n.}\s*$|
386648012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
386748012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
386848012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
38692b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
38702b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
387148012058SAndy Whitcroft			    )/x) {
38722b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
38732b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
38742b474a1aSAndy Whitcroft			} else {
38752b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
38760a920b5bSAndy Whitcroft			}
38770a920b5bSAndy Whitcroft		}
38782b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
38792b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
38802b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
38812b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
38822b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
38832b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
38842b474a1aSAndy Whitcroft		}
38852b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
38862b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3887000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3888000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
38892b474a1aSAndy Whitcroft		}
38900a920b5bSAndy Whitcroft
38915150bda4SJoe Eloff# check for global initialisers.
38926d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3893d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
38946d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3895d5e616fcSJoe Perches			    $fix) {
38966d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3897d5e616fcSJoe Perches			}
3898f0a594c1SAndy Whitcroft		}
38990a920b5bSAndy Whitcroft# check for static initialisers.
39006d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3901d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
39026d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3903d5e616fcSJoe Perches				      $herecurr) &&
3904d5e616fcSJoe Perches			    $fix) {
39056d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3906d5e616fcSJoe Perches			}
39070a920b5bSAndy Whitcroft		}
39080a920b5bSAndy Whitcroft
39091813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
39101813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
39111813087dSJoe Perches			my $tmp = trim($1);
39121813087dSJoe Perches			WARN("MISORDERED_TYPE",
39131813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
39141813087dSJoe Perches		}
39151813087dSJoe Perches
3916809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
3917809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3918809e082eSJoe Perches			my $type = trim($1);
3919809e082eSJoe Perches			next if ($type !~ /\bint\b/);
3920809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3921809e082eSJoe Perches			my $new_type = $type;
3922809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
3923809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3924809e082eSJoe Perches			$new_type =~ s/^const\s+//;
3925809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3926809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
3927809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
3928809e082eSJoe Perches			$new_type = trim($new_type);
3929809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
3930809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3931809e082eSJoe Perches			    $fix) {
3932809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3933809e082eSJoe Perches			}
3934809e082eSJoe Perches		}
3935809e082eSJoe Perches
3936cb710ecaSJoe Perches# check for static const char * arrays.
3937cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3938000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3939000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3940cb710ecaSJoe Perches				$herecurr);
3941cb710ecaSJoe Perches		}
3942cb710ecaSJoe Perches
394377b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
394477b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
394577b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
394677b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
394777b8c0a8SJoe Perches			    $fix) {
394877b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
394977b8c0a8SJoe Perches			}
395077b8c0a8SJoe Perches		}
395177b8c0a8SJoe Perches
3952cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3953cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3954000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3955000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3956cb710ecaSJoe Perches				$herecurr);
3957cb710ecaSJoe Perches		}
3958cb710ecaSJoe Perches
3959ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3960ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3961ab7e23f3SJoe Perches			my $found = $1;
3962ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3963ab7e23f3SJoe Perches				WARN("CONST_CONST",
3964ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3965ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3966ab7e23f3SJoe Perches				WARN("CONST_CONST",
3967ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3968ab7e23f3SJoe Perches			}
3969ab7e23f3SJoe Perches		}
3970ab7e23f3SJoe Perches
39719b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
39729b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
39739b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
39749b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
39759b0fa60dSJoe Perches				$herecurr);
39769b0fa60dSJoe Perches               }
39779b0fa60dSJoe Perches
3978b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3979b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3980b598b670SJoe Perches			my $array = $1;
3981b598b670SJoe 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*\))@) {
3982b598b670SJoe Perches				my $array_div = $1;
3983b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3984b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3985b598b670SJoe Perches				    $fix) {
3986b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3987b598b670SJoe Perches				}
3988b598b670SJoe Perches			}
3989b598b670SJoe Perches		}
3990b598b670SJoe Perches
3991b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3992b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3993b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3994b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3995b36190c5SJoe Perches			    $fix) {
3996194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3997b36190c5SJoe Perches			}
3998b36190c5SJoe Perches		}
3999b36190c5SJoe Perches
4000653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4001653d4876SAndy Whitcroft# make sense.
4002653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
40038054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4004c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
40058ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
400646d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4007000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4008000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
40090a920b5bSAndy Whitcroft		}
40100a920b5bSAndy Whitcroft
40110a920b5bSAndy Whitcroft# * goes on variable not on type
401265863862SAndy Whitcroft		# (char*[ const])
4013bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4014bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
40153705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4016d8aaf121SAndy Whitcroft
401765863862SAndy Whitcroft			# Should start with a space.
401865863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
401965863862SAndy Whitcroft			# Should not end with a space.
402065863862SAndy Whitcroft			$to =~ s/\s+$//;
402165863862SAndy Whitcroft			# '*'s should not have spaces between.
4022f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
402365863862SAndy Whitcroft			}
4024d8aaf121SAndy Whitcroft
40253705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
402665863862SAndy Whitcroft			if ($from ne $to) {
40273705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40283705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
40293705ce5bSJoe Perches				    $fix) {
40303705ce5bSJoe Perches					my $sub_from = $ident;
40313705ce5bSJoe Perches					my $sub_to = $ident;
40323705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4033194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40343705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
40353705ce5bSJoe Perches				}
403665863862SAndy Whitcroft			}
4037bfcb2cc7SAndy Whitcroft		}
4038bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4039bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
40403705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4041d8aaf121SAndy Whitcroft
404265863862SAndy Whitcroft			# Should start with a space.
404365863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
404465863862SAndy Whitcroft			# Should not end with a space.
404565863862SAndy Whitcroft			$to =~ s/\s+$//;
404665863862SAndy Whitcroft			# '*'s should not have spaces between.
4047f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
404865863862SAndy Whitcroft			}
404965863862SAndy Whitcroft			# Modifiers should have spaces.
405065863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
405165863862SAndy Whitcroft
40523705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4053667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
40543705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40553705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
40563705ce5bSJoe Perches				    $fix) {
40573705ce5bSJoe Perches
40583705ce5bSJoe Perches					my $sub_from = $match;
40593705ce5bSJoe Perches					my $sub_to = $match;
40603705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4061194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40623705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
40633705ce5bSJoe Perches				}
406465863862SAndy Whitcroft			}
40650a920b5bSAndy Whitcroft		}
40660a920b5bSAndy Whitcroft
40679d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
40689d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
40690675a8fbSJean Delvare			my $msg_level = \&WARN;
40700675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
40710675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
40729d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
40739d3e3c70SJoe Perches		}
40740a920b5bSAndy Whitcroft
40759d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
40768905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4077000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4078000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
40798905a67cSAndy Whitcroft		}
40808905a67cSAndy Whitcroft
408117441227SJoe Perches# check for uses of printk_ratelimit
408217441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4083000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4084000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
408517441227SJoe Perches		}
408617441227SJoe Perches
4087eeef5733SJoe Perches# printk should use KERN_* levels
4088eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4089000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4090eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
409100df344fSAndy Whitcroft		}
40920a920b5bSAndy Whitcroft
4093243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4094243f3803SJoe Perches			my $orig = $1;
4095243f3803SJoe Perches			my $level = lc($orig);
4096243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
40978f26b837SJoe Perches			my $level2 = $level;
40988f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4099243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4100daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4101243f3803SJoe Perches		}
4102243f3803SJoe Perches
4103243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
4104d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
4105d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4106d5e616fcSJoe Perches			    $fix) {
4107194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4108d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
4109d5e616fcSJoe Perches			}
4110243f3803SJoe Perches		}
4111243f3803SJoe Perches
4112dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4113dc139313SJoe Perches			my $orig = $1;
4114dc139313SJoe Perches			my $level = lc($orig);
4115dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4116dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4117dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4118dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4119dc139313SJoe Perches		}
4120dc139313SJoe Perches
412191c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
412291c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
412391c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
412491c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
412591c9afafSAndy Lutomirski			WARN("ENOSYS",
412691c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
412791c9afafSAndy Lutomirski		}
412891c9afafSAndy Lutomirski
4129653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4130653d4876SAndy Whitcroft# or if closed on same line
41315b57980dSJoe Perches		if ($perl_version_ok &&
41322d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
41332d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
41342d453e3bSJoe Perches		    $sline !~ /}/) {
41358d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41362d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
41378d182478SJoe Perches			    $fix) {
41388d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41398d182478SJoe Perches				my $fixed_line = $rawline;
41408d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
41418d182478SJoe Perches				my $line1 = $1;
41428d182478SJoe Perches				my $line2 = $2;
41438d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
41448d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
41458d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
41468d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
41478d182478SJoe Perches				}
41488d182478SJoe Perches			}
41490a920b5bSAndy Whitcroft		}
4150653d4876SAndy Whitcroft
41518905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
41528905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
41538905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
41548d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41558d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
41568d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41578d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
41588d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41598d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
41608d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
41618d182478SJoe Perches				$fixedline = $rawline;
41628d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
41638d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
41648d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41658d182478SJoe Perches				}
41668d182478SJoe Perches			}
41678905a67cSAndy Whitcroft		}
41688905a67cSAndy Whitcroft
41690c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
41703705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
41713705ce5bSJoe Perches			if (WARN("SPACING",
41723705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
41733705ce5bSJoe Perches			    $fix) {
4174194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41753705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
41763705ce5bSJoe Perches			}
41770c73b4ebSAndy Whitcroft		}
41780c73b4ebSAndy Whitcroft
417931070b5dSJoe Perches# Function pointer declarations
418031070b5dSJoe Perches# check spacing between type, funcptr, and args
418131070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
418291f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
418331070b5dSJoe Perches			my $declare = $1;
418431070b5dSJoe Perches			my $pre_pointer_space = $2;
418531070b5dSJoe Perches			my $post_pointer_space = $3;
418631070b5dSJoe Perches			my $funcname = $4;
418731070b5dSJoe Perches			my $post_funcname_space = $5;
418831070b5dSJoe Perches			my $pre_args_space = $6;
418931070b5dSJoe Perches
419091f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
419191f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
419291f72e9cSJoe Perches# don't need a space so don't warn for those.
419391f72e9cSJoe Perches			my $post_declare_space = "";
419491f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
419591f72e9cSJoe Perches				$post_declare_space = $1;
419691f72e9cSJoe Perches				$declare = rtrim($declare);
419791f72e9cSJoe Perches			}
419891f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
419931070b5dSJoe Perches				WARN("SPACING",
420031070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
420191f72e9cSJoe Perches				$post_declare_space = " ";
420231070b5dSJoe Perches			}
420331070b5dSJoe Perches
420431070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
420591f72e9cSJoe Perches# This test is not currently implemented because these declarations are
420691f72e9cSJoe Perches# equivalent to
420791f72e9cSJoe Perches#	int  foo(int bar, ...)
420891f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
420991f72e9cSJoe Perches#
421091f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
421191f72e9cSJoe Perches#				WARN("SPACING",
421291f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
421391f72e9cSJoe Perches#			}
421431070b5dSJoe Perches
421531070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
421631070b5dSJoe Perches			if (defined $pre_pointer_space &&
421731070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
421831070b5dSJoe Perches				WARN("SPACING",
421931070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
422031070b5dSJoe Perches			}
422131070b5dSJoe Perches
422231070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
422331070b5dSJoe Perches			if (defined $post_pointer_space &&
422431070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
422531070b5dSJoe Perches				WARN("SPACING",
422631070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
422731070b5dSJoe Perches			}
422831070b5dSJoe Perches
422931070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
423031070b5dSJoe Perches			if (defined $post_funcname_space &&
423131070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
423231070b5dSJoe Perches				WARN("SPACING",
423331070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
423431070b5dSJoe Perches			}
423531070b5dSJoe Perches
423631070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
423731070b5dSJoe Perches			if (defined $pre_args_space &&
423831070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
423931070b5dSJoe Perches				WARN("SPACING",
424031070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
424131070b5dSJoe Perches			}
424231070b5dSJoe Perches
424331070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4244194f66fcSJoe Perches				$fixed[$fixlinenr] =~
424591f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
424631070b5dSJoe Perches			}
424731070b5dSJoe Perches		}
424831070b5dSJoe Perches
42498d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
42508d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4251fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4252fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
42538d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
42548d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
42558d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4256fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
425738dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
42583705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
42593705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
42603705ce5bSJoe Perches				    $fix) {
4261194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
42623705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
42633705ce5bSJoe Perches				}
42648d31cfceSAndy Whitcroft			}
42658d31cfceSAndy Whitcroft		}
42668d31cfceSAndy Whitcroft
4267f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
42686c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4269c2fdda0dSAndy Whitcroft			my $name = $1;
4270773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4271773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4272c2fdda0dSAndy Whitcroft
4273c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4274773647a0SAndy Whitcroft			if ($name =~ /^(?:
4275773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4276773647a0SAndy Whitcroft				volatile|__volatile__|
4277773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4278773647a0SAndy Whitcroft				asm|__asm__)$/x)
4279773647a0SAndy Whitcroft			{
4280c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4281c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4282c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4283c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4284773647a0SAndy Whitcroft
4285773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4286c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4287c2fdda0dSAndy Whitcroft
4288c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4289c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4290773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4291c2fdda0dSAndy Whitcroft
4292c2fdda0dSAndy Whitcroft			} else {
42933705ce5bSJoe Perches				if (WARN("SPACING",
42943705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
42953705ce5bSJoe Perches					     $fix) {
4296194f66fcSJoe Perches					$fixed[$fixlinenr] =~
42973705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
42983705ce5bSJoe Perches				}
4299f0a594c1SAndy Whitcroft			}
43006c72ffaaSAndy Whitcroft		}
43019a4cad4eSEric Nelson
4302653d4876SAndy Whitcroft# Check operator spacing.
43030a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
43043705ce5bSJoe Perches			my $fixed_line = "";
43053705ce5bSJoe Perches			my $line_fixed = 0;
43063705ce5bSJoe Perches
43079c0ca6f9SAndy Whitcroft			my $ops = qr{
43089c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
43099c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
43109c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
43111f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
431284731623SJoe Perches				\?:|\?|:
43139c0ca6f9SAndy Whitcroft			}x;
4314cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
43153705ce5bSJoe Perches
43163705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
43173705ce5bSJoe Perches##			foreach my $el (@elements) {
43183705ce5bSJoe Perches##				print("el: <$el>\n");
43193705ce5bSJoe Perches##			}
43203705ce5bSJoe Perches
43213705ce5bSJoe Perches			my @fix_elements = ();
432200df344fSAndy Whitcroft			my $off = 0;
43236c72ffaaSAndy Whitcroft
43243705ce5bSJoe Perches			foreach my $el (@elements) {
43253705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
43263705ce5bSJoe Perches				$off += length($el);
43273705ce5bSJoe Perches			}
43283705ce5bSJoe Perches
43293705ce5bSJoe Perches			$off = 0;
43303705ce5bSJoe Perches
43316c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4332b34c648bSJoe Perches			my $last_after = -1;
43336c72ffaaSAndy Whitcroft
43340a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
43353705ce5bSJoe Perches
43363705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
43373705ce5bSJoe Perches
43383705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
43393705ce5bSJoe Perches
43404a0df2efSAndy Whitcroft				$off += length($elements[$n]);
43414a0df2efSAndy Whitcroft
434225985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4343773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4344773647a0SAndy Whitcroft				my $cc = '';
4345773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4346773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4347773647a0SAndy Whitcroft				}
4348773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4349773647a0SAndy Whitcroft
43504a0df2efSAndy Whitcroft				my $a = '';
43514a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
43524a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4353cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
43544a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
43554a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4356773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
43574a0df2efSAndy Whitcroft
43580a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
43594a0df2efSAndy Whitcroft
43604a0df2efSAndy Whitcroft				my $c = '';
43610a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
43624a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
43634a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4364cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
43654a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
43664a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
43678b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
43684a0df2efSAndy Whitcroft				} else {
43694a0df2efSAndy Whitcroft					$c = 'E';
43700a920b5bSAndy Whitcroft				}
43710a920b5bSAndy Whitcroft
43724a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
43734a0df2efSAndy Whitcroft
43744a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
43754a0df2efSAndy Whitcroft
43766c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4377de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
43780a920b5bSAndy Whitcroft
437974048ed8SAndy Whitcroft				# Pull out the value of this operator.
43806c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
43810a920b5bSAndy Whitcroft
43821f65f947SAndy Whitcroft				# Get the full operator variant.
43831f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
43841f65f947SAndy Whitcroft
438513214adfSAndy Whitcroft				# Ignore operators passed as parameters.
438613214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4387d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
438813214adfSAndy Whitcroft
4389cf655043SAndy Whitcroft#				# Ignore comments
4390cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
439113214adfSAndy Whitcroft
4392d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
439313214adfSAndy Whitcroft				} elsif ($op eq ';') {
4394cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4395cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
43963705ce5bSJoe Perches						if (ERROR("SPACING",
43973705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4398b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
43993705ce5bSJoe Perches							$line_fixed = 1;
44003705ce5bSJoe Perches						}
4401d8aaf121SAndy Whitcroft					}
4402d8aaf121SAndy Whitcroft
4403d8aaf121SAndy Whitcroft				# // is a comment
4404d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
44050a920b5bSAndy Whitcroft
4406b00e4814SJoe Perches				#   :   when part of a bitfield
4407b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4408b00e4814SJoe Perches					# skip the bitfield test for now
4409b00e4814SJoe Perches
44101f65f947SAndy Whitcroft				# No spaces for:
44111f65f947SAndy Whitcroft				#   ->
4412b00e4814SJoe Perches				} elsif ($op eq '->') {
44134a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
44143705ce5bSJoe Perches						if (ERROR("SPACING",
44153705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4416b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44173705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44183705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44193705ce5bSJoe Perches							}
4420b34c648bSJoe Perches							$line_fixed = 1;
44213705ce5bSJoe Perches						}
44220a920b5bSAndy Whitcroft					}
44230a920b5bSAndy Whitcroft
44242381097bSJoe Perches				# , must not have a space before and must have a space on the right.
44250a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
44262381097bSJoe Perches					my $rtrim_before = 0;
44272381097bSJoe Perches					my $space_after = 0;
44282381097bSJoe Perches					if ($ctx =~ /Wx./) {
44292381097bSJoe Perches						if (ERROR("SPACING",
44302381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
44312381097bSJoe Perches							$line_fixed = 1;
44322381097bSJoe Perches							$rtrim_before = 1;
44332381097bSJoe Perches						}
44342381097bSJoe Perches					}
4435cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
44363705ce5bSJoe Perches						if (ERROR("SPACING",
44373705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
44383705ce5bSJoe Perches							$line_fixed = 1;
4439b34c648bSJoe Perches							$last_after = $n;
44402381097bSJoe Perches							$space_after = 1;
44412381097bSJoe Perches						}
44422381097bSJoe Perches					}
44432381097bSJoe Perches					if ($rtrim_before || $space_after) {
44442381097bSJoe Perches						if ($rtrim_before) {
44452381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44462381097bSJoe Perches						} else {
44472381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
44482381097bSJoe Perches						}
44492381097bSJoe Perches						if ($space_after) {
44502381097bSJoe Perches							$good .= " ";
44513705ce5bSJoe Perches						}
44520a920b5bSAndy Whitcroft					}
44530a920b5bSAndy Whitcroft
44549c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
445574048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
44569c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
44579c0ca6f9SAndy Whitcroft
44589c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
44599c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
44609c0ca6f9SAndy Whitcroft				# unary operator, or a cast
44619c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
446274048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
44630d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4464cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
44653705ce5bSJoe Perches						if (ERROR("SPACING",
44663705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4467b34c648bSJoe Perches							if ($n != $last_after + 2) {
4468b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
44693705ce5bSJoe Perches								$line_fixed = 1;
44703705ce5bSJoe Perches							}
44710a920b5bSAndy Whitcroft						}
4472b34c648bSJoe Perches					}
4473a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4474171ae1a4SAndy Whitcroft						# A unary '*' may be const
4475171ae1a4SAndy Whitcroft
4476171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
44773705ce5bSJoe Perches						if (ERROR("SPACING",
44783705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4479b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
44803705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44813705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44823705ce5bSJoe Perches							}
4483b34c648bSJoe Perches							$line_fixed = 1;
44843705ce5bSJoe Perches						}
44850a920b5bSAndy Whitcroft					}
44860a920b5bSAndy Whitcroft
44870a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
44880a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4489773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
44903705ce5bSJoe Perches						if (ERROR("SPACING",
44913705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4492b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
44933705ce5bSJoe Perches							$line_fixed = 1;
44943705ce5bSJoe Perches						}
44950a920b5bSAndy Whitcroft					}
4496773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4497773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
44983705ce5bSJoe Perches						if (ERROR("SPACING",
44993705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4500b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45013705ce5bSJoe Perches							$line_fixed = 1;
45023705ce5bSJoe Perches						}
4503653d4876SAndy Whitcroft					}
4504773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
45053705ce5bSJoe Perches						if (ERROR("SPACING",
45063705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4507b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45083705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45093705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4510773647a0SAndy Whitcroft							}
4511b34c648bSJoe Perches							$line_fixed = 1;
45123705ce5bSJoe Perches						}
45133705ce5bSJoe Perches					}
45140a920b5bSAndy Whitcroft
45150a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
45169c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
45179c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
45189c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4519c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4520c2fdda0dSAndy Whitcroft					 $op eq '%')
45210a920b5bSAndy Whitcroft				{
4522d2e025f3SJoe Perches					if ($check) {
4523d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4524d2e025f3SJoe Perches							if (CHK("SPACING",
4525d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4526d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4527d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4528d2e025f3SJoe Perches								$line_fixed = 1;
4529d2e025f3SJoe Perches							}
4530d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4531d2e025f3SJoe Perches							if (CHK("SPACING",
4532d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4533d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4534d2e025f3SJoe Perches								$line_fixed = 1;
4535d2e025f3SJoe Perches							}
4536d2e025f3SJoe Perches						}
4537d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
45383705ce5bSJoe Perches						if (ERROR("SPACING",
45393705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4540b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4541b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4542b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4543b34c648bSJoe Perches							}
45443705ce5bSJoe Perches							$line_fixed = 1;
45453705ce5bSJoe Perches						}
45460a920b5bSAndy Whitcroft					}
45470a920b5bSAndy Whitcroft
45481f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
45491f65f947SAndy Whitcroft				# terminating a case value or a label.
45501f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
45511f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
45523705ce5bSJoe Perches						if (ERROR("SPACING",
45533705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4554b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45553705ce5bSJoe Perches							$line_fixed = 1;
45563705ce5bSJoe Perches						}
45571f65f947SAndy Whitcroft					}
45581f65f947SAndy Whitcroft
45590a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4560cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
45611f65f947SAndy Whitcroft					my $ok = 0;
45621f65f947SAndy Whitcroft
456322f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
45641f65f947SAndy Whitcroft					if (($op eq '<' &&
45651f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
45661f65f947SAndy Whitcroft					    ($op eq '>' &&
45671f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
45681f65f947SAndy Whitcroft					{
45691f65f947SAndy Whitcroft					    	$ok = 1;
45701f65f947SAndy Whitcroft					}
45711f65f947SAndy Whitcroft
4572e0df7e1fSJoe Perches					# for asm volatile statements
4573e0df7e1fSJoe Perches					# ignore a colon with another
4574e0df7e1fSJoe Perches					# colon immediately before or after
4575e0df7e1fSJoe Perches					if (($op eq ':') &&
4576e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4577e0df7e1fSJoe Perches						$ok = 1;
4578e0df7e1fSJoe Perches					}
4579e0df7e1fSJoe Perches
458084731623SJoe Perches					# messages are ERROR, but ?: are CHK
45811f65f947SAndy Whitcroft					if ($ok == 0) {
45820675a8fbSJean Delvare						my $msg_level = \&ERROR;
45830675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
458484731623SJoe Perches
45850675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
45863705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4587b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4588b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4589b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4590b34c648bSJoe Perches							}
45913705ce5bSJoe Perches							$line_fixed = 1;
45923705ce5bSJoe Perches						}
45930a920b5bSAndy Whitcroft					}
459422f2a2efSAndy Whitcroft				}
45954a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
45963705ce5bSJoe Perches
45973705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
45983705ce5bSJoe Perches
45993705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
46000a920b5bSAndy Whitcroft			}
46013705ce5bSJoe Perches
46023705ce5bSJoe Perches			if (($#elements % 2) == 0) {
46033705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
46043705ce5bSJoe Perches			}
46053705ce5bSJoe Perches
4606194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4607194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
46083705ce5bSJoe Perches			}
46093705ce5bSJoe Perches
46103705ce5bSJoe Perches
46110a920b5bSAndy Whitcroft		}
46120a920b5bSAndy Whitcroft
4613786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4614d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4615786b6326SJoe Perches			if (WARN("SPACING",
4616786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4617786b6326SJoe Perches			    $fix) {
4618194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4619786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4620786b6326SJoe Perches			}
4621786b6326SJoe Perches		}
4622786b6326SJoe Perches
4623f0a594c1SAndy Whitcroft# check for multiple assignments
4624f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4625000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4626000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4627f0a594c1SAndy Whitcroft		}
4628f0a594c1SAndy Whitcroft
462922f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
463022f2a2efSAndy Whitcroft## # continuation.
463122f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
463222f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
463322f2a2efSAndy Whitcroft##
463422f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
463522f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
463622f2a2efSAndy Whitcroft## 			my $ln = $line;
463722f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
463822f2a2efSAndy Whitcroft## 			}
463922f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4640000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4641000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
464222f2a2efSAndy Whitcroft## 			}
464322f2a2efSAndy Whitcroft## 		}
4644f0a594c1SAndy Whitcroft
46450a920b5bSAndy Whitcroft#need space before brace following if, while, etc
46466b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
46476ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
46483705ce5bSJoe Perches			if (ERROR("SPACING",
46493705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
46503705ce5bSJoe Perches			    $fix) {
46516ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
46523705ce5bSJoe Perches			}
4653de7d4f0eSAndy Whitcroft		}
4654de7d4f0eSAndy Whitcroft
4655c4a62ef9SJoe Perches## # check for blank lines before declarations
4656c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4657c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4658c4a62ef9SJoe Perches##			WARN("SPACING",
4659c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4660c4a62ef9SJoe Perches##		}
4661c4a62ef9SJoe Perches##
4662c4a62ef9SJoe Perches
4663de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4664de7d4f0eSAndy Whitcroft# on the line
4665de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4666d5e616fcSJoe Perches			if (ERROR("SPACING",
4667d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4668d5e616fcSJoe Perches			    $fix) {
4669194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4670d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4671d5e616fcSJoe Perches			}
46720a920b5bSAndy Whitcroft		}
46730a920b5bSAndy Whitcroft
467422f2a2efSAndy Whitcroft# check spacing on square brackets
467522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
46763705ce5bSJoe Perches			if (ERROR("SPACING",
46773705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
46783705ce5bSJoe Perches			    $fix) {
4679194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46803705ce5bSJoe Perches				    s/\[\s+/\[/;
46813705ce5bSJoe Perches			}
468222f2a2efSAndy Whitcroft		}
468322f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
46843705ce5bSJoe Perches			if (ERROR("SPACING",
46853705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
46863705ce5bSJoe Perches			    $fix) {
4687194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46883705ce5bSJoe Perches				    s/\s+\]/\]/;
46893705ce5bSJoe Perches			}
469022f2a2efSAndy Whitcroft		}
469122f2a2efSAndy Whitcroft
4692c45dcabdSAndy Whitcroft# check spacing on parentheses
46939c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
46949c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
46953705ce5bSJoe Perches			if (ERROR("SPACING",
46963705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
46973705ce5bSJoe Perches			    $fix) {
4698194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46993705ce5bSJoe Perches				    s/\(\s+/\(/;
47003705ce5bSJoe Perches			}
470122f2a2efSAndy Whitcroft		}
470213214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4703c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4704c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
47053705ce5bSJoe Perches			if (ERROR("SPACING",
47063705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
47073705ce5bSJoe Perches			    $fix) {
4708194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47093705ce5bSJoe Perches				    s/\s+\)/\)/;
47103705ce5bSJoe Perches			}
471122f2a2efSAndy Whitcroft		}
471222f2a2efSAndy Whitcroft
4713e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4714e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4715e2826fd0SJoe Perches
4716e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4717ea4acbb1SJoe Perches			my $var = $1;
4718ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4719ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4720ea4acbb1SJoe Perches			    $fix) {
4721ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4722ea4acbb1SJoe Perches			}
4723ea4acbb1SJoe Perches		}
4724ea4acbb1SJoe Perches
4725ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4726ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4727ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4728ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4729ea4acbb1SJoe Perches			my $var = $2;
4730ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4731ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4732ea4acbb1SJoe Perches			    $fix) {
4733ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4734ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4735ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4736ea4acbb1SJoe Perches			}
4737e2826fd0SJoe Perches		}
4738e2826fd0SJoe Perches
473963b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4740a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4741a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
47425b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
474363b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
474463b7c73eSJoe Perches			my $if_stat = $1;
474563b7c73eSJoe Perches			my $test = substr($2, 1, -1);
474663b7c73eSJoe Perches			my $herectx;
474763b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
474863b7c73eSJoe Perches				my $match = $1;
474963b7c73eSJoe Perches				# avoid parentheses around potential macro args
475063b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
475163b7c73eSJoe Perches				if (!defined($herectx)) {
475263b7c73eSJoe Perches					$herectx = $here . "\n";
475363b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
475463b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
475563b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
475663b7c73eSJoe Perches						$herectx .=  $rl . "\n";
475763b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
475863b7c73eSJoe Perches					}
475963b7c73eSJoe Perches				}
476063b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
476163b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
476263b7c73eSJoe Perches			}
476363b7c73eSJoe Perches		}
476463b7c73eSJoe Perches
47650a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
47664a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
47670a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
47683705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
47693705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
47703705ce5bSJoe Perches			    $fix) {
4771194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47723705ce5bSJoe Perches				    s/^(.)\s+/$1/;
47733705ce5bSJoe Perches			}
47740a920b5bSAndy Whitcroft		}
47750a920b5bSAndy Whitcroft
47765b9553abSJoe Perches# return is not a function
4777507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4778c45dcabdSAndy Whitcroft			my $spacing = $1;
47795b57980dSJoe Perches			if ($perl_version_ok &&
47805b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
47815b9553abSJoe Perches				my $value = $1;
47825b9553abSJoe Perches				$value = deparenthesize($value);
47835b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4784000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4785000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
47865b9553abSJoe Perches				}
4787c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4788000d1cc1SJoe Perches				ERROR("SPACING",
4789000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4790c45dcabdSAndy Whitcroft			}
4791c45dcabdSAndy Whitcroft		}
4792507e5141SJoe Perches
4793b43ae21bSJoe Perches# unnecessary return in a void function
4794b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4795b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4796b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4797b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4798b43ae21bSJoe Perches		    $linenr >= 3 &&
4799b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4800b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
48019819cf25SJoe Perches			WARN("RETURN_VOID",
4802b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
48039819cf25SJoe Perches               }
48049819cf25SJoe Perches
4805189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
48065b57980dSJoe Perches		if ($perl_version_ok &&
4807189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4808189248d8SJoe Perches			my $openparens = $1;
4809189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4810189248d8SJoe Perches			my $msg = "";
4811189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4812189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4813189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4814189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4815189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4816189248d8SJoe Perches			}
4817189248d8SJoe Perches		}
4818189248d8SJoe Perches
4819c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4820c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4821c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4822c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
48235b57980dSJoe Perches		if ($perl_version_ok &&
4824c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4825c5595fa2SJoe Perches			my $lead = $1;
4826c5595fa2SJoe Perches			my $const = $2;
4827c5595fa2SJoe Perches			my $comp = $3;
4828c5595fa2SJoe Perches			my $to = $4;
4829c5595fa2SJoe Perches			my $newcomp = $comp;
4830f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4831c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4832c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4833c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4834c5595fa2SJoe Perches			    $fix) {
4835c5595fa2SJoe Perches				if ($comp eq "<") {
4836c5595fa2SJoe Perches					$newcomp = ">";
4837c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4838c5595fa2SJoe Perches					$newcomp = ">=";
4839c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4840c5595fa2SJoe Perches					$newcomp = "<";
4841c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4842c5595fa2SJoe Perches					$newcomp = "<=";
4843c5595fa2SJoe Perches				}
4844c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4845c5595fa2SJoe Perches			}
4846c5595fa2SJoe Perches		}
4847c5595fa2SJoe Perches
4848f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4849f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
485053a3c448SAndy Whitcroft			my $name = $1;
485153a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4852000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4853f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
485453a3c448SAndy Whitcroft			}
485553a3c448SAndy Whitcroft		}
4856c45dcabdSAndy Whitcroft
48570a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
48584a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
48593705ce5bSJoe Perches			if (ERROR("SPACING",
48603705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
48613705ce5bSJoe Perches			    $fix) {
4862194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48633705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
48643705ce5bSJoe Perches			}
48650a920b5bSAndy Whitcroft		}
48660a920b5bSAndy Whitcroft
4867f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4868f5fe35ddSAndy Whitcroft# statements after the conditional.
4869170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
48703e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
48713e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
48723e469cdcSAndy Whitcroft					if (!defined $stat);
4873170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4874170d3a22SAndy Whitcroft						$remain_next, $off_next);
4875170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4876170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4877170d3a22SAndy Whitcroft
4878170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4879170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4880170d3a22SAndy Whitcroft				# then count those as offsets.
4881170d3a22SAndy Whitcroft				my ($whitespace) =
4882170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4883170d3a22SAndy Whitcroft				my $offset =
4884170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4885170d3a22SAndy Whitcroft
4886170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4887170d3a22SAndy Whitcroft								$offset} = 1;
4888170d3a22SAndy Whitcroft			}
4889170d3a22SAndy Whitcroft		}
4890170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4891c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4892170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4893171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
48948905a67cSAndy Whitcroft
4895b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4896000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4897000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
48988905a67cSAndy Whitcroft			}
48998905a67cSAndy Whitcroft
49008905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
49018905a67cSAndy Whitcroft			# conditional.
4902773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
49038905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
490413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
490553210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
490653210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4907773647a0SAndy Whitcroft			{
4908bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4909bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4910bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
491142bdf74cSHidetoshi Seto				my $stat_real = '';
4912bb44ad39SAndy Whitcroft
491342bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
491442bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4915bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4916bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4917bb44ad39SAndy Whitcroft				}
4918bb44ad39SAndy Whitcroft
4919000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4920000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
49218905a67cSAndy Whitcroft			}
49228905a67cSAndy Whitcroft		}
49238905a67cSAndy Whitcroft
492413214adfSAndy Whitcroft# Check for bitwise tests written as boolean
492513214adfSAndy Whitcroft		if ($line =~ /
492613214adfSAndy Whitcroft			(?:
492713214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
492813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
492913214adfSAndy Whitcroft				(?:\&\&|\|\|)
493013214adfSAndy Whitcroft			|
493113214adfSAndy Whitcroft				(?:\&\&|\|\|)
493213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
493313214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
493413214adfSAndy Whitcroft			)/x)
493513214adfSAndy Whitcroft		{
4936000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4937000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
493813214adfSAndy Whitcroft		}
493913214adfSAndy Whitcroft
49408905a67cSAndy Whitcroft# if and else should not have general statements after it
494113214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
494213214adfSAndy Whitcroft			my $s = $1;
494313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
494413214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4945000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4946000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
49470a920b5bSAndy Whitcroft			}
494813214adfSAndy Whitcroft		}
494939667782SAndy Whitcroft# if should not continue a brace
495039667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4951000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4952048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
495339667782SAndy Whitcroft				$herecurr);
495439667782SAndy Whitcroft		}
4955a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4956a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4957a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
49583fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4959a1080bf8SAndy Whitcroft			\s*return\s+
4960a1080bf8SAndy Whitcroft		    )/xg)
4961a1080bf8SAndy Whitcroft		{
4962000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4963000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4964a1080bf8SAndy Whitcroft		}
49650a920b5bSAndy Whitcroft
49660a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
49670a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
49688b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
49690a920b5bSAndy Whitcroft		    $previndent == $indent) {
49708b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
49718b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
49728b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49738b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
49748b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49758b8856f4SJoe Perches				my $fixedline = $prevrawline;
49768b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
49778b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
49788b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49798b8856f4SJoe Perches				}
49808b8856f4SJoe Perches				$fixedline = $rawline;
49818b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
49828b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
49838b8856f4SJoe Perches			}
49840a920b5bSAndy Whitcroft		}
49850a920b5bSAndy Whitcroft
49868b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4987c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4988c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4989c2fdda0dSAndy Whitcroft
4990c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4991c2fdda0dSAndy Whitcroft			# conditional.
4992773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4993c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4994c2fdda0dSAndy Whitcroft
4995c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
49968b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
49978b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
49988b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49998b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
50008b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
50018b8856f4SJoe Perches					my $fixedline = $prevrawline;
50028b8856f4SJoe Perches					my $trailing = $rawline;
50038b8856f4SJoe Perches					$trailing =~ s/^\+//;
50048b8856f4SJoe Perches					$trailing = trim($trailing);
50058b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
50068b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50078b8856f4SJoe Perches				}
5008c2fdda0dSAndy Whitcroft			}
5009c2fdda0dSAndy Whitcroft		}
5010c2fdda0dSAndy Whitcroft
501195e2c602SJoe Perches#Specific variable tests
5012323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5013323c1260SJoe Perches			my $var = $1;
501495e2c602SJoe Perches
501595e2c602SJoe Perches#CamelCase
5016807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5017be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
501822735ce8SJoe Perches#Ignore Page<foo> variants
5019807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
502022735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
5021f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
5022f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5023f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
50247e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
50257e781f67SJoe Perches					my $word = $1;
50267e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5027d8b07710SJoe Perches					if ($check) {
5028d8b07710SJoe Perches						seed_camelcase_includes();
5029d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5030d8b07710SJoe Perches							seed_camelcase_file($realfile);
5031d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5032d8b07710SJoe Perches						}
5033d8b07710SJoe Perches					}
50347e781f67SJoe Perches					if (!defined $camelcase{$word}) {
50357e781f67SJoe Perches						$camelcase{$word} = 1;
5036be79794bSJoe Perches						CHK("CAMELCASE",
50377e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
50387e781f67SJoe Perches					}
5039323c1260SJoe Perches				}
5040323c1260SJoe Perches			}
50413445686aSJoe Perches		}
50420a920b5bSAndy Whitcroft
50430a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5044d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5045d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5046d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5047d5e616fcSJoe Perches			    $fix) {
5048194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5049d5e616fcSJoe Perches			}
50500a920b5bSAndy Whitcroft		}
50510a920b5bSAndy Whitcroft
50520e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
50530e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5054c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5055e09dec48SAndy Whitcroft			my $file = "$1.h";
5056e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5057e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5058e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
50597840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5060c45dcabdSAndy Whitcroft			{
50610e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
50620e212e0aSFabian Frederick				if ($asminclude > 0) {
5063e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5064000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5065000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5066e09dec48SAndy Whitcroft					} else {
5067000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5068000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5069e09dec48SAndy Whitcroft					}
50700a920b5bSAndy Whitcroft				}
50710a920b5bSAndy Whitcroft			}
50720e212e0aSFabian Frederick		}
50730a920b5bSAndy Whitcroft
5074653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5075653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5076cf655043SAndy Whitcroft# in a known good container
5077b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5078b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5079d8aaf121SAndy Whitcroft			my $ln = $linenr;
5080d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5081c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5082c45dcabdSAndy Whitcroft			my $ctx = '';
508308a2843eSJoe Perches			my $has_flow_statement = 0;
508408a2843eSJoe Perches			my $has_arg_concat = 0;
5085c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5086f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5087f74bd194SAndy Whitcroft			$ctx = $dstat;
5088c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5089a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5090c45dcabdSAndy Whitcroft
509108a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
509262e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
509308a2843eSJoe Perches
5094f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5095f59b64bfSJoe Perches			my $define_args = $1;
5096f59b64bfSJoe Perches			my $define_stmt = $dstat;
5097f59b64bfSJoe Perches			my @def_args = ();
5098f59b64bfSJoe Perches
5099f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5100f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5101f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
51028c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5103f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5104f59b64bfSJoe Perches			}
5105f59b64bfSJoe Perches
5106292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5107c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5108c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5109c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5110c45dcabdSAndy Whitcroft
5111c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5112bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5113bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
51146b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5115bf30d6edSAndy Whitcroft			{
5116c45dcabdSAndy Whitcroft			}
5117c45dcabdSAndy Whitcroft
5118e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
511933acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
512033acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5121e45bab8eSAndy Whitcroft			{
5122e45bab8eSAndy Whitcroft			}
5123e45bab8eSAndy Whitcroft
512442e15293SJoe Perches			# Make asm volatile uses seem like a generic function
512542e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
512642e15293SJoe Perches
5127c45dcabdSAndy Whitcroft			my $exceptions = qr{
5128c45dcabdSAndy Whitcroft				$Declare|
5129c45dcabdSAndy Whitcroft				module_param_named|
5130a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5131c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5132c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5133383099fdSAndy Whitcroft				__typeof__\(|
513422fd2d3eSStefani Seibold				union|
513522fd2d3eSStefani Seibold				struct|
5136ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
51376b10df42SVladimir Zapolskiy				^\"|\"$|
51386b10df42SVladimir Zapolskiy				^\[
5139c45dcabdSAndy Whitcroft			}x;
51405eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5141f59b64bfSJoe Perches
5142f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5143f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5144e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5145f59b64bfSJoe Perches
5146f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5147f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5148f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
51493cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5150356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5151f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5152f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5153e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
515472f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5155f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5156f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5157f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
51584e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5159f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5160c45dcabdSAndy Whitcroft			{
5161e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5162e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5163e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5164e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5165f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5166f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5167f74bd194SAndy Whitcroft				} else {
5168000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5169388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5170d8aaf121SAndy Whitcroft				}
5171f59b64bfSJoe Perches
5172f59b64bfSJoe Perches			}
51735207649bSJoe Perches
51745207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
51755207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
51765207649bSJoe Perches			my $first = 1;
51775207649bSJoe Perches			$define_stmt = "";
51785207649bSJoe Perches			foreach my $l (@stmt_array) {
51795207649bSJoe Perches				$l =~ s/\\$//;
51805207649bSJoe Perches				if ($first) {
51815207649bSJoe Perches					$define_stmt = $l;
51825207649bSJoe Perches					$first = 0;
51835207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
51845207649bSJoe Perches					$define_stmt .= substr($l, 1);
51855207649bSJoe Perches				}
51865207649bSJoe Perches			}
51875207649bSJoe Perches			$define_stmt =~ s/$;//g;
51885207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
51895207649bSJoe Perches			$define_stmt = trim($define_stmt);
51905207649bSJoe Perches
5191f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5192f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5193f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
51949192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
51957fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
51967fe528a2SJoe Perches				$tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
51977fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
51987fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5199d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5200f59b64bfSJoe Perches				if ($use_cnt > 1) {
5201f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5202f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5203f59b64bfSJoe Perches				    }
52049192d41aSJoe Perches# check if any macro arguments may have other precedence issues
52057fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
52069192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
52079192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
52089192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
52099192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
52109192d41aSJoe Perches				}
52110a920b5bSAndy Whitcroft			}
52125023d347SJoe Perches
521308a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
521408a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
521508a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
521608a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5217e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
521808a2843eSJoe Perches
521908a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
522008a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
522108a2843eSJoe Perches			}
522208a2843eSJoe Perches
5223481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
52245023d347SJoe Perches
52255023d347SJoe Perches		} else {
52265023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5227481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5228481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
52295023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
52305023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
52315023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
52325023d347SJoe Perches			}
5233653d4876SAndy Whitcroft		}
52340a920b5bSAndy Whitcroft
5235b13edf7fSJoe Perches# do {} while (0) macro tests:
5236b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5237b13edf7fSJoe Perches# macro should not end with a semicolon
52385b57980dSJoe Perches		if ($perl_version_ok &&
5239b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5240b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5241b13edf7fSJoe Perches			my $ln = $linenr;
5242b13edf7fSJoe Perches			my $cnt = $realcnt;
5243b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5244b13edf7fSJoe Perches			my $ctx = '';
5245b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5246b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5247b13edf7fSJoe Perches			$ctx = $dstat;
5248b13edf7fSJoe Perches
5249b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
52501b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5251b13edf7fSJoe Perches
5252b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5253b13edf7fSJoe Perches				my $stmts = $2;
5254b13edf7fSJoe Perches				my $semis = $3;
5255b13edf7fSJoe Perches
5256b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5257b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5258e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5259b13edf7fSJoe Perches
5260ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5261ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5262b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5263b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5264b13edf7fSJoe Perches				}
5265b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5266b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5267b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5268b13edf7fSJoe Perches				}
5269f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5270f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5271f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5272e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5273f5ef95b1SJoe Perches
5274f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5275f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5276b13edf7fSJoe Perches			}
5277b13edf7fSJoe Perches		}
5278b13edf7fSJoe Perches
5279f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
528013214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
528113214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5282cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
528313214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5284cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5285cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5286aad4f614SJoe Perches				my @allowed = ();
5287aad4f614SJoe Perches				my $allow = 0;
528813214adfSAndy Whitcroft				my $seen = 0;
5289773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5290cf655043SAndy Whitcroft				my $ln = $linenr - 1;
529113214adfSAndy Whitcroft				for my $chunk (@chunks) {
529213214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
529313214adfSAndy Whitcroft
5294773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5295773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5296773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5297773647a0SAndy Whitcroft
5298aad4f614SJoe Perches					$allowed[$allow] = 0;
5299773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5300773647a0SAndy Whitcroft
5301773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5302773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5303773647a0SAndy Whitcroft
5304773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5305cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5306cf655043SAndy Whitcroft
5307773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
530813214adfSAndy Whitcroft
530913214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
531013214adfSAndy Whitcroft
5311aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5312cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5313cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5314aad4f614SJoe Perches						$allowed[$allow] = 1;
531513214adfSAndy Whitcroft					}
531613214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5317cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5318aad4f614SJoe Perches						$allowed[$allow] = 1;
531913214adfSAndy Whitcroft					}
5320cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5321cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5322aad4f614SJoe Perches						$allowed[$allow] = 1;
532313214adfSAndy Whitcroft					}
5324aad4f614SJoe Perches					$allow++;
532513214adfSAndy Whitcroft				}
5326aad4f614SJoe Perches				if ($seen) {
5327aad4f614SJoe Perches					my $sum_allowed = 0;
5328aad4f614SJoe Perches					foreach (@allowed) {
5329aad4f614SJoe Perches						$sum_allowed += $_;
5330aad4f614SJoe Perches					}
5331aad4f614SJoe Perches					if ($sum_allowed == 0) {
5332000d1cc1SJoe Perches						WARN("BRACES",
5333000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5334aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5335aad4f614SJoe Perches						 $seen != $allow) {
5336aad4f614SJoe Perches						CHK("BRACES",
5337aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5338aad4f614SJoe Perches					}
533913214adfSAndy Whitcroft				}
534013214adfSAndy Whitcroft			}
534113214adfSAndy Whitcroft		}
5342773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
534313214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5344cf655043SAndy Whitcroft			my $allowed = 0;
5345f0a594c1SAndy Whitcroft
5346cf655043SAndy Whitcroft			# Check the pre-context.
5347cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5348cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5349cf655043SAndy Whitcroft				$allowed = 1;
5350f0a594c1SAndy Whitcroft			}
5351773647a0SAndy Whitcroft
5352773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5353773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5354773647a0SAndy Whitcroft
5355cf655043SAndy Whitcroft			# Check the condition.
5356cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5357773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5358cf655043SAndy Whitcroft			if (defined $cond) {
5359773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5360cf655043SAndy Whitcroft			}
5361cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5362cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5363cf655043SAndy Whitcroft				$allowed = 1;
5364cf655043SAndy Whitcroft			}
5365cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5366cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5367cf655043SAndy Whitcroft				$allowed = 1;
5368cf655043SAndy Whitcroft			}
5369cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5370cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5371cf655043SAndy Whitcroft				$allowed = 1;
5372cf655043SAndy Whitcroft			}
5373cf655043SAndy Whitcroft			# Check the post-context.
5374cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5375cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5376cf655043SAndy Whitcroft				if (defined $cond) {
5377773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5378cf655043SAndy Whitcroft				}
5379cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5380cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5381cf655043SAndy Whitcroft					$allowed = 1;
5382cf655043SAndy Whitcroft				}
5383cf655043SAndy Whitcroft			}
5384cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5385f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5386e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5387cf655043SAndy Whitcroft
5388000d1cc1SJoe Perches				WARN("BRACES",
5389000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5390f0a594c1SAndy Whitcroft			}
5391f0a594c1SAndy Whitcroft		}
5392f0a594c1SAndy Whitcroft
5393e4c5babdSJoe Perches# check for single line unbalanced braces
539495330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
539595330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5396e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5397e4c5babdSJoe Perches		}
5398e4c5babdSJoe Perches
53990979ae66SJoe Perches# check for unnecessary blank lines around braces
540077b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5401f8e58219SJoe Perches			if (CHK("BRACES",
5402f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5403f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5404f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5405f8e58219SJoe Perches			}
54060979ae66SJoe Perches		}
540777b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5408f8e58219SJoe Perches			if (CHK("BRACES",
5409f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5410f8e58219SJoe Perches			    $fix) {
5411f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5412f8e58219SJoe Perches			}
54130979ae66SJoe Perches		}
54140979ae66SJoe Perches
54154a0df2efSAndy Whitcroft# no volatiles please
54166c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
54176c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5418000d1cc1SJoe Perches			WARN("VOLATILE",
54198c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
54204a0df2efSAndy Whitcroft		}
54214a0df2efSAndy Whitcroft
54225e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
54235e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
54245e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
54255e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
542633acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
54275e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
54285e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
54295e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
54305e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
54315e4f6ba5SJoe Perches				     $fix &&
54325e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
54335e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
54345e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
54355e4f6ba5SJoe Perches				my $comma_close = "";
54365e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
54375e4f6ba5SJoe Perches					$comma_close = $1;
54385e4f6ba5SJoe Perches				}
54395e4f6ba5SJoe Perches
54405e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
54415e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
54425e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
54435e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
54445e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
54455e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
54465e4f6ba5SJoe Perches				$fixedline = $rawline;
54475e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
54485e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
54495e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54505e4f6ba5SJoe Perches				}
54515e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
54525e4f6ba5SJoe Perches			}
54535e4f6ba5SJoe Perches		}
54545e4f6ba5SJoe Perches
54555e4f6ba5SJoe Perches# check for missing a space in a string concatenation
54565e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
54575e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
54585e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
54595e4f6ba5SJoe Perches		}
54605e4f6ba5SJoe Perches
546177cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5462e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5463e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5464e4b7d309SJoe Perches# function declarations
546577cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
546677cb8546SJoe Perches		    defined($context_function) &&
5467e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5468e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
546977cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5470e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
547177cb8546SJoe Perches		}
547277cb8546SJoe Perches
54735e4f6ba5SJoe Perches# check for spaces before a quoted newline
54745e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
54755e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
54765e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
54775e4f6ba5SJoe Perches			    $fix) {
54785e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
54795e4f6ba5SJoe Perches			}
54805e4f6ba5SJoe Perches
54815e4f6ba5SJoe Perches		}
54825e4f6ba5SJoe Perches
5483f17dba4fSJoe Perches# concatenated string without spaces between elements
548479682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
548579682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
548679682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
548779682c0cSJoe Perches			    $fix) {
548879682c0cSJoe Perches				while ($line =~ /($String)/g) {
548979682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
549079682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
549179682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
549279682c0cSJoe Perches				}
549379682c0cSJoe Perches			}
5494f17dba4fSJoe Perches		}
5495f17dba4fSJoe Perches
549690ad30e5SJoe Perches# uncoalesced string fragments
549733acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
549879682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
549979682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
550079682c0cSJoe Perches			    $fix) {
550179682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
550279682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
550379682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
550479682c0cSJoe Perches				}
550579682c0cSJoe Perches			}
550690ad30e5SJoe Perches		}
550790ad30e5SJoe Perches
5508522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5509522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5510522b837cSAlexey Dobriyan		my $show_Z = 1;
55115e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5512522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
55135e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5514522b837cSAlexey Dobriyan			# check for %L
5515522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
55165e4f6ba5SJoe Perches				WARN("PRINTF_L",
5517522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5518522b837cSAlexey Dobriyan				$show_L = 0;
55195e4f6ba5SJoe Perches			}
5520522b837cSAlexey Dobriyan			# check for %Z
5521522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5522522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5523522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5524522b837cSAlexey Dobriyan				$show_Z = 0;
5525522b837cSAlexey Dobriyan			}
5526522b837cSAlexey Dobriyan			# check for 0x<decimal>
5527522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5528522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
55296e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
55306e300757SJoe Perches			}
55315e4f6ba5SJoe Perches		}
55325e4f6ba5SJoe Perches
55335e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
55343f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
55355e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
55365e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
55375e4f6ba5SJoe Perches		}
55385e4f6ba5SJoe Perches
553900df344fSAndy Whitcroft# warn about #if 0
5540c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
554160f89010SPrakruthi Deepak Heragu			WARN("IF_0",
554260f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
554360f89010SPrakruthi Deepak Heragu		}
554460f89010SPrakruthi Deepak Heragu
554560f89010SPrakruthi Deepak Heragu# warn about #if 1
554660f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
554760f89010SPrakruthi Deepak Heragu			WARN("IF_1",
554860f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
55494a0df2efSAndy Whitcroft		}
55504a0df2efSAndy Whitcroft
555103df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
555203df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5553100425deSJoe Perches			my $tested = quotemeta($1);
5554100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5555100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5556100425deSJoe Perches				my $func = $1;
5557100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5558100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5559100425deSJoe Perches				    $fix) {
5560100425deSJoe Perches					my $do_fix = 1;
5561100425deSJoe Perches					my $leading_tabs = "";
5562100425deSJoe Perches					my $new_leading_tabs = "";
5563100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5564100425deSJoe Perches						$leading_tabs = $1;
5565100425deSJoe Perches					} else {
5566100425deSJoe Perches						$do_fix = 0;
5567100425deSJoe Perches					}
5568100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5569100425deSJoe Perches						$new_leading_tabs = $1;
5570100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5571100425deSJoe Perches							$do_fix = 0;
5572100425deSJoe Perches						}
5573100425deSJoe Perches					} else {
5574100425deSJoe Perches						$do_fix = 0;
5575100425deSJoe Perches					}
5576100425deSJoe Perches					if ($do_fix) {
5577100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5578100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5579100425deSJoe Perches					}
5580100425deSJoe Perches				}
55814c432a8fSGreg Kroah-Hartman			}
55824c432a8fSGreg Kroah-Hartman		}
5583f0a594c1SAndy Whitcroft
5584ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5585ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5586ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5587ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5588ebfdc409SJoe Perches		    $linenr > 3) {
5589ebfdc409SJoe Perches			my $testval = $2;
5590ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5591ebfdc409SJoe Perches
5592ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5593ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5594ebfdc409SJoe Perches
5595e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5596e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
5597ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5598ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5599ebfdc409SJoe Perches			}
5600ebfdc409SJoe Perches		}
5601ebfdc409SJoe Perches
5602f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5603dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5604f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5605f78d98f6SJoe Perches			my $level = $1;
5606f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5607f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5608f78d98f6SJoe Perches			    $fix) {
5609f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5610f78d98f6SJoe Perches			}
5611f78d98f6SJoe Perches		}
5612f78d98f6SJoe Perches
561345c55e92SJoe Perches# check for logging continuations
561445c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
561545c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
561645c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
561745c55e92SJoe Perches		}
561845c55e92SJoe Perches
5619abb08a53SJoe Perches# check for mask then right shift without a parentheses
56205b57980dSJoe Perches		if ($perl_version_ok &&
5621abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5622abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5623abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5624abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5625abb08a53SJoe Perches		}
5626abb08a53SJoe Perches
5627b75ac618SJoe Perches# check for pointer comparisons to NULL
56285b57980dSJoe Perches		if ($perl_version_ok) {
5629b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5630b75ac618SJoe Perches				my $val = $1;
5631b75ac618SJoe Perches				my $equal = "!";
5632b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5633b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5634b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5635b75ac618SJoe Perches					    $fix) {
5636b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5637b75ac618SJoe Perches				}
5638b75ac618SJoe Perches			}
5639b75ac618SJoe Perches		}
5640b75ac618SJoe Perches
56418716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
56428716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
56438716de38SJoe Perches			my $attr = $1;
56448716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
56458716de38SJoe Perches				my $ptr = $1;
56468716de38SJoe Perches				my $var = $2;
56478716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
56488716de38SJoe Perches				      ERROR("MISPLACED_INIT",
56498716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
56508716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
56518716de38SJoe Perches				      WARN("MISPLACED_INIT",
56528716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
56538716de38SJoe Perches				    $fix) {
5654194f66fcSJoe 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;
56558716de38SJoe Perches				}
56568716de38SJoe Perches			}
56578716de38SJoe Perches		}
56588716de38SJoe Perches
5659e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5660e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5661e970b884SJoe Perches			my $attr = $1;
5662e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5663e970b884SJoe Perches			my $attr_prefix = $1;
5664e970b884SJoe Perches			my $attr_type = $2;
5665e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5666e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5667e970b884SJoe Perches			    $fix) {
5668194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5669e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5670e970b884SJoe Perches			}
5671e970b884SJoe Perches		}
5672e970b884SJoe Perches
5673e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5674e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5675e970b884SJoe Perches			my $attr = $1;
5676e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5677e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5678e970b884SJoe Perches			    $fix) {
5679194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5680e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5681e970b884SJoe Perches				$lead = rtrim($1);
5682e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5683e970b884SJoe Perches				$lead = "${lead}const ";
5684194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5685e970b884SJoe Perches			}
5686e970b884SJoe Perches		}
5687e970b884SJoe Perches
5688c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5689c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5690c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5691c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5692c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5693c17893c7SJoe Perches			    $fix) {
5694c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5695c17893c7SJoe Perches			}
5696c17893c7SJoe Perches		}
5697c17893c7SJoe Perches
5698fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5699fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5700fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5701fbdb8138SJoe Perches			my $constant_func = $1;
5702fbdb8138SJoe Perches			my $func = $constant_func;
5703fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5704fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5705fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5706fbdb8138SJoe Perches			    $fix) {
5707194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5708fbdb8138SJoe Perches			}
5709fbdb8138SJoe Perches		}
5710fbdb8138SJoe Perches
57111a15a250SPatrick Pannuto# prefer usleep_range over udelay
571237581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
571343c1d77cSJoe Perches			my $delay = $1;
57141a15a250SPatrick Pannuto			# ignore udelay's < 10, however
571543c1d77cSJoe Perches			if (! ($delay < 10) ) {
5716000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
5717458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
571843c1d77cSJoe Perches			}
571943c1d77cSJoe Perches			if ($delay > 2000) {
572043c1d77cSJoe Perches				WARN("LONG_UDELAY",
572143c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
57221a15a250SPatrick Pannuto			}
57231a15a250SPatrick Pannuto		}
57241a15a250SPatrick Pannuto
572509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
572609ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
572709ef8725SPatrick Pannuto			if ($1 < 20) {
5728000d1cc1SJoe Perches				WARN("MSLEEP",
5729458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
573009ef8725SPatrick Pannuto			}
573109ef8725SPatrick Pannuto		}
573209ef8725SPatrick Pannuto
573336ec1939SJoe Perches# check for comparisons of jiffies
573436ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
573536ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
573636ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
573736ec1939SJoe Perches		}
573836ec1939SJoe Perches
57399d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
57409d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
57419d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
57429d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
57439d7a34a5SJoe Perches		}
57449d7a34a5SJoe Perches
574500df344fSAndy Whitcroft# warn about #ifdefs in C files
5746c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
574700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
574800df344fSAndy Whitcroft#			print "$herecurr";
574900df344fSAndy Whitcroft#			$clean = 0;
575000df344fSAndy Whitcroft#		}
575100df344fSAndy Whitcroft
575222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5753c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
57543705ce5bSJoe Perches			if (ERROR("SPACING",
57553705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
57563705ce5bSJoe Perches			    $fix) {
5757194f66fcSJoe Perches				$fixed[$fixlinenr] =~
57583705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
57593705ce5bSJoe Perches			}
57603705ce5bSJoe Perches
576122f2a2efSAndy Whitcroft		}
576222f2a2efSAndy Whitcroft
57634a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5764171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5765171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
57664a0df2efSAndy Whitcroft			my $which = $1;
57674a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5768000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5769000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
57704a0df2efSAndy Whitcroft			}
57714a0df2efSAndy Whitcroft		}
57724a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5773402c2553SMichael S. Tsirkin
5774402c2553SMichael S. Tsirkin		my $barriers = qr{
5775402c2553SMichael S. Tsirkin			mb|
5776402c2553SMichael S. Tsirkin			rmb|
5777402c2553SMichael S. Tsirkin			wmb|
5778402c2553SMichael S. Tsirkin			read_barrier_depends
5779402c2553SMichael S. Tsirkin		}x;
5780402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5781402c2553SMichael S. Tsirkin			mb__before_atomic|
5782402c2553SMichael S. Tsirkin			mb__after_atomic|
5783402c2553SMichael S. Tsirkin			store_release|
5784402c2553SMichael S. Tsirkin			load_acquire|
5785402c2553SMichael S. Tsirkin			store_mb|
5786402c2553SMichael S. Tsirkin			(?:$barriers)
5787402c2553SMichael S. Tsirkin		}x;
5788402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5789402c2553SMichael S. Tsirkin			(?:$barriers)|
579043e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
579143e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5792402c2553SMichael S. Tsirkin		}x;
5793402c2553SMichael S. Tsirkin
5794402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
57954a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5796c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5797000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
57984a0df2efSAndy Whitcroft			}
57994a0df2efSAndy Whitcroft		}
58003ad81779SPaul E. McKenney
5801f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5802f4073b0fSMichael S. Tsirkin
5803f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5804f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5805f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5806f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5807f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5808f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5809f4073b0fSMichael S. Tsirkin		}
5810f4073b0fSMichael S. Tsirkin
5811cb426e99SJoe Perches# check for waitqueue_active without a comment.
5812cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5813cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5814cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5815cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5816cb426e99SJoe Perches			}
5817cb426e99SJoe Perches		}
58183ad81779SPaul E. McKenney
581991db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
582091db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
582191db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
582291db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
582391db2592SPaul E. McKenney		}
582491db2592SPaul E. McKenney
58254a0df2efSAndy Whitcroft# check of hardware specific defines
5826c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5827000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5828000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
58290a920b5bSAndy Whitcroft		}
5830653d4876SAndy Whitcroft
5831596ed45bSJoe Perches# check that the storage class is not after a type
5832596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5833000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5834596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5835596ed45bSJoe Perches		}
5836596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5837596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5838596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5839596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5840596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5841596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5842596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5843d4977c78STobias Klauser		}
5844d4977c78STobias Klauser
5845de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5846de7d4f0eSAndy Whitcroft# storage class and type.
58479c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
58489c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5849000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5850000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5851de7d4f0eSAndy Whitcroft		}
5852de7d4f0eSAndy Whitcroft
58538905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
58542b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58552b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5856d5e616fcSJoe Perches			if (WARN("INLINE",
5857d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5858d5e616fcSJoe Perches			    $fix) {
5859194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5860d5e616fcSJoe Perches
5861d5e616fcSJoe Perches			}
58628905a67cSAndy Whitcroft		}
58638905a67cSAndy Whitcroft
58643d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
58652b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58662b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5867000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5868000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
58693d130fd0SJoe Perches		}
58703d130fd0SJoe Perches
587139b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
58722b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58732b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5874000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5875000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
587639b7e287SJoe Perches		}
587739b7e287SJoe Perches
58785f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
58792b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58802b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5881d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5882d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5883d5e616fcSJoe Perches			    $fix) {
5884194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5885d5e616fcSJoe Perches
5886d5e616fcSJoe Perches			}
58875f14d3bdSJoe Perches		}
58885f14d3bdSJoe Perches
58896061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
58902b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58912b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5892d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5893d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5894d5e616fcSJoe Perches			    $fix) {
5895194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5896d5e616fcSJoe Perches			}
58976061d949SJoe Perches		}
58986061d949SJoe Perches
5899619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
59005b57980dSJoe Perches		if ($perl_version_ok &&
5901619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5902619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5903619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5904619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5905619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5906619a908aSJoe Perches		}
5907619a908aSJoe Perches
5908fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5909e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5910fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5911e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5912e6176fa4SJoe Perches			my $type = $1;
5913e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5914e6176fa4SJoe Perches				$type = $1;
5915e6176fa4SJoe Perches				my $kernel_type = 'u';
5916e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5917e6176fa4SJoe Perches				$type =~ /(\d+)/;
5918e6176fa4SJoe Perches				$kernel_type .= $1;
5919e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5920e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5921e6176fa4SJoe Perches				    $fix) {
5922e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5923e6176fa4SJoe Perches				}
5924e6176fa4SJoe Perches			}
5925e6176fa4SJoe Perches		}
5926e6176fa4SJoe Perches
5927938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5928938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5929938224b5SJoe Perches			my $cast = $1;
5930938224b5SJoe Perches			my $const = $2;
5931938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5932938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5933938224b5SJoe Perches			    $fix) {
5934938224b5SJoe Perches				my $suffix = "";
5935938224b5SJoe Perches				my $newconst = $const;
5936938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5937938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5938938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5939938224b5SJoe Perches					$suffix .= 'LL';
5940938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5941938224b5SJoe Perches					$suffix .= 'L';
5942938224b5SJoe Perches				}
5943938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5944938224b5SJoe Perches			}
5945938224b5SJoe Perches		}
5946938224b5SJoe Perches
59478f53a9b8SJoe Perches# check for sizeof(&)
59488f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5949000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5950000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
59518f53a9b8SJoe Perches		}
59528f53a9b8SJoe Perches
595366c80b60SJoe Perches# check for sizeof without parenthesis
595466c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5955d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5956d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5957d5e616fcSJoe Perches			    $fix) {
5958194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5959d5e616fcSJoe Perches			}
596066c80b60SJoe Perches		}
596166c80b60SJoe Perches
596288982feaSJoe Perches# check for struct spinlock declarations
596388982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
596488982feaSJoe Perches			WARN("USE_SPINLOCK_T",
596588982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
596688982feaSJoe Perches		}
596788982feaSJoe Perches
5968a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
596906668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5970a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5971caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5972caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5973d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5974d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5975d5e616fcSJoe Perches				    $fix) {
5976194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5977d5e616fcSJoe Perches				}
5978a6962d72SJoe Perches			}
5979a6962d72SJoe Perches		}
5980a6962d72SJoe Perches
59810b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
59825b57980dSJoe Perches		if ($perl_version_ok &&
59830b523769SJoe Perches		    defined $stat &&
59840b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
59850b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
5986e3c6bc95STobin C. Harding			my $stat_real;
5987e3c6bc95STobin C. Harding
59880b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
59890b523769SJoe Perches			$lc = $lc + $linenr;
59900b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
5991ffe07513SJoe Perches				my $specifier;
5992ffe07513SJoe Perches				my $extension;
5993ffe07513SJoe Perches				my $bad_specifier = "";
59940b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
59950b523769SJoe Perches				$fmt =~ s/%%//g;
5996e3c6bc95STobin C. Harding
5997e3c6bc95STobin C. Harding				while ($fmt =~ /(\%[\*\d\.]*p(\w))/g) {
5998e3c6bc95STobin C. Harding					$specifier = $1;
5999e3c6bc95STobin C. Harding					$extension = $2;
60004462996eSAlexandre Belloni					if ($extension !~ /[SsBKRraEhMmIiUDdgVCbGNOxt]/) {
6001e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
60020b523769SJoe Perches						last;
60030b523769SJoe Perches					}
6004e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6005e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6006e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
60070b523769SJoe Perches						}
6008e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6009e3c6bc95STobin C. Harding						     "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n");
6010e3c6bc95STobin C. Harding					}
6011e3c6bc95STobin C. Harding				}
6012e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
60132a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
60141df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
60151df7338aSSergey Senozhatsky					my $use = "";
6016e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
60171df7338aSSergey Senozhatsky						$ext_type = "Deprecated";
60181df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6019e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
60201df7338aSSergey Senozhatsky					}
60212a9f9d85STobin C. Harding
60220b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6023e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6024e3c6bc95STobin C. Harding				}
60250b523769SJoe Perches			}
60260b523769SJoe Perches		}
60270b523769SJoe Perches
6028554e165cSAndy Whitcroft# Check for misused memsets
60295b57980dSJoe Perches		if ($perl_version_ok &&
6030d1fe9c09SJoe Perches		    defined $stat &&
60319e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6032554e165cSAndy Whitcroft
6033d7c76ba7SJoe Perches			my $ms_addr = $2;
6034d1fe9c09SJoe Perches			my $ms_val = $7;
6035d1fe9c09SJoe Perches			my $ms_size = $12;
6036d7c76ba7SJoe Perches
6037554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6038554e165cSAndy Whitcroft				ERROR("MEMSET",
6039d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6040554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6041554e165cSAndy Whitcroft				WARN("MEMSET",
6042d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6043d7c76ba7SJoe Perches			}
6044d7c76ba7SJoe Perches		}
6045d7c76ba7SJoe Perches
604698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
60475b57980dSJoe Perches#		if ($perl_version_ok &&
6048f333195dSJoe Perches#		    defined $stat &&
6049f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6050f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6051f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6052f333195dSJoe Perches#			    $fix) {
6053f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6054f333195dSJoe Perches#			}
6055f333195dSJoe Perches#		}
605698a9bba5SJoe Perches
6057b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
60585b57980dSJoe Perches#		if ($perl_version_ok &&
6059f333195dSJoe Perches#		    defined $stat &&
6060f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6061f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6062f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6063f333195dSJoe Perches#		}
6064b6117d17SMateusz Kulikowski
60658617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
60668617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
60675b57980dSJoe Perches#		if ($perl_version_ok &&
6068f333195dSJoe Perches#		    defined $stat &&
6069f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6070f333195dSJoe Perches#
6071f333195dSJoe Perches#			my $ms_val = $7;
6072f333195dSJoe Perches#
6073f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6074f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6075f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6076f333195dSJoe Perches#				    $fix) {
6077f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6078f333195dSJoe Perches#				}
6079f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6080f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6081f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6082f333195dSJoe Perches#				    $fix) {
6083f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6084f333195dSJoe Perches#				}
6085f333195dSJoe Perches#			}
6086f333195dSJoe Perches#		}
60878617cd09SMateusz Kulikowski
6088d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
60895b57980dSJoe Perches		if ($perl_version_ok &&
6090d1fe9c09SJoe Perches		    defined $stat &&
6091d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6092d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6093d7c76ba7SJoe Perches				my $call = $1;
6094d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6095d7c76ba7SJoe Perches				my $arg1 = $3;
6096d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6097d1fe9c09SJoe Perches				my $arg2 = $8;
6098d7c76ba7SJoe Perches				my $cast;
6099d7c76ba7SJoe Perches
6100d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6101d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6102d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6103d7c76ba7SJoe Perches					$cast = $cast1;
6104d7c76ba7SJoe Perches				} else {
6105d7c76ba7SJoe Perches					$cast = $cast2;
6106d7c76ba7SJoe Perches				}
6107d7c76ba7SJoe Perches				WARN("MINMAX",
6108d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6109554e165cSAndy Whitcroft			}
6110554e165cSAndy Whitcroft		}
6111554e165cSAndy Whitcroft
61124a273195SJoe Perches# check usleep_range arguments
61135b57980dSJoe Perches		if ($perl_version_ok &&
61144a273195SJoe Perches		    defined $stat &&
61154a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
61164a273195SJoe Perches			my $min = $1;
61174a273195SJoe Perches			my $max = $7;
61184a273195SJoe Perches			if ($min eq $max) {
61194a273195SJoe Perches				WARN("USLEEP_RANGE",
6120458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61214a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
61224a273195SJoe Perches				 $min > $max) {
61234a273195SJoe Perches				WARN("USLEEP_RANGE",
6124458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61254a273195SJoe Perches			}
61264a273195SJoe Perches		}
61274a273195SJoe Perches
6128823b794cSJoe Perches# check for naked sscanf
61295b57980dSJoe Perches		if ($perl_version_ok &&
6130823b794cSJoe Perches		    defined $stat &&
61316c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6132823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6133823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6134823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6135823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6136823b794cSJoe Perches			$lc = $lc + $linenr;
61372a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6138823b794cSJoe Perches			WARN("NAKED_SSCANF",
6139823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6140823b794cSJoe Perches		}
6141823b794cSJoe Perches
6142afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
61435b57980dSJoe Perches		if ($perl_version_ok &&
6144afc819abSJoe Perches		    defined $stat &&
6145afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6146afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6147afc819abSJoe Perches			$lc = $lc + $linenr;
61482a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6149afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6150afc819abSJoe Perches				my $format = $6;
6151afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6152afc819abSJoe Perches				if ($count == 1 &&
6153afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6154afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6155afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6156afc819abSJoe Perches				}
6157afc819abSJoe Perches			}
6158afc819abSJoe Perches		}
6159afc819abSJoe Perches
616070dc8a48SJoe Perches# check for new externs in .h files.
616170dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
616270dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6163d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
616470dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
616570dc8a48SJoe Perches			    $fix) {
6166194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
616770dc8a48SJoe Perches			}
616870dc8a48SJoe Perches		}
616970dc8a48SJoe Perches
6170de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6171171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6172c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6173171ae1a4SAndy Whitcroft		{
6174c45dcabdSAndy Whitcroft			my $function_name = $1;
6175c45dcabdSAndy Whitcroft			my $paren_space = $2;
6176171ae1a4SAndy Whitcroft
6177171ae1a4SAndy Whitcroft			my $s = $stat;
6178171ae1a4SAndy Whitcroft			if (defined $cond) {
6179171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6180171ae1a4SAndy Whitcroft			}
6181c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6182c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6183c45dcabdSAndy Whitcroft			{
6184000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6185000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6186de7d4f0eSAndy Whitcroft			}
6187de7d4f0eSAndy Whitcroft
6188171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6189000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6190000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6191171ae1a4SAndy Whitcroft			}
61929c9ba34eSAndy Whitcroft
61939c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
61949c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
61959c9ba34eSAndy Whitcroft		{
6196000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6197000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6198171ae1a4SAndy Whitcroft		}
6199171ae1a4SAndy Whitcroft
6200a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6201a0ad7596SJoe Perches		if (defined $stat &&
620225bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6203ca0d8929SJoe Perches		    $1 ne "void") {
6204ca0d8929SJoe Perches			my $args = trim($1);
6205ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6206ca0d8929SJoe Perches				my $arg = trim($1);
6207ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6208ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6209ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6210ca0d8929SJoe Perches				}
6211ca0d8929SJoe Perches			}
6212ca0d8929SJoe Perches		}
6213ca0d8929SJoe Perches
6214a0ad7596SJoe Perches# check for function definitions
62155b57980dSJoe Perches		if ($perl_version_ok &&
6216a0ad7596SJoe Perches		    defined $stat &&
6217a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6218a0ad7596SJoe Perches			$context_function = $1;
6219a0ad7596SJoe Perches
6220a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6221a0ad7596SJoe Perches			my $ok = 0;
6222a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6223a0ad7596SJoe Perches			my $herectx = $here . "\n";
6224a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6225a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6226a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6227a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6228a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6229a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6230a0ad7596SJoe Perches			}
6231a0ad7596SJoe Perches			if (!$ok) {
6232a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6233a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6234a0ad7596SJoe Perches			}
6235a0ad7596SJoe Perches		}
6236a0ad7596SJoe Perches
6237de7d4f0eSAndy Whitcroft# checks for new __setup's
6238de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6239de7d4f0eSAndy Whitcroft			my $name = $1;
6240de7d4f0eSAndy Whitcroft
6241de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6242000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
62438c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6244de7d4f0eSAndy Whitcroft			}
6245653d4876SAndy Whitcroft		}
62469c0ca6f9SAndy Whitcroft
6247e29a70f1SJoe Perches# check for pointless casting of alloc functions
6248e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6249000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6250000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
62519c0ca6f9SAndy Whitcroft		}
625213214adfSAndy Whitcroft
6253a640d25cSJoe Perches# alloc style
6254a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
62555b57980dSJoe Perches		if ($perl_version_ok &&
6256e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6257a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6258a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6259a640d25cSJoe Perches		}
6260a640d25cSJoe Perches
626160a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
62625b57980dSJoe Perches		if ($perl_version_ok &&
62631b4a2ed4SJoe Perches		    defined $stat &&
62641b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
626560a55369SJoe Perches			my $oldfunc = $3;
626660a55369SJoe Perches			my $a1 = $4;
626760a55369SJoe Perches			my $a2 = $10;
626860a55369SJoe Perches			my $newfunc = "kmalloc_array";
626960a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
627060a55369SJoe Perches			my $r1 = $a1;
627160a55369SJoe Perches			my $r2 = $a2;
627260a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
627360a55369SJoe Perches				$r1 = $a2;
627460a55369SJoe Perches				$r2 = $a1;
627560a55369SJoe Perches			}
6276e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6277e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
62781b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6279e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6280e3d95a2aSTobin C. Harding
6281e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
62821b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
62831b4a2ed4SJoe Perches				    $cnt == 1 &&
6284e367455aSJoe Perches				    $fix) {
6285194f66fcSJoe 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;
628660a55369SJoe Perches				}
628760a55369SJoe Perches			}
628860a55369SJoe Perches		}
628960a55369SJoe Perches
6290972fdea2SJoe Perches# check for krealloc arg reuse
62915b57980dSJoe Perches		if ($perl_version_ok &&
62924cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
62934cab63ceSJoe Perches		    $1 eq $3) {
6294972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6295972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6296972fdea2SJoe Perches		}
6297972fdea2SJoe Perches
62985ce59ae0SJoe Perches# check for alloc argument mismatch
62995ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
63005ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
63015ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
63025ce59ae0SJoe Perches		}
63035ce59ae0SJoe Perches
6304caf2a54fSJoe Perches# check for multiple semicolons
6305caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6306d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6307d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6308d5e616fcSJoe Perches			    $fix) {
6309194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6310d5e616fcSJoe Perches			}
6311d1e2ad07SJoe Perches		}
6312d1e2ad07SJoe Perches
6313cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6314cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6315cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
63160ab90191SJoe Perches			my $ull = "";
63170ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
63180ab90191SJoe Perches			if (CHK("BIT_MACRO",
63190ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
63200ab90191SJoe Perches			    $fix) {
63210ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
63220ab90191SJoe Perches			}
63230ab90191SJoe Perches		}
63240ab90191SJoe Perches
63252d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
63262d632745SJoe 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*$/) {
63272d632745SJoe Perches			my $config = $1;
63282d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
63292d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
63302d632745SJoe Perches			    $fix) {
63312d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
63322d632745SJoe Perches			}
63332d632745SJoe Perches		}
63342d632745SJoe Perches
6335e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6336c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6337c34c09a8SJoe Perches			my $has_break = 0;
6338c34c09a8SJoe Perches			my $has_statement = 0;
6339c34c09a8SJoe Perches			my $count = 0;
6340c34c09a8SJoe Perches			my $prevline = $linenr;
6341e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6342c34c09a8SJoe Perches				$prevline--;
6343c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6344c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6345c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6346c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6347c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6348c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6349c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6350c34c09a8SJoe Perches				$has_statement = 1;
6351c34c09a8SJoe Perches				$count++;
6352258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6353c34c09a8SJoe Perches			}
6354c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6355c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6356224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6357c34c09a8SJoe Perches			}
6358c34c09a8SJoe Perches		}
6359c34c09a8SJoe Perches
6360d1e2ad07SJoe Perches# check for switch/default statements without a break;
63615b57980dSJoe Perches		if ($perl_version_ok &&
6362d1e2ad07SJoe Perches		    defined $stat &&
6363d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6364d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6365e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6366e3d95a2aSTobin C. Harding
6367d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6368d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6369caf2a54fSJoe Perches		}
6370caf2a54fSJoe Perches
637113214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6372d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6373d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6374d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6375d5e616fcSJoe Perches			    $fix) {
6376194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6377d5e616fcSJoe Perches			}
637813214adfSAndy Whitcroft		}
6379773647a0SAndy Whitcroft
638062ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
638162ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
638262ec818fSJoe Perches			ERROR("DATE_TIME",
638362ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
638462ec818fSJoe Perches		}
638562ec818fSJoe Perches
63862c92488aSJoe Perches# check for use of yield()
63872c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
63882c92488aSJoe Perches			WARN("YIELD",
63892c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
63902c92488aSJoe Perches		}
63912c92488aSJoe Perches
6392179f8f40SJoe Perches# check for comparisons against true and false
6393179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6394179f8f40SJoe Perches			my $lead = $1;
6395179f8f40SJoe Perches			my $arg = $2;
6396179f8f40SJoe Perches			my $test = $3;
6397179f8f40SJoe Perches			my $otype = $4;
6398179f8f40SJoe Perches			my $trail = $5;
6399179f8f40SJoe Perches			my $op = "!";
6400179f8f40SJoe Perches
6401179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6402179f8f40SJoe Perches
6403179f8f40SJoe Perches			my $type = lc($otype);
6404179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6405179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6406179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6407179f8f40SJoe Perches					$op = "";
6408179f8f40SJoe Perches				}
6409179f8f40SJoe Perches
6410179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6411179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6412179f8f40SJoe Perches
6413179f8f40SJoe Perches## maybe suggesting a correct construct would better
6414179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6415179f8f40SJoe Perches
6416179f8f40SJoe Perches			}
6417179f8f40SJoe Perches		}
6418179f8f40SJoe Perches
64194882720bSThomas Gleixner# check for semaphores initialized locked
64204882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6421000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6422000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6423773647a0SAndy Whitcroft		}
64246712d858SJoe Perches
642567d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
642667d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6427000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
642867d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6429773647a0SAndy Whitcroft		}
64306712d858SJoe Perches
6431ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6432f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6433000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6434ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6435f3db6639SMichael Ellerman		}
64366712d858SJoe Perches
64373d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
64383d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
64393d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
64403d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
64413d709ab5SPaul E. McKenney		}
64423d709ab5SPaul E. McKenney
64439189c7e7SJoe Perches# check for deprecated apis
64449189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
64459189c7e7SJoe Perches			my $deprecated_api = $1;
64469189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
64479189c7e7SJoe Perches			WARN("DEPRECATED_API",
64489189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
64499189c7e7SJoe Perches		}
64509189c7e7SJoe Perches
64510f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6452d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
64536903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6454d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6455000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6456d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
64572b6db5cbSAndy Whitcroft		}
6458773647a0SAndy Whitcroft
6459773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6460773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6461773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6462c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6463c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6464171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6465171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6466171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6467773647a0SAndy Whitcroft		{
6468000d1cc1SJoe Perches			WARN("NR_CPUS",
6469000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6470773647a0SAndy Whitcroft		}
64719c9ba34eSAndy Whitcroft
647252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
647352ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
647452ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
647552ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
647652ea8506SJoe Perches		}
647752ea8506SJoe Perches
6478acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
64795b57980dSJoe Perches		if ($perl_version_ok &&
6480acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6481acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6482acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6483acd9362cSJoe Perches		}
6484acd9362cSJoe Perches
6485691d77b6SAndy Whitcroft# whine mightly about in_atomic
6486691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6487691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6488000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6489000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6490f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6491000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6492000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6493691d77b6SAndy Whitcroft			}
6494691d77b6SAndy Whitcroft		}
64951704f47bSPeter Zijlstra
64960f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
64970f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
64980f5225b0SPeter Zijlstra			ERROR("LOCKING",
64990f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
65000f5225b0SPeter Zijlstra		}
65010f5225b0SPeter Zijlstra
65021704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
65031704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
65041704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
65051704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
65061704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
65071704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6508000d1cc1SJoe Perches				ERROR("LOCKDEP",
6509000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
65101704f47bSPeter Zijlstra			}
65111704f47bSPeter Zijlstra		}
651288f8831cSDave Jones
6513b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6514b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6515000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6516000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
651788f8831cSDave Jones		}
65182435880fSJoe Perches
651900180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
652000180468SJoe Perches# and whether or not function naming is typical and if
652100180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
65225b57980dSJoe Perches		if ($perl_version_ok &&
652300180468SJoe Perches		    defined $stat &&
652400180468SJoe Perches		    $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) {
652500180468SJoe Perches			my $var = $1;
652600180468SJoe Perches			my $perms = $2;
652700180468SJoe Perches			my $show = $3;
652800180468SJoe Perches			my $store = $4;
652900180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
653000180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
653100180468SJoe Perches			    $store =~ /^${var}_store$/ &&
653200180468SJoe Perches			    $octal_perms eq "0644") {
653300180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
653400180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
653500180468SJoe Perches				    $fix) {
653600180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
653700180468SJoe Perches				}
653800180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
653900180468SJoe Perches				 $store =~ /^NULL$/ &&
654000180468SJoe Perches				 $octal_perms eq "0444") {
654100180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
654200180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
654300180468SJoe Perches				    $fix) {
654400180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
654500180468SJoe Perches				}
654600180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
654700180468SJoe Perches				 $store =~ /^${var}_store$/ &&
654800180468SJoe Perches				 $octal_perms eq "0200") {
654900180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
655000180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
655100180468SJoe Perches				    $fix) {
655200180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
655300180468SJoe Perches				}
655400180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
655500180468SJoe Perches				 $octal_perms eq "0444" ||
655600180468SJoe Perches				 $octal_perms eq "0200") {
655700180468SJoe Perches				my $newshow = "$show";
655800180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
655900180468SJoe Perches				my $newstore = $store;
656000180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
656100180468SJoe Perches				my $rename = "";
656200180468SJoe Perches				if ($show ne $newshow) {
656300180468SJoe Perches					$rename .= " '$show' to '$newshow'";
656400180468SJoe Perches				}
656500180468SJoe Perches				if ($store ne $newstore) {
656600180468SJoe Perches					$rename .= " '$store' to '$newstore'";
656700180468SJoe Perches				}
656800180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
656900180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
657000180468SJoe Perches			} else {
657100180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
657200180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
657300180468SJoe Perches			}
657400180468SJoe Perches		}
657500180468SJoe Perches
6576515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6577515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
657873121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
657973121534SJoe Perches#   specific definition of not visible in sysfs.
658073121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
658173121534SJoe Perches#   use the default permissions
65825b57980dSJoe Perches		if ($perl_version_ok &&
6583459cf0aeSJoe Perches		    defined $stat &&
6584515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
65852435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
65862435880fSJoe Perches				my $func = $entry->[0];
65872435880fSJoe Perches				my $arg_pos = $entry->[1];
65882435880fSJoe Perches
6589459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6590459cf0aeSJoe Perches				$lc = $lc + $linenr;
65912a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6592459cf0aeSJoe Perches
65932435880fSJoe Perches				my $skip_args = "";
65942435880fSJoe Perches				if ($arg_pos > 1) {
65952435880fSJoe Perches					$arg_pos--;
65962435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
65972435880fSJoe Perches				}
6598f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6599459cf0aeSJoe Perches				if ($stat =~ /$test/) {
66002435880fSJoe Perches					my $val = $1;
66012435880fSJoe Perches					$val = $6 if ($skip_args ne "");
660273121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
660373121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
660473121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
66052435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6606459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6607f90774e1SJoe Perches					}
6608f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6609c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6610459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
66112435880fSJoe Perches					}
6612459cf0aeSJoe Perches				}
6613459cf0aeSJoe Perches			}
6614459cf0aeSJoe Perches		}
6615459cf0aeSJoe Perches
6616459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6617bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
661800180468SJoe Perches			my $oval = $1;
661900180468SJoe Perches			my $octal = perms_to_octal($oval);
6620f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6621459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6622f90774e1SJoe Perches			    $fix) {
662300180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
66242435880fSJoe Perches			}
662513214adfSAndy Whitcroft		}
66265a6d20ceSBjorn Andersson
66275a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
66285a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
66295a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
66305a6d20ceSBjorn Andersson			my $valid_licenses = qr{
66315a6d20ceSBjorn Andersson						GPL|
66325a6d20ceSBjorn Andersson						GPL\ v2|
66335a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
66345a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
66355a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
66365a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
66375a6d20ceSBjorn Andersson						Proprietary
66385a6d20ceSBjorn Andersson					}x;
66395a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
66405a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
66415a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
66425a6d20ceSBjorn Andersson			}
66435a6d20ceSBjorn Andersson		}
66446a8d76cbSMatteo Croce
66456a8d76cbSMatteo Croce# check for sysctl duplicate constants
66466a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
66476a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
66486a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
66496a8d76cbSMatteo Croce		}
6650515a235eSJoe Perches	}
665113214adfSAndy Whitcroft
665213214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
665313214adfSAndy Whitcroft	# so just keep quiet.
665413214adfSAndy Whitcroft	if ($#rawlines == -1) {
665513214adfSAndy Whitcroft		exit(0);
66560a920b5bSAndy Whitcroft	}
66570a920b5bSAndy Whitcroft
66588905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
66598905a67cSAndy Whitcroft	# things that appear to be patches.
66608905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
66618905a67cSAndy Whitcroft		exit(0);
66628905a67cSAndy Whitcroft	}
66638905a67cSAndy Whitcroft
66648905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
66658905a67cSAndy Whitcroft	# just keep quiet.
66668905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
66678905a67cSAndy Whitcroft		exit(0);
66688905a67cSAndy Whitcroft	}
66698905a67cSAndy Whitcroft
6670a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6671000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6672000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
66730a920b5bSAndy Whitcroft	}
6674cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6675cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6676000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6677000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6678cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6679cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6680cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6681cd261496SGeert Uytterhoeven		}
66820a920b5bSAndy Whitcroft	}
66830a920b5bSAndy Whitcroft
6684f0a594c1SAndy Whitcroft	print report_dump();
668513214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
668613214adfSAndy Whitcroft		print "$filename " if ($summary_file);
66876c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
66886c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
66896c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
66906c72ffaaSAndy Whitcroft	}
66918905a67cSAndy Whitcroft
6692d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6693ef212196SJoe Perches		# If there were any defects found and not already fixing them
6694ef212196SJoe Perches		if (!$clean and !$fix) {
6695ef212196SJoe Perches			print << "EOM"
6696ef212196SJoe Perches
6697ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6698ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6699ef212196SJoe PerchesEOM
6700ef212196SJoe Perches		}
6701d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6702d2c0a235SAndy Whitcroft		# then suggest that.
6703d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6704b0781216SMike Frysinger			$rpt_cleaners = 0;
6705d8469f16SJoe Perches			print << "EOM"
6706d8469f16SJoe Perches
6707d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6708d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6709d8469f16SJoe PerchesEOM
6710d2c0a235SAndy Whitcroft		}
6711d2c0a235SAndy Whitcroft	}
6712d2c0a235SAndy Whitcroft
6713d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6714d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6715d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
67169624b8d6SJoe Perches		my $newfile = $filename;
67179624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
67183705ce5bSJoe Perches		my $linecount = 0;
67193705ce5bSJoe Perches		my $f;
67203705ce5bSJoe Perches
6721d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6722d752fcc8SJoe Perches
67233705ce5bSJoe Perches		open($f, '>', $newfile)
67243705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
67253705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
67263705ce5bSJoe Perches			$linecount++;
67273705ce5bSJoe Perches			if ($file) {
67283705ce5bSJoe Perches				if ($linecount > 3) {
67293705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
67303705ce5bSJoe Perches					print $f $fixed_line . "\n";
67313705ce5bSJoe Perches				}
67323705ce5bSJoe Perches			} else {
67333705ce5bSJoe Perches				print $f $fixed_line . "\n";
67343705ce5bSJoe Perches			}
67353705ce5bSJoe Perches		}
67363705ce5bSJoe Perches		close($f);
67373705ce5bSJoe Perches
67383705ce5bSJoe Perches		if (!$quiet) {
67393705ce5bSJoe Perches			print << "EOM";
6740d8469f16SJoe Perches
67413705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
67423705ce5bSJoe Perches
67433705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
67443705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
67453705ce5bSJoe Perches
67463705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
67473705ce5bSJoe PerchesNo warranties, expressed or implied...
67483705ce5bSJoe PerchesEOM
67493705ce5bSJoe Perches		}
67503705ce5bSJoe Perches	}
67513705ce5bSJoe Perches
6752d8469f16SJoe Perches	if ($quiet == 0) {
6753d8469f16SJoe Perches		print "\n";
6754d8469f16SJoe Perches		if ($clean == 1) {
6755d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6756d8469f16SJoe Perches		} else {
6757d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
67580a920b5bSAndy Whitcroft		}
67590a920b5bSAndy Whitcroft	}
67600a920b5bSAndy Whitcroft	return $clean;
67610a920b5bSAndy Whitcroft}
6762