xref: /linux-6.15/scripts/checkpatch.pl (revision 0bc989ff)
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}
569515a235eSJoe Perches
570b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
571b392c64fSJoe Perches	S_IWUGO		|
572b392c64fSJoe Perches	S_IWOTH		|
573b392c64fSJoe Perches	S_IRWXUGO	|
574b392c64fSJoe Perches	S_IALLUGO	|
575b392c64fSJoe Perches	0[0-7][0-7][2367]
576b392c64fSJoe Perches}x;
577b392c64fSJoe Perches
578f90774e1SJoe Perchesour %mode_permission_string_types = (
579f90774e1SJoe Perches	"S_IRWXU" => 0700,
580f90774e1SJoe Perches	"S_IRUSR" => 0400,
581f90774e1SJoe Perches	"S_IWUSR" => 0200,
582f90774e1SJoe Perches	"S_IXUSR" => 0100,
583f90774e1SJoe Perches	"S_IRWXG" => 0070,
584f90774e1SJoe Perches	"S_IRGRP" => 0040,
585f90774e1SJoe Perches	"S_IWGRP" => 0020,
586f90774e1SJoe Perches	"S_IXGRP" => 0010,
587f90774e1SJoe Perches	"S_IRWXO" => 0007,
588f90774e1SJoe Perches	"S_IROTH" => 0004,
589f90774e1SJoe Perches	"S_IWOTH" => 0002,
590f90774e1SJoe Perches	"S_IXOTH" => 0001,
591f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
592f90774e1SJoe Perches	"S_IRUGO" => 0444,
593f90774e1SJoe Perches	"S_IWUGO" => 0222,
594f90774e1SJoe Perches	"S_IXUGO" => 0111,
595f90774e1SJoe Perches);
596f90774e1SJoe Perches
597f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
598f90774e1SJoe Perchesour $mode_perms_string_search = "";
599f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
600f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
601f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
602f90774e1SJoe Perches}
603f90774e1SJoe Perches
6047840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6057840a94cSWolfram Sang	irq|
606cdcee686SSergey Ryazanov	memory|
607cdcee686SSergey Ryazanov	time|
608cdcee686SSergey Ryazanov	reboot
6097840a94cSWolfram Sang)};
6107840a94cSWolfram Sang# memory.h: ARM has a custom one
6117840a94cSWolfram Sang
61266b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
61366b47b4aSKees Cookmy $misspellings;
61466b47b4aSKees Cookmy %spelling_fix;
61536061e38SJoe Perches
61636061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
61766b47b4aSKees Cook	while (<$spelling>) {
61866b47b4aSKees Cook		my $line = $_;
61966b47b4aSKees Cook
62066b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
62166b47b4aSKees Cook		$line =~ s/^\s*//g;
62266b47b4aSKees Cook
62366b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
62466b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
62566b47b4aSKees Cook
62666b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
62766b47b4aSKees Cook
62866b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
62966b47b4aSKees Cook	}
63066b47b4aSKees Cook	close($spelling);
63136061e38SJoe Perches} else {
63236061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
63336061e38SJoe Perches}
63466b47b4aSKees Cook
635ebfd7d62SJoe Perchesif ($codespell) {
636ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
637ebfd7d62SJoe Perches		while (<$spelling>) {
638ebfd7d62SJoe Perches			my $line = $_;
639ebfd7d62SJoe Perches
640ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
641ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
642ebfd7d62SJoe Perches
643ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
644ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
645ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
646ebfd7d62SJoe Perches
647ebfd7d62SJoe Perches			$line =~ s/,.*$//;
648ebfd7d62SJoe Perches
649ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
650ebfd7d62SJoe Perches
651ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
652ebfd7d62SJoe Perches		}
653ebfd7d62SJoe Perches		close($spelling);
654ebfd7d62SJoe Perches	} else {
655ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
656ebfd7d62SJoe Perches	}
657ebfd7d62SJoe Perches}
658ebfd7d62SJoe Perches
659ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
660ebfd7d62SJoe Perches
66175ad8c57SJerome Forissiersub read_words {
66275ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
66375ad8c57SJerome Forissier
66475ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
66575ad8c57SJerome Forissier		while (<$words>) {
666bf1fa1daSJoe Perches			my $line = $_;
667bf1fa1daSJoe Perches
668bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
669bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
670bf1fa1daSJoe Perches
671bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
672bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
673bf1fa1daSJoe Perches			if ($line =~ /\s/) {
67475ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
675bf1fa1daSJoe Perches				next;
676bf1fa1daSJoe Perches			}
677bf1fa1daSJoe Perches
67875ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
67975ad8c57SJerome Forissier			$$wordsRef .= $line;
680bf1fa1daSJoe Perches		}
68175ad8c57SJerome Forissier		close($file);
68275ad8c57SJerome Forissier		return 1;
683bf1fa1daSJoe Perches	}
684bf1fa1daSJoe Perches
68575ad8c57SJerome Forissier	return 0;
68675ad8c57SJerome Forissier}
68775ad8c57SJerome Forissier
68875ad8c57SJerome Forissiermy $const_structs = "";
68975ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
69075ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
69175ad8c57SJerome Forissier
69275ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
69375ad8c57SJerome Forissierif (length($typedefsfile)) {
69475ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
69575ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
69675ad8c57SJerome Forissier}
69775ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
69875ad8c57SJerome Forissier
6998905a67cSAndy Whitcroftsub build_types {
700485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
701485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7021813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7038716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
704c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
705ab7e23f3SJoe Perches	$BasicType	= qr{
706ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
707ab7e23f3SJoe Perches				(?:${all}\b)
708ab7e23f3SJoe Perches		}x;
7098905a67cSAndy Whitcroft	$NonptrType	= qr{
710d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
711cf655043SAndy Whitcroft			(?:
7126b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7138ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
714c45dcabdSAndy Whitcroft				(?:${all}\b)
715cf655043SAndy Whitcroft			)
716c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7178905a67cSAndy Whitcroft		  }x;
7181813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7191813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7201813087dSJoe Perches			(?:
7211813087dSJoe Perches				(?:${Misordered}\b)
7221813087dSJoe Perches			)
7231813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7241813087dSJoe Perches		  }x;
7258716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7268716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7278716de38SJoe Perches			(?:
7288716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
7298716de38SJoe Perches				(?:$typeTypedefs\b)|
7308716de38SJoe Perches				(?:${allWithAttr}\b)
7318716de38SJoe Perches			)
7328716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
7338716de38SJoe Perches		  }x;
7348905a67cSAndy Whitcroft	$Type	= qr{
735c45dcabdSAndy Whitcroft			$NonptrType
7361574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
737c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
7388905a67cSAndy Whitcroft		  }x;
7391813087dSJoe Perches	$TypeMisordered	= qr{
7401813087dSJoe Perches			$NonptrTypeMisordered
7411813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
7421813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
7431813087dSJoe Perches		  }x;
74491cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
7451813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
7468905a67cSAndy Whitcroft}
7478905a67cSAndy Whitcroftbuild_types();
7486c72ffaaSAndy Whitcroft
7497d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
750d1fe9c09SJoe Perches
751d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
752d1fe9c09SJoe Perches# requires at least perl version v5.10.0
753d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
754d1fe9c09SJoe Perches
755d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
7562435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
757c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
7587d2367afSJoe Perches
759f8422308SJoe Perchesour $declaration_macros = qr{(?x:
7603e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
761fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
762f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
763f8422308SJoe Perches)};
764f8422308SJoe Perches
7657d2367afSJoe Perchessub deparenthesize {
7667d2367afSJoe Perches	my ($string) = @_;
7677d2367afSJoe Perches	return "" if (!defined($string));
7685b9553abSJoe Perches
7695b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
7705b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
7715b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
7725b9553abSJoe Perches	}
7735b9553abSJoe Perches
7747d2367afSJoe Perches	$string =~ s@\s+@ @g;
7755b9553abSJoe Perches
7767d2367afSJoe Perches	return $string;
7777d2367afSJoe Perches}
7787d2367afSJoe Perches
7793445686aSJoe Perchessub seed_camelcase_file {
7803445686aSJoe Perches	my ($file) = @_;
7813445686aSJoe Perches
7823445686aSJoe Perches	return if (!(-f $file));
7833445686aSJoe Perches
7843445686aSJoe Perches	local $/;
7853445686aSJoe Perches
7863445686aSJoe Perches	open(my $include_file, '<', "$file")
7873445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
7883445686aSJoe Perches	my $text = <$include_file>;
7893445686aSJoe Perches	close($include_file);
7903445686aSJoe Perches
7913445686aSJoe Perches	my @lines = split('\n', $text);
7923445686aSJoe Perches
7933445686aSJoe Perches	foreach my $line (@lines) {
7943445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
7953445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
7963445686aSJoe Perches			$camelcase{$1} = 1;
79711ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
79811ea516aSJoe Perches			$camelcase{$1} = 1;
79911ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8003445686aSJoe Perches			$camelcase{$1} = 1;
8013445686aSJoe Perches		}
8023445686aSJoe Perches	}
8033445686aSJoe Perches}
8043445686aSJoe Perches
80585b0ee18SJoe Perchessub is_maintained_obsolete {
80685b0ee18SJoe Perches	my ($filename) = @_;
80785b0ee18SJoe Perches
808f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
80985b0ee18SJoe Perches
8100616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
81185b0ee18SJoe Perches
81285b0ee18SJoe Perches	return $status =~ /obsolete/i;
81385b0ee18SJoe Perches}
81485b0ee18SJoe Perches
8153445686aSJoe Perchesmy $camelcase_seeded = 0;
8163445686aSJoe Perchessub seed_camelcase_includes {
8173445686aSJoe Perches	return if ($camelcase_seeded);
8183445686aSJoe Perches
8193445686aSJoe Perches	my $files;
820c707a81dSJoe Perches	my $camelcase_cache = "";
821c707a81dSJoe Perches	my @include_files = ();
822c707a81dSJoe Perches
823c707a81dSJoe Perches	$camelcase_seeded = 1;
824351b2a1fSJoe Perches
8253645e328SRichard Genoud	if (-e ".git") {
826351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
827351b2a1fSJoe Perches		chomp $git_last_include_commit;
828c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
829c707a81dSJoe Perches	} else {
830c707a81dSJoe Perches		my $last_mod_date = 0;
831c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
832c707a81dSJoe Perches		@include_files = split('\n', $files);
833c707a81dSJoe Perches		foreach my $file (@include_files) {
834c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
835c707a81dSJoe Perches						   localtime((stat $file)[9]));
836c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
837c707a81dSJoe Perches		}
838c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
839c707a81dSJoe Perches	}
840c707a81dSJoe Perches
841c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
842c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
843c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
844351b2a1fSJoe Perches		while (<$camelcase_file>) {
845351b2a1fSJoe Perches			chomp;
846351b2a1fSJoe Perches			$camelcase{$_} = 1;
847351b2a1fSJoe Perches		}
848351b2a1fSJoe Perches		close($camelcase_file);
849351b2a1fSJoe Perches
850351b2a1fSJoe Perches		return;
851351b2a1fSJoe Perches	}
852c707a81dSJoe Perches
8533645e328SRichard Genoud	if (-e ".git") {
854c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
855c707a81dSJoe Perches		@include_files = split('\n', $files);
8563445686aSJoe Perches	}
857c707a81dSJoe Perches
8583445686aSJoe Perches	foreach my $file (@include_files) {
8593445686aSJoe Perches		seed_camelcase_file($file);
8603445686aSJoe Perches	}
861351b2a1fSJoe Perches
862c707a81dSJoe Perches	if ($camelcase_cache ne "") {
863351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
864c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
865c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
866351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
867351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
868351b2a1fSJoe Perches		}
869351b2a1fSJoe Perches		close($camelcase_file);
870351b2a1fSJoe Perches	}
8713445686aSJoe Perches}
8723445686aSJoe Perches
873d311cd44SJoe Perchessub git_commit_info {
874d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
875d311cd44SJoe Perches
876d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
877d311cd44SJoe Perches
878d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
879d311cd44SJoe Perches	$output =~ s/^\s*//gm;
880d311cd44SJoe Perches	my @lines = split("\n", $output);
881d311cd44SJoe Perches
8820d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
8830d7835fcSJoe Perches
884d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
885d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
886d311cd44SJoe Perches# all matching commit ids, but it's very slow...
887d311cd44SJoe Perches#
888d311cd44SJoe Perches#		echo "checking commits $1..."
889d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
890d311cd44SJoe Perches#		while read line ; do
891d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
892d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
893d311cd44SJoe Perches#		done
894d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
895948b133aSHeinrich Schuchardt		$id = undef;
896d311cd44SJoe Perches	} else {
897d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
898d311cd44SJoe Perches		$desc = substr($lines[0], 41);
899d311cd44SJoe Perches	}
900d311cd44SJoe Perches
901d311cd44SJoe Perches	return ($id, $desc);
902d311cd44SJoe Perches}
903d311cd44SJoe Perches
9046c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9050a920b5bSAndy Whitcroft
90600df344fSAndy Whitcroftmy @rawlines = ();
907c2fdda0dSAndy Whitcroftmy @lines = ();
9083705ce5bSJoe Perchesmy @fixed = ();
909d752fcc8SJoe Perchesmy @fixed_inserted = ();
910d752fcc8SJoe Perchesmy @fixed_deleted = ();
911194f66fcSJoe Perchesmy $fixlinenr = -1;
912194f66fcSJoe Perches
9134a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
9144a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
9154a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
9164a593c34SDu, Changbin
9174a593c34SDu, Changbinif ($git) {
9184a593c34SDu, Changbin	my @commits = ();
9190dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
9204a593c34SDu, Changbin		my $git_range;
92128898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
92228898fd1SJoe Perches			$git_range = "-$2 $1";
9234a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
9244a593c34SDu, Changbin			$git_range = "$commit_expr";
9254a593c34SDu, Changbin		} else {
9260dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
9270dea9f1eSJoe Perches		}
9280dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
9290dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
93028898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
93128898fd1SJoe Perches			next if (!defined($1) || !defined($2));
9320dea9f1eSJoe Perches			my $sha1 = $1;
9330dea9f1eSJoe Perches			my $subject = $2;
9340dea9f1eSJoe Perches			unshift(@commits, $sha1);
9350dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
9364a593c34SDu, Changbin		}
9374a593c34SDu, Changbin	}
9384a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
9394a593c34SDu, Changbin	@ARGV = @commits;
9404a593c34SDu, Changbin}
9414a593c34SDu, Changbin
942c2fdda0dSAndy Whitcroftmy $vname;
9436c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
94421caa13cSAndy Whitcroft	my $FILE;
9454a593c34SDu, Changbin	if ($git) {
9464a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
9474a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
9484a593c34SDu, Changbin	} elsif ($file) {
94921caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
9506c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
95121caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
95221caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
9536c72ffaaSAndy Whitcroft	} else {
95421caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
9556c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
9566c72ffaaSAndy Whitcroft	}
957c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
958c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
9594a593c34SDu, Changbin	} elsif ($git) {
9600dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
961c2fdda0dSAndy Whitcroft	} else {
962c2fdda0dSAndy Whitcroft		$vname = $filename;
963c2fdda0dSAndy Whitcroft	}
96421caa13cSAndy Whitcroft	while (<$FILE>) {
9650a920b5bSAndy Whitcroft		chomp;
96600df344fSAndy Whitcroft		push(@rawlines, $_);
9676c72ffaaSAndy Whitcroft	}
96821caa13cSAndy Whitcroft	close($FILE);
969d8469f16SJoe Perches
970d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
971d8469f16SJoe Perches		print '-' x length($vname) . "\n";
972d8469f16SJoe Perches		print "$vname\n";
973d8469f16SJoe Perches		print '-' x length($vname) . "\n";
974d8469f16SJoe Perches	}
975d8469f16SJoe Perches
976c2fdda0dSAndy Whitcroft	if (!process($filename)) {
9770a920b5bSAndy Whitcroft		$exit = 1;
9780a920b5bSAndy Whitcroft	}
97900df344fSAndy Whitcroft	@rawlines = ();
98013214adfSAndy Whitcroft	@lines = ();
9813705ce5bSJoe Perches	@fixed = ();
982d752fcc8SJoe Perches	@fixed_inserted = ();
983d752fcc8SJoe Perches	@fixed_deleted = ();
984194f66fcSJoe Perches	$fixlinenr = -1;
985485ff23eSAlex Dowad	@modifierListFile = ();
986485ff23eSAlex Dowad	@typeListFile = ();
987485ff23eSAlex Dowad	build_types();
9880a920b5bSAndy Whitcroft}
9890a920b5bSAndy Whitcroft
990d8469f16SJoe Perchesif (!$quiet) {
9913c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
9923c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
9933c816e49SJoe Perches
994d8469f16SJoe Perches	if ($^V lt 5.10.0) {
995d8469f16SJoe Perches		print << "EOM"
996d8469f16SJoe Perches
997d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
998d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
999d8469f16SJoe PerchesEOM
1000d8469f16SJoe Perches	}
1001d8469f16SJoe Perches	if ($exit) {
1002d8469f16SJoe Perches		print << "EOM"
1003d8469f16SJoe Perches
1004d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1005d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1006d8469f16SJoe PerchesEOM
1007d8469f16SJoe Perches	}
1008d8469f16SJoe Perches}
1009d8469f16SJoe Perches
10100a920b5bSAndy Whitcroftexit($exit);
10110a920b5bSAndy Whitcroft
10120a920b5bSAndy Whitcroftsub top_of_kernel_tree {
10136c72ffaaSAndy Whitcroft	my ($root) = @_;
10146c72ffaaSAndy Whitcroft
10156c72ffaaSAndy Whitcroft	my @tree_check = (
10166c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
10176c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
10186c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
10196c72ffaaSAndy Whitcroft	);
10206c72ffaaSAndy Whitcroft
10216c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
10226c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
10230a920b5bSAndy Whitcroft			return 0;
10240a920b5bSAndy Whitcroft		}
10256c72ffaaSAndy Whitcroft	}
10266c72ffaaSAndy Whitcroft	return 1;
10276c72ffaaSAndy Whitcroft}
10280a920b5bSAndy Whitcroft
102920112475SJoe Perchessub parse_email {
103020112475SJoe Perches	my ($formatted_email) = @_;
103120112475SJoe Perches
103220112475SJoe Perches	my $name = "";
103320112475SJoe Perches	my $address = "";
103420112475SJoe Perches	my $comment = "";
103520112475SJoe Perches
103620112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
103720112475SJoe Perches		$name = $1;
103820112475SJoe Perches		$address = $2;
103920112475SJoe Perches		$comment = $3 if defined $3;
104020112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
104120112475SJoe Perches		$address = $1;
104220112475SJoe Perches		$comment = $2 if defined $2;
104320112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
104420112475SJoe Perches		$address = $1;
104520112475SJoe Perches		$comment = $2 if defined $2;
104620112475SJoe Perches		$formatted_email =~ s/$address.*$//;
104720112475SJoe Perches		$name = $formatted_email;
10483705ce5bSJoe Perches		$name = trim($name);
104920112475SJoe Perches		$name =~ s/^\"|\"$//g;
105020112475SJoe Perches		# If there's a name left after stripping spaces and
105120112475SJoe Perches		# leading quotes, and the address doesn't have both
105220112475SJoe Perches		# leading and trailing angle brackets, the address
105320112475SJoe Perches		# is invalid. ie:
105420112475SJoe Perches		#   "joe smith [email protected]" bad
105520112475SJoe Perches		#   "joe smith <[email protected]" bad
105620112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
105720112475SJoe Perches			$name = "";
105820112475SJoe Perches			$address = "";
105920112475SJoe Perches			$comment = "";
106020112475SJoe Perches		}
106120112475SJoe Perches	}
106220112475SJoe Perches
10633705ce5bSJoe Perches	$name = trim($name);
106420112475SJoe Perches	$name =~ s/^\"|\"$//g;
10653705ce5bSJoe Perches	$address = trim($address);
106620112475SJoe Perches	$address =~ s/^\<|\>$//g;
106720112475SJoe Perches
106820112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
106920112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
107020112475SJoe Perches		$name = "\"$name\"";
107120112475SJoe Perches	}
107220112475SJoe Perches
107320112475SJoe Perches	return ($name, $address, $comment);
107420112475SJoe Perches}
107520112475SJoe Perches
107620112475SJoe Perchessub format_email {
107720112475SJoe Perches	my ($name, $address) = @_;
107820112475SJoe Perches
107920112475SJoe Perches	my $formatted_email;
108020112475SJoe Perches
10813705ce5bSJoe Perches	$name = trim($name);
108220112475SJoe Perches	$name =~ s/^\"|\"$//g;
10833705ce5bSJoe Perches	$address = trim($address);
108420112475SJoe Perches
108520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
108620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
108720112475SJoe Perches		$name = "\"$name\"";
108820112475SJoe Perches	}
108920112475SJoe Perches
109020112475SJoe Perches	if ("$name" eq "") {
109120112475SJoe Perches		$formatted_email = "$address";
109220112475SJoe Perches	} else {
109320112475SJoe Perches		$formatted_email = "$name <$address>";
109420112475SJoe Perches	}
109520112475SJoe Perches
109620112475SJoe Perches	return $formatted_email;
109720112475SJoe Perches}
109820112475SJoe Perches
1099d311cd44SJoe Perchessub which {
1100d311cd44SJoe Perches	my ($bin) = @_;
1101d311cd44SJoe Perches
1102d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1103d311cd44SJoe Perches		if (-e "$path/$bin") {
1104d311cd44SJoe Perches			return "$path/$bin";
1105d311cd44SJoe Perches		}
1106d311cd44SJoe Perches	}
1107d311cd44SJoe Perches
1108d311cd44SJoe Perches	return "";
1109d311cd44SJoe Perches}
1110d311cd44SJoe Perches
1111000d1cc1SJoe Perchessub which_conf {
1112000d1cc1SJoe Perches	my ($conf) = @_;
1113000d1cc1SJoe Perches
1114000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1115000d1cc1SJoe Perches		if (-e "$path/$conf") {
1116000d1cc1SJoe Perches			return "$path/$conf";
1117000d1cc1SJoe Perches		}
1118000d1cc1SJoe Perches	}
1119000d1cc1SJoe Perches
1120000d1cc1SJoe Perches	return "";
1121000d1cc1SJoe Perches}
1122000d1cc1SJoe Perches
11230a920b5bSAndy Whitcroftsub expand_tabs {
11240a920b5bSAndy Whitcroft	my ($str) = @_;
11250a920b5bSAndy Whitcroft
11260a920b5bSAndy Whitcroft	my $res = '';
11270a920b5bSAndy Whitcroft	my $n = 0;
11280a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
11290a920b5bSAndy Whitcroft		if ($c eq "\t") {
11300a920b5bSAndy Whitcroft			$res .= ' ';
11310a920b5bSAndy Whitcroft			$n++;
11320a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
11330a920b5bSAndy Whitcroft				$res .= ' ';
11340a920b5bSAndy Whitcroft			}
11350a920b5bSAndy Whitcroft			next;
11360a920b5bSAndy Whitcroft		}
11370a920b5bSAndy Whitcroft		$res .= $c;
11380a920b5bSAndy Whitcroft		$n++;
11390a920b5bSAndy Whitcroft	}
11400a920b5bSAndy Whitcroft
11410a920b5bSAndy Whitcroft	return $res;
11420a920b5bSAndy Whitcroft}
11436c72ffaaSAndy Whitcroftsub copy_spacing {
1144773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
11456c72ffaaSAndy Whitcroft	return $res;
11466c72ffaaSAndy Whitcroft}
11470a920b5bSAndy Whitcroft
11484a0df2efSAndy Whitcroftsub line_stats {
11494a0df2efSAndy Whitcroft	my ($line) = @_;
11504a0df2efSAndy Whitcroft
11514a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
11524a0df2efSAndy Whitcroft	$line =~ s/^.//;
11534a0df2efSAndy Whitcroft	$line = expand_tabs($line);
11544a0df2efSAndy Whitcroft
11554a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
11564a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
11574a0df2efSAndy Whitcroft
11584a0df2efSAndy Whitcroft	return (length($line), length($white));
11594a0df2efSAndy Whitcroft}
11604a0df2efSAndy Whitcroft
1161773647a0SAndy Whitcroftmy $sanitise_quote = '';
1162773647a0SAndy Whitcroft
1163773647a0SAndy Whitcroftsub sanitise_line_reset {
1164773647a0SAndy Whitcroft	my ($in_comment) = @_;
1165773647a0SAndy Whitcroft
1166773647a0SAndy Whitcroft	if ($in_comment) {
1167773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1168773647a0SAndy Whitcroft	} else {
1169773647a0SAndy Whitcroft		$sanitise_quote = '';
1170773647a0SAndy Whitcroft	}
1171773647a0SAndy Whitcroft}
117200df344fSAndy Whitcroftsub sanitise_line {
117300df344fSAndy Whitcroft	my ($line) = @_;
117400df344fSAndy Whitcroft
117500df344fSAndy Whitcroft	my $res = '';
117600df344fSAndy Whitcroft	my $l = '';
117700df344fSAndy Whitcroft
1178c2fdda0dSAndy Whitcroft	my $qlen = 0;
1179773647a0SAndy Whitcroft	my $off = 0;
1180773647a0SAndy Whitcroft	my $c;
118100df344fSAndy Whitcroft
1182773647a0SAndy Whitcroft	# Always copy over the diff marker.
1183773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1184773647a0SAndy Whitcroft
1185773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1186773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1187773647a0SAndy Whitcroft
1188773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
1189773647a0SAndy Whitcroft		# and end, all to $;.
1190773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1191773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1192773647a0SAndy Whitcroft
1193773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1194773647a0SAndy Whitcroft			$off++;
119500df344fSAndy Whitcroft			next;
1196773647a0SAndy Whitcroft		}
119781bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1198773647a0SAndy Whitcroft			$sanitise_quote = '';
1199773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1200773647a0SAndy Whitcroft			$off++;
1201773647a0SAndy Whitcroft			next;
1202773647a0SAndy Whitcroft		}
1203113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1204113f04a8SDaniel Walker			$sanitise_quote = '//';
1205113f04a8SDaniel Walker
1206113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1207113f04a8SDaniel Walker			$off++;
1208113f04a8SDaniel Walker			next;
1209113f04a8SDaniel Walker		}
1210773647a0SAndy Whitcroft
1211773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1212773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1213773647a0SAndy Whitcroft		    $c eq "\\") {
1214773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1215773647a0SAndy Whitcroft			$off++;
1216773647a0SAndy Whitcroft			next;
1217773647a0SAndy Whitcroft		}
1218773647a0SAndy Whitcroft		# Regular quotes.
1219773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1220773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1221773647a0SAndy Whitcroft				$sanitise_quote = $c;
1222773647a0SAndy Whitcroft
1223773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1224773647a0SAndy Whitcroft				next;
1225773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1226773647a0SAndy Whitcroft				$sanitise_quote = '';
122700df344fSAndy Whitcroft			}
122800df344fSAndy Whitcroft		}
1229773647a0SAndy Whitcroft
1230fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1231773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1232773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1233113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1234113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1235773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1236773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
123700df344fSAndy Whitcroft		} else {
1238773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
123900df344fSAndy Whitcroft		}
1240c2fdda0dSAndy Whitcroft	}
1241c2fdda0dSAndy Whitcroft
1242113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1243113f04a8SDaniel Walker		$sanitise_quote = '';
1244113f04a8SDaniel Walker	}
1245113f04a8SDaniel Walker
1246c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1247c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1248c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1249c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1250c2fdda0dSAndy Whitcroft
1251c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1252c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1253c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1254c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1255c2fdda0dSAndy Whitcroft	}
1256c2fdda0dSAndy Whitcroft
1257dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1258dadf680dSJoe Perches		my $match = $1;
1259dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1260dadf680dSJoe Perches	}
1261dadf680dSJoe Perches
126200df344fSAndy Whitcroft	return $res;
126300df344fSAndy Whitcroft}
126400df344fSAndy Whitcroft
1265a6962d72SJoe Perchessub get_quoted_string {
1266a6962d72SJoe Perches	my ($line, $rawline) = @_;
1267a6962d72SJoe Perches
126833acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1269a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1270a6962d72SJoe Perches}
1271a6962d72SJoe Perches
12728905a67cSAndy Whitcroftsub ctx_statement_block {
12738905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12748905a67cSAndy Whitcroft	my $line = $linenr - 1;
12758905a67cSAndy Whitcroft	my $blk = '';
12768905a67cSAndy Whitcroft	my $soff = $off;
12778905a67cSAndy Whitcroft	my $coff = $off - 1;
1278773647a0SAndy Whitcroft	my $coff_set = 0;
12798905a67cSAndy Whitcroft
128013214adfSAndy Whitcroft	my $loff = 0;
128113214adfSAndy Whitcroft
12828905a67cSAndy Whitcroft	my $type = '';
12838905a67cSAndy Whitcroft	my $level = 0;
1284a2750645SAndy Whitcroft	my @stack = ();
1285cf655043SAndy Whitcroft	my $p;
12868905a67cSAndy Whitcroft	my $c;
12878905a67cSAndy Whitcroft	my $len = 0;
128813214adfSAndy Whitcroft
128913214adfSAndy Whitcroft	my $remainder;
12908905a67cSAndy Whitcroft	while (1) {
1291a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1292a2750645SAndy Whitcroft
1293773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
12948905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
12958905a67cSAndy Whitcroft		# context.
12968905a67cSAndy Whitcroft		if ($off >= $len) {
12978905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1298dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1299c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
13008905a67cSAndy Whitcroft				$remain--;
130113214adfSAndy Whitcroft				$loff = $len;
1302c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13038905a67cSAndy Whitcroft				$len = length($blk);
13048905a67cSAndy Whitcroft				$line++;
13058905a67cSAndy Whitcroft				last;
13068905a67cSAndy Whitcroft			}
13078905a67cSAndy Whitcroft			# Bail if there is no further context.
13088905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
130913214adfSAndy Whitcroft			if ($off >= $len) {
13108905a67cSAndy Whitcroft				last;
13118905a67cSAndy Whitcroft			}
1312f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1313f74bd194SAndy Whitcroft				$level++;
1314f74bd194SAndy Whitcroft				$type = '#';
1315f74bd194SAndy Whitcroft			}
13168905a67cSAndy Whitcroft		}
1317cf655043SAndy Whitcroft		$p = $c;
13188905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
131913214adfSAndy Whitcroft		$remainder = substr($blk, $off);
13208905a67cSAndy Whitcroft
1321773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
13224635f4fbSAndy Whitcroft
13234635f4fbSAndy Whitcroft		# Handle nested #if/#else.
13244635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
13254635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
13264635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
13274635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
13284635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
13294635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
13304635f4fbSAndy Whitcroft		}
13314635f4fbSAndy Whitcroft
13328905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
13338905a67cSAndy Whitcroft		# outermost level.
13348905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
13358905a67cSAndy Whitcroft			last;
13368905a67cSAndy Whitcroft		}
13378905a67cSAndy Whitcroft
133813214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1339773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1340773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1341773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1342773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1343773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1344773647a0SAndy Whitcroft			$coff_set = 1;
1345773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1346773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
134713214adfSAndy Whitcroft		}
134813214adfSAndy Whitcroft
13498905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
13508905a67cSAndy Whitcroft			$level++;
13518905a67cSAndy Whitcroft			$type = '(';
13528905a67cSAndy Whitcroft		}
13538905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
13548905a67cSAndy Whitcroft			$level--;
13558905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
13568905a67cSAndy Whitcroft
13578905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
13588905a67cSAndy Whitcroft				$coff = $off;
1359773647a0SAndy Whitcroft				$coff_set = 1;
1360773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
13618905a67cSAndy Whitcroft			}
13628905a67cSAndy Whitcroft		}
13638905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
13648905a67cSAndy Whitcroft			$level++;
13658905a67cSAndy Whitcroft			$type = '{';
13668905a67cSAndy Whitcroft		}
13678905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
13688905a67cSAndy Whitcroft			$level--;
13698905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
13708905a67cSAndy Whitcroft
13718905a67cSAndy Whitcroft			if ($level == 0) {
1372b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1373b998e001SPatrick Pannuto					$off++;
1374b998e001SPatrick Pannuto				}
13758905a67cSAndy Whitcroft				last;
13768905a67cSAndy Whitcroft			}
13778905a67cSAndy Whitcroft		}
1378f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1379f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1380f74bd194SAndy Whitcroft			$level--;
1381f74bd194SAndy Whitcroft			$type = '';
1382f74bd194SAndy Whitcroft			$off++;
1383f74bd194SAndy Whitcroft			last;
1384f74bd194SAndy Whitcroft		}
13858905a67cSAndy Whitcroft		$off++;
13868905a67cSAndy Whitcroft	}
1387a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
138813214adfSAndy Whitcroft	if ($off == $len) {
1389a3bb97a7SAndy Whitcroft		$loff = $len + 1;
139013214adfSAndy Whitcroft		$line++;
139113214adfSAndy Whitcroft		$remain--;
139213214adfSAndy Whitcroft	}
13938905a67cSAndy Whitcroft
13948905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
13958905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
13968905a67cSAndy Whitcroft
13978905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
13988905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
13998905a67cSAndy Whitcroft
1400773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
140113214adfSAndy Whitcroft
140213214adfSAndy Whitcroft	return ($statement, $condition,
140313214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
140413214adfSAndy Whitcroft}
140513214adfSAndy Whitcroft
1406cf655043SAndy Whitcroftsub statement_lines {
1407cf655043SAndy Whitcroft	my ($stmt) = @_;
1408cf655043SAndy Whitcroft
1409cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1410cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1411cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1412cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1413cf655043SAndy Whitcroft
1414cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1415cf655043SAndy Whitcroft
1416cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1417cf655043SAndy Whitcroft}
1418cf655043SAndy Whitcroft
1419cf655043SAndy Whitcroftsub statement_rawlines {
1420cf655043SAndy Whitcroft	my ($stmt) = @_;
1421cf655043SAndy Whitcroft
1422cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1423cf655043SAndy Whitcroft
1424cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1425cf655043SAndy Whitcroft}
1426cf655043SAndy Whitcroft
1427cf655043SAndy Whitcroftsub statement_block_size {
1428cf655043SAndy Whitcroft	my ($stmt) = @_;
1429cf655043SAndy Whitcroft
1430cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1431cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1432cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1433cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1434cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1435cf655043SAndy Whitcroft
1436cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1437cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1438cf655043SAndy Whitcroft
1439cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1440cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1441cf655043SAndy Whitcroft
1442cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1443cf655043SAndy Whitcroft		return $stmt_lines;
1444cf655043SAndy Whitcroft	} else {
1445cf655043SAndy Whitcroft		return $stmt_statements;
1446cf655043SAndy Whitcroft	}
1447cf655043SAndy Whitcroft}
1448cf655043SAndy Whitcroft
144913214adfSAndy Whitcroftsub ctx_statement_full {
145013214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
145113214adfSAndy Whitcroft	my ($statement, $condition, $level);
145213214adfSAndy Whitcroft
145313214adfSAndy Whitcroft	my (@chunks);
145413214adfSAndy Whitcroft
1455cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
145613214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
145713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1458773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
145913214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1460cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1461cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1462cf655043SAndy Whitcroft	}
1463cf655043SAndy Whitcroft
1464cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1465cf655043SAndy Whitcroft	# could continue the statement.
1466cf655043SAndy Whitcroft	for (;;) {
146713214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
146813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1469cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1470773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1471cf655043SAndy Whitcroft		#print "C: push\n";
1472cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
147313214adfSAndy Whitcroft	}
147413214adfSAndy Whitcroft
147513214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
14768905a67cSAndy Whitcroft}
14778905a67cSAndy Whitcroft
14784a0df2efSAndy Whitcroftsub ctx_block_get {
1479f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
14804a0df2efSAndy Whitcroft	my $line;
14814a0df2efSAndy Whitcroft	my $start = $linenr - 1;
14824a0df2efSAndy Whitcroft	my $blk = '';
14834a0df2efSAndy Whitcroft	my @o;
14844a0df2efSAndy Whitcroft	my @c;
14854a0df2efSAndy Whitcroft	my @res = ();
14864a0df2efSAndy Whitcroft
1487f0a594c1SAndy Whitcroft	my $level = 0;
14884635f4fbSAndy Whitcroft	my @stack = ($level);
148900df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
149000df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
149100df344fSAndy Whitcroft		$remain--;
149200df344fSAndy Whitcroft
149300df344fSAndy Whitcroft		$blk .= $rawlines[$line];
14944635f4fbSAndy Whitcroft
14954635f4fbSAndy Whitcroft		# Handle nested #if/#else.
149601464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
14974635f4fbSAndy Whitcroft			push(@stack, $level);
149801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
14994635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
150001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
15014635f4fbSAndy Whitcroft			$level = pop(@stack);
15024635f4fbSAndy Whitcroft		}
15034635f4fbSAndy Whitcroft
150401464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1505f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1506f0a594c1SAndy Whitcroft			if ($off > 0) {
1507f0a594c1SAndy Whitcroft				$off--;
1508f0a594c1SAndy Whitcroft				next;
1509f0a594c1SAndy Whitcroft			}
15104a0df2efSAndy Whitcroft
1511f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1512f0a594c1SAndy Whitcroft				$level--;
1513f0a594c1SAndy Whitcroft				last if ($level == 0);
1514f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1515f0a594c1SAndy Whitcroft				$level++;
1516f0a594c1SAndy Whitcroft			}
1517f0a594c1SAndy Whitcroft		}
15184a0df2efSAndy Whitcroft
1519f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
152000df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
15214a0df2efSAndy Whitcroft		}
15224a0df2efSAndy Whitcroft
1523f0a594c1SAndy Whitcroft		last if ($level == 0);
15244a0df2efSAndy Whitcroft	}
15254a0df2efSAndy Whitcroft
1526f0a594c1SAndy Whitcroft	return ($level, @res);
15274a0df2efSAndy Whitcroft}
15284a0df2efSAndy Whitcroftsub ctx_block_outer {
15294a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15304a0df2efSAndy Whitcroft
1531f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1532f0a594c1SAndy Whitcroft	return @r;
15334a0df2efSAndy Whitcroft}
15344a0df2efSAndy Whitcroftsub ctx_block {
15354a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15364a0df2efSAndy Whitcroft
1537f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1538f0a594c1SAndy Whitcroft	return @r;
1539653d4876SAndy Whitcroft}
1540653d4876SAndy Whitcroftsub ctx_statement {
1541f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1542f0a594c1SAndy Whitcroft
1543f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1544f0a594c1SAndy Whitcroft	return @r;
1545f0a594c1SAndy Whitcroft}
1546f0a594c1SAndy Whitcroftsub ctx_block_level {
1547653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1548653d4876SAndy Whitcroft
1549f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
15504a0df2efSAndy Whitcroft}
15519c0ca6f9SAndy Whitcroftsub ctx_statement_level {
15529c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
15539c0ca6f9SAndy Whitcroft
15549c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
15559c0ca6f9SAndy Whitcroft}
15564a0df2efSAndy Whitcroft
15574a0df2efSAndy Whitcroftsub ctx_locate_comment {
15584a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15594a0df2efSAndy Whitcroft
15604a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1561beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
15624a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
15634a0df2efSAndy Whitcroft
15644a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
15654a0df2efSAndy Whitcroft	# comment.
15664a0df2efSAndy Whitcroft	my $in_comment = 0;
15674a0df2efSAndy Whitcroft	$current_comment = '';
15684a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
156900df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
157000df344fSAndy Whitcroft		#warn "           $line\n";
15714a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
15724a0df2efSAndy Whitcroft			$in_comment = 1;
15734a0df2efSAndy Whitcroft		}
15744a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
15754a0df2efSAndy Whitcroft			$in_comment = 1;
15764a0df2efSAndy Whitcroft		}
15774a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
15784a0df2efSAndy Whitcroft			$current_comment = '';
15794a0df2efSAndy Whitcroft		}
15804a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
15814a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
15824a0df2efSAndy Whitcroft			$in_comment = 0;
15834a0df2efSAndy Whitcroft		}
15844a0df2efSAndy Whitcroft	}
15854a0df2efSAndy Whitcroft
15864a0df2efSAndy Whitcroft	chomp($current_comment);
15874a0df2efSAndy Whitcroft	return($current_comment);
15884a0df2efSAndy Whitcroft}
15894a0df2efSAndy Whitcroftsub ctx_has_comment {
15904a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15914a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
15924a0df2efSAndy Whitcroft
159300df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
15944a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
15954a0df2efSAndy Whitcroft
15964a0df2efSAndy Whitcroft	return ($cmt ne '');
15974a0df2efSAndy Whitcroft}
15984a0df2efSAndy Whitcroft
15994d001e4dSAndy Whitcroftsub raw_line {
16004d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
16014d001e4dSAndy Whitcroft
16024d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16034d001e4dSAndy Whitcroft	$cnt++;
16044d001e4dSAndy Whitcroft
16054d001e4dSAndy Whitcroft	my $line;
16064d001e4dSAndy Whitcroft	while ($cnt) {
16074d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16084d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16094d001e4dSAndy Whitcroft		$cnt--;
16104d001e4dSAndy Whitcroft	}
16114d001e4dSAndy Whitcroft
16124d001e4dSAndy Whitcroft	return $line;
16134d001e4dSAndy Whitcroft}
16144d001e4dSAndy Whitcroft
16150a920b5bSAndy Whitcroftsub cat_vet {
16160a920b5bSAndy Whitcroft	my ($vet) = @_;
16179c0ca6f9SAndy Whitcroft	my ($res, $coded);
16180a920b5bSAndy Whitcroft
16199c0ca6f9SAndy Whitcroft	$res = '';
16206c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
16216c72ffaaSAndy Whitcroft		$res .= $1;
16226c72ffaaSAndy Whitcroft		if ($2 ne '') {
16239c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
16246c72ffaaSAndy Whitcroft			$res .= $coded;
16256c72ffaaSAndy Whitcroft		}
16269c0ca6f9SAndy Whitcroft	}
16279c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
16280a920b5bSAndy Whitcroft
16299c0ca6f9SAndy Whitcroft	return $res;
16300a920b5bSAndy Whitcroft}
16310a920b5bSAndy Whitcroft
1632c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1633cf655043SAndy Whitcroftmy $av_pending;
1634c2fdda0dSAndy Whitcroftmy @av_paren_type;
16351f65f947SAndy Whitcroftmy $av_pend_colon;
1636c2fdda0dSAndy Whitcroft
1637c2fdda0dSAndy Whitcroftsub annotate_reset {
1638c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1639cf655043SAndy Whitcroft	$av_pending = '_';
1640cf655043SAndy Whitcroft	@av_paren_type = ('E');
16411f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1642c2fdda0dSAndy Whitcroft}
1643c2fdda0dSAndy Whitcroft
16446c72ffaaSAndy Whitcroftsub annotate_values {
16456c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
16466c72ffaaSAndy Whitcroft
16476c72ffaaSAndy Whitcroft	my $res;
16481f65f947SAndy Whitcroft	my $var = '_' x length($stream);
16496c72ffaaSAndy Whitcroft	my $cur = $stream;
16506c72ffaaSAndy Whitcroft
1651c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
16526c72ffaaSAndy Whitcroft
16536c72ffaaSAndy Whitcroft	while (length($cur)) {
1654773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1655cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1656171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
16576c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1658c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1659c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1660cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1661c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
16626c72ffaaSAndy Whitcroft			}
16636c72ffaaSAndy Whitcroft
1664c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
16659446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
16669446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1667addcdceaSAndy Whitcroft			$type = 'c';
16689446ef56SAndy Whitcroft
1669e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1670c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
16716c72ffaaSAndy Whitcroft			$type = 'T';
16726c72ffaaSAndy Whitcroft
1673389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1674389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1675389a2fe5SAndy Whitcroft			$type = 'T';
1676389a2fe5SAndy Whitcroft
1677c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1678171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1679c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1680171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1681171ae1a4SAndy Whitcroft			if ($2 ne '') {
1682cf655043SAndy Whitcroft				$av_pending = 'N';
1683171ae1a4SAndy Whitcroft			}
1684171ae1a4SAndy Whitcroft			$type = 'E';
1685171ae1a4SAndy Whitcroft
1686c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1687171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1688171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1689171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
16906c72ffaaSAndy Whitcroft
1691c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1692cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1693c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1694cf655043SAndy Whitcroft
1695cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1696cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1697171ae1a4SAndy Whitcroft			$type = 'E';
1698cf655043SAndy Whitcroft
1699c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1700cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1701cf655043SAndy Whitcroft			$av_preprocessor = 1;
1702cf655043SAndy Whitcroft
1703cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1704cf655043SAndy Whitcroft
1705171ae1a4SAndy Whitcroft			$type = 'E';
1706cf655043SAndy Whitcroft
1707c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1708cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1709cf655043SAndy Whitcroft
1710cf655043SAndy Whitcroft			$av_preprocessor = 1;
1711cf655043SAndy Whitcroft
1712cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1713cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1714cf655043SAndy Whitcroft			pop(@av_paren_type);
1715cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1716171ae1a4SAndy Whitcroft			$type = 'E';
17176c72ffaaSAndy Whitcroft
17186c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1719c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
17206c72ffaaSAndy Whitcroft
1721171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1722171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1723171ae1a4SAndy Whitcroft			$av_pending = $type;
1724171ae1a4SAndy Whitcroft			$type = 'N';
1725171ae1a4SAndy Whitcroft
17266c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1727c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
17286c72ffaaSAndy Whitcroft			if (defined $2) {
1729cf655043SAndy Whitcroft				$av_pending = 'V';
17306c72ffaaSAndy Whitcroft			}
17316c72ffaaSAndy Whitcroft			$type = 'N';
17326c72ffaaSAndy Whitcroft
173314b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1734c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
173514b111c1SAndy Whitcroft			$av_pending = 'E';
17366c72ffaaSAndy Whitcroft			$type = 'N';
17376c72ffaaSAndy Whitcroft
17381f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
17391f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
17401f65f947SAndy Whitcroft			$av_pend_colon = 'C';
17411f65f947SAndy Whitcroft			$type = 'N';
17421f65f947SAndy Whitcroft
174314b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1744c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
17456c72ffaaSAndy Whitcroft			$type = 'N';
17466c72ffaaSAndy Whitcroft
17476c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1748c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1749cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1750cf655043SAndy Whitcroft			$av_pending = '_';
17516c72ffaaSAndy Whitcroft			$type = 'N';
17526c72ffaaSAndy Whitcroft
17536c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1754cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1755cf655043SAndy Whitcroft			if ($new_type ne '_') {
1756cf655043SAndy Whitcroft				$type = $new_type;
1757c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1758c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
17596c72ffaaSAndy Whitcroft			} else {
1760c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
17616c72ffaaSAndy Whitcroft			}
17626c72ffaaSAndy Whitcroft
1763c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1764c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1765c8cb2ca3SAndy Whitcroft			$type = 'V';
1766cf655043SAndy Whitcroft			$av_pending = 'V';
17676c72ffaaSAndy Whitcroft
17688e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
17698e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
17701f65f947SAndy Whitcroft				$av_pend_colon = 'B';
17718e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
17728e761b04SAndy Whitcroft				$av_pend_colon = 'L';
17731f65f947SAndy Whitcroft			}
17741f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
17751f65f947SAndy Whitcroft			$type = 'V';
17761f65f947SAndy Whitcroft
17776c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1778c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
17796c72ffaaSAndy Whitcroft			$type = 'V';
17806c72ffaaSAndy Whitcroft
17816c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1782c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
17836c72ffaaSAndy Whitcroft			$type = 'N';
17846c72ffaaSAndy Whitcroft
1785cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1786c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
178713214adfSAndy Whitcroft			$type = 'E';
17881f65f947SAndy Whitcroft			$av_pend_colon = 'O';
178913214adfSAndy Whitcroft
17908e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
17918e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
17928e761b04SAndy Whitcroft			$type = 'C';
17938e761b04SAndy Whitcroft
17941f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
17951f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
17961f65f947SAndy Whitcroft			$type = 'N';
17971f65f947SAndy Whitcroft
17981f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
17991f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
18001f65f947SAndy Whitcroft
18011f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
18021f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
18031f65f947SAndy Whitcroft				$type = 'E';
18041f65f947SAndy Whitcroft			} else {
18051f65f947SAndy Whitcroft				$type = 'N';
18061f65f947SAndy Whitcroft			}
18071f65f947SAndy Whitcroft			$av_pend_colon = 'O';
18081f65f947SAndy Whitcroft
18098e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
181013214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
18116c72ffaaSAndy Whitcroft			$type = 'N';
18126c72ffaaSAndy Whitcroft
18130d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
181474048ed8SAndy Whitcroft			my $variant;
181574048ed8SAndy Whitcroft
181674048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
181774048ed8SAndy Whitcroft			if ($type eq 'V') {
181874048ed8SAndy Whitcroft				$variant = 'B';
181974048ed8SAndy Whitcroft			} else {
182074048ed8SAndy Whitcroft				$variant = 'U';
182174048ed8SAndy Whitcroft			}
182274048ed8SAndy Whitcroft
182374048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
182474048ed8SAndy Whitcroft			$type = 'N';
182574048ed8SAndy Whitcroft
18266c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1827c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
18286c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
18296c72ffaaSAndy Whitcroft				$type = 'N';
18306c72ffaaSAndy Whitcroft			}
18316c72ffaaSAndy Whitcroft
18326c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1833c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
18346c72ffaaSAndy Whitcroft		}
18356c72ffaaSAndy Whitcroft		if (defined $1) {
18366c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
18376c72ffaaSAndy Whitcroft			$res .= $type x length($1);
18386c72ffaaSAndy Whitcroft		}
18396c72ffaaSAndy Whitcroft	}
18406c72ffaaSAndy Whitcroft
18411f65f947SAndy Whitcroft	return ($res, $var);
18426c72ffaaSAndy Whitcroft}
18436c72ffaaSAndy Whitcroft
18448905a67cSAndy Whitcroftsub possible {
184513214adfSAndy Whitcroft	my ($possible, $line) = @_;
18469a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
18470776e594SAndy Whitcroft		^(?:
18480776e594SAndy Whitcroft			$Modifier|
18490776e594SAndy Whitcroft			$Storage|
18500776e594SAndy Whitcroft			$Type|
18519a974fdbSAndy Whitcroft			DEFINE_\S+
18529a974fdbSAndy Whitcroft		)$|
18539a974fdbSAndy Whitcroft		^(?:
18540776e594SAndy Whitcroft			goto|
18550776e594SAndy Whitcroft			return|
18560776e594SAndy Whitcroft			case|
18570776e594SAndy Whitcroft			else|
18580776e594SAndy Whitcroft			asm|__asm__|
185989a88353SAndy Whitcroft			do|
186089a88353SAndy Whitcroft			\#|
186189a88353SAndy Whitcroft			\#\#|
18629a974fdbSAndy Whitcroft		)(?:\s|$)|
18630776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
18649a974fdbSAndy Whitcroft	    )}x;
18659a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
18669a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1867c45dcabdSAndy Whitcroft		# Check for modifiers.
1868c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1869c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1870c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1871c45dcabdSAndy Whitcroft
1872c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1873c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1874d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
18759a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1876d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1877485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1878d2506586SAndy Whitcroft				}
18799a974fdbSAndy Whitcroft			}
1880c45dcabdSAndy Whitcroft
1881c45dcabdSAndy Whitcroft		} else {
188213214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1883485ff23eSAlex Dowad			push(@typeListFile, $possible);
1884c45dcabdSAndy Whitcroft		}
18858905a67cSAndy Whitcroft		build_types();
18860776e594SAndy Whitcroft	} else {
18870776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
18888905a67cSAndy Whitcroft	}
18898905a67cSAndy Whitcroft}
18908905a67cSAndy Whitcroft
18916c72ffaaSAndy Whitcroftmy $prefix = '';
18926c72ffaaSAndy Whitcroft
1893000d1cc1SJoe Perchessub show_type {
1894cbec18afSJoe Perches	my ($type) = @_;
189591bfe484SJoe Perches
1896522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
1897522b837cSAlexey Dobriyan
1898cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1899cbec18afSJoe Perches
1900cbec18afSJoe Perches	return !defined $ignore_type{$type};
1901000d1cc1SJoe Perches}
1902000d1cc1SJoe Perches
1903f0a594c1SAndy Whitcroftsub report {
1904cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1905cbec18afSJoe Perches
1906cbec18afSJoe Perches	if (!show_type($type) ||
1907cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1908773647a0SAndy Whitcroft		return 0;
1909773647a0SAndy Whitcroft	}
191057230297SJoe Perches	my $output = '';
1911737c0767SJohn Brooks	if ($color) {
191257230297SJoe Perches		if ($level eq 'ERROR') {
191357230297SJoe Perches			$output .= RED;
191457230297SJoe Perches		} elsif ($level eq 'WARNING') {
191557230297SJoe Perches			$output .= YELLOW;
1916000d1cc1SJoe Perches		} else {
191757230297SJoe Perches			$output .= GREEN;
1918000d1cc1SJoe Perches		}
191957230297SJoe Perches	}
192057230297SJoe Perches	$output .= $prefix . $level . ':';
192157230297SJoe Perches	if ($show_types) {
1922737c0767SJohn Brooks		$output .= BLUE if ($color);
192357230297SJoe Perches		$output .= "$type:";
192457230297SJoe Perches	}
1925737c0767SJohn Brooks	$output .= RESET if ($color);
192657230297SJoe Perches	$output .= ' ' . $msg . "\n";
192734d8815fSJoe Perches
192834d8815fSJoe Perches	if ($showfile) {
192934d8815fSJoe Perches		my @lines = split("\n", $output, -1);
193034d8815fSJoe Perches		splice(@lines, 1, 1);
193134d8815fSJoe Perches		$output = join("\n", @lines);
193234d8815fSJoe Perches	}
193357230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
19348905a67cSAndy Whitcroft
193557230297SJoe Perches	push(our @report, $output);
1936773647a0SAndy Whitcroft
1937773647a0SAndy Whitcroft	return 1;
1938f0a594c1SAndy Whitcroft}
1939cbec18afSJoe Perches
1940f0a594c1SAndy Whitcroftsub report_dump {
194113214adfSAndy Whitcroft	our @report;
1942f0a594c1SAndy Whitcroft}
1943000d1cc1SJoe Perches
1944d752fcc8SJoe Perchessub fixup_current_range {
1945d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1946d752fcc8SJoe Perches
1947d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1948d752fcc8SJoe Perches		my $o = $1;
1949d752fcc8SJoe Perches		my $l = $2;
1950d752fcc8SJoe Perches		my $no = $o + $offset;
1951d752fcc8SJoe Perches		my $nl = $l + $length;
1952d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1953d752fcc8SJoe Perches	}
1954d752fcc8SJoe Perches}
1955d752fcc8SJoe Perches
1956d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1957d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1958d752fcc8SJoe Perches
1959d752fcc8SJoe Perches	my $range_last_linenr = 0;
1960d752fcc8SJoe Perches	my $delta_offset = 0;
1961d752fcc8SJoe Perches
1962d752fcc8SJoe Perches	my $old_linenr = 0;
1963d752fcc8SJoe Perches	my $new_linenr = 0;
1964d752fcc8SJoe Perches
1965d752fcc8SJoe Perches	my $next_insert = 0;
1966d752fcc8SJoe Perches	my $next_delete = 0;
1967d752fcc8SJoe Perches
1968d752fcc8SJoe Perches	my @lines = ();
1969d752fcc8SJoe Perches
1970d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1971d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1972d752fcc8SJoe Perches
1973d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1974d752fcc8SJoe Perches		my $save_line = 1;
1975d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1976323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1977d752fcc8SJoe Perches			$delta_offset = 0;
1978d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1979d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1980d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1981d752fcc8SJoe Perches		}
1982d752fcc8SJoe Perches
1983d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1984d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1985d752fcc8SJoe Perches			$save_line = 0;
1986d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1987d752fcc8SJoe Perches		}
1988d752fcc8SJoe Perches
1989d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1990d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1991d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1992d752fcc8SJoe Perches			$new_linenr++;
1993d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1994d752fcc8SJoe Perches		}
1995d752fcc8SJoe Perches
1996d752fcc8SJoe Perches		if ($save_line) {
1997d752fcc8SJoe Perches			push(@lines, $line);
1998d752fcc8SJoe Perches			$new_linenr++;
1999d752fcc8SJoe Perches		}
2000d752fcc8SJoe Perches
2001d752fcc8SJoe Perches		$old_linenr++;
2002d752fcc8SJoe Perches	}
2003d752fcc8SJoe Perches
2004d752fcc8SJoe Perches	return @lines;
2005d752fcc8SJoe Perches}
2006d752fcc8SJoe Perches
2007f2d7e4d4SJoe Perchessub fix_insert_line {
2008f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2009f2d7e4d4SJoe Perches
2010f2d7e4d4SJoe Perches	my $inserted = {
2011f2d7e4d4SJoe Perches		LINENR => $linenr,
2012f2d7e4d4SJoe Perches		LINE => $line,
2013f2d7e4d4SJoe Perches	};
2014f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2015f2d7e4d4SJoe Perches}
2016f2d7e4d4SJoe Perches
2017f2d7e4d4SJoe Perchessub fix_delete_line {
2018f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2019f2d7e4d4SJoe Perches
2020f2d7e4d4SJoe Perches	my $deleted = {
2021f2d7e4d4SJoe Perches		LINENR => $linenr,
2022f2d7e4d4SJoe Perches		LINE => $line,
2023f2d7e4d4SJoe Perches	};
2024f2d7e4d4SJoe Perches
2025f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2026f2d7e4d4SJoe Perches}
2027f2d7e4d4SJoe Perches
2028de7d4f0eSAndy Whitcroftsub ERROR {
2029cbec18afSJoe Perches	my ($type, $msg) = @_;
2030cbec18afSJoe Perches
2031cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2032de7d4f0eSAndy Whitcroft		our $clean = 0;
20336c72ffaaSAndy Whitcroft		our $cnt_error++;
20343705ce5bSJoe Perches		return 1;
2035de7d4f0eSAndy Whitcroft	}
20363705ce5bSJoe Perches	return 0;
2037773647a0SAndy Whitcroft}
2038de7d4f0eSAndy Whitcroftsub WARN {
2039cbec18afSJoe Perches	my ($type, $msg) = @_;
2040cbec18afSJoe Perches
2041cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2042de7d4f0eSAndy Whitcroft		our $clean = 0;
20436c72ffaaSAndy Whitcroft		our $cnt_warn++;
20443705ce5bSJoe Perches		return 1;
2045de7d4f0eSAndy Whitcroft	}
20463705ce5bSJoe Perches	return 0;
2047773647a0SAndy Whitcroft}
2048de7d4f0eSAndy Whitcroftsub CHK {
2049cbec18afSJoe Perches	my ($type, $msg) = @_;
2050cbec18afSJoe Perches
2051cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2052de7d4f0eSAndy Whitcroft		our $clean = 0;
20536c72ffaaSAndy Whitcroft		our $cnt_chk++;
20543705ce5bSJoe Perches		return 1;
20556c72ffaaSAndy Whitcroft	}
20563705ce5bSJoe Perches	return 0;
2057de7d4f0eSAndy Whitcroft}
2058de7d4f0eSAndy Whitcroft
20596ecd9674SAndy Whitcroftsub check_absolute_file {
20606ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
20616ecd9674SAndy Whitcroft	my $file = $absolute;
20626ecd9674SAndy Whitcroft
20636ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
20646ecd9674SAndy Whitcroft
20656ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
20666ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
20676ecd9674SAndy Whitcroft		if (-f "$root/$file") {
20686ecd9674SAndy Whitcroft			##print "file<$file>\n";
20696ecd9674SAndy Whitcroft			last;
20706ecd9674SAndy Whitcroft		}
20716ecd9674SAndy Whitcroft	}
20726ecd9674SAndy Whitcroft	if (! -f _)  {
20736ecd9674SAndy Whitcroft		return 0;
20746ecd9674SAndy Whitcroft	}
20756ecd9674SAndy Whitcroft
20766ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
20776ecd9674SAndy Whitcroft	my $prefix = $absolute;
20786ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
20796ecd9674SAndy Whitcroft
20806ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
20816ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2082000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2083000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
20846ecd9674SAndy Whitcroft	}
20856ecd9674SAndy Whitcroft}
20866ecd9674SAndy Whitcroft
20873705ce5bSJoe Perchessub trim {
20883705ce5bSJoe Perches	my ($string) = @_;
20893705ce5bSJoe Perches
2090b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2091b34c648bSJoe Perches
2092b34c648bSJoe Perches	return $string;
2093b34c648bSJoe Perches}
2094b34c648bSJoe Perches
2095b34c648bSJoe Perchessub ltrim {
2096b34c648bSJoe Perches	my ($string) = @_;
2097b34c648bSJoe Perches
2098b34c648bSJoe Perches	$string =~ s/^\s+//;
2099b34c648bSJoe Perches
2100b34c648bSJoe Perches	return $string;
2101b34c648bSJoe Perches}
2102b34c648bSJoe Perches
2103b34c648bSJoe Perchessub rtrim {
2104b34c648bSJoe Perches	my ($string) = @_;
2105b34c648bSJoe Perches
2106b34c648bSJoe Perches	$string =~ s/\s+$//;
21073705ce5bSJoe Perches
21083705ce5bSJoe Perches	return $string;
21093705ce5bSJoe Perches}
21103705ce5bSJoe Perches
211152ea8506SJoe Perchessub string_find_replace {
211252ea8506SJoe Perches	my ($string, $find, $replace) = @_;
211352ea8506SJoe Perches
211452ea8506SJoe Perches	$string =~ s/$find/$replace/g;
211552ea8506SJoe Perches
211652ea8506SJoe Perches	return $string;
211752ea8506SJoe Perches}
211852ea8506SJoe Perches
21193705ce5bSJoe Perchessub tabify {
21203705ce5bSJoe Perches	my ($leading) = @_;
21213705ce5bSJoe Perches
21223705ce5bSJoe Perches	my $source_indent = 8;
21233705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
21243705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
21253705ce5bSJoe Perches
21263705ce5bSJoe Perches	#convert leading spaces to tabs
21273705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
21283705ce5bSJoe Perches	#Remove spaces before a tab
21293705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
21303705ce5bSJoe Perches
21313705ce5bSJoe Perches	return "$leading";
21323705ce5bSJoe Perches}
21333705ce5bSJoe Perches
2134d1fe9c09SJoe Perchessub pos_last_openparen {
2135d1fe9c09SJoe Perches	my ($line) = @_;
2136d1fe9c09SJoe Perches
2137d1fe9c09SJoe Perches	my $pos = 0;
2138d1fe9c09SJoe Perches
2139d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2140d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2141d1fe9c09SJoe Perches
2142d1fe9c09SJoe Perches	my $last_openparen = 0;
2143d1fe9c09SJoe Perches
2144d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2145d1fe9c09SJoe Perches		return -1;
2146d1fe9c09SJoe Perches	}
2147d1fe9c09SJoe Perches
2148d1fe9c09SJoe Perches	my $len = length($line);
2149d1fe9c09SJoe Perches
2150d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2151d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2152d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2153d1fe9c09SJoe Perches			$pos += length($1) - 1;
2154d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2155d1fe9c09SJoe Perches			$last_openparen = $pos;
2156d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2157d1fe9c09SJoe Perches			last;
2158d1fe9c09SJoe Perches		}
2159d1fe9c09SJoe Perches	}
2160d1fe9c09SJoe Perches
216191cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2162d1fe9c09SJoe Perches}
2163d1fe9c09SJoe Perches
21640a920b5bSAndy Whitcroftsub process {
21650a920b5bSAndy Whitcroft	my $filename = shift;
21660a920b5bSAndy Whitcroft
21670a920b5bSAndy Whitcroft	my $linenr=0;
21680a920b5bSAndy Whitcroft	my $prevline="";
2169c2fdda0dSAndy Whitcroft	my $prevrawline="";
21700a920b5bSAndy Whitcroft	my $stashline="";
2171c2fdda0dSAndy Whitcroft	my $stashrawline="";
21720a920b5bSAndy Whitcroft
21734a0df2efSAndy Whitcroft	my $length;
21740a920b5bSAndy Whitcroft	my $indent;
21750a920b5bSAndy Whitcroft	my $previndent=0;
21760a920b5bSAndy Whitcroft	my $stashindent=0;
21770a920b5bSAndy Whitcroft
2178de7d4f0eSAndy Whitcroft	our $clean = 1;
21790a920b5bSAndy Whitcroft	my $signoff = 0;
21800a920b5bSAndy Whitcroft	my $is_patch = 0;
218129ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
218215662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2183ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2184bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
21852a076f40SJoe Perches	my $commit_log_long_line = 0;
2186e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
218713f1937eSJoe Perches	my $reported_maintainer_file = 0;
2188fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2189fa64205dSPasi Savanainen
2190365dd4eaSJoe Perches	my $last_blank_line = 0;
21915e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2192365dd4eaSJoe Perches
219313214adfSAndy Whitcroft	our @report = ();
21946c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
21956c72ffaaSAndy Whitcroft	our $cnt_error = 0;
21966c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
21976c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
21986c72ffaaSAndy Whitcroft
21990a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
22000a920b5bSAndy Whitcroft	my $realfile = '';
22010a920b5bSAndy Whitcroft	my $realline = 0;
22020a920b5bSAndy Whitcroft	my $realcnt = 0;
22030a920b5bSAndy Whitcroft	my $here = '';
220477cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
22050a920b5bSAndy Whitcroft	my $in_comment = 0;
2206c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
22070a920b5bSAndy Whitcroft	my $first_line = 0;
22081e855726SWolfram Sang	my $p1_prefix = '';
22090a920b5bSAndy Whitcroft
221013214adfSAndy Whitcroft	my $prev_values = 'E';
221113214adfSAndy Whitcroft
221213214adfSAndy Whitcroft	# suppression flags
2213773647a0SAndy Whitcroft	my %suppress_ifbraces;
2214170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
22152b474a1aSAndy Whitcroft	my %suppress_export;
22163e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2217653d4876SAndy Whitcroft
22187e51f197SJoe Perches	my %signatures = ();
2219323c1260SJoe Perches
2220c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2221de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2222c2fdda0dSAndy Whitcroft	#
2223de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2224de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2225773647a0SAndy Whitcroft
2226d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2227d8b07710SJoe Perches
2228773647a0SAndy Whitcroft	sanitise_line_reset();
2229c2fdda0dSAndy Whitcroft	my $line;
2230c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2231773647a0SAndy Whitcroft		$linenr++;
2232773647a0SAndy Whitcroft		$line = $rawline;
2233c2fdda0dSAndy Whitcroft
22343705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
22353705ce5bSJoe Perches
2236773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2237de7d4f0eSAndy Whitcroft			$setup_docs = 0;
22388c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2239de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2240de7d4f0eSAndy Whitcroft			}
2241773647a0SAndy Whitcroft			#next;
2242de7d4f0eSAndy Whitcroft		}
224374fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2244773647a0SAndy Whitcroft			$realline=$1-1;
2245773647a0SAndy Whitcroft			if (defined $2) {
2246773647a0SAndy Whitcroft				$realcnt=$3+1;
2247773647a0SAndy Whitcroft			} else {
2248773647a0SAndy Whitcroft				$realcnt=1+1;
2249773647a0SAndy Whitcroft			}
2250c45dcabdSAndy Whitcroft			$in_comment = 0;
2251773647a0SAndy Whitcroft
2252773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2253773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2254773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2255773647a0SAndy Whitcroft			# at context start.
2256773647a0SAndy Whitcroft			my $edge;
225701fa9147SAndy Whitcroft			my $cnt = $realcnt;
225801fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
225901fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
226001fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
226101fa9147SAndy Whitcroft				$cnt--;
226201fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2263721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2264fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2265fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2266fae17daeSAndy Whitcroft					($edge) = $1;
2267fae17daeSAndy Whitcroft					last;
2268fae17daeSAndy Whitcroft				}
2269773647a0SAndy Whitcroft			}
2270773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2271773647a0SAndy Whitcroft				$in_comment = 1;
2272773647a0SAndy Whitcroft			}
2273773647a0SAndy Whitcroft
2274773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2275773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2276773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2277773647a0SAndy Whitcroft			if (!defined $edge &&
227883242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2279773647a0SAndy Whitcroft			{
2280773647a0SAndy Whitcroft				$in_comment = 1;
2281773647a0SAndy Whitcroft			}
2282773647a0SAndy Whitcroft
2283773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2284773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2285773647a0SAndy Whitcroft
2286171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2287773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2288171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2289773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2290773647a0SAndy Whitcroft		}
2291773647a0SAndy Whitcroft		push(@lines, $line);
2292773647a0SAndy Whitcroft
2293773647a0SAndy Whitcroft		if ($realcnt > 1) {
2294773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2295773647a0SAndy Whitcroft		} else {
2296773647a0SAndy Whitcroft			$realcnt = 0;
2297773647a0SAndy Whitcroft		}
2298773647a0SAndy Whitcroft
2299773647a0SAndy Whitcroft		#print "==>$rawline\n";
2300773647a0SAndy Whitcroft		#print "-->$line\n";
2301de7d4f0eSAndy Whitcroft
2302de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2303de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2304de7d4f0eSAndy Whitcroft		}
2305de7d4f0eSAndy Whitcroft	}
2306de7d4f0eSAndy Whitcroft
23076c72ffaaSAndy Whitcroft	$prefix = '';
23086c72ffaaSAndy Whitcroft
2309773647a0SAndy Whitcroft	$realcnt = 0;
2310773647a0SAndy Whitcroft	$linenr = 0;
2311194f66fcSJoe Perches	$fixlinenr = -1;
23120a920b5bSAndy Whitcroft	foreach my $line (@lines) {
23130a920b5bSAndy Whitcroft		$linenr++;
2314194f66fcSJoe Perches		$fixlinenr++;
23151b5539b1SJoe Perches		my $sline = $line;	#copy of $line
23161b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
23170a920b5bSAndy Whitcroft
2318c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
23196c72ffaaSAndy Whitcroft
23200a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2321e518e9a5SJoe Perches		if (!$in_commit_log &&
232274fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
232374fd4f34SJoe Perches			my $context = $4;
23240a920b5bSAndy Whitcroft			$is_patch = 1;
23254a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
23260a920b5bSAndy Whitcroft			$realline=$1-1;
23270a920b5bSAndy Whitcroft			if (defined $2) {
23280a920b5bSAndy Whitcroft				$realcnt=$3+1;
23290a920b5bSAndy Whitcroft			} else {
23300a920b5bSAndy Whitcroft				$realcnt=1+1;
23310a920b5bSAndy Whitcroft			}
2332c2fdda0dSAndy Whitcroft			annotate_reset();
233313214adfSAndy Whitcroft			$prev_values = 'E';
233413214adfSAndy Whitcroft
2335773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2336170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
23372b474a1aSAndy Whitcroft			%suppress_export = ();
23383e469cdcSAndy Whitcroft			$suppress_statement = 0;
233974fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
234074fd4f34SJoe Perches				$context_function = $1;
234174fd4f34SJoe Perches			} else {
234274fd4f34SJoe Perches				undef $context_function;
234374fd4f34SJoe Perches			}
23440a920b5bSAndy Whitcroft			next;
23450a920b5bSAndy Whitcroft
23464a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
23474a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
23484a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2349773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
23500a920b5bSAndy Whitcroft			$realline++;
2351d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
23520a920b5bSAndy Whitcroft
23534a0df2efSAndy Whitcroft			# Measure the line length and indent.
2354c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
23550a920b5bSAndy Whitcroft
23560a920b5bSAndy Whitcroft			# Track the previous line.
23570a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
23580a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2359c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2360c2fdda0dSAndy Whitcroft
2361773647a0SAndy Whitcroft			#warn "line<$line>\n";
23626c72ffaaSAndy Whitcroft
2363d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2364d8aaf121SAndy Whitcroft			$realcnt--;
23650a920b5bSAndy Whitcroft		}
23660a920b5bSAndy Whitcroft
2367cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2368cc77cdcaSAndy Whitcroft
23696c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
23706c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2371773647a0SAndy Whitcroft
23722ac73b4fSJoe Perches		my $found_file = 0;
2373773647a0SAndy Whitcroft		# extract the filename as it passes
23743bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
23753bf9a009SRabin Vincent			$realfile = $1;
23762b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2377270c49a0SJoe Perches			$in_commit_log = 0;
23782ac73b4fSJoe Perches			$found_file = 1;
23793bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2380773647a0SAndy Whitcroft			$realfile = $1;
23812b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2382270c49a0SJoe Perches			$in_commit_log = 0;
23831e855726SWolfram Sang
23841e855726SWolfram Sang			$p1_prefix = $1;
2385e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2386e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2387000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2388000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
23891e855726SWolfram Sang			}
2390773647a0SAndy Whitcroft
2391c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2392000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2393000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2394773647a0SAndy Whitcroft			}
23952ac73b4fSJoe Perches			$found_file = 1;
23962ac73b4fSJoe Perches		}
23972ac73b4fSJoe Perches
239834d8815fSJoe Perches#make up the handle for any error we report on this line
239934d8815fSJoe Perches		if ($showfile) {
240034d8815fSJoe Perches			$prefix = "$realfile:$realline: "
240134d8815fSJoe Perches		} elsif ($emacs) {
24027d3a9f67SJoe Perches			if ($file) {
24037d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
24047d3a9f67SJoe Perches			} else {
240534d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
240634d8815fSJoe Perches			}
24077d3a9f67SJoe Perches		}
240834d8815fSJoe Perches
24092ac73b4fSJoe Perches		if ($found_file) {
241085b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
241185b0ee18SJoe Perches				WARN("OBSOLETE",
241285b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
241385b0ee18SJoe Perches			}
24147bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
24152ac73b4fSJoe Perches				$check = 1;
24162ac73b4fSJoe Perches			} else {
24172ac73b4fSJoe Perches				$check = $check_orig;
24182ac73b4fSJoe Perches			}
2419773647a0SAndy Whitcroft			next;
2420773647a0SAndy Whitcroft		}
2421773647a0SAndy Whitcroft
2422389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
24230a920b5bSAndy Whitcroft
2424c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2425c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2426c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
24270a920b5bSAndy Whitcroft
24286c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
24296c72ffaaSAndy Whitcroft
2430e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2431e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2432e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2433e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2434e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2435e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2436e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2437e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2438e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2439e518e9a5SJoe Perches		}
2440e518e9a5SJoe Perches
24413bf9a009SRabin Vincent# Check for incorrect file permissions
24423bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
24433bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
244404db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
244504db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2446000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2447000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
24483bf9a009SRabin Vincent			}
24493bf9a009SRabin Vincent		}
24503bf9a009SRabin Vincent
245120112475SJoe Perches# Check the patch for a signoff:
2452d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
24534a0df2efSAndy Whitcroft			$signoff++;
245415662b3eSJoe Perches			$in_commit_log = 0;
24550a920b5bSAndy Whitcroft		}
245620112475SJoe Perches
2457e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2458e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2459e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2460e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2461e0d975b1SJoe Perches		}
2462e0d975b1SJoe Perches
246320112475SJoe Perches# Check signature styles
2464270c49a0SJoe Perches		if (!$in_header_lines &&
2465ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
246620112475SJoe Perches			my $space_before = $1;
246720112475SJoe Perches			my $sign_off = $2;
246820112475SJoe Perches			my $space_after = $3;
246920112475SJoe Perches			my $email = $4;
247020112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
247120112475SJoe Perches
2472ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2473ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2474ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2475ce0338dfSJoe Perches			}
247620112475SJoe Perches			if (defined $space_before && $space_before ne "") {
24773705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24783705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
24793705ce5bSJoe Perches				    $fix) {
2480194f66fcSJoe Perches					$fixed[$fixlinenr] =
24813705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24823705ce5bSJoe Perches				}
248320112475SJoe Perches			}
248420112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
24853705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24863705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
24873705ce5bSJoe Perches				    $fix) {
2488194f66fcSJoe Perches					$fixed[$fixlinenr] =
24893705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24903705ce5bSJoe Perches				}
24913705ce5bSJoe Perches
249220112475SJoe Perches			}
249320112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
24943705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24953705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
24963705ce5bSJoe Perches				    $fix) {
2497194f66fcSJoe Perches					$fixed[$fixlinenr] =
24983705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24993705ce5bSJoe Perches				}
250020112475SJoe Perches			}
250120112475SJoe Perches
250220112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
250320112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
250420112475SJoe Perches			if ($suggested_email eq "") {
2505000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2506000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
250720112475SJoe Perches			} else {
250820112475SJoe Perches				my $dequoted = $suggested_email;
250920112475SJoe Perches				$dequoted =~ s/^"//;
251020112475SJoe Perches				$dequoted =~ s/" </ </;
251120112475SJoe Perches				# Don't force email to have quotes
251220112475SJoe Perches				# Allow just an angle bracketed address
251320112475SJoe Perches				if ("$dequoted$comment" ne $email &&
251420112475SJoe Perches				    "<$email_address>$comment" ne $email &&
251520112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2516000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2517000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
251820112475SJoe Perches				}
25190a920b5bSAndy Whitcroft			}
25207e51f197SJoe Perches
25217e51f197SJoe Perches# Check for duplicate signatures
25227e51f197SJoe Perches			my $sig_nospace = $line;
25237e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
25247e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
25257e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
25267e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
25277e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
25287e51f197SJoe Perches			} else {
25297e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
25307e51f197SJoe Perches			}
25310a920b5bSAndy Whitcroft		}
25320a920b5bSAndy Whitcroft
2533a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2534a2fe16b9SJoe Perches		if ($in_header_lines &&
2535a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2536a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2537a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2538a2fe16b9SJoe Perches		}
2539a2fe16b9SJoe Perches
25409b3189ebSJoe Perches# Check for old stable address
25419b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
25429b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
25439b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
25449b3189ebSJoe Perches		}
25459b3189ebSJoe Perches
25467ebd05efSChristopher Covington# Check for unwanted Gerrit info
25477ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
25487ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
25497ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
25507ebd05efSChristopher Covington		}
25517ebd05efSChristopher Covington
2552369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2553369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2554369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2555369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2556369c8dd3SJoe Perches					# timestamp
2557369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2558369c8dd3SJoe Perches					# stack dump address
2559369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2560369c8dd3SJoe Perches		}
2561369c8dd3SJoe Perches
25622a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
25632a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2564bf4daf12SJoe Perches		    length($line) > 75 &&
2565bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2566bf4daf12SJoe Perches					# file delta changes
2567bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2568bf4daf12SJoe Perches					# filename then :
2569bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2570bf4daf12SJoe Perches					# A Fixes: or Link: line
2571bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
25722a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
25732a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
25742a076f40SJoe Perches			$commit_log_long_line = 1;
25752a076f40SJoe Perches		}
25762a076f40SJoe Perches
2577bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2578bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2579bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2580bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2581bf4daf12SJoe Perches		}
2582bf4daf12SJoe Perches
25830d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2584369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2585aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2586e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2587fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2588aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2589369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2590bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2591fe043ea1SJoe Perches			my $init_char = "c";
2592fe043ea1SJoe Perches			my $orig_commit = "";
25930d7835fcSJoe Perches			my $short = 1;
25940d7835fcSJoe Perches			my $long = 0;
25950d7835fcSJoe Perches			my $case = 1;
25960d7835fcSJoe Perches			my $space = 1;
25970d7835fcSJoe Perches			my $hasdesc = 0;
259819c146a6SJoe Perches			my $hasparens = 0;
25990d7835fcSJoe Perches			my $id = '0123456789ab';
26000d7835fcSJoe Perches			my $orig_desc = "commit description";
26010d7835fcSJoe Perches			my $description = "";
26020d7835fcSJoe Perches
2603fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2604fe043ea1SJoe Perches				$init_char = $1;
2605fe043ea1SJoe Perches				$orig_commit = lc($2);
2606fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2607fe043ea1SJoe Perches				$orig_commit = lc($1);
2608fe043ea1SJoe Perches			}
2609fe043ea1SJoe Perches
26100d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
26110d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
26120d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
26130d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
26140d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
26150d7835fcSJoe Perches				$orig_desc = $1;
261619c146a6SJoe Perches				$hasparens = 1;
26170d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
26180d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
26190d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
26200d7835fcSJoe Perches				$orig_desc = $1;
262119c146a6SJoe Perches				$hasparens = 1;
2622b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2623b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2624b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2625b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2626b671fde0SJoe Perches				$orig_desc = $1;
2627b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2628b671fde0SJoe Perches				$orig_desc .= " " . $1;
262919c146a6SJoe Perches				$hasparens = 1;
26300d7835fcSJoe Perches			}
26310d7835fcSJoe Perches
26320d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
26330d7835fcSJoe Perches							      $id, $orig_desc);
26340d7835fcSJoe Perches
2635948b133aSHeinrich Schuchardt			if (defined($id) &&
2636948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2637d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
26380d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
26390d7835fcSJoe Perches			}
2640d311cd44SJoe Perches		}
2641d311cd44SJoe Perches
264213f1937eSJoe Perches# Check for added, moved or deleted files
264313f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
264413f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
264513f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
264613f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
264713f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2648a82603a8SAndrew Jeffery			$is_patch = 1;
264913f1937eSJoe Perches			$reported_maintainer_file = 1;
265013f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
265113f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
265213f1937eSJoe Perches		}
265313f1937eSJoe Perches
265400df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
26558905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2656000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2657000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
26586c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2659de7d4f0eSAndy Whitcroft		}
2660de7d4f0eSAndy Whitcroft
2661de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2662de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2663171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2664171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2665171ae1a4SAndy Whitcroft
2666171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2667171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2668171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2669171ae1a4SAndy Whitcroft
267034d99219SJoe Perches			CHK("INVALID_UTF8",
2671000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
267200df344fSAndy Whitcroft		}
26730a920b5bSAndy Whitcroft
267415662b3eSJoe Perches# Check if it's the start of a commit log
267515662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
267615662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2677eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2678eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
267915662b3eSJoe Perches			$in_header_lines = 0;
268015662b3eSJoe Perches			$in_commit_log = 1;
2681ed43c4e5SAllen Hubbe			$has_commit_log = 1;
268215662b3eSJoe Perches		}
268315662b3eSJoe Perches
2684fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2685fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2686fa64205dSPasi Savanainen		if ($in_header_lines &&
2687fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2688fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2689fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2690fa64205dSPasi Savanainen		}
2691fa64205dSPasi Savanainen
2692fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
269315662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2694fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
269515662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
269615662b3eSJoe Perches		}
269715662b3eSJoe Perches
2698d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2699d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2700d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2701d6430f71SJoe Perches				my $file = $1;
2702d6430f71SJoe Perches
2703d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2704d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2705d6430f71SJoe Perches					#
2706d6430f71SJoe Perches				} else {
2707d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2708d6430f71SJoe Perches				}
2709d6430f71SJoe Perches			}
2710d6430f71SJoe Perches		}
2711d6430f71SJoe Perches
271266b47b4aSKees Cook# Check for various typo / spelling mistakes
271366d7a382SJoe Perches		if (defined($misspellings) &&
271466d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2715ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
271666b47b4aSKees Cook				my $typo = $1;
271766b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
271866b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
271966b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
27200675a8fbSJean Delvare				my $msg_level = \&WARN;
27210675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
27220675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
272366b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
272466b47b4aSKees Cook				    $fix) {
272566b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
272666b47b4aSKees Cook				}
272766b47b4aSKees Cook			}
272866b47b4aSKees Cook		}
272966b47b4aSKees Cook
273030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
273130670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
273200df344fSAndy Whitcroft
27330a920b5bSAndy Whitcroft#trailing whitespace
27349c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2735c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2736d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2737d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2738d5e616fcSJoe Perches			    $fix) {
2739194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2740d5e616fcSJoe Perches			}
2741c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2742c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27433705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
27443705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
27453705ce5bSJoe Perches			    $fix) {
2746194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
27473705ce5bSJoe Perches			}
27483705ce5bSJoe Perches
2749d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
27500a920b5bSAndy Whitcroft		}
27515368df20SAndy Whitcroft
27524783f894SJosh Triplett# Check for FSF mailing addresses.
2753109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
27541bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
27553e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
27563e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
27574783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27580675a8fbSJean Delvare			my $msg_level = \&ERROR;
27590675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
27600675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
27614783f894SJosh 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)
27624783f894SJosh Triplett		}
27634783f894SJosh Triplett
27643354957aSAndi Kleen# check for Kconfig help text having a real description
27659fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
27669fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
27673354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
27688d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
27693354957aSAndi Kleen			my $length = 0;
27709fe287d7SAndy Whitcroft			my $cnt = $realcnt;
27719fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
27729fe287d7SAndy Whitcroft			my $f;
2773a1385803SAndy Whitcroft			my $is_start = 0;
27749fe287d7SAndy Whitcroft			my $is_end = 0;
2775a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
27769fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
27779fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
27789fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
27799fe287d7SAndy Whitcroft
27809fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
27818d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2782a1385803SAndy Whitcroft
27838d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2784a1385803SAndy Whitcroft					$is_start = 1;
27858d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2786a1385803SAndy Whitcroft					$length = -1;
2787a1385803SAndy Whitcroft				}
2788a1385803SAndy Whitcroft
27899fe287d7SAndy Whitcroft				$f =~ s/^.//;
27903354957aSAndi Kleen				$f =~ s/#.*//;
27913354957aSAndi Kleen				$f =~ s/^\s+//;
27923354957aSAndi Kleen				next if ($f =~ /^$/);
27939fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
27949fe287d7SAndy Whitcroft					$is_end = 1;
27959fe287d7SAndy Whitcroft					last;
27969fe287d7SAndy Whitcroft				}
27973354957aSAndi Kleen				$length++;
27983354957aSAndi Kleen			}
279956193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2800000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
280156193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
280256193274SVadim Bendebury			}
2803a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
28043354957aSAndi Kleen		}
28053354957aSAndi Kleen
2806628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
2807628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
2808628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
2809628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
2810628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
2811628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2812628f91a2SJoe Perches			    $fix) {
2813628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2814628f91a2SJoe Perches			}
2815628f91a2SJoe Perches		}
2816628f91a2SJoe Perches
2817327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2818327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2819327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2820327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2821327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2822327953e9SChristoph Jaeger		}
2823327953e9SChristoph Jaeger
2824c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2825c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2826c68e5878SArnaud Lacombe			my $flag = $1;
2827c68e5878SArnaud Lacombe			my $replacement = {
2828c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2829c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2830c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2831c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2832c68e5878SArnaud Lacombe			};
2833c68e5878SArnaud Lacombe
2834c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2835c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2836c68e5878SArnaud Lacombe		}
2837c68e5878SArnaud Lacombe
2838bff5da43SRob Herring# check for DT compatible documentation
28397dd05b38SFlorian Vaussard		if (defined $root &&
28407dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
28417dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
28427dd05b38SFlorian Vaussard
2843bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2844bff5da43SRob Herring
2845cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2846cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2847cc93319bSFlorian Vaussard
2848bff5da43SRob Herring			foreach my $compat (@compats) {
2849bff5da43SRob Herring				my $compat2 = $compat;
2850185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2851185d566bSRob Herring				my $compat3 = $compat;
2852185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2853185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2854bff5da43SRob Herring				if ( $? >> 8 ) {
2855bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2856bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2857bff5da43SRob Herring				}
2858bff5da43SRob Herring
28594fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
28604fbf32a6SFlorian Vaussard				my $vendor = $1;
2861cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2862bff5da43SRob Herring				if ( $? >> 8 ) {
2863bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2864cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2865bff5da43SRob Herring				}
2866bff5da43SRob Herring			}
2867bff5da43SRob Herring		}
2868bff5da43SRob Herring
28695368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2870d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
28715368df20SAndy Whitcroft
287247e0c88bSJoe Perches# line length limit (with some exclusions)
287347e0c88bSJoe Perches#
287447e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
287547e0c88bSJoe Perches#	logging functions like pr_info that end in a string
287647e0c88bSJoe Perches#	lines with a single string
287747e0c88bSJoe Perches#	#defines that are a single string
287847e0c88bSJoe Perches#
287947e0c88bSJoe Perches# There are 3 different line length message types:
2880ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
288147e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
288247e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
288347e0c88bSJoe Perches#
288447e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
288547e0c88bSJoe Perches#
288647e0c88bSJoe Perches
2887b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
288847e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
288947e0c88bSJoe Perches
289047e0c88bSJoe Perches			# Check the allowed long line types first
289147e0c88bSJoe Perches
289247e0c88bSJoe Perches			# logging functions that end in a string that starts
289347e0c88bSJoe Perches			# before $max_line_length
289447e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
289547e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
289647e0c88bSJoe Perches				$msg_type = "";
289747e0c88bSJoe Perches
289847e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
289947e0c88bSJoe Perches			# #defines with only strings
290047e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
290147e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
290247e0c88bSJoe Perches				$msg_type = "";
290347e0c88bSJoe Perches
2904cc147506SJoe Perches			# More special cases
2905cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
2906cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
2907d560a5f8SJoe Perches				$msg_type = "";
2908d560a5f8SJoe Perches
290947e0c88bSJoe Perches			# Otherwise set the alternate message types
291047e0c88bSJoe Perches
291147e0c88bSJoe Perches			# a comment starts before $max_line_length
291247e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
291347e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
291447e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
291547e0c88bSJoe Perches
291647e0c88bSJoe Perches			# a quoted string starts before $max_line_length
291747e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
291847e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
291947e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
292047e0c88bSJoe Perches			}
292147e0c88bSJoe Perches
292247e0c88bSJoe Perches			if ($msg_type ne "" &&
292347e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
292447e0c88bSJoe Perches				WARN($msg_type,
29256cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
29260a920b5bSAndy Whitcroft			}
292747e0c88bSJoe Perches		}
29280a920b5bSAndy Whitcroft
29298905a67cSAndy Whitcroft# check for adding lines without a newline.
29308905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2931000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2932000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
29338905a67cSAndy Whitcroft		}
29348905a67cSAndy Whitcroft
293542e41c54SMike Frysinger# Blackfin: use hi/lo macros
293642e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
293742e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
293842e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2939000d1cc1SJoe Perches				ERROR("LO_MACRO",
2940000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
294142e41c54SMike Frysinger			}
294242e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
294342e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2944000d1cc1SJoe Perches				ERROR("HI_MACRO",
2945000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
294642e41c54SMike Frysinger			}
294742e41c54SMike Frysinger		}
294842e41c54SMike Frysinger
2949b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2950de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
29510a920b5bSAndy Whitcroft
29520a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
29530a920b5bSAndy Whitcroft# more than 8 must use tabs.
2954c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2955c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2956c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2957d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29583705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
29593705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
29603705ce5bSJoe Perches			    $fix) {
2961194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
29623705ce5bSJoe Perches			}
29630a920b5bSAndy Whitcroft		}
29640a920b5bSAndy Whitcroft
296508e44365SAlberto Panizzo# check for space before tabs.
296608e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
296708e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29683705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
29693705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
29703705ce5bSJoe Perches			    $fix) {
2971194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2972d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2973194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2974c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
29753705ce5bSJoe Perches			}
297608e44365SAlberto Panizzo		}
297708e44365SAlberto Panizzo
2978d1fe9c09SJoe Perches# check for && or || at the start of a line
2979d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2980d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2981d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2982d1fe9c09SJoe Perches		}
2983d1fe9c09SJoe Perches
2984a91e8994SJoe Perches# check indentation starts on a tab stop
2985a91e8994SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2986a91e8994SJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2987a91e8994SJoe Perches			my $indent = length($1);
2988a91e8994SJoe Perches			if ($indent % 8) {
2989a91e8994SJoe Perches				if (WARN("TABSTOP",
2990a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
2991a91e8994SJoe Perches				    $fix) {
2992a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2993a91e8994SJoe Perches				}
2994a91e8994SJoe Perches			}
2995a91e8994SJoe Perches		}
2996a91e8994SJoe Perches
2997d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2998d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2999fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3000d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3001d1fe9c09SJoe Perches			my $oldindent = $1;
3002d1fe9c09SJoe Perches			my $rest = $2;
3003d1fe9c09SJoe Perches
3004d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3005d1fe9c09SJoe Perches			if ($pos >= 0) {
3006b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3007b34a26f3SJoe Perches				my $newindent = $2;
3008d1fe9c09SJoe Perches
3009d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3010d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3011d1fe9c09SJoe Perches					" "  x ($pos % 8);
3012d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3013d1fe9c09SJoe Perches
3014d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3015d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
30163705ce5bSJoe Perches
30173705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
30183705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
30193705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3020194f66fcSJoe Perches						$fixed[$fixlinenr] =~
30213705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
30223705ce5bSJoe Perches					}
3023d1fe9c09SJoe Perches				}
3024d1fe9c09SJoe Perches			}
3025d1fe9c09SJoe Perches		}
3026d1fe9c09SJoe Perches
30276ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
30286ab3a970SJoe Perches# avoid checking a few false positives:
30296ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
30306ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
30316ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
30326ab3a970SJoe Perches#   multiline macros that define functions
30336ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
30346ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
30356ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
30363705ce5bSJoe Perches			if (CHK("SPACING",
3037f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
30383705ce5bSJoe Perches			    $fix) {
3039194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3040f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
30413705ce5bSJoe Perches			}
3042aad4f614SJoe Perches		}
3043aad4f614SJoe Perches
304486406b1cSJoe Perches# Block comment styles
304586406b1cSJoe Perches# Networking with an initial /*
304605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3047fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
304885ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
304985ad978cSJoe Perches		    $realline > 2) {
305005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
305105880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
305205880600SJoe Perches		}
305305880600SJoe Perches
305486406b1cSJoe Perches# Block comments use * on subsequent lines
305586406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
305686406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3057a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
305861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3059a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
306086406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
306186406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3062a605e32eSJoe Perches		}
3063a605e32eSJoe Perches
306486406b1cSJoe Perches# Block comments use */ on trailing lines
306586406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3066c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3067c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3068c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
306986406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
307086406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
307105880600SJoe Perches		}
307205880600SJoe Perches
307308eb9b80SJoe Perches# Block comment * alignment
307408eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3075af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3076af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3077af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
307808eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3079af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3080af207524SJoe Perches			my $oldindent;
308108eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3082af207524SJoe Perches			if (defined($1)) {
3083af207524SJoe Perches				$oldindent = expand_tabs($1);
3084af207524SJoe Perches			} else {
3085af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3086af207524SJoe Perches				$oldindent = expand_tabs($1);
3087af207524SJoe Perches			}
308808eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
308908eb9b80SJoe Perches			my $newindent = $1;
309008eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3091af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
309208eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
309308eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
309408eb9b80SJoe Perches			}
309508eb9b80SJoe Perches		}
309608eb9b80SJoe Perches
30977f619191SJoe Perches# check for missing blank lines after struct/union declarations
30987f619191SJoe Perches# with exceptions for various attributes and macros
30997f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
31007f619191SJoe Perches		    $line =~ /^\+/ &&
31017f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
31027f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
31037f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
31047f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
31057f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
31067f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
31077f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
3108*0bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
31097f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3110d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3111d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3112d752fcc8SJoe Perches			    $fix) {
3113f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3114d752fcc8SJoe Perches			}
31157f619191SJoe Perches		}
31167f619191SJoe Perches
3117365dd4eaSJoe Perches# check for multiple consecutive blank lines
3118365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3119365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3120365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3121d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3122d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3123d752fcc8SJoe Perches			    $fix) {
3124f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3125d752fcc8SJoe Perches			}
3126d752fcc8SJoe Perches
3127365dd4eaSJoe Perches			$last_blank_line = $linenr;
3128365dd4eaSJoe Perches		}
3129365dd4eaSJoe Perches
31303b617e3bSJoe Perches# check for missing blank lines after declarations
31313f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
31323f7bac03SJoe Perches			# actual declarations
31333f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
31345a4e1fd3SJoe Perches			# function pointer declarations
31355a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
31363f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
31373f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
31383f7bac03SJoe Perches			# known declaration macros
31393f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
31403f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
31413f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
31423f7bac03SJoe Perches			# other possible extensions of declaration lines
31433f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
31443f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
31453f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
31463f7bac03SJoe Perches			# looks like a declaration
31473f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
31485a4e1fd3SJoe Perches			# function pointer declarations
31495a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
31503f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
31513f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
31523f7bac03SJoe Perches			# known declaration macros
31533f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
31543f7bac03SJoe Perches			# start of struct or union or enum
31553b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
31563f7bac03SJoe Perches			# start or end of block or continuation of declaration
31573f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
31583f7bac03SJoe Perches			# bitfield continuation
31593f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
31603f7bac03SJoe Perches			# other possible extensions of declaration lines
31613f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
31623f7bac03SJoe Perches			# indentation of previous and current line are the same
31633f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3164d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3165d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3166d752fcc8SJoe Perches			    $fix) {
3167f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3168d752fcc8SJoe Perches			}
31693b617e3bSJoe Perches		}
31703b617e3bSJoe Perches
31715f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
31726b4c5bebSAndy Whitcroft# Exceptions:
31736b4c5bebSAndy Whitcroft#  1) within comments
31746b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
31756b4c5bebSAndy Whitcroft#  3) hanging labels
31763705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
31775f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
31783705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
31793705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
31803705ce5bSJoe Perches			    $fix) {
3181194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
31823705ce5bSJoe Perches			}
31835f7ddae6SRaffaele Recalcati		}
31845f7ddae6SRaffaele Recalcati
3185b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3186b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3187b9ea10d6SAndy Whitcroft
31885751a24eSJoe Perches# check for unusual line ending [ or (
31895751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
31905751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
31915751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
31925751a24eSJoe Perches		}
31935751a24eSJoe Perches
31944dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
31954dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
31964dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
31974dbed76fSJoe Perches			$context_function = $1;
31984dbed76fSJoe Perches		}
31994dbed76fSJoe Perches
32004dbed76fSJoe Perches# check if this appears to be the end of function declaration
32014dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
32024dbed76fSJoe Perches			undef $context_function;
32034dbed76fSJoe Perches		}
32044dbed76fSJoe Perches
3205032a4c0fSJoe Perches# check indentation of any line with a bare else
3206840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3207032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3208032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3209032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3210840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3211840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3212840080a0SJoe Perches			     defined $lines[$linenr] &&
3213840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3214032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3215032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3216032a4c0fSJoe Perches			}
3217032a4c0fSJoe Perches		}
3218032a4c0fSJoe Perches
3219c00df19aSJoe Perches# check indentation of a line with a break;
3220c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3221c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3222c00df19aSJoe Perches			my $tabs = $1;
3223c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3224c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3225c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3226c00df19aSJoe Perches			}
3227c00df19aSJoe Perches		}
3228c00df19aSJoe Perches
3229c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3230cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3231000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3232000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3233c2fdda0dSAndy Whitcroft		}
323422f2a2efSAndy Whitcroft
323542e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
323642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
323742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3238000d1cc1SJoe Perches			ERROR("CSYNC",
3239000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
324042e41c54SMike Frysinger		}
324142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
324242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3243000d1cc1SJoe Perches			ERROR("SSYNC",
3244000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
324542e41c54SMike Frysinger		}
324642e41c54SMike Frysinger
324756e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
324856e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
324956e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
325056e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
325156e77d70SJoe Perches		}
325256e77d70SJoe Perches
32539c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
32542b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
32552b474a1aSAndy Whitcroft		    $realline_next);
32563e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3257ca819864SJoe Perches		if ($linenr > $suppress_statement &&
32581b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3259170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3260f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3261171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3262171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3263171ae1a4SAndy Whitcroft
32643e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
32653e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
32663e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
32673e469cdcSAndy Whitcroft			# until we hit end of it.
32683e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
32693e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
32703e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
32713e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
32723e469cdcSAndy Whitcroft			}
3273f74bd194SAndy Whitcroft
32742b474a1aSAndy Whitcroft			# Find the real next line.
32752b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
32762b474a1aSAndy Whitcroft			if (defined $realline_next &&
32772b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
32782b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
32792b474a1aSAndy Whitcroft				$realline_next++;
32802b474a1aSAndy Whitcroft			}
32812b474a1aSAndy Whitcroft
3282171ae1a4SAndy Whitcroft			my $s = $stat;
3283171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3284cf655043SAndy Whitcroft
3285c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3286171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3287c2fdda0dSAndy Whitcroft
3288c2fdda0dSAndy Whitcroft			# Ignore functions being called
3289171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3290c2fdda0dSAndy Whitcroft
3291463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3292463f2864SAndy Whitcroft
3293c45dcabdSAndy Whitcroft			# declarations always start with types
3294d2506586SAndy 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) {
3295c45dcabdSAndy Whitcroft				my $type = $1;
3296c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3297c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3298c45dcabdSAndy Whitcroft
32996c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3300a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3301c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3302c2fdda0dSAndy Whitcroft			}
33038905a67cSAndy Whitcroft
33046c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
330565863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3306c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
33079c0ca6f9SAndy Whitcroft			}
33088905a67cSAndy Whitcroft
33098905a67cSAndy Whitcroft			# Check for any sort of function declaration.
33108905a67cSAndy Whitcroft			# int foo(something bar, other baz);
33118905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3312171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
33138905a67cSAndy Whitcroft				my ($name_len) = length($1);
33148905a67cSAndy Whitcroft
3315cf655043SAndy Whitcroft				my $ctx = $s;
3316773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
33178905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3318cf655043SAndy Whitcroft
33198905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3320c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
33218905a67cSAndy Whitcroft
3322c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
33238905a67cSAndy Whitcroft					}
33248905a67cSAndy Whitcroft				}
33258905a67cSAndy Whitcroft			}
33268905a67cSAndy Whitcroft
33279c0ca6f9SAndy Whitcroft		}
33289c0ca6f9SAndy Whitcroft
332900df344fSAndy Whitcroft#
333000df344fSAndy Whitcroft# Checks which may be anchored in the context.
333100df344fSAndy Whitcroft#
333200df344fSAndy Whitcroft
333300df344fSAndy Whitcroft# Check for switch () and associated case and default
333400df344fSAndy Whitcroft# statements should be at the same indent.
333500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
333600df344fSAndy Whitcroft			my $err = '';
333700df344fSAndy Whitcroft			my $sep = '';
333800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
333900df344fSAndy Whitcroft			shift(@ctx);
334000df344fSAndy Whitcroft			for my $ctx (@ctx) {
334100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
334200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
334300df344fSAndy Whitcroft							$indent != $cindent) {
334400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
334500df344fSAndy Whitcroft					$sep = '';
334600df344fSAndy Whitcroft				} else {
334700df344fSAndy Whitcroft					$sep = "[...]\n";
334800df344fSAndy Whitcroft				}
334900df344fSAndy Whitcroft			}
335000df344fSAndy Whitcroft			if ($err ne '') {
3351000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3352000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3353de7d4f0eSAndy Whitcroft			}
3354de7d4f0eSAndy Whitcroft		}
3355de7d4f0eSAndy Whitcroft
3356de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3357de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
33580fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3359773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3360773647a0SAndy Whitcroft
33619c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
33628eef05ddSJoe Perches
33638eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
33648eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
33658eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
33668eef05ddSJoe Perches			}
33678eef05ddSJoe Perches
3368de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3369de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3370de7d4f0eSAndy Whitcroft
3371548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3372548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3373de7d4f0eSAndy Whitcroft
3374548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3375548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3376548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3377548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3378548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3379773647a0SAndy Whitcroft				$ctx_ln++;
3380773647a0SAndy Whitcroft			}
3381548596d5SAndy Whitcroft
338253210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
338353210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3384773647a0SAndy Whitcroft
3385773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3386000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3387000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
338801464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
338900df344fSAndy Whitcroft			}
3390773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3391773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3392773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3393773647a0SAndy Whitcroft			{
33949c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
33959c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3396000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3397000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
339801464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
33999c0ca6f9SAndy Whitcroft				}
34009c0ca6f9SAndy Whitcroft			}
340100df344fSAndy Whitcroft		}
340200df344fSAndy Whitcroft
34034d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3404f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
34053e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
34063e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
34073e469cdcSAndy Whitcroft					if (!defined $stat);
34084d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
34094d001e4dSAndy Whitcroft
34104d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
34114d001e4dSAndy Whitcroft
34129f5af480SJoe Perches			# remove inline comments
34139f5af480SJoe Perches			$s =~ s/$;/ /g;
34149f5af480SJoe Perches			$c =~ s/$;/ /g;
34154d001e4dSAndy Whitcroft
34164d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
34176f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
34186f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
34194d001e4dSAndy Whitcroft
34209f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
34219f5af480SJoe Perches			# none on the first line, and are going to readd them
34229f5af480SJoe Perches			# where necessary.
34239f5af480SJoe Perches			$s =~ s/\n./\n/gs;
34249f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
34259f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
34269f5af480SJoe Perches			}
34279f5af480SJoe Perches
34284d001e4dSAndy Whitcroft			# We want to check the first line inside the block
34294d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
34304d001e4dSAndy Whitcroft			#  1) any blank line termination
34314d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
34324d001e4dSAndy Whitcroft			#  3) any do (...) {
34334d001e4dSAndy Whitcroft			my $continuation = 0;
34344d001e4dSAndy Whitcroft			my $check = 0;
34354d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
34364d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
34374d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
34384d001e4dSAndy Whitcroft				$continuation = 1;
34394d001e4dSAndy Whitcroft			}
34409bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
34414d001e4dSAndy Whitcroft				$check = 1;
34424d001e4dSAndy Whitcroft				$cond_lines++;
34434d001e4dSAndy Whitcroft			}
34444d001e4dSAndy Whitcroft
34454d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
34464d001e4dSAndy Whitcroft			# preprocessor statement.
34474d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
34484d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
34494d001e4dSAndy Whitcroft				$check = 0;
34504d001e4dSAndy Whitcroft			}
34514d001e4dSAndy Whitcroft
34529bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3453740504c6SAndy Whitcroft			$continuation = 0;
34549bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
34559bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
34564d001e4dSAndy Whitcroft
3457f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3458f16fa28fSAndy Whitcroft				# is not linear.
3459f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3460f16fa28fSAndy Whitcroft					$check = 0;
3461f16fa28fSAndy Whitcroft				}
3462f16fa28fSAndy Whitcroft
34639bd49efeSAndy Whitcroft				# Ignore:
34649bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
34659bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
34669bd49efeSAndy Whitcroft				#  3) labels.
3467740504c6SAndy Whitcroft				if ($continuation ||
3468740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
34699bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
34709bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3471740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
347230dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
34739bd49efeSAndy Whitcroft						$cond_lines++;
34749bd49efeSAndy Whitcroft					}
34754d001e4dSAndy Whitcroft				}
347630dad6ebSAndy Whitcroft			}
34774d001e4dSAndy Whitcroft
34784d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
34794d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
34804d001e4dSAndy Whitcroft
34814d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
34824d001e4dSAndy Whitcroft			# this is not this patch's fault.
34834d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
34844d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
34854d001e4dSAndy Whitcroft				$check = 0;
34864d001e4dSAndy Whitcroft			}
34874d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
34884d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
34894d001e4dSAndy Whitcroft			}
34904d001e4dSAndy Whitcroft
34919bd49efeSAndy 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";
34924d001e4dSAndy Whitcroft
34939f5af480SJoe Perches			if ($check && $s ne '' &&
34949f5af480SJoe Perches			    (($sindent % 8) != 0 ||
34959f5af480SJoe Perches			     ($sindent < $indent) ||
3496f6950a73SJoe Perches			     ($sindent == $indent &&
3497f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
34989f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3499000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3500000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
35014d001e4dSAndy Whitcroft			}
35024d001e4dSAndy Whitcroft		}
35034d001e4dSAndy Whitcroft
35046c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
35056c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
35061f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
35071f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
35086c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3509c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3510c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3511cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3512cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
35131f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3514c2fdda0dSAndy Whitcroft		}
35156c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
35166c72ffaaSAndy Whitcroft
351700df344fSAndy Whitcroft#ignore lines not being added
35183705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
351900df344fSAndy Whitcroft
352011ca40a0SJoe Perches# check for dereferences that span multiple lines
352111ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
352211ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
352311ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
352411ca40a0SJoe Perches			my $ref = $1;
352511ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
352611ca40a0SJoe Perches			$ref .= $1;
352711ca40a0SJoe Perches			$ref =~ s/\s//g;
352811ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
352911ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
353011ca40a0SJoe Perches		}
353111ca40a0SJoe Perches
3532a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3533c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3534a1ce18e4SJoe Perches			my $type = $1;
3535a1ce18e4SJoe Perches			my $var = $2;
3536207a8e84SJoe Perches			$var = "" if (!defined $var);
3537207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3538a1ce18e4SJoe Perches				my $sign = $1;
3539a1ce18e4SJoe Perches				my $pointer = $2;
3540a1ce18e4SJoe Perches
3541a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3542a1ce18e4SJoe Perches
3543a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3544a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3545a1ce18e4SJoe Perches				    $fix) {
3546a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3547207a8e84SJoe Perches					my $comp_pointer = $pointer;
3548207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3549207a8e84SJoe Perches					$decl .= $comp_pointer;
3550207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3551207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3552a1ce18e4SJoe Perches				}
3553a1ce18e4SJoe Perches			}
3554a1ce18e4SJoe Perches		}
3555a1ce18e4SJoe Perches
3556653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
35577429c690SAndy Whitcroft		if ($dbg_type) {
35587429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3559000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3560000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
35617429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3562000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3563000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
35647429c690SAndy Whitcroft			}
3565653d4876SAndy Whitcroft			next;
3566653d4876SAndy Whitcroft		}
3567a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3568a1ef277eSAndy Whitcroft		if ($dbg_attr) {
35699360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3570000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3571000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
35729360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3573000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3574000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3575a1ef277eSAndy Whitcroft			}
3576a1ef277eSAndy Whitcroft			next;
3577a1ef277eSAndy Whitcroft		}
3578653d4876SAndy Whitcroft
3579f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
358099423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
358199423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3582d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3583d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3584f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3585f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3586f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3587d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3588d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3589f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3590d752fcc8SJoe Perches				$fixedline = $line;
35918d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3592f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3593d752fcc8SJoe Perches			}
3594f0a594c1SAndy Whitcroft		}
3595f0a594c1SAndy Whitcroft
359600df344fSAndy Whitcroft#
359700df344fSAndy Whitcroft# Checks which are anchored on the added line.
359800df344fSAndy Whitcroft#
359900df344fSAndy Whitcroft
3600653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3601c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3602653d4876SAndy Whitcroft			my $path = $1;
3603653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3604000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3605495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3606495e9d84SJoe Perches			}
3607495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3608495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3609495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3610653d4876SAndy Whitcroft			}
3611653d4876SAndy Whitcroft		}
3612653d4876SAndy Whitcroft
361300df344fSAndy Whitcroft# no C99 // comments
361400df344fSAndy Whitcroft		if ($line =~ m{//}) {
36153705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
36163705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
36173705ce5bSJoe Perches			    $fix) {
3618194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
36193705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
36203705ce5bSJoe Perches					my $comment = trim($1);
3621194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
36223705ce5bSJoe Perches				}
36233705ce5bSJoe Perches			}
362400df344fSAndy Whitcroft		}
362500df344fSAndy Whitcroft		# Remove C99 comments.
36260a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
36276c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
36280a920b5bSAndy Whitcroft
36292b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
36302b474a1aSAndy Whitcroft# the whole statement.
36312b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
36322b474a1aSAndy Whitcroft		if (defined $realline_next &&
36332b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
36342b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
36352b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
36362b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
36373cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
36383cbf62dfSAndy Whitcroft			# a prefix:
36393cbf62dfSAndy Whitcroft			#   XXX(foo);
36403cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3641653d4876SAndy Whitcroft			my $name = $1;
364287a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
36433cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
36443cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
36453cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
36463cbf62dfSAndy Whitcroft
36473cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
36482b474a1aSAndy Whitcroft				\n.}\s*$|
364948012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
365048012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
365148012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
36522b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
36532b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
365448012058SAndy Whitcroft			    )/x) {
36552b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
36562b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
36572b474a1aSAndy Whitcroft			} else {
36582b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
36590a920b5bSAndy Whitcroft			}
36600a920b5bSAndy Whitcroft		}
36612b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
36622b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
36632b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
36642b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
36652b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
36662b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
36672b474a1aSAndy Whitcroft		}
36682b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
36692b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3670000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3671000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
36722b474a1aSAndy Whitcroft		}
36730a920b5bSAndy Whitcroft
36745150bda4SJoe Eloff# check for global initialisers.
36756d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3676d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
36776d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3678d5e616fcSJoe Perches			    $fix) {
36796d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3680d5e616fcSJoe Perches			}
3681f0a594c1SAndy Whitcroft		}
36820a920b5bSAndy Whitcroft# check for static initialisers.
36836d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3684d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
36856d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3686d5e616fcSJoe Perches				      $herecurr) &&
3687d5e616fcSJoe Perches			    $fix) {
36886d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3689d5e616fcSJoe Perches			}
36900a920b5bSAndy Whitcroft		}
36910a920b5bSAndy Whitcroft
36921813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
36931813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
36941813087dSJoe Perches			my $tmp = trim($1);
36951813087dSJoe Perches			WARN("MISORDERED_TYPE",
36961813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
36971813087dSJoe Perches		}
36981813087dSJoe Perches
3699cb710ecaSJoe Perches# check for static const char * arrays.
3700cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3701000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3702000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3703cb710ecaSJoe Perches				$herecurr);
3704cb710ecaSJoe Perches               }
3705cb710ecaSJoe Perches
3706cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3707cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3708000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3709000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3710cb710ecaSJoe Perches				$herecurr);
3711cb710ecaSJoe Perches               }
3712cb710ecaSJoe Perches
3713ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3714ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3715ab7e23f3SJoe Perches			my $found = $1;
3716ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3717ab7e23f3SJoe Perches				WARN("CONST_CONST",
3718ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3719ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3720ab7e23f3SJoe Perches				WARN("CONST_CONST",
3721ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3722ab7e23f3SJoe Perches			}
3723ab7e23f3SJoe Perches		}
3724ab7e23f3SJoe Perches
37259b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
37269b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
37279b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
37289b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
37299b0fa60dSJoe Perches				$herecurr);
37309b0fa60dSJoe Perches               }
37319b0fa60dSJoe Perches
3732b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3733b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3734b598b670SJoe Perches			my $array = $1;
3735b598b670SJoe 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*\))@) {
3736b598b670SJoe Perches				my $array_div = $1;
3737b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3738b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3739b598b670SJoe Perches				    $fix) {
3740b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3741b598b670SJoe Perches				}
3742b598b670SJoe Perches			}
3743b598b670SJoe Perches		}
3744b598b670SJoe Perches
3745b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3746b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3747b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3748b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3749b36190c5SJoe Perches			    $fix) {
3750194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3751b36190c5SJoe Perches			}
3752b36190c5SJoe Perches		}
3753b36190c5SJoe Perches
3754653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3755653d4876SAndy Whitcroft# make sense.
3756653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
37578054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3758c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
37598ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
376046d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
3761000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3762000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
37630a920b5bSAndy Whitcroft		}
37640a920b5bSAndy Whitcroft
37650a920b5bSAndy Whitcroft# * goes on variable not on type
376665863862SAndy Whitcroft		# (char*[ const])
3767bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3768bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
37693705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3770d8aaf121SAndy Whitcroft
377165863862SAndy Whitcroft			# Should start with a space.
377265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
377365863862SAndy Whitcroft			# Should not end with a space.
377465863862SAndy Whitcroft			$to =~ s/\s+$//;
377565863862SAndy Whitcroft			# '*'s should not have spaces between.
3776f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
377765863862SAndy Whitcroft			}
3778d8aaf121SAndy Whitcroft
37793705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
378065863862SAndy Whitcroft			if ($from ne $to) {
37813705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
37823705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
37833705ce5bSJoe Perches				    $fix) {
37843705ce5bSJoe Perches					my $sub_from = $ident;
37853705ce5bSJoe Perches					my $sub_to = $ident;
37863705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3787194f66fcSJoe Perches					$fixed[$fixlinenr] =~
37883705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
37893705ce5bSJoe Perches				}
379065863862SAndy Whitcroft			}
3791bfcb2cc7SAndy Whitcroft		}
3792bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3793bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
37943705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3795d8aaf121SAndy Whitcroft
379665863862SAndy Whitcroft			# Should start with a space.
379765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
379865863862SAndy Whitcroft			# Should not end with a space.
379965863862SAndy Whitcroft			$to =~ s/\s+$//;
380065863862SAndy Whitcroft			# '*'s should not have spaces between.
3801f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
380265863862SAndy Whitcroft			}
380365863862SAndy Whitcroft			# Modifiers should have spaces.
380465863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
380565863862SAndy Whitcroft
38063705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3807667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
38083705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
38093705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
38103705ce5bSJoe Perches				    $fix) {
38113705ce5bSJoe Perches
38123705ce5bSJoe Perches					my $sub_from = $match;
38133705ce5bSJoe Perches					my $sub_to = $match;
38143705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3815194f66fcSJoe Perches					$fixed[$fixlinenr] =~
38163705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
38173705ce5bSJoe Perches				}
381865863862SAndy Whitcroft			}
38190a920b5bSAndy Whitcroft		}
38200a920b5bSAndy Whitcroft
38219d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
38229d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
38230675a8fbSJean Delvare			my $msg_level = \&WARN;
38240675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
38250675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
38269d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
38279d3e3c70SJoe Perches		}
38280a920b5bSAndy Whitcroft
38299d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
38308905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3831000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3832000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
38338905a67cSAndy Whitcroft		}
38348905a67cSAndy Whitcroft
383517441227SJoe Perches# check for uses of printk_ratelimit
383617441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3837000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3838000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
383917441227SJoe Perches		}
384017441227SJoe Perches
3841eeef5733SJoe Perches# printk should use KERN_* levels
3842eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
3843000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
3844eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
384500df344fSAndy Whitcroft		}
38460a920b5bSAndy Whitcroft
3847243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3848243f3803SJoe Perches			my $orig = $1;
3849243f3803SJoe Perches			my $level = lc($orig);
3850243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
38518f26b837SJoe Perches			my $level2 = $level;
38528f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3853243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3854daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3855243f3803SJoe Perches		}
3856243f3803SJoe Perches
3857243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3858d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3859d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3860d5e616fcSJoe Perches			    $fix) {
3861194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3862d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3863d5e616fcSJoe Perches			}
3864243f3803SJoe Perches		}
3865243f3803SJoe Perches
3866dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3867dc139313SJoe Perches			my $orig = $1;
3868dc139313SJoe Perches			my $level = lc($orig);
3869dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3870dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3871dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3872dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3873dc139313SJoe Perches		}
3874dc139313SJoe Perches
387591c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
387691c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
387791c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
387891c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
387991c9afafSAndy Lutomirski			WARN("ENOSYS",
388091c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
388191c9afafSAndy Lutomirski		}
388291c9afafSAndy Lutomirski
3883653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3884653d4876SAndy Whitcroft# or if closed on same line
38858d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
38864e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
38878d182478SJoe Perches			if (ERROR("OPEN_BRACE",
38888d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
38898d182478SJoe Perches			    $fix) {
38908d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
38918d182478SJoe Perches				my $fixed_line = $rawline;
38928d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
38938d182478SJoe Perches				my $line1 = $1;
38948d182478SJoe Perches				my $line2 = $2;
38958d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
38968d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
38978d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
38988d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
38998d182478SJoe Perches				}
39008d182478SJoe Perches			}
39010a920b5bSAndy Whitcroft		}
3902653d4876SAndy Whitcroft
39038905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
39048905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
39058905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
39068d182478SJoe Perches			if (ERROR("OPEN_BRACE",
39078d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
39088d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39098d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
39108d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39118d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
39128d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
39138d182478SJoe Perches				$fixedline = $rawline;
39148d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
39158d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
39168d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39178d182478SJoe Perches				}
39188d182478SJoe Perches			}
39198905a67cSAndy Whitcroft		}
39208905a67cSAndy Whitcroft
39210c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
39223705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
39233705ce5bSJoe Perches			if (WARN("SPACING",
39243705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
39253705ce5bSJoe Perches			    $fix) {
3926194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39273705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
39283705ce5bSJoe Perches			}
39290c73b4ebSAndy Whitcroft		}
39300c73b4ebSAndy Whitcroft
393131070b5dSJoe Perches# Function pointer declarations
393231070b5dSJoe Perches# check spacing between type, funcptr, and args
393331070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
393491f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
393531070b5dSJoe Perches			my $declare = $1;
393631070b5dSJoe Perches			my $pre_pointer_space = $2;
393731070b5dSJoe Perches			my $post_pointer_space = $3;
393831070b5dSJoe Perches			my $funcname = $4;
393931070b5dSJoe Perches			my $post_funcname_space = $5;
394031070b5dSJoe Perches			my $pre_args_space = $6;
394131070b5dSJoe Perches
394291f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
394391f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
394491f72e9cSJoe Perches# don't need a space so don't warn for those.
394591f72e9cSJoe Perches			my $post_declare_space = "";
394691f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
394791f72e9cSJoe Perches				$post_declare_space = $1;
394891f72e9cSJoe Perches				$declare = rtrim($declare);
394991f72e9cSJoe Perches			}
395091f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
395131070b5dSJoe Perches				WARN("SPACING",
395231070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
395391f72e9cSJoe Perches				$post_declare_space = " ";
395431070b5dSJoe Perches			}
395531070b5dSJoe Perches
395631070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
395791f72e9cSJoe Perches# This test is not currently implemented because these declarations are
395891f72e9cSJoe Perches# equivalent to
395991f72e9cSJoe Perches#	int  foo(int bar, ...)
396091f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
396191f72e9cSJoe Perches#
396291f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
396391f72e9cSJoe Perches#				WARN("SPACING",
396491f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
396591f72e9cSJoe Perches#			}
396631070b5dSJoe Perches
396731070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
396831070b5dSJoe Perches			if (defined $pre_pointer_space &&
396931070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
397031070b5dSJoe Perches				WARN("SPACING",
397131070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
397231070b5dSJoe Perches			}
397331070b5dSJoe Perches
397431070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
397531070b5dSJoe Perches			if (defined $post_pointer_space &&
397631070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
397731070b5dSJoe Perches				WARN("SPACING",
397831070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
397931070b5dSJoe Perches			}
398031070b5dSJoe Perches
398131070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
398231070b5dSJoe Perches			if (defined $post_funcname_space &&
398331070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
398431070b5dSJoe Perches				WARN("SPACING",
398531070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
398631070b5dSJoe Perches			}
398731070b5dSJoe Perches
398831070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
398931070b5dSJoe Perches			if (defined $pre_args_space &&
399031070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
399131070b5dSJoe Perches				WARN("SPACING",
399231070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
399331070b5dSJoe Perches			}
399431070b5dSJoe Perches
399531070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3996194f66fcSJoe Perches				$fixed[$fixlinenr] =~
399791f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
399831070b5dSJoe Perches			}
399931070b5dSJoe Perches		}
400031070b5dSJoe Perches
40018d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
40028d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4003fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4004fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
40058d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
40068d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
40078d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4008fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
4009daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
40103705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
40113705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
40123705ce5bSJoe Perches				    $fix) {
4013194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
40143705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
40153705ce5bSJoe Perches				}
40168d31cfceSAndy Whitcroft			}
40178d31cfceSAndy Whitcroft		}
40188d31cfceSAndy Whitcroft
4019f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
40206c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4021c2fdda0dSAndy Whitcroft			my $name = $1;
4022773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4023773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4024c2fdda0dSAndy Whitcroft
4025c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4026773647a0SAndy Whitcroft			if ($name =~ /^(?:
4027773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4028773647a0SAndy Whitcroft				volatile|__volatile__|
4029773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4030773647a0SAndy Whitcroft				asm|__asm__)$/x)
4031773647a0SAndy Whitcroft			{
4032c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4033c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4034c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4035c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4036773647a0SAndy Whitcroft
4037773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4038c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4039c2fdda0dSAndy Whitcroft
4040c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4041c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4042773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4043c2fdda0dSAndy Whitcroft
4044c2fdda0dSAndy Whitcroft			} else {
40453705ce5bSJoe Perches				if (WARN("SPACING",
40463705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
40473705ce5bSJoe Perches					     $fix) {
4048194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40493705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
40503705ce5bSJoe Perches				}
4051f0a594c1SAndy Whitcroft			}
40526c72ffaaSAndy Whitcroft		}
40539a4cad4eSEric Nelson
4054653d4876SAndy Whitcroft# Check operator spacing.
40550a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
40563705ce5bSJoe Perches			my $fixed_line = "";
40573705ce5bSJoe Perches			my $line_fixed = 0;
40583705ce5bSJoe Perches
40599c0ca6f9SAndy Whitcroft			my $ops = qr{
40609c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
40619c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
40629c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
40631f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
406484731623SJoe Perches				\?:|\?|:
40659c0ca6f9SAndy Whitcroft			}x;
4066cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
40673705ce5bSJoe Perches
40683705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
40693705ce5bSJoe Perches##			foreach my $el (@elements) {
40703705ce5bSJoe Perches##				print("el: <$el>\n");
40713705ce5bSJoe Perches##			}
40723705ce5bSJoe Perches
40733705ce5bSJoe Perches			my @fix_elements = ();
407400df344fSAndy Whitcroft			my $off = 0;
40756c72ffaaSAndy Whitcroft
40763705ce5bSJoe Perches			foreach my $el (@elements) {
40773705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
40783705ce5bSJoe Perches				$off += length($el);
40793705ce5bSJoe Perches			}
40803705ce5bSJoe Perches
40813705ce5bSJoe Perches			$off = 0;
40823705ce5bSJoe Perches
40836c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4084b34c648bSJoe Perches			my $last_after = -1;
40856c72ffaaSAndy Whitcroft
40860a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
40873705ce5bSJoe Perches
40883705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
40893705ce5bSJoe Perches
40903705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
40913705ce5bSJoe Perches
40924a0df2efSAndy Whitcroft				$off += length($elements[$n]);
40934a0df2efSAndy Whitcroft
409425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4095773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4096773647a0SAndy Whitcroft				my $cc = '';
4097773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4098773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4099773647a0SAndy Whitcroft				}
4100773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4101773647a0SAndy Whitcroft
41024a0df2efSAndy Whitcroft				my $a = '';
41034a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
41044a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4105cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
41064a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
41074a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4108773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
41094a0df2efSAndy Whitcroft
41100a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
41114a0df2efSAndy Whitcroft
41124a0df2efSAndy Whitcroft				my $c = '';
41130a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
41144a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
41154a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4116cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
41174a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
41184a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
41198b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
41204a0df2efSAndy Whitcroft				} else {
41214a0df2efSAndy Whitcroft					$c = 'E';
41220a920b5bSAndy Whitcroft				}
41230a920b5bSAndy Whitcroft
41244a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
41254a0df2efSAndy Whitcroft
41264a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
41274a0df2efSAndy Whitcroft
41286c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4129de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
41300a920b5bSAndy Whitcroft
413174048ed8SAndy Whitcroft				# Pull out the value of this operator.
41326c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
41330a920b5bSAndy Whitcroft
41341f65f947SAndy Whitcroft				# Get the full operator variant.
41351f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
41361f65f947SAndy Whitcroft
413713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
413813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4139d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
414013214adfSAndy Whitcroft
4141cf655043SAndy Whitcroft#				# Ignore comments
4142cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
414313214adfSAndy Whitcroft
4144d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
414513214adfSAndy Whitcroft				} elsif ($op eq ';') {
4146cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4147cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
41483705ce5bSJoe Perches						if (ERROR("SPACING",
41493705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4150b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
41513705ce5bSJoe Perches							$line_fixed = 1;
41523705ce5bSJoe Perches						}
4153d8aaf121SAndy Whitcroft					}
4154d8aaf121SAndy Whitcroft
4155d8aaf121SAndy Whitcroft				# // is a comment
4156d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
41570a920b5bSAndy Whitcroft
4158b00e4814SJoe Perches				#   :   when part of a bitfield
4159b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4160b00e4814SJoe Perches					# skip the bitfield test for now
4161b00e4814SJoe Perches
41621f65f947SAndy Whitcroft				# No spaces for:
41631f65f947SAndy Whitcroft				#   ->
4164b00e4814SJoe Perches				} elsif ($op eq '->') {
41654a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
41663705ce5bSJoe Perches						if (ERROR("SPACING",
41673705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4168b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
41693705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
41703705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
41713705ce5bSJoe Perches							}
4172b34c648bSJoe Perches							$line_fixed = 1;
41733705ce5bSJoe Perches						}
41740a920b5bSAndy Whitcroft					}
41750a920b5bSAndy Whitcroft
41762381097bSJoe Perches				# , must not have a space before and must have a space on the right.
41770a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
41782381097bSJoe Perches					my $rtrim_before = 0;
41792381097bSJoe Perches					my $space_after = 0;
41802381097bSJoe Perches					if ($ctx =~ /Wx./) {
41812381097bSJoe Perches						if (ERROR("SPACING",
41822381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
41832381097bSJoe Perches							$line_fixed = 1;
41842381097bSJoe Perches							$rtrim_before = 1;
41852381097bSJoe Perches						}
41862381097bSJoe Perches					}
4187cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
41883705ce5bSJoe Perches						if (ERROR("SPACING",
41893705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
41903705ce5bSJoe Perches							$line_fixed = 1;
4191b34c648bSJoe Perches							$last_after = $n;
41922381097bSJoe Perches							$space_after = 1;
41932381097bSJoe Perches						}
41942381097bSJoe Perches					}
41952381097bSJoe Perches					if ($rtrim_before || $space_after) {
41962381097bSJoe Perches						if ($rtrim_before) {
41972381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
41982381097bSJoe Perches						} else {
41992381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
42002381097bSJoe Perches						}
42012381097bSJoe Perches						if ($space_after) {
42022381097bSJoe Perches							$good .= " ";
42033705ce5bSJoe Perches						}
42040a920b5bSAndy Whitcroft					}
42050a920b5bSAndy Whitcroft
42069c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
420774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
42089c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
42099c0ca6f9SAndy Whitcroft
42109c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
42119c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
42129c0ca6f9SAndy Whitcroft				# unary operator, or a cast
42139c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
421474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
42150d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4216cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
42173705ce5bSJoe Perches						if (ERROR("SPACING",
42183705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4219b34c648bSJoe Perches							if ($n != $last_after + 2) {
4220b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
42213705ce5bSJoe Perches								$line_fixed = 1;
42223705ce5bSJoe Perches							}
42230a920b5bSAndy Whitcroft						}
4224b34c648bSJoe Perches					}
4225a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4226171ae1a4SAndy Whitcroft						# A unary '*' may be const
4227171ae1a4SAndy Whitcroft
4228171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
42293705ce5bSJoe Perches						if (ERROR("SPACING",
42303705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4231b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
42323705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42333705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
42343705ce5bSJoe Perches							}
4235b34c648bSJoe Perches							$line_fixed = 1;
42363705ce5bSJoe Perches						}
42370a920b5bSAndy Whitcroft					}
42380a920b5bSAndy Whitcroft
42390a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
42400a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4241773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
42423705ce5bSJoe Perches						if (ERROR("SPACING",
42433705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4244b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
42453705ce5bSJoe Perches							$line_fixed = 1;
42463705ce5bSJoe Perches						}
42470a920b5bSAndy Whitcroft					}
4248773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4249773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
42503705ce5bSJoe Perches						if (ERROR("SPACING",
42513705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4252b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42533705ce5bSJoe Perches							$line_fixed = 1;
42543705ce5bSJoe Perches						}
4255653d4876SAndy Whitcroft					}
4256773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
42573705ce5bSJoe Perches						if (ERROR("SPACING",
42583705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4259b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
42603705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42613705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4262773647a0SAndy Whitcroft							}
4263b34c648bSJoe Perches							$line_fixed = 1;
42643705ce5bSJoe Perches						}
42653705ce5bSJoe Perches					}
42660a920b5bSAndy Whitcroft
42670a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
42689c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
42699c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
42709c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4271c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4272c2fdda0dSAndy Whitcroft					 $op eq '%')
42730a920b5bSAndy Whitcroft				{
4274d2e025f3SJoe Perches					if ($check) {
4275d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4276d2e025f3SJoe Perches							if (CHK("SPACING",
4277d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4278d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4279d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4280d2e025f3SJoe Perches								$line_fixed = 1;
4281d2e025f3SJoe Perches							}
4282d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4283d2e025f3SJoe Perches							if (CHK("SPACING",
4284d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4285d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4286d2e025f3SJoe Perches								$line_fixed = 1;
4287d2e025f3SJoe Perches							}
4288d2e025f3SJoe Perches						}
4289d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
42903705ce5bSJoe Perches						if (ERROR("SPACING",
42913705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4292b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4293b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4294b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4295b34c648bSJoe Perches							}
42963705ce5bSJoe Perches							$line_fixed = 1;
42973705ce5bSJoe Perches						}
42980a920b5bSAndy Whitcroft					}
42990a920b5bSAndy Whitcroft
43001f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
43011f65f947SAndy Whitcroft				# terminating a case value or a label.
43021f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
43031f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
43043705ce5bSJoe Perches						if (ERROR("SPACING",
43053705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4306b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43073705ce5bSJoe Perches							$line_fixed = 1;
43083705ce5bSJoe Perches						}
43091f65f947SAndy Whitcroft					}
43101f65f947SAndy Whitcroft
43110a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4312cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
43131f65f947SAndy Whitcroft					my $ok = 0;
43141f65f947SAndy Whitcroft
431522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
43161f65f947SAndy Whitcroft					if (($op eq '<' &&
43171f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
43181f65f947SAndy Whitcroft					    ($op eq '>' &&
43191f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
43201f65f947SAndy Whitcroft					{
43211f65f947SAndy Whitcroft					    	$ok = 1;
43221f65f947SAndy Whitcroft					}
43231f65f947SAndy Whitcroft
4324e0df7e1fSJoe Perches					# for asm volatile statements
4325e0df7e1fSJoe Perches					# ignore a colon with another
4326e0df7e1fSJoe Perches					# colon immediately before or after
4327e0df7e1fSJoe Perches					if (($op eq ':') &&
4328e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4329e0df7e1fSJoe Perches						$ok = 1;
4330e0df7e1fSJoe Perches					}
4331e0df7e1fSJoe Perches
433284731623SJoe Perches					# messages are ERROR, but ?: are CHK
43331f65f947SAndy Whitcroft					if ($ok == 0) {
43340675a8fbSJean Delvare						my $msg_level = \&ERROR;
43350675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
433684731623SJoe Perches
43370675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
43383705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4339b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4340b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4341b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4342b34c648bSJoe Perches							}
43433705ce5bSJoe Perches							$line_fixed = 1;
43443705ce5bSJoe Perches						}
43450a920b5bSAndy Whitcroft					}
434622f2a2efSAndy Whitcroft				}
43474a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
43483705ce5bSJoe Perches
43493705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
43503705ce5bSJoe Perches
43513705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
43520a920b5bSAndy Whitcroft			}
43533705ce5bSJoe Perches
43543705ce5bSJoe Perches			if (($#elements % 2) == 0) {
43553705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
43563705ce5bSJoe Perches			}
43573705ce5bSJoe Perches
4358194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4359194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
43603705ce5bSJoe Perches			}
43613705ce5bSJoe Perches
43623705ce5bSJoe Perches
43630a920b5bSAndy Whitcroft		}
43640a920b5bSAndy Whitcroft
4365786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4366d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4367786b6326SJoe Perches			if (WARN("SPACING",
4368786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4369786b6326SJoe Perches			    $fix) {
4370194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4371786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4372786b6326SJoe Perches			}
4373786b6326SJoe Perches		}
4374786b6326SJoe Perches
4375f0a594c1SAndy Whitcroft# check for multiple assignments
4376f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4377000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4378000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4379f0a594c1SAndy Whitcroft		}
4380f0a594c1SAndy Whitcroft
438122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
438222f2a2efSAndy Whitcroft## # continuation.
438322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
438422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
438522f2a2efSAndy Whitcroft##
438622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
438722f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
438822f2a2efSAndy Whitcroft## 			my $ln = $line;
438922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
439022f2a2efSAndy Whitcroft## 			}
439122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4392000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4393000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
439422f2a2efSAndy Whitcroft## 			}
439522f2a2efSAndy Whitcroft## 		}
4396f0a594c1SAndy Whitcroft
43970a920b5bSAndy Whitcroft#need space before brace following if, while, etc
43986b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
43994e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
44003705ce5bSJoe Perches			if (ERROR("SPACING",
44013705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
44023705ce5bSJoe Perches			    $fix) {
44038d81ae05SCyril Bur				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
44043705ce5bSJoe Perches			}
4405de7d4f0eSAndy Whitcroft		}
4406de7d4f0eSAndy Whitcroft
4407c4a62ef9SJoe Perches## # check for blank lines before declarations
4408c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4409c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4410c4a62ef9SJoe Perches##			WARN("SPACING",
4411c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4412c4a62ef9SJoe Perches##		}
4413c4a62ef9SJoe Perches##
4414c4a62ef9SJoe Perches
4415de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4416de7d4f0eSAndy Whitcroft# on the line
4417de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4418d5e616fcSJoe Perches			if (ERROR("SPACING",
4419d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4420d5e616fcSJoe Perches			    $fix) {
4421194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4422d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4423d5e616fcSJoe Perches			}
44240a920b5bSAndy Whitcroft		}
44250a920b5bSAndy Whitcroft
442622f2a2efSAndy Whitcroft# check spacing on square brackets
442722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
44283705ce5bSJoe Perches			if (ERROR("SPACING",
44293705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
44303705ce5bSJoe Perches			    $fix) {
4431194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44323705ce5bSJoe Perches				    s/\[\s+/\[/;
44333705ce5bSJoe Perches			}
443422f2a2efSAndy Whitcroft		}
443522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
44363705ce5bSJoe Perches			if (ERROR("SPACING",
44373705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
44383705ce5bSJoe Perches			    $fix) {
4439194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44403705ce5bSJoe Perches				    s/\s+\]/\]/;
44413705ce5bSJoe Perches			}
444222f2a2efSAndy Whitcroft		}
444322f2a2efSAndy Whitcroft
4444c45dcabdSAndy Whitcroft# check spacing on parentheses
44459c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
44469c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
44473705ce5bSJoe Perches			if (ERROR("SPACING",
44483705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
44493705ce5bSJoe Perches			    $fix) {
4450194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44513705ce5bSJoe Perches				    s/\(\s+/\(/;
44523705ce5bSJoe Perches			}
445322f2a2efSAndy Whitcroft		}
445413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4455c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4456c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
44573705ce5bSJoe Perches			if (ERROR("SPACING",
44583705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
44593705ce5bSJoe Perches			    $fix) {
4460194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44613705ce5bSJoe Perches				    s/\s+\)/\)/;
44623705ce5bSJoe Perches			}
446322f2a2efSAndy Whitcroft		}
446422f2a2efSAndy Whitcroft
4465e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4466e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4467e2826fd0SJoe Perches
4468e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4469ea4acbb1SJoe Perches			my $var = $1;
4470ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4471ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4472ea4acbb1SJoe Perches			    $fix) {
4473ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4474ea4acbb1SJoe Perches			}
4475ea4acbb1SJoe Perches		}
4476ea4acbb1SJoe Perches
4477ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4478ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4479ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4480ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4481ea4acbb1SJoe Perches			my $var = $2;
4482ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4483ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4484ea4acbb1SJoe Perches			    $fix) {
4485ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4486ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4487ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4488ea4acbb1SJoe Perches			}
4489e2826fd0SJoe Perches		}
4490e2826fd0SJoe Perches
449163b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
449263b7c73eSJoe Perches		if ($^V && $^V ge 5.10.0 && defined($stat) &&
449363b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
449463b7c73eSJoe Perches			my $if_stat = $1;
449563b7c73eSJoe Perches			my $test = substr($2, 1, -1);
449663b7c73eSJoe Perches			my $herectx;
449763b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
449863b7c73eSJoe Perches				my $match = $1;
449963b7c73eSJoe Perches				# avoid parentheses around potential macro args
450063b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
450163b7c73eSJoe Perches				if (!defined($herectx)) {
450263b7c73eSJoe Perches					$herectx = $here . "\n";
450363b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
450463b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
450563b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
450663b7c73eSJoe Perches						$herectx .=  $rl . "\n";
450763b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
450863b7c73eSJoe Perches					}
450963b7c73eSJoe Perches				}
451063b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
451163b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
451263b7c73eSJoe Perches			}
451363b7c73eSJoe Perches		}
451463b7c73eSJoe Perches
45150a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
45164a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
45170a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
45183705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
45193705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
45203705ce5bSJoe Perches			    $fix) {
4521194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45223705ce5bSJoe Perches				    s/^(.)\s+/$1/;
45233705ce5bSJoe Perches			}
45240a920b5bSAndy Whitcroft		}
45250a920b5bSAndy Whitcroft
45265b9553abSJoe Perches# return is not a function
4527507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4528c45dcabdSAndy Whitcroft			my $spacing = $1;
4529507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
45305b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
45315b9553abSJoe Perches				my $value = $1;
45325b9553abSJoe Perches				$value = deparenthesize($value);
45335b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4534000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4535000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
45365b9553abSJoe Perches				}
4537c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4538000d1cc1SJoe Perches				ERROR("SPACING",
4539000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4540c45dcabdSAndy Whitcroft			}
4541c45dcabdSAndy Whitcroft		}
4542507e5141SJoe Perches
4543b43ae21bSJoe Perches# unnecessary return in a void function
4544b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4545b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4546b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4547b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4548b43ae21bSJoe Perches		    $linenr >= 3 &&
4549b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4550b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
45519819cf25SJoe Perches			WARN("RETURN_VOID",
4552b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
45539819cf25SJoe Perches               }
45549819cf25SJoe Perches
4555189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4556189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4557189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4558189248d8SJoe Perches			my $openparens = $1;
4559189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4560189248d8SJoe Perches			my $msg = "";
4561189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4562189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4563189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4564189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4565189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4566189248d8SJoe Perches			}
4567189248d8SJoe Perches		}
4568189248d8SJoe Perches
4569c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4570c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4571c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4572c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4573c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4574c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4575c5595fa2SJoe Perches			my $lead = $1;
4576c5595fa2SJoe Perches			my $const = $2;
4577c5595fa2SJoe Perches			my $comp = $3;
4578c5595fa2SJoe Perches			my $to = $4;
4579c5595fa2SJoe Perches			my $newcomp = $comp;
4580f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4581c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4582c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4583c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4584c5595fa2SJoe Perches			    $fix) {
4585c5595fa2SJoe Perches				if ($comp eq "<") {
4586c5595fa2SJoe Perches					$newcomp = ">";
4587c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4588c5595fa2SJoe Perches					$newcomp = ">=";
4589c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4590c5595fa2SJoe Perches					$newcomp = "<";
4591c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4592c5595fa2SJoe Perches					$newcomp = "<=";
4593c5595fa2SJoe Perches				}
4594c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4595c5595fa2SJoe Perches			}
4596c5595fa2SJoe Perches		}
4597c5595fa2SJoe Perches
4598f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4599f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
460053a3c448SAndy Whitcroft			my $name = $1;
460153a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4602000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4603f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
460453a3c448SAndy Whitcroft			}
460553a3c448SAndy Whitcroft		}
4606c45dcabdSAndy Whitcroft
46070a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
46084a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
46093705ce5bSJoe Perches			if (ERROR("SPACING",
46103705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
46113705ce5bSJoe Perches			    $fix) {
4612194f66fcSJoe Perches				$fixed[$fixlinenr] =~
46133705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
46143705ce5bSJoe Perches			}
46150a920b5bSAndy Whitcroft		}
46160a920b5bSAndy Whitcroft
4617f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4618f5fe35ddSAndy Whitcroft# statements after the conditional.
4619170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
46203e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
46213e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
46223e469cdcSAndy Whitcroft					if (!defined $stat);
4623170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4624170d3a22SAndy Whitcroft						$remain_next, $off_next);
4625170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4626170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4627170d3a22SAndy Whitcroft
4628170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4629170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4630170d3a22SAndy Whitcroft				# then count those as offsets.
4631170d3a22SAndy Whitcroft				my ($whitespace) =
4632170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4633170d3a22SAndy Whitcroft				my $offset =
4634170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4635170d3a22SAndy Whitcroft
4636170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4637170d3a22SAndy Whitcroft								$offset} = 1;
4638170d3a22SAndy Whitcroft			}
4639170d3a22SAndy Whitcroft		}
4640170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4641c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4642170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4643171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
46448905a67cSAndy Whitcroft
4645b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4646000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4647000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
46488905a67cSAndy Whitcroft			}
46498905a67cSAndy Whitcroft
46508905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
46518905a67cSAndy Whitcroft			# conditional.
4652773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
46538905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
465413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
465553210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
465653210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4657773647a0SAndy Whitcroft			{
4658bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4659bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4660bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
466142bdf74cSHidetoshi Seto				my $stat_real = '';
4662bb44ad39SAndy Whitcroft
466342bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
466442bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4665bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4666bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4667bb44ad39SAndy Whitcroft				}
4668bb44ad39SAndy Whitcroft
4669000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4670000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
46718905a67cSAndy Whitcroft			}
46728905a67cSAndy Whitcroft		}
46738905a67cSAndy Whitcroft
467413214adfSAndy Whitcroft# Check for bitwise tests written as boolean
467513214adfSAndy Whitcroft		if ($line =~ /
467613214adfSAndy Whitcroft			(?:
467713214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
467813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
467913214adfSAndy Whitcroft				(?:\&\&|\|\|)
468013214adfSAndy Whitcroft			|
468113214adfSAndy Whitcroft				(?:\&\&|\|\|)
468213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
468313214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
468413214adfSAndy Whitcroft			)/x)
468513214adfSAndy Whitcroft		{
4686000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4687000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
468813214adfSAndy Whitcroft		}
468913214adfSAndy Whitcroft
46908905a67cSAndy Whitcroft# if and else should not have general statements after it
469113214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
469213214adfSAndy Whitcroft			my $s = $1;
469313214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
469413214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4695000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4696000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
46970a920b5bSAndy Whitcroft			}
469813214adfSAndy Whitcroft		}
469939667782SAndy Whitcroft# if should not continue a brace
470039667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4701000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4702048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
470339667782SAndy Whitcroft				$herecurr);
470439667782SAndy Whitcroft		}
4705a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4706a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4707a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
47083fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4709a1080bf8SAndy Whitcroft			\s*return\s+
4710a1080bf8SAndy Whitcroft		    )/xg)
4711a1080bf8SAndy Whitcroft		{
4712000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4713000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4714a1080bf8SAndy Whitcroft		}
47150a920b5bSAndy Whitcroft
47160a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
47170a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
47188b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
47190a920b5bSAndy Whitcroft		    $previndent == $indent) {
47208b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
47218b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
47228b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
47238b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
47248b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
47258b8856f4SJoe Perches				my $fixedline = $prevrawline;
47268b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
47278b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
47288b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
47298b8856f4SJoe Perches				}
47308b8856f4SJoe Perches				$fixedline = $rawline;
47318b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
47328b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
47338b8856f4SJoe Perches			}
47340a920b5bSAndy Whitcroft		}
47350a920b5bSAndy Whitcroft
47368b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4737c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4738c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4739c2fdda0dSAndy Whitcroft
4740c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4741c2fdda0dSAndy Whitcroft			# conditional.
4742773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4743c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4744c2fdda0dSAndy Whitcroft
4745c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
47468b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
47478b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
47488b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
47498b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
47508b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
47518b8856f4SJoe Perches					my $fixedline = $prevrawline;
47528b8856f4SJoe Perches					my $trailing = $rawline;
47538b8856f4SJoe Perches					$trailing =~ s/^\+//;
47548b8856f4SJoe Perches					$trailing = trim($trailing);
47558b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
47568b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
47578b8856f4SJoe Perches				}
4758c2fdda0dSAndy Whitcroft			}
4759c2fdda0dSAndy Whitcroft		}
4760c2fdda0dSAndy Whitcroft
476195e2c602SJoe Perches#Specific variable tests
4762323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4763323c1260SJoe Perches			my $var = $1;
476495e2c602SJoe Perches
476595e2c602SJoe Perches#gcc binary extension
476695e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4767d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4768d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4769d5e616fcSJoe Perches				    $fix) {
4770d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4771194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4772d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4773d5e616fcSJoe Perches				}
477495e2c602SJoe Perches			}
477595e2c602SJoe Perches
477695e2c602SJoe Perches#CamelCase
4777807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4778be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
477922735ce8SJoe Perches#Ignore Page<foo> variants
4780807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
478122735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4782f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4783f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4784f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
47857e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
47867e781f67SJoe Perches					my $word = $1;
47877e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4788d8b07710SJoe Perches					if ($check) {
4789d8b07710SJoe Perches						seed_camelcase_includes();
4790d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4791d8b07710SJoe Perches							seed_camelcase_file($realfile);
4792d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4793d8b07710SJoe Perches						}
4794d8b07710SJoe Perches					}
47957e781f67SJoe Perches					if (!defined $camelcase{$word}) {
47967e781f67SJoe Perches						$camelcase{$word} = 1;
4797be79794bSJoe Perches						CHK("CAMELCASE",
47987e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
47997e781f67SJoe Perches					}
4800323c1260SJoe Perches				}
4801323c1260SJoe Perches			}
48023445686aSJoe Perches		}
48030a920b5bSAndy Whitcroft
48040a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4805d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4806d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4807d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4808d5e616fcSJoe Perches			    $fix) {
4809194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4810d5e616fcSJoe Perches			}
48110a920b5bSAndy Whitcroft		}
48120a920b5bSAndy Whitcroft
48130e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
48140e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4815c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4816e09dec48SAndy Whitcroft			my $file = "$1.h";
4817e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4818e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4819e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
48207840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4821c45dcabdSAndy Whitcroft			{
48220e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
48230e212e0aSFabian Frederick				if ($asminclude > 0) {
4824e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4825000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4826000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4827e09dec48SAndy Whitcroft					} else {
4828000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4829000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4830e09dec48SAndy Whitcroft					}
48310a920b5bSAndy Whitcroft				}
48320a920b5bSAndy Whitcroft			}
48330e212e0aSFabian Frederick		}
48340a920b5bSAndy Whitcroft
4835653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4836653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4837cf655043SAndy Whitcroft# in a known good container
4838b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4839b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4840d8aaf121SAndy Whitcroft			my $ln = $linenr;
4841d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4842c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4843c45dcabdSAndy Whitcroft			my $ctx = '';
484408a2843eSJoe Perches			my $has_flow_statement = 0;
484508a2843eSJoe Perches			my $has_arg_concat = 0;
4846c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4847f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4848f74bd194SAndy Whitcroft			$ctx = $dstat;
4849c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4850a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4851c45dcabdSAndy Whitcroft
485208a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
485362e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
485408a2843eSJoe Perches
4855f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4856f59b64bfSJoe Perches			my $define_args = $1;
4857f59b64bfSJoe Perches			my $define_stmt = $dstat;
4858f59b64bfSJoe Perches			my @def_args = ();
4859f59b64bfSJoe Perches
4860f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
4861f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
4862f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
4863f59b64bfSJoe Perches				@def_args = split(",", $define_args);
4864f59b64bfSJoe Perches			}
4865f59b64bfSJoe Perches
4866292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4867c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4868c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4869c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4870c45dcabdSAndy Whitcroft
4871c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4872bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4873bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
48746b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4875bf30d6edSAndy Whitcroft			{
4876c45dcabdSAndy Whitcroft			}
4877c45dcabdSAndy Whitcroft
4878e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
487933acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
488033acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4881e45bab8eSAndy Whitcroft			{
4882e45bab8eSAndy Whitcroft			}
4883e45bab8eSAndy Whitcroft
488442e15293SJoe Perches			# Make asm volatile uses seem like a generic function
488542e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
488642e15293SJoe Perches
4887c45dcabdSAndy Whitcroft			my $exceptions = qr{
4888c45dcabdSAndy Whitcroft				$Declare|
4889c45dcabdSAndy Whitcroft				module_param_named|
4890a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4891c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4892c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4893383099fdSAndy Whitcroft				__typeof__\(|
489422fd2d3eSStefani Seibold				union|
489522fd2d3eSStefani Seibold				struct|
4896ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
48976b10df42SVladimir Zapolskiy				^\"|\"$|
48986b10df42SVladimir Zapolskiy				^\[
4899c45dcabdSAndy Whitcroft			}x;
49005eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4901f59b64bfSJoe Perches
4902f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
4903f59b64bfSJoe Perches			my $herectx = $here . "\n";
4904f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
4905f59b64bfSJoe Perches
4906f59b64bfSJoe Perches			for (my $n = 0; $n < $stmt_cnt; $n++) {
4907f59b64bfSJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4908f59b64bfSJoe Perches			}
4909f59b64bfSJoe Perches
4910f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4911f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4912f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
49133cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4914356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4915f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4916f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4917e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
491872f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4919f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4920f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4921f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
49224e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4923f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4924c45dcabdSAndy Whitcroft			{
4925e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
4926e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4927e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4928e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
4929f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4930f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4931f74bd194SAndy Whitcroft				} else {
4932000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4933388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4934d8aaf121SAndy Whitcroft				}
4935f59b64bfSJoe Perches
4936f59b64bfSJoe Perches			}
49375207649bSJoe Perches
49385207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
49395207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
49405207649bSJoe Perches			my $first = 1;
49415207649bSJoe Perches			$define_stmt = "";
49425207649bSJoe Perches			foreach my $l (@stmt_array) {
49435207649bSJoe Perches				$l =~ s/\\$//;
49445207649bSJoe Perches				if ($first) {
49455207649bSJoe Perches					$define_stmt = $l;
49465207649bSJoe Perches					$first = 0;
49475207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
49485207649bSJoe Perches					$define_stmt .= substr($l, 1);
49495207649bSJoe Perches				}
49505207649bSJoe Perches			}
49515207649bSJoe Perches			$define_stmt =~ s/$;//g;
49525207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
49535207649bSJoe Perches			$define_stmt = trim($define_stmt);
49545207649bSJoe Perches
4955f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
4956f59b64bfSJoe Perches			foreach my $arg (@def_args) {
4957f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
49589192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
49597fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
49607fe528a2SJoe Perches				$tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
49617fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
49627fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
49637fe528a2SJoe Perches				my $use_cnt = $tmp_stmt =~ s/\b$arg\b//g;
4964f59b64bfSJoe Perches				if ($use_cnt > 1) {
4965f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
4966f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
4967f59b64bfSJoe Perches				    }
49689192d41aSJoe Perches# check if any macro arguments may have other precedence issues
49697fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
49709192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
49719192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
49729192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
49739192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
49749192d41aSJoe Perches				}
49750a920b5bSAndy Whitcroft			}
49765023d347SJoe Perches
497708a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
497808a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
497908a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
498008a2843eSJoe Perches				my $herectx = $here . "\n";
498108a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
498208a2843eSJoe Perches
498308a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
498408a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
498508a2843eSJoe Perches				}
498608a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
498708a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
498808a2843eSJoe Perches			}
498908a2843eSJoe Perches
4990481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
49915023d347SJoe Perches
49925023d347SJoe Perches		} else {
49935023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4994481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4995481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
49965023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
49975023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
49985023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
49995023d347SJoe Perches			}
5000653d4876SAndy Whitcroft		}
50010a920b5bSAndy Whitcroft
5002b13edf7fSJoe Perches# do {} while (0) macro tests:
5003b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5004b13edf7fSJoe Perches# macro should not end with a semicolon
5005b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5006b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5007b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5008b13edf7fSJoe Perches			my $ln = $linenr;
5009b13edf7fSJoe Perches			my $cnt = $realcnt;
5010b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5011b13edf7fSJoe Perches			my $ctx = '';
5012b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5013b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5014b13edf7fSJoe Perches			$ctx = $dstat;
5015b13edf7fSJoe Perches
5016b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
50171b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5018b13edf7fSJoe Perches
5019b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5020b13edf7fSJoe Perches				my $stmts = $2;
5021b13edf7fSJoe Perches				my $semis = $3;
5022b13edf7fSJoe Perches
5023b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5024b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5025b13edf7fSJoe Perches				my $herectx = $here . "\n";
5026b13edf7fSJoe Perches
5027b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
5028b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
5029b13edf7fSJoe Perches				}
5030b13edf7fSJoe Perches
5031ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5032ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5033b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5034b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5035b13edf7fSJoe Perches				}
5036b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5037b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5038b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5039b13edf7fSJoe Perches				}
5040f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5041f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5042f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5043f5ef95b1SJoe Perches				my $herectx = $here . "\n";
5044f5ef95b1SJoe Perches
5045f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
5046f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
5047f5ef95b1SJoe Perches				}
5048f5ef95b1SJoe Perches
5049f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5050f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5051b13edf7fSJoe Perches			}
5052b13edf7fSJoe Perches		}
5053b13edf7fSJoe Perches
5054080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5055080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
5056080ba929SMike Frysinger#	.
5057080ba929SMike Frysinger#	ALIGN(...)
5058080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
5059080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
5060000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
5061000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
5062080ba929SMike Frysinger		}
5063080ba929SMike Frysinger
5064f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
506513214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
506613214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5067cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
506813214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5069cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5070cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5071aad4f614SJoe Perches				my @allowed = ();
5072aad4f614SJoe Perches				my $allow = 0;
507313214adfSAndy Whitcroft				my $seen = 0;
5074773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5075cf655043SAndy Whitcroft				my $ln = $linenr - 1;
507613214adfSAndy Whitcroft				for my $chunk (@chunks) {
507713214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
507813214adfSAndy Whitcroft
5079773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5080773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5081773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5082773647a0SAndy Whitcroft
5083aad4f614SJoe Perches					$allowed[$allow] = 0;
5084773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5085773647a0SAndy Whitcroft
5086773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5087773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5088773647a0SAndy Whitcroft
5089773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5090cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5091cf655043SAndy Whitcroft
5092773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
509313214adfSAndy Whitcroft
509413214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
509513214adfSAndy Whitcroft
5096aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5097cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5098cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5099aad4f614SJoe Perches						$allowed[$allow] = 1;
510013214adfSAndy Whitcroft					}
510113214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5102cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5103aad4f614SJoe Perches						$allowed[$allow] = 1;
510413214adfSAndy Whitcroft					}
5105cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5106cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5107aad4f614SJoe Perches						$allowed[$allow] = 1;
510813214adfSAndy Whitcroft					}
5109aad4f614SJoe Perches					$allow++;
511013214adfSAndy Whitcroft				}
5111aad4f614SJoe Perches				if ($seen) {
5112aad4f614SJoe Perches					my $sum_allowed = 0;
5113aad4f614SJoe Perches					foreach (@allowed) {
5114aad4f614SJoe Perches						$sum_allowed += $_;
5115aad4f614SJoe Perches					}
5116aad4f614SJoe Perches					if ($sum_allowed == 0) {
5117000d1cc1SJoe Perches						WARN("BRACES",
5118000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5119aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5120aad4f614SJoe Perches						 $seen != $allow) {
5121aad4f614SJoe Perches						CHK("BRACES",
5122aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5123aad4f614SJoe Perches					}
512413214adfSAndy Whitcroft				}
512513214adfSAndy Whitcroft			}
512613214adfSAndy Whitcroft		}
5127773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
512813214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5129cf655043SAndy Whitcroft			my $allowed = 0;
5130f0a594c1SAndy Whitcroft
5131cf655043SAndy Whitcroft			# Check the pre-context.
5132cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5133cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5134cf655043SAndy Whitcroft				$allowed = 1;
5135f0a594c1SAndy Whitcroft			}
5136773647a0SAndy Whitcroft
5137773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5138773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5139773647a0SAndy Whitcroft
5140cf655043SAndy Whitcroft			# Check the condition.
5141cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5142773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5143cf655043SAndy Whitcroft			if (defined $cond) {
5144773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5145cf655043SAndy Whitcroft			}
5146cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5147cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5148cf655043SAndy Whitcroft				$allowed = 1;
5149cf655043SAndy Whitcroft			}
5150cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5151cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5152cf655043SAndy Whitcroft				$allowed = 1;
5153cf655043SAndy Whitcroft			}
5154cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5155cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5156cf655043SAndy Whitcroft				$allowed = 1;
5157cf655043SAndy Whitcroft			}
5158cf655043SAndy Whitcroft			# Check the post-context.
5159cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5160cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5161cf655043SAndy Whitcroft				if (defined $cond) {
5162773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5163cf655043SAndy Whitcroft				}
5164cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5165cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5166cf655043SAndy Whitcroft					$allowed = 1;
5167cf655043SAndy Whitcroft				}
5168cf655043SAndy Whitcroft			}
5169cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
517069932487SJustin P. Mattock				my $herectx = $here . "\n";
5171f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5172cf655043SAndy Whitcroft
5173f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
517469932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
5175cf655043SAndy Whitcroft				}
5176cf655043SAndy Whitcroft
5177000d1cc1SJoe Perches				WARN("BRACES",
5178000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5179f0a594c1SAndy Whitcroft			}
5180f0a594c1SAndy Whitcroft		}
5181f0a594c1SAndy Whitcroft
5182e4c5babdSJoe Perches# check for single line unbalanced braces
518395330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
518495330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5185e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5186e4c5babdSJoe Perches		}
5187e4c5babdSJoe Perches
51880979ae66SJoe Perches# check for unnecessary blank lines around braces
518977b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5190f8e58219SJoe Perches			if (CHK("BRACES",
5191f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5192f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5193f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5194f8e58219SJoe Perches			}
51950979ae66SJoe Perches		}
519677b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5197f8e58219SJoe Perches			if (CHK("BRACES",
5198f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5199f8e58219SJoe Perches			    $fix) {
5200f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5201f8e58219SJoe Perches			}
52020979ae66SJoe Perches		}
52030979ae66SJoe Perches
52044a0df2efSAndy Whitcroft# no volatiles please
52056c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
52066c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5207000d1cc1SJoe Perches			WARN("VOLATILE",
52088c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
52094a0df2efSAndy Whitcroft		}
52104a0df2efSAndy Whitcroft
52115e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
52125e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
52135e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
52145e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
521533acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
52165e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
52175e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
52185e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
52195e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
52205e4f6ba5SJoe Perches				     $fix &&
52215e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
52225e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
52235e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
52245e4f6ba5SJoe Perches				my $comma_close = "";
52255e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
52265e4f6ba5SJoe Perches					$comma_close = $1;
52275e4f6ba5SJoe Perches				}
52285e4f6ba5SJoe Perches
52295e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
52305e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
52315e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
52325e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
52335e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
52345e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
52355e4f6ba5SJoe Perches				$fixedline = $rawline;
52365e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
52375e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
52385e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
52395e4f6ba5SJoe Perches				}
52405e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
52415e4f6ba5SJoe Perches			}
52425e4f6ba5SJoe Perches		}
52435e4f6ba5SJoe Perches
52445e4f6ba5SJoe Perches# check for missing a space in a string concatenation
52455e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
52465e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
52475e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
52485e4f6ba5SJoe Perches		}
52495e4f6ba5SJoe Perches
525077cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5251e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5252e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5253e4b7d309SJoe Perches# function declarations
525477cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
525577cb8546SJoe Perches		    defined($context_function) &&
5256e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5257e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
525877cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5259e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
526077cb8546SJoe Perches		}
526177cb8546SJoe Perches
52625e4f6ba5SJoe Perches# check for spaces before a quoted newline
52635e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
52645e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
52655e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
52665e4f6ba5SJoe Perches			    $fix) {
52675e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
52685e4f6ba5SJoe Perches			}
52695e4f6ba5SJoe Perches
52705e4f6ba5SJoe Perches		}
52715e4f6ba5SJoe Perches
5272f17dba4fSJoe Perches# concatenated string without spaces between elements
527333acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5274f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
5275f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
5276f17dba4fSJoe Perches		}
5277f17dba4fSJoe Perches
527890ad30e5SJoe Perches# uncoalesced string fragments
527933acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
528090ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
528190ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
528290ad30e5SJoe Perches		}
528390ad30e5SJoe Perches
5284522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5285522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5286522b837cSAlexey Dobriyan		my $show_Z = 1;
52875e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5288522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
52895e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5290522b837cSAlexey Dobriyan			# check for %L
5291522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
52925e4f6ba5SJoe Perches				WARN("PRINTF_L",
5293522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5294522b837cSAlexey Dobriyan				$show_L = 0;
52955e4f6ba5SJoe Perches			}
5296522b837cSAlexey Dobriyan			# check for %Z
5297522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5298522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5299522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5300522b837cSAlexey Dobriyan				$show_Z = 0;
5301522b837cSAlexey Dobriyan			}
5302522b837cSAlexey Dobriyan			# check for 0x<decimal>
5303522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5304522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
53056e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
53066e300757SJoe Perches			}
53075e4f6ba5SJoe Perches		}
53085e4f6ba5SJoe Perches
53095e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
53105e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
53115e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
53125e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
53135e4f6ba5SJoe Perches		}
53145e4f6ba5SJoe Perches
531500df344fSAndy Whitcroft# warn about #if 0
5316c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5317000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
5318000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
5319de7d4f0eSAndy Whitcroft				$herecurr);
53204a0df2efSAndy Whitcroft		}
53214a0df2efSAndy Whitcroft
532203df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
532303df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5324100425deSJoe Perches			my $tested = quotemeta($1);
5325100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5326100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5327100425deSJoe Perches				my $func = $1;
5328100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5329100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5330100425deSJoe Perches				    $fix) {
5331100425deSJoe Perches					my $do_fix = 1;
5332100425deSJoe Perches					my $leading_tabs = "";
5333100425deSJoe Perches					my $new_leading_tabs = "";
5334100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5335100425deSJoe Perches						$leading_tabs = $1;
5336100425deSJoe Perches					} else {
5337100425deSJoe Perches						$do_fix = 0;
5338100425deSJoe Perches					}
5339100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5340100425deSJoe Perches						$new_leading_tabs = $1;
5341100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5342100425deSJoe Perches							$do_fix = 0;
5343100425deSJoe Perches						}
5344100425deSJoe Perches					} else {
5345100425deSJoe Perches						$do_fix = 0;
5346100425deSJoe Perches					}
5347100425deSJoe Perches					if ($do_fix) {
5348100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5349100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5350100425deSJoe Perches					}
5351100425deSJoe Perches				}
53524c432a8fSGreg Kroah-Hartman			}
53534c432a8fSGreg Kroah-Hartman		}
5354f0a594c1SAndy Whitcroft
5355ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5356ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5357ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5358ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5359ebfdc409SJoe Perches		    $linenr > 3) {
5360ebfdc409SJoe Perches			my $testval = $2;
5361ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5362ebfdc409SJoe Perches
5363ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5364ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5365ebfdc409SJoe Perches
5366fb0d0e08SJoe 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)/) {
5367ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5368ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5369ebfdc409SJoe Perches			}
5370ebfdc409SJoe Perches		}
5371ebfdc409SJoe Perches
5372f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5373dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5374f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5375f78d98f6SJoe Perches			my $level = $1;
5376f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5377f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5378f78d98f6SJoe Perches			    $fix) {
5379f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5380f78d98f6SJoe Perches			}
5381f78d98f6SJoe Perches		}
5382f78d98f6SJoe Perches
538345c55e92SJoe Perches# check for logging continuations
538445c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
538545c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
538645c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
538745c55e92SJoe Perches		}
538845c55e92SJoe Perches
5389abb08a53SJoe Perches# check for mask then right shift without a parentheses
5390abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5391abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5392abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5393abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5394abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5395abb08a53SJoe Perches		}
5396abb08a53SJoe Perches
5397b75ac618SJoe Perches# check for pointer comparisons to NULL
5398b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
5399b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5400b75ac618SJoe Perches				my $val = $1;
5401b75ac618SJoe Perches				my $equal = "!";
5402b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5403b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5404b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5405b75ac618SJoe Perches					    $fix) {
5406b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5407b75ac618SJoe Perches				}
5408b75ac618SJoe Perches			}
5409b75ac618SJoe Perches		}
5410b75ac618SJoe Perches
54118716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
54128716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
54138716de38SJoe Perches			my $attr = $1;
54148716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
54158716de38SJoe Perches				my $ptr = $1;
54168716de38SJoe Perches				my $var = $2;
54178716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
54188716de38SJoe Perches				      ERROR("MISPLACED_INIT",
54198716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
54208716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
54218716de38SJoe Perches				      WARN("MISPLACED_INIT",
54228716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
54238716de38SJoe Perches				    $fix) {
5424194f66fcSJoe 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;
54258716de38SJoe Perches				}
54268716de38SJoe Perches			}
54278716de38SJoe Perches		}
54288716de38SJoe Perches
5429e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5430e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5431e970b884SJoe Perches			my $attr = $1;
5432e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5433e970b884SJoe Perches			my $attr_prefix = $1;
5434e970b884SJoe Perches			my $attr_type = $2;
5435e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5436e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5437e970b884SJoe Perches			    $fix) {
5438194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5439e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5440e970b884SJoe Perches			}
5441e970b884SJoe Perches		}
5442e970b884SJoe Perches
5443e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5444e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5445e970b884SJoe Perches			my $attr = $1;
5446e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5447e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5448e970b884SJoe Perches			    $fix) {
5449194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5450e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5451e970b884SJoe Perches				$lead = rtrim($1);
5452e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5453e970b884SJoe Perches				$lead = "${lead}const ";
5454194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5455e970b884SJoe Perches			}
5456e970b884SJoe Perches		}
5457e970b884SJoe Perches
5458c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5459c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5460c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5461c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5462c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5463c17893c7SJoe Perches			    $fix) {
5464c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5465c17893c7SJoe Perches			}
5466c17893c7SJoe Perches		}
5467c17893c7SJoe Perches
5468fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5469fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5470fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5471fbdb8138SJoe Perches			my $constant_func = $1;
5472fbdb8138SJoe Perches			my $func = $constant_func;
5473fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5474fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5475fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5476fbdb8138SJoe Perches			    $fix) {
5477194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5478fbdb8138SJoe Perches			}
5479fbdb8138SJoe Perches		}
5480fbdb8138SJoe Perches
54811a15a250SPatrick Pannuto# prefer usleep_range over udelay
548237581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
548343c1d77cSJoe Perches			my $delay = $1;
54841a15a250SPatrick Pannuto			# ignore udelay's < 10, however
548543c1d77cSJoe Perches			if (! ($delay < 10) ) {
5486000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
548743c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
548843c1d77cSJoe Perches			}
548943c1d77cSJoe Perches			if ($delay > 2000) {
549043c1d77cSJoe Perches				WARN("LONG_UDELAY",
549143c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
54921a15a250SPatrick Pannuto			}
54931a15a250SPatrick Pannuto		}
54941a15a250SPatrick Pannuto
549509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
549609ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
549709ef8725SPatrick Pannuto			if ($1 < 20) {
5498000d1cc1SJoe Perches				WARN("MSLEEP",
549943c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
550009ef8725SPatrick Pannuto			}
550109ef8725SPatrick Pannuto		}
550209ef8725SPatrick Pannuto
550336ec1939SJoe Perches# check for comparisons of jiffies
550436ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
550536ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
550636ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
550736ec1939SJoe Perches		}
550836ec1939SJoe Perches
55099d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
55109d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
55119d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
55129d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
55139d7a34a5SJoe Perches		}
55149d7a34a5SJoe Perches
551500df344fSAndy Whitcroft# warn about #ifdefs in C files
5516c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
551700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
551800df344fSAndy Whitcroft#			print "$herecurr";
551900df344fSAndy Whitcroft#			$clean = 0;
552000df344fSAndy Whitcroft#		}
552100df344fSAndy Whitcroft
552222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5523c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
55243705ce5bSJoe Perches			if (ERROR("SPACING",
55253705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
55263705ce5bSJoe Perches			    $fix) {
5527194f66fcSJoe Perches				$fixed[$fixlinenr] =~
55283705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
55293705ce5bSJoe Perches			}
55303705ce5bSJoe Perches
553122f2a2efSAndy Whitcroft		}
553222f2a2efSAndy Whitcroft
55334a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5534171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5535171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
55364a0df2efSAndy Whitcroft			my $which = $1;
55374a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5538000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5539000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
55404a0df2efSAndy Whitcroft			}
55414a0df2efSAndy Whitcroft		}
55424a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5543402c2553SMichael S. Tsirkin
5544402c2553SMichael S. Tsirkin		my $barriers = qr{
5545402c2553SMichael S. Tsirkin			mb|
5546402c2553SMichael S. Tsirkin			rmb|
5547402c2553SMichael S. Tsirkin			wmb|
5548402c2553SMichael S. Tsirkin			read_barrier_depends
5549402c2553SMichael S. Tsirkin		}x;
5550402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5551402c2553SMichael S. Tsirkin			mb__before_atomic|
5552402c2553SMichael S. Tsirkin			mb__after_atomic|
5553402c2553SMichael S. Tsirkin			store_release|
5554402c2553SMichael S. Tsirkin			load_acquire|
5555402c2553SMichael S. Tsirkin			store_mb|
5556402c2553SMichael S. Tsirkin			(?:$barriers)
5557402c2553SMichael S. Tsirkin		}x;
5558402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5559402c2553SMichael S. Tsirkin			(?:$barriers)|
556043e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
556143e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5562402c2553SMichael S. Tsirkin		}x;
5563402c2553SMichael S. Tsirkin
5564402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
55654a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5566c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5567000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
55684a0df2efSAndy Whitcroft			}
55694a0df2efSAndy Whitcroft		}
55703ad81779SPaul E. McKenney
5571f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5572f4073b0fSMichael S. Tsirkin
5573f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5574f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5575f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5576f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5577f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5578f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5579f4073b0fSMichael S. Tsirkin		}
5580f4073b0fSMichael S. Tsirkin
5581cb426e99SJoe Perches# check for waitqueue_active without a comment.
5582cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5583cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5584cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5585cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5586cb426e99SJoe Perches			}
5587cb426e99SJoe Perches		}
55883ad81779SPaul E. McKenney
55894a0df2efSAndy Whitcroft# check of hardware specific defines
5590c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5591000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5592000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
55930a920b5bSAndy Whitcroft		}
5594653d4876SAndy Whitcroft
5595596ed45bSJoe Perches# check that the storage class is not after a type
5596596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5597000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5598596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5599596ed45bSJoe Perches		}
5600596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5601596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5602596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5603596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5604596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5605596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5606596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5607d4977c78STobias Klauser		}
5608d4977c78STobias Klauser
5609de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5610de7d4f0eSAndy Whitcroft# storage class and type.
56119c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
56129c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5613000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5614000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5615de7d4f0eSAndy Whitcroft		}
5616de7d4f0eSAndy Whitcroft
56178905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
56182b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56192b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5620d5e616fcSJoe Perches			if (WARN("INLINE",
5621d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5622d5e616fcSJoe Perches			    $fix) {
5623194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5624d5e616fcSJoe Perches
5625d5e616fcSJoe Perches			}
56268905a67cSAndy Whitcroft		}
56278905a67cSAndy Whitcroft
56283d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
56292b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56302b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5631000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5632000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
56333d130fd0SJoe Perches		}
56343d130fd0SJoe Perches
563539b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
56362b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56372b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5638000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5639000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
564039b7e287SJoe Perches		}
564139b7e287SJoe Perches
56425f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
56432b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56442b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5645d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5646d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5647d5e616fcSJoe Perches			    $fix) {
5648194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5649d5e616fcSJoe Perches
5650d5e616fcSJoe Perches			}
56515f14d3bdSJoe Perches		}
56525f14d3bdSJoe Perches
56536061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
56542b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56552b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5656d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5657d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5658d5e616fcSJoe Perches			    $fix) {
5659194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5660d5e616fcSJoe Perches			}
56616061d949SJoe Perches		}
56626061d949SJoe Perches
5663619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5664619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5665619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5666619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5667619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5668619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5669619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5670619a908aSJoe Perches		}
5671619a908aSJoe Perches
5672fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5673e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5674fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5675e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5676e6176fa4SJoe Perches			my $type = $1;
5677e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5678e6176fa4SJoe Perches				$type = $1;
5679e6176fa4SJoe Perches				my $kernel_type = 'u';
5680e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5681e6176fa4SJoe Perches				$type =~ /(\d+)/;
5682e6176fa4SJoe Perches				$kernel_type .= $1;
5683e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5684e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5685e6176fa4SJoe Perches				    $fix) {
5686e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5687e6176fa4SJoe Perches				}
5688e6176fa4SJoe Perches			}
5689e6176fa4SJoe Perches		}
5690e6176fa4SJoe Perches
5691938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5692938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5693938224b5SJoe Perches			my $cast = $1;
5694938224b5SJoe Perches			my $const = $2;
5695938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5696938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5697938224b5SJoe Perches			    $fix) {
5698938224b5SJoe Perches				my $suffix = "";
5699938224b5SJoe Perches				my $newconst = $const;
5700938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5701938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5702938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5703938224b5SJoe Perches					$suffix .= 'LL';
5704938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5705938224b5SJoe Perches					$suffix .= 'L';
5706938224b5SJoe Perches				}
5707938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5708938224b5SJoe Perches			}
5709938224b5SJoe Perches		}
5710938224b5SJoe Perches
57118f53a9b8SJoe Perches# check for sizeof(&)
57128f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5713000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5714000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
57158f53a9b8SJoe Perches		}
57168f53a9b8SJoe Perches
571766c80b60SJoe Perches# check for sizeof without parenthesis
571866c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5719d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5720d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5721d5e616fcSJoe Perches			    $fix) {
5722194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5723d5e616fcSJoe Perches			}
572466c80b60SJoe Perches		}
572566c80b60SJoe Perches
572688982feaSJoe Perches# check for struct spinlock declarations
572788982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
572888982feaSJoe Perches			WARN("USE_SPINLOCK_T",
572988982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
573088982feaSJoe Perches		}
573188982feaSJoe Perches
5732a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
573306668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5734a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5735caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5736caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5737d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5738d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5739d5e616fcSJoe Perches				    $fix) {
5740194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5741d5e616fcSJoe Perches				}
5742a6962d72SJoe Perches			}
5743a6962d72SJoe Perches		}
5744a6962d72SJoe Perches
57450b523769SJoe Perches		# check for vsprintf extension %p<foo> misuses
57460b523769SJoe Perches		if ($^V && $^V ge 5.10.0 &&
57470b523769SJoe Perches		    defined $stat &&
57480b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
57490b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
57500b523769SJoe Perches			my $bad_extension = "";
57510b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
57520b523769SJoe Perches			$lc = $lc + $linenr;
57530b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
57540b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
57550b523769SJoe Perches				$fmt =~ s/%%//g;
5756ce4fecf1SPantelis Antoniou				if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNO]).)/) {
57570b523769SJoe Perches					$bad_extension = $1;
57580b523769SJoe Perches					last;
57590b523769SJoe Perches				}
57600b523769SJoe Perches			}
57610b523769SJoe Perches			if ($bad_extension ne "") {
57620b523769SJoe Perches				my $stat_real = raw_line($linenr, 0);
57630b523769SJoe Perches				for (my $count = $linenr + 1; $count <= $lc; $count++) {
57640b523769SJoe Perches					$stat_real = $stat_real . "\n" . raw_line($count, 0);
57650b523769SJoe Perches				}
57660b523769SJoe Perches				WARN("VSPRINTF_POINTER_EXTENSION",
57670b523769SJoe Perches				     "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
57680b523769SJoe Perches			}
57690b523769SJoe Perches		}
57700b523769SJoe Perches
5771554e165cSAndy Whitcroft# Check for misused memsets
5772d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5773d1fe9c09SJoe Perches		    defined $stat &&
57749e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5775554e165cSAndy Whitcroft
5776d7c76ba7SJoe Perches			my $ms_addr = $2;
5777d1fe9c09SJoe Perches			my $ms_val = $7;
5778d1fe9c09SJoe Perches			my $ms_size = $12;
5779d7c76ba7SJoe Perches
5780554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5781554e165cSAndy Whitcroft				ERROR("MEMSET",
5782d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5783554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5784554e165cSAndy Whitcroft				WARN("MEMSET",
5785d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5786d7c76ba7SJoe Perches			}
5787d7c76ba7SJoe Perches		}
5788d7c76ba7SJoe Perches
578998a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5790f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5791f333195dSJoe Perches#		    defined $stat &&
5792f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5793f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
5794f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5795f333195dSJoe Perches#			    $fix) {
5796f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5797f333195dSJoe Perches#			}
5798f333195dSJoe Perches#		}
579998a9bba5SJoe Perches
5800b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5801f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5802f333195dSJoe Perches#		    defined $stat &&
5803f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5804f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
5805f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5806f333195dSJoe Perches#		}
5807b6117d17SMateusz Kulikowski
58088617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
58098617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5810f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5811f333195dSJoe Perches#		    defined $stat &&
5812f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5813f333195dSJoe Perches#
5814f333195dSJoe Perches#			my $ms_val = $7;
5815f333195dSJoe Perches#
5816f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
5817f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
5818f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5819f333195dSJoe Perches#				    $fix) {
5820f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5821f333195dSJoe Perches#				}
5822f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5823f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
5824f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5825f333195dSJoe Perches#				    $fix) {
5826f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5827f333195dSJoe Perches#				}
5828f333195dSJoe Perches#			}
5829f333195dSJoe Perches#		}
58308617cd09SMateusz Kulikowski
5831d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5832d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5833d1fe9c09SJoe Perches		    defined $stat &&
5834d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5835d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5836d7c76ba7SJoe Perches				my $call = $1;
5837d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5838d7c76ba7SJoe Perches				my $arg1 = $3;
5839d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5840d1fe9c09SJoe Perches				my $arg2 = $8;
5841d7c76ba7SJoe Perches				my $cast;
5842d7c76ba7SJoe Perches
5843d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5844d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5845d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5846d7c76ba7SJoe Perches					$cast = $cast1;
5847d7c76ba7SJoe Perches				} else {
5848d7c76ba7SJoe Perches					$cast = $cast2;
5849d7c76ba7SJoe Perches				}
5850d7c76ba7SJoe Perches				WARN("MINMAX",
5851d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5852554e165cSAndy Whitcroft			}
5853554e165cSAndy Whitcroft		}
5854554e165cSAndy Whitcroft
58554a273195SJoe Perches# check usleep_range arguments
58564a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
58574a273195SJoe Perches		    defined $stat &&
58584a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
58594a273195SJoe Perches			my $min = $1;
58604a273195SJoe Perches			my $max = $7;
58614a273195SJoe Perches			if ($min eq $max) {
58624a273195SJoe Perches				WARN("USLEEP_RANGE",
58634a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
58644a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
58654a273195SJoe Perches				 $min > $max) {
58664a273195SJoe Perches				WARN("USLEEP_RANGE",
58674a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
58684a273195SJoe Perches			}
58694a273195SJoe Perches		}
58704a273195SJoe Perches
5871823b794cSJoe Perches# check for naked sscanf
5872823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5873823b794cSJoe Perches		    defined $stat &&
58746c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5875823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5876823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5877823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5878823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5879823b794cSJoe Perches			$lc = $lc + $linenr;
5880823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5881823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5882823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5883823b794cSJoe Perches			}
5884823b794cSJoe Perches			WARN("NAKED_SSCANF",
5885823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5886823b794cSJoe Perches		}
5887823b794cSJoe Perches
5888afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5889afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5890afc819abSJoe Perches		    defined $stat &&
5891afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5892afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5893afc819abSJoe Perches			$lc = $lc + $linenr;
5894afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5895afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5896afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5897afc819abSJoe Perches			}
5898afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5899afc819abSJoe Perches				my $format = $6;
5900afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5901afc819abSJoe Perches				if ($count == 1 &&
5902afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5903afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5904afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5905afc819abSJoe Perches				}
5906afc819abSJoe Perches			}
5907afc819abSJoe Perches		}
5908afc819abSJoe Perches
590970dc8a48SJoe Perches# check for new externs in .h files.
591070dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
591170dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5912d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
591370dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
591470dc8a48SJoe Perches			    $fix) {
5915194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
591670dc8a48SJoe Perches			}
591770dc8a48SJoe Perches		}
591870dc8a48SJoe Perches
5919de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5920171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5921c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5922171ae1a4SAndy Whitcroft		{
5923c45dcabdSAndy Whitcroft			my $function_name = $1;
5924c45dcabdSAndy Whitcroft			my $paren_space = $2;
5925171ae1a4SAndy Whitcroft
5926171ae1a4SAndy Whitcroft			my $s = $stat;
5927171ae1a4SAndy Whitcroft			if (defined $cond) {
5928171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5929171ae1a4SAndy Whitcroft			}
5930c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5931c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5932c45dcabdSAndy Whitcroft			{
5933000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5934000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5935de7d4f0eSAndy Whitcroft			}
5936de7d4f0eSAndy Whitcroft
5937171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5938000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5939000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5940171ae1a4SAndy Whitcroft			}
59419c9ba34eSAndy Whitcroft
59429c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
59439c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
59449c9ba34eSAndy Whitcroft		{
5945000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5946000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5947171ae1a4SAndy Whitcroft		}
5948171ae1a4SAndy Whitcroft
5949a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
5950a0ad7596SJoe Perches		if (defined $stat &&
595125bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
5952ca0d8929SJoe Perches		    $1 ne "void") {
5953ca0d8929SJoe Perches			my $args = trim($1);
5954ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5955ca0d8929SJoe Perches				my $arg = trim($1);
5956ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5957ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
5958ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5959ca0d8929SJoe Perches				}
5960ca0d8929SJoe Perches			}
5961ca0d8929SJoe Perches		}
5962ca0d8929SJoe Perches
5963a0ad7596SJoe Perches# check for function definitions
5964a0ad7596SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5965a0ad7596SJoe Perches		    defined $stat &&
5966a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
5967a0ad7596SJoe Perches			$context_function = $1;
5968a0ad7596SJoe Perches
5969a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
5970a0ad7596SJoe Perches			my $ok = 0;
5971a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
5972a0ad7596SJoe Perches			my $herectx = $here . "\n";
5973a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5974a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
5975a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
5976a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
5977a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
5978a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
5979a0ad7596SJoe Perches			}
5980a0ad7596SJoe Perches			if (!$ok) {
5981a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
5982a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
5983a0ad7596SJoe Perches			}
5984a0ad7596SJoe Perches		}
5985a0ad7596SJoe Perches
5986de7d4f0eSAndy Whitcroft# checks for new __setup's
5987de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5988de7d4f0eSAndy Whitcroft			my $name = $1;
5989de7d4f0eSAndy Whitcroft
5990de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5991000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
59928c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
5993de7d4f0eSAndy Whitcroft			}
5994653d4876SAndy Whitcroft		}
59959c0ca6f9SAndy Whitcroft
59969c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5997caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5998000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5999000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
60009c0ca6f9SAndy Whitcroft		}
600113214adfSAndy Whitcroft
6002a640d25cSJoe Perches# alloc style
6003a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6004a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6005a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6006a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6007a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6008a640d25cSJoe Perches		}
6009a640d25cSJoe Perches
601060a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
601160a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
60121b4a2ed4SJoe Perches		    defined $stat &&
60131b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
601460a55369SJoe Perches			my $oldfunc = $3;
601560a55369SJoe Perches			my $a1 = $4;
601660a55369SJoe Perches			my $a2 = $10;
601760a55369SJoe Perches			my $newfunc = "kmalloc_array";
601860a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
601960a55369SJoe Perches			my $r1 = $a1;
602060a55369SJoe Perches			my $r2 = $a2;
602160a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
602260a55369SJoe Perches				$r1 = $a2;
602360a55369SJoe Perches				$r2 = $a1;
602460a55369SJoe Perches			}
6025e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6026e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
60271b4a2ed4SJoe Perches				my $ctx = '';
60281b4a2ed4SJoe Perches				my $herectx = $here . "\n";
60291b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
60301b4a2ed4SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
60311b4a2ed4SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
60321b4a2ed4SJoe Perches				}
6033e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
60341b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
60351b4a2ed4SJoe Perches				    $cnt == 1 &&
6036e367455aSJoe Perches				    $fix) {
6037194f66fcSJoe 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;
603860a55369SJoe Perches				}
603960a55369SJoe Perches			}
604060a55369SJoe Perches		}
604160a55369SJoe Perches
6042972fdea2SJoe Perches# check for krealloc arg reuse
6043972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
6044972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6045972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6046972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6047972fdea2SJoe Perches		}
6048972fdea2SJoe Perches
60495ce59ae0SJoe Perches# check for alloc argument mismatch
60505ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
60515ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
60525ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
60535ce59ae0SJoe Perches		}
60545ce59ae0SJoe Perches
6055caf2a54fSJoe Perches# check for multiple semicolons
6056caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6057d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6058d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6059d5e616fcSJoe Perches			    $fix) {
6060194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6061d5e616fcSJoe Perches			}
6062d1e2ad07SJoe Perches		}
6063d1e2ad07SJoe Perches
6064cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6065cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6066cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
60670ab90191SJoe Perches			my $ull = "";
60680ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
60690ab90191SJoe Perches			if (CHK("BIT_MACRO",
60700ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
60710ab90191SJoe Perches			    $fix) {
60720ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
60730ab90191SJoe Perches			}
60740ab90191SJoe Perches		}
60750ab90191SJoe Perches
60762d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
60772d632745SJoe 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*$/) {
60782d632745SJoe Perches			my $config = $1;
60792d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
60802d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
60812d632745SJoe Perches			    $fix) {
60822d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
60832d632745SJoe Perches			}
60842d632745SJoe Perches		}
60852d632745SJoe Perches
6086e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6087c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6088c34c09a8SJoe Perches			my $has_break = 0;
6089c34c09a8SJoe Perches			my $has_statement = 0;
6090c34c09a8SJoe Perches			my $count = 0;
6091c34c09a8SJoe Perches			my $prevline = $linenr;
6092e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6093c34c09a8SJoe Perches				$prevline--;
6094c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6095c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6096c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6097c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6098c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6099c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6100c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6101c34c09a8SJoe Perches				$has_statement = 1;
6102c34c09a8SJoe Perches				$count++;
6103258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6104c34c09a8SJoe Perches			}
6105c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6106c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6107224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6108c34c09a8SJoe Perches			}
6109c34c09a8SJoe Perches		}
6110c34c09a8SJoe Perches
6111d1e2ad07SJoe Perches# check for switch/default statements without a break;
6112d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
6113d1e2ad07SJoe Perches		    defined $stat &&
6114d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6115d1e2ad07SJoe Perches			my $ctx = '';
6116d1e2ad07SJoe Perches			my $herectx = $here . "\n";
6117d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6118d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6119d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
6120d1e2ad07SJoe Perches			}
6121d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6122d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6123caf2a54fSJoe Perches		}
6124caf2a54fSJoe Perches
612513214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6126d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6127d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6128d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6129d5e616fcSJoe Perches			    $fix) {
6130194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6131d5e616fcSJoe Perches			}
613213214adfSAndy Whitcroft		}
6133773647a0SAndy Whitcroft
613462ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
613562ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
613662ec818fSJoe Perches			ERROR("DATE_TIME",
613762ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
613862ec818fSJoe Perches		}
613962ec818fSJoe Perches
61402c92488aSJoe Perches# check for use of yield()
61412c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
61422c92488aSJoe Perches			WARN("YIELD",
61432c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
61442c92488aSJoe Perches		}
61452c92488aSJoe Perches
6146179f8f40SJoe Perches# check for comparisons against true and false
6147179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6148179f8f40SJoe Perches			my $lead = $1;
6149179f8f40SJoe Perches			my $arg = $2;
6150179f8f40SJoe Perches			my $test = $3;
6151179f8f40SJoe Perches			my $otype = $4;
6152179f8f40SJoe Perches			my $trail = $5;
6153179f8f40SJoe Perches			my $op = "!";
6154179f8f40SJoe Perches
6155179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6156179f8f40SJoe Perches
6157179f8f40SJoe Perches			my $type = lc($otype);
6158179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6159179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6160179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6161179f8f40SJoe Perches					$op = "";
6162179f8f40SJoe Perches				}
6163179f8f40SJoe Perches
6164179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6165179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6166179f8f40SJoe Perches
6167179f8f40SJoe Perches## maybe suggesting a correct construct would better
6168179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6169179f8f40SJoe Perches
6170179f8f40SJoe Perches			}
6171179f8f40SJoe Perches		}
6172179f8f40SJoe Perches
61734882720bSThomas Gleixner# check for semaphores initialized locked
61744882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6175000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6176000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6177773647a0SAndy Whitcroft		}
61786712d858SJoe Perches
617967d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
618067d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6181000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
618267d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6183773647a0SAndy Whitcroft		}
61846712d858SJoe Perches
6185ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6186f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6187000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6188ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6189f3db6639SMichael Ellerman		}
61906712d858SJoe Perches
61910f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6192d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
61936903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6194d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6195000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6196d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
61972b6db5cbSAndy Whitcroft		}
6198773647a0SAndy Whitcroft
6199773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6200773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6201773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6202c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6203c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6204171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6205171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6206171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6207773647a0SAndy Whitcroft		{
6208000d1cc1SJoe Perches			WARN("NR_CPUS",
6209000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6210773647a0SAndy Whitcroft		}
62119c9ba34eSAndy Whitcroft
621252ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
621352ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
621452ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
621552ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
621652ea8506SJoe Perches		}
621752ea8506SJoe Perches
6218acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6219acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6220acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6221acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6222acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6223acd9362cSJoe Perches		}
6224acd9362cSJoe Perches
6225691d77b6SAndy Whitcroft# whine mightly about in_atomic
6226691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6227691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6228000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6229000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6230f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6231000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6232000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6233691d77b6SAndy Whitcroft			}
6234691d77b6SAndy Whitcroft		}
62351704f47bSPeter Zijlstra
6236481aea5cSJoe Perches# whine about ACCESS_ONCE
6237481aea5cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6238481aea5cSJoe Perches		    $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6239481aea5cSJoe Perches			my $par = $1;
6240481aea5cSJoe Perches			my $eq = $2;
6241481aea5cSJoe Perches			my $fun = $3;
6242481aea5cSJoe Perches			$par =~ s/^\(\s*(.*)\s*\)$/$1/;
6243481aea5cSJoe Perches			if (defined($eq)) {
6244481aea5cSJoe Perches				if (WARN("PREFER_WRITE_ONCE",
6245481aea5cSJoe Perches					 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6246481aea5cSJoe Perches				    $fix) {
6247481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6248481aea5cSJoe Perches				}
6249481aea5cSJoe Perches			} else {
6250481aea5cSJoe Perches				if (WARN("PREFER_READ_ONCE",
6251481aea5cSJoe Perches					 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6252481aea5cSJoe Perches				    $fix) {
6253481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6254481aea5cSJoe Perches				}
6255481aea5cSJoe Perches			}
6256481aea5cSJoe Perches		}
6257481aea5cSJoe Perches
62580f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
62590f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
62600f5225b0SPeter Zijlstra			ERROR("LOCKING",
62610f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
62620f5225b0SPeter Zijlstra		}
62630f5225b0SPeter Zijlstra
62641704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
62651704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
62661704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
62671704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
62681704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
62691704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6270000d1cc1SJoe Perches				ERROR("LOCKDEP",
6271000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
62721704f47bSPeter Zijlstra			}
62731704f47bSPeter Zijlstra		}
627488f8831cSDave Jones
6275b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6276b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6277000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6278000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
627988f8831cSDave Jones		}
62802435880fSJoe Perches
6281515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6282515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6283515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6284459cf0aeSJoe Perches		    defined $stat &&
6285515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
62862435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
62872435880fSJoe Perches				my $func = $entry->[0];
62882435880fSJoe Perches				my $arg_pos = $entry->[1];
62892435880fSJoe Perches
6290459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6291459cf0aeSJoe Perches				$lc = $lc + $linenr;
6292459cf0aeSJoe Perches				my $stat_real = raw_line($linenr, 0);
6293459cf0aeSJoe Perches				for (my $count = $linenr + 1; $count <= $lc; $count++) {
6294459cf0aeSJoe Perches					$stat_real = $stat_real . "\n" . raw_line($count, 0);
6295459cf0aeSJoe Perches				}
6296459cf0aeSJoe Perches
62972435880fSJoe Perches				my $skip_args = "";
62982435880fSJoe Perches				if ($arg_pos > 1) {
62992435880fSJoe Perches					$arg_pos--;
63002435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
63012435880fSJoe Perches				}
6302f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6303459cf0aeSJoe Perches				if ($stat =~ /$test/) {
63042435880fSJoe Perches					my $val = $1;
63052435880fSJoe Perches					$val = $6 if ($skip_args ne "");
6306f90774e1SJoe Perches					if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6307f90774e1SJoe Perches					    ($val =~ /^$Octal$/ && length($val) ne 4)) {
63082435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6309459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6310f90774e1SJoe Perches					}
6311f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6312c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6313459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
63142435880fSJoe Perches					}
6315459cf0aeSJoe Perches				}
6316459cf0aeSJoe Perches			}
6317459cf0aeSJoe Perches		}
6318459cf0aeSJoe Perches
6319459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6320459cf0aeSJoe Perches		if ($line =~ /\b$mode_perms_string_search\b/) {
6321459cf0aeSJoe Perches			my $val = "";
6322459cf0aeSJoe Perches			my $oval = "";
6323f90774e1SJoe Perches			my $to = 0;
6324459cf0aeSJoe Perches			my $curpos = 0;
6325459cf0aeSJoe Perches			my $lastpos = 0;
6326459cf0aeSJoe Perches			while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6327459cf0aeSJoe Perches				$curpos = pos($line);
6328459cf0aeSJoe Perches				my $match = $2;
6329459cf0aeSJoe Perches				my $omatch = $1;
6330459cf0aeSJoe Perches				last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6331459cf0aeSJoe Perches				$lastpos = $curpos;
6332459cf0aeSJoe Perches				$to |= $mode_permission_string_types{$match};
6333459cf0aeSJoe Perches				$val .= '\s*\|\s*' if ($val ne "");
6334459cf0aeSJoe Perches				$val .= $match;
6335459cf0aeSJoe Perches				$oval .= $omatch;
6336f90774e1SJoe Perches			}
6337459cf0aeSJoe Perches			$oval =~ s/^\s*\|\s*//;
6338459cf0aeSJoe Perches			$oval =~ s/\s*\|\s*$//;
6339459cf0aeSJoe Perches			my $octal = sprintf("%04o", $to);
6340f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6341459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6342f90774e1SJoe Perches			    $fix) {
6343459cf0aeSJoe Perches				$fixed[$fixlinenr] =~ s/$val/$octal/;
63442435880fSJoe Perches			}
634513214adfSAndy Whitcroft		}
63465a6d20ceSBjorn Andersson
63475a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
63485a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
63495a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
63505a6d20ceSBjorn Andersson			my $valid_licenses = qr{
63515a6d20ceSBjorn Andersson						GPL|
63525a6d20ceSBjorn Andersson						GPL\ v2|
63535a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
63545a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
63555a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
63565a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
63575a6d20ceSBjorn Andersson						Proprietary
63585a6d20ceSBjorn Andersson					}x;
63595a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
63605a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
63615a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
63625a6d20ceSBjorn Andersson			}
63635a6d20ceSBjorn Andersson		}
6364515a235eSJoe Perches	}
636513214adfSAndy Whitcroft
636613214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
636713214adfSAndy Whitcroft	# so just keep quiet.
636813214adfSAndy Whitcroft	if ($#rawlines == -1) {
636913214adfSAndy Whitcroft		exit(0);
63700a920b5bSAndy Whitcroft	}
63710a920b5bSAndy Whitcroft
63728905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
63738905a67cSAndy Whitcroft	# things that appear to be patches.
63748905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
63758905a67cSAndy Whitcroft		exit(0);
63768905a67cSAndy Whitcroft	}
63778905a67cSAndy Whitcroft
63788905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
63798905a67cSAndy Whitcroft	# just keep quiet.
63808905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
63818905a67cSAndy Whitcroft		exit(0);
63828905a67cSAndy Whitcroft	}
63838905a67cSAndy Whitcroft
6384a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6385000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6386000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
63870a920b5bSAndy Whitcroft	}
6388ed43c4e5SAllen Hubbe	if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6389000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
6390000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
63910a920b5bSAndy Whitcroft	}
63920a920b5bSAndy Whitcroft
6393f0a594c1SAndy Whitcroft	print report_dump();
639413214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
639513214adfSAndy Whitcroft		print "$filename " if ($summary_file);
63966c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
63976c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
63986c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
63996c72ffaaSAndy Whitcroft	}
64008905a67cSAndy Whitcroft
6401d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6402ef212196SJoe Perches		# If there were any defects found and not already fixing them
6403ef212196SJoe Perches		if (!$clean and !$fix) {
6404ef212196SJoe Perches			print << "EOM"
6405ef212196SJoe Perches
6406ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6407ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6408ef212196SJoe PerchesEOM
6409ef212196SJoe Perches		}
6410d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6411d2c0a235SAndy Whitcroft		# then suggest that.
6412d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6413b0781216SMike Frysinger			$rpt_cleaners = 0;
6414d8469f16SJoe Perches			print << "EOM"
6415d8469f16SJoe Perches
6416d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6417d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6418d8469f16SJoe PerchesEOM
6419d2c0a235SAndy Whitcroft		}
6420d2c0a235SAndy Whitcroft	}
6421d2c0a235SAndy Whitcroft
6422d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6423d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6424d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
64259624b8d6SJoe Perches		my $newfile = $filename;
64269624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
64273705ce5bSJoe Perches		my $linecount = 0;
64283705ce5bSJoe Perches		my $f;
64293705ce5bSJoe Perches
6430d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6431d752fcc8SJoe Perches
64323705ce5bSJoe Perches		open($f, '>', $newfile)
64333705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
64343705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
64353705ce5bSJoe Perches			$linecount++;
64363705ce5bSJoe Perches			if ($file) {
64373705ce5bSJoe Perches				if ($linecount > 3) {
64383705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
64393705ce5bSJoe Perches					print $f $fixed_line . "\n";
64403705ce5bSJoe Perches				}
64413705ce5bSJoe Perches			} else {
64423705ce5bSJoe Perches				print $f $fixed_line . "\n";
64433705ce5bSJoe Perches			}
64443705ce5bSJoe Perches		}
64453705ce5bSJoe Perches		close($f);
64463705ce5bSJoe Perches
64473705ce5bSJoe Perches		if (!$quiet) {
64483705ce5bSJoe Perches			print << "EOM";
6449d8469f16SJoe Perches
64503705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
64513705ce5bSJoe Perches
64523705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
64533705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
64543705ce5bSJoe Perches
64553705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
64563705ce5bSJoe PerchesNo warranties, expressed or implied...
64573705ce5bSJoe PerchesEOM
64583705ce5bSJoe Perches		}
64593705ce5bSJoe Perches	}
64603705ce5bSJoe Perches
6461d8469f16SJoe Perches	if ($quiet == 0) {
6462d8469f16SJoe Perches		print "\n";
6463d8469f16SJoe Perches		if ($clean == 1) {
6464d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6465d8469f16SJoe Perches		} else {
6466d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
64670a920b5bSAndy Whitcroft		}
64680a920b5bSAndy Whitcroft	}
64690a920b5bSAndy Whitcroft	return $clean;
64700a920b5bSAndy Whitcroft}
6471