xref: /linux-6.15/scripts/checkpatch.pl (revision a134f8de)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
9cb77f0d6SKamil Rytarowskiuse warnings;
10c707a81dSJoe Perchesuse POSIX;
1136061e38SJoe Perchesuse File::Basename;
1236061e38SJoe Perchesuse Cwd 'abs_path';
1357230297SJoe Perchesuse Term::ANSIColor qw(:constants);
140a920b5bSAndy Whitcroft
150a920b5bSAndy Whitcroftmy $P = $0;
1636061e38SJoe Perchesmy $D = dirname(abs_path($P));
170a920b5bSAndy Whitcroft
18000d1cc1SJoe Perchesmy $V = '0.32';
190a920b5bSAndy Whitcroft
200a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
210a920b5bSAndy Whitcroft
220a920b5bSAndy Whitcroftmy $quiet = 0;
230a920b5bSAndy Whitcroftmy $tree = 1;
240a920b5bSAndy Whitcroftmy $chk_signoff = 1;
250a920b5bSAndy Whitcroftmy $chk_patch = 1;
26773647a0SAndy Whitcroftmy $tst_only;
276c72ffaaSAndy Whitcroftmy $emacs = 0;
288905a67cSAndy Whitcroftmy $terse = 0;
2934d8815fSJoe Perchesmy $showfile = 0;
306c72ffaaSAndy Whitcroftmy $file = 0;
314a593c34SDu, Changbinmy $git = 0;
320dea9f1eSJoe Perchesmy %git_commits = ();
336c72ffaaSAndy Whitcroftmy $check = 0;
342ac73b4fSJoe Perchesmy $check_orig = 0;
358905a67cSAndy Whitcroftmy $summary = 1;
368905a67cSAndy Whitcroftmy $mailback = 0;
3713214adfSAndy Whitcroftmy $summary_file = 0;
38000d1cc1SJoe Perchesmy $show_types = 0;
393beb42ecSJoe Perchesmy $list_types = 0;
403705ce5bSJoe Perchesmy $fix = 0;
419624b8d6SJoe Perchesmy $fix_inplace = 0;
426c72ffaaSAndy Whitcroftmy $root;
43c2fdda0dSAndy Whitcroftmy %debug;
443445686aSJoe Perchesmy %camelcase = ();
4591bfe484SJoe Perchesmy %use_type = ();
4691bfe484SJoe Perchesmy @use = ();
4791bfe484SJoe Perchesmy %ignore_type = ();
48000d1cc1SJoe Perchesmy @ignore = ();
4977f5b10aSHannes Edermy $help = 0;
50000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
516cd7f386SJoe Perchesmy $max_line_length = 80;
52d62a201fSDave Hansenmy $ignore_perl_version = 0;
53d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5456193274SVadim Bendeburymy $min_conf_desc_length = 4;
5566b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
56ebfd7d62SJoe Perchesmy $codespell = 0;
57f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
58bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
5975ad8c57SJerome Forissiermy $typedefsfile = "";
60737c0767SJohn Brooksmy $color = "auto";
61dadf680dSJoe Perchesmy $allow_c99_comments = 1;
6277f5b10aSHannes Eder
6377f5b10aSHannes Edersub help {
6477f5b10aSHannes Eder	my ($exitcode) = @_;
6577f5b10aSHannes Eder
6677f5b10aSHannes Eder	print << "EOM";
6777f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
6877f5b10aSHannes EderVersion: $V
6977f5b10aSHannes Eder
7077f5b10aSHannes EderOptions:
7177f5b10aSHannes Eder  -q, --quiet                quiet
7277f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7377f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
7477f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
7577f5b10aSHannes Eder  --emacs                    emacs compile window format
7677f5b10aSHannes Eder  --terse                    one line per report
7734d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
784a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
794a593c34SDu, Changbin                             single git commit with:
804a593c34SDu, Changbin                               <rev>
814a593c34SDu, Changbin                               <rev>^
824a593c34SDu, Changbin                               <rev>~n
834a593c34SDu, Changbin                             multiple git commits with:
844a593c34SDu, Changbin                               <rev1>..<rev2>
854a593c34SDu, Changbin                               <rev1>...<rev2>
864a593c34SDu, Changbin                               <rev>-<count>
874a593c34SDu, Changbin                             git merges are ignored
8877f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
8977f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
903beb42ecSJoe Perches  --list-types               list the possible message types
9191bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
92000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
933beb42ecSJoe Perches  --show-types               show the specific message type in the output
946cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
9556193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
9677f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
9777f5b10aSHannes Eder  --no-summary               suppress the per-file summary
9877f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
9977f5b10aSHannes Eder  --summary-file             include the filename in summary
10077f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
10177f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
10277f5b10aSHannes Eder                             is all off)
10377f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
10477f5b10aSHannes Eder                             literally
1053705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1063705ce5bSJoe Perches                             If correctable single-line errors exist, create
1073705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1083705ce5bSJoe Perches                             with potential errors corrected to the preferred
1093705ce5bSJoe Perches                             checkpatch style
1109624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1119624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1129624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
113d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
114d62a201fSDave Hansen                             runtime errors.
115ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
116f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
117ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
11875ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
119737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
120737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
12177f5b10aSHannes Eder  -h, --help, --version      display this help and exit
12277f5b10aSHannes Eder
12377f5b10aSHannes EderWhen FILE is - read standard input.
12477f5b10aSHannes EderEOM
12577f5b10aSHannes Eder
12677f5b10aSHannes Eder	exit($exitcode);
12777f5b10aSHannes Eder}
12877f5b10aSHannes Eder
1293beb42ecSJoe Perchessub uniq {
1303beb42ecSJoe Perches	my %seen;
1313beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1323beb42ecSJoe Perches}
1333beb42ecSJoe Perches
1343beb42ecSJoe Perchessub list_types {
1353beb42ecSJoe Perches	my ($exitcode) = @_;
1363beb42ecSJoe Perches
1373beb42ecSJoe Perches	my $count = 0;
1383beb42ecSJoe Perches
1393beb42ecSJoe Perches	local $/ = undef;
1403beb42ecSJoe Perches
1413beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1423beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1433beb42ecSJoe Perches
1443beb42ecSJoe Perches	my $text = <$script>;
1453beb42ecSJoe Perches	close($script);
1463beb42ecSJoe Perches
1473beb42ecSJoe Perches	my @types = ();
1480547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1490547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1503beb42ecSJoe Perches		push (@types, $_);
1513beb42ecSJoe Perches	}
1523beb42ecSJoe Perches	@types = sort(uniq(@types));
1533beb42ecSJoe Perches	print("#\tMessage type\n\n");
1543beb42ecSJoe Perches	foreach my $type (@types) {
1553beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1563beb42ecSJoe Perches	}
1573beb42ecSJoe Perches
1583beb42ecSJoe Perches	exit($exitcode);
1593beb42ecSJoe Perches}
1603beb42ecSJoe Perches
161000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
162000d1cc1SJoe Perchesif (-f $conf) {
163000d1cc1SJoe Perches	my @conf_args;
164000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
165000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
166000d1cc1SJoe Perches
167000d1cc1SJoe Perches	while (<$conffile>) {
168000d1cc1SJoe Perches		my $line = $_;
169000d1cc1SJoe Perches
170000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
171000d1cc1SJoe Perches		$line =~ s/^\s*//g;
172000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
173000d1cc1SJoe Perches
174000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
175000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
176000d1cc1SJoe Perches
177000d1cc1SJoe Perches		my @words = split(" ", $line);
178000d1cc1SJoe Perches		foreach my $word (@words) {
179000d1cc1SJoe Perches			last if ($word =~ m/^#/);
180000d1cc1SJoe Perches			push (@conf_args, $word);
181000d1cc1SJoe Perches		}
182000d1cc1SJoe Perches	}
183000d1cc1SJoe Perches	close($conffile);
184000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
185000d1cc1SJoe Perches}
186000d1cc1SJoe Perches
187737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
188737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
189737c0767SJohn Brooksforeach (@ARGV) {
190737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
191737c0767SJohn Brooks		$_ = "--color=$color";
192737c0767SJohn Brooks	}
193737c0767SJohn Brooks}
194737c0767SJohn Brooks
1950a920b5bSAndy WhitcroftGetOptions(
1966c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1970a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1980a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1990a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2006c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2018905a67cSAndy Whitcroft	'terse!'	=> \$terse,
20234d8815fSJoe Perches	'showfile!'	=> \$showfile,
20377f5b10aSHannes Eder	'f|file!'	=> \$file,
2044a593c34SDu, Changbin	'g|git!'	=> \$git,
2056c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2066c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
207000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
20891bfe484SJoe Perches	'types=s'	=> \@use,
209000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2103beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2116cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
21256193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
2136c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2148905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2158905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
21613214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2173705ce5bSJoe Perches	'fix!'		=> \$fix,
2189624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
219d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
220c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
221773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
222ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
223ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
22475ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
225737c0767SJohn Brooks	'color=s'	=> \$color,
226737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
227737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
22877f5b10aSHannes Eder	'h|help'	=> \$help,
22977f5b10aSHannes Eder	'version'	=> \$help
23077f5b10aSHannes Eder) or help(1);
23177f5b10aSHannes Eder
23277f5b10aSHannes Ederhelp(0) if ($help);
2330a920b5bSAndy Whitcroft
2343beb42ecSJoe Percheslist_types(0) if ($list_types);
2353beb42ecSJoe Perches
2369624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2372ac73b4fSJoe Perches$check_orig = $check;
2389624b8d6SJoe Perches
2390a920b5bSAndy Whitcroftmy $exit = 0;
2400a920b5bSAndy Whitcroft
241d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
242d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
243d62a201fSDave Hansen	if (!$ignore_perl_version) {
244d62a201fSDave Hansen		exit(1);
245d62a201fSDave Hansen	}
246d62a201fSDave Hansen}
247d62a201fSDave Hansen
24845107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2490a920b5bSAndy Whitcroftif ($#ARGV < 0) {
25045107ff6SAllen Hubbe	push(@ARGV, '-');
2510a920b5bSAndy Whitcroft}
2520a920b5bSAndy Whitcroft
253737c0767SJohn Brooksif ($color =~ /^[01]$/) {
254737c0767SJohn Brooks	$color = !$color;
255737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
256737c0767SJohn Brooks	$color = 1;
257737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
258737c0767SJohn Brooks	$color = 0;
259737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
260737c0767SJohn Brooks	$color = (-t STDOUT);
261737c0767SJohn Brooks} else {
262737c0767SJohn Brooks	die "Invalid color mode: $color\n";
263737c0767SJohn Brooks}
264737c0767SJohn Brooks
26591bfe484SJoe Perchessub hash_save_array_words {
26691bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
26791bfe484SJoe Perches
26891bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
26991bfe484SJoe Perches	foreach my $word (@array) {
270000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
271000d1cc1SJoe Perches		$word =~ s/^\s*//g;
272000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
273000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
274000d1cc1SJoe Perches
275000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
276000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
277000d1cc1SJoe Perches
27891bfe484SJoe Perches		$hashRef->{$word}++;
279000d1cc1SJoe Perches	}
28091bfe484SJoe Perches}
28191bfe484SJoe Perches
28291bfe484SJoe Perchessub hash_show_words {
28391bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
28491bfe484SJoe Perches
2853c816e49SJoe Perches	if (keys %$hashRef) {
286d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
28758cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
28891bfe484SJoe Perches			print " $word";
28991bfe484SJoe Perches		}
290d8469f16SJoe Perches		print "\n";
29191bfe484SJoe Perches	}
29291bfe484SJoe Perches}
29391bfe484SJoe Perches
29491bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
29591bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
296000d1cc1SJoe Perches
297c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
298c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2997429c690SAndy Whitcroftmy $dbg_type = 0;
300a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
301c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
30221caa13cSAndy Whitcroft	## no critic
30321caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
30421caa13cSAndy Whitcroft	die "$@" if ($@);
305c2fdda0dSAndy Whitcroft}
306c2fdda0dSAndy Whitcroft
307d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
308d2c0a235SAndy Whitcroft
3098905a67cSAndy Whitcroftif ($terse) {
3108905a67cSAndy Whitcroft	$emacs = 1;
3118905a67cSAndy Whitcroft	$quiet++;
3128905a67cSAndy Whitcroft}
3138905a67cSAndy Whitcroft
3146c72ffaaSAndy Whitcroftif ($tree) {
3156c72ffaaSAndy Whitcroft	if (defined $root) {
3166c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3176c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3186c72ffaaSAndy Whitcroft		}
3196c72ffaaSAndy Whitcroft	} else {
3206c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3216c72ffaaSAndy Whitcroft			$root = '.';
3226c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3236c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3246c72ffaaSAndy Whitcroft			$root = $1;
3256c72ffaaSAndy Whitcroft		}
3266c72ffaaSAndy Whitcroft	}
3276c72ffaaSAndy Whitcroft
3286c72ffaaSAndy Whitcroft	if (!defined $root) {
3290a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3300a920b5bSAndy Whitcroft		exit(2);
3310a920b5bSAndy Whitcroft	}
3326c72ffaaSAndy Whitcroft}
3336c72ffaaSAndy Whitcroft
3346c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3356c72ffaaSAndy Whitcroft
3362ceb532bSAndy Whitcroftour $Ident	= qr{
3372ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3382ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3392ceb532bSAndy Whitcroft		}x;
3406c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3416c72ffaaSAndy Whitcroftour $Sparse	= qr{
3426c72ffaaSAndy Whitcroft			__user|
3436c72ffaaSAndy Whitcroft			__kernel|
3446c72ffaaSAndy Whitcroft			__force|
3456c72ffaaSAndy Whitcroft			__iomem|
3466c72ffaaSAndy Whitcroft			__must_check|
3476c72ffaaSAndy Whitcroft			__init_refok|
348417495edSAndy Whitcroft			__kprobes|
349165e72a6SSven Eckelmann			__ref|
350ad315455SBoqun Feng			__rcu|
351ad315455SBoqun Feng			__private
3526c72ffaaSAndy Whitcroft		}x;
353e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
354e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
355e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
356e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
357e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3588716de38SJoe Perches
35952131292SWolfram Sang# Notes to $Attribute:
36052131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3616c72ffaaSAndy Whitcroftour $Attribute	= qr{
3626c72ffaaSAndy Whitcroft			const|
36303f1df7dSJoe Perches			__percpu|
36403f1df7dSJoe Perches			__nocast|
36503f1df7dSJoe Perches			__safe|
36646d832f5SMichael S. Tsirkin			__bitwise|
36703f1df7dSJoe Perches			__packed__|
36803f1df7dSJoe Perches			__packed2__|
36903f1df7dSJoe Perches			__naked|
37003f1df7dSJoe Perches			__maybe_unused|
37103f1df7dSJoe Perches			__always_unused|
37203f1df7dSJoe Perches			__noreturn|
37303f1df7dSJoe Perches			__used|
37403f1df7dSJoe Perches			__cold|
375e23ef1f3SJoe Perches			__pure|
37603f1df7dSJoe Perches			__noclone|
37703f1df7dSJoe Perches			__deprecated|
3786c72ffaaSAndy Whitcroft			__read_mostly|
3796c72ffaaSAndy Whitcroft			__kprobes|
3808716de38SJoe Perches			$InitAttribute|
38124e1d81aSAndy Whitcroft			____cacheline_aligned|
38224e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3835fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3845fe3af11SAndy Whitcroft			__weak
3856c72ffaaSAndy Whitcroft		  }x;
386c45dcabdSAndy Whitcroftour $Modifier;
38791cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3886c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3896c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3906c72ffaaSAndy Whitcroft
39195e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
39295e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
39395e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
39495e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3952435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
396c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
397326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
398326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
399326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
40074349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4012435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
402326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
403447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
40423f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4056c72ffaaSAndy Whitcroftour $Operators	= qr{
4066c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4076c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
40823f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4096c72ffaaSAndy Whitcroft		  }x;
4106c72ffaaSAndy Whitcroft
41191cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
41291cb5195SJoe Perches
413ab7e23f3SJoe Perchesour $BasicType;
4148905a67cSAndy Whitcroftour $NonptrType;
4151813087dSJoe Perchesour $NonptrTypeMisordered;
4168716de38SJoe Perchesour $NonptrTypeWithAttr;
4178905a67cSAndy Whitcroftour $Type;
4181813087dSJoe Perchesour $TypeMisordered;
4198905a67cSAndy Whitcroftour $Declare;
4201813087dSJoe Perchesour $DeclareMisordered;
4218905a67cSAndy Whitcroft
42215662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
42315662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
424171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
425171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
426171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
427171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
428171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
429171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
430171ae1a4SAndy Whitcroft}x;
431171ae1a4SAndy Whitcroft
43215662b3eSJoe Perchesour $UTF8	= qr{
43315662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
43415662b3eSJoe Perches	| $NON_ASCII_UTF8
43515662b3eSJoe Perches}x;
43615662b3eSJoe Perches
437e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
438021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
439021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
440021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
441021158b4SJoe Perches)};
442e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
443fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4448ed22cadSAndy Whitcroft	atomic_t
4458ed22cadSAndy Whitcroft)};
446e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
447e6176fa4SJoe Perches	$typeC99Typedefs\b|
448e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
449e6176fa4SJoe Perches	$typeKernelTypedefs\b
450e6176fa4SJoe Perches)};
4518ed22cadSAndy Whitcroft
4526d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4536d32f7a3SJoe Perches
454691e669bSJoe Perchesour $logFunctions = qr{(?x:
455758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4567d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
45787bd499aSJoe Perches	TP_printk|
4586e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
459b0531722SJoe Perches	panic|
46006668727SJoe Perches	MODULE_[A-Z_]+|
46106668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
462691e669bSJoe Perches)};
463691e669bSJoe Perches
46420112475SJoe Perchesour $signature_tags = qr{(?xi:
46520112475SJoe Perches	Signed-off-by:|
46620112475SJoe Perches	Acked-by:|
46720112475SJoe Perches	Tested-by:|
46820112475SJoe Perches	Reviewed-by:|
46920112475SJoe Perches	Reported-by:|
4708543ae12SMugunthan V N	Suggested-by:|
47120112475SJoe Perches	To:|
47220112475SJoe Perches	Cc:
47320112475SJoe Perches)};
47420112475SJoe Perches
4751813087dSJoe Perchesour @typeListMisordered = (
4761813087dSJoe Perches	qr{char\s+(?:un)?signed},
4771813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4781813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4791813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4801813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4811813087dSJoe Perches	qr{short\s+(?:un)?signed},
4821813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4831813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4841813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4851813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4861813087dSJoe Perches	qr{int\s+(?:un)?signed},
4871813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4881813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4891813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4901813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4911813087dSJoe Perches	qr{long\s+(?:un)?signed},
4921813087dSJoe Perches);
4931813087dSJoe Perches
4948905a67cSAndy Whitcroftour @typeList = (
4958905a67cSAndy Whitcroft	qr{void},
4960c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4970c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4980c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4990c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5000c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5010c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5020c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5030c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5040c773d9dSJoe Perches	qr{(?:un)?signed},
5058905a67cSAndy Whitcroft	qr{float},
5068905a67cSAndy Whitcroft	qr{double},
5078905a67cSAndy Whitcroft	qr{bool},
5088905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5098905a67cSAndy Whitcroft	qr{union\s+$Ident},
5108905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5118905a67cSAndy Whitcroft	qr{${Ident}_t},
5128905a67cSAndy Whitcroft	qr{${Ident}_handler},
5138905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5141813087dSJoe Perches	@typeListMisordered,
5158905a67cSAndy Whitcroft);
516938224b5SJoe Perches
517938224b5SJoe Perchesour $C90_int_types = qr{(?x:
518938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
519938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
520938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
521938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
522938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
523938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
524938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
525938224b5SJoe Perches
526938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
527938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
528938224b5SJoe Perches	long\s+(?:un)?signed|
529938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
530938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
531938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
532938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
533938224b5SJoe Perches
534938224b5SJoe Perches	int\s+(?:un)?signed|
535938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
536938224b5SJoe Perches)};
537938224b5SJoe Perches
538485ff23eSAlex Dowadour @typeListFile = ();
5398716de38SJoe Perchesour @typeListWithAttr = (
5408716de38SJoe Perches	@typeList,
5418716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5428716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5438716de38SJoe Perches);
5448716de38SJoe Perches
545c45dcabdSAndy Whitcroftour @modifierList = (
546c45dcabdSAndy Whitcroft	qr{fastcall},
547c45dcabdSAndy Whitcroft);
548485ff23eSAlex Dowadour @modifierListFile = ();
5498905a67cSAndy Whitcroft
5502435880fSJoe Perchesour @mode_permission_funcs = (
5512435880fSJoe Perches	["module_param", 3],
5522435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5532435880fSJoe Perches	["module_param_array_named", 5],
5542435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5552435880fSJoe Perches	["proc_create(?:_data|)", 2],
556459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
557459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
558459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
559459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
560459cf0aeSJoe Perches	["__ATTR", 2],
5612435880fSJoe Perches);
5622435880fSJoe Perches
563515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
564515a235eSJoe Perchesour $mode_perms_search = "";
565515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
566515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
567515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
568515a235eSJoe Perches}
56900180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
570515a235eSJoe Perches
571b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
572b392c64fSJoe Perches	S_IWUGO		|
573b392c64fSJoe Perches	S_IWOTH		|
574b392c64fSJoe Perches	S_IRWXUGO	|
575b392c64fSJoe Perches	S_IALLUGO	|
576b392c64fSJoe Perches	0[0-7][0-7][2367]
577b392c64fSJoe Perches}x;
578b392c64fSJoe Perches
579f90774e1SJoe Perchesour %mode_permission_string_types = (
580f90774e1SJoe Perches	"S_IRWXU" => 0700,
581f90774e1SJoe Perches	"S_IRUSR" => 0400,
582f90774e1SJoe Perches	"S_IWUSR" => 0200,
583f90774e1SJoe Perches	"S_IXUSR" => 0100,
584f90774e1SJoe Perches	"S_IRWXG" => 0070,
585f90774e1SJoe Perches	"S_IRGRP" => 0040,
586f90774e1SJoe Perches	"S_IWGRP" => 0020,
587f90774e1SJoe Perches	"S_IXGRP" => 0010,
588f90774e1SJoe Perches	"S_IRWXO" => 0007,
589f90774e1SJoe Perches	"S_IROTH" => 0004,
590f90774e1SJoe Perches	"S_IWOTH" => 0002,
591f90774e1SJoe Perches	"S_IXOTH" => 0001,
592f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
593f90774e1SJoe Perches	"S_IRUGO" => 0444,
594f90774e1SJoe Perches	"S_IWUGO" => 0222,
595f90774e1SJoe Perches	"S_IXUGO" => 0111,
596f90774e1SJoe Perches);
597f90774e1SJoe Perches
598f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
599f90774e1SJoe Perchesour $mode_perms_string_search = "";
600f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
601f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
602f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
603f90774e1SJoe Perches}
60400180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
60500180468SJoe Perchesour $multi_mode_perms_string_search = qr{
60600180468SJoe Perches	${single_mode_perms_string_search}
60700180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
60800180468SJoe Perches}x;
60900180468SJoe Perches
61000180468SJoe Perchessub perms_to_octal {
61100180468SJoe Perches	my ($string) = @_;
61200180468SJoe Perches
61300180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
61400180468SJoe Perches
61500180468SJoe Perches	my $val = "";
61600180468SJoe Perches	my $oval = "";
61700180468SJoe Perches	my $to = 0;
61800180468SJoe Perches	my $curpos = 0;
61900180468SJoe Perches	my $lastpos = 0;
62000180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
62100180468SJoe Perches		$curpos = pos($string);
62200180468SJoe Perches		my $match = $2;
62300180468SJoe Perches		my $omatch = $1;
62400180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
62500180468SJoe Perches		$lastpos = $curpos;
62600180468SJoe Perches		$to |= $mode_permission_string_types{$match};
62700180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
62800180468SJoe Perches		$val .= $match;
62900180468SJoe Perches		$oval .= $omatch;
63000180468SJoe Perches	}
63100180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
63200180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
63300180468SJoe Perches	return sprintf("%04o", $to);
63400180468SJoe Perches}
635f90774e1SJoe Perches
6367840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6377840a94cSWolfram Sang	irq|
638cdcee686SSergey Ryazanov	memory|
639cdcee686SSergey Ryazanov	time|
640cdcee686SSergey Ryazanov	reboot
6417840a94cSWolfram Sang)};
6427840a94cSWolfram Sang# memory.h: ARM has a custom one
6437840a94cSWolfram Sang
64466b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
64566b47b4aSKees Cookmy $misspellings;
64666b47b4aSKees Cookmy %spelling_fix;
64736061e38SJoe Perches
64836061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
64966b47b4aSKees Cook	while (<$spelling>) {
65066b47b4aSKees Cook		my $line = $_;
65166b47b4aSKees Cook
65266b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
65366b47b4aSKees Cook		$line =~ s/^\s*//g;
65466b47b4aSKees Cook
65566b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
65666b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
65766b47b4aSKees Cook
65866b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
65966b47b4aSKees Cook
66066b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
66166b47b4aSKees Cook	}
66266b47b4aSKees Cook	close($spelling);
66336061e38SJoe Perches} else {
66436061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
66536061e38SJoe Perches}
66666b47b4aSKees Cook
667ebfd7d62SJoe Perchesif ($codespell) {
668ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
669ebfd7d62SJoe Perches		while (<$spelling>) {
670ebfd7d62SJoe Perches			my $line = $_;
671ebfd7d62SJoe Perches
672ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
673ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
674ebfd7d62SJoe Perches
675ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
676ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
677ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
678ebfd7d62SJoe Perches
679ebfd7d62SJoe Perches			$line =~ s/,.*$//;
680ebfd7d62SJoe Perches
681ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
682ebfd7d62SJoe Perches
683ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
684ebfd7d62SJoe Perches		}
685ebfd7d62SJoe Perches		close($spelling);
686ebfd7d62SJoe Perches	} else {
687ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
688ebfd7d62SJoe Perches	}
689ebfd7d62SJoe Perches}
690ebfd7d62SJoe Perches
691ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
692ebfd7d62SJoe Perches
69375ad8c57SJerome Forissiersub read_words {
69475ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
69575ad8c57SJerome Forissier
69675ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
69775ad8c57SJerome Forissier		while (<$words>) {
698bf1fa1daSJoe Perches			my $line = $_;
699bf1fa1daSJoe Perches
700bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
701bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
702bf1fa1daSJoe Perches
703bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
704bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
705bf1fa1daSJoe Perches			if ($line =~ /\s/) {
70675ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
707bf1fa1daSJoe Perches				next;
708bf1fa1daSJoe Perches			}
709bf1fa1daSJoe Perches
71075ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
71175ad8c57SJerome Forissier			$$wordsRef .= $line;
712bf1fa1daSJoe Perches		}
71375ad8c57SJerome Forissier		close($file);
71475ad8c57SJerome Forissier		return 1;
715bf1fa1daSJoe Perches	}
716bf1fa1daSJoe Perches
71775ad8c57SJerome Forissier	return 0;
71875ad8c57SJerome Forissier}
71975ad8c57SJerome Forissier
72075ad8c57SJerome Forissiermy $const_structs = "";
72175ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
72275ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
72375ad8c57SJerome Forissier
72475ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
72575ad8c57SJerome Forissierif (length($typedefsfile)) {
72675ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
72775ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
72875ad8c57SJerome Forissier}
72975ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
73075ad8c57SJerome Forissier
7318905a67cSAndy Whitcroftsub build_types {
732485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
733485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7341813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7358716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
736c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
737ab7e23f3SJoe Perches	$BasicType	= qr{
738ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
739ab7e23f3SJoe Perches				(?:${all}\b)
740ab7e23f3SJoe Perches		}x;
7418905a67cSAndy Whitcroft	$NonptrType	= qr{
742d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
743cf655043SAndy Whitcroft			(?:
7446b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7458ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
746c45dcabdSAndy Whitcroft				(?:${all}\b)
747cf655043SAndy Whitcroft			)
748c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7498905a67cSAndy Whitcroft		  }x;
7501813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7511813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7521813087dSJoe Perches			(?:
7531813087dSJoe Perches				(?:${Misordered}\b)
7541813087dSJoe Perches			)
7551813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7561813087dSJoe Perches		  }x;
7578716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7588716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7598716de38SJoe Perches			(?:
7608716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
7618716de38SJoe Perches				(?:$typeTypedefs\b)|
7628716de38SJoe Perches				(?:${allWithAttr}\b)
7638716de38SJoe Perches			)
7648716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
7658716de38SJoe Perches		  }x;
7668905a67cSAndy Whitcroft	$Type	= qr{
767c45dcabdSAndy Whitcroft			$NonptrType
7681574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
769c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
7708905a67cSAndy Whitcroft		  }x;
7711813087dSJoe Perches	$TypeMisordered	= qr{
7721813087dSJoe Perches			$NonptrTypeMisordered
7731813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
7741813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
7751813087dSJoe Perches		  }x;
77691cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
7771813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
7788905a67cSAndy Whitcroft}
7798905a67cSAndy Whitcroftbuild_types();
7806c72ffaaSAndy Whitcroft
7817d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
782d1fe9c09SJoe Perches
783d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
784d1fe9c09SJoe Perches# requires at least perl version v5.10.0
785d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
786d1fe9c09SJoe Perches
787d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
7882435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
789c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
7907d2367afSJoe Perches
791f8422308SJoe Perchesour $declaration_macros = qr{(?x:
7923e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
793fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
794f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
795f8422308SJoe Perches)};
796f8422308SJoe Perches
7977d2367afSJoe Perchessub deparenthesize {
7987d2367afSJoe Perches	my ($string) = @_;
7997d2367afSJoe Perches	return "" if (!defined($string));
8005b9553abSJoe Perches
8015b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8025b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8035b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8045b9553abSJoe Perches	}
8055b9553abSJoe Perches
8067d2367afSJoe Perches	$string =~ s@\s+@ @g;
8075b9553abSJoe Perches
8087d2367afSJoe Perches	return $string;
8097d2367afSJoe Perches}
8107d2367afSJoe Perches
8113445686aSJoe Perchessub seed_camelcase_file {
8123445686aSJoe Perches	my ($file) = @_;
8133445686aSJoe Perches
8143445686aSJoe Perches	return if (!(-f $file));
8153445686aSJoe Perches
8163445686aSJoe Perches	local $/;
8173445686aSJoe Perches
8183445686aSJoe Perches	open(my $include_file, '<', "$file")
8193445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8203445686aSJoe Perches	my $text = <$include_file>;
8213445686aSJoe Perches	close($include_file);
8223445686aSJoe Perches
8233445686aSJoe Perches	my @lines = split('\n', $text);
8243445686aSJoe Perches
8253445686aSJoe Perches	foreach my $line (@lines) {
8263445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8273445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8283445686aSJoe Perches			$camelcase{$1} = 1;
82911ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
83011ea516aSJoe Perches			$camelcase{$1} = 1;
83111ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8323445686aSJoe Perches			$camelcase{$1} = 1;
8333445686aSJoe Perches		}
8343445686aSJoe Perches	}
8353445686aSJoe Perches}
8363445686aSJoe Perches
83785b0ee18SJoe Perchessub is_maintained_obsolete {
83885b0ee18SJoe Perches	my ($filename) = @_;
83985b0ee18SJoe Perches
840f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
84185b0ee18SJoe Perches
8420616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
84385b0ee18SJoe Perches
84485b0ee18SJoe Perches	return $status =~ /obsolete/i;
84585b0ee18SJoe Perches}
84685b0ee18SJoe Perches
8473445686aSJoe Perchesmy $camelcase_seeded = 0;
8483445686aSJoe Perchessub seed_camelcase_includes {
8493445686aSJoe Perches	return if ($camelcase_seeded);
8503445686aSJoe Perches
8513445686aSJoe Perches	my $files;
852c707a81dSJoe Perches	my $camelcase_cache = "";
853c707a81dSJoe Perches	my @include_files = ();
854c707a81dSJoe Perches
855c707a81dSJoe Perches	$camelcase_seeded = 1;
856351b2a1fSJoe Perches
8573645e328SRichard Genoud	if (-e ".git") {
858351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
859351b2a1fSJoe Perches		chomp $git_last_include_commit;
860c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
861c707a81dSJoe Perches	} else {
862c707a81dSJoe Perches		my $last_mod_date = 0;
863c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
864c707a81dSJoe Perches		@include_files = split('\n', $files);
865c707a81dSJoe Perches		foreach my $file (@include_files) {
866c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
867c707a81dSJoe Perches						   localtime((stat $file)[9]));
868c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
869c707a81dSJoe Perches		}
870c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
871c707a81dSJoe Perches	}
872c707a81dSJoe Perches
873c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
874c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
875c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
876351b2a1fSJoe Perches		while (<$camelcase_file>) {
877351b2a1fSJoe Perches			chomp;
878351b2a1fSJoe Perches			$camelcase{$_} = 1;
879351b2a1fSJoe Perches		}
880351b2a1fSJoe Perches		close($camelcase_file);
881351b2a1fSJoe Perches
882351b2a1fSJoe Perches		return;
883351b2a1fSJoe Perches	}
884c707a81dSJoe Perches
8853645e328SRichard Genoud	if (-e ".git") {
886c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
887c707a81dSJoe Perches		@include_files = split('\n', $files);
8883445686aSJoe Perches	}
889c707a81dSJoe Perches
8903445686aSJoe Perches	foreach my $file (@include_files) {
8913445686aSJoe Perches		seed_camelcase_file($file);
8923445686aSJoe Perches	}
893351b2a1fSJoe Perches
894c707a81dSJoe Perches	if ($camelcase_cache ne "") {
895351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
896c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
897c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
898351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
899351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
900351b2a1fSJoe Perches		}
901351b2a1fSJoe Perches		close($camelcase_file);
902351b2a1fSJoe Perches	}
9033445686aSJoe Perches}
9043445686aSJoe Perches
905d311cd44SJoe Perchessub git_commit_info {
906d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
907d311cd44SJoe Perches
908d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
909d311cd44SJoe Perches
910d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
911d311cd44SJoe Perches	$output =~ s/^\s*//gm;
912d311cd44SJoe Perches	my @lines = split("\n", $output);
913d311cd44SJoe Perches
9140d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9150d7835fcSJoe Perches
916d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
917d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
918d311cd44SJoe Perches# all matching commit ids, but it's very slow...
919d311cd44SJoe Perches#
920d311cd44SJoe Perches#		echo "checking commits $1..."
921d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
922d311cd44SJoe Perches#		while read line ; do
923d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
924d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
925d311cd44SJoe Perches#		done
926d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
927948b133aSHeinrich Schuchardt		$id = undef;
928d311cd44SJoe Perches	} else {
929d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
930d311cd44SJoe Perches		$desc = substr($lines[0], 41);
931d311cd44SJoe Perches	}
932d311cd44SJoe Perches
933d311cd44SJoe Perches	return ($id, $desc);
934d311cd44SJoe Perches}
935d311cd44SJoe Perches
9366c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9370a920b5bSAndy Whitcroft
93800df344fSAndy Whitcroftmy @rawlines = ();
939c2fdda0dSAndy Whitcroftmy @lines = ();
9403705ce5bSJoe Perchesmy @fixed = ();
941d752fcc8SJoe Perchesmy @fixed_inserted = ();
942d752fcc8SJoe Perchesmy @fixed_deleted = ();
943194f66fcSJoe Perchesmy $fixlinenr = -1;
944194f66fcSJoe Perches
9454a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
9464a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
9474a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
9484a593c34SDu, Changbin
9494a593c34SDu, Changbinif ($git) {
9504a593c34SDu, Changbin	my @commits = ();
9510dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
9524a593c34SDu, Changbin		my $git_range;
95328898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
95428898fd1SJoe Perches			$git_range = "-$2 $1";
9554a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
9564a593c34SDu, Changbin			$git_range = "$commit_expr";
9574a593c34SDu, Changbin		} else {
9580dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
9590dea9f1eSJoe Perches		}
9600dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
9610dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
96228898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
96328898fd1SJoe Perches			next if (!defined($1) || !defined($2));
9640dea9f1eSJoe Perches			my $sha1 = $1;
9650dea9f1eSJoe Perches			my $subject = $2;
9660dea9f1eSJoe Perches			unshift(@commits, $sha1);
9670dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
9684a593c34SDu, Changbin		}
9694a593c34SDu, Changbin	}
9704a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
9714a593c34SDu, Changbin	@ARGV = @commits;
9724a593c34SDu, Changbin}
9734a593c34SDu, Changbin
974c2fdda0dSAndy Whitcroftmy $vname;
9756c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
97621caa13cSAndy Whitcroft	my $FILE;
9774a593c34SDu, Changbin	if ($git) {
9784a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
9794a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
9804a593c34SDu, Changbin	} elsif ($file) {
98121caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
9826c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
98321caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
98421caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
9856c72ffaaSAndy Whitcroft	} else {
98621caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
9876c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
9886c72ffaaSAndy Whitcroft	}
989c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
990c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
9914a593c34SDu, Changbin	} elsif ($git) {
9920dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
993c2fdda0dSAndy Whitcroft	} else {
994c2fdda0dSAndy Whitcroft		$vname = $filename;
995c2fdda0dSAndy Whitcroft	}
99621caa13cSAndy Whitcroft	while (<$FILE>) {
9970a920b5bSAndy Whitcroft		chomp;
99800df344fSAndy Whitcroft		push(@rawlines, $_);
9996c72ffaaSAndy Whitcroft	}
100021caa13cSAndy Whitcroft	close($FILE);
1001d8469f16SJoe Perches
1002d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1003d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1004d8469f16SJoe Perches		print "$vname\n";
1005d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1006d8469f16SJoe Perches	}
1007d8469f16SJoe Perches
1008c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10090a920b5bSAndy Whitcroft		$exit = 1;
10100a920b5bSAndy Whitcroft	}
101100df344fSAndy Whitcroft	@rawlines = ();
101213214adfSAndy Whitcroft	@lines = ();
10133705ce5bSJoe Perches	@fixed = ();
1014d752fcc8SJoe Perches	@fixed_inserted = ();
1015d752fcc8SJoe Perches	@fixed_deleted = ();
1016194f66fcSJoe Perches	$fixlinenr = -1;
1017485ff23eSAlex Dowad	@modifierListFile = ();
1018485ff23eSAlex Dowad	@typeListFile = ();
1019485ff23eSAlex Dowad	build_types();
10200a920b5bSAndy Whitcroft}
10210a920b5bSAndy Whitcroft
1022d8469f16SJoe Perchesif (!$quiet) {
10233c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10243c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10253c816e49SJoe Perches
1026d8469f16SJoe Perches	if ($^V lt 5.10.0) {
1027d8469f16SJoe Perches		print << "EOM"
1028d8469f16SJoe Perches
1029d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
1030d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
1031d8469f16SJoe PerchesEOM
1032d8469f16SJoe Perches	}
1033d8469f16SJoe Perches	if ($exit) {
1034d8469f16SJoe Perches		print << "EOM"
1035d8469f16SJoe Perches
1036d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1037d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1038d8469f16SJoe PerchesEOM
1039d8469f16SJoe Perches	}
1040d8469f16SJoe Perches}
1041d8469f16SJoe Perches
10420a920b5bSAndy Whitcroftexit($exit);
10430a920b5bSAndy Whitcroft
10440a920b5bSAndy Whitcroftsub top_of_kernel_tree {
10456c72ffaaSAndy Whitcroft	my ($root) = @_;
10466c72ffaaSAndy Whitcroft
10476c72ffaaSAndy Whitcroft	my @tree_check = (
10486c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
10496c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
10506c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
10516c72ffaaSAndy Whitcroft	);
10526c72ffaaSAndy Whitcroft
10536c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
10546c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
10550a920b5bSAndy Whitcroft			return 0;
10560a920b5bSAndy Whitcroft		}
10576c72ffaaSAndy Whitcroft	}
10586c72ffaaSAndy Whitcroft	return 1;
10596c72ffaaSAndy Whitcroft}
10600a920b5bSAndy Whitcroft
106120112475SJoe Perchessub parse_email {
106220112475SJoe Perches	my ($formatted_email) = @_;
106320112475SJoe Perches
106420112475SJoe Perches	my $name = "";
106520112475SJoe Perches	my $address = "";
106620112475SJoe Perches	my $comment = "";
106720112475SJoe Perches
106820112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
106920112475SJoe Perches		$name = $1;
107020112475SJoe Perches		$address = $2;
107120112475SJoe Perches		$comment = $3 if defined $3;
107220112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
107320112475SJoe Perches		$address = $1;
107420112475SJoe Perches		$comment = $2 if defined $2;
107520112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
107620112475SJoe Perches		$address = $1;
107720112475SJoe Perches		$comment = $2 if defined $2;
107820112475SJoe Perches		$formatted_email =~ s/$address.*$//;
107920112475SJoe Perches		$name = $formatted_email;
10803705ce5bSJoe Perches		$name = trim($name);
108120112475SJoe Perches		$name =~ s/^\"|\"$//g;
108220112475SJoe Perches		# If there's a name left after stripping spaces and
108320112475SJoe Perches		# leading quotes, and the address doesn't have both
108420112475SJoe Perches		# leading and trailing angle brackets, the address
108520112475SJoe Perches		# is invalid. ie:
108620112475SJoe Perches		#   "joe smith [email protected]" bad
108720112475SJoe Perches		#   "joe smith <[email protected]" bad
108820112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
108920112475SJoe Perches			$name = "";
109020112475SJoe Perches			$address = "";
109120112475SJoe Perches			$comment = "";
109220112475SJoe Perches		}
109320112475SJoe Perches	}
109420112475SJoe Perches
10953705ce5bSJoe Perches	$name = trim($name);
109620112475SJoe Perches	$name =~ s/^\"|\"$//g;
10973705ce5bSJoe Perches	$address = trim($address);
109820112475SJoe Perches	$address =~ s/^\<|\>$//g;
109920112475SJoe Perches
110020112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
110120112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
110220112475SJoe Perches		$name = "\"$name\"";
110320112475SJoe Perches	}
110420112475SJoe Perches
110520112475SJoe Perches	return ($name, $address, $comment);
110620112475SJoe Perches}
110720112475SJoe Perches
110820112475SJoe Perchessub format_email {
110920112475SJoe Perches	my ($name, $address) = @_;
111020112475SJoe Perches
111120112475SJoe Perches	my $formatted_email;
111220112475SJoe Perches
11133705ce5bSJoe Perches	$name = trim($name);
111420112475SJoe Perches	$name =~ s/^\"|\"$//g;
11153705ce5bSJoe Perches	$address = trim($address);
111620112475SJoe Perches
111720112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
111820112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
111920112475SJoe Perches		$name = "\"$name\"";
112020112475SJoe Perches	}
112120112475SJoe Perches
112220112475SJoe Perches	if ("$name" eq "") {
112320112475SJoe Perches		$formatted_email = "$address";
112420112475SJoe Perches	} else {
112520112475SJoe Perches		$formatted_email = "$name <$address>";
112620112475SJoe Perches	}
112720112475SJoe Perches
112820112475SJoe Perches	return $formatted_email;
112920112475SJoe Perches}
113020112475SJoe Perches
1131d311cd44SJoe Perchessub which {
1132d311cd44SJoe Perches	my ($bin) = @_;
1133d311cd44SJoe Perches
1134d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1135d311cd44SJoe Perches		if (-e "$path/$bin") {
1136d311cd44SJoe Perches			return "$path/$bin";
1137d311cd44SJoe Perches		}
1138d311cd44SJoe Perches	}
1139d311cd44SJoe Perches
1140d311cd44SJoe Perches	return "";
1141d311cd44SJoe Perches}
1142d311cd44SJoe Perches
1143000d1cc1SJoe Perchessub which_conf {
1144000d1cc1SJoe Perches	my ($conf) = @_;
1145000d1cc1SJoe Perches
1146000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1147000d1cc1SJoe Perches		if (-e "$path/$conf") {
1148000d1cc1SJoe Perches			return "$path/$conf";
1149000d1cc1SJoe Perches		}
1150000d1cc1SJoe Perches	}
1151000d1cc1SJoe Perches
1152000d1cc1SJoe Perches	return "";
1153000d1cc1SJoe Perches}
1154000d1cc1SJoe Perches
11550a920b5bSAndy Whitcroftsub expand_tabs {
11560a920b5bSAndy Whitcroft	my ($str) = @_;
11570a920b5bSAndy Whitcroft
11580a920b5bSAndy Whitcroft	my $res = '';
11590a920b5bSAndy Whitcroft	my $n = 0;
11600a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
11610a920b5bSAndy Whitcroft		if ($c eq "\t") {
11620a920b5bSAndy Whitcroft			$res .= ' ';
11630a920b5bSAndy Whitcroft			$n++;
11640a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
11650a920b5bSAndy Whitcroft				$res .= ' ';
11660a920b5bSAndy Whitcroft			}
11670a920b5bSAndy Whitcroft			next;
11680a920b5bSAndy Whitcroft		}
11690a920b5bSAndy Whitcroft		$res .= $c;
11700a920b5bSAndy Whitcroft		$n++;
11710a920b5bSAndy Whitcroft	}
11720a920b5bSAndy Whitcroft
11730a920b5bSAndy Whitcroft	return $res;
11740a920b5bSAndy Whitcroft}
11756c72ffaaSAndy Whitcroftsub copy_spacing {
1176773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
11776c72ffaaSAndy Whitcroft	return $res;
11786c72ffaaSAndy Whitcroft}
11790a920b5bSAndy Whitcroft
11804a0df2efSAndy Whitcroftsub line_stats {
11814a0df2efSAndy Whitcroft	my ($line) = @_;
11824a0df2efSAndy Whitcroft
11834a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
11844a0df2efSAndy Whitcroft	$line =~ s/^.//;
11854a0df2efSAndy Whitcroft	$line = expand_tabs($line);
11864a0df2efSAndy Whitcroft
11874a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
11884a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
11894a0df2efSAndy Whitcroft
11904a0df2efSAndy Whitcroft	return (length($line), length($white));
11914a0df2efSAndy Whitcroft}
11924a0df2efSAndy Whitcroft
1193773647a0SAndy Whitcroftmy $sanitise_quote = '';
1194773647a0SAndy Whitcroft
1195773647a0SAndy Whitcroftsub sanitise_line_reset {
1196773647a0SAndy Whitcroft	my ($in_comment) = @_;
1197773647a0SAndy Whitcroft
1198773647a0SAndy Whitcroft	if ($in_comment) {
1199773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1200773647a0SAndy Whitcroft	} else {
1201773647a0SAndy Whitcroft		$sanitise_quote = '';
1202773647a0SAndy Whitcroft	}
1203773647a0SAndy Whitcroft}
120400df344fSAndy Whitcroftsub sanitise_line {
120500df344fSAndy Whitcroft	my ($line) = @_;
120600df344fSAndy Whitcroft
120700df344fSAndy Whitcroft	my $res = '';
120800df344fSAndy Whitcroft	my $l = '';
120900df344fSAndy Whitcroft
1210c2fdda0dSAndy Whitcroft	my $qlen = 0;
1211773647a0SAndy Whitcroft	my $off = 0;
1212773647a0SAndy Whitcroft	my $c;
121300df344fSAndy Whitcroft
1214773647a0SAndy Whitcroft	# Always copy over the diff marker.
1215773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1216773647a0SAndy Whitcroft
1217773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1218773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1219773647a0SAndy Whitcroft
1220773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
1221773647a0SAndy Whitcroft		# and end, all to $;.
1222773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1223773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1224773647a0SAndy Whitcroft
1225773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1226773647a0SAndy Whitcroft			$off++;
122700df344fSAndy Whitcroft			next;
1228773647a0SAndy Whitcroft		}
122981bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1230773647a0SAndy Whitcroft			$sanitise_quote = '';
1231773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1232773647a0SAndy Whitcroft			$off++;
1233773647a0SAndy Whitcroft			next;
1234773647a0SAndy Whitcroft		}
1235113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1236113f04a8SDaniel Walker			$sanitise_quote = '//';
1237113f04a8SDaniel Walker
1238113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1239113f04a8SDaniel Walker			$off++;
1240113f04a8SDaniel Walker			next;
1241113f04a8SDaniel Walker		}
1242773647a0SAndy Whitcroft
1243773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1244773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1245773647a0SAndy Whitcroft		    $c eq "\\") {
1246773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1247773647a0SAndy Whitcroft			$off++;
1248773647a0SAndy Whitcroft			next;
1249773647a0SAndy Whitcroft		}
1250773647a0SAndy Whitcroft		# Regular quotes.
1251773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1252773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1253773647a0SAndy Whitcroft				$sanitise_quote = $c;
1254773647a0SAndy Whitcroft
1255773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1256773647a0SAndy Whitcroft				next;
1257773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1258773647a0SAndy Whitcroft				$sanitise_quote = '';
125900df344fSAndy Whitcroft			}
126000df344fSAndy Whitcroft		}
1261773647a0SAndy Whitcroft
1262fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1263773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1264773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1265113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1266113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1267773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1268773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
126900df344fSAndy Whitcroft		} else {
1270773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
127100df344fSAndy Whitcroft		}
1272c2fdda0dSAndy Whitcroft	}
1273c2fdda0dSAndy Whitcroft
1274113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1275113f04a8SDaniel Walker		$sanitise_quote = '';
1276113f04a8SDaniel Walker	}
1277113f04a8SDaniel Walker
1278c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1279c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1280c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1281c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1282c2fdda0dSAndy Whitcroft
1283c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1284c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1285c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1286c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1287c2fdda0dSAndy Whitcroft	}
1288c2fdda0dSAndy Whitcroft
1289dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1290dadf680dSJoe Perches		my $match = $1;
1291dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1292dadf680dSJoe Perches	}
1293dadf680dSJoe Perches
129400df344fSAndy Whitcroft	return $res;
129500df344fSAndy Whitcroft}
129600df344fSAndy Whitcroft
1297a6962d72SJoe Perchessub get_quoted_string {
1298a6962d72SJoe Perches	my ($line, $rawline) = @_;
1299a6962d72SJoe Perches
130033acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1301a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1302a6962d72SJoe Perches}
1303a6962d72SJoe Perches
13048905a67cSAndy Whitcroftsub ctx_statement_block {
13058905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13068905a67cSAndy Whitcroft	my $line = $linenr - 1;
13078905a67cSAndy Whitcroft	my $blk = '';
13088905a67cSAndy Whitcroft	my $soff = $off;
13098905a67cSAndy Whitcroft	my $coff = $off - 1;
1310773647a0SAndy Whitcroft	my $coff_set = 0;
13118905a67cSAndy Whitcroft
131213214adfSAndy Whitcroft	my $loff = 0;
131313214adfSAndy Whitcroft
13148905a67cSAndy Whitcroft	my $type = '';
13158905a67cSAndy Whitcroft	my $level = 0;
1316a2750645SAndy Whitcroft	my @stack = ();
1317cf655043SAndy Whitcroft	my $p;
13188905a67cSAndy Whitcroft	my $c;
13198905a67cSAndy Whitcroft	my $len = 0;
132013214adfSAndy Whitcroft
132113214adfSAndy Whitcroft	my $remainder;
13228905a67cSAndy Whitcroft	while (1) {
1323a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1324a2750645SAndy Whitcroft
1325773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
13268905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
13278905a67cSAndy Whitcroft		# context.
13288905a67cSAndy Whitcroft		if ($off >= $len) {
13298905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1330dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1331c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
13328905a67cSAndy Whitcroft				$remain--;
133313214adfSAndy Whitcroft				$loff = $len;
1334c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13358905a67cSAndy Whitcroft				$len = length($blk);
13368905a67cSAndy Whitcroft				$line++;
13378905a67cSAndy Whitcroft				last;
13388905a67cSAndy Whitcroft			}
13398905a67cSAndy Whitcroft			# Bail if there is no further context.
13408905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
134113214adfSAndy Whitcroft			if ($off >= $len) {
13428905a67cSAndy Whitcroft				last;
13438905a67cSAndy Whitcroft			}
1344f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1345f74bd194SAndy Whitcroft				$level++;
1346f74bd194SAndy Whitcroft				$type = '#';
1347f74bd194SAndy Whitcroft			}
13488905a67cSAndy Whitcroft		}
1349cf655043SAndy Whitcroft		$p = $c;
13508905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
135113214adfSAndy Whitcroft		$remainder = substr($blk, $off);
13528905a67cSAndy Whitcroft
1353773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
13544635f4fbSAndy Whitcroft
13554635f4fbSAndy Whitcroft		# Handle nested #if/#else.
13564635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
13574635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
13584635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
13594635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
13604635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
13614635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
13624635f4fbSAndy Whitcroft		}
13634635f4fbSAndy Whitcroft
13648905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
13658905a67cSAndy Whitcroft		# outermost level.
13668905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
13678905a67cSAndy Whitcroft			last;
13688905a67cSAndy Whitcroft		}
13698905a67cSAndy Whitcroft
137013214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1371773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1372773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1373773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1374773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1375773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1376773647a0SAndy Whitcroft			$coff_set = 1;
1377773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1378773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
137913214adfSAndy Whitcroft		}
138013214adfSAndy Whitcroft
13818905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
13828905a67cSAndy Whitcroft			$level++;
13838905a67cSAndy Whitcroft			$type = '(';
13848905a67cSAndy Whitcroft		}
13858905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
13868905a67cSAndy Whitcroft			$level--;
13878905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
13888905a67cSAndy Whitcroft
13898905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
13908905a67cSAndy Whitcroft				$coff = $off;
1391773647a0SAndy Whitcroft				$coff_set = 1;
1392773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
13938905a67cSAndy Whitcroft			}
13948905a67cSAndy Whitcroft		}
13958905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
13968905a67cSAndy Whitcroft			$level++;
13978905a67cSAndy Whitcroft			$type = '{';
13988905a67cSAndy Whitcroft		}
13998905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14008905a67cSAndy Whitcroft			$level--;
14018905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14028905a67cSAndy Whitcroft
14038905a67cSAndy Whitcroft			if ($level == 0) {
1404b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1405b998e001SPatrick Pannuto					$off++;
1406b998e001SPatrick Pannuto				}
14078905a67cSAndy Whitcroft				last;
14088905a67cSAndy Whitcroft			}
14098905a67cSAndy Whitcroft		}
1410f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1411f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1412f74bd194SAndy Whitcroft			$level--;
1413f74bd194SAndy Whitcroft			$type = '';
1414f74bd194SAndy Whitcroft			$off++;
1415f74bd194SAndy Whitcroft			last;
1416f74bd194SAndy Whitcroft		}
14178905a67cSAndy Whitcroft		$off++;
14188905a67cSAndy Whitcroft	}
1419a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
142013214adfSAndy Whitcroft	if ($off == $len) {
1421a3bb97a7SAndy Whitcroft		$loff = $len + 1;
142213214adfSAndy Whitcroft		$line++;
142313214adfSAndy Whitcroft		$remain--;
142413214adfSAndy Whitcroft	}
14258905a67cSAndy Whitcroft
14268905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
14278905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
14288905a67cSAndy Whitcroft
14298905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
14308905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
14318905a67cSAndy Whitcroft
1432773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
143313214adfSAndy Whitcroft
143413214adfSAndy Whitcroft	return ($statement, $condition,
143513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
143613214adfSAndy Whitcroft}
143713214adfSAndy Whitcroft
1438cf655043SAndy Whitcroftsub statement_lines {
1439cf655043SAndy Whitcroft	my ($stmt) = @_;
1440cf655043SAndy Whitcroft
1441cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1442cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1443cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1444cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1445cf655043SAndy Whitcroft
1446cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1447cf655043SAndy Whitcroft
1448cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1449cf655043SAndy Whitcroft}
1450cf655043SAndy Whitcroft
1451cf655043SAndy Whitcroftsub statement_rawlines {
1452cf655043SAndy Whitcroft	my ($stmt) = @_;
1453cf655043SAndy Whitcroft
1454cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1455cf655043SAndy Whitcroft
1456cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1457cf655043SAndy Whitcroft}
1458cf655043SAndy Whitcroft
1459cf655043SAndy Whitcroftsub statement_block_size {
1460cf655043SAndy Whitcroft	my ($stmt) = @_;
1461cf655043SAndy Whitcroft
1462cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1463cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1464cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1465cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1466cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1467cf655043SAndy Whitcroft
1468cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1469cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1470cf655043SAndy Whitcroft
1471cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1472cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1473cf655043SAndy Whitcroft
1474cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1475cf655043SAndy Whitcroft		return $stmt_lines;
1476cf655043SAndy Whitcroft	} else {
1477cf655043SAndy Whitcroft		return $stmt_statements;
1478cf655043SAndy Whitcroft	}
1479cf655043SAndy Whitcroft}
1480cf655043SAndy Whitcroft
148113214adfSAndy Whitcroftsub ctx_statement_full {
148213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
148313214adfSAndy Whitcroft	my ($statement, $condition, $level);
148413214adfSAndy Whitcroft
148513214adfSAndy Whitcroft	my (@chunks);
148613214adfSAndy Whitcroft
1487cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
148813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
148913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1490773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
149113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1492cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1493cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1494cf655043SAndy Whitcroft	}
1495cf655043SAndy Whitcroft
1496cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1497cf655043SAndy Whitcroft	# could continue the statement.
1498cf655043SAndy Whitcroft	for (;;) {
149913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
150013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1501cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1502773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1503cf655043SAndy Whitcroft		#print "C: push\n";
1504cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
150513214adfSAndy Whitcroft	}
150613214adfSAndy Whitcroft
150713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
15088905a67cSAndy Whitcroft}
15098905a67cSAndy Whitcroft
15104a0df2efSAndy Whitcroftsub ctx_block_get {
1511f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
15124a0df2efSAndy Whitcroft	my $line;
15134a0df2efSAndy Whitcroft	my $start = $linenr - 1;
15144a0df2efSAndy Whitcroft	my $blk = '';
15154a0df2efSAndy Whitcroft	my @o;
15164a0df2efSAndy Whitcroft	my @c;
15174a0df2efSAndy Whitcroft	my @res = ();
15184a0df2efSAndy Whitcroft
1519f0a594c1SAndy Whitcroft	my $level = 0;
15204635f4fbSAndy Whitcroft	my @stack = ($level);
152100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
152200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
152300df344fSAndy Whitcroft		$remain--;
152400df344fSAndy Whitcroft
152500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
15264635f4fbSAndy Whitcroft
15274635f4fbSAndy Whitcroft		# Handle nested #if/#else.
152801464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
15294635f4fbSAndy Whitcroft			push(@stack, $level);
153001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
15314635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
153201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
15334635f4fbSAndy Whitcroft			$level = pop(@stack);
15344635f4fbSAndy Whitcroft		}
15354635f4fbSAndy Whitcroft
153601464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1537f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1538f0a594c1SAndy Whitcroft			if ($off > 0) {
1539f0a594c1SAndy Whitcroft				$off--;
1540f0a594c1SAndy Whitcroft				next;
1541f0a594c1SAndy Whitcroft			}
15424a0df2efSAndy Whitcroft
1543f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1544f0a594c1SAndy Whitcroft				$level--;
1545f0a594c1SAndy Whitcroft				last if ($level == 0);
1546f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1547f0a594c1SAndy Whitcroft				$level++;
1548f0a594c1SAndy Whitcroft			}
1549f0a594c1SAndy Whitcroft		}
15504a0df2efSAndy Whitcroft
1551f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
155200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
15534a0df2efSAndy Whitcroft		}
15544a0df2efSAndy Whitcroft
1555f0a594c1SAndy Whitcroft		last if ($level == 0);
15564a0df2efSAndy Whitcroft	}
15574a0df2efSAndy Whitcroft
1558f0a594c1SAndy Whitcroft	return ($level, @res);
15594a0df2efSAndy Whitcroft}
15604a0df2efSAndy Whitcroftsub ctx_block_outer {
15614a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15624a0df2efSAndy Whitcroft
1563f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1564f0a594c1SAndy Whitcroft	return @r;
15654a0df2efSAndy Whitcroft}
15664a0df2efSAndy Whitcroftsub ctx_block {
15674a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15684a0df2efSAndy Whitcroft
1569f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1570f0a594c1SAndy Whitcroft	return @r;
1571653d4876SAndy Whitcroft}
1572653d4876SAndy Whitcroftsub ctx_statement {
1573f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1574f0a594c1SAndy Whitcroft
1575f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1576f0a594c1SAndy Whitcroft	return @r;
1577f0a594c1SAndy Whitcroft}
1578f0a594c1SAndy Whitcroftsub ctx_block_level {
1579653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1580653d4876SAndy Whitcroft
1581f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
15824a0df2efSAndy Whitcroft}
15839c0ca6f9SAndy Whitcroftsub ctx_statement_level {
15849c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
15859c0ca6f9SAndy Whitcroft
15869c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
15879c0ca6f9SAndy Whitcroft}
15884a0df2efSAndy Whitcroft
15894a0df2efSAndy Whitcroftsub ctx_locate_comment {
15904a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15914a0df2efSAndy Whitcroft
15924a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1593beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
15944a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
15954a0df2efSAndy Whitcroft
15964a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
15974a0df2efSAndy Whitcroft	# comment.
15984a0df2efSAndy Whitcroft	my $in_comment = 0;
15994a0df2efSAndy Whitcroft	$current_comment = '';
16004a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
160100df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
160200df344fSAndy Whitcroft		#warn "           $line\n";
16034a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
16044a0df2efSAndy Whitcroft			$in_comment = 1;
16054a0df2efSAndy Whitcroft		}
16064a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
16074a0df2efSAndy Whitcroft			$in_comment = 1;
16084a0df2efSAndy Whitcroft		}
16094a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
16104a0df2efSAndy Whitcroft			$current_comment = '';
16114a0df2efSAndy Whitcroft		}
16124a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
16134a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
16144a0df2efSAndy Whitcroft			$in_comment = 0;
16154a0df2efSAndy Whitcroft		}
16164a0df2efSAndy Whitcroft	}
16174a0df2efSAndy Whitcroft
16184a0df2efSAndy Whitcroft	chomp($current_comment);
16194a0df2efSAndy Whitcroft	return($current_comment);
16204a0df2efSAndy Whitcroft}
16214a0df2efSAndy Whitcroftsub ctx_has_comment {
16224a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16234a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
16244a0df2efSAndy Whitcroft
162500df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
16264a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
16274a0df2efSAndy Whitcroft
16284a0df2efSAndy Whitcroft	return ($cmt ne '');
16294a0df2efSAndy Whitcroft}
16304a0df2efSAndy Whitcroft
16314d001e4dSAndy Whitcroftsub raw_line {
16324d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
16334d001e4dSAndy Whitcroft
16344d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16354d001e4dSAndy Whitcroft	$cnt++;
16364d001e4dSAndy Whitcroft
16374d001e4dSAndy Whitcroft	my $line;
16384d001e4dSAndy Whitcroft	while ($cnt) {
16394d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16404d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16414d001e4dSAndy Whitcroft		$cnt--;
16424d001e4dSAndy Whitcroft	}
16434d001e4dSAndy Whitcroft
16444d001e4dSAndy Whitcroft	return $line;
16454d001e4dSAndy Whitcroft}
16464d001e4dSAndy Whitcroft
16470a920b5bSAndy Whitcroftsub cat_vet {
16480a920b5bSAndy Whitcroft	my ($vet) = @_;
16499c0ca6f9SAndy Whitcroft	my ($res, $coded);
16500a920b5bSAndy Whitcroft
16519c0ca6f9SAndy Whitcroft	$res = '';
16526c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
16536c72ffaaSAndy Whitcroft		$res .= $1;
16546c72ffaaSAndy Whitcroft		if ($2 ne '') {
16559c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
16566c72ffaaSAndy Whitcroft			$res .= $coded;
16576c72ffaaSAndy Whitcroft		}
16589c0ca6f9SAndy Whitcroft	}
16599c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
16600a920b5bSAndy Whitcroft
16619c0ca6f9SAndy Whitcroft	return $res;
16620a920b5bSAndy Whitcroft}
16630a920b5bSAndy Whitcroft
1664c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1665cf655043SAndy Whitcroftmy $av_pending;
1666c2fdda0dSAndy Whitcroftmy @av_paren_type;
16671f65f947SAndy Whitcroftmy $av_pend_colon;
1668c2fdda0dSAndy Whitcroft
1669c2fdda0dSAndy Whitcroftsub annotate_reset {
1670c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1671cf655043SAndy Whitcroft	$av_pending = '_';
1672cf655043SAndy Whitcroft	@av_paren_type = ('E');
16731f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1674c2fdda0dSAndy Whitcroft}
1675c2fdda0dSAndy Whitcroft
16766c72ffaaSAndy Whitcroftsub annotate_values {
16776c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
16786c72ffaaSAndy Whitcroft
16796c72ffaaSAndy Whitcroft	my $res;
16801f65f947SAndy Whitcroft	my $var = '_' x length($stream);
16816c72ffaaSAndy Whitcroft	my $cur = $stream;
16826c72ffaaSAndy Whitcroft
1683c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
16846c72ffaaSAndy Whitcroft
16856c72ffaaSAndy Whitcroft	while (length($cur)) {
1686773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1687cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1688171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
16896c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1690c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1691c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1692cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1693c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
16946c72ffaaSAndy Whitcroft			}
16956c72ffaaSAndy Whitcroft
1696c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
16979446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
16989446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1699addcdceaSAndy Whitcroft			$type = 'c';
17009446ef56SAndy Whitcroft
1701e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1702c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
17036c72ffaaSAndy Whitcroft			$type = 'T';
17046c72ffaaSAndy Whitcroft
1705389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1706389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1707389a2fe5SAndy Whitcroft			$type = 'T';
1708389a2fe5SAndy Whitcroft
1709c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1710171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1711c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1712171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1713171ae1a4SAndy Whitcroft			if ($2 ne '') {
1714cf655043SAndy Whitcroft				$av_pending = 'N';
1715171ae1a4SAndy Whitcroft			}
1716171ae1a4SAndy Whitcroft			$type = 'E';
1717171ae1a4SAndy Whitcroft
1718c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1719171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1720171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1721171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
17226c72ffaaSAndy Whitcroft
1723c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1724cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1725c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1726cf655043SAndy Whitcroft
1727cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1728cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1729171ae1a4SAndy Whitcroft			$type = 'E';
1730cf655043SAndy Whitcroft
1731c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1732cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1733cf655043SAndy Whitcroft			$av_preprocessor = 1;
1734cf655043SAndy Whitcroft
1735cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1736cf655043SAndy Whitcroft
1737171ae1a4SAndy Whitcroft			$type = 'E';
1738cf655043SAndy Whitcroft
1739c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1740cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1741cf655043SAndy Whitcroft
1742cf655043SAndy Whitcroft			$av_preprocessor = 1;
1743cf655043SAndy Whitcroft
1744cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1745cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1746cf655043SAndy Whitcroft			pop(@av_paren_type);
1747cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1748171ae1a4SAndy Whitcroft			$type = 'E';
17496c72ffaaSAndy Whitcroft
17506c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1751c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
17526c72ffaaSAndy Whitcroft
1753171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1754171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1755171ae1a4SAndy Whitcroft			$av_pending = $type;
1756171ae1a4SAndy Whitcroft			$type = 'N';
1757171ae1a4SAndy Whitcroft
17586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1759c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
17606c72ffaaSAndy Whitcroft			if (defined $2) {
1761cf655043SAndy Whitcroft				$av_pending = 'V';
17626c72ffaaSAndy Whitcroft			}
17636c72ffaaSAndy Whitcroft			$type = 'N';
17646c72ffaaSAndy Whitcroft
176514b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1766c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
176714b111c1SAndy Whitcroft			$av_pending = 'E';
17686c72ffaaSAndy Whitcroft			$type = 'N';
17696c72ffaaSAndy Whitcroft
17701f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
17711f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
17721f65f947SAndy Whitcroft			$av_pend_colon = 'C';
17731f65f947SAndy Whitcroft			$type = 'N';
17741f65f947SAndy Whitcroft
177514b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1776c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
17776c72ffaaSAndy Whitcroft			$type = 'N';
17786c72ffaaSAndy Whitcroft
17796c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1780c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1781cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1782cf655043SAndy Whitcroft			$av_pending = '_';
17836c72ffaaSAndy Whitcroft			$type = 'N';
17846c72ffaaSAndy Whitcroft
17856c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1786cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1787cf655043SAndy Whitcroft			if ($new_type ne '_') {
1788cf655043SAndy Whitcroft				$type = $new_type;
1789c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1790c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
17916c72ffaaSAndy Whitcroft			} else {
1792c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
17936c72ffaaSAndy Whitcroft			}
17946c72ffaaSAndy Whitcroft
1795c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1796c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1797c8cb2ca3SAndy Whitcroft			$type = 'V';
1798cf655043SAndy Whitcroft			$av_pending = 'V';
17996c72ffaaSAndy Whitcroft
18008e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
18018e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
18021f65f947SAndy Whitcroft				$av_pend_colon = 'B';
18038e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
18048e761b04SAndy Whitcroft				$av_pend_colon = 'L';
18051f65f947SAndy Whitcroft			}
18061f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
18071f65f947SAndy Whitcroft			$type = 'V';
18081f65f947SAndy Whitcroft
18096c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1810c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
18116c72ffaaSAndy Whitcroft			$type = 'V';
18126c72ffaaSAndy Whitcroft
18136c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1814c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
18156c72ffaaSAndy Whitcroft			$type = 'N';
18166c72ffaaSAndy Whitcroft
1817cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1818c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
181913214adfSAndy Whitcroft			$type = 'E';
18201f65f947SAndy Whitcroft			$av_pend_colon = 'O';
182113214adfSAndy Whitcroft
18228e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
18238e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
18248e761b04SAndy Whitcroft			$type = 'C';
18258e761b04SAndy Whitcroft
18261f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
18271f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
18281f65f947SAndy Whitcroft			$type = 'N';
18291f65f947SAndy Whitcroft
18301f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
18311f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
18321f65f947SAndy Whitcroft
18331f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
18341f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
18351f65f947SAndy Whitcroft				$type = 'E';
18361f65f947SAndy Whitcroft			} else {
18371f65f947SAndy Whitcroft				$type = 'N';
18381f65f947SAndy Whitcroft			}
18391f65f947SAndy Whitcroft			$av_pend_colon = 'O';
18401f65f947SAndy Whitcroft
18418e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
184213214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
18436c72ffaaSAndy Whitcroft			$type = 'N';
18446c72ffaaSAndy Whitcroft
18450d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
184674048ed8SAndy Whitcroft			my $variant;
184774048ed8SAndy Whitcroft
184874048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
184974048ed8SAndy Whitcroft			if ($type eq 'V') {
185074048ed8SAndy Whitcroft				$variant = 'B';
185174048ed8SAndy Whitcroft			} else {
185274048ed8SAndy Whitcroft				$variant = 'U';
185374048ed8SAndy Whitcroft			}
185474048ed8SAndy Whitcroft
185574048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
185674048ed8SAndy Whitcroft			$type = 'N';
185774048ed8SAndy Whitcroft
18586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1859c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
18606c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
18616c72ffaaSAndy Whitcroft				$type = 'N';
18626c72ffaaSAndy Whitcroft			}
18636c72ffaaSAndy Whitcroft
18646c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1865c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
18666c72ffaaSAndy Whitcroft		}
18676c72ffaaSAndy Whitcroft		if (defined $1) {
18686c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
18696c72ffaaSAndy Whitcroft			$res .= $type x length($1);
18706c72ffaaSAndy Whitcroft		}
18716c72ffaaSAndy Whitcroft	}
18726c72ffaaSAndy Whitcroft
18731f65f947SAndy Whitcroft	return ($res, $var);
18746c72ffaaSAndy Whitcroft}
18756c72ffaaSAndy Whitcroft
18768905a67cSAndy Whitcroftsub possible {
187713214adfSAndy Whitcroft	my ($possible, $line) = @_;
18789a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
18790776e594SAndy Whitcroft		^(?:
18800776e594SAndy Whitcroft			$Modifier|
18810776e594SAndy Whitcroft			$Storage|
18820776e594SAndy Whitcroft			$Type|
18839a974fdbSAndy Whitcroft			DEFINE_\S+
18849a974fdbSAndy Whitcroft		)$|
18859a974fdbSAndy Whitcroft		^(?:
18860776e594SAndy Whitcroft			goto|
18870776e594SAndy Whitcroft			return|
18880776e594SAndy Whitcroft			case|
18890776e594SAndy Whitcroft			else|
18900776e594SAndy Whitcroft			asm|__asm__|
189189a88353SAndy Whitcroft			do|
189289a88353SAndy Whitcroft			\#|
189389a88353SAndy Whitcroft			\#\#|
18949a974fdbSAndy Whitcroft		)(?:\s|$)|
18950776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
18969a974fdbSAndy Whitcroft	    )}x;
18979a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
18989a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1899c45dcabdSAndy Whitcroft		# Check for modifiers.
1900c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1901c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1902c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1903c45dcabdSAndy Whitcroft
1904c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1905c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1906d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
19079a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1908d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1909485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1910d2506586SAndy Whitcroft				}
19119a974fdbSAndy Whitcroft			}
1912c45dcabdSAndy Whitcroft
1913c45dcabdSAndy Whitcroft		} else {
191413214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1915485ff23eSAlex Dowad			push(@typeListFile, $possible);
1916c45dcabdSAndy Whitcroft		}
19178905a67cSAndy Whitcroft		build_types();
19180776e594SAndy Whitcroft	} else {
19190776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
19208905a67cSAndy Whitcroft	}
19218905a67cSAndy Whitcroft}
19228905a67cSAndy Whitcroft
19236c72ffaaSAndy Whitcroftmy $prefix = '';
19246c72ffaaSAndy Whitcroft
1925000d1cc1SJoe Perchessub show_type {
1926cbec18afSJoe Perches	my ($type) = @_;
192791bfe484SJoe Perches
1928522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
1929522b837cSAlexey Dobriyan
1930cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1931cbec18afSJoe Perches
1932cbec18afSJoe Perches	return !defined $ignore_type{$type};
1933000d1cc1SJoe Perches}
1934000d1cc1SJoe Perches
1935f0a594c1SAndy Whitcroftsub report {
1936cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1937cbec18afSJoe Perches
1938cbec18afSJoe Perches	if (!show_type($type) ||
1939cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1940773647a0SAndy Whitcroft		return 0;
1941773647a0SAndy Whitcroft	}
194257230297SJoe Perches	my $output = '';
1943737c0767SJohn Brooks	if ($color) {
194457230297SJoe Perches		if ($level eq 'ERROR') {
194557230297SJoe Perches			$output .= RED;
194657230297SJoe Perches		} elsif ($level eq 'WARNING') {
194757230297SJoe Perches			$output .= YELLOW;
1948000d1cc1SJoe Perches		} else {
194957230297SJoe Perches			$output .= GREEN;
1950000d1cc1SJoe Perches		}
195157230297SJoe Perches	}
195257230297SJoe Perches	$output .= $prefix . $level . ':';
195357230297SJoe Perches	if ($show_types) {
1954737c0767SJohn Brooks		$output .= BLUE if ($color);
195557230297SJoe Perches		$output .= "$type:";
195657230297SJoe Perches	}
1957737c0767SJohn Brooks	$output .= RESET if ($color);
195857230297SJoe Perches	$output .= ' ' . $msg . "\n";
195934d8815fSJoe Perches
196034d8815fSJoe Perches	if ($showfile) {
196134d8815fSJoe Perches		my @lines = split("\n", $output, -1);
196234d8815fSJoe Perches		splice(@lines, 1, 1);
196334d8815fSJoe Perches		$output = join("\n", @lines);
196434d8815fSJoe Perches	}
196557230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
19668905a67cSAndy Whitcroft
196757230297SJoe Perches	push(our @report, $output);
1968773647a0SAndy Whitcroft
1969773647a0SAndy Whitcroft	return 1;
1970f0a594c1SAndy Whitcroft}
1971cbec18afSJoe Perches
1972f0a594c1SAndy Whitcroftsub report_dump {
197313214adfSAndy Whitcroft	our @report;
1974f0a594c1SAndy Whitcroft}
1975000d1cc1SJoe Perches
1976d752fcc8SJoe Perchessub fixup_current_range {
1977d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1978d752fcc8SJoe Perches
1979d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1980d752fcc8SJoe Perches		my $o = $1;
1981d752fcc8SJoe Perches		my $l = $2;
1982d752fcc8SJoe Perches		my $no = $o + $offset;
1983d752fcc8SJoe Perches		my $nl = $l + $length;
1984d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1985d752fcc8SJoe Perches	}
1986d752fcc8SJoe Perches}
1987d752fcc8SJoe Perches
1988d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1989d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1990d752fcc8SJoe Perches
1991d752fcc8SJoe Perches	my $range_last_linenr = 0;
1992d752fcc8SJoe Perches	my $delta_offset = 0;
1993d752fcc8SJoe Perches
1994d752fcc8SJoe Perches	my $old_linenr = 0;
1995d752fcc8SJoe Perches	my $new_linenr = 0;
1996d752fcc8SJoe Perches
1997d752fcc8SJoe Perches	my $next_insert = 0;
1998d752fcc8SJoe Perches	my $next_delete = 0;
1999d752fcc8SJoe Perches
2000d752fcc8SJoe Perches	my @lines = ();
2001d752fcc8SJoe Perches
2002d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2003d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2004d752fcc8SJoe Perches
2005d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2006d752fcc8SJoe Perches		my $save_line = 1;
2007d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2008323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2009d752fcc8SJoe Perches			$delta_offset = 0;
2010d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2011d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2012d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2013d752fcc8SJoe Perches		}
2014d752fcc8SJoe Perches
2015d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2016d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2017d752fcc8SJoe Perches			$save_line = 0;
2018d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2019d752fcc8SJoe Perches		}
2020d752fcc8SJoe Perches
2021d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2022d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2023d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2024d752fcc8SJoe Perches			$new_linenr++;
2025d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2026d752fcc8SJoe Perches		}
2027d752fcc8SJoe Perches
2028d752fcc8SJoe Perches		if ($save_line) {
2029d752fcc8SJoe Perches			push(@lines, $line);
2030d752fcc8SJoe Perches			$new_linenr++;
2031d752fcc8SJoe Perches		}
2032d752fcc8SJoe Perches
2033d752fcc8SJoe Perches		$old_linenr++;
2034d752fcc8SJoe Perches	}
2035d752fcc8SJoe Perches
2036d752fcc8SJoe Perches	return @lines;
2037d752fcc8SJoe Perches}
2038d752fcc8SJoe Perches
2039f2d7e4d4SJoe Perchessub fix_insert_line {
2040f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2041f2d7e4d4SJoe Perches
2042f2d7e4d4SJoe Perches	my $inserted = {
2043f2d7e4d4SJoe Perches		LINENR => $linenr,
2044f2d7e4d4SJoe Perches		LINE => $line,
2045f2d7e4d4SJoe Perches	};
2046f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2047f2d7e4d4SJoe Perches}
2048f2d7e4d4SJoe Perches
2049f2d7e4d4SJoe Perchessub fix_delete_line {
2050f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2051f2d7e4d4SJoe Perches
2052f2d7e4d4SJoe Perches	my $deleted = {
2053f2d7e4d4SJoe Perches		LINENR => $linenr,
2054f2d7e4d4SJoe Perches		LINE => $line,
2055f2d7e4d4SJoe Perches	};
2056f2d7e4d4SJoe Perches
2057f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2058f2d7e4d4SJoe Perches}
2059f2d7e4d4SJoe Perches
2060de7d4f0eSAndy Whitcroftsub ERROR {
2061cbec18afSJoe Perches	my ($type, $msg) = @_;
2062cbec18afSJoe Perches
2063cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2064de7d4f0eSAndy Whitcroft		our $clean = 0;
20656c72ffaaSAndy Whitcroft		our $cnt_error++;
20663705ce5bSJoe Perches		return 1;
2067de7d4f0eSAndy Whitcroft	}
20683705ce5bSJoe Perches	return 0;
2069773647a0SAndy Whitcroft}
2070de7d4f0eSAndy Whitcroftsub WARN {
2071cbec18afSJoe Perches	my ($type, $msg) = @_;
2072cbec18afSJoe Perches
2073cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2074de7d4f0eSAndy Whitcroft		our $clean = 0;
20756c72ffaaSAndy Whitcroft		our $cnt_warn++;
20763705ce5bSJoe Perches		return 1;
2077de7d4f0eSAndy Whitcroft	}
20783705ce5bSJoe Perches	return 0;
2079773647a0SAndy Whitcroft}
2080de7d4f0eSAndy Whitcroftsub CHK {
2081cbec18afSJoe Perches	my ($type, $msg) = @_;
2082cbec18afSJoe Perches
2083cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2084de7d4f0eSAndy Whitcroft		our $clean = 0;
20856c72ffaaSAndy Whitcroft		our $cnt_chk++;
20863705ce5bSJoe Perches		return 1;
20876c72ffaaSAndy Whitcroft	}
20883705ce5bSJoe Perches	return 0;
2089de7d4f0eSAndy Whitcroft}
2090de7d4f0eSAndy Whitcroft
20916ecd9674SAndy Whitcroftsub check_absolute_file {
20926ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
20936ecd9674SAndy Whitcroft	my $file = $absolute;
20946ecd9674SAndy Whitcroft
20956ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
20966ecd9674SAndy Whitcroft
20976ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
20986ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
20996ecd9674SAndy Whitcroft		if (-f "$root/$file") {
21006ecd9674SAndy Whitcroft			##print "file<$file>\n";
21016ecd9674SAndy Whitcroft			last;
21026ecd9674SAndy Whitcroft		}
21036ecd9674SAndy Whitcroft	}
21046ecd9674SAndy Whitcroft	if (! -f _)  {
21056ecd9674SAndy Whitcroft		return 0;
21066ecd9674SAndy Whitcroft	}
21076ecd9674SAndy Whitcroft
21086ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
21096ecd9674SAndy Whitcroft	my $prefix = $absolute;
21106ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
21116ecd9674SAndy Whitcroft
21126ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
21136ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2114000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2115000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
21166ecd9674SAndy Whitcroft	}
21176ecd9674SAndy Whitcroft}
21186ecd9674SAndy Whitcroft
21193705ce5bSJoe Perchessub trim {
21203705ce5bSJoe Perches	my ($string) = @_;
21213705ce5bSJoe Perches
2122b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2123b34c648bSJoe Perches
2124b34c648bSJoe Perches	return $string;
2125b34c648bSJoe Perches}
2126b34c648bSJoe Perches
2127b34c648bSJoe Perchessub ltrim {
2128b34c648bSJoe Perches	my ($string) = @_;
2129b34c648bSJoe Perches
2130b34c648bSJoe Perches	$string =~ s/^\s+//;
2131b34c648bSJoe Perches
2132b34c648bSJoe Perches	return $string;
2133b34c648bSJoe Perches}
2134b34c648bSJoe Perches
2135b34c648bSJoe Perchessub rtrim {
2136b34c648bSJoe Perches	my ($string) = @_;
2137b34c648bSJoe Perches
2138b34c648bSJoe Perches	$string =~ s/\s+$//;
21393705ce5bSJoe Perches
21403705ce5bSJoe Perches	return $string;
21413705ce5bSJoe Perches}
21423705ce5bSJoe Perches
214352ea8506SJoe Perchessub string_find_replace {
214452ea8506SJoe Perches	my ($string, $find, $replace) = @_;
214552ea8506SJoe Perches
214652ea8506SJoe Perches	$string =~ s/$find/$replace/g;
214752ea8506SJoe Perches
214852ea8506SJoe Perches	return $string;
214952ea8506SJoe Perches}
215052ea8506SJoe Perches
21513705ce5bSJoe Perchessub tabify {
21523705ce5bSJoe Perches	my ($leading) = @_;
21533705ce5bSJoe Perches
21543705ce5bSJoe Perches	my $source_indent = 8;
21553705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
21563705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
21573705ce5bSJoe Perches
21583705ce5bSJoe Perches	#convert leading spaces to tabs
21593705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
21603705ce5bSJoe Perches	#Remove spaces before a tab
21613705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
21623705ce5bSJoe Perches
21633705ce5bSJoe Perches	return "$leading";
21643705ce5bSJoe Perches}
21653705ce5bSJoe Perches
2166d1fe9c09SJoe Perchessub pos_last_openparen {
2167d1fe9c09SJoe Perches	my ($line) = @_;
2168d1fe9c09SJoe Perches
2169d1fe9c09SJoe Perches	my $pos = 0;
2170d1fe9c09SJoe Perches
2171d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2172d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2173d1fe9c09SJoe Perches
2174d1fe9c09SJoe Perches	my $last_openparen = 0;
2175d1fe9c09SJoe Perches
2176d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2177d1fe9c09SJoe Perches		return -1;
2178d1fe9c09SJoe Perches	}
2179d1fe9c09SJoe Perches
2180d1fe9c09SJoe Perches	my $len = length($line);
2181d1fe9c09SJoe Perches
2182d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2183d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2184d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2185d1fe9c09SJoe Perches			$pos += length($1) - 1;
2186d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2187d1fe9c09SJoe Perches			$last_openparen = $pos;
2188d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2189d1fe9c09SJoe Perches			last;
2190d1fe9c09SJoe Perches		}
2191d1fe9c09SJoe Perches	}
2192d1fe9c09SJoe Perches
219391cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2194d1fe9c09SJoe Perches}
2195d1fe9c09SJoe Perches
21960a920b5bSAndy Whitcroftsub process {
21970a920b5bSAndy Whitcroft	my $filename = shift;
21980a920b5bSAndy Whitcroft
21990a920b5bSAndy Whitcroft	my $linenr=0;
22000a920b5bSAndy Whitcroft	my $prevline="";
2201c2fdda0dSAndy Whitcroft	my $prevrawline="";
22020a920b5bSAndy Whitcroft	my $stashline="";
2203c2fdda0dSAndy Whitcroft	my $stashrawline="";
22040a920b5bSAndy Whitcroft
22054a0df2efSAndy Whitcroft	my $length;
22060a920b5bSAndy Whitcroft	my $indent;
22070a920b5bSAndy Whitcroft	my $previndent=0;
22080a920b5bSAndy Whitcroft	my $stashindent=0;
22090a920b5bSAndy Whitcroft
2210de7d4f0eSAndy Whitcroft	our $clean = 1;
22110a920b5bSAndy Whitcroft	my $signoff = 0;
22120a920b5bSAndy Whitcroft	my $is_patch = 0;
221329ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
221415662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2215ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2216bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
22172a076f40SJoe Perches	my $commit_log_long_line = 0;
2218e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
221913f1937eSJoe Perches	my $reported_maintainer_file = 0;
2220fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2221fa64205dSPasi Savanainen
2222365dd4eaSJoe Perches	my $last_blank_line = 0;
22235e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2224365dd4eaSJoe Perches
222513214adfSAndy Whitcroft	our @report = ();
22266c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
22276c72ffaaSAndy Whitcroft	our $cnt_error = 0;
22286c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
22296c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
22306c72ffaaSAndy Whitcroft
22310a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
22320a920b5bSAndy Whitcroft	my $realfile = '';
22330a920b5bSAndy Whitcroft	my $realline = 0;
22340a920b5bSAndy Whitcroft	my $realcnt = 0;
22350a920b5bSAndy Whitcroft	my $here = '';
223677cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
22370a920b5bSAndy Whitcroft	my $in_comment = 0;
2238c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
22390a920b5bSAndy Whitcroft	my $first_line = 0;
22401e855726SWolfram Sang	my $p1_prefix = '';
22410a920b5bSAndy Whitcroft
224213214adfSAndy Whitcroft	my $prev_values = 'E';
224313214adfSAndy Whitcroft
224413214adfSAndy Whitcroft	# suppression flags
2245773647a0SAndy Whitcroft	my %suppress_ifbraces;
2246170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
22472b474a1aSAndy Whitcroft	my %suppress_export;
22483e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2249653d4876SAndy Whitcroft
22507e51f197SJoe Perches	my %signatures = ();
2251323c1260SJoe Perches
2252c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2253de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2254c2fdda0dSAndy Whitcroft	#
2255de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2256de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2257773647a0SAndy Whitcroft
2258d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2259d8b07710SJoe Perches
2260773647a0SAndy Whitcroft	sanitise_line_reset();
2261c2fdda0dSAndy Whitcroft	my $line;
2262c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2263773647a0SAndy Whitcroft		$linenr++;
2264773647a0SAndy Whitcroft		$line = $rawline;
2265c2fdda0dSAndy Whitcroft
22663705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
22673705ce5bSJoe Perches
2268773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2269de7d4f0eSAndy Whitcroft			$setup_docs = 0;
22708c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2271de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2272de7d4f0eSAndy Whitcroft			}
2273773647a0SAndy Whitcroft			#next;
2274de7d4f0eSAndy Whitcroft		}
227574fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2276773647a0SAndy Whitcroft			$realline=$1-1;
2277773647a0SAndy Whitcroft			if (defined $2) {
2278773647a0SAndy Whitcroft				$realcnt=$3+1;
2279773647a0SAndy Whitcroft			} else {
2280773647a0SAndy Whitcroft				$realcnt=1+1;
2281773647a0SAndy Whitcroft			}
2282c45dcabdSAndy Whitcroft			$in_comment = 0;
2283773647a0SAndy Whitcroft
2284773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2285773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2286773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2287773647a0SAndy Whitcroft			# at context start.
2288773647a0SAndy Whitcroft			my $edge;
228901fa9147SAndy Whitcroft			my $cnt = $realcnt;
229001fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
229101fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
229201fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
229301fa9147SAndy Whitcroft				$cnt--;
229401fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2295721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2296fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2297fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2298fae17daeSAndy Whitcroft					($edge) = $1;
2299fae17daeSAndy Whitcroft					last;
2300fae17daeSAndy Whitcroft				}
2301773647a0SAndy Whitcroft			}
2302773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2303773647a0SAndy Whitcroft				$in_comment = 1;
2304773647a0SAndy Whitcroft			}
2305773647a0SAndy Whitcroft
2306773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2307773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2308773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2309773647a0SAndy Whitcroft			if (!defined $edge &&
231083242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2311773647a0SAndy Whitcroft			{
2312773647a0SAndy Whitcroft				$in_comment = 1;
2313773647a0SAndy Whitcroft			}
2314773647a0SAndy Whitcroft
2315773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2316773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2317773647a0SAndy Whitcroft
2318171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2319773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2320171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2321773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2322773647a0SAndy Whitcroft		}
2323773647a0SAndy Whitcroft		push(@lines, $line);
2324773647a0SAndy Whitcroft
2325773647a0SAndy Whitcroft		if ($realcnt > 1) {
2326773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2327773647a0SAndy Whitcroft		} else {
2328773647a0SAndy Whitcroft			$realcnt = 0;
2329773647a0SAndy Whitcroft		}
2330773647a0SAndy Whitcroft
2331773647a0SAndy Whitcroft		#print "==>$rawline\n";
2332773647a0SAndy Whitcroft		#print "-->$line\n";
2333de7d4f0eSAndy Whitcroft
2334de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2335de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2336de7d4f0eSAndy Whitcroft		}
2337de7d4f0eSAndy Whitcroft	}
2338de7d4f0eSAndy Whitcroft
23396c72ffaaSAndy Whitcroft	$prefix = '';
23406c72ffaaSAndy Whitcroft
2341773647a0SAndy Whitcroft	$realcnt = 0;
2342773647a0SAndy Whitcroft	$linenr = 0;
2343194f66fcSJoe Perches	$fixlinenr = -1;
23440a920b5bSAndy Whitcroft	foreach my $line (@lines) {
23450a920b5bSAndy Whitcroft		$linenr++;
2346194f66fcSJoe Perches		$fixlinenr++;
23471b5539b1SJoe Perches		my $sline = $line;	#copy of $line
23481b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
23490a920b5bSAndy Whitcroft
2350c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
23516c72ffaaSAndy Whitcroft
23520a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2353e518e9a5SJoe Perches		if (!$in_commit_log &&
235474fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
235574fd4f34SJoe Perches			my $context = $4;
23560a920b5bSAndy Whitcroft			$is_patch = 1;
23574a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
23580a920b5bSAndy Whitcroft			$realline=$1-1;
23590a920b5bSAndy Whitcroft			if (defined $2) {
23600a920b5bSAndy Whitcroft				$realcnt=$3+1;
23610a920b5bSAndy Whitcroft			} else {
23620a920b5bSAndy Whitcroft				$realcnt=1+1;
23630a920b5bSAndy Whitcroft			}
2364c2fdda0dSAndy Whitcroft			annotate_reset();
236513214adfSAndy Whitcroft			$prev_values = 'E';
236613214adfSAndy Whitcroft
2367773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2368170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
23692b474a1aSAndy Whitcroft			%suppress_export = ();
23703e469cdcSAndy Whitcroft			$suppress_statement = 0;
237174fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
237274fd4f34SJoe Perches				$context_function = $1;
237374fd4f34SJoe Perches			} else {
237474fd4f34SJoe Perches				undef $context_function;
237574fd4f34SJoe Perches			}
23760a920b5bSAndy Whitcroft			next;
23770a920b5bSAndy Whitcroft
23784a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
23794a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
23804a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2381773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
23820a920b5bSAndy Whitcroft			$realline++;
2383d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
23840a920b5bSAndy Whitcroft
23854a0df2efSAndy Whitcroft			# Measure the line length and indent.
2386c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
23870a920b5bSAndy Whitcroft
23880a920b5bSAndy Whitcroft			# Track the previous line.
23890a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
23900a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2391c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2392c2fdda0dSAndy Whitcroft
2393773647a0SAndy Whitcroft			#warn "line<$line>\n";
23946c72ffaaSAndy Whitcroft
2395d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2396d8aaf121SAndy Whitcroft			$realcnt--;
23970a920b5bSAndy Whitcroft		}
23980a920b5bSAndy Whitcroft
2399cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2400cc77cdcaSAndy Whitcroft
24016c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
24026c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2403773647a0SAndy Whitcroft
24042ac73b4fSJoe Perches		my $found_file = 0;
2405773647a0SAndy Whitcroft		# extract the filename as it passes
24063bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
24073bf9a009SRabin Vincent			$realfile = $1;
24082b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2409270c49a0SJoe Perches			$in_commit_log = 0;
24102ac73b4fSJoe Perches			$found_file = 1;
24113bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2412773647a0SAndy Whitcroft			$realfile = $1;
24132b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2414270c49a0SJoe Perches			$in_commit_log = 0;
24151e855726SWolfram Sang
24161e855726SWolfram Sang			$p1_prefix = $1;
2417e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2418e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2419000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2420000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
24211e855726SWolfram Sang			}
2422773647a0SAndy Whitcroft
2423c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2424000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2425000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2426773647a0SAndy Whitcroft			}
24272ac73b4fSJoe Perches			$found_file = 1;
24282ac73b4fSJoe Perches		}
24292ac73b4fSJoe Perches
243034d8815fSJoe Perches#make up the handle for any error we report on this line
243134d8815fSJoe Perches		if ($showfile) {
243234d8815fSJoe Perches			$prefix = "$realfile:$realline: "
243334d8815fSJoe Perches		} elsif ($emacs) {
24347d3a9f67SJoe Perches			if ($file) {
24357d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
24367d3a9f67SJoe Perches			} else {
243734d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
243834d8815fSJoe Perches			}
24397d3a9f67SJoe Perches		}
244034d8815fSJoe Perches
24412ac73b4fSJoe Perches		if ($found_file) {
244285b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
244385b0ee18SJoe Perches				WARN("OBSOLETE",
244485b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
244585b0ee18SJoe Perches			}
24467bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
24472ac73b4fSJoe Perches				$check = 1;
24482ac73b4fSJoe Perches			} else {
24492ac73b4fSJoe Perches				$check = $check_orig;
24502ac73b4fSJoe Perches			}
2451773647a0SAndy Whitcroft			next;
2452773647a0SAndy Whitcroft		}
2453773647a0SAndy Whitcroft
2454389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
24550a920b5bSAndy Whitcroft
2456c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2457c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2458c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
24590a920b5bSAndy Whitcroft
24606c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
24616c72ffaaSAndy Whitcroft
2462e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2463e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2464e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2465e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2466e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2467e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2468e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2469e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2470e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2471e518e9a5SJoe Perches		}
2472e518e9a5SJoe Perches
24733bf9a009SRabin Vincent# Check for incorrect file permissions
24743bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
24753bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
247604db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
247704db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2478000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2479000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
24803bf9a009SRabin Vincent			}
24813bf9a009SRabin Vincent		}
24823bf9a009SRabin Vincent
248320112475SJoe Perches# Check the patch for a signoff:
2484d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
24854a0df2efSAndy Whitcroft			$signoff++;
248615662b3eSJoe Perches			$in_commit_log = 0;
24870a920b5bSAndy Whitcroft		}
248820112475SJoe Perches
2489e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2490e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2491e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2492e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2493e0d975b1SJoe Perches		}
2494e0d975b1SJoe Perches
249520112475SJoe Perches# Check signature styles
2496270c49a0SJoe Perches		if (!$in_header_lines &&
2497ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
249820112475SJoe Perches			my $space_before = $1;
249920112475SJoe Perches			my $sign_off = $2;
250020112475SJoe Perches			my $space_after = $3;
250120112475SJoe Perches			my $email = $4;
250220112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
250320112475SJoe Perches
2504ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2505ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2506ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2507ce0338dfSJoe Perches			}
250820112475SJoe Perches			if (defined $space_before && $space_before ne "") {
25093705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
25103705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
25113705ce5bSJoe Perches				    $fix) {
2512194f66fcSJoe Perches					$fixed[$fixlinenr] =
25133705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
25143705ce5bSJoe Perches				}
251520112475SJoe Perches			}
251620112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
25173705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
25183705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
25193705ce5bSJoe Perches				    $fix) {
2520194f66fcSJoe Perches					$fixed[$fixlinenr] =
25213705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
25223705ce5bSJoe Perches				}
25233705ce5bSJoe Perches
252420112475SJoe Perches			}
252520112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
25263705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
25273705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
25283705ce5bSJoe Perches				    $fix) {
2529194f66fcSJoe Perches					$fixed[$fixlinenr] =
25303705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
25313705ce5bSJoe Perches				}
253220112475SJoe Perches			}
253320112475SJoe Perches
253420112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
253520112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
253620112475SJoe Perches			if ($suggested_email eq "") {
2537000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2538000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
253920112475SJoe Perches			} else {
254020112475SJoe Perches				my $dequoted = $suggested_email;
254120112475SJoe Perches				$dequoted =~ s/^"//;
254220112475SJoe Perches				$dequoted =~ s/" </ </;
254320112475SJoe Perches				# Don't force email to have quotes
254420112475SJoe Perches				# Allow just an angle bracketed address
254520112475SJoe Perches				if ("$dequoted$comment" ne $email &&
254620112475SJoe Perches				    "<$email_address>$comment" ne $email &&
254720112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2548000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2549000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
255020112475SJoe Perches				}
25510a920b5bSAndy Whitcroft			}
25527e51f197SJoe Perches
25537e51f197SJoe Perches# Check for duplicate signatures
25547e51f197SJoe Perches			my $sig_nospace = $line;
25557e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
25567e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
25577e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
25587e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
25597e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
25607e51f197SJoe Perches			} else {
25617e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
25627e51f197SJoe Perches			}
25630a920b5bSAndy Whitcroft		}
25640a920b5bSAndy Whitcroft
2565a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2566a2fe16b9SJoe Perches		if ($in_header_lines &&
2567a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2568a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2569a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2570a2fe16b9SJoe Perches		}
2571a2fe16b9SJoe Perches
25729b3189ebSJoe Perches# Check for old stable address
25739b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
25749b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
25759b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
25769b3189ebSJoe Perches		}
25779b3189ebSJoe Perches
25787ebd05efSChristopher Covington# Check for unwanted Gerrit info
25797ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
25807ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
25817ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
25827ebd05efSChristopher Covington		}
25837ebd05efSChristopher Covington
2584369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2585369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2586369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2587369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2588369c8dd3SJoe Perches					# timestamp
2589369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2590369c8dd3SJoe Perches					# stack dump address
2591369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2592369c8dd3SJoe Perches		}
2593369c8dd3SJoe Perches
25942a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
25952a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2596bf4daf12SJoe Perches		    length($line) > 75 &&
2597bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2598bf4daf12SJoe Perches					# file delta changes
2599bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2600bf4daf12SJoe Perches					# filename then :
2601bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2602bf4daf12SJoe Perches					# A Fixes: or Link: line
2603bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
26042a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
26052a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
26062a076f40SJoe Perches			$commit_log_long_line = 1;
26072a076f40SJoe Perches		}
26082a076f40SJoe Perches
2609bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2610bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2611bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2612bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2613bf4daf12SJoe Perches		}
2614bf4daf12SJoe Perches
26150d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2616369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2617aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2618e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2619fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2620aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2621369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2622bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2623fe043ea1SJoe Perches			my $init_char = "c";
2624fe043ea1SJoe Perches			my $orig_commit = "";
26250d7835fcSJoe Perches			my $short = 1;
26260d7835fcSJoe Perches			my $long = 0;
26270d7835fcSJoe Perches			my $case = 1;
26280d7835fcSJoe Perches			my $space = 1;
26290d7835fcSJoe Perches			my $hasdesc = 0;
263019c146a6SJoe Perches			my $hasparens = 0;
26310d7835fcSJoe Perches			my $id = '0123456789ab';
26320d7835fcSJoe Perches			my $orig_desc = "commit description";
26330d7835fcSJoe Perches			my $description = "";
26340d7835fcSJoe Perches
2635fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2636fe043ea1SJoe Perches				$init_char = $1;
2637fe043ea1SJoe Perches				$orig_commit = lc($2);
2638fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2639fe043ea1SJoe Perches				$orig_commit = lc($1);
2640fe043ea1SJoe Perches			}
2641fe043ea1SJoe Perches
26420d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
26430d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
26440d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
26450d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
26460d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
26470d7835fcSJoe Perches				$orig_desc = $1;
264819c146a6SJoe Perches				$hasparens = 1;
26490d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
26500d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
26510d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
26520d7835fcSJoe Perches				$orig_desc = $1;
265319c146a6SJoe Perches				$hasparens = 1;
2654b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2655b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2656b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2657b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2658b671fde0SJoe Perches				$orig_desc = $1;
2659b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2660b671fde0SJoe Perches				$orig_desc .= " " . $1;
266119c146a6SJoe Perches				$hasparens = 1;
26620d7835fcSJoe Perches			}
26630d7835fcSJoe Perches
26640d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
26650d7835fcSJoe Perches							      $id, $orig_desc);
26660d7835fcSJoe Perches
2667948b133aSHeinrich Schuchardt			if (defined($id) &&
2668948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2669d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
26700d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
26710d7835fcSJoe Perches			}
2672d311cd44SJoe Perches		}
2673d311cd44SJoe Perches
267413f1937eSJoe Perches# Check for added, moved or deleted files
267513f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
267613f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
267713f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
267813f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
267913f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2680a82603a8SAndrew Jeffery			$is_patch = 1;
268113f1937eSJoe Perches			$reported_maintainer_file = 1;
268213f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
268313f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
268413f1937eSJoe Perches		}
268513f1937eSJoe Perches
268600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
26878905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2688000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2689000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
26906c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2691de7d4f0eSAndy Whitcroft		}
2692de7d4f0eSAndy Whitcroft
2693de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2694de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2695171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2696171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2697171ae1a4SAndy Whitcroft
2698171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2699171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2700171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2701171ae1a4SAndy Whitcroft
270234d99219SJoe Perches			CHK("INVALID_UTF8",
2703000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
270400df344fSAndy Whitcroft		}
27050a920b5bSAndy Whitcroft
270615662b3eSJoe Perches# Check if it's the start of a commit log
270715662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
270815662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2709eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2710eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
271115662b3eSJoe Perches			$in_header_lines = 0;
271215662b3eSJoe Perches			$in_commit_log = 1;
2713ed43c4e5SAllen Hubbe			$has_commit_log = 1;
271415662b3eSJoe Perches		}
271515662b3eSJoe Perches
2716fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2717fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2718fa64205dSPasi Savanainen		if ($in_header_lines &&
2719fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2720fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2721fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2722fa64205dSPasi Savanainen		}
2723fa64205dSPasi Savanainen
2724fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
272515662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2726fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
272715662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
272815662b3eSJoe Perches		}
272915662b3eSJoe Perches
2730d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2731d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2732d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2733d6430f71SJoe Perches				my $file = $1;
2734d6430f71SJoe Perches
2735d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2736d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2737d6430f71SJoe Perches					#
2738d6430f71SJoe Perches				} else {
2739d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2740d6430f71SJoe Perches				}
2741d6430f71SJoe Perches			}
2742d6430f71SJoe Perches		}
2743d6430f71SJoe Perches
274466b47b4aSKees Cook# Check for various typo / spelling mistakes
274566d7a382SJoe Perches		if (defined($misspellings) &&
274666d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2747ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
274866b47b4aSKees Cook				my $typo = $1;
274966b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
275066b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
275166b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
27520675a8fbSJean Delvare				my $msg_level = \&WARN;
27530675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
27540675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
275566b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
275666b47b4aSKees Cook				    $fix) {
275766b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
275866b47b4aSKees Cook				}
275966b47b4aSKees Cook			}
276066b47b4aSKees Cook		}
276166b47b4aSKees Cook
276230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
276330670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
276400df344fSAndy Whitcroft
27650a920b5bSAndy Whitcroft#trailing whitespace
27669c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2767c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2768d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2769d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2770d5e616fcSJoe Perches			    $fix) {
2771194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2772d5e616fcSJoe Perches			}
2773c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2774c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27753705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
27763705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
27773705ce5bSJoe Perches			    $fix) {
2778194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
27793705ce5bSJoe Perches			}
27803705ce5bSJoe Perches
2781d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
27820a920b5bSAndy Whitcroft		}
27835368df20SAndy Whitcroft
27844783f894SJosh Triplett# Check for FSF mailing addresses.
2785109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
27861bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
27873e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
27883e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
27894783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27900675a8fbSJean Delvare			my $msg_level = \&ERROR;
27910675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
27920675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
27934783f894SJosh 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)
27944783f894SJosh Triplett		}
27954783f894SJosh Triplett
27963354957aSAndi Kleen# check for Kconfig help text having a real description
27979fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
27989fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
27993354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
28008d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
28013354957aSAndi Kleen			my $length = 0;
28029fe287d7SAndy Whitcroft			my $cnt = $realcnt;
28039fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
28049fe287d7SAndy Whitcroft			my $f;
2805a1385803SAndy Whitcroft			my $is_start = 0;
28069fe287d7SAndy Whitcroft			my $is_end = 0;
2807a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
28089fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
28099fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
28109fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
28119fe287d7SAndy Whitcroft
28129fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
28138d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2814a1385803SAndy Whitcroft
28158d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2816a1385803SAndy Whitcroft					$is_start = 1;
28178d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2818a1385803SAndy Whitcroft					$length = -1;
2819a1385803SAndy Whitcroft				}
2820a1385803SAndy Whitcroft
28219fe287d7SAndy Whitcroft				$f =~ s/^.//;
28223354957aSAndi Kleen				$f =~ s/#.*//;
28233354957aSAndi Kleen				$f =~ s/^\s+//;
28243354957aSAndi Kleen				next if ($f =~ /^$/);
28259fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
28269fe287d7SAndy Whitcroft					$is_end = 1;
28279fe287d7SAndy Whitcroft					last;
28289fe287d7SAndy Whitcroft				}
28293354957aSAndi Kleen				$length++;
28303354957aSAndi Kleen			}
283156193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2832000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
283356193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
283456193274SVadim Bendebury			}
2835a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
28363354957aSAndi Kleen		}
28373354957aSAndi Kleen
2838628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
2839628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
2840628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
2841628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
2842628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
2843628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2844628f91a2SJoe Perches			    $fix) {
2845628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2846628f91a2SJoe Perches			}
2847628f91a2SJoe Perches		}
2848628f91a2SJoe Perches
2849327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2850327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2851327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2852327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2853327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2854327953e9SChristoph Jaeger		}
2855327953e9SChristoph Jaeger
2856c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2857c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2858c68e5878SArnaud Lacombe			my $flag = $1;
2859c68e5878SArnaud Lacombe			my $replacement = {
2860c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2861c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2862c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2863c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2864c68e5878SArnaud Lacombe			};
2865c68e5878SArnaud Lacombe
2866c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2867c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2868c68e5878SArnaud Lacombe		}
2869c68e5878SArnaud Lacombe
2870bff5da43SRob Herring# check for DT compatible documentation
28717dd05b38SFlorian Vaussard		if (defined $root &&
28727dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
28737dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
28747dd05b38SFlorian Vaussard
2875bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2876bff5da43SRob Herring
2877cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2878cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2879cc93319bSFlorian Vaussard
2880bff5da43SRob Herring			foreach my $compat (@compats) {
2881bff5da43SRob Herring				my $compat2 = $compat;
2882185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2883185d566bSRob Herring				my $compat3 = $compat;
2884185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2885185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2886bff5da43SRob Herring				if ( $? >> 8 ) {
2887bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2888bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2889bff5da43SRob Herring				}
2890bff5da43SRob Herring
28914fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
28924fbf32a6SFlorian Vaussard				my $vendor = $1;
2893cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2894bff5da43SRob Herring				if ( $? >> 8 ) {
2895bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2896cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2897bff5da43SRob Herring				}
2898bff5da43SRob Herring			}
2899bff5da43SRob Herring		}
2900bff5da43SRob Herring
29015368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2902d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
29035368df20SAndy Whitcroft
290447e0c88bSJoe Perches# line length limit (with some exclusions)
290547e0c88bSJoe Perches#
290647e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
290747e0c88bSJoe Perches#	logging functions like pr_info that end in a string
290847e0c88bSJoe Perches#	lines with a single string
290947e0c88bSJoe Perches#	#defines that are a single string
29102e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
291147e0c88bSJoe Perches#
291247e0c88bSJoe Perches# There are 3 different line length message types:
2913ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
291447e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
291547e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
291647e0c88bSJoe Perches#
291747e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
291847e0c88bSJoe Perches#
291947e0c88bSJoe Perches
2920b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
292147e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
292247e0c88bSJoe Perches
292347e0c88bSJoe Perches			# Check the allowed long line types first
292447e0c88bSJoe Perches
292547e0c88bSJoe Perches			# logging functions that end in a string that starts
292647e0c88bSJoe Perches			# before $max_line_length
292747e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
292847e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
292947e0c88bSJoe Perches				$msg_type = "";
293047e0c88bSJoe Perches
293147e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
293247e0c88bSJoe Perches			# #defines with only strings
293347e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
293447e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
293547e0c88bSJoe Perches				$msg_type = "";
293647e0c88bSJoe Perches
2937cc147506SJoe Perches			# More special cases
2938cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
2939cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
2940d560a5f8SJoe Perches				$msg_type = "";
2941d560a5f8SJoe Perches
29422e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
29432e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
29442e4bbbc5SAndreas Brauchli				$msg_type = "";
29452e4bbbc5SAndreas Brauchli
294647e0c88bSJoe Perches			# Otherwise set the alternate message types
294747e0c88bSJoe Perches
294847e0c88bSJoe Perches			# a comment starts before $max_line_length
294947e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
295047e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
295147e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
295247e0c88bSJoe Perches
295347e0c88bSJoe Perches			# a quoted string starts before $max_line_length
295447e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
295547e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
295647e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
295747e0c88bSJoe Perches			}
295847e0c88bSJoe Perches
295947e0c88bSJoe Perches			if ($msg_type ne "" &&
296047e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
296147e0c88bSJoe Perches				WARN($msg_type,
29626cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
29630a920b5bSAndy Whitcroft			}
296447e0c88bSJoe Perches		}
29650a920b5bSAndy Whitcroft
29668905a67cSAndy Whitcroft# check for adding lines without a newline.
29678905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2968000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2969000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
29708905a67cSAndy Whitcroft		}
29718905a67cSAndy Whitcroft
297242e41c54SMike Frysinger# Blackfin: use hi/lo macros
297342e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
297442e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
297542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2976000d1cc1SJoe Perches				ERROR("LO_MACRO",
2977000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
297842e41c54SMike Frysinger			}
297942e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
298042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2981000d1cc1SJoe Perches				ERROR("HI_MACRO",
2982000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
298342e41c54SMike Frysinger			}
298442e41c54SMike Frysinger		}
298542e41c54SMike Frysinger
2986b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2987de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
29880a920b5bSAndy Whitcroft
29890a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
29900a920b5bSAndy Whitcroft# more than 8 must use tabs.
2991c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2992c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2993c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2994d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29953705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
29963705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
29973705ce5bSJoe Perches			    $fix) {
2998194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
29993705ce5bSJoe Perches			}
30000a920b5bSAndy Whitcroft		}
30010a920b5bSAndy Whitcroft
300208e44365SAlberto Panizzo# check for space before tabs.
300308e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
300408e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30053705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
30063705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
30073705ce5bSJoe Perches			    $fix) {
3008194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3009d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
3010194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3011c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
30123705ce5bSJoe Perches			}
301308e44365SAlberto Panizzo		}
301408e44365SAlberto Panizzo
3015d1fe9c09SJoe Perches# check for && or || at the start of a line
3016d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3017d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3018d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3019d1fe9c09SJoe Perches		}
3020d1fe9c09SJoe Perches
3021a91e8994SJoe Perches# check indentation starts on a tab stop
3022a91e8994SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3023*a134f8deSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=\[])/) {
3024a91e8994SJoe Perches			my $indent = length($1);
3025a91e8994SJoe Perches			if ($indent % 8) {
3026a91e8994SJoe Perches				if (WARN("TABSTOP",
3027a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3028a91e8994SJoe Perches				    $fix) {
3029a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3030a91e8994SJoe Perches				}
3031a91e8994SJoe Perches			}
3032a91e8994SJoe Perches		}
3033a91e8994SJoe Perches
3034d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
3035d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
3036fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3037d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3038d1fe9c09SJoe Perches			my $oldindent = $1;
3039d1fe9c09SJoe Perches			my $rest = $2;
3040d1fe9c09SJoe Perches
3041d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3042d1fe9c09SJoe Perches			if ($pos >= 0) {
3043b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3044b34a26f3SJoe Perches				my $newindent = $2;
3045d1fe9c09SJoe Perches
3046d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3047d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3048d1fe9c09SJoe Perches					" "  x ($pos % 8);
3049d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3050d1fe9c09SJoe Perches
3051d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3052d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
30533705ce5bSJoe Perches
30543705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
30553705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
30563705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3057194f66fcSJoe Perches						$fixed[$fixlinenr] =~
30583705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
30593705ce5bSJoe Perches					}
3060d1fe9c09SJoe Perches				}
3061d1fe9c09SJoe Perches			}
3062d1fe9c09SJoe Perches		}
3063d1fe9c09SJoe Perches
30646ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
30656ab3a970SJoe Perches# avoid checking a few false positives:
30666ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
30676ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
30686ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
30696ab3a970SJoe Perches#   multiline macros that define functions
30706ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
30716ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
30726ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
30733705ce5bSJoe Perches			if (CHK("SPACING",
3074f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
30753705ce5bSJoe Perches			    $fix) {
3076194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3077f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
30783705ce5bSJoe Perches			}
3079aad4f614SJoe Perches		}
3080aad4f614SJoe Perches
308186406b1cSJoe Perches# Block comment styles
308286406b1cSJoe Perches# Networking with an initial /*
308305880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3084fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
308585ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
308685ad978cSJoe Perches		    $realline > 2) {
308705880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
308805880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
308905880600SJoe Perches		}
309005880600SJoe Perches
309186406b1cSJoe Perches# Block comments use * on subsequent lines
309286406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
309386406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3094a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
309561135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3096a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
309786406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
309886406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3099a605e32eSJoe Perches		}
3100a605e32eSJoe Perches
310186406b1cSJoe Perches# Block comments use */ on trailing lines
310286406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3103c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3104c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3105c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
310686406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
310786406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
310805880600SJoe Perches		}
310905880600SJoe Perches
311008eb9b80SJoe Perches# Block comment * alignment
311108eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3112af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3113af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3114af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
311508eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3116af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3117af207524SJoe Perches			my $oldindent;
311808eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3119af207524SJoe Perches			if (defined($1)) {
3120af207524SJoe Perches				$oldindent = expand_tabs($1);
3121af207524SJoe Perches			} else {
3122af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3123af207524SJoe Perches				$oldindent = expand_tabs($1);
3124af207524SJoe Perches			}
312508eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
312608eb9b80SJoe Perches			my $newindent = $1;
312708eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3128af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
312908eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
313008eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
313108eb9b80SJoe Perches			}
313208eb9b80SJoe Perches		}
313308eb9b80SJoe Perches
31347f619191SJoe Perches# check for missing blank lines after struct/union declarations
31357f619191SJoe Perches# with exceptions for various attributes and macros
31367f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
31377f619191SJoe Perches		    $line =~ /^\+/ &&
31387f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
31397f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
31407f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
31417f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
31427f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
31437f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
31447f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
31450bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
31467f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3147d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3148d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3149d752fcc8SJoe Perches			    $fix) {
3150f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3151d752fcc8SJoe Perches			}
31527f619191SJoe Perches		}
31537f619191SJoe Perches
3154365dd4eaSJoe Perches# check for multiple consecutive blank lines
3155365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3156365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3157365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3158d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3159d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3160d752fcc8SJoe Perches			    $fix) {
3161f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3162d752fcc8SJoe Perches			}
3163d752fcc8SJoe Perches
3164365dd4eaSJoe Perches			$last_blank_line = $linenr;
3165365dd4eaSJoe Perches		}
3166365dd4eaSJoe Perches
31673b617e3bSJoe Perches# check for missing blank lines after declarations
31683f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
31693f7bac03SJoe Perches			# actual declarations
31703f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
31715a4e1fd3SJoe Perches			# function pointer declarations
31725a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
31733f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
31743f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
31753f7bac03SJoe Perches			# known declaration macros
31763f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
31773f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
31783f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
31793f7bac03SJoe Perches			# other possible extensions of declaration lines
31803f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
31813f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
31823f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
31833f7bac03SJoe Perches			# looks like a declaration
31843f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
31855a4e1fd3SJoe Perches			# function pointer declarations
31865a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
31873f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
31883f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
31893f7bac03SJoe Perches			# known declaration macros
31903f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
31913f7bac03SJoe Perches			# start of struct or union or enum
31923b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
31933f7bac03SJoe Perches			# start or end of block or continuation of declaration
31943f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
31953f7bac03SJoe Perches			# bitfield continuation
31963f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
31973f7bac03SJoe Perches			# other possible extensions of declaration lines
31983f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
31993f7bac03SJoe Perches			# indentation of previous and current line are the same
32003f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3201d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3202d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3203d752fcc8SJoe Perches			    $fix) {
3204f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3205d752fcc8SJoe Perches			}
32063b617e3bSJoe Perches		}
32073b617e3bSJoe Perches
32085f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
32096b4c5bebSAndy Whitcroft# Exceptions:
32106b4c5bebSAndy Whitcroft#  1) within comments
32116b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
32126b4c5bebSAndy Whitcroft#  3) hanging labels
32133705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
32145f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32153705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
32163705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
32173705ce5bSJoe Perches			    $fix) {
3218194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
32193705ce5bSJoe Perches			}
32205f7ddae6SRaffaele Recalcati		}
32215f7ddae6SRaffaele Recalcati
3222b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3223b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3224b9ea10d6SAndy Whitcroft
32255751a24eSJoe Perches# check for unusual line ending [ or (
32265751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
32275751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
32285751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
32295751a24eSJoe Perches		}
32305751a24eSJoe Perches
32314dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
32324dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
32334dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
32344dbed76fSJoe Perches			$context_function = $1;
32354dbed76fSJoe Perches		}
32364dbed76fSJoe Perches
32374dbed76fSJoe Perches# check if this appears to be the end of function declaration
32384dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
32394dbed76fSJoe Perches			undef $context_function;
32404dbed76fSJoe Perches		}
32414dbed76fSJoe Perches
3242032a4c0fSJoe Perches# check indentation of any line with a bare else
3243840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3244032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3245032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3246032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3247840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3248840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3249840080a0SJoe Perches			     defined $lines[$linenr] &&
3250840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3251032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3252032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3253032a4c0fSJoe Perches			}
3254032a4c0fSJoe Perches		}
3255032a4c0fSJoe Perches
3256c00df19aSJoe Perches# check indentation of a line with a break;
3257c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3258c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3259c00df19aSJoe Perches			my $tabs = $1;
3260c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3261c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3262c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3263c00df19aSJoe Perches			}
3264c00df19aSJoe Perches		}
3265c00df19aSJoe Perches
3266c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3267cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3268000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3269000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3270c2fdda0dSAndy Whitcroft		}
327122f2a2efSAndy Whitcroft
327242e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
327342e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
327442e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3275000d1cc1SJoe Perches			ERROR("CSYNC",
3276000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
327742e41c54SMike Frysinger		}
327842e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
327942e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3280000d1cc1SJoe Perches			ERROR("SSYNC",
3281000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
328242e41c54SMike Frysinger		}
328342e41c54SMike Frysinger
328456e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
328556e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
328656e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
328756e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
328856e77d70SJoe Perches		}
328956e77d70SJoe Perches
32909c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
32912b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
32922b474a1aSAndy Whitcroft		    $realline_next);
32933e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3294ca819864SJoe Perches		if ($linenr > $suppress_statement &&
32951b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3296170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3297f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3298171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3299171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3300171ae1a4SAndy Whitcroft
33013e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
33023e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
33033e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
33043e469cdcSAndy Whitcroft			# until we hit end of it.
33053e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
33063e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
33073e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
33083e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
33093e469cdcSAndy Whitcroft			}
3310f74bd194SAndy Whitcroft
33112b474a1aSAndy Whitcroft			# Find the real next line.
33122b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
33132b474a1aSAndy Whitcroft			if (defined $realline_next &&
33142b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
33152b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
33162b474a1aSAndy Whitcroft				$realline_next++;
33172b474a1aSAndy Whitcroft			}
33182b474a1aSAndy Whitcroft
3319171ae1a4SAndy Whitcroft			my $s = $stat;
3320171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3321cf655043SAndy Whitcroft
3322c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3323171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3324c2fdda0dSAndy Whitcroft
3325c2fdda0dSAndy Whitcroft			# Ignore functions being called
3326171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3327c2fdda0dSAndy Whitcroft
3328463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3329463f2864SAndy Whitcroft
3330c45dcabdSAndy Whitcroft			# declarations always start with types
3331d2506586SAndy 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) {
3332c45dcabdSAndy Whitcroft				my $type = $1;
3333c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3334c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3335c45dcabdSAndy Whitcroft
33366c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3337a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3338c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3339c2fdda0dSAndy Whitcroft			}
33408905a67cSAndy Whitcroft
33416c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
334265863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3343c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
33449c0ca6f9SAndy Whitcroft			}
33458905a67cSAndy Whitcroft
33468905a67cSAndy Whitcroft			# Check for any sort of function declaration.
33478905a67cSAndy Whitcroft			# int foo(something bar, other baz);
33488905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3349171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
33508905a67cSAndy Whitcroft				my ($name_len) = length($1);
33518905a67cSAndy Whitcroft
3352cf655043SAndy Whitcroft				my $ctx = $s;
3353773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
33548905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3355cf655043SAndy Whitcroft
33568905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3357c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
33588905a67cSAndy Whitcroft
3359c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
33608905a67cSAndy Whitcroft					}
33618905a67cSAndy Whitcroft				}
33628905a67cSAndy Whitcroft			}
33638905a67cSAndy Whitcroft
33649c0ca6f9SAndy Whitcroft		}
33659c0ca6f9SAndy Whitcroft
336600df344fSAndy Whitcroft#
336700df344fSAndy Whitcroft# Checks which may be anchored in the context.
336800df344fSAndy Whitcroft#
336900df344fSAndy Whitcroft
337000df344fSAndy Whitcroft# Check for switch () and associated case and default
337100df344fSAndy Whitcroft# statements should be at the same indent.
337200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
337300df344fSAndy Whitcroft			my $err = '';
337400df344fSAndy Whitcroft			my $sep = '';
337500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
337600df344fSAndy Whitcroft			shift(@ctx);
337700df344fSAndy Whitcroft			for my $ctx (@ctx) {
337800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
337900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
338000df344fSAndy Whitcroft							$indent != $cindent) {
338100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
338200df344fSAndy Whitcroft					$sep = '';
338300df344fSAndy Whitcroft				} else {
338400df344fSAndy Whitcroft					$sep = "[...]\n";
338500df344fSAndy Whitcroft				}
338600df344fSAndy Whitcroft			}
338700df344fSAndy Whitcroft			if ($err ne '') {
3388000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3389000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3390de7d4f0eSAndy Whitcroft			}
3391de7d4f0eSAndy Whitcroft		}
3392de7d4f0eSAndy Whitcroft
3393de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3394de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
33950fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3396773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3397773647a0SAndy Whitcroft
33989c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
33998eef05ddSJoe Perches
34008eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
34018eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
34028eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
34038eef05ddSJoe Perches			}
34048eef05ddSJoe Perches
3405de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3406de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3407de7d4f0eSAndy Whitcroft
3408548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3409548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3410de7d4f0eSAndy Whitcroft
3411548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3412548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3413548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3414548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3415548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3416773647a0SAndy Whitcroft				$ctx_ln++;
3417773647a0SAndy Whitcroft			}
3418548596d5SAndy Whitcroft
341953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
342053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3421773647a0SAndy Whitcroft
3422773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3423000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3424000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
342501464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
342600df344fSAndy Whitcroft			}
3427773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3428773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3429773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3430773647a0SAndy Whitcroft			{
34319c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
34329c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3433000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3434000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
343501464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
34369c0ca6f9SAndy Whitcroft				}
34379c0ca6f9SAndy Whitcroft			}
343800df344fSAndy Whitcroft		}
343900df344fSAndy Whitcroft
34404d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3441f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
34423e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
34433e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
34443e469cdcSAndy Whitcroft					if (!defined $stat);
34454d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
34464d001e4dSAndy Whitcroft
34474d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
34484d001e4dSAndy Whitcroft
34499f5af480SJoe Perches			# remove inline comments
34509f5af480SJoe Perches			$s =~ s/$;/ /g;
34519f5af480SJoe Perches			$c =~ s/$;/ /g;
34524d001e4dSAndy Whitcroft
34534d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
34546f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
34556f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
34564d001e4dSAndy Whitcroft
34579f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
34589f5af480SJoe Perches			# none on the first line, and are going to readd them
34599f5af480SJoe Perches			# where necessary.
34609f5af480SJoe Perches			$s =~ s/\n./\n/gs;
34619f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
34629f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
34639f5af480SJoe Perches			}
34649f5af480SJoe Perches
34654d001e4dSAndy Whitcroft			# We want to check the first line inside the block
34664d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
34674d001e4dSAndy Whitcroft			#  1) any blank line termination
34684d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
34694d001e4dSAndy Whitcroft			#  3) any do (...) {
34704d001e4dSAndy Whitcroft			my $continuation = 0;
34714d001e4dSAndy Whitcroft			my $check = 0;
34724d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
34734d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
34744d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
34754d001e4dSAndy Whitcroft				$continuation = 1;
34764d001e4dSAndy Whitcroft			}
34779bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
34784d001e4dSAndy Whitcroft				$check = 1;
34794d001e4dSAndy Whitcroft				$cond_lines++;
34804d001e4dSAndy Whitcroft			}
34814d001e4dSAndy Whitcroft
34824d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
34834d001e4dSAndy Whitcroft			# preprocessor statement.
34844d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
34854d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
34864d001e4dSAndy Whitcroft				$check = 0;
34874d001e4dSAndy Whitcroft			}
34884d001e4dSAndy Whitcroft
34899bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3490740504c6SAndy Whitcroft			$continuation = 0;
34919bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
34929bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
34934d001e4dSAndy Whitcroft
3494f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3495f16fa28fSAndy Whitcroft				# is not linear.
3496f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3497f16fa28fSAndy Whitcroft					$check = 0;
3498f16fa28fSAndy Whitcroft				}
3499f16fa28fSAndy Whitcroft
35009bd49efeSAndy Whitcroft				# Ignore:
35019bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
35029bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
35039bd49efeSAndy Whitcroft				#  3) labels.
3504740504c6SAndy Whitcroft				if ($continuation ||
3505740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
35069bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
35079bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3508740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
350930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
35109bd49efeSAndy Whitcroft						$cond_lines++;
35119bd49efeSAndy Whitcroft					}
35124d001e4dSAndy Whitcroft				}
351330dad6ebSAndy Whitcroft			}
35144d001e4dSAndy Whitcroft
35154d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
35164d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
35174d001e4dSAndy Whitcroft
35184d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
35194d001e4dSAndy Whitcroft			# this is not this patch's fault.
35204d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
35214d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
35224d001e4dSAndy Whitcroft				$check = 0;
35234d001e4dSAndy Whitcroft			}
35244d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
35254d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
35264d001e4dSAndy Whitcroft			}
35274d001e4dSAndy Whitcroft
35289bd49efeSAndy 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";
35294d001e4dSAndy Whitcroft
35309f5af480SJoe Perches			if ($check && $s ne '' &&
35319f5af480SJoe Perches			    (($sindent % 8) != 0 ||
35329f5af480SJoe Perches			     ($sindent < $indent) ||
3533f6950a73SJoe Perches			     ($sindent == $indent &&
3534f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
35359f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3536000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3537000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
35384d001e4dSAndy Whitcroft			}
35394d001e4dSAndy Whitcroft		}
35404d001e4dSAndy Whitcroft
35416c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
35426c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
35431f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
35441f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
35456c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3546c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3547c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3548cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3549cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
35501f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3551c2fdda0dSAndy Whitcroft		}
35526c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
35536c72ffaaSAndy Whitcroft
355400df344fSAndy Whitcroft#ignore lines not being added
35553705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
355600df344fSAndy Whitcroft
355711ca40a0SJoe Perches# check for dereferences that span multiple lines
355811ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
355911ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
356011ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
356111ca40a0SJoe Perches			my $ref = $1;
356211ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
356311ca40a0SJoe Perches			$ref .= $1;
356411ca40a0SJoe Perches			$ref =~ s/\s//g;
356511ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
356611ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
356711ca40a0SJoe Perches		}
356811ca40a0SJoe Perches
3569a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3570c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3571a1ce18e4SJoe Perches			my $type = $1;
3572a1ce18e4SJoe Perches			my $var = $2;
3573207a8e84SJoe Perches			$var = "" if (!defined $var);
3574207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3575a1ce18e4SJoe Perches				my $sign = $1;
3576a1ce18e4SJoe Perches				my $pointer = $2;
3577a1ce18e4SJoe Perches
3578a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3579a1ce18e4SJoe Perches
3580a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3581a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3582a1ce18e4SJoe Perches				    $fix) {
3583a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3584207a8e84SJoe Perches					my $comp_pointer = $pointer;
3585207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3586207a8e84SJoe Perches					$decl .= $comp_pointer;
3587207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3588207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3589a1ce18e4SJoe Perches				}
3590a1ce18e4SJoe Perches			}
3591a1ce18e4SJoe Perches		}
3592a1ce18e4SJoe Perches
3593653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
35947429c690SAndy Whitcroft		if ($dbg_type) {
35957429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3596000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3597000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
35987429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3599000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3600000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
36017429c690SAndy Whitcroft			}
3602653d4876SAndy Whitcroft			next;
3603653d4876SAndy Whitcroft		}
3604a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3605a1ef277eSAndy Whitcroft		if ($dbg_attr) {
36069360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3607000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3608000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
36099360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3610000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3611000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3612a1ef277eSAndy Whitcroft			}
3613a1ef277eSAndy Whitcroft			next;
3614a1ef277eSAndy Whitcroft		}
3615653d4876SAndy Whitcroft
3616f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
361799423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
361899423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3619d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3620d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3621f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3622f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3623f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3624d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3625d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3626f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3627d752fcc8SJoe Perches				$fixedline = $line;
36288d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3629f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3630d752fcc8SJoe Perches			}
3631f0a594c1SAndy Whitcroft		}
3632f0a594c1SAndy Whitcroft
363300df344fSAndy Whitcroft#
363400df344fSAndy Whitcroft# Checks which are anchored on the added line.
363500df344fSAndy Whitcroft#
363600df344fSAndy Whitcroft
3637653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3638c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3639653d4876SAndy Whitcroft			my $path = $1;
3640653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3641000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3642495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3643495e9d84SJoe Perches			}
3644495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3645495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3646495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3647653d4876SAndy Whitcroft			}
3648653d4876SAndy Whitcroft		}
3649653d4876SAndy Whitcroft
365000df344fSAndy Whitcroft# no C99 // comments
365100df344fSAndy Whitcroft		if ($line =~ m{//}) {
36523705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
36533705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
36543705ce5bSJoe Perches			    $fix) {
3655194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
36563705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
36573705ce5bSJoe Perches					my $comment = trim($1);
3658194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
36593705ce5bSJoe Perches				}
36603705ce5bSJoe Perches			}
366100df344fSAndy Whitcroft		}
366200df344fSAndy Whitcroft		# Remove C99 comments.
36630a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
36646c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
36650a920b5bSAndy Whitcroft
36662b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
36672b474a1aSAndy Whitcroft# the whole statement.
36682b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
36692b474a1aSAndy Whitcroft		if (defined $realline_next &&
36702b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
36712b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
36722b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
36732b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
36743cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
36753cbf62dfSAndy Whitcroft			# a prefix:
36763cbf62dfSAndy Whitcroft			#   XXX(foo);
36773cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3678653d4876SAndy Whitcroft			my $name = $1;
367987a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
36803cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
36813cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
36823cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
36833cbf62dfSAndy Whitcroft
36843cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
36852b474a1aSAndy Whitcroft				\n.}\s*$|
368648012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
368748012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
368848012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
36892b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
36902b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
369148012058SAndy Whitcroft			    )/x) {
36922b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
36932b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
36942b474a1aSAndy Whitcroft			} else {
36952b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
36960a920b5bSAndy Whitcroft			}
36970a920b5bSAndy Whitcroft		}
36982b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
36992b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
37002b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
37012b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
37022b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
37032b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
37042b474a1aSAndy Whitcroft		}
37052b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
37062b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3707000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3708000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
37092b474a1aSAndy Whitcroft		}
37100a920b5bSAndy Whitcroft
37115150bda4SJoe Eloff# check for global initialisers.
37126d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3713d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
37146d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3715d5e616fcSJoe Perches			    $fix) {
37166d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3717d5e616fcSJoe Perches			}
3718f0a594c1SAndy Whitcroft		}
37190a920b5bSAndy Whitcroft# check for static initialisers.
37206d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3721d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
37226d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3723d5e616fcSJoe Perches				      $herecurr) &&
3724d5e616fcSJoe Perches			    $fix) {
37256d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3726d5e616fcSJoe Perches			}
37270a920b5bSAndy Whitcroft		}
37280a920b5bSAndy Whitcroft
37291813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
37301813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
37311813087dSJoe Perches			my $tmp = trim($1);
37321813087dSJoe Perches			WARN("MISORDERED_TYPE",
37331813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
37341813087dSJoe Perches		}
37351813087dSJoe Perches
3736cb710ecaSJoe Perches# check for static const char * arrays.
3737cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3738000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3739000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3740cb710ecaSJoe Perches				$herecurr);
3741cb710ecaSJoe Perches               }
3742cb710ecaSJoe Perches
3743cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3744cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3745000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3746000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3747cb710ecaSJoe Perches				$herecurr);
3748cb710ecaSJoe Perches               }
3749cb710ecaSJoe Perches
3750ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3751ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3752ab7e23f3SJoe Perches			my $found = $1;
3753ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3754ab7e23f3SJoe Perches				WARN("CONST_CONST",
3755ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3756ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3757ab7e23f3SJoe Perches				WARN("CONST_CONST",
3758ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3759ab7e23f3SJoe Perches			}
3760ab7e23f3SJoe Perches		}
3761ab7e23f3SJoe Perches
37629b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
37639b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
37649b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
37659b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
37669b0fa60dSJoe Perches				$herecurr);
37679b0fa60dSJoe Perches               }
37689b0fa60dSJoe Perches
3769b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3770b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3771b598b670SJoe Perches			my $array = $1;
3772b598b670SJoe 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*\))@) {
3773b598b670SJoe Perches				my $array_div = $1;
3774b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3775b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3776b598b670SJoe Perches				    $fix) {
3777b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3778b598b670SJoe Perches				}
3779b598b670SJoe Perches			}
3780b598b670SJoe Perches		}
3781b598b670SJoe Perches
3782b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3783b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3784b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3785b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3786b36190c5SJoe Perches			    $fix) {
3787194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3788b36190c5SJoe Perches			}
3789b36190c5SJoe Perches		}
3790b36190c5SJoe Perches
3791653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3792653d4876SAndy Whitcroft# make sense.
3793653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
37948054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3795c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
37968ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
379746d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
3798000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3799000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
38000a920b5bSAndy Whitcroft		}
38010a920b5bSAndy Whitcroft
38020a920b5bSAndy Whitcroft# * goes on variable not on type
380365863862SAndy Whitcroft		# (char*[ const])
3804bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3805bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
38063705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3807d8aaf121SAndy Whitcroft
380865863862SAndy Whitcroft			# Should start with a space.
380965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
381065863862SAndy Whitcroft			# Should not end with a space.
381165863862SAndy Whitcroft			$to =~ s/\s+$//;
381265863862SAndy Whitcroft			# '*'s should not have spaces between.
3813f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
381465863862SAndy Whitcroft			}
3815d8aaf121SAndy Whitcroft
38163705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
381765863862SAndy Whitcroft			if ($from ne $to) {
38183705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
38193705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
38203705ce5bSJoe Perches				    $fix) {
38213705ce5bSJoe Perches					my $sub_from = $ident;
38223705ce5bSJoe Perches					my $sub_to = $ident;
38233705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3824194f66fcSJoe Perches					$fixed[$fixlinenr] =~
38253705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
38263705ce5bSJoe Perches				}
382765863862SAndy Whitcroft			}
3828bfcb2cc7SAndy Whitcroft		}
3829bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3830bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
38313705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3832d8aaf121SAndy Whitcroft
383365863862SAndy Whitcroft			# Should start with a space.
383465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
383565863862SAndy Whitcroft			# Should not end with a space.
383665863862SAndy Whitcroft			$to =~ s/\s+$//;
383765863862SAndy Whitcroft			# '*'s should not have spaces between.
3838f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
383965863862SAndy Whitcroft			}
384065863862SAndy Whitcroft			# Modifiers should have spaces.
384165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
384265863862SAndy Whitcroft
38433705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3844667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
38453705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
38463705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
38473705ce5bSJoe Perches				    $fix) {
38483705ce5bSJoe Perches
38493705ce5bSJoe Perches					my $sub_from = $match;
38503705ce5bSJoe Perches					my $sub_to = $match;
38513705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3852194f66fcSJoe Perches					$fixed[$fixlinenr] =~
38533705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
38543705ce5bSJoe Perches				}
385565863862SAndy Whitcroft			}
38560a920b5bSAndy Whitcroft		}
38570a920b5bSAndy Whitcroft
38589d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
38599d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
38600675a8fbSJean Delvare			my $msg_level = \&WARN;
38610675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
38620675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
38639d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
38649d3e3c70SJoe Perches		}
38650a920b5bSAndy Whitcroft
38669d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
38678905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3868000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3869000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
38708905a67cSAndy Whitcroft		}
38718905a67cSAndy Whitcroft
387217441227SJoe Perches# check for uses of printk_ratelimit
387317441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3874000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3875000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
387617441227SJoe Perches		}
387717441227SJoe Perches
3878eeef5733SJoe Perches# printk should use KERN_* levels
3879eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3880000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
3881eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
388200df344fSAndy Whitcroft		}
38830a920b5bSAndy Whitcroft
3884243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3885243f3803SJoe Perches			my $orig = $1;
3886243f3803SJoe Perches			my $level = lc($orig);
3887243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
38888f26b837SJoe Perches			my $level2 = $level;
38898f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3890243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3891daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3892243f3803SJoe Perches		}
3893243f3803SJoe Perches
3894243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3895d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3896d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3897d5e616fcSJoe Perches			    $fix) {
3898194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3899d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3900d5e616fcSJoe Perches			}
3901243f3803SJoe Perches		}
3902243f3803SJoe Perches
3903dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3904dc139313SJoe Perches			my $orig = $1;
3905dc139313SJoe Perches			my $level = lc($orig);
3906dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3907dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3908dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3909dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3910dc139313SJoe Perches		}
3911dc139313SJoe Perches
391291c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
391391c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
391491c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
391591c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
391691c9afafSAndy Lutomirski			WARN("ENOSYS",
391791c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
391891c9afafSAndy Lutomirski		}
391991c9afafSAndy Lutomirski
3920653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3921653d4876SAndy Whitcroft# or if closed on same line
39228d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
39234e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
39248d182478SJoe Perches			if (ERROR("OPEN_BRACE",
39258d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
39268d182478SJoe Perches			    $fix) {
39278d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39288d182478SJoe Perches				my $fixed_line = $rawline;
39298d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
39308d182478SJoe Perches				my $line1 = $1;
39318d182478SJoe Perches				my $line2 = $2;
39328d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
39338d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
39348d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
39358d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
39368d182478SJoe Perches				}
39378d182478SJoe Perches			}
39380a920b5bSAndy Whitcroft		}
3939653d4876SAndy Whitcroft
39408905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
39418905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
39428905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
39438d182478SJoe Perches			if (ERROR("OPEN_BRACE",
39448d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
39458d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39468d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
39478d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39488d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
39498d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
39508d182478SJoe Perches				$fixedline = $rawline;
39518d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
39528d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
39538d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39548d182478SJoe Perches				}
39558d182478SJoe Perches			}
39568905a67cSAndy Whitcroft		}
39578905a67cSAndy Whitcroft
39580c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
39593705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
39603705ce5bSJoe Perches			if (WARN("SPACING",
39613705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
39623705ce5bSJoe Perches			    $fix) {
3963194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39643705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
39653705ce5bSJoe Perches			}
39660c73b4ebSAndy Whitcroft		}
39670c73b4ebSAndy Whitcroft
396831070b5dSJoe Perches# Function pointer declarations
396931070b5dSJoe Perches# check spacing between type, funcptr, and args
397031070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
397191f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
397231070b5dSJoe Perches			my $declare = $1;
397331070b5dSJoe Perches			my $pre_pointer_space = $2;
397431070b5dSJoe Perches			my $post_pointer_space = $3;
397531070b5dSJoe Perches			my $funcname = $4;
397631070b5dSJoe Perches			my $post_funcname_space = $5;
397731070b5dSJoe Perches			my $pre_args_space = $6;
397831070b5dSJoe Perches
397991f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
398091f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
398191f72e9cSJoe Perches# don't need a space so don't warn for those.
398291f72e9cSJoe Perches			my $post_declare_space = "";
398391f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
398491f72e9cSJoe Perches				$post_declare_space = $1;
398591f72e9cSJoe Perches				$declare = rtrim($declare);
398691f72e9cSJoe Perches			}
398791f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
398831070b5dSJoe Perches				WARN("SPACING",
398931070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
399091f72e9cSJoe Perches				$post_declare_space = " ";
399131070b5dSJoe Perches			}
399231070b5dSJoe Perches
399331070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
399491f72e9cSJoe Perches# This test is not currently implemented because these declarations are
399591f72e9cSJoe Perches# equivalent to
399691f72e9cSJoe Perches#	int  foo(int bar, ...)
399791f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
399891f72e9cSJoe Perches#
399991f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
400091f72e9cSJoe Perches#				WARN("SPACING",
400191f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
400291f72e9cSJoe Perches#			}
400331070b5dSJoe Perches
400431070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
400531070b5dSJoe Perches			if (defined $pre_pointer_space &&
400631070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
400731070b5dSJoe Perches				WARN("SPACING",
400831070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
400931070b5dSJoe Perches			}
401031070b5dSJoe Perches
401131070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
401231070b5dSJoe Perches			if (defined $post_pointer_space &&
401331070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
401431070b5dSJoe Perches				WARN("SPACING",
401531070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
401631070b5dSJoe Perches			}
401731070b5dSJoe Perches
401831070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
401931070b5dSJoe Perches			if (defined $post_funcname_space &&
402031070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
402131070b5dSJoe Perches				WARN("SPACING",
402231070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
402331070b5dSJoe Perches			}
402431070b5dSJoe Perches
402531070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
402631070b5dSJoe Perches			if (defined $pre_args_space &&
402731070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
402831070b5dSJoe Perches				WARN("SPACING",
402931070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
403031070b5dSJoe Perches			}
403131070b5dSJoe Perches
403231070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4033194f66fcSJoe Perches				$fixed[$fixlinenr] =~
403491f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
403531070b5dSJoe Perches			}
403631070b5dSJoe Perches		}
403731070b5dSJoe Perches
40388d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
40398d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4040fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4041fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
40428d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
40438d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
40448d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4045fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
4046daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
40473705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
40483705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
40493705ce5bSJoe Perches				    $fix) {
4050194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
40513705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
40523705ce5bSJoe Perches				}
40538d31cfceSAndy Whitcroft			}
40548d31cfceSAndy Whitcroft		}
40558d31cfceSAndy Whitcroft
4056f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
40576c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4058c2fdda0dSAndy Whitcroft			my $name = $1;
4059773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4060773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4061c2fdda0dSAndy Whitcroft
4062c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4063773647a0SAndy Whitcroft			if ($name =~ /^(?:
4064773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4065773647a0SAndy Whitcroft				volatile|__volatile__|
4066773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4067773647a0SAndy Whitcroft				asm|__asm__)$/x)
4068773647a0SAndy Whitcroft			{
4069c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4070c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4071c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4072c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4073773647a0SAndy Whitcroft
4074773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4075c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4076c2fdda0dSAndy Whitcroft
4077c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4078c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4079773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4080c2fdda0dSAndy Whitcroft
4081c2fdda0dSAndy Whitcroft			} else {
40823705ce5bSJoe Perches				if (WARN("SPACING",
40833705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
40843705ce5bSJoe Perches					     $fix) {
4085194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40863705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
40873705ce5bSJoe Perches				}
4088f0a594c1SAndy Whitcroft			}
40896c72ffaaSAndy Whitcroft		}
40909a4cad4eSEric Nelson
4091653d4876SAndy Whitcroft# Check operator spacing.
40920a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
40933705ce5bSJoe Perches			my $fixed_line = "";
40943705ce5bSJoe Perches			my $line_fixed = 0;
40953705ce5bSJoe Perches
40969c0ca6f9SAndy Whitcroft			my $ops = qr{
40979c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
40989c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
40999c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
41001f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
410184731623SJoe Perches				\?:|\?|:
41029c0ca6f9SAndy Whitcroft			}x;
4103cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
41043705ce5bSJoe Perches
41053705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
41063705ce5bSJoe Perches##			foreach my $el (@elements) {
41073705ce5bSJoe Perches##				print("el: <$el>\n");
41083705ce5bSJoe Perches##			}
41093705ce5bSJoe Perches
41103705ce5bSJoe Perches			my @fix_elements = ();
411100df344fSAndy Whitcroft			my $off = 0;
41126c72ffaaSAndy Whitcroft
41133705ce5bSJoe Perches			foreach my $el (@elements) {
41143705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
41153705ce5bSJoe Perches				$off += length($el);
41163705ce5bSJoe Perches			}
41173705ce5bSJoe Perches
41183705ce5bSJoe Perches			$off = 0;
41193705ce5bSJoe Perches
41206c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4121b34c648bSJoe Perches			my $last_after = -1;
41226c72ffaaSAndy Whitcroft
41230a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
41243705ce5bSJoe Perches
41253705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
41263705ce5bSJoe Perches
41273705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
41283705ce5bSJoe Perches
41294a0df2efSAndy Whitcroft				$off += length($elements[$n]);
41304a0df2efSAndy Whitcroft
413125985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4132773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4133773647a0SAndy Whitcroft				my $cc = '';
4134773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4135773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4136773647a0SAndy Whitcroft				}
4137773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4138773647a0SAndy Whitcroft
41394a0df2efSAndy Whitcroft				my $a = '';
41404a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
41414a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4142cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
41434a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
41444a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4145773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
41464a0df2efSAndy Whitcroft
41470a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
41484a0df2efSAndy Whitcroft
41494a0df2efSAndy Whitcroft				my $c = '';
41500a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
41514a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
41524a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4153cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
41544a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
41554a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
41568b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
41574a0df2efSAndy Whitcroft				} else {
41584a0df2efSAndy Whitcroft					$c = 'E';
41590a920b5bSAndy Whitcroft				}
41600a920b5bSAndy Whitcroft
41614a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
41624a0df2efSAndy Whitcroft
41634a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
41644a0df2efSAndy Whitcroft
41656c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4166de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
41670a920b5bSAndy Whitcroft
416874048ed8SAndy Whitcroft				# Pull out the value of this operator.
41696c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
41700a920b5bSAndy Whitcroft
41711f65f947SAndy Whitcroft				# Get the full operator variant.
41721f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
41731f65f947SAndy Whitcroft
417413214adfSAndy Whitcroft				# Ignore operators passed as parameters.
417513214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4176d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
417713214adfSAndy Whitcroft
4178cf655043SAndy Whitcroft#				# Ignore comments
4179cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
418013214adfSAndy Whitcroft
4181d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
418213214adfSAndy Whitcroft				} elsif ($op eq ';') {
4183cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4184cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
41853705ce5bSJoe Perches						if (ERROR("SPACING",
41863705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4187b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
41883705ce5bSJoe Perches							$line_fixed = 1;
41893705ce5bSJoe Perches						}
4190d8aaf121SAndy Whitcroft					}
4191d8aaf121SAndy Whitcroft
4192d8aaf121SAndy Whitcroft				# // is a comment
4193d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
41940a920b5bSAndy Whitcroft
4195b00e4814SJoe Perches				#   :   when part of a bitfield
4196b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4197b00e4814SJoe Perches					# skip the bitfield test for now
4198b00e4814SJoe Perches
41991f65f947SAndy Whitcroft				# No spaces for:
42001f65f947SAndy Whitcroft				#   ->
4201b00e4814SJoe Perches				} elsif ($op eq '->') {
42024a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
42033705ce5bSJoe Perches						if (ERROR("SPACING",
42043705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4205b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42063705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42073705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
42083705ce5bSJoe Perches							}
4209b34c648bSJoe Perches							$line_fixed = 1;
42103705ce5bSJoe Perches						}
42110a920b5bSAndy Whitcroft					}
42120a920b5bSAndy Whitcroft
42132381097bSJoe Perches				# , must not have a space before and must have a space on the right.
42140a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
42152381097bSJoe Perches					my $rtrim_before = 0;
42162381097bSJoe Perches					my $space_after = 0;
42172381097bSJoe Perches					if ($ctx =~ /Wx./) {
42182381097bSJoe Perches						if (ERROR("SPACING",
42192381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
42202381097bSJoe Perches							$line_fixed = 1;
42212381097bSJoe Perches							$rtrim_before = 1;
42222381097bSJoe Perches						}
42232381097bSJoe Perches					}
4224cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
42253705ce5bSJoe Perches						if (ERROR("SPACING",
42263705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
42273705ce5bSJoe Perches							$line_fixed = 1;
4228b34c648bSJoe Perches							$last_after = $n;
42292381097bSJoe Perches							$space_after = 1;
42302381097bSJoe Perches						}
42312381097bSJoe Perches					}
42322381097bSJoe Perches					if ($rtrim_before || $space_after) {
42332381097bSJoe Perches						if ($rtrim_before) {
42342381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42352381097bSJoe Perches						} else {
42362381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
42372381097bSJoe Perches						}
42382381097bSJoe Perches						if ($space_after) {
42392381097bSJoe Perches							$good .= " ";
42403705ce5bSJoe Perches						}
42410a920b5bSAndy Whitcroft					}
42420a920b5bSAndy Whitcroft
42439c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
424474048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
42459c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
42469c0ca6f9SAndy Whitcroft
42479c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
42489c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
42499c0ca6f9SAndy Whitcroft				# unary operator, or a cast
42509c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
425174048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
42520d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4253cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
42543705ce5bSJoe Perches						if (ERROR("SPACING",
42553705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4256b34c648bSJoe Perches							if ($n != $last_after + 2) {
4257b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
42583705ce5bSJoe Perches								$line_fixed = 1;
42593705ce5bSJoe Perches							}
42600a920b5bSAndy Whitcroft						}
4261b34c648bSJoe Perches					}
4262a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4263171ae1a4SAndy Whitcroft						# A unary '*' may be const
4264171ae1a4SAndy Whitcroft
4265171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
42663705ce5bSJoe Perches						if (ERROR("SPACING",
42673705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4268b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
42693705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42703705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
42713705ce5bSJoe Perches							}
4272b34c648bSJoe Perches							$line_fixed = 1;
42733705ce5bSJoe Perches						}
42740a920b5bSAndy Whitcroft					}
42750a920b5bSAndy Whitcroft
42760a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
42770a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4278773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
42793705ce5bSJoe Perches						if (ERROR("SPACING",
42803705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4281b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
42823705ce5bSJoe Perches							$line_fixed = 1;
42833705ce5bSJoe Perches						}
42840a920b5bSAndy Whitcroft					}
4285773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4286773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
42873705ce5bSJoe Perches						if (ERROR("SPACING",
42883705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4289b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42903705ce5bSJoe Perches							$line_fixed = 1;
42913705ce5bSJoe Perches						}
4292653d4876SAndy Whitcroft					}
4293773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
42943705ce5bSJoe Perches						if (ERROR("SPACING",
42953705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4296b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
42973705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42983705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4299773647a0SAndy Whitcroft							}
4300b34c648bSJoe Perches							$line_fixed = 1;
43013705ce5bSJoe Perches						}
43023705ce5bSJoe Perches					}
43030a920b5bSAndy Whitcroft
43040a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
43059c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
43069c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
43079c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4308c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4309c2fdda0dSAndy Whitcroft					 $op eq '%')
43100a920b5bSAndy Whitcroft				{
4311d2e025f3SJoe Perches					if ($check) {
4312d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4313d2e025f3SJoe Perches							if (CHK("SPACING",
4314d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4315d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4316d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4317d2e025f3SJoe Perches								$line_fixed = 1;
4318d2e025f3SJoe Perches							}
4319d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4320d2e025f3SJoe Perches							if (CHK("SPACING",
4321d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4322d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4323d2e025f3SJoe Perches								$line_fixed = 1;
4324d2e025f3SJoe Perches							}
4325d2e025f3SJoe Perches						}
4326d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
43273705ce5bSJoe Perches						if (ERROR("SPACING",
43283705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4329b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4330b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4331b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4332b34c648bSJoe Perches							}
43333705ce5bSJoe Perches							$line_fixed = 1;
43343705ce5bSJoe Perches						}
43350a920b5bSAndy Whitcroft					}
43360a920b5bSAndy Whitcroft
43371f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
43381f65f947SAndy Whitcroft				# terminating a case value or a label.
43391f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
43401f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
43413705ce5bSJoe Perches						if (ERROR("SPACING",
43423705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4343b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43443705ce5bSJoe Perches							$line_fixed = 1;
43453705ce5bSJoe Perches						}
43461f65f947SAndy Whitcroft					}
43471f65f947SAndy Whitcroft
43480a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4349cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
43501f65f947SAndy Whitcroft					my $ok = 0;
43511f65f947SAndy Whitcroft
435222f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
43531f65f947SAndy Whitcroft					if (($op eq '<' &&
43541f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
43551f65f947SAndy Whitcroft					    ($op eq '>' &&
43561f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
43571f65f947SAndy Whitcroft					{
43581f65f947SAndy Whitcroft					    	$ok = 1;
43591f65f947SAndy Whitcroft					}
43601f65f947SAndy Whitcroft
4361e0df7e1fSJoe Perches					# for asm volatile statements
4362e0df7e1fSJoe Perches					# ignore a colon with another
4363e0df7e1fSJoe Perches					# colon immediately before or after
4364e0df7e1fSJoe Perches					if (($op eq ':') &&
4365e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4366e0df7e1fSJoe Perches						$ok = 1;
4367e0df7e1fSJoe Perches					}
4368e0df7e1fSJoe Perches
436984731623SJoe Perches					# messages are ERROR, but ?: are CHK
43701f65f947SAndy Whitcroft					if ($ok == 0) {
43710675a8fbSJean Delvare						my $msg_level = \&ERROR;
43720675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
437384731623SJoe Perches
43740675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
43753705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4376b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4377b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4378b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4379b34c648bSJoe Perches							}
43803705ce5bSJoe Perches							$line_fixed = 1;
43813705ce5bSJoe Perches						}
43820a920b5bSAndy Whitcroft					}
438322f2a2efSAndy Whitcroft				}
43844a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
43853705ce5bSJoe Perches
43863705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
43873705ce5bSJoe Perches
43883705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
43890a920b5bSAndy Whitcroft			}
43903705ce5bSJoe Perches
43913705ce5bSJoe Perches			if (($#elements % 2) == 0) {
43923705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
43933705ce5bSJoe Perches			}
43943705ce5bSJoe Perches
4395194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4396194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
43973705ce5bSJoe Perches			}
43983705ce5bSJoe Perches
43993705ce5bSJoe Perches
44000a920b5bSAndy Whitcroft		}
44010a920b5bSAndy Whitcroft
4402786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4403d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4404786b6326SJoe Perches			if (WARN("SPACING",
4405786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4406786b6326SJoe Perches			    $fix) {
4407194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4408786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4409786b6326SJoe Perches			}
4410786b6326SJoe Perches		}
4411786b6326SJoe Perches
4412f0a594c1SAndy Whitcroft# check for multiple assignments
4413f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4414000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4415000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4416f0a594c1SAndy Whitcroft		}
4417f0a594c1SAndy Whitcroft
441822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
441922f2a2efSAndy Whitcroft## # continuation.
442022f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
442122f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
442222f2a2efSAndy Whitcroft##
442322f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
442422f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
442522f2a2efSAndy Whitcroft## 			my $ln = $line;
442622f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
442722f2a2efSAndy Whitcroft## 			}
442822f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4429000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4430000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
443122f2a2efSAndy Whitcroft## 			}
443222f2a2efSAndy Whitcroft## 		}
4433f0a594c1SAndy Whitcroft
44340a920b5bSAndy Whitcroft#need space before brace following if, while, etc
44356b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
44364e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
44373705ce5bSJoe Perches			if (ERROR("SPACING",
44383705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
44393705ce5bSJoe Perches			    $fix) {
44408d81ae05SCyril Bur				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
44413705ce5bSJoe Perches			}
4442de7d4f0eSAndy Whitcroft		}
4443de7d4f0eSAndy Whitcroft
4444c4a62ef9SJoe Perches## # check for blank lines before declarations
4445c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4446c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4447c4a62ef9SJoe Perches##			WARN("SPACING",
4448c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4449c4a62ef9SJoe Perches##		}
4450c4a62ef9SJoe Perches##
4451c4a62ef9SJoe Perches
4452de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4453de7d4f0eSAndy Whitcroft# on the line
4454de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4455d5e616fcSJoe Perches			if (ERROR("SPACING",
4456d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4457d5e616fcSJoe Perches			    $fix) {
4458194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4459d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4460d5e616fcSJoe Perches			}
44610a920b5bSAndy Whitcroft		}
44620a920b5bSAndy Whitcroft
446322f2a2efSAndy Whitcroft# check spacing on square brackets
446422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
44653705ce5bSJoe Perches			if (ERROR("SPACING",
44663705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
44673705ce5bSJoe Perches			    $fix) {
4468194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44693705ce5bSJoe Perches				    s/\[\s+/\[/;
44703705ce5bSJoe Perches			}
447122f2a2efSAndy Whitcroft		}
447222f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
44733705ce5bSJoe Perches			if (ERROR("SPACING",
44743705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
44753705ce5bSJoe Perches			    $fix) {
4476194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44773705ce5bSJoe Perches				    s/\s+\]/\]/;
44783705ce5bSJoe Perches			}
447922f2a2efSAndy Whitcroft		}
448022f2a2efSAndy Whitcroft
4481c45dcabdSAndy Whitcroft# check spacing on parentheses
44829c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
44839c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
44843705ce5bSJoe Perches			if (ERROR("SPACING",
44853705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
44863705ce5bSJoe Perches			    $fix) {
4487194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44883705ce5bSJoe Perches				    s/\(\s+/\(/;
44893705ce5bSJoe Perches			}
449022f2a2efSAndy Whitcroft		}
449113214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4492c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4493c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
44943705ce5bSJoe Perches			if (ERROR("SPACING",
44953705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
44963705ce5bSJoe Perches			    $fix) {
4497194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44983705ce5bSJoe Perches				    s/\s+\)/\)/;
44993705ce5bSJoe Perches			}
450022f2a2efSAndy Whitcroft		}
450122f2a2efSAndy Whitcroft
4502e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4503e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4504e2826fd0SJoe Perches
4505e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4506ea4acbb1SJoe Perches			my $var = $1;
4507ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4508ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4509ea4acbb1SJoe Perches			    $fix) {
4510ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4511ea4acbb1SJoe Perches			}
4512ea4acbb1SJoe Perches		}
4513ea4acbb1SJoe Perches
4514ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4515ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4516ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4517ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4518ea4acbb1SJoe Perches			my $var = $2;
4519ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4520ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4521ea4acbb1SJoe Perches			    $fix) {
4522ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4523ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4524ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4525ea4acbb1SJoe Perches			}
4526e2826fd0SJoe Perches		}
4527e2826fd0SJoe Perches
452863b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
452963b7c73eSJoe Perches		if ($^V && $^V ge 5.10.0 && defined($stat) &&
453063b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
453163b7c73eSJoe Perches			my $if_stat = $1;
453263b7c73eSJoe Perches			my $test = substr($2, 1, -1);
453363b7c73eSJoe Perches			my $herectx;
453463b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
453563b7c73eSJoe Perches				my $match = $1;
453663b7c73eSJoe Perches				# avoid parentheses around potential macro args
453763b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
453863b7c73eSJoe Perches				if (!defined($herectx)) {
453963b7c73eSJoe Perches					$herectx = $here . "\n";
454063b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
454163b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
454263b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
454363b7c73eSJoe Perches						$herectx .=  $rl . "\n";
454463b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
454563b7c73eSJoe Perches					}
454663b7c73eSJoe Perches				}
454763b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
454863b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
454963b7c73eSJoe Perches			}
455063b7c73eSJoe Perches		}
455163b7c73eSJoe Perches
45520a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
45534a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
45540a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
45553705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
45563705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
45573705ce5bSJoe Perches			    $fix) {
4558194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45593705ce5bSJoe Perches				    s/^(.)\s+/$1/;
45603705ce5bSJoe Perches			}
45610a920b5bSAndy Whitcroft		}
45620a920b5bSAndy Whitcroft
45635b9553abSJoe Perches# return is not a function
4564507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4565c45dcabdSAndy Whitcroft			my $spacing = $1;
4566507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
45675b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
45685b9553abSJoe Perches				my $value = $1;
45695b9553abSJoe Perches				$value = deparenthesize($value);
45705b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4571000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4572000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
45735b9553abSJoe Perches				}
4574c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4575000d1cc1SJoe Perches				ERROR("SPACING",
4576000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4577c45dcabdSAndy Whitcroft			}
4578c45dcabdSAndy Whitcroft		}
4579507e5141SJoe Perches
4580b43ae21bSJoe Perches# unnecessary return in a void function
4581b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4582b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4583b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4584b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4585b43ae21bSJoe Perches		    $linenr >= 3 &&
4586b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4587b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
45889819cf25SJoe Perches			WARN("RETURN_VOID",
4589b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
45909819cf25SJoe Perches               }
45919819cf25SJoe Perches
4592189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4593189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4594189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4595189248d8SJoe Perches			my $openparens = $1;
4596189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4597189248d8SJoe Perches			my $msg = "";
4598189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4599189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4600189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4601189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4602189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4603189248d8SJoe Perches			}
4604189248d8SJoe Perches		}
4605189248d8SJoe Perches
4606c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4607c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4608c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4609c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4610c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4611c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4612c5595fa2SJoe Perches			my $lead = $1;
4613c5595fa2SJoe Perches			my $const = $2;
4614c5595fa2SJoe Perches			my $comp = $3;
4615c5595fa2SJoe Perches			my $to = $4;
4616c5595fa2SJoe Perches			my $newcomp = $comp;
4617f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4618c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4619c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4620c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4621c5595fa2SJoe Perches			    $fix) {
4622c5595fa2SJoe Perches				if ($comp eq "<") {
4623c5595fa2SJoe Perches					$newcomp = ">";
4624c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4625c5595fa2SJoe Perches					$newcomp = ">=";
4626c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4627c5595fa2SJoe Perches					$newcomp = "<";
4628c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4629c5595fa2SJoe Perches					$newcomp = "<=";
4630c5595fa2SJoe Perches				}
4631c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4632c5595fa2SJoe Perches			}
4633c5595fa2SJoe Perches		}
4634c5595fa2SJoe Perches
4635f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4636f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
463753a3c448SAndy Whitcroft			my $name = $1;
463853a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4639000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4640f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
464153a3c448SAndy Whitcroft			}
464253a3c448SAndy Whitcroft		}
4643c45dcabdSAndy Whitcroft
46440a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
46454a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
46463705ce5bSJoe Perches			if (ERROR("SPACING",
46473705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
46483705ce5bSJoe Perches			    $fix) {
4649194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46503705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
46513705ce5bSJoe Perches			}
46520a920b5bSAndy Whitcroft		}
46530a920b5bSAndy Whitcroft
4654f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4655f5fe35ddSAndy Whitcroft# statements after the conditional.
4656170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
46573e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
46583e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
46593e469cdcSAndy Whitcroft					if (!defined $stat);
4660170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4661170d3a22SAndy Whitcroft						$remain_next, $off_next);
4662170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4663170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4664170d3a22SAndy Whitcroft
4665170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4666170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4667170d3a22SAndy Whitcroft				# then count those as offsets.
4668170d3a22SAndy Whitcroft				my ($whitespace) =
4669170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4670170d3a22SAndy Whitcroft				my $offset =
4671170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4672170d3a22SAndy Whitcroft
4673170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4674170d3a22SAndy Whitcroft								$offset} = 1;
4675170d3a22SAndy Whitcroft			}
4676170d3a22SAndy Whitcroft		}
4677170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4678c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4679170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4680171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
46818905a67cSAndy Whitcroft
4682b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4683000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4684000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
46858905a67cSAndy Whitcroft			}
46868905a67cSAndy Whitcroft
46878905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
46888905a67cSAndy Whitcroft			# conditional.
4689773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
46908905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
469113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
469253210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
469353210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4694773647a0SAndy Whitcroft			{
4695bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4696bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4697bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
469842bdf74cSHidetoshi Seto				my $stat_real = '';
4699bb44ad39SAndy Whitcroft
470042bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
470142bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4702bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4703bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4704bb44ad39SAndy Whitcroft				}
4705bb44ad39SAndy Whitcroft
4706000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4707000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
47088905a67cSAndy Whitcroft			}
47098905a67cSAndy Whitcroft		}
47108905a67cSAndy Whitcroft
471113214adfSAndy Whitcroft# Check for bitwise tests written as boolean
471213214adfSAndy Whitcroft		if ($line =~ /
471313214adfSAndy Whitcroft			(?:
471413214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
471513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
471613214adfSAndy Whitcroft				(?:\&\&|\|\|)
471713214adfSAndy Whitcroft			|
471813214adfSAndy Whitcroft				(?:\&\&|\|\|)
471913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
472013214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
472113214adfSAndy Whitcroft			)/x)
472213214adfSAndy Whitcroft		{
4723000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4724000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
472513214adfSAndy Whitcroft		}
472613214adfSAndy Whitcroft
47278905a67cSAndy Whitcroft# if and else should not have general statements after it
472813214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
472913214adfSAndy Whitcroft			my $s = $1;
473013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
473113214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4732000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4733000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
47340a920b5bSAndy Whitcroft			}
473513214adfSAndy Whitcroft		}
473639667782SAndy Whitcroft# if should not continue a brace
473739667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4738000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4739048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
474039667782SAndy Whitcroft				$herecurr);
474139667782SAndy Whitcroft		}
4742a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4743a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4744a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
47453fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4746a1080bf8SAndy Whitcroft			\s*return\s+
4747a1080bf8SAndy Whitcroft		    )/xg)
4748a1080bf8SAndy Whitcroft		{
4749000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4750000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4751a1080bf8SAndy Whitcroft		}
47520a920b5bSAndy Whitcroft
47530a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
47540a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
47558b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
47560a920b5bSAndy Whitcroft		    $previndent == $indent) {
47578b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
47588b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
47598b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
47608b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
47618b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
47628b8856f4SJoe Perches				my $fixedline = $prevrawline;
47638b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
47648b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
47658b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
47668b8856f4SJoe Perches				}
47678b8856f4SJoe Perches				$fixedline = $rawline;
47688b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
47698b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
47708b8856f4SJoe Perches			}
47710a920b5bSAndy Whitcroft		}
47720a920b5bSAndy Whitcroft
47738b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4774c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4775c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4776c2fdda0dSAndy Whitcroft
4777c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4778c2fdda0dSAndy Whitcroft			# conditional.
4779773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4780c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4781c2fdda0dSAndy Whitcroft
4782c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
47838b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
47848b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
47858b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
47868b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
47878b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
47888b8856f4SJoe Perches					my $fixedline = $prevrawline;
47898b8856f4SJoe Perches					my $trailing = $rawline;
47908b8856f4SJoe Perches					$trailing =~ s/^\+//;
47918b8856f4SJoe Perches					$trailing = trim($trailing);
47928b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
47938b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
47948b8856f4SJoe Perches				}
4795c2fdda0dSAndy Whitcroft			}
4796c2fdda0dSAndy Whitcroft		}
4797c2fdda0dSAndy Whitcroft
479895e2c602SJoe Perches#Specific variable tests
4799323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4800323c1260SJoe Perches			my $var = $1;
480195e2c602SJoe Perches
480295e2c602SJoe Perches#gcc binary extension
480395e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4804d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4805d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4806d5e616fcSJoe Perches				    $fix) {
4807d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4808194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4809d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4810d5e616fcSJoe Perches				}
481195e2c602SJoe Perches			}
481295e2c602SJoe Perches
481395e2c602SJoe Perches#CamelCase
4814807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4815be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
481622735ce8SJoe Perches#Ignore Page<foo> variants
4817807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
481822735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4819f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4820f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4821f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
48227e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
48237e781f67SJoe Perches					my $word = $1;
48247e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4825d8b07710SJoe Perches					if ($check) {
4826d8b07710SJoe Perches						seed_camelcase_includes();
4827d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4828d8b07710SJoe Perches							seed_camelcase_file($realfile);
4829d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4830d8b07710SJoe Perches						}
4831d8b07710SJoe Perches					}
48327e781f67SJoe Perches					if (!defined $camelcase{$word}) {
48337e781f67SJoe Perches						$camelcase{$word} = 1;
4834be79794bSJoe Perches						CHK("CAMELCASE",
48357e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
48367e781f67SJoe Perches					}
4837323c1260SJoe Perches				}
4838323c1260SJoe Perches			}
48393445686aSJoe Perches		}
48400a920b5bSAndy Whitcroft
48410a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4842d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4843d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4844d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4845d5e616fcSJoe Perches			    $fix) {
4846194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4847d5e616fcSJoe Perches			}
48480a920b5bSAndy Whitcroft		}
48490a920b5bSAndy Whitcroft
48500e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
48510e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4852c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4853e09dec48SAndy Whitcroft			my $file = "$1.h";
4854e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4855e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4856e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
48577840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4858c45dcabdSAndy Whitcroft			{
48590e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
48600e212e0aSFabian Frederick				if ($asminclude > 0) {
4861e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4862000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4863000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4864e09dec48SAndy Whitcroft					} else {
4865000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4866000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4867e09dec48SAndy Whitcroft					}
48680a920b5bSAndy Whitcroft				}
48690a920b5bSAndy Whitcroft			}
48700e212e0aSFabian Frederick		}
48710a920b5bSAndy Whitcroft
4872653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4873653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4874cf655043SAndy Whitcroft# in a known good container
4875b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4876b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4877d8aaf121SAndy Whitcroft			my $ln = $linenr;
4878d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4879c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4880c45dcabdSAndy Whitcroft			my $ctx = '';
488108a2843eSJoe Perches			my $has_flow_statement = 0;
488208a2843eSJoe Perches			my $has_arg_concat = 0;
4883c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4884f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4885f74bd194SAndy Whitcroft			$ctx = $dstat;
4886c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4887a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4888c45dcabdSAndy Whitcroft
488908a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
489062e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
489108a2843eSJoe Perches
4892f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4893f59b64bfSJoe Perches			my $define_args = $1;
4894f59b64bfSJoe Perches			my $define_stmt = $dstat;
4895f59b64bfSJoe Perches			my @def_args = ();
4896f59b64bfSJoe Perches
4897f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
4898f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
4899f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
4900f59b64bfSJoe Perches				@def_args = split(",", $define_args);
4901f59b64bfSJoe Perches			}
4902f59b64bfSJoe Perches
4903292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4904c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4905c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4906c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4907c45dcabdSAndy Whitcroft
4908c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4909bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4910bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
49116b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4912bf30d6edSAndy Whitcroft			{
4913c45dcabdSAndy Whitcroft			}
4914c45dcabdSAndy Whitcroft
4915e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
491633acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
491733acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4918e45bab8eSAndy Whitcroft			{
4919e45bab8eSAndy Whitcroft			}
4920e45bab8eSAndy Whitcroft
492142e15293SJoe Perches			# Make asm volatile uses seem like a generic function
492242e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
492342e15293SJoe Perches
4924c45dcabdSAndy Whitcroft			my $exceptions = qr{
4925c45dcabdSAndy Whitcroft				$Declare|
4926c45dcabdSAndy Whitcroft				module_param_named|
4927a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4928c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4929c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4930383099fdSAndy Whitcroft				__typeof__\(|
493122fd2d3eSStefani Seibold				union|
493222fd2d3eSStefani Seibold				struct|
4933ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
49346b10df42SVladimir Zapolskiy				^\"|\"$|
49356b10df42SVladimir Zapolskiy				^\[
4936c45dcabdSAndy Whitcroft			}x;
49375eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4938f59b64bfSJoe Perches
4939f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
4940f59b64bfSJoe Perches			my $herectx = $here . "\n";
4941f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
4942f59b64bfSJoe Perches
4943f59b64bfSJoe Perches			for (my $n = 0; $n < $stmt_cnt; $n++) {
4944f59b64bfSJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4945f59b64bfSJoe Perches			}
4946f59b64bfSJoe Perches
4947f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4948f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4949f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
49503cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4951356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4952f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4953f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4954e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
495572f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4956f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4957f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4958f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
49594e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4960f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4961c45dcabdSAndy Whitcroft			{
4962e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
4963e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4964e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4965e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
4966f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4967f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4968f74bd194SAndy Whitcroft				} else {
4969000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4970388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4971d8aaf121SAndy Whitcroft				}
4972f59b64bfSJoe Perches
4973f59b64bfSJoe Perches			}
49745207649bSJoe Perches
49755207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
49765207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
49775207649bSJoe Perches			my $first = 1;
49785207649bSJoe Perches			$define_stmt = "";
49795207649bSJoe Perches			foreach my $l (@stmt_array) {
49805207649bSJoe Perches				$l =~ s/\\$//;
49815207649bSJoe Perches				if ($first) {
49825207649bSJoe Perches					$define_stmt = $l;
49835207649bSJoe Perches					$first = 0;
49845207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
49855207649bSJoe Perches					$define_stmt .= substr($l, 1);
49865207649bSJoe Perches				}
49875207649bSJoe Perches			}
49885207649bSJoe Perches			$define_stmt =~ s/$;//g;
49895207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
49905207649bSJoe Perches			$define_stmt = trim($define_stmt);
49915207649bSJoe Perches
4992f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
4993f59b64bfSJoe Perches			foreach my $arg (@def_args) {
4994f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
49959192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
49967fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
49977fe528a2SJoe Perches				$tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
49987fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
49997fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
50007fe528a2SJoe Perches				my $use_cnt = $tmp_stmt =~ s/\b$arg\b//g;
5001f59b64bfSJoe Perches				if ($use_cnt > 1) {
5002f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5003f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5004f59b64bfSJoe Perches				    }
50059192d41aSJoe Perches# check if any macro arguments may have other precedence issues
50067fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
50079192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
50089192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
50099192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
50109192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
50119192d41aSJoe Perches				}
50120a920b5bSAndy Whitcroft			}
50135023d347SJoe Perches
501408a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
501508a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
501608a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
501708a2843eSJoe Perches				my $herectx = $here . "\n";
501808a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
501908a2843eSJoe Perches
502008a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
502108a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
502208a2843eSJoe Perches				}
502308a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
502408a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
502508a2843eSJoe Perches			}
502608a2843eSJoe Perches
5027481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
50285023d347SJoe Perches
50295023d347SJoe Perches		} else {
50305023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5031481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5032481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
50335023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
50345023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
50355023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
50365023d347SJoe Perches			}
5037653d4876SAndy Whitcroft		}
50380a920b5bSAndy Whitcroft
5039b13edf7fSJoe Perches# do {} while (0) macro tests:
5040b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5041b13edf7fSJoe Perches# macro should not end with a semicolon
5042b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5043b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5044b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5045b13edf7fSJoe Perches			my $ln = $linenr;
5046b13edf7fSJoe Perches			my $cnt = $realcnt;
5047b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5048b13edf7fSJoe Perches			my $ctx = '';
5049b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5050b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5051b13edf7fSJoe Perches			$ctx = $dstat;
5052b13edf7fSJoe Perches
5053b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
50541b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5055b13edf7fSJoe Perches
5056b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5057b13edf7fSJoe Perches				my $stmts = $2;
5058b13edf7fSJoe Perches				my $semis = $3;
5059b13edf7fSJoe Perches
5060b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5061b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5062b13edf7fSJoe Perches				my $herectx = $here . "\n";
5063b13edf7fSJoe Perches
5064b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
5065b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
5066b13edf7fSJoe Perches				}
5067b13edf7fSJoe Perches
5068ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5069ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5070b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5071b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5072b13edf7fSJoe Perches				}
5073b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5074b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5075b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5076b13edf7fSJoe Perches				}
5077f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5078f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5079f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5080f5ef95b1SJoe Perches				my $herectx = $here . "\n";
5081f5ef95b1SJoe Perches
5082f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
5083f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
5084f5ef95b1SJoe Perches				}
5085f5ef95b1SJoe Perches
5086f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5087f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5088b13edf7fSJoe Perches			}
5089b13edf7fSJoe Perches		}
5090b13edf7fSJoe Perches
5091080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5092080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
5093080ba929SMike Frysinger#	.
5094080ba929SMike Frysinger#	ALIGN(...)
5095080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
5096080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
5097000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
5098000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
5099080ba929SMike Frysinger		}
5100080ba929SMike Frysinger
5101f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
510213214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
510313214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5104cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
510513214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5106cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5107cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5108aad4f614SJoe Perches				my @allowed = ();
5109aad4f614SJoe Perches				my $allow = 0;
511013214adfSAndy Whitcroft				my $seen = 0;
5111773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5112cf655043SAndy Whitcroft				my $ln = $linenr - 1;
511313214adfSAndy Whitcroft				for my $chunk (@chunks) {
511413214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
511513214adfSAndy Whitcroft
5116773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5117773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5118773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5119773647a0SAndy Whitcroft
5120aad4f614SJoe Perches					$allowed[$allow] = 0;
5121773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5122773647a0SAndy Whitcroft
5123773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5124773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5125773647a0SAndy Whitcroft
5126773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5127cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5128cf655043SAndy Whitcroft
5129773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
513013214adfSAndy Whitcroft
513113214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
513213214adfSAndy Whitcroft
5133aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5134cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5135cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5136aad4f614SJoe Perches						$allowed[$allow] = 1;
513713214adfSAndy Whitcroft					}
513813214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5139cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5140aad4f614SJoe Perches						$allowed[$allow] = 1;
514113214adfSAndy Whitcroft					}
5142cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5143cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5144aad4f614SJoe Perches						$allowed[$allow] = 1;
514513214adfSAndy Whitcroft					}
5146aad4f614SJoe Perches					$allow++;
514713214adfSAndy Whitcroft				}
5148aad4f614SJoe Perches				if ($seen) {
5149aad4f614SJoe Perches					my $sum_allowed = 0;
5150aad4f614SJoe Perches					foreach (@allowed) {
5151aad4f614SJoe Perches						$sum_allowed += $_;
5152aad4f614SJoe Perches					}
5153aad4f614SJoe Perches					if ($sum_allowed == 0) {
5154000d1cc1SJoe Perches						WARN("BRACES",
5155000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5156aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5157aad4f614SJoe Perches						 $seen != $allow) {
5158aad4f614SJoe Perches						CHK("BRACES",
5159aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5160aad4f614SJoe Perches					}
516113214adfSAndy Whitcroft				}
516213214adfSAndy Whitcroft			}
516313214adfSAndy Whitcroft		}
5164773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
516513214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5166cf655043SAndy Whitcroft			my $allowed = 0;
5167f0a594c1SAndy Whitcroft
5168cf655043SAndy Whitcroft			# Check the pre-context.
5169cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5170cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5171cf655043SAndy Whitcroft				$allowed = 1;
5172f0a594c1SAndy Whitcroft			}
5173773647a0SAndy Whitcroft
5174773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5175773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5176773647a0SAndy Whitcroft
5177cf655043SAndy Whitcroft			# Check the condition.
5178cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5179773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5180cf655043SAndy Whitcroft			if (defined $cond) {
5181773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5182cf655043SAndy Whitcroft			}
5183cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5184cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5185cf655043SAndy Whitcroft				$allowed = 1;
5186cf655043SAndy Whitcroft			}
5187cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5188cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5189cf655043SAndy Whitcroft				$allowed = 1;
5190cf655043SAndy Whitcroft			}
5191cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5192cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5193cf655043SAndy Whitcroft				$allowed = 1;
5194cf655043SAndy Whitcroft			}
5195cf655043SAndy Whitcroft			# Check the post-context.
5196cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5197cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5198cf655043SAndy Whitcroft				if (defined $cond) {
5199773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5200cf655043SAndy Whitcroft				}
5201cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5202cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5203cf655043SAndy Whitcroft					$allowed = 1;
5204cf655043SAndy Whitcroft				}
5205cf655043SAndy Whitcroft			}
5206cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
520769932487SJustin P. Mattock				my $herectx = $here . "\n";
5208f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5209cf655043SAndy Whitcroft
5210f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
521169932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
5212cf655043SAndy Whitcroft				}
5213cf655043SAndy Whitcroft
5214000d1cc1SJoe Perches				WARN("BRACES",
5215000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5216f0a594c1SAndy Whitcroft			}
5217f0a594c1SAndy Whitcroft		}
5218f0a594c1SAndy Whitcroft
5219e4c5babdSJoe Perches# check for single line unbalanced braces
522095330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
522195330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5222e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5223e4c5babdSJoe Perches		}
5224e4c5babdSJoe Perches
52250979ae66SJoe Perches# check for unnecessary blank lines around braces
522677b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5227f8e58219SJoe Perches			if (CHK("BRACES",
5228f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5229f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5230f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5231f8e58219SJoe Perches			}
52320979ae66SJoe Perches		}
523377b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5234f8e58219SJoe Perches			if (CHK("BRACES",
5235f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5236f8e58219SJoe Perches			    $fix) {
5237f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5238f8e58219SJoe Perches			}
52390979ae66SJoe Perches		}
52400979ae66SJoe Perches
52414a0df2efSAndy Whitcroft# no volatiles please
52426c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
52436c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5244000d1cc1SJoe Perches			WARN("VOLATILE",
52458c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
52464a0df2efSAndy Whitcroft		}
52474a0df2efSAndy Whitcroft
52485e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
52495e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
52505e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
52515e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
525233acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
52535e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
52545e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
52555e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
52565e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
52575e4f6ba5SJoe Perches				     $fix &&
52585e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
52595e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
52605e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
52615e4f6ba5SJoe Perches				my $comma_close = "";
52625e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
52635e4f6ba5SJoe Perches					$comma_close = $1;
52645e4f6ba5SJoe Perches				}
52655e4f6ba5SJoe Perches
52665e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
52675e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
52685e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
52695e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
52705e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
52715e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
52725e4f6ba5SJoe Perches				$fixedline = $rawline;
52735e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
52745e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
52755e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
52765e4f6ba5SJoe Perches				}
52775e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
52785e4f6ba5SJoe Perches			}
52795e4f6ba5SJoe Perches		}
52805e4f6ba5SJoe Perches
52815e4f6ba5SJoe Perches# check for missing a space in a string concatenation
52825e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
52835e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
52845e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
52855e4f6ba5SJoe Perches		}
52865e4f6ba5SJoe Perches
528777cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5288e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5289e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5290e4b7d309SJoe Perches# function declarations
529177cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
529277cb8546SJoe Perches		    defined($context_function) &&
5293e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5294e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
529577cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5296e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
529777cb8546SJoe Perches		}
529877cb8546SJoe Perches
52995e4f6ba5SJoe Perches# check for spaces before a quoted newline
53005e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
53015e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
53025e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
53035e4f6ba5SJoe Perches			    $fix) {
53045e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
53055e4f6ba5SJoe Perches			}
53065e4f6ba5SJoe Perches
53075e4f6ba5SJoe Perches		}
53085e4f6ba5SJoe Perches
5309f17dba4fSJoe Perches# concatenated string without spaces between elements
531033acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5311f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
5312f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
5313f17dba4fSJoe Perches		}
5314f17dba4fSJoe Perches
531590ad30e5SJoe Perches# uncoalesced string fragments
531633acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
531790ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
531890ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
531990ad30e5SJoe Perches		}
532090ad30e5SJoe Perches
5321522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5322522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5323522b837cSAlexey Dobriyan		my $show_Z = 1;
53245e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5325522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
53265e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5327522b837cSAlexey Dobriyan			# check for %L
5328522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
53295e4f6ba5SJoe Perches				WARN("PRINTF_L",
5330522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5331522b837cSAlexey Dobriyan				$show_L = 0;
53325e4f6ba5SJoe Perches			}
5333522b837cSAlexey Dobriyan			# check for %Z
5334522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5335522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5336522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5337522b837cSAlexey Dobriyan				$show_Z = 0;
5338522b837cSAlexey Dobriyan			}
5339522b837cSAlexey Dobriyan			# check for 0x<decimal>
5340522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5341522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
53426e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
53436e300757SJoe Perches			}
53445e4f6ba5SJoe Perches		}
53455e4f6ba5SJoe Perches
53465e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
53473f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
53485e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
53495e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
53505e4f6ba5SJoe Perches		}
53515e4f6ba5SJoe Perches
535200df344fSAndy Whitcroft# warn about #if 0
5353c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5354000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
5355000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
5356de7d4f0eSAndy Whitcroft				$herecurr);
53574a0df2efSAndy Whitcroft		}
53584a0df2efSAndy Whitcroft
535903df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
536003df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5361100425deSJoe Perches			my $tested = quotemeta($1);
5362100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5363100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5364100425deSJoe Perches				my $func = $1;
5365100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5366100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5367100425deSJoe Perches				    $fix) {
5368100425deSJoe Perches					my $do_fix = 1;
5369100425deSJoe Perches					my $leading_tabs = "";
5370100425deSJoe Perches					my $new_leading_tabs = "";
5371100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5372100425deSJoe Perches						$leading_tabs = $1;
5373100425deSJoe Perches					} else {
5374100425deSJoe Perches						$do_fix = 0;
5375100425deSJoe Perches					}
5376100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5377100425deSJoe Perches						$new_leading_tabs = $1;
5378100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5379100425deSJoe Perches							$do_fix = 0;
5380100425deSJoe Perches						}
5381100425deSJoe Perches					} else {
5382100425deSJoe Perches						$do_fix = 0;
5383100425deSJoe Perches					}
5384100425deSJoe Perches					if ($do_fix) {
5385100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5386100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5387100425deSJoe Perches					}
5388100425deSJoe Perches				}
53894c432a8fSGreg Kroah-Hartman			}
53904c432a8fSGreg Kroah-Hartman		}
5391f0a594c1SAndy Whitcroft
5392ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5393ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5394ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5395ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5396ebfdc409SJoe Perches		    $linenr > 3) {
5397ebfdc409SJoe Perches			my $testval = $2;
5398ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5399ebfdc409SJoe Perches
5400ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5401ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5402ebfdc409SJoe Perches
5403fb0d0e08SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
5404ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5405ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5406ebfdc409SJoe Perches			}
5407ebfdc409SJoe Perches		}
5408ebfdc409SJoe Perches
5409f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5410dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5411f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5412f78d98f6SJoe Perches			my $level = $1;
5413f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5414f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5415f78d98f6SJoe Perches			    $fix) {
5416f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5417f78d98f6SJoe Perches			}
5418f78d98f6SJoe Perches		}
5419f78d98f6SJoe Perches
542045c55e92SJoe Perches# check for logging continuations
542145c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
542245c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
542345c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
542445c55e92SJoe Perches		}
542545c55e92SJoe Perches
5426abb08a53SJoe Perches# check for mask then right shift without a parentheses
5427abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5428abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5429abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5430abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5431abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5432abb08a53SJoe Perches		}
5433abb08a53SJoe Perches
5434b75ac618SJoe Perches# check for pointer comparisons to NULL
5435b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
5436b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5437b75ac618SJoe Perches				my $val = $1;
5438b75ac618SJoe Perches				my $equal = "!";
5439b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5440b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5441b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5442b75ac618SJoe Perches					    $fix) {
5443b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5444b75ac618SJoe Perches				}
5445b75ac618SJoe Perches			}
5446b75ac618SJoe Perches		}
5447b75ac618SJoe Perches
54488716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
54498716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
54508716de38SJoe Perches			my $attr = $1;
54518716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
54528716de38SJoe Perches				my $ptr = $1;
54538716de38SJoe Perches				my $var = $2;
54548716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
54558716de38SJoe Perches				      ERROR("MISPLACED_INIT",
54568716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
54578716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
54588716de38SJoe Perches				      WARN("MISPLACED_INIT",
54598716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
54608716de38SJoe Perches				    $fix) {
5461194f66fcSJoe 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;
54628716de38SJoe Perches				}
54638716de38SJoe Perches			}
54648716de38SJoe Perches		}
54658716de38SJoe Perches
5466e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5467e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5468e970b884SJoe Perches			my $attr = $1;
5469e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5470e970b884SJoe Perches			my $attr_prefix = $1;
5471e970b884SJoe Perches			my $attr_type = $2;
5472e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5473e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5474e970b884SJoe Perches			    $fix) {
5475194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5476e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5477e970b884SJoe Perches			}
5478e970b884SJoe Perches		}
5479e970b884SJoe Perches
5480e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5481e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5482e970b884SJoe Perches			my $attr = $1;
5483e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5484e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5485e970b884SJoe Perches			    $fix) {
5486194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5487e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5488e970b884SJoe Perches				$lead = rtrim($1);
5489e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5490e970b884SJoe Perches				$lead = "${lead}const ";
5491194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5492e970b884SJoe Perches			}
5493e970b884SJoe Perches		}
5494e970b884SJoe Perches
5495c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5496c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5497c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5498c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5499c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5500c17893c7SJoe Perches			    $fix) {
5501c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5502c17893c7SJoe Perches			}
5503c17893c7SJoe Perches		}
5504c17893c7SJoe Perches
5505fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5506fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5507fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5508fbdb8138SJoe Perches			my $constant_func = $1;
5509fbdb8138SJoe Perches			my $func = $constant_func;
5510fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5511fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5512fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5513fbdb8138SJoe Perches			    $fix) {
5514194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5515fbdb8138SJoe Perches			}
5516fbdb8138SJoe Perches		}
5517fbdb8138SJoe Perches
55181a15a250SPatrick Pannuto# prefer usleep_range over udelay
551937581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
552043c1d77cSJoe Perches			my $delay = $1;
55211a15a250SPatrick Pannuto			# ignore udelay's < 10, however
552243c1d77cSJoe Perches			if (! ($delay < 10) ) {
5523000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
552443c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
552543c1d77cSJoe Perches			}
552643c1d77cSJoe Perches			if ($delay > 2000) {
552743c1d77cSJoe Perches				WARN("LONG_UDELAY",
552843c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
55291a15a250SPatrick Pannuto			}
55301a15a250SPatrick Pannuto		}
55311a15a250SPatrick Pannuto
553209ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
553309ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
553409ef8725SPatrick Pannuto			if ($1 < 20) {
5535000d1cc1SJoe Perches				WARN("MSLEEP",
553643c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
553709ef8725SPatrick Pannuto			}
553809ef8725SPatrick Pannuto		}
553909ef8725SPatrick Pannuto
554036ec1939SJoe Perches# check for comparisons of jiffies
554136ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
554236ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
554336ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
554436ec1939SJoe Perches		}
554536ec1939SJoe Perches
55469d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
55479d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
55489d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
55499d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
55509d7a34a5SJoe Perches		}
55519d7a34a5SJoe Perches
555200df344fSAndy Whitcroft# warn about #ifdefs in C files
5553c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
555400df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
555500df344fSAndy Whitcroft#			print "$herecurr";
555600df344fSAndy Whitcroft#			$clean = 0;
555700df344fSAndy Whitcroft#		}
555800df344fSAndy Whitcroft
555922f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5560c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
55613705ce5bSJoe Perches			if (ERROR("SPACING",
55623705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
55633705ce5bSJoe Perches			    $fix) {
5564194f66fcSJoe Perches				$fixed[$fixlinenr] =~
55653705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
55663705ce5bSJoe Perches			}
55673705ce5bSJoe Perches
556822f2a2efSAndy Whitcroft		}
556922f2a2efSAndy Whitcroft
55704a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5571171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5572171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
55734a0df2efSAndy Whitcroft			my $which = $1;
55744a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5575000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5576000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
55774a0df2efSAndy Whitcroft			}
55784a0df2efSAndy Whitcroft		}
55794a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5580402c2553SMichael S. Tsirkin
5581402c2553SMichael S. Tsirkin		my $barriers = qr{
5582402c2553SMichael S. Tsirkin			mb|
5583402c2553SMichael S. Tsirkin			rmb|
5584402c2553SMichael S. Tsirkin			wmb|
5585402c2553SMichael S. Tsirkin			read_barrier_depends
5586402c2553SMichael S. Tsirkin		}x;
5587402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5588402c2553SMichael S. Tsirkin			mb__before_atomic|
5589402c2553SMichael S. Tsirkin			mb__after_atomic|
5590402c2553SMichael S. Tsirkin			store_release|
5591402c2553SMichael S. Tsirkin			load_acquire|
5592402c2553SMichael S. Tsirkin			store_mb|
5593402c2553SMichael S. Tsirkin			(?:$barriers)
5594402c2553SMichael S. Tsirkin		}x;
5595402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5596402c2553SMichael S. Tsirkin			(?:$barriers)|
559743e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
559843e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5599402c2553SMichael S. Tsirkin		}x;
5600402c2553SMichael S. Tsirkin
5601402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
56024a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5603c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5604000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
56054a0df2efSAndy Whitcroft			}
56064a0df2efSAndy Whitcroft		}
56073ad81779SPaul E. McKenney
5608f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5609f4073b0fSMichael S. Tsirkin
5610f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5611f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5612f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5613f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5614f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5615f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5616f4073b0fSMichael S. Tsirkin		}
5617f4073b0fSMichael S. Tsirkin
5618cb426e99SJoe Perches# check for waitqueue_active without a comment.
5619cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5620cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5621cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5622cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5623cb426e99SJoe Perches			}
5624cb426e99SJoe Perches		}
56253ad81779SPaul E. McKenney
562691db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
562791db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
562891db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
562991db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
563091db2592SPaul E. McKenney		}
563191db2592SPaul E. McKenney
56324a0df2efSAndy Whitcroft# check of hardware specific defines
5633c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5634000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5635000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
56360a920b5bSAndy Whitcroft		}
5637653d4876SAndy Whitcroft
5638596ed45bSJoe Perches# check that the storage class is not after a type
5639596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5640000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5641596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5642596ed45bSJoe Perches		}
5643596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5644596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5645596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5646596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5647596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5648596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5649596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5650d4977c78STobias Klauser		}
5651d4977c78STobias Klauser
5652de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5653de7d4f0eSAndy Whitcroft# storage class and type.
56549c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
56559c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5656000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5657000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5658de7d4f0eSAndy Whitcroft		}
5659de7d4f0eSAndy Whitcroft
56608905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
56612b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56622b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5663d5e616fcSJoe Perches			if (WARN("INLINE",
5664d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5665d5e616fcSJoe Perches			    $fix) {
5666194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5667d5e616fcSJoe Perches
5668d5e616fcSJoe Perches			}
56698905a67cSAndy Whitcroft		}
56708905a67cSAndy Whitcroft
56713d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
56722b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56732b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5674000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5675000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
56763d130fd0SJoe Perches		}
56773d130fd0SJoe Perches
567839b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
56792b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56802b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5681000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5682000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
568339b7e287SJoe Perches		}
568439b7e287SJoe Perches
56855f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
56862b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56872b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5688d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5689d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5690d5e616fcSJoe Perches			    $fix) {
5691194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5692d5e616fcSJoe Perches
5693d5e616fcSJoe Perches			}
56945f14d3bdSJoe Perches		}
56955f14d3bdSJoe Perches
56966061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
56972b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56982b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5699d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5700d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5701d5e616fcSJoe Perches			    $fix) {
5702194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5703d5e616fcSJoe Perches			}
57046061d949SJoe Perches		}
57056061d949SJoe Perches
5706619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5707619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5708619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5709619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5710619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5711619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5712619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5713619a908aSJoe Perches		}
5714619a908aSJoe Perches
5715fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5716e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5717fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5718e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5719e6176fa4SJoe Perches			my $type = $1;
5720e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5721e6176fa4SJoe Perches				$type = $1;
5722e6176fa4SJoe Perches				my $kernel_type = 'u';
5723e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5724e6176fa4SJoe Perches				$type =~ /(\d+)/;
5725e6176fa4SJoe Perches				$kernel_type .= $1;
5726e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5727e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5728e6176fa4SJoe Perches				    $fix) {
5729e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5730e6176fa4SJoe Perches				}
5731e6176fa4SJoe Perches			}
5732e6176fa4SJoe Perches		}
5733e6176fa4SJoe Perches
5734938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5735938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5736938224b5SJoe Perches			my $cast = $1;
5737938224b5SJoe Perches			my $const = $2;
5738938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5739938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5740938224b5SJoe Perches			    $fix) {
5741938224b5SJoe Perches				my $suffix = "";
5742938224b5SJoe Perches				my $newconst = $const;
5743938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5744938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5745938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5746938224b5SJoe Perches					$suffix .= 'LL';
5747938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5748938224b5SJoe Perches					$suffix .= 'L';
5749938224b5SJoe Perches				}
5750938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5751938224b5SJoe Perches			}
5752938224b5SJoe Perches		}
5753938224b5SJoe Perches
57548f53a9b8SJoe Perches# check for sizeof(&)
57558f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5756000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5757000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
57588f53a9b8SJoe Perches		}
57598f53a9b8SJoe Perches
576066c80b60SJoe Perches# check for sizeof without parenthesis
576166c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5762d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5763d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5764d5e616fcSJoe Perches			    $fix) {
5765194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5766d5e616fcSJoe Perches			}
576766c80b60SJoe Perches		}
576866c80b60SJoe Perches
576988982feaSJoe Perches# check for struct spinlock declarations
577088982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
577188982feaSJoe Perches			WARN("USE_SPINLOCK_T",
577288982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
577388982feaSJoe Perches		}
577488982feaSJoe Perches
5775a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
577606668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5777a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5778caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5779caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5780d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5781d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5782d5e616fcSJoe Perches				    $fix) {
5783194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5784d5e616fcSJoe Perches				}
5785a6962d72SJoe Perches			}
5786a6962d72SJoe Perches		}
5787a6962d72SJoe Perches
57880b523769SJoe Perches		# check for vsprintf extension %p<foo> misuses
57890b523769SJoe Perches		if ($^V && $^V ge 5.10.0 &&
57900b523769SJoe Perches		    defined $stat &&
57910b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
57920b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
57930b523769SJoe Perches			my $bad_extension = "";
57940b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
57950b523769SJoe Perches			$lc = $lc + $linenr;
57960b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
57970b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
57980b523769SJoe Perches				$fmt =~ s/%%//g;
5799ab486bc9SLinus Torvalds				if ($fmt =~ /(\%[\*\d\.]*p(?![\WSsBKRraEhMmIiUDdgVCbGNOx]).)/) {
58000b523769SJoe Perches					$bad_extension = $1;
58010b523769SJoe Perches					last;
58020b523769SJoe Perches				}
58030b523769SJoe Perches			}
58040b523769SJoe Perches			if ($bad_extension ne "") {
58050b523769SJoe Perches				my $stat_real = raw_line($linenr, 0);
58061df7338aSSergey Senozhatsky				my $ext_type = "Invalid";
58071df7338aSSergey Senozhatsky				my $use = "";
58080b523769SJoe Perches				for (my $count = $linenr + 1; $count <= $lc; $count++) {
58090b523769SJoe Perches					$stat_real = $stat_real . "\n" . raw_line($count, 0);
58100b523769SJoe Perches				}
58111df7338aSSergey Senozhatsky				if ($bad_extension =~ /p[Ff]/) {
58121df7338aSSergey Senozhatsky					$ext_type = "Deprecated";
58131df7338aSSergey Senozhatsky					$use = " - use %pS instead";
58141df7338aSSergey Senozhatsky					$use =~ s/pS/ps/ if ($bad_extension =~ /pf/);
58151df7338aSSergey Senozhatsky				}
58160b523769SJoe Perches				WARN("VSPRINTF_POINTER_EXTENSION",
58171df7338aSSergey Senozhatsky				     "$ext_type vsprintf pointer extension '$bad_extension'$use\n" . "$here\n$stat_real\n");
58180b523769SJoe Perches			}
58190b523769SJoe Perches		}
58200b523769SJoe Perches
5821554e165cSAndy Whitcroft# Check for misused memsets
5822d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5823d1fe9c09SJoe Perches		    defined $stat &&
58249e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5825554e165cSAndy Whitcroft
5826d7c76ba7SJoe Perches			my $ms_addr = $2;
5827d1fe9c09SJoe Perches			my $ms_val = $7;
5828d1fe9c09SJoe Perches			my $ms_size = $12;
5829d7c76ba7SJoe Perches
5830554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5831554e165cSAndy Whitcroft				ERROR("MEMSET",
5832d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5833554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5834554e165cSAndy Whitcroft				WARN("MEMSET",
5835d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5836d7c76ba7SJoe Perches			}
5837d7c76ba7SJoe Perches		}
5838d7c76ba7SJoe Perches
583998a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5840f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5841f333195dSJoe Perches#		    defined $stat &&
5842f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5843f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
5844f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5845f333195dSJoe Perches#			    $fix) {
5846f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5847f333195dSJoe Perches#			}
5848f333195dSJoe Perches#		}
584998a9bba5SJoe Perches
5850b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5851f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5852f333195dSJoe Perches#		    defined $stat &&
5853f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5854f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
5855f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5856f333195dSJoe Perches#		}
5857b6117d17SMateusz Kulikowski
58588617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
58598617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5860f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5861f333195dSJoe Perches#		    defined $stat &&
5862f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5863f333195dSJoe Perches#
5864f333195dSJoe Perches#			my $ms_val = $7;
5865f333195dSJoe Perches#
5866f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
5867f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
5868f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5869f333195dSJoe Perches#				    $fix) {
5870f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5871f333195dSJoe Perches#				}
5872f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5873f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
5874f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5875f333195dSJoe Perches#				    $fix) {
5876f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5877f333195dSJoe Perches#				}
5878f333195dSJoe Perches#			}
5879f333195dSJoe Perches#		}
58808617cd09SMateusz Kulikowski
5881d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5882d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5883d1fe9c09SJoe Perches		    defined $stat &&
5884d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5885d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5886d7c76ba7SJoe Perches				my $call = $1;
5887d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5888d7c76ba7SJoe Perches				my $arg1 = $3;
5889d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5890d1fe9c09SJoe Perches				my $arg2 = $8;
5891d7c76ba7SJoe Perches				my $cast;
5892d7c76ba7SJoe Perches
5893d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5894d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5895d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5896d7c76ba7SJoe Perches					$cast = $cast1;
5897d7c76ba7SJoe Perches				} else {
5898d7c76ba7SJoe Perches					$cast = $cast2;
5899d7c76ba7SJoe Perches				}
5900d7c76ba7SJoe Perches				WARN("MINMAX",
5901d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5902554e165cSAndy Whitcroft			}
5903554e165cSAndy Whitcroft		}
5904554e165cSAndy Whitcroft
59054a273195SJoe Perches# check usleep_range arguments
59064a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
59074a273195SJoe Perches		    defined $stat &&
59084a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
59094a273195SJoe Perches			my $min = $1;
59104a273195SJoe Perches			my $max = $7;
59114a273195SJoe Perches			if ($min eq $max) {
59124a273195SJoe Perches				WARN("USLEEP_RANGE",
59134a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
59144a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
59154a273195SJoe Perches				 $min > $max) {
59164a273195SJoe Perches				WARN("USLEEP_RANGE",
59174a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
59184a273195SJoe Perches			}
59194a273195SJoe Perches		}
59204a273195SJoe Perches
5921823b794cSJoe Perches# check for naked sscanf
5922823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5923823b794cSJoe Perches		    defined $stat &&
59246c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5925823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5926823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5927823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5928823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5929823b794cSJoe Perches			$lc = $lc + $linenr;
5930823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5931823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5932823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5933823b794cSJoe Perches			}
5934823b794cSJoe Perches			WARN("NAKED_SSCANF",
5935823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5936823b794cSJoe Perches		}
5937823b794cSJoe Perches
5938afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5939afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5940afc819abSJoe Perches		    defined $stat &&
5941afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5942afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5943afc819abSJoe Perches			$lc = $lc + $linenr;
5944afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5945afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5946afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5947afc819abSJoe Perches			}
5948afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5949afc819abSJoe Perches				my $format = $6;
5950afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5951afc819abSJoe Perches				if ($count == 1 &&
5952afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5953afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5954afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5955afc819abSJoe Perches				}
5956afc819abSJoe Perches			}
5957afc819abSJoe Perches		}
5958afc819abSJoe Perches
595970dc8a48SJoe Perches# check for new externs in .h files.
596070dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
596170dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5962d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
596370dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
596470dc8a48SJoe Perches			    $fix) {
5965194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
596670dc8a48SJoe Perches			}
596770dc8a48SJoe Perches		}
596870dc8a48SJoe Perches
5969de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5970171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5971c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5972171ae1a4SAndy Whitcroft		{
5973c45dcabdSAndy Whitcroft			my $function_name = $1;
5974c45dcabdSAndy Whitcroft			my $paren_space = $2;
5975171ae1a4SAndy Whitcroft
5976171ae1a4SAndy Whitcroft			my $s = $stat;
5977171ae1a4SAndy Whitcroft			if (defined $cond) {
5978171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5979171ae1a4SAndy Whitcroft			}
5980c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5981c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5982c45dcabdSAndy Whitcroft			{
5983000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5984000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5985de7d4f0eSAndy Whitcroft			}
5986de7d4f0eSAndy Whitcroft
5987171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5988000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5989000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5990171ae1a4SAndy Whitcroft			}
59919c9ba34eSAndy Whitcroft
59929c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
59939c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
59949c9ba34eSAndy Whitcroft		{
5995000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5996000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5997171ae1a4SAndy Whitcroft		}
5998171ae1a4SAndy Whitcroft
5999a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6000a0ad7596SJoe Perches		if (defined $stat &&
600125bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6002ca0d8929SJoe Perches		    $1 ne "void") {
6003ca0d8929SJoe Perches			my $args = trim($1);
6004ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6005ca0d8929SJoe Perches				my $arg = trim($1);
6006ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6007ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6008ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6009ca0d8929SJoe Perches				}
6010ca0d8929SJoe Perches			}
6011ca0d8929SJoe Perches		}
6012ca0d8929SJoe Perches
6013a0ad7596SJoe Perches# check for function definitions
6014a0ad7596SJoe Perches		if ($^V && $^V ge 5.10.0 &&
6015a0ad7596SJoe Perches		    defined $stat &&
6016a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6017a0ad7596SJoe Perches			$context_function = $1;
6018a0ad7596SJoe Perches
6019a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6020a0ad7596SJoe Perches			my $ok = 0;
6021a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6022a0ad7596SJoe Perches			my $herectx = $here . "\n";
6023a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6024a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6025a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6026a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6027a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6028a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6029a0ad7596SJoe Perches			}
6030a0ad7596SJoe Perches			if (!$ok) {
6031a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6032a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6033a0ad7596SJoe Perches			}
6034a0ad7596SJoe Perches		}
6035a0ad7596SJoe Perches
6036de7d4f0eSAndy Whitcroft# checks for new __setup's
6037de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6038de7d4f0eSAndy Whitcroft			my $name = $1;
6039de7d4f0eSAndy Whitcroft
6040de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6041000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
60428c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6043de7d4f0eSAndy Whitcroft			}
6044653d4876SAndy Whitcroft		}
60459c0ca6f9SAndy Whitcroft
60469c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
6047caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
6048000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6049000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
60509c0ca6f9SAndy Whitcroft		}
605113214adfSAndy Whitcroft
6052a640d25cSJoe Perches# alloc style
6053a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6054a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6055a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6056a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6057a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6058a640d25cSJoe Perches		}
6059a640d25cSJoe Perches
606060a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
606160a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
60621b4a2ed4SJoe Perches		    defined $stat &&
60631b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
606460a55369SJoe Perches			my $oldfunc = $3;
606560a55369SJoe Perches			my $a1 = $4;
606660a55369SJoe Perches			my $a2 = $10;
606760a55369SJoe Perches			my $newfunc = "kmalloc_array";
606860a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
606960a55369SJoe Perches			my $r1 = $a1;
607060a55369SJoe Perches			my $r2 = $a2;
607160a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
607260a55369SJoe Perches				$r1 = $a2;
607360a55369SJoe Perches				$r2 = $a1;
607460a55369SJoe Perches			}
6075e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6076e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
60771b4a2ed4SJoe Perches				my $ctx = '';
60781b4a2ed4SJoe Perches				my $herectx = $here . "\n";
60791b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
60801b4a2ed4SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
60811b4a2ed4SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
60821b4a2ed4SJoe Perches				}
6083e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
60841b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
60851b4a2ed4SJoe Perches				    $cnt == 1 &&
6086e367455aSJoe Perches				    $fix) {
6087194f66fcSJoe 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;
608860a55369SJoe Perches				}
608960a55369SJoe Perches			}
609060a55369SJoe Perches		}
609160a55369SJoe Perches
6092972fdea2SJoe Perches# check for krealloc arg reuse
6093972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
6094972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6095972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6096972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6097972fdea2SJoe Perches		}
6098972fdea2SJoe Perches
60995ce59ae0SJoe Perches# check for alloc argument mismatch
61005ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
61015ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
61025ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
61035ce59ae0SJoe Perches		}
61045ce59ae0SJoe Perches
6105caf2a54fSJoe Perches# check for multiple semicolons
6106caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6107d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6108d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6109d5e616fcSJoe Perches			    $fix) {
6110194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6111d5e616fcSJoe Perches			}
6112d1e2ad07SJoe Perches		}
6113d1e2ad07SJoe Perches
6114cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6115cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6116cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
61170ab90191SJoe Perches			my $ull = "";
61180ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
61190ab90191SJoe Perches			if (CHK("BIT_MACRO",
61200ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
61210ab90191SJoe Perches			    $fix) {
61220ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
61230ab90191SJoe Perches			}
61240ab90191SJoe Perches		}
61250ab90191SJoe Perches
61262d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
61272d632745SJoe 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*$/) {
61282d632745SJoe Perches			my $config = $1;
61292d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
61302d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
61312d632745SJoe Perches			    $fix) {
61322d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
61332d632745SJoe Perches			}
61342d632745SJoe Perches		}
61352d632745SJoe Perches
6136e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6137c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6138c34c09a8SJoe Perches			my $has_break = 0;
6139c34c09a8SJoe Perches			my $has_statement = 0;
6140c34c09a8SJoe Perches			my $count = 0;
6141c34c09a8SJoe Perches			my $prevline = $linenr;
6142e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6143c34c09a8SJoe Perches				$prevline--;
6144c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6145c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6146c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6147c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6148c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6149c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6150c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6151c34c09a8SJoe Perches				$has_statement = 1;
6152c34c09a8SJoe Perches				$count++;
6153258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6154c34c09a8SJoe Perches			}
6155c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6156c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6157224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6158c34c09a8SJoe Perches			}
6159c34c09a8SJoe Perches		}
6160c34c09a8SJoe Perches
6161d1e2ad07SJoe Perches# check for switch/default statements without a break;
6162d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
6163d1e2ad07SJoe Perches		    defined $stat &&
6164d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6165d1e2ad07SJoe Perches			my $ctx = '';
6166d1e2ad07SJoe Perches			my $herectx = $here . "\n";
6167d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6168d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6169d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
6170d1e2ad07SJoe Perches			}
6171d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6172d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6173caf2a54fSJoe Perches		}
6174caf2a54fSJoe Perches
617513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6176d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6177d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6178d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6179d5e616fcSJoe Perches			    $fix) {
6180194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6181d5e616fcSJoe Perches			}
618213214adfSAndy Whitcroft		}
6183773647a0SAndy Whitcroft
618462ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
618562ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
618662ec818fSJoe Perches			ERROR("DATE_TIME",
618762ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
618862ec818fSJoe Perches		}
618962ec818fSJoe Perches
61902c92488aSJoe Perches# check for use of yield()
61912c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
61922c92488aSJoe Perches			WARN("YIELD",
61932c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
61942c92488aSJoe Perches		}
61952c92488aSJoe Perches
6196179f8f40SJoe Perches# check for comparisons against true and false
6197179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6198179f8f40SJoe Perches			my $lead = $1;
6199179f8f40SJoe Perches			my $arg = $2;
6200179f8f40SJoe Perches			my $test = $3;
6201179f8f40SJoe Perches			my $otype = $4;
6202179f8f40SJoe Perches			my $trail = $5;
6203179f8f40SJoe Perches			my $op = "!";
6204179f8f40SJoe Perches
6205179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6206179f8f40SJoe Perches
6207179f8f40SJoe Perches			my $type = lc($otype);
6208179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6209179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6210179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6211179f8f40SJoe Perches					$op = "";
6212179f8f40SJoe Perches				}
6213179f8f40SJoe Perches
6214179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6215179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6216179f8f40SJoe Perches
6217179f8f40SJoe Perches## maybe suggesting a correct construct would better
6218179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6219179f8f40SJoe Perches
6220179f8f40SJoe Perches			}
6221179f8f40SJoe Perches		}
6222179f8f40SJoe Perches
62234882720bSThomas Gleixner# check for semaphores initialized locked
62244882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6225000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6226000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6227773647a0SAndy Whitcroft		}
62286712d858SJoe Perches
622967d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
623067d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6231000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
623267d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6233773647a0SAndy Whitcroft		}
62346712d858SJoe Perches
6235ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6236f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6237000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6238ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6239f3db6639SMichael Ellerman		}
62406712d858SJoe Perches
62410f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6242d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
62436903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6244d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6245000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6246d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
62472b6db5cbSAndy Whitcroft		}
6248773647a0SAndy Whitcroft
6249773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6250773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6251773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6252c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6253c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6254171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6255171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6256171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6257773647a0SAndy Whitcroft		{
6258000d1cc1SJoe Perches			WARN("NR_CPUS",
6259000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6260773647a0SAndy Whitcroft		}
62619c9ba34eSAndy Whitcroft
626252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
626352ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
626452ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
626552ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
626652ea8506SJoe Perches		}
626752ea8506SJoe Perches
6268acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6269acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6270acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6271acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6272acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6273acd9362cSJoe Perches		}
6274acd9362cSJoe Perches
6275691d77b6SAndy Whitcroft# whine mightly about in_atomic
6276691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6277691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6278000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6279000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6280f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6281000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6282000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6283691d77b6SAndy Whitcroft			}
6284691d77b6SAndy Whitcroft		}
62851704f47bSPeter Zijlstra
62860f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
62870f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
62880f5225b0SPeter Zijlstra			ERROR("LOCKING",
62890f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
62900f5225b0SPeter Zijlstra		}
62910f5225b0SPeter Zijlstra
62921704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
62931704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
62941704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
62951704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
62961704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
62971704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6298000d1cc1SJoe Perches				ERROR("LOCKDEP",
6299000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
63001704f47bSPeter Zijlstra			}
63011704f47bSPeter Zijlstra		}
630288f8831cSDave Jones
6303b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6304b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6305000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6306000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
630788f8831cSDave Jones		}
63082435880fSJoe Perches
630900180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
631000180468SJoe Perches# and whether or not function naming is typical and if
631100180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
631200180468SJoe Perches		if ($^V && $^V ge 5.10.0 &&
631300180468SJoe Perches		    defined $stat &&
631400180468SJoe 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*\)/) {
631500180468SJoe Perches			my $var = $1;
631600180468SJoe Perches			my $perms = $2;
631700180468SJoe Perches			my $show = $3;
631800180468SJoe Perches			my $store = $4;
631900180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
632000180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
632100180468SJoe Perches			    $store =~ /^${var}_store$/ &&
632200180468SJoe Perches			    $octal_perms eq "0644") {
632300180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
632400180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
632500180468SJoe Perches				    $fix) {
632600180468SJoe 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})/;
632700180468SJoe Perches				}
632800180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
632900180468SJoe Perches				 $store =~ /^NULL$/ &&
633000180468SJoe Perches				 $octal_perms eq "0444") {
633100180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
633200180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
633300180468SJoe Perches				    $fix) {
633400180468SJoe 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})/;
633500180468SJoe Perches				}
633600180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
633700180468SJoe Perches				 $store =~ /^${var}_store$/ &&
633800180468SJoe Perches				 $octal_perms eq "0200") {
633900180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
634000180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
634100180468SJoe Perches				    $fix) {
634200180468SJoe 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})/;
634300180468SJoe Perches				}
634400180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
634500180468SJoe Perches				 $octal_perms eq "0444" ||
634600180468SJoe Perches				 $octal_perms eq "0200") {
634700180468SJoe Perches				my $newshow = "$show";
634800180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
634900180468SJoe Perches				my $newstore = $store;
635000180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
635100180468SJoe Perches				my $rename = "";
635200180468SJoe Perches				if ($show ne $newshow) {
635300180468SJoe Perches					$rename .= " '$show' to '$newshow'";
635400180468SJoe Perches				}
635500180468SJoe Perches				if ($store ne $newstore) {
635600180468SJoe Perches					$rename .= " '$store' to '$newstore'";
635700180468SJoe Perches				}
635800180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
635900180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
636000180468SJoe Perches			} else {
636100180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
636200180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
636300180468SJoe Perches			}
636400180468SJoe Perches		}
636500180468SJoe Perches
6366515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6367515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
636873121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
636973121534SJoe Perches#   specific definition of not visible in sysfs.
637073121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
637173121534SJoe Perches#   use the default permissions
6372515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6373459cf0aeSJoe Perches		    defined $stat &&
6374515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
63752435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
63762435880fSJoe Perches				my $func = $entry->[0];
63772435880fSJoe Perches				my $arg_pos = $entry->[1];
63782435880fSJoe Perches
6379459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6380459cf0aeSJoe Perches				$lc = $lc + $linenr;
6381459cf0aeSJoe Perches				my $stat_real = raw_line($linenr, 0);
6382459cf0aeSJoe Perches				for (my $count = $linenr + 1; $count <= $lc; $count++) {
6383459cf0aeSJoe Perches					$stat_real = $stat_real . "\n" . raw_line($count, 0);
6384459cf0aeSJoe Perches				}
6385459cf0aeSJoe Perches
63862435880fSJoe Perches				my $skip_args = "";
63872435880fSJoe Perches				if ($arg_pos > 1) {
63882435880fSJoe Perches					$arg_pos--;
63892435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
63902435880fSJoe Perches				}
6391f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6392459cf0aeSJoe Perches				if ($stat =~ /$test/) {
63932435880fSJoe Perches					my $val = $1;
63942435880fSJoe Perches					$val = $6 if ($skip_args ne "");
639573121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
639673121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
639773121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
63982435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6399459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6400f90774e1SJoe Perches					}
6401f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6402c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6403459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
64042435880fSJoe Perches					}
6405459cf0aeSJoe Perches				}
6406459cf0aeSJoe Perches			}
6407459cf0aeSJoe Perches		}
6408459cf0aeSJoe Perches
6409459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
641000180468SJoe Perches		if ($line =~ /\b($multi_mode_perms_string_search)\b/) {
641100180468SJoe Perches			my $oval = $1;
641200180468SJoe Perches			my $octal = perms_to_octal($oval);
6413f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6414459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6415f90774e1SJoe Perches			    $fix) {
641600180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
64172435880fSJoe Perches			}
641813214adfSAndy Whitcroft		}
64195a6d20ceSBjorn Andersson
64205a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
64215a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
64225a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
64235a6d20ceSBjorn Andersson			my $valid_licenses = qr{
64245a6d20ceSBjorn Andersson						GPL|
64255a6d20ceSBjorn Andersson						GPL\ v2|
64265a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
64275a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
64285a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
64295a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
64305a6d20ceSBjorn Andersson						Proprietary
64315a6d20ceSBjorn Andersson					}x;
64325a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
64335a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
64345a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
64355a6d20ceSBjorn Andersson			}
64365a6d20ceSBjorn Andersson		}
6437515a235eSJoe Perches	}
643813214adfSAndy Whitcroft
643913214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
644013214adfSAndy Whitcroft	# so just keep quiet.
644113214adfSAndy Whitcroft	if ($#rawlines == -1) {
644213214adfSAndy Whitcroft		exit(0);
64430a920b5bSAndy Whitcroft	}
64440a920b5bSAndy Whitcroft
64458905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
64468905a67cSAndy Whitcroft	# things that appear to be patches.
64478905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
64488905a67cSAndy Whitcroft		exit(0);
64498905a67cSAndy Whitcroft	}
64508905a67cSAndy Whitcroft
64518905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
64528905a67cSAndy Whitcroft	# just keep quiet.
64538905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
64548905a67cSAndy Whitcroft		exit(0);
64558905a67cSAndy Whitcroft	}
64568905a67cSAndy Whitcroft
6457a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6458000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6459000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
64600a920b5bSAndy Whitcroft	}
6461ed43c4e5SAllen Hubbe	if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6462000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
6463000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
64640a920b5bSAndy Whitcroft	}
64650a920b5bSAndy Whitcroft
6466f0a594c1SAndy Whitcroft	print report_dump();
646713214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
646813214adfSAndy Whitcroft		print "$filename " if ($summary_file);
64696c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
64706c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
64716c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
64726c72ffaaSAndy Whitcroft	}
64738905a67cSAndy Whitcroft
6474d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6475ef212196SJoe Perches		# If there were any defects found and not already fixing them
6476ef212196SJoe Perches		if (!$clean and !$fix) {
6477ef212196SJoe Perches			print << "EOM"
6478ef212196SJoe Perches
6479ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6480ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6481ef212196SJoe PerchesEOM
6482ef212196SJoe Perches		}
6483d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6484d2c0a235SAndy Whitcroft		# then suggest that.
6485d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6486b0781216SMike Frysinger			$rpt_cleaners = 0;
6487d8469f16SJoe Perches			print << "EOM"
6488d8469f16SJoe Perches
6489d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6490d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6491d8469f16SJoe PerchesEOM
6492d2c0a235SAndy Whitcroft		}
6493d2c0a235SAndy Whitcroft	}
6494d2c0a235SAndy Whitcroft
6495d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6496d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6497d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
64989624b8d6SJoe Perches		my $newfile = $filename;
64999624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
65003705ce5bSJoe Perches		my $linecount = 0;
65013705ce5bSJoe Perches		my $f;
65023705ce5bSJoe Perches
6503d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6504d752fcc8SJoe Perches
65053705ce5bSJoe Perches		open($f, '>', $newfile)
65063705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
65073705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
65083705ce5bSJoe Perches			$linecount++;
65093705ce5bSJoe Perches			if ($file) {
65103705ce5bSJoe Perches				if ($linecount > 3) {
65113705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
65123705ce5bSJoe Perches					print $f $fixed_line . "\n";
65133705ce5bSJoe Perches				}
65143705ce5bSJoe Perches			} else {
65153705ce5bSJoe Perches				print $f $fixed_line . "\n";
65163705ce5bSJoe Perches			}
65173705ce5bSJoe Perches		}
65183705ce5bSJoe Perches		close($f);
65193705ce5bSJoe Perches
65203705ce5bSJoe Perches		if (!$quiet) {
65213705ce5bSJoe Perches			print << "EOM";
6522d8469f16SJoe Perches
65233705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
65243705ce5bSJoe Perches
65253705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
65263705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
65273705ce5bSJoe Perches
65283705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
65293705ce5bSJoe PerchesNo warranties, expressed or implied...
65303705ce5bSJoe PerchesEOM
65313705ce5bSJoe Perches		}
65323705ce5bSJoe Perches	}
65333705ce5bSJoe Perches
6534d8469f16SJoe Perches	if ($quiet == 0) {
6535d8469f16SJoe Perches		print "\n";
6536d8469f16SJoe Perches		if ($clean == 1) {
6537d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6538d8469f16SJoe Perches		} else {
6539d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
65400a920b5bSAndy Whitcroft		}
65410a920b5bSAndy Whitcroft	}
65420a920b5bSAndy Whitcroft	return $clean;
65430a920b5bSAndy Whitcroft}
6544