xref: /linux-6.15/scripts/checkpatch.pl (revision 596ed45b)
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 = ();
1483beb42ecSJoe Perches	for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
1493beb42ecSJoe Perches		push (@types, $_);
1503beb42ecSJoe Perches	}
1513beb42ecSJoe Perches	@types = sort(uniq(@types));
1523beb42ecSJoe Perches	print("#\tMessage type\n\n");
1533beb42ecSJoe Perches	foreach my $type (@types) {
1543beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1553beb42ecSJoe Perches	}
1563beb42ecSJoe Perches
1573beb42ecSJoe Perches	exit($exitcode);
1583beb42ecSJoe Perches}
1593beb42ecSJoe Perches
160000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
161000d1cc1SJoe Perchesif (-f $conf) {
162000d1cc1SJoe Perches	my @conf_args;
163000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
164000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
165000d1cc1SJoe Perches
166000d1cc1SJoe Perches	while (<$conffile>) {
167000d1cc1SJoe Perches		my $line = $_;
168000d1cc1SJoe Perches
169000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
170000d1cc1SJoe Perches		$line =~ s/^\s*//g;
171000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
172000d1cc1SJoe Perches
173000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
174000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
175000d1cc1SJoe Perches
176000d1cc1SJoe Perches		my @words = split(" ", $line);
177000d1cc1SJoe Perches		foreach my $word (@words) {
178000d1cc1SJoe Perches			last if ($word =~ m/^#/);
179000d1cc1SJoe Perches			push (@conf_args, $word);
180000d1cc1SJoe Perches		}
181000d1cc1SJoe Perches	}
182000d1cc1SJoe Perches	close($conffile);
183000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
184000d1cc1SJoe Perches}
185000d1cc1SJoe Perches
186737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
187737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
188737c0767SJohn Brooksforeach (@ARGV) {
189737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
190737c0767SJohn Brooks		$_ = "--color=$color";
191737c0767SJohn Brooks	}
192737c0767SJohn Brooks}
193737c0767SJohn Brooks
1940a920b5bSAndy WhitcroftGetOptions(
1956c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1960a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1970a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1980a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1996c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2008905a67cSAndy Whitcroft	'terse!'	=> \$terse,
20134d8815fSJoe Perches	'showfile!'	=> \$showfile,
20277f5b10aSHannes Eder	'f|file!'	=> \$file,
2034a593c34SDu, Changbin	'g|git!'	=> \$git,
2046c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2056c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
206000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
20791bfe484SJoe Perches	'types=s'	=> \@use,
208000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2093beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2106cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
21156193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
2126c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2138905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2148905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
21513214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2163705ce5bSJoe Perches	'fix!'		=> \$fix,
2179624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
218d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
219c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
220773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
221ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
222ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
22375ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
224737c0767SJohn Brooks	'color=s'	=> \$color,
225737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
226737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
22777f5b10aSHannes Eder	'h|help'	=> \$help,
22877f5b10aSHannes Eder	'version'	=> \$help
22977f5b10aSHannes Eder) or help(1);
23077f5b10aSHannes Eder
23177f5b10aSHannes Ederhelp(0) if ($help);
2320a920b5bSAndy Whitcroft
2333beb42ecSJoe Percheslist_types(0) if ($list_types);
2343beb42ecSJoe Perches
2359624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2362ac73b4fSJoe Perches$check_orig = $check;
2379624b8d6SJoe Perches
2380a920b5bSAndy Whitcroftmy $exit = 0;
2390a920b5bSAndy Whitcroft
240d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
241d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
242d62a201fSDave Hansen	if (!$ignore_perl_version) {
243d62a201fSDave Hansen		exit(1);
244d62a201fSDave Hansen	}
245d62a201fSDave Hansen}
246d62a201fSDave Hansen
24745107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2480a920b5bSAndy Whitcroftif ($#ARGV < 0) {
24945107ff6SAllen Hubbe	push(@ARGV, '-');
2500a920b5bSAndy Whitcroft}
2510a920b5bSAndy Whitcroft
252737c0767SJohn Brooksif ($color =~ /^[01]$/) {
253737c0767SJohn Brooks	$color = !$color;
254737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
255737c0767SJohn Brooks	$color = 1;
256737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
257737c0767SJohn Brooks	$color = 0;
258737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
259737c0767SJohn Brooks	$color = (-t STDOUT);
260737c0767SJohn Brooks} else {
261737c0767SJohn Brooks	die "Invalid color mode: $color\n";
262737c0767SJohn Brooks}
263737c0767SJohn Brooks
26491bfe484SJoe Perchessub hash_save_array_words {
26591bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
26691bfe484SJoe Perches
26791bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
26891bfe484SJoe Perches	foreach my $word (@array) {
269000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
270000d1cc1SJoe Perches		$word =~ s/^\s*//g;
271000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
272000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
273000d1cc1SJoe Perches
274000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
275000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
276000d1cc1SJoe Perches
27791bfe484SJoe Perches		$hashRef->{$word}++;
278000d1cc1SJoe Perches	}
27991bfe484SJoe Perches}
28091bfe484SJoe Perches
28191bfe484SJoe Perchessub hash_show_words {
28291bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
28391bfe484SJoe Perches
2843c816e49SJoe Perches	if (keys %$hashRef) {
285d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
28658cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
28791bfe484SJoe Perches			print " $word";
28891bfe484SJoe Perches		}
289d8469f16SJoe Perches		print "\n";
29091bfe484SJoe Perches	}
29191bfe484SJoe Perches}
29291bfe484SJoe Perches
29391bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
29491bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
295000d1cc1SJoe Perches
296c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
297c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2987429c690SAndy Whitcroftmy $dbg_type = 0;
299a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
300c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
30121caa13cSAndy Whitcroft	## no critic
30221caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
30321caa13cSAndy Whitcroft	die "$@" if ($@);
304c2fdda0dSAndy Whitcroft}
305c2fdda0dSAndy Whitcroft
306d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
307d2c0a235SAndy Whitcroft
3088905a67cSAndy Whitcroftif ($terse) {
3098905a67cSAndy Whitcroft	$emacs = 1;
3108905a67cSAndy Whitcroft	$quiet++;
3118905a67cSAndy Whitcroft}
3128905a67cSAndy Whitcroft
3136c72ffaaSAndy Whitcroftif ($tree) {
3146c72ffaaSAndy Whitcroft	if (defined $root) {
3156c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3166c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3176c72ffaaSAndy Whitcroft		}
3186c72ffaaSAndy Whitcroft	} else {
3196c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3206c72ffaaSAndy Whitcroft			$root = '.';
3216c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3226c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3236c72ffaaSAndy Whitcroft			$root = $1;
3246c72ffaaSAndy Whitcroft		}
3256c72ffaaSAndy Whitcroft	}
3266c72ffaaSAndy Whitcroft
3276c72ffaaSAndy Whitcroft	if (!defined $root) {
3280a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3290a920b5bSAndy Whitcroft		exit(2);
3300a920b5bSAndy Whitcroft	}
3316c72ffaaSAndy Whitcroft}
3326c72ffaaSAndy Whitcroft
3336c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3346c72ffaaSAndy Whitcroft
3352ceb532bSAndy Whitcroftour $Ident	= qr{
3362ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3372ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3382ceb532bSAndy Whitcroft		}x;
3396c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3406c72ffaaSAndy Whitcroftour $Sparse	= qr{
3416c72ffaaSAndy Whitcroft			__user|
3426c72ffaaSAndy Whitcroft			__kernel|
3436c72ffaaSAndy Whitcroft			__force|
3446c72ffaaSAndy Whitcroft			__iomem|
3456c72ffaaSAndy Whitcroft			__must_check|
3466c72ffaaSAndy Whitcroft			__init_refok|
347417495edSAndy Whitcroft			__kprobes|
348165e72a6SSven Eckelmann			__ref|
349ad315455SBoqun Feng			__rcu|
350ad315455SBoqun Feng			__private
3516c72ffaaSAndy Whitcroft		}x;
352e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
353e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
354e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
355e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
356e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3578716de38SJoe Perches
35852131292SWolfram Sang# Notes to $Attribute:
35952131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3606c72ffaaSAndy Whitcroftour $Attribute	= qr{
3616c72ffaaSAndy Whitcroft			const|
36203f1df7dSJoe Perches			__percpu|
36303f1df7dSJoe Perches			__nocast|
36403f1df7dSJoe Perches			__safe|
36546d832f5SMichael S. Tsirkin			__bitwise|
36603f1df7dSJoe Perches			__packed__|
36703f1df7dSJoe Perches			__packed2__|
36803f1df7dSJoe Perches			__naked|
36903f1df7dSJoe Perches			__maybe_unused|
37003f1df7dSJoe Perches			__always_unused|
37103f1df7dSJoe Perches			__noreturn|
37203f1df7dSJoe Perches			__used|
37303f1df7dSJoe Perches			__cold|
374e23ef1f3SJoe Perches			__pure|
37503f1df7dSJoe Perches			__noclone|
37603f1df7dSJoe Perches			__deprecated|
3776c72ffaaSAndy Whitcroft			__read_mostly|
3786c72ffaaSAndy Whitcroft			__kprobes|
3798716de38SJoe Perches			$InitAttribute|
38024e1d81aSAndy Whitcroft			____cacheline_aligned|
38124e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3825fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3835fe3af11SAndy Whitcroft			__weak
3846c72ffaaSAndy Whitcroft		  }x;
385c45dcabdSAndy Whitcroftour $Modifier;
38691cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3876c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3886c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3896c72ffaaSAndy Whitcroft
39095e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
39195e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
39295e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
39395e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3942435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
395c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
396326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
397326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
398326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
39974349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4002435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
401326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
402447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
40323f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4046c72ffaaSAndy Whitcroftour $Operators	= qr{
4056c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4066c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
40723f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4086c72ffaaSAndy Whitcroft		  }x;
4096c72ffaaSAndy Whitcroft
41091cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
41191cb5195SJoe Perches
412ab7e23f3SJoe Perchesour $BasicType;
4138905a67cSAndy Whitcroftour $NonptrType;
4141813087dSJoe Perchesour $NonptrTypeMisordered;
4158716de38SJoe Perchesour $NonptrTypeWithAttr;
4168905a67cSAndy Whitcroftour $Type;
4171813087dSJoe Perchesour $TypeMisordered;
4188905a67cSAndy Whitcroftour $Declare;
4191813087dSJoe Perchesour $DeclareMisordered;
4208905a67cSAndy Whitcroft
42115662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
42215662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
423171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
424171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
425171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
426171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
427171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
428171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
429171ae1a4SAndy Whitcroft}x;
430171ae1a4SAndy Whitcroft
43115662b3eSJoe Perchesour $UTF8	= qr{
43215662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
43315662b3eSJoe Perches	| $NON_ASCII_UTF8
43415662b3eSJoe Perches}x;
43515662b3eSJoe Perches
436e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
437021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
438021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
439021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
440021158b4SJoe Perches)};
441e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
442fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4438ed22cadSAndy Whitcroft	atomic_t
4448ed22cadSAndy Whitcroft)};
445e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
446e6176fa4SJoe Perches	$typeC99Typedefs\b|
447e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
448e6176fa4SJoe Perches	$typeKernelTypedefs\b
449e6176fa4SJoe Perches)};
4508ed22cadSAndy Whitcroft
4516d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4526d32f7a3SJoe Perches
453691e669bSJoe Perchesour $logFunctions = qr{(?x:
454758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4557d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
4566e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
457b0531722SJoe Perches	panic|
45806668727SJoe Perches	MODULE_[A-Z_]+|
45906668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
460691e669bSJoe Perches)};
461691e669bSJoe Perches
46220112475SJoe Perchesour $signature_tags = qr{(?xi:
46320112475SJoe Perches	Signed-off-by:|
46420112475SJoe Perches	Acked-by:|
46520112475SJoe Perches	Tested-by:|
46620112475SJoe Perches	Reviewed-by:|
46720112475SJoe Perches	Reported-by:|
4688543ae12SMugunthan V N	Suggested-by:|
46920112475SJoe Perches	To:|
47020112475SJoe Perches	Cc:
47120112475SJoe Perches)};
47220112475SJoe Perches
4731813087dSJoe Perchesour @typeListMisordered = (
4741813087dSJoe Perches	qr{char\s+(?:un)?signed},
4751813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4761813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4771813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4781813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4791813087dSJoe Perches	qr{short\s+(?:un)?signed},
4801813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4811813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4821813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4831813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4841813087dSJoe Perches	qr{int\s+(?:un)?signed},
4851813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4861813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4871813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4881813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4891813087dSJoe Perches	qr{long\s+(?:un)?signed},
4901813087dSJoe Perches);
4911813087dSJoe Perches
4928905a67cSAndy Whitcroftour @typeList = (
4938905a67cSAndy Whitcroft	qr{void},
4940c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4950c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4960c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4970c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4980c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4990c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5000c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5010c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5020c773d9dSJoe Perches	qr{(?:un)?signed},
5038905a67cSAndy Whitcroft	qr{float},
5048905a67cSAndy Whitcroft	qr{double},
5058905a67cSAndy Whitcroft	qr{bool},
5068905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5078905a67cSAndy Whitcroft	qr{union\s+$Ident},
5088905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5098905a67cSAndy Whitcroft	qr{${Ident}_t},
5108905a67cSAndy Whitcroft	qr{${Ident}_handler},
5118905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5121813087dSJoe Perches	@typeListMisordered,
5138905a67cSAndy Whitcroft);
514938224b5SJoe Perches
515938224b5SJoe Perchesour $C90_int_types = qr{(?x:
516938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
517938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
518938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
519938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
520938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
521938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
522938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
523938224b5SJoe Perches
524938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
525938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
526938224b5SJoe Perches	long\s+(?:un)?signed|
527938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
528938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
529938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
530938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
531938224b5SJoe Perches
532938224b5SJoe Perches	int\s+(?:un)?signed|
533938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
534938224b5SJoe Perches)};
535938224b5SJoe Perches
536485ff23eSAlex Dowadour @typeListFile = ();
5378716de38SJoe Perchesour @typeListWithAttr = (
5388716de38SJoe Perches	@typeList,
5398716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5408716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5418716de38SJoe Perches);
5428716de38SJoe Perches
543c45dcabdSAndy Whitcroftour @modifierList = (
544c45dcabdSAndy Whitcroft	qr{fastcall},
545c45dcabdSAndy Whitcroft);
546485ff23eSAlex Dowadour @modifierListFile = ();
5478905a67cSAndy Whitcroft
5482435880fSJoe Perchesour @mode_permission_funcs = (
5492435880fSJoe Perches	["module_param", 3],
5502435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5512435880fSJoe Perches	["module_param_array_named", 5],
5522435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5532435880fSJoe Perches	["proc_create(?:_data|)", 2],
554459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
555459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
556459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
557459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
558459cf0aeSJoe Perches	["__ATTR", 2],
5592435880fSJoe Perches);
5602435880fSJoe Perches
561515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
562515a235eSJoe Perchesour $mode_perms_search = "";
563515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
564515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
565515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
566515a235eSJoe Perches}
567515a235eSJoe Perches
568b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
569b392c64fSJoe Perches	S_IWUGO		|
570b392c64fSJoe Perches	S_IWOTH		|
571b392c64fSJoe Perches	S_IRWXUGO	|
572b392c64fSJoe Perches	S_IALLUGO	|
573b392c64fSJoe Perches	0[0-7][0-7][2367]
574b392c64fSJoe Perches}x;
575b392c64fSJoe Perches
576f90774e1SJoe Perchesour %mode_permission_string_types = (
577f90774e1SJoe Perches	"S_IRWXU" => 0700,
578f90774e1SJoe Perches	"S_IRUSR" => 0400,
579f90774e1SJoe Perches	"S_IWUSR" => 0200,
580f90774e1SJoe Perches	"S_IXUSR" => 0100,
581f90774e1SJoe Perches	"S_IRWXG" => 0070,
582f90774e1SJoe Perches	"S_IRGRP" => 0040,
583f90774e1SJoe Perches	"S_IWGRP" => 0020,
584f90774e1SJoe Perches	"S_IXGRP" => 0010,
585f90774e1SJoe Perches	"S_IRWXO" => 0007,
586f90774e1SJoe Perches	"S_IROTH" => 0004,
587f90774e1SJoe Perches	"S_IWOTH" => 0002,
588f90774e1SJoe Perches	"S_IXOTH" => 0001,
589f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
590f90774e1SJoe Perches	"S_IRUGO" => 0444,
591f90774e1SJoe Perches	"S_IWUGO" => 0222,
592f90774e1SJoe Perches	"S_IXUGO" => 0111,
593f90774e1SJoe Perches);
594f90774e1SJoe Perches
595f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
596f90774e1SJoe Perchesour $mode_perms_string_search = "";
597f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
598f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
599f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
600f90774e1SJoe Perches}
601f90774e1SJoe Perches
6027840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6037840a94cSWolfram Sang	irq|
604cdcee686SSergey Ryazanov	memory|
605cdcee686SSergey Ryazanov	time|
606cdcee686SSergey Ryazanov	reboot
6077840a94cSWolfram Sang)};
6087840a94cSWolfram Sang# memory.h: ARM has a custom one
6097840a94cSWolfram Sang
61066b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
61166b47b4aSKees Cookmy $misspellings;
61266b47b4aSKees Cookmy %spelling_fix;
61336061e38SJoe Perches
61436061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
61566b47b4aSKees Cook	while (<$spelling>) {
61666b47b4aSKees Cook		my $line = $_;
61766b47b4aSKees Cook
61866b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
61966b47b4aSKees Cook		$line =~ s/^\s*//g;
62066b47b4aSKees Cook
62166b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
62266b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
62366b47b4aSKees Cook
62466b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
62566b47b4aSKees Cook
62666b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
62766b47b4aSKees Cook	}
62866b47b4aSKees Cook	close($spelling);
62936061e38SJoe Perches} else {
63036061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
63136061e38SJoe Perches}
63266b47b4aSKees Cook
633ebfd7d62SJoe Perchesif ($codespell) {
634ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
635ebfd7d62SJoe Perches		while (<$spelling>) {
636ebfd7d62SJoe Perches			my $line = $_;
637ebfd7d62SJoe Perches
638ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
639ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
640ebfd7d62SJoe Perches
641ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
642ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
643ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
644ebfd7d62SJoe Perches
645ebfd7d62SJoe Perches			$line =~ s/,.*$//;
646ebfd7d62SJoe Perches
647ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
648ebfd7d62SJoe Perches
649ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
650ebfd7d62SJoe Perches		}
651ebfd7d62SJoe Perches		close($spelling);
652ebfd7d62SJoe Perches	} else {
653ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
654ebfd7d62SJoe Perches	}
655ebfd7d62SJoe Perches}
656ebfd7d62SJoe Perches
657ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
658ebfd7d62SJoe Perches
65975ad8c57SJerome Forissiersub read_words {
66075ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
66175ad8c57SJerome Forissier
66275ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
66375ad8c57SJerome Forissier		while (<$words>) {
664bf1fa1daSJoe Perches			my $line = $_;
665bf1fa1daSJoe Perches
666bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
667bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
668bf1fa1daSJoe Perches
669bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
670bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
671bf1fa1daSJoe Perches			if ($line =~ /\s/) {
67275ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
673bf1fa1daSJoe Perches				next;
674bf1fa1daSJoe Perches			}
675bf1fa1daSJoe Perches
67675ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
67775ad8c57SJerome Forissier			$$wordsRef .= $line;
678bf1fa1daSJoe Perches		}
67975ad8c57SJerome Forissier		close($file);
68075ad8c57SJerome Forissier		return 1;
681bf1fa1daSJoe Perches	}
682bf1fa1daSJoe Perches
68375ad8c57SJerome Forissier	return 0;
68475ad8c57SJerome Forissier}
68575ad8c57SJerome Forissier
68675ad8c57SJerome Forissiermy $const_structs = "";
68775ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
68875ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
68975ad8c57SJerome Forissier
69075ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
69175ad8c57SJerome Forissierif (length($typedefsfile)) {
69275ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
69375ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
69475ad8c57SJerome Forissier}
69575ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
69675ad8c57SJerome Forissier
6978905a67cSAndy Whitcroftsub build_types {
698485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
699485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7001813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7018716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
702c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
703ab7e23f3SJoe Perches	$BasicType	= qr{
704ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
705ab7e23f3SJoe Perches				(?:${all}\b)
706ab7e23f3SJoe Perches		}x;
7078905a67cSAndy Whitcroft	$NonptrType	= qr{
708d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
709cf655043SAndy Whitcroft			(?:
7106b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7118ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
712c45dcabdSAndy Whitcroft				(?:${all}\b)
713cf655043SAndy Whitcroft			)
714c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7158905a67cSAndy Whitcroft		  }x;
7161813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7171813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7181813087dSJoe Perches			(?:
7191813087dSJoe Perches				(?:${Misordered}\b)
7201813087dSJoe Perches			)
7211813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7221813087dSJoe Perches		  }x;
7238716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7248716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7258716de38SJoe Perches			(?:
7268716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
7278716de38SJoe Perches				(?:$typeTypedefs\b)|
7288716de38SJoe Perches				(?:${allWithAttr}\b)
7298716de38SJoe Perches			)
7308716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
7318716de38SJoe Perches		  }x;
7328905a67cSAndy Whitcroft	$Type	= qr{
733c45dcabdSAndy Whitcroft			$NonptrType
7341574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
735c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
7368905a67cSAndy Whitcroft		  }x;
7371813087dSJoe Perches	$TypeMisordered	= qr{
7381813087dSJoe Perches			$NonptrTypeMisordered
7391813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
7401813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
7411813087dSJoe Perches		  }x;
74291cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
7431813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
7448905a67cSAndy Whitcroft}
7458905a67cSAndy Whitcroftbuild_types();
7466c72ffaaSAndy Whitcroft
7477d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
748d1fe9c09SJoe Perches
749d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
750d1fe9c09SJoe Perches# requires at least perl version v5.10.0
751d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
752d1fe9c09SJoe Perches
753d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
7542435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
755c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
7567d2367afSJoe Perches
757f8422308SJoe Perchesour $declaration_macros = qr{(?x:
7583e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
759fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
760f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
761f8422308SJoe Perches)};
762f8422308SJoe Perches
7637d2367afSJoe Perchessub deparenthesize {
7647d2367afSJoe Perches	my ($string) = @_;
7657d2367afSJoe Perches	return "" if (!defined($string));
7665b9553abSJoe Perches
7675b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
7685b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
7695b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
7705b9553abSJoe Perches	}
7715b9553abSJoe Perches
7727d2367afSJoe Perches	$string =~ s@\s+@ @g;
7735b9553abSJoe Perches
7747d2367afSJoe Perches	return $string;
7757d2367afSJoe Perches}
7767d2367afSJoe Perches
7773445686aSJoe Perchessub seed_camelcase_file {
7783445686aSJoe Perches	my ($file) = @_;
7793445686aSJoe Perches
7803445686aSJoe Perches	return if (!(-f $file));
7813445686aSJoe Perches
7823445686aSJoe Perches	local $/;
7833445686aSJoe Perches
7843445686aSJoe Perches	open(my $include_file, '<', "$file")
7853445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
7863445686aSJoe Perches	my $text = <$include_file>;
7873445686aSJoe Perches	close($include_file);
7883445686aSJoe Perches
7893445686aSJoe Perches	my @lines = split('\n', $text);
7903445686aSJoe Perches
7913445686aSJoe Perches	foreach my $line (@lines) {
7923445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
7933445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
7943445686aSJoe Perches			$camelcase{$1} = 1;
79511ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
79611ea516aSJoe Perches			$camelcase{$1} = 1;
79711ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
7983445686aSJoe Perches			$camelcase{$1} = 1;
7993445686aSJoe Perches		}
8003445686aSJoe Perches	}
8013445686aSJoe Perches}
8023445686aSJoe Perches
80385b0ee18SJoe Perchessub is_maintained_obsolete {
80485b0ee18SJoe Perches	my ($filename) = @_;
80585b0ee18SJoe Perches
806f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
80785b0ee18SJoe Perches
8080616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
80985b0ee18SJoe Perches
81085b0ee18SJoe Perches	return $status =~ /obsolete/i;
81185b0ee18SJoe Perches}
81285b0ee18SJoe Perches
8133445686aSJoe Perchesmy $camelcase_seeded = 0;
8143445686aSJoe Perchessub seed_camelcase_includes {
8153445686aSJoe Perches	return if ($camelcase_seeded);
8163445686aSJoe Perches
8173445686aSJoe Perches	my $files;
818c707a81dSJoe Perches	my $camelcase_cache = "";
819c707a81dSJoe Perches	my @include_files = ();
820c707a81dSJoe Perches
821c707a81dSJoe Perches	$camelcase_seeded = 1;
822351b2a1fSJoe Perches
8233645e328SRichard Genoud	if (-e ".git") {
824351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
825351b2a1fSJoe Perches		chomp $git_last_include_commit;
826c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
827c707a81dSJoe Perches	} else {
828c707a81dSJoe Perches		my $last_mod_date = 0;
829c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
830c707a81dSJoe Perches		@include_files = split('\n', $files);
831c707a81dSJoe Perches		foreach my $file (@include_files) {
832c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
833c707a81dSJoe Perches						   localtime((stat $file)[9]));
834c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
835c707a81dSJoe Perches		}
836c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
837c707a81dSJoe Perches	}
838c707a81dSJoe Perches
839c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
840c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
841c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
842351b2a1fSJoe Perches		while (<$camelcase_file>) {
843351b2a1fSJoe Perches			chomp;
844351b2a1fSJoe Perches			$camelcase{$_} = 1;
845351b2a1fSJoe Perches		}
846351b2a1fSJoe Perches		close($camelcase_file);
847351b2a1fSJoe Perches
848351b2a1fSJoe Perches		return;
849351b2a1fSJoe Perches	}
850c707a81dSJoe Perches
8513645e328SRichard Genoud	if (-e ".git") {
852c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
853c707a81dSJoe Perches		@include_files = split('\n', $files);
8543445686aSJoe Perches	}
855c707a81dSJoe Perches
8563445686aSJoe Perches	foreach my $file (@include_files) {
8573445686aSJoe Perches		seed_camelcase_file($file);
8583445686aSJoe Perches	}
859351b2a1fSJoe Perches
860c707a81dSJoe Perches	if ($camelcase_cache ne "") {
861351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
862c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
863c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
864351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
865351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
866351b2a1fSJoe Perches		}
867351b2a1fSJoe Perches		close($camelcase_file);
868351b2a1fSJoe Perches	}
8693445686aSJoe Perches}
8703445686aSJoe Perches
871d311cd44SJoe Perchessub git_commit_info {
872d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
873d311cd44SJoe Perches
874d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
875d311cd44SJoe Perches
876d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
877d311cd44SJoe Perches	$output =~ s/^\s*//gm;
878d311cd44SJoe Perches	my @lines = split("\n", $output);
879d311cd44SJoe Perches
8800d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
8810d7835fcSJoe Perches
882d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
883d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
884d311cd44SJoe Perches# all matching commit ids, but it's very slow...
885d311cd44SJoe Perches#
886d311cd44SJoe Perches#		echo "checking commits $1..."
887d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
888d311cd44SJoe Perches#		while read line ; do
889d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
890d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
891d311cd44SJoe Perches#		done
892d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
893948b133aSHeinrich Schuchardt		$id = undef;
894d311cd44SJoe Perches	} else {
895d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
896d311cd44SJoe Perches		$desc = substr($lines[0], 41);
897d311cd44SJoe Perches	}
898d311cd44SJoe Perches
899d311cd44SJoe Perches	return ($id, $desc);
900d311cd44SJoe Perches}
901d311cd44SJoe Perches
9026c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9030a920b5bSAndy Whitcroft
90400df344fSAndy Whitcroftmy @rawlines = ();
905c2fdda0dSAndy Whitcroftmy @lines = ();
9063705ce5bSJoe Perchesmy @fixed = ();
907d752fcc8SJoe Perchesmy @fixed_inserted = ();
908d752fcc8SJoe Perchesmy @fixed_deleted = ();
909194f66fcSJoe Perchesmy $fixlinenr = -1;
910194f66fcSJoe Perches
9114a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
9124a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
9134a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
9144a593c34SDu, Changbin
9154a593c34SDu, Changbinif ($git) {
9164a593c34SDu, Changbin	my @commits = ();
9170dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
9184a593c34SDu, Changbin		my $git_range;
91928898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
92028898fd1SJoe Perches			$git_range = "-$2 $1";
9214a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
9224a593c34SDu, Changbin			$git_range = "$commit_expr";
9234a593c34SDu, Changbin		} else {
9240dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
9250dea9f1eSJoe Perches		}
9260dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
9270dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
92828898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
92928898fd1SJoe Perches			next if (!defined($1) || !defined($2));
9300dea9f1eSJoe Perches			my $sha1 = $1;
9310dea9f1eSJoe Perches			my $subject = $2;
9320dea9f1eSJoe Perches			unshift(@commits, $sha1);
9330dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
9344a593c34SDu, Changbin		}
9354a593c34SDu, Changbin	}
9364a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
9374a593c34SDu, Changbin	@ARGV = @commits;
9384a593c34SDu, Changbin}
9394a593c34SDu, Changbin
940c2fdda0dSAndy Whitcroftmy $vname;
9416c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
94221caa13cSAndy Whitcroft	my $FILE;
9434a593c34SDu, Changbin	if ($git) {
9444a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
9454a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
9464a593c34SDu, Changbin	} elsif ($file) {
94721caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
9486c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
94921caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
95021caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
9516c72ffaaSAndy Whitcroft	} else {
95221caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
9536c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
9546c72ffaaSAndy Whitcroft	}
955c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
956c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
9574a593c34SDu, Changbin	} elsif ($git) {
9580dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
959c2fdda0dSAndy Whitcroft	} else {
960c2fdda0dSAndy Whitcroft		$vname = $filename;
961c2fdda0dSAndy Whitcroft	}
96221caa13cSAndy Whitcroft	while (<$FILE>) {
9630a920b5bSAndy Whitcroft		chomp;
96400df344fSAndy Whitcroft		push(@rawlines, $_);
9656c72ffaaSAndy Whitcroft	}
96621caa13cSAndy Whitcroft	close($FILE);
967d8469f16SJoe Perches
968d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
969d8469f16SJoe Perches		print '-' x length($vname) . "\n";
970d8469f16SJoe Perches		print "$vname\n";
971d8469f16SJoe Perches		print '-' x length($vname) . "\n";
972d8469f16SJoe Perches	}
973d8469f16SJoe Perches
974c2fdda0dSAndy Whitcroft	if (!process($filename)) {
9750a920b5bSAndy Whitcroft		$exit = 1;
9760a920b5bSAndy Whitcroft	}
97700df344fSAndy Whitcroft	@rawlines = ();
97813214adfSAndy Whitcroft	@lines = ();
9793705ce5bSJoe Perches	@fixed = ();
980d752fcc8SJoe Perches	@fixed_inserted = ();
981d752fcc8SJoe Perches	@fixed_deleted = ();
982194f66fcSJoe Perches	$fixlinenr = -1;
983485ff23eSAlex Dowad	@modifierListFile = ();
984485ff23eSAlex Dowad	@typeListFile = ();
985485ff23eSAlex Dowad	build_types();
9860a920b5bSAndy Whitcroft}
9870a920b5bSAndy Whitcroft
988d8469f16SJoe Perchesif (!$quiet) {
9893c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
9903c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
9913c816e49SJoe Perches
992d8469f16SJoe Perches	if ($^V lt 5.10.0) {
993d8469f16SJoe Perches		print << "EOM"
994d8469f16SJoe Perches
995d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
996d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
997d8469f16SJoe PerchesEOM
998d8469f16SJoe Perches	}
999d8469f16SJoe Perches	if ($exit) {
1000d8469f16SJoe Perches		print << "EOM"
1001d8469f16SJoe Perches
1002d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1003d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1004d8469f16SJoe PerchesEOM
1005d8469f16SJoe Perches	}
1006d8469f16SJoe Perches}
1007d8469f16SJoe Perches
10080a920b5bSAndy Whitcroftexit($exit);
10090a920b5bSAndy Whitcroft
10100a920b5bSAndy Whitcroftsub top_of_kernel_tree {
10116c72ffaaSAndy Whitcroft	my ($root) = @_;
10126c72ffaaSAndy Whitcroft
10136c72ffaaSAndy Whitcroft	my @tree_check = (
10146c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
10156c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
10166c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
10176c72ffaaSAndy Whitcroft	);
10186c72ffaaSAndy Whitcroft
10196c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
10206c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
10210a920b5bSAndy Whitcroft			return 0;
10220a920b5bSAndy Whitcroft		}
10236c72ffaaSAndy Whitcroft	}
10246c72ffaaSAndy Whitcroft	return 1;
10256c72ffaaSAndy Whitcroft}
10260a920b5bSAndy Whitcroft
102720112475SJoe Perchessub parse_email {
102820112475SJoe Perches	my ($formatted_email) = @_;
102920112475SJoe Perches
103020112475SJoe Perches	my $name = "";
103120112475SJoe Perches	my $address = "";
103220112475SJoe Perches	my $comment = "";
103320112475SJoe Perches
103420112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
103520112475SJoe Perches		$name = $1;
103620112475SJoe Perches		$address = $2;
103720112475SJoe Perches		$comment = $3 if defined $3;
103820112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
103920112475SJoe Perches		$address = $1;
104020112475SJoe Perches		$comment = $2 if defined $2;
104120112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
104220112475SJoe Perches		$address = $1;
104320112475SJoe Perches		$comment = $2 if defined $2;
104420112475SJoe Perches		$formatted_email =~ s/$address.*$//;
104520112475SJoe Perches		$name = $formatted_email;
10463705ce5bSJoe Perches		$name = trim($name);
104720112475SJoe Perches		$name =~ s/^\"|\"$//g;
104820112475SJoe Perches		# If there's a name left after stripping spaces and
104920112475SJoe Perches		# leading quotes, and the address doesn't have both
105020112475SJoe Perches		# leading and trailing angle brackets, the address
105120112475SJoe Perches		# is invalid. ie:
105220112475SJoe Perches		#   "joe smith [email protected]" bad
105320112475SJoe Perches		#   "joe smith <[email protected]" bad
105420112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
105520112475SJoe Perches			$name = "";
105620112475SJoe Perches			$address = "";
105720112475SJoe Perches			$comment = "";
105820112475SJoe Perches		}
105920112475SJoe Perches	}
106020112475SJoe Perches
10613705ce5bSJoe Perches	$name = trim($name);
106220112475SJoe Perches	$name =~ s/^\"|\"$//g;
10633705ce5bSJoe Perches	$address = trim($address);
106420112475SJoe Perches	$address =~ s/^\<|\>$//g;
106520112475SJoe Perches
106620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
106720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
106820112475SJoe Perches		$name = "\"$name\"";
106920112475SJoe Perches	}
107020112475SJoe Perches
107120112475SJoe Perches	return ($name, $address, $comment);
107220112475SJoe Perches}
107320112475SJoe Perches
107420112475SJoe Perchessub format_email {
107520112475SJoe Perches	my ($name, $address) = @_;
107620112475SJoe Perches
107720112475SJoe Perches	my $formatted_email;
107820112475SJoe Perches
10793705ce5bSJoe Perches	$name = trim($name);
108020112475SJoe Perches	$name =~ s/^\"|\"$//g;
10813705ce5bSJoe Perches	$address = trim($address);
108220112475SJoe Perches
108320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
108420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
108520112475SJoe Perches		$name = "\"$name\"";
108620112475SJoe Perches	}
108720112475SJoe Perches
108820112475SJoe Perches	if ("$name" eq "") {
108920112475SJoe Perches		$formatted_email = "$address";
109020112475SJoe Perches	} else {
109120112475SJoe Perches		$formatted_email = "$name <$address>";
109220112475SJoe Perches	}
109320112475SJoe Perches
109420112475SJoe Perches	return $formatted_email;
109520112475SJoe Perches}
109620112475SJoe Perches
1097d311cd44SJoe Perchessub which {
1098d311cd44SJoe Perches	my ($bin) = @_;
1099d311cd44SJoe Perches
1100d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1101d311cd44SJoe Perches		if (-e "$path/$bin") {
1102d311cd44SJoe Perches			return "$path/$bin";
1103d311cd44SJoe Perches		}
1104d311cd44SJoe Perches	}
1105d311cd44SJoe Perches
1106d311cd44SJoe Perches	return "";
1107d311cd44SJoe Perches}
1108d311cd44SJoe Perches
1109000d1cc1SJoe Perchessub which_conf {
1110000d1cc1SJoe Perches	my ($conf) = @_;
1111000d1cc1SJoe Perches
1112000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1113000d1cc1SJoe Perches		if (-e "$path/$conf") {
1114000d1cc1SJoe Perches			return "$path/$conf";
1115000d1cc1SJoe Perches		}
1116000d1cc1SJoe Perches	}
1117000d1cc1SJoe Perches
1118000d1cc1SJoe Perches	return "";
1119000d1cc1SJoe Perches}
1120000d1cc1SJoe Perches
11210a920b5bSAndy Whitcroftsub expand_tabs {
11220a920b5bSAndy Whitcroft	my ($str) = @_;
11230a920b5bSAndy Whitcroft
11240a920b5bSAndy Whitcroft	my $res = '';
11250a920b5bSAndy Whitcroft	my $n = 0;
11260a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
11270a920b5bSAndy Whitcroft		if ($c eq "\t") {
11280a920b5bSAndy Whitcroft			$res .= ' ';
11290a920b5bSAndy Whitcroft			$n++;
11300a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
11310a920b5bSAndy Whitcroft				$res .= ' ';
11320a920b5bSAndy Whitcroft			}
11330a920b5bSAndy Whitcroft			next;
11340a920b5bSAndy Whitcroft		}
11350a920b5bSAndy Whitcroft		$res .= $c;
11360a920b5bSAndy Whitcroft		$n++;
11370a920b5bSAndy Whitcroft	}
11380a920b5bSAndy Whitcroft
11390a920b5bSAndy Whitcroft	return $res;
11400a920b5bSAndy Whitcroft}
11416c72ffaaSAndy Whitcroftsub copy_spacing {
1142773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
11436c72ffaaSAndy Whitcroft	return $res;
11446c72ffaaSAndy Whitcroft}
11450a920b5bSAndy Whitcroft
11464a0df2efSAndy Whitcroftsub line_stats {
11474a0df2efSAndy Whitcroft	my ($line) = @_;
11484a0df2efSAndy Whitcroft
11494a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
11504a0df2efSAndy Whitcroft	$line =~ s/^.//;
11514a0df2efSAndy Whitcroft	$line = expand_tabs($line);
11524a0df2efSAndy Whitcroft
11534a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
11544a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
11554a0df2efSAndy Whitcroft
11564a0df2efSAndy Whitcroft	return (length($line), length($white));
11574a0df2efSAndy Whitcroft}
11584a0df2efSAndy Whitcroft
1159773647a0SAndy Whitcroftmy $sanitise_quote = '';
1160773647a0SAndy Whitcroft
1161773647a0SAndy Whitcroftsub sanitise_line_reset {
1162773647a0SAndy Whitcroft	my ($in_comment) = @_;
1163773647a0SAndy Whitcroft
1164773647a0SAndy Whitcroft	if ($in_comment) {
1165773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1166773647a0SAndy Whitcroft	} else {
1167773647a0SAndy Whitcroft		$sanitise_quote = '';
1168773647a0SAndy Whitcroft	}
1169773647a0SAndy Whitcroft}
117000df344fSAndy Whitcroftsub sanitise_line {
117100df344fSAndy Whitcroft	my ($line) = @_;
117200df344fSAndy Whitcroft
117300df344fSAndy Whitcroft	my $res = '';
117400df344fSAndy Whitcroft	my $l = '';
117500df344fSAndy Whitcroft
1176c2fdda0dSAndy Whitcroft	my $qlen = 0;
1177773647a0SAndy Whitcroft	my $off = 0;
1178773647a0SAndy Whitcroft	my $c;
117900df344fSAndy Whitcroft
1180773647a0SAndy Whitcroft	# Always copy over the diff marker.
1181773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1182773647a0SAndy Whitcroft
1183773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1184773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1185773647a0SAndy Whitcroft
1186773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
1187773647a0SAndy Whitcroft		# and end, all to $;.
1188773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1189773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1190773647a0SAndy Whitcroft
1191773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1192773647a0SAndy Whitcroft			$off++;
119300df344fSAndy Whitcroft			next;
1194773647a0SAndy Whitcroft		}
119581bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1196773647a0SAndy Whitcroft			$sanitise_quote = '';
1197773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1198773647a0SAndy Whitcroft			$off++;
1199773647a0SAndy Whitcroft			next;
1200773647a0SAndy Whitcroft		}
1201113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1202113f04a8SDaniel Walker			$sanitise_quote = '//';
1203113f04a8SDaniel Walker
1204113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1205113f04a8SDaniel Walker			$off++;
1206113f04a8SDaniel Walker			next;
1207113f04a8SDaniel Walker		}
1208773647a0SAndy Whitcroft
1209773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1210773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1211773647a0SAndy Whitcroft		    $c eq "\\") {
1212773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1213773647a0SAndy Whitcroft			$off++;
1214773647a0SAndy Whitcroft			next;
1215773647a0SAndy Whitcroft		}
1216773647a0SAndy Whitcroft		# Regular quotes.
1217773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1218773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1219773647a0SAndy Whitcroft				$sanitise_quote = $c;
1220773647a0SAndy Whitcroft
1221773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1222773647a0SAndy Whitcroft				next;
1223773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1224773647a0SAndy Whitcroft				$sanitise_quote = '';
122500df344fSAndy Whitcroft			}
122600df344fSAndy Whitcroft		}
1227773647a0SAndy Whitcroft
1228fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1229773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1230773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1231113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1232113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1233773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1234773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
123500df344fSAndy Whitcroft		} else {
1236773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
123700df344fSAndy Whitcroft		}
1238c2fdda0dSAndy Whitcroft	}
1239c2fdda0dSAndy Whitcroft
1240113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1241113f04a8SDaniel Walker		$sanitise_quote = '';
1242113f04a8SDaniel Walker	}
1243113f04a8SDaniel Walker
1244c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1245c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1246c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1247c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1248c2fdda0dSAndy Whitcroft
1249c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1250c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1251c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1252c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1253c2fdda0dSAndy Whitcroft	}
1254c2fdda0dSAndy Whitcroft
1255dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1256dadf680dSJoe Perches		my $match = $1;
1257dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1258dadf680dSJoe Perches	}
1259dadf680dSJoe Perches
126000df344fSAndy Whitcroft	return $res;
126100df344fSAndy Whitcroft}
126200df344fSAndy Whitcroft
1263a6962d72SJoe Perchessub get_quoted_string {
1264a6962d72SJoe Perches	my ($line, $rawline) = @_;
1265a6962d72SJoe Perches
126633acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1267a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1268a6962d72SJoe Perches}
1269a6962d72SJoe Perches
12708905a67cSAndy Whitcroftsub ctx_statement_block {
12718905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12728905a67cSAndy Whitcroft	my $line = $linenr - 1;
12738905a67cSAndy Whitcroft	my $blk = '';
12748905a67cSAndy Whitcroft	my $soff = $off;
12758905a67cSAndy Whitcroft	my $coff = $off - 1;
1276773647a0SAndy Whitcroft	my $coff_set = 0;
12778905a67cSAndy Whitcroft
127813214adfSAndy Whitcroft	my $loff = 0;
127913214adfSAndy Whitcroft
12808905a67cSAndy Whitcroft	my $type = '';
12818905a67cSAndy Whitcroft	my $level = 0;
1282a2750645SAndy Whitcroft	my @stack = ();
1283cf655043SAndy Whitcroft	my $p;
12848905a67cSAndy Whitcroft	my $c;
12858905a67cSAndy Whitcroft	my $len = 0;
128613214adfSAndy Whitcroft
128713214adfSAndy Whitcroft	my $remainder;
12888905a67cSAndy Whitcroft	while (1) {
1289a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1290a2750645SAndy Whitcroft
1291773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
12928905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
12938905a67cSAndy Whitcroft		# context.
12948905a67cSAndy Whitcroft		if ($off >= $len) {
12958905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1296dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1297c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
12988905a67cSAndy Whitcroft				$remain--;
129913214adfSAndy Whitcroft				$loff = $len;
1300c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13018905a67cSAndy Whitcroft				$len = length($blk);
13028905a67cSAndy Whitcroft				$line++;
13038905a67cSAndy Whitcroft				last;
13048905a67cSAndy Whitcroft			}
13058905a67cSAndy Whitcroft			# Bail if there is no further context.
13068905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
130713214adfSAndy Whitcroft			if ($off >= $len) {
13088905a67cSAndy Whitcroft				last;
13098905a67cSAndy Whitcroft			}
1310f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1311f74bd194SAndy Whitcroft				$level++;
1312f74bd194SAndy Whitcroft				$type = '#';
1313f74bd194SAndy Whitcroft			}
13148905a67cSAndy Whitcroft		}
1315cf655043SAndy Whitcroft		$p = $c;
13168905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
131713214adfSAndy Whitcroft		$remainder = substr($blk, $off);
13188905a67cSAndy Whitcroft
1319773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
13204635f4fbSAndy Whitcroft
13214635f4fbSAndy Whitcroft		# Handle nested #if/#else.
13224635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
13234635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
13244635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
13254635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
13264635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
13274635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
13284635f4fbSAndy Whitcroft		}
13294635f4fbSAndy Whitcroft
13308905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
13318905a67cSAndy Whitcroft		# outermost level.
13328905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
13338905a67cSAndy Whitcroft			last;
13348905a67cSAndy Whitcroft		}
13358905a67cSAndy Whitcroft
133613214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1337773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1338773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1339773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1340773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1341773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1342773647a0SAndy Whitcroft			$coff_set = 1;
1343773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1344773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
134513214adfSAndy Whitcroft		}
134613214adfSAndy Whitcroft
13478905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
13488905a67cSAndy Whitcroft			$level++;
13498905a67cSAndy Whitcroft			$type = '(';
13508905a67cSAndy Whitcroft		}
13518905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
13528905a67cSAndy Whitcroft			$level--;
13538905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
13548905a67cSAndy Whitcroft
13558905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
13568905a67cSAndy Whitcroft				$coff = $off;
1357773647a0SAndy Whitcroft				$coff_set = 1;
1358773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
13598905a67cSAndy Whitcroft			}
13608905a67cSAndy Whitcroft		}
13618905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
13628905a67cSAndy Whitcroft			$level++;
13638905a67cSAndy Whitcroft			$type = '{';
13648905a67cSAndy Whitcroft		}
13658905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
13668905a67cSAndy Whitcroft			$level--;
13678905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
13688905a67cSAndy Whitcroft
13698905a67cSAndy Whitcroft			if ($level == 0) {
1370b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1371b998e001SPatrick Pannuto					$off++;
1372b998e001SPatrick Pannuto				}
13738905a67cSAndy Whitcroft				last;
13748905a67cSAndy Whitcroft			}
13758905a67cSAndy Whitcroft		}
1376f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1377f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1378f74bd194SAndy Whitcroft			$level--;
1379f74bd194SAndy Whitcroft			$type = '';
1380f74bd194SAndy Whitcroft			$off++;
1381f74bd194SAndy Whitcroft			last;
1382f74bd194SAndy Whitcroft		}
13838905a67cSAndy Whitcroft		$off++;
13848905a67cSAndy Whitcroft	}
1385a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
138613214adfSAndy Whitcroft	if ($off == $len) {
1387a3bb97a7SAndy Whitcroft		$loff = $len + 1;
138813214adfSAndy Whitcroft		$line++;
138913214adfSAndy Whitcroft		$remain--;
139013214adfSAndy Whitcroft	}
13918905a67cSAndy Whitcroft
13928905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
13938905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
13948905a67cSAndy Whitcroft
13958905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
13968905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
13978905a67cSAndy Whitcroft
1398773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
139913214adfSAndy Whitcroft
140013214adfSAndy Whitcroft	return ($statement, $condition,
140113214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
140213214adfSAndy Whitcroft}
140313214adfSAndy Whitcroft
1404cf655043SAndy Whitcroftsub statement_lines {
1405cf655043SAndy Whitcroft	my ($stmt) = @_;
1406cf655043SAndy Whitcroft
1407cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1408cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1409cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1410cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1411cf655043SAndy Whitcroft
1412cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1413cf655043SAndy Whitcroft
1414cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1415cf655043SAndy Whitcroft}
1416cf655043SAndy Whitcroft
1417cf655043SAndy Whitcroftsub statement_rawlines {
1418cf655043SAndy Whitcroft	my ($stmt) = @_;
1419cf655043SAndy Whitcroft
1420cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1421cf655043SAndy Whitcroft
1422cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1423cf655043SAndy Whitcroft}
1424cf655043SAndy Whitcroft
1425cf655043SAndy Whitcroftsub statement_block_size {
1426cf655043SAndy Whitcroft	my ($stmt) = @_;
1427cf655043SAndy Whitcroft
1428cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1429cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1430cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1431cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1432cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1433cf655043SAndy Whitcroft
1434cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1435cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1436cf655043SAndy Whitcroft
1437cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1438cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1439cf655043SAndy Whitcroft
1440cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1441cf655043SAndy Whitcroft		return $stmt_lines;
1442cf655043SAndy Whitcroft	} else {
1443cf655043SAndy Whitcroft		return $stmt_statements;
1444cf655043SAndy Whitcroft	}
1445cf655043SAndy Whitcroft}
1446cf655043SAndy Whitcroft
144713214adfSAndy Whitcroftsub ctx_statement_full {
144813214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
144913214adfSAndy Whitcroft	my ($statement, $condition, $level);
145013214adfSAndy Whitcroft
145113214adfSAndy Whitcroft	my (@chunks);
145213214adfSAndy Whitcroft
1453cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
145413214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
145513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1456773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
145713214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1458cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1459cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1460cf655043SAndy Whitcroft	}
1461cf655043SAndy Whitcroft
1462cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1463cf655043SAndy Whitcroft	# could continue the statement.
1464cf655043SAndy Whitcroft	for (;;) {
146513214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
146613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1467cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1468773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1469cf655043SAndy Whitcroft		#print "C: push\n";
1470cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
147113214adfSAndy Whitcroft	}
147213214adfSAndy Whitcroft
147313214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
14748905a67cSAndy Whitcroft}
14758905a67cSAndy Whitcroft
14764a0df2efSAndy Whitcroftsub ctx_block_get {
1477f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
14784a0df2efSAndy Whitcroft	my $line;
14794a0df2efSAndy Whitcroft	my $start = $linenr - 1;
14804a0df2efSAndy Whitcroft	my $blk = '';
14814a0df2efSAndy Whitcroft	my @o;
14824a0df2efSAndy Whitcroft	my @c;
14834a0df2efSAndy Whitcroft	my @res = ();
14844a0df2efSAndy Whitcroft
1485f0a594c1SAndy Whitcroft	my $level = 0;
14864635f4fbSAndy Whitcroft	my @stack = ($level);
148700df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
148800df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
148900df344fSAndy Whitcroft		$remain--;
149000df344fSAndy Whitcroft
149100df344fSAndy Whitcroft		$blk .= $rawlines[$line];
14924635f4fbSAndy Whitcroft
14934635f4fbSAndy Whitcroft		# Handle nested #if/#else.
149401464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
14954635f4fbSAndy Whitcroft			push(@stack, $level);
149601464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
14974635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
149801464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
14994635f4fbSAndy Whitcroft			$level = pop(@stack);
15004635f4fbSAndy Whitcroft		}
15014635f4fbSAndy Whitcroft
150201464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1503f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1504f0a594c1SAndy Whitcroft			if ($off > 0) {
1505f0a594c1SAndy Whitcroft				$off--;
1506f0a594c1SAndy Whitcroft				next;
1507f0a594c1SAndy Whitcroft			}
15084a0df2efSAndy Whitcroft
1509f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1510f0a594c1SAndy Whitcroft				$level--;
1511f0a594c1SAndy Whitcroft				last if ($level == 0);
1512f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1513f0a594c1SAndy Whitcroft				$level++;
1514f0a594c1SAndy Whitcroft			}
1515f0a594c1SAndy Whitcroft		}
15164a0df2efSAndy Whitcroft
1517f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
151800df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
15194a0df2efSAndy Whitcroft		}
15204a0df2efSAndy Whitcroft
1521f0a594c1SAndy Whitcroft		last if ($level == 0);
15224a0df2efSAndy Whitcroft	}
15234a0df2efSAndy Whitcroft
1524f0a594c1SAndy Whitcroft	return ($level, @res);
15254a0df2efSAndy Whitcroft}
15264a0df2efSAndy Whitcroftsub ctx_block_outer {
15274a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15284a0df2efSAndy Whitcroft
1529f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1530f0a594c1SAndy Whitcroft	return @r;
15314a0df2efSAndy Whitcroft}
15324a0df2efSAndy Whitcroftsub ctx_block {
15334a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
15344a0df2efSAndy Whitcroft
1535f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1536f0a594c1SAndy Whitcroft	return @r;
1537653d4876SAndy Whitcroft}
1538653d4876SAndy Whitcroftsub ctx_statement {
1539f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1540f0a594c1SAndy Whitcroft
1541f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1542f0a594c1SAndy Whitcroft	return @r;
1543f0a594c1SAndy Whitcroft}
1544f0a594c1SAndy Whitcroftsub ctx_block_level {
1545653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1546653d4876SAndy Whitcroft
1547f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
15484a0df2efSAndy Whitcroft}
15499c0ca6f9SAndy Whitcroftsub ctx_statement_level {
15509c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
15519c0ca6f9SAndy Whitcroft
15529c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
15539c0ca6f9SAndy Whitcroft}
15544a0df2efSAndy Whitcroft
15554a0df2efSAndy Whitcroftsub ctx_locate_comment {
15564a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15574a0df2efSAndy Whitcroft
15584a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1559beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
15604a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
15614a0df2efSAndy Whitcroft
15624a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
15634a0df2efSAndy Whitcroft	# comment.
15644a0df2efSAndy Whitcroft	my $in_comment = 0;
15654a0df2efSAndy Whitcroft	$current_comment = '';
15664a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
156700df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
156800df344fSAndy Whitcroft		#warn "           $line\n";
15694a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
15704a0df2efSAndy Whitcroft			$in_comment = 1;
15714a0df2efSAndy Whitcroft		}
15724a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
15734a0df2efSAndy Whitcroft			$in_comment = 1;
15744a0df2efSAndy Whitcroft		}
15754a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
15764a0df2efSAndy Whitcroft			$current_comment = '';
15774a0df2efSAndy Whitcroft		}
15784a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
15794a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
15804a0df2efSAndy Whitcroft			$in_comment = 0;
15814a0df2efSAndy Whitcroft		}
15824a0df2efSAndy Whitcroft	}
15834a0df2efSAndy Whitcroft
15844a0df2efSAndy Whitcroft	chomp($current_comment);
15854a0df2efSAndy Whitcroft	return($current_comment);
15864a0df2efSAndy Whitcroft}
15874a0df2efSAndy Whitcroftsub ctx_has_comment {
15884a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15894a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
15904a0df2efSAndy Whitcroft
159100df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
15924a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
15934a0df2efSAndy Whitcroft
15944a0df2efSAndy Whitcroft	return ($cmt ne '');
15954a0df2efSAndy Whitcroft}
15964a0df2efSAndy Whitcroft
15974d001e4dSAndy Whitcroftsub raw_line {
15984d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
15994d001e4dSAndy Whitcroft
16004d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16014d001e4dSAndy Whitcroft	$cnt++;
16024d001e4dSAndy Whitcroft
16034d001e4dSAndy Whitcroft	my $line;
16044d001e4dSAndy Whitcroft	while ($cnt) {
16054d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16064d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16074d001e4dSAndy Whitcroft		$cnt--;
16084d001e4dSAndy Whitcroft	}
16094d001e4dSAndy Whitcroft
16104d001e4dSAndy Whitcroft	return $line;
16114d001e4dSAndy Whitcroft}
16124d001e4dSAndy Whitcroft
16130a920b5bSAndy Whitcroftsub cat_vet {
16140a920b5bSAndy Whitcroft	my ($vet) = @_;
16159c0ca6f9SAndy Whitcroft	my ($res, $coded);
16160a920b5bSAndy Whitcroft
16179c0ca6f9SAndy Whitcroft	$res = '';
16186c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
16196c72ffaaSAndy Whitcroft		$res .= $1;
16206c72ffaaSAndy Whitcroft		if ($2 ne '') {
16219c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
16226c72ffaaSAndy Whitcroft			$res .= $coded;
16236c72ffaaSAndy Whitcroft		}
16249c0ca6f9SAndy Whitcroft	}
16259c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
16260a920b5bSAndy Whitcroft
16279c0ca6f9SAndy Whitcroft	return $res;
16280a920b5bSAndy Whitcroft}
16290a920b5bSAndy Whitcroft
1630c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1631cf655043SAndy Whitcroftmy $av_pending;
1632c2fdda0dSAndy Whitcroftmy @av_paren_type;
16331f65f947SAndy Whitcroftmy $av_pend_colon;
1634c2fdda0dSAndy Whitcroft
1635c2fdda0dSAndy Whitcroftsub annotate_reset {
1636c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1637cf655043SAndy Whitcroft	$av_pending = '_';
1638cf655043SAndy Whitcroft	@av_paren_type = ('E');
16391f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1640c2fdda0dSAndy Whitcroft}
1641c2fdda0dSAndy Whitcroft
16426c72ffaaSAndy Whitcroftsub annotate_values {
16436c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
16446c72ffaaSAndy Whitcroft
16456c72ffaaSAndy Whitcroft	my $res;
16461f65f947SAndy Whitcroft	my $var = '_' x length($stream);
16476c72ffaaSAndy Whitcroft	my $cur = $stream;
16486c72ffaaSAndy Whitcroft
1649c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
16506c72ffaaSAndy Whitcroft
16516c72ffaaSAndy Whitcroft	while (length($cur)) {
1652773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1653cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1654171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
16556c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1656c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1657c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1658cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1659c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
16606c72ffaaSAndy Whitcroft			}
16616c72ffaaSAndy Whitcroft
1662c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
16639446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
16649446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1665addcdceaSAndy Whitcroft			$type = 'c';
16669446ef56SAndy Whitcroft
1667e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1668c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
16696c72ffaaSAndy Whitcroft			$type = 'T';
16706c72ffaaSAndy Whitcroft
1671389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1672389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1673389a2fe5SAndy Whitcroft			$type = 'T';
1674389a2fe5SAndy Whitcroft
1675c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1676171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1677c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1678171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1679171ae1a4SAndy Whitcroft			if ($2 ne '') {
1680cf655043SAndy Whitcroft				$av_pending = 'N';
1681171ae1a4SAndy Whitcroft			}
1682171ae1a4SAndy Whitcroft			$type = 'E';
1683171ae1a4SAndy Whitcroft
1684c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1685171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1686171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1687171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
16886c72ffaaSAndy Whitcroft
1689c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1690cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1691c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1692cf655043SAndy Whitcroft
1693cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1694cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1695171ae1a4SAndy Whitcroft			$type = 'E';
1696cf655043SAndy Whitcroft
1697c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1698cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1699cf655043SAndy Whitcroft			$av_preprocessor = 1;
1700cf655043SAndy Whitcroft
1701cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1702cf655043SAndy Whitcroft
1703171ae1a4SAndy Whitcroft			$type = 'E';
1704cf655043SAndy Whitcroft
1705c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1706cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1707cf655043SAndy Whitcroft
1708cf655043SAndy Whitcroft			$av_preprocessor = 1;
1709cf655043SAndy Whitcroft
1710cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1711cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1712cf655043SAndy Whitcroft			pop(@av_paren_type);
1713cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1714171ae1a4SAndy Whitcroft			$type = 'E';
17156c72ffaaSAndy Whitcroft
17166c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1717c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
17186c72ffaaSAndy Whitcroft
1719171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1720171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1721171ae1a4SAndy Whitcroft			$av_pending = $type;
1722171ae1a4SAndy Whitcroft			$type = 'N';
1723171ae1a4SAndy Whitcroft
17246c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1725c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
17266c72ffaaSAndy Whitcroft			if (defined $2) {
1727cf655043SAndy Whitcroft				$av_pending = 'V';
17286c72ffaaSAndy Whitcroft			}
17296c72ffaaSAndy Whitcroft			$type = 'N';
17306c72ffaaSAndy Whitcroft
173114b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1732c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
173314b111c1SAndy Whitcroft			$av_pending = 'E';
17346c72ffaaSAndy Whitcroft			$type = 'N';
17356c72ffaaSAndy Whitcroft
17361f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
17371f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
17381f65f947SAndy Whitcroft			$av_pend_colon = 'C';
17391f65f947SAndy Whitcroft			$type = 'N';
17401f65f947SAndy Whitcroft
174114b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1742c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
17436c72ffaaSAndy Whitcroft			$type = 'N';
17446c72ffaaSAndy Whitcroft
17456c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1746c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1747cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1748cf655043SAndy Whitcroft			$av_pending = '_';
17496c72ffaaSAndy Whitcroft			$type = 'N';
17506c72ffaaSAndy Whitcroft
17516c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1752cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1753cf655043SAndy Whitcroft			if ($new_type ne '_') {
1754cf655043SAndy Whitcroft				$type = $new_type;
1755c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1756c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
17576c72ffaaSAndy Whitcroft			} else {
1758c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
17596c72ffaaSAndy Whitcroft			}
17606c72ffaaSAndy Whitcroft
1761c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1762c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1763c8cb2ca3SAndy Whitcroft			$type = 'V';
1764cf655043SAndy Whitcroft			$av_pending = 'V';
17656c72ffaaSAndy Whitcroft
17668e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
17678e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
17681f65f947SAndy Whitcroft				$av_pend_colon = 'B';
17698e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
17708e761b04SAndy Whitcroft				$av_pend_colon = 'L';
17711f65f947SAndy Whitcroft			}
17721f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
17731f65f947SAndy Whitcroft			$type = 'V';
17741f65f947SAndy Whitcroft
17756c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1776c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
17776c72ffaaSAndy Whitcroft			$type = 'V';
17786c72ffaaSAndy Whitcroft
17796c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1780c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
17816c72ffaaSAndy Whitcroft			$type = 'N';
17826c72ffaaSAndy Whitcroft
1783cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1784c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
178513214adfSAndy Whitcroft			$type = 'E';
17861f65f947SAndy Whitcroft			$av_pend_colon = 'O';
178713214adfSAndy Whitcroft
17888e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
17898e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
17908e761b04SAndy Whitcroft			$type = 'C';
17918e761b04SAndy Whitcroft
17921f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
17931f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
17941f65f947SAndy Whitcroft			$type = 'N';
17951f65f947SAndy Whitcroft
17961f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
17971f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
17981f65f947SAndy Whitcroft
17991f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
18001f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
18011f65f947SAndy Whitcroft				$type = 'E';
18021f65f947SAndy Whitcroft			} else {
18031f65f947SAndy Whitcroft				$type = 'N';
18041f65f947SAndy Whitcroft			}
18051f65f947SAndy Whitcroft			$av_pend_colon = 'O';
18061f65f947SAndy Whitcroft
18078e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
180813214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
18096c72ffaaSAndy Whitcroft			$type = 'N';
18106c72ffaaSAndy Whitcroft
18110d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
181274048ed8SAndy Whitcroft			my $variant;
181374048ed8SAndy Whitcroft
181474048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
181574048ed8SAndy Whitcroft			if ($type eq 'V') {
181674048ed8SAndy Whitcroft				$variant = 'B';
181774048ed8SAndy Whitcroft			} else {
181874048ed8SAndy Whitcroft				$variant = 'U';
181974048ed8SAndy Whitcroft			}
182074048ed8SAndy Whitcroft
182174048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
182274048ed8SAndy Whitcroft			$type = 'N';
182374048ed8SAndy Whitcroft
18246c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1825c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
18266c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
18276c72ffaaSAndy Whitcroft				$type = 'N';
18286c72ffaaSAndy Whitcroft			}
18296c72ffaaSAndy Whitcroft
18306c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1831c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
18326c72ffaaSAndy Whitcroft		}
18336c72ffaaSAndy Whitcroft		if (defined $1) {
18346c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
18356c72ffaaSAndy Whitcroft			$res .= $type x length($1);
18366c72ffaaSAndy Whitcroft		}
18376c72ffaaSAndy Whitcroft	}
18386c72ffaaSAndy Whitcroft
18391f65f947SAndy Whitcroft	return ($res, $var);
18406c72ffaaSAndy Whitcroft}
18416c72ffaaSAndy Whitcroft
18428905a67cSAndy Whitcroftsub possible {
184313214adfSAndy Whitcroft	my ($possible, $line) = @_;
18449a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
18450776e594SAndy Whitcroft		^(?:
18460776e594SAndy Whitcroft			$Modifier|
18470776e594SAndy Whitcroft			$Storage|
18480776e594SAndy Whitcroft			$Type|
18499a974fdbSAndy Whitcroft			DEFINE_\S+
18509a974fdbSAndy Whitcroft		)$|
18519a974fdbSAndy Whitcroft		^(?:
18520776e594SAndy Whitcroft			goto|
18530776e594SAndy Whitcroft			return|
18540776e594SAndy Whitcroft			case|
18550776e594SAndy Whitcroft			else|
18560776e594SAndy Whitcroft			asm|__asm__|
185789a88353SAndy Whitcroft			do|
185889a88353SAndy Whitcroft			\#|
185989a88353SAndy Whitcroft			\#\#|
18609a974fdbSAndy Whitcroft		)(?:\s|$)|
18610776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
18629a974fdbSAndy Whitcroft	    )}x;
18639a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
18649a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1865c45dcabdSAndy Whitcroft		# Check for modifiers.
1866c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1867c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1868c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1869c45dcabdSAndy Whitcroft
1870c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1871c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1872d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
18739a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1874d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1875485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1876d2506586SAndy Whitcroft				}
18779a974fdbSAndy Whitcroft			}
1878c45dcabdSAndy Whitcroft
1879c45dcabdSAndy Whitcroft		} else {
188013214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1881485ff23eSAlex Dowad			push(@typeListFile, $possible);
1882c45dcabdSAndy Whitcroft		}
18838905a67cSAndy Whitcroft		build_types();
18840776e594SAndy Whitcroft	} else {
18850776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
18868905a67cSAndy Whitcroft	}
18878905a67cSAndy Whitcroft}
18888905a67cSAndy Whitcroft
18896c72ffaaSAndy Whitcroftmy $prefix = '';
18906c72ffaaSAndy Whitcroft
1891000d1cc1SJoe Perchessub show_type {
1892cbec18afSJoe Perches	my ($type) = @_;
189391bfe484SJoe Perches
1894522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
1895522b837cSAlexey Dobriyan
1896cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1897cbec18afSJoe Perches
1898cbec18afSJoe Perches	return !defined $ignore_type{$type};
1899000d1cc1SJoe Perches}
1900000d1cc1SJoe Perches
1901f0a594c1SAndy Whitcroftsub report {
1902cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1903cbec18afSJoe Perches
1904cbec18afSJoe Perches	if (!show_type($type) ||
1905cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1906773647a0SAndy Whitcroft		return 0;
1907773647a0SAndy Whitcroft	}
190857230297SJoe Perches	my $output = '';
1909737c0767SJohn Brooks	if ($color) {
191057230297SJoe Perches		if ($level eq 'ERROR') {
191157230297SJoe Perches			$output .= RED;
191257230297SJoe Perches		} elsif ($level eq 'WARNING') {
191357230297SJoe Perches			$output .= YELLOW;
1914000d1cc1SJoe Perches		} else {
191557230297SJoe Perches			$output .= GREEN;
1916000d1cc1SJoe Perches		}
191757230297SJoe Perches	}
191857230297SJoe Perches	$output .= $prefix . $level . ':';
191957230297SJoe Perches	if ($show_types) {
1920737c0767SJohn Brooks		$output .= BLUE if ($color);
192157230297SJoe Perches		$output .= "$type:";
192257230297SJoe Perches	}
1923737c0767SJohn Brooks	$output .= RESET if ($color);
192457230297SJoe Perches	$output .= ' ' . $msg . "\n";
192534d8815fSJoe Perches
192634d8815fSJoe Perches	if ($showfile) {
192734d8815fSJoe Perches		my @lines = split("\n", $output, -1);
192834d8815fSJoe Perches		splice(@lines, 1, 1);
192934d8815fSJoe Perches		$output = join("\n", @lines);
193034d8815fSJoe Perches	}
193157230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
19328905a67cSAndy Whitcroft
193357230297SJoe Perches	push(our @report, $output);
1934773647a0SAndy Whitcroft
1935773647a0SAndy Whitcroft	return 1;
1936f0a594c1SAndy Whitcroft}
1937cbec18afSJoe Perches
1938f0a594c1SAndy Whitcroftsub report_dump {
193913214adfSAndy Whitcroft	our @report;
1940f0a594c1SAndy Whitcroft}
1941000d1cc1SJoe Perches
1942d752fcc8SJoe Perchessub fixup_current_range {
1943d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1944d752fcc8SJoe Perches
1945d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1946d752fcc8SJoe Perches		my $o = $1;
1947d752fcc8SJoe Perches		my $l = $2;
1948d752fcc8SJoe Perches		my $no = $o + $offset;
1949d752fcc8SJoe Perches		my $nl = $l + $length;
1950d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1951d752fcc8SJoe Perches	}
1952d752fcc8SJoe Perches}
1953d752fcc8SJoe Perches
1954d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1955d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1956d752fcc8SJoe Perches
1957d752fcc8SJoe Perches	my $range_last_linenr = 0;
1958d752fcc8SJoe Perches	my $delta_offset = 0;
1959d752fcc8SJoe Perches
1960d752fcc8SJoe Perches	my $old_linenr = 0;
1961d752fcc8SJoe Perches	my $new_linenr = 0;
1962d752fcc8SJoe Perches
1963d752fcc8SJoe Perches	my $next_insert = 0;
1964d752fcc8SJoe Perches	my $next_delete = 0;
1965d752fcc8SJoe Perches
1966d752fcc8SJoe Perches	my @lines = ();
1967d752fcc8SJoe Perches
1968d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1969d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1970d752fcc8SJoe Perches
1971d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1972d752fcc8SJoe Perches		my $save_line = 1;
1973d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1974323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1975d752fcc8SJoe Perches			$delta_offset = 0;
1976d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1977d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1978d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1979d752fcc8SJoe Perches		}
1980d752fcc8SJoe Perches
1981d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1982d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1983d752fcc8SJoe Perches			$save_line = 0;
1984d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1985d752fcc8SJoe Perches		}
1986d752fcc8SJoe Perches
1987d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1988d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1989d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1990d752fcc8SJoe Perches			$new_linenr++;
1991d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1992d752fcc8SJoe Perches		}
1993d752fcc8SJoe Perches
1994d752fcc8SJoe Perches		if ($save_line) {
1995d752fcc8SJoe Perches			push(@lines, $line);
1996d752fcc8SJoe Perches			$new_linenr++;
1997d752fcc8SJoe Perches		}
1998d752fcc8SJoe Perches
1999d752fcc8SJoe Perches		$old_linenr++;
2000d752fcc8SJoe Perches	}
2001d752fcc8SJoe Perches
2002d752fcc8SJoe Perches	return @lines;
2003d752fcc8SJoe Perches}
2004d752fcc8SJoe Perches
2005f2d7e4d4SJoe Perchessub fix_insert_line {
2006f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2007f2d7e4d4SJoe Perches
2008f2d7e4d4SJoe Perches	my $inserted = {
2009f2d7e4d4SJoe Perches		LINENR => $linenr,
2010f2d7e4d4SJoe Perches		LINE => $line,
2011f2d7e4d4SJoe Perches	};
2012f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2013f2d7e4d4SJoe Perches}
2014f2d7e4d4SJoe Perches
2015f2d7e4d4SJoe Perchessub fix_delete_line {
2016f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2017f2d7e4d4SJoe Perches
2018f2d7e4d4SJoe Perches	my $deleted = {
2019f2d7e4d4SJoe Perches		LINENR => $linenr,
2020f2d7e4d4SJoe Perches		LINE => $line,
2021f2d7e4d4SJoe Perches	};
2022f2d7e4d4SJoe Perches
2023f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2024f2d7e4d4SJoe Perches}
2025f2d7e4d4SJoe Perches
2026de7d4f0eSAndy Whitcroftsub ERROR {
2027cbec18afSJoe Perches	my ($type, $msg) = @_;
2028cbec18afSJoe Perches
2029cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2030de7d4f0eSAndy Whitcroft		our $clean = 0;
20316c72ffaaSAndy Whitcroft		our $cnt_error++;
20323705ce5bSJoe Perches		return 1;
2033de7d4f0eSAndy Whitcroft	}
20343705ce5bSJoe Perches	return 0;
2035773647a0SAndy Whitcroft}
2036de7d4f0eSAndy Whitcroftsub WARN {
2037cbec18afSJoe Perches	my ($type, $msg) = @_;
2038cbec18afSJoe Perches
2039cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2040de7d4f0eSAndy Whitcroft		our $clean = 0;
20416c72ffaaSAndy Whitcroft		our $cnt_warn++;
20423705ce5bSJoe Perches		return 1;
2043de7d4f0eSAndy Whitcroft	}
20443705ce5bSJoe Perches	return 0;
2045773647a0SAndy Whitcroft}
2046de7d4f0eSAndy Whitcroftsub CHK {
2047cbec18afSJoe Perches	my ($type, $msg) = @_;
2048cbec18afSJoe Perches
2049cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2050de7d4f0eSAndy Whitcroft		our $clean = 0;
20516c72ffaaSAndy Whitcroft		our $cnt_chk++;
20523705ce5bSJoe Perches		return 1;
20536c72ffaaSAndy Whitcroft	}
20543705ce5bSJoe Perches	return 0;
2055de7d4f0eSAndy Whitcroft}
2056de7d4f0eSAndy Whitcroft
20576ecd9674SAndy Whitcroftsub check_absolute_file {
20586ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
20596ecd9674SAndy Whitcroft	my $file = $absolute;
20606ecd9674SAndy Whitcroft
20616ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
20626ecd9674SAndy Whitcroft
20636ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
20646ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
20656ecd9674SAndy Whitcroft		if (-f "$root/$file") {
20666ecd9674SAndy Whitcroft			##print "file<$file>\n";
20676ecd9674SAndy Whitcroft			last;
20686ecd9674SAndy Whitcroft		}
20696ecd9674SAndy Whitcroft	}
20706ecd9674SAndy Whitcroft	if (! -f _)  {
20716ecd9674SAndy Whitcroft		return 0;
20726ecd9674SAndy Whitcroft	}
20736ecd9674SAndy Whitcroft
20746ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
20756ecd9674SAndy Whitcroft	my $prefix = $absolute;
20766ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
20776ecd9674SAndy Whitcroft
20786ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
20796ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2080000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2081000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
20826ecd9674SAndy Whitcroft	}
20836ecd9674SAndy Whitcroft}
20846ecd9674SAndy Whitcroft
20853705ce5bSJoe Perchessub trim {
20863705ce5bSJoe Perches	my ($string) = @_;
20873705ce5bSJoe Perches
2088b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2089b34c648bSJoe Perches
2090b34c648bSJoe Perches	return $string;
2091b34c648bSJoe Perches}
2092b34c648bSJoe Perches
2093b34c648bSJoe Perchessub ltrim {
2094b34c648bSJoe Perches	my ($string) = @_;
2095b34c648bSJoe Perches
2096b34c648bSJoe Perches	$string =~ s/^\s+//;
2097b34c648bSJoe Perches
2098b34c648bSJoe Perches	return $string;
2099b34c648bSJoe Perches}
2100b34c648bSJoe Perches
2101b34c648bSJoe Perchessub rtrim {
2102b34c648bSJoe Perches	my ($string) = @_;
2103b34c648bSJoe Perches
2104b34c648bSJoe Perches	$string =~ s/\s+$//;
21053705ce5bSJoe Perches
21063705ce5bSJoe Perches	return $string;
21073705ce5bSJoe Perches}
21083705ce5bSJoe Perches
210952ea8506SJoe Perchessub string_find_replace {
211052ea8506SJoe Perches	my ($string, $find, $replace) = @_;
211152ea8506SJoe Perches
211252ea8506SJoe Perches	$string =~ s/$find/$replace/g;
211352ea8506SJoe Perches
211452ea8506SJoe Perches	return $string;
211552ea8506SJoe Perches}
211652ea8506SJoe Perches
21173705ce5bSJoe Perchessub tabify {
21183705ce5bSJoe Perches	my ($leading) = @_;
21193705ce5bSJoe Perches
21203705ce5bSJoe Perches	my $source_indent = 8;
21213705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
21223705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
21233705ce5bSJoe Perches
21243705ce5bSJoe Perches	#convert leading spaces to tabs
21253705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
21263705ce5bSJoe Perches	#Remove spaces before a tab
21273705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
21283705ce5bSJoe Perches
21293705ce5bSJoe Perches	return "$leading";
21303705ce5bSJoe Perches}
21313705ce5bSJoe Perches
2132d1fe9c09SJoe Perchessub pos_last_openparen {
2133d1fe9c09SJoe Perches	my ($line) = @_;
2134d1fe9c09SJoe Perches
2135d1fe9c09SJoe Perches	my $pos = 0;
2136d1fe9c09SJoe Perches
2137d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2138d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2139d1fe9c09SJoe Perches
2140d1fe9c09SJoe Perches	my $last_openparen = 0;
2141d1fe9c09SJoe Perches
2142d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2143d1fe9c09SJoe Perches		return -1;
2144d1fe9c09SJoe Perches	}
2145d1fe9c09SJoe Perches
2146d1fe9c09SJoe Perches	my $len = length($line);
2147d1fe9c09SJoe Perches
2148d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2149d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2150d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2151d1fe9c09SJoe Perches			$pos += length($1) - 1;
2152d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2153d1fe9c09SJoe Perches			$last_openparen = $pos;
2154d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2155d1fe9c09SJoe Perches			last;
2156d1fe9c09SJoe Perches		}
2157d1fe9c09SJoe Perches	}
2158d1fe9c09SJoe Perches
215991cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2160d1fe9c09SJoe Perches}
2161d1fe9c09SJoe Perches
21620a920b5bSAndy Whitcroftsub process {
21630a920b5bSAndy Whitcroft	my $filename = shift;
21640a920b5bSAndy Whitcroft
21650a920b5bSAndy Whitcroft	my $linenr=0;
21660a920b5bSAndy Whitcroft	my $prevline="";
2167c2fdda0dSAndy Whitcroft	my $prevrawline="";
21680a920b5bSAndy Whitcroft	my $stashline="";
2169c2fdda0dSAndy Whitcroft	my $stashrawline="";
21700a920b5bSAndy Whitcroft
21714a0df2efSAndy Whitcroft	my $length;
21720a920b5bSAndy Whitcroft	my $indent;
21730a920b5bSAndy Whitcroft	my $previndent=0;
21740a920b5bSAndy Whitcroft	my $stashindent=0;
21750a920b5bSAndy Whitcroft
2176de7d4f0eSAndy Whitcroft	our $clean = 1;
21770a920b5bSAndy Whitcroft	my $signoff = 0;
21780a920b5bSAndy Whitcroft	my $is_patch = 0;
217929ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
218015662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2181ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2182bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
21832a076f40SJoe Perches	my $commit_log_long_line = 0;
2184e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
218513f1937eSJoe Perches	my $reported_maintainer_file = 0;
2186fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2187fa64205dSPasi Savanainen
2188365dd4eaSJoe Perches	my $last_blank_line = 0;
21895e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2190365dd4eaSJoe Perches
219113214adfSAndy Whitcroft	our @report = ();
21926c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
21936c72ffaaSAndy Whitcroft	our $cnt_error = 0;
21946c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
21956c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
21966c72ffaaSAndy Whitcroft
21970a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
21980a920b5bSAndy Whitcroft	my $realfile = '';
21990a920b5bSAndy Whitcroft	my $realline = 0;
22000a920b5bSAndy Whitcroft	my $realcnt = 0;
22010a920b5bSAndy Whitcroft	my $here = '';
220277cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
22030a920b5bSAndy Whitcroft	my $in_comment = 0;
2204c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
22050a920b5bSAndy Whitcroft	my $first_line = 0;
22061e855726SWolfram Sang	my $p1_prefix = '';
22070a920b5bSAndy Whitcroft
220813214adfSAndy Whitcroft	my $prev_values = 'E';
220913214adfSAndy Whitcroft
221013214adfSAndy Whitcroft	# suppression flags
2211773647a0SAndy Whitcroft	my %suppress_ifbraces;
2212170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
22132b474a1aSAndy Whitcroft	my %suppress_export;
22143e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2215653d4876SAndy Whitcroft
22167e51f197SJoe Perches	my %signatures = ();
2217323c1260SJoe Perches
2218c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2219de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2220c2fdda0dSAndy Whitcroft	#
2221de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2222de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2223773647a0SAndy Whitcroft
2224d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2225d8b07710SJoe Perches
2226773647a0SAndy Whitcroft	sanitise_line_reset();
2227c2fdda0dSAndy Whitcroft	my $line;
2228c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2229773647a0SAndy Whitcroft		$linenr++;
2230773647a0SAndy Whitcroft		$line = $rawline;
2231c2fdda0dSAndy Whitcroft
22323705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
22333705ce5bSJoe Perches
2234773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2235de7d4f0eSAndy Whitcroft			$setup_docs = 0;
22368c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2237de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2238de7d4f0eSAndy Whitcroft			}
2239773647a0SAndy Whitcroft			#next;
2240de7d4f0eSAndy Whitcroft		}
224174fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2242773647a0SAndy Whitcroft			$realline=$1-1;
2243773647a0SAndy Whitcroft			if (defined $2) {
2244773647a0SAndy Whitcroft				$realcnt=$3+1;
2245773647a0SAndy Whitcroft			} else {
2246773647a0SAndy Whitcroft				$realcnt=1+1;
2247773647a0SAndy Whitcroft			}
2248c45dcabdSAndy Whitcroft			$in_comment = 0;
2249773647a0SAndy Whitcroft
2250773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2251773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2252773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2253773647a0SAndy Whitcroft			# at context start.
2254773647a0SAndy Whitcroft			my $edge;
225501fa9147SAndy Whitcroft			my $cnt = $realcnt;
225601fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
225701fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
225801fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
225901fa9147SAndy Whitcroft				$cnt--;
226001fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2261721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2262fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2263fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2264fae17daeSAndy Whitcroft					($edge) = $1;
2265fae17daeSAndy Whitcroft					last;
2266fae17daeSAndy Whitcroft				}
2267773647a0SAndy Whitcroft			}
2268773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2269773647a0SAndy Whitcroft				$in_comment = 1;
2270773647a0SAndy Whitcroft			}
2271773647a0SAndy Whitcroft
2272773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2273773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2274773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2275773647a0SAndy Whitcroft			if (!defined $edge &&
227683242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2277773647a0SAndy Whitcroft			{
2278773647a0SAndy Whitcroft				$in_comment = 1;
2279773647a0SAndy Whitcroft			}
2280773647a0SAndy Whitcroft
2281773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2282773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2283773647a0SAndy Whitcroft
2284171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2285773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2286171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2287773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2288773647a0SAndy Whitcroft		}
2289773647a0SAndy Whitcroft		push(@lines, $line);
2290773647a0SAndy Whitcroft
2291773647a0SAndy Whitcroft		if ($realcnt > 1) {
2292773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2293773647a0SAndy Whitcroft		} else {
2294773647a0SAndy Whitcroft			$realcnt = 0;
2295773647a0SAndy Whitcroft		}
2296773647a0SAndy Whitcroft
2297773647a0SAndy Whitcroft		#print "==>$rawline\n";
2298773647a0SAndy Whitcroft		#print "-->$line\n";
2299de7d4f0eSAndy Whitcroft
2300de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2301de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2302de7d4f0eSAndy Whitcroft		}
2303de7d4f0eSAndy Whitcroft	}
2304de7d4f0eSAndy Whitcroft
23056c72ffaaSAndy Whitcroft	$prefix = '';
23066c72ffaaSAndy Whitcroft
2307773647a0SAndy Whitcroft	$realcnt = 0;
2308773647a0SAndy Whitcroft	$linenr = 0;
2309194f66fcSJoe Perches	$fixlinenr = -1;
23100a920b5bSAndy Whitcroft	foreach my $line (@lines) {
23110a920b5bSAndy Whitcroft		$linenr++;
2312194f66fcSJoe Perches		$fixlinenr++;
23131b5539b1SJoe Perches		my $sline = $line;	#copy of $line
23141b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
23150a920b5bSAndy Whitcroft
2316c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
23176c72ffaaSAndy Whitcroft
23180a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2319e518e9a5SJoe Perches		if (!$in_commit_log &&
232074fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
232174fd4f34SJoe Perches			my $context = $4;
23220a920b5bSAndy Whitcroft			$is_patch = 1;
23234a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
23240a920b5bSAndy Whitcroft			$realline=$1-1;
23250a920b5bSAndy Whitcroft			if (defined $2) {
23260a920b5bSAndy Whitcroft				$realcnt=$3+1;
23270a920b5bSAndy Whitcroft			} else {
23280a920b5bSAndy Whitcroft				$realcnt=1+1;
23290a920b5bSAndy Whitcroft			}
2330c2fdda0dSAndy Whitcroft			annotate_reset();
233113214adfSAndy Whitcroft			$prev_values = 'E';
233213214adfSAndy Whitcroft
2333773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2334170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
23352b474a1aSAndy Whitcroft			%suppress_export = ();
23363e469cdcSAndy Whitcroft			$suppress_statement = 0;
233774fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
233874fd4f34SJoe Perches				$context_function = $1;
233974fd4f34SJoe Perches			} else {
234074fd4f34SJoe Perches				undef $context_function;
234174fd4f34SJoe Perches			}
23420a920b5bSAndy Whitcroft			next;
23430a920b5bSAndy Whitcroft
23444a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
23454a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
23464a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2347773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
23480a920b5bSAndy Whitcroft			$realline++;
2349d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
23500a920b5bSAndy Whitcroft
23514a0df2efSAndy Whitcroft			# Measure the line length and indent.
2352c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
23530a920b5bSAndy Whitcroft
23540a920b5bSAndy Whitcroft			# Track the previous line.
23550a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
23560a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2357c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2358c2fdda0dSAndy Whitcroft
2359773647a0SAndy Whitcroft			#warn "line<$line>\n";
23606c72ffaaSAndy Whitcroft
2361d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2362d8aaf121SAndy Whitcroft			$realcnt--;
23630a920b5bSAndy Whitcroft		}
23640a920b5bSAndy Whitcroft
2365cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2366cc77cdcaSAndy Whitcroft
23676c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
23686c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2369773647a0SAndy Whitcroft
23702ac73b4fSJoe Perches		my $found_file = 0;
2371773647a0SAndy Whitcroft		# extract the filename as it passes
23723bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
23733bf9a009SRabin Vincent			$realfile = $1;
23742b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2375270c49a0SJoe Perches			$in_commit_log = 0;
23762ac73b4fSJoe Perches			$found_file = 1;
23773bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2378773647a0SAndy Whitcroft			$realfile = $1;
23792b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2380270c49a0SJoe Perches			$in_commit_log = 0;
23811e855726SWolfram Sang
23821e855726SWolfram Sang			$p1_prefix = $1;
2383e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2384e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2385000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2386000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
23871e855726SWolfram Sang			}
2388773647a0SAndy Whitcroft
2389c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2390000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2391000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2392773647a0SAndy Whitcroft			}
23932ac73b4fSJoe Perches			$found_file = 1;
23942ac73b4fSJoe Perches		}
23952ac73b4fSJoe Perches
239634d8815fSJoe Perches#make up the handle for any error we report on this line
239734d8815fSJoe Perches		if ($showfile) {
239834d8815fSJoe Perches			$prefix = "$realfile:$realline: "
239934d8815fSJoe Perches		} elsif ($emacs) {
24007d3a9f67SJoe Perches			if ($file) {
24017d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
24027d3a9f67SJoe Perches			} else {
240334d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
240434d8815fSJoe Perches			}
24057d3a9f67SJoe Perches		}
240634d8815fSJoe Perches
24072ac73b4fSJoe Perches		if ($found_file) {
240885b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
240985b0ee18SJoe Perches				WARN("OBSOLETE",
241085b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
241185b0ee18SJoe Perches			}
24127bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
24132ac73b4fSJoe Perches				$check = 1;
24142ac73b4fSJoe Perches			} else {
24152ac73b4fSJoe Perches				$check = $check_orig;
24162ac73b4fSJoe Perches			}
2417773647a0SAndy Whitcroft			next;
2418773647a0SAndy Whitcroft		}
2419773647a0SAndy Whitcroft
2420389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
24210a920b5bSAndy Whitcroft
2422c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2423c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2424c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
24250a920b5bSAndy Whitcroft
24266c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
24276c72ffaaSAndy Whitcroft
2428e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2429e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2430e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2431e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2432e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2433e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2434e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2435e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2436e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2437e518e9a5SJoe Perches		}
2438e518e9a5SJoe Perches
24393bf9a009SRabin Vincent# Check for incorrect file permissions
24403bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
24413bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
244204db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
244304db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2444000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2445000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
24463bf9a009SRabin Vincent			}
24473bf9a009SRabin Vincent		}
24483bf9a009SRabin Vincent
244920112475SJoe Perches# Check the patch for a signoff:
2450d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
24514a0df2efSAndy Whitcroft			$signoff++;
245215662b3eSJoe Perches			$in_commit_log = 0;
24530a920b5bSAndy Whitcroft		}
245420112475SJoe Perches
2455e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2456e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2457e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2458e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2459e0d975b1SJoe Perches		}
2460e0d975b1SJoe Perches
246120112475SJoe Perches# Check signature styles
2462270c49a0SJoe Perches		if (!$in_header_lines &&
2463ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
246420112475SJoe Perches			my $space_before = $1;
246520112475SJoe Perches			my $sign_off = $2;
246620112475SJoe Perches			my $space_after = $3;
246720112475SJoe Perches			my $email = $4;
246820112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
246920112475SJoe Perches
2470ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2471ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2472ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2473ce0338dfSJoe Perches			}
247420112475SJoe Perches			if (defined $space_before && $space_before ne "") {
24753705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24763705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
24773705ce5bSJoe Perches				    $fix) {
2478194f66fcSJoe Perches					$fixed[$fixlinenr] =
24793705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24803705ce5bSJoe Perches				}
248120112475SJoe Perches			}
248220112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
24833705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24843705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
24853705ce5bSJoe Perches				    $fix) {
2486194f66fcSJoe Perches					$fixed[$fixlinenr] =
24873705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24883705ce5bSJoe Perches				}
24893705ce5bSJoe Perches
249020112475SJoe Perches			}
249120112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
24923705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24933705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
24943705ce5bSJoe Perches				    $fix) {
2495194f66fcSJoe Perches					$fixed[$fixlinenr] =
24963705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24973705ce5bSJoe Perches				}
249820112475SJoe Perches			}
249920112475SJoe Perches
250020112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
250120112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
250220112475SJoe Perches			if ($suggested_email eq "") {
2503000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2504000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
250520112475SJoe Perches			} else {
250620112475SJoe Perches				my $dequoted = $suggested_email;
250720112475SJoe Perches				$dequoted =~ s/^"//;
250820112475SJoe Perches				$dequoted =~ s/" </ </;
250920112475SJoe Perches				# Don't force email to have quotes
251020112475SJoe Perches				# Allow just an angle bracketed address
251120112475SJoe Perches				if ("$dequoted$comment" ne $email &&
251220112475SJoe Perches				    "<$email_address>$comment" ne $email &&
251320112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2514000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2515000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
251620112475SJoe Perches				}
25170a920b5bSAndy Whitcroft			}
25187e51f197SJoe Perches
25197e51f197SJoe Perches# Check for duplicate signatures
25207e51f197SJoe Perches			my $sig_nospace = $line;
25217e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
25227e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
25237e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
25247e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
25257e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
25267e51f197SJoe Perches			} else {
25277e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
25287e51f197SJoe Perches			}
25290a920b5bSAndy Whitcroft		}
25300a920b5bSAndy Whitcroft
2531a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2532a2fe16b9SJoe Perches		if ($in_header_lines &&
2533a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2534a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2535a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2536a2fe16b9SJoe Perches		}
2537a2fe16b9SJoe Perches
25389b3189ebSJoe Perches# Check for old stable address
25399b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
25409b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
25419b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
25429b3189ebSJoe Perches		}
25439b3189ebSJoe Perches
25447ebd05efSChristopher Covington# Check for unwanted Gerrit info
25457ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
25467ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
25477ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
25487ebd05efSChristopher Covington		}
25497ebd05efSChristopher Covington
2550369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2551369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2552369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2553369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2554369c8dd3SJoe Perches					# timestamp
2555369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2556369c8dd3SJoe Perches					# stack dump address
2557369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2558369c8dd3SJoe Perches		}
2559369c8dd3SJoe Perches
25602a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
25612a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2562bf4daf12SJoe Perches		    length($line) > 75 &&
2563bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2564bf4daf12SJoe Perches					# file delta changes
2565bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2566bf4daf12SJoe Perches					# filename then :
2567bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2568bf4daf12SJoe Perches					# A Fixes: or Link: line
2569bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
25702a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
25712a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
25722a076f40SJoe Perches			$commit_log_long_line = 1;
25732a076f40SJoe Perches		}
25742a076f40SJoe Perches
2575bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2576bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2577bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2578bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2579bf4daf12SJoe Perches		}
2580bf4daf12SJoe Perches
25810d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2582369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2583aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2584e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2585fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2586aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2587369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2588bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2589fe043ea1SJoe Perches			my $init_char = "c";
2590fe043ea1SJoe Perches			my $orig_commit = "";
25910d7835fcSJoe Perches			my $short = 1;
25920d7835fcSJoe Perches			my $long = 0;
25930d7835fcSJoe Perches			my $case = 1;
25940d7835fcSJoe Perches			my $space = 1;
25950d7835fcSJoe Perches			my $hasdesc = 0;
259619c146a6SJoe Perches			my $hasparens = 0;
25970d7835fcSJoe Perches			my $id = '0123456789ab';
25980d7835fcSJoe Perches			my $orig_desc = "commit description";
25990d7835fcSJoe Perches			my $description = "";
26000d7835fcSJoe Perches
2601fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2602fe043ea1SJoe Perches				$init_char = $1;
2603fe043ea1SJoe Perches				$orig_commit = lc($2);
2604fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2605fe043ea1SJoe Perches				$orig_commit = lc($1);
2606fe043ea1SJoe Perches			}
2607fe043ea1SJoe Perches
26080d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
26090d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
26100d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
26110d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
26120d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
26130d7835fcSJoe Perches				$orig_desc = $1;
261419c146a6SJoe Perches				$hasparens = 1;
26150d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
26160d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
26170d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
26180d7835fcSJoe Perches				$orig_desc = $1;
261919c146a6SJoe Perches				$hasparens = 1;
2620b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2621b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2622b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2623b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2624b671fde0SJoe Perches				$orig_desc = $1;
2625b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2626b671fde0SJoe Perches				$orig_desc .= " " . $1;
262719c146a6SJoe Perches				$hasparens = 1;
26280d7835fcSJoe Perches			}
26290d7835fcSJoe Perches
26300d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
26310d7835fcSJoe Perches							      $id, $orig_desc);
26320d7835fcSJoe Perches
2633948b133aSHeinrich Schuchardt			if (defined($id) &&
2634948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2635d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
26360d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
26370d7835fcSJoe Perches			}
2638d311cd44SJoe Perches		}
2639d311cd44SJoe Perches
264013f1937eSJoe Perches# Check for added, moved or deleted files
264113f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
264213f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
264313f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
264413f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
264513f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2646a82603a8SAndrew Jeffery			$is_patch = 1;
264713f1937eSJoe Perches			$reported_maintainer_file = 1;
264813f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
264913f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
265013f1937eSJoe Perches		}
265113f1937eSJoe Perches
265200df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
26538905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2654000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2655000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
26566c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2657de7d4f0eSAndy Whitcroft		}
2658de7d4f0eSAndy Whitcroft
2659de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2660de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2661171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2662171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2663171ae1a4SAndy Whitcroft
2664171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2665171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2666171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2667171ae1a4SAndy Whitcroft
266834d99219SJoe Perches			CHK("INVALID_UTF8",
2669000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
267000df344fSAndy Whitcroft		}
26710a920b5bSAndy Whitcroft
267215662b3eSJoe Perches# Check if it's the start of a commit log
267315662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
267415662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2675eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2676eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
267715662b3eSJoe Perches			$in_header_lines = 0;
267815662b3eSJoe Perches			$in_commit_log = 1;
2679ed43c4e5SAllen Hubbe			$has_commit_log = 1;
268015662b3eSJoe Perches		}
268115662b3eSJoe Perches
2682fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2683fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2684fa64205dSPasi Savanainen		if ($in_header_lines &&
2685fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2686fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2687fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2688fa64205dSPasi Savanainen		}
2689fa64205dSPasi Savanainen
2690fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
269115662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2692fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
269315662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
269415662b3eSJoe Perches		}
269515662b3eSJoe Perches
2696d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2697d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2698d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2699d6430f71SJoe Perches				my $file = $1;
2700d6430f71SJoe Perches
2701d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2702d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2703d6430f71SJoe Perches					#
2704d6430f71SJoe Perches				} else {
2705d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2706d6430f71SJoe Perches				}
2707d6430f71SJoe Perches			}
2708d6430f71SJoe Perches		}
2709d6430f71SJoe Perches
271066b47b4aSKees Cook# Check for various typo / spelling mistakes
271166d7a382SJoe Perches		if (defined($misspellings) &&
271266d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2713ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
271466b47b4aSKees Cook				my $typo = $1;
271566b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
271666b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
271766b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
271866b47b4aSKees Cook				my $msg_type = \&WARN;
271966b47b4aSKees Cook				$msg_type = \&CHK if ($file);
272066b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
272166b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
272266b47b4aSKees Cook				    $fix) {
272366b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
272466b47b4aSKees Cook				}
272566b47b4aSKees Cook			}
272666b47b4aSKees Cook		}
272766b47b4aSKees Cook
272830670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
272930670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
273000df344fSAndy Whitcroft
27310a920b5bSAndy Whitcroft#trailing whitespace
27329c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2733c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2734d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2735d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2736d5e616fcSJoe Perches			    $fix) {
2737194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2738d5e616fcSJoe Perches			}
2739c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2740c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27413705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
27423705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
27433705ce5bSJoe Perches			    $fix) {
2744194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
27453705ce5bSJoe Perches			}
27463705ce5bSJoe Perches
2747d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
27480a920b5bSAndy Whitcroft		}
27495368df20SAndy Whitcroft
27504783f894SJosh Triplett# Check for FSF mailing addresses.
2751109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
27521bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
27533e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
27543e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
27554783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27564783f894SJosh Triplett			my $msg_type = \&ERROR;
27574783f894SJosh Triplett			$msg_type = \&CHK if ($file);
27584783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
27594783f894SJosh 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)
27604783f894SJosh Triplett		}
27614783f894SJosh Triplett
27623354957aSAndi Kleen# check for Kconfig help text having a real description
27639fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
27649fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
27653354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
27668d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
27673354957aSAndi Kleen			my $length = 0;
27689fe287d7SAndy Whitcroft			my $cnt = $realcnt;
27699fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
27709fe287d7SAndy Whitcroft			my $f;
2771a1385803SAndy Whitcroft			my $is_start = 0;
27729fe287d7SAndy Whitcroft			my $is_end = 0;
2773a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
27749fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
27759fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
27769fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
27779fe287d7SAndy Whitcroft
27789fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
27798d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2780a1385803SAndy Whitcroft
27818d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2782a1385803SAndy Whitcroft					$is_start = 1;
27838d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2784a1385803SAndy Whitcroft					$length = -1;
2785a1385803SAndy Whitcroft				}
2786a1385803SAndy Whitcroft
27879fe287d7SAndy Whitcroft				$f =~ s/^.//;
27883354957aSAndi Kleen				$f =~ s/#.*//;
27893354957aSAndi Kleen				$f =~ s/^\s+//;
27903354957aSAndi Kleen				next if ($f =~ /^$/);
27919fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
27929fe287d7SAndy Whitcroft					$is_end = 1;
27939fe287d7SAndy Whitcroft					last;
27949fe287d7SAndy Whitcroft				}
27953354957aSAndi Kleen				$length++;
27963354957aSAndi Kleen			}
279756193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2798000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
279956193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
280056193274SVadim Bendebury			}
2801a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
28023354957aSAndi Kleen		}
28033354957aSAndi Kleen
2804628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
2805628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
2806628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
2807628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
2808628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
2809628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2810628f91a2SJoe Perches			    $fix) {
2811628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2812628f91a2SJoe Perches			}
2813628f91a2SJoe Perches		}
2814628f91a2SJoe Perches
2815327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2816327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2817327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2818327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2819327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2820327953e9SChristoph Jaeger		}
2821327953e9SChristoph Jaeger
2822c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2823c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2824c68e5878SArnaud Lacombe			my $flag = $1;
2825c68e5878SArnaud Lacombe			my $replacement = {
2826c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2827c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2828c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2829c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2830c68e5878SArnaud Lacombe			};
2831c68e5878SArnaud Lacombe
2832c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2833c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2834c68e5878SArnaud Lacombe		}
2835c68e5878SArnaud Lacombe
2836bff5da43SRob Herring# check for DT compatible documentation
28377dd05b38SFlorian Vaussard		if (defined $root &&
28387dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
28397dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
28407dd05b38SFlorian Vaussard
2841bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2842bff5da43SRob Herring
2843cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2844cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2845cc93319bSFlorian Vaussard
2846bff5da43SRob Herring			foreach my $compat (@compats) {
2847bff5da43SRob Herring				my $compat2 = $compat;
2848185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2849185d566bSRob Herring				my $compat3 = $compat;
2850185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2851185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2852bff5da43SRob Herring				if ( $? >> 8 ) {
2853bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2854bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2855bff5da43SRob Herring				}
2856bff5da43SRob Herring
28574fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
28584fbf32a6SFlorian Vaussard				my $vendor = $1;
2859cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2860bff5da43SRob Herring				if ( $? >> 8 ) {
2861bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2862cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2863bff5da43SRob Herring				}
2864bff5da43SRob Herring			}
2865bff5da43SRob Herring		}
2866bff5da43SRob Herring
28675368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2868d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
28695368df20SAndy Whitcroft
287047e0c88bSJoe Perches# line length limit (with some exclusions)
287147e0c88bSJoe Perches#
287247e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
287347e0c88bSJoe Perches#	logging functions like pr_info that end in a string
287447e0c88bSJoe Perches#	lines with a single string
287547e0c88bSJoe Perches#	#defines that are a single string
287647e0c88bSJoe Perches#
287747e0c88bSJoe Perches# There are 3 different line length message types:
287847e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_linelength
287947e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
288047e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
288147e0c88bSJoe Perches#
288247e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
288347e0c88bSJoe Perches#
288447e0c88bSJoe Perches
2885b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
288647e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
288747e0c88bSJoe Perches
288847e0c88bSJoe Perches			# Check the allowed long line types first
288947e0c88bSJoe Perches
289047e0c88bSJoe Perches			# logging functions that end in a string that starts
289147e0c88bSJoe Perches			# before $max_line_length
289247e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
289347e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
289447e0c88bSJoe Perches				$msg_type = "";
289547e0c88bSJoe Perches
289647e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
289747e0c88bSJoe Perches			# #defines with only strings
289847e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
289947e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
290047e0c88bSJoe Perches				$msg_type = "";
290147e0c88bSJoe Perches
2902d560a5f8SJoe Perches			# EFI_GUID is another special case
2903d560a5f8SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2904d560a5f8SJoe Perches				$msg_type = "";
2905d560a5f8SJoe Perches
290647e0c88bSJoe Perches			# Otherwise set the alternate message types
290747e0c88bSJoe Perches
290847e0c88bSJoe Perches			# a comment starts before $max_line_length
290947e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
291047e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
291147e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
291247e0c88bSJoe Perches
291347e0c88bSJoe Perches			# a quoted string starts before $max_line_length
291447e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
291547e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
291647e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
291747e0c88bSJoe Perches			}
291847e0c88bSJoe Perches
291947e0c88bSJoe Perches			if ($msg_type ne "" &&
292047e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
292147e0c88bSJoe Perches				WARN($msg_type,
29226cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
29230a920b5bSAndy Whitcroft			}
292447e0c88bSJoe Perches		}
29250a920b5bSAndy Whitcroft
29268905a67cSAndy Whitcroft# check for adding lines without a newline.
29278905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2928000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2929000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
29308905a67cSAndy Whitcroft		}
29318905a67cSAndy Whitcroft
293242e41c54SMike Frysinger# Blackfin: use hi/lo macros
293342e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
293442e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
293542e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2936000d1cc1SJoe Perches				ERROR("LO_MACRO",
2937000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
293842e41c54SMike Frysinger			}
293942e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
294042e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2941000d1cc1SJoe Perches				ERROR("HI_MACRO",
2942000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
294342e41c54SMike Frysinger			}
294442e41c54SMike Frysinger		}
294542e41c54SMike Frysinger
2946b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2947de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
29480a920b5bSAndy Whitcroft
29490a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
29500a920b5bSAndy Whitcroft# more than 8 must use tabs.
2951c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2952c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2953c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2954d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29553705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
29563705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
29573705ce5bSJoe Perches			    $fix) {
2958194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
29593705ce5bSJoe Perches			}
29600a920b5bSAndy Whitcroft		}
29610a920b5bSAndy Whitcroft
296208e44365SAlberto Panizzo# check for space before tabs.
296308e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
296408e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29653705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
29663705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
29673705ce5bSJoe Perches			    $fix) {
2968194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2969d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2970194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2971c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
29723705ce5bSJoe Perches			}
297308e44365SAlberto Panizzo		}
297408e44365SAlberto Panizzo
2975d1fe9c09SJoe Perches# check for && or || at the start of a line
2976d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2977d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2978d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2979d1fe9c09SJoe Perches		}
2980d1fe9c09SJoe Perches
2981a91e8994SJoe Perches# check indentation starts on a tab stop
2982a91e8994SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2983a91e8994SJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2984a91e8994SJoe Perches			my $indent = length($1);
2985a91e8994SJoe Perches			if ($indent % 8) {
2986a91e8994SJoe Perches				if (WARN("TABSTOP",
2987a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
2988a91e8994SJoe Perches				    $fix) {
2989a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2990a91e8994SJoe Perches				}
2991a91e8994SJoe Perches			}
2992a91e8994SJoe Perches		}
2993a91e8994SJoe Perches
2994d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2995d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2996fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2997d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2998d1fe9c09SJoe Perches			my $oldindent = $1;
2999d1fe9c09SJoe Perches			my $rest = $2;
3000d1fe9c09SJoe Perches
3001d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3002d1fe9c09SJoe Perches			if ($pos >= 0) {
3003b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3004b34a26f3SJoe Perches				my $newindent = $2;
3005d1fe9c09SJoe Perches
3006d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3007d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3008d1fe9c09SJoe Perches					" "  x ($pos % 8);
3009d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3010d1fe9c09SJoe Perches
3011d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3012d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
30133705ce5bSJoe Perches
30143705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
30153705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
30163705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3017194f66fcSJoe Perches						$fixed[$fixlinenr] =~
30183705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
30193705ce5bSJoe Perches					}
3020d1fe9c09SJoe Perches				}
3021d1fe9c09SJoe Perches			}
3022d1fe9c09SJoe Perches		}
3023d1fe9c09SJoe Perches
30246ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
30256ab3a970SJoe Perches# avoid checking a few false positives:
30266ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
30276ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
30286ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
30296ab3a970SJoe Perches#   multiline macros that define functions
30306ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
30316ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
30326ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
30333705ce5bSJoe Perches			if (CHK("SPACING",
3034f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
30353705ce5bSJoe Perches			    $fix) {
3036194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3037f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
30383705ce5bSJoe Perches			}
3039aad4f614SJoe Perches		}
3040aad4f614SJoe Perches
304186406b1cSJoe Perches# Block comment styles
304286406b1cSJoe Perches# Networking with an initial /*
304305880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3044fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
304585ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
304685ad978cSJoe Perches		    $realline > 2) {
304705880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
304805880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
304905880600SJoe Perches		}
305005880600SJoe Perches
305186406b1cSJoe Perches# Block comments use * on subsequent lines
305286406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
305386406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3054a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
305561135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3056a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
305786406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
305886406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3059a605e32eSJoe Perches		}
3060a605e32eSJoe Perches
306186406b1cSJoe Perches# Block comments use */ on trailing lines
306286406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3063c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3064c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3065c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
306686406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
306786406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
306805880600SJoe Perches		}
306905880600SJoe Perches
307008eb9b80SJoe Perches# Block comment * alignment
307108eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3072af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3073af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3074af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
307508eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3076af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3077af207524SJoe Perches			my $oldindent;
307808eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3079af207524SJoe Perches			if (defined($1)) {
3080af207524SJoe Perches				$oldindent = expand_tabs($1);
3081af207524SJoe Perches			} else {
3082af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3083af207524SJoe Perches				$oldindent = expand_tabs($1);
3084af207524SJoe Perches			}
308508eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
308608eb9b80SJoe Perches			my $newindent = $1;
308708eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3088af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
308908eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
309008eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
309108eb9b80SJoe Perches			}
309208eb9b80SJoe Perches		}
309308eb9b80SJoe Perches
30947f619191SJoe Perches# check for missing blank lines after struct/union declarations
30957f619191SJoe Perches# with exceptions for various attributes and macros
30967f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
30977f619191SJoe Perches		    $line =~ /^\+/ &&
30987f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
30997f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
31007f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
31017f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
31027f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
31037f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
31047f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
31057f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3106d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3107d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3108d752fcc8SJoe Perches			    $fix) {
3109f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3110d752fcc8SJoe Perches			}
31117f619191SJoe Perches		}
31127f619191SJoe Perches
3113365dd4eaSJoe Perches# check for multiple consecutive blank lines
3114365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3115365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3116365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3117d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3118d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3119d752fcc8SJoe Perches			    $fix) {
3120f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3121d752fcc8SJoe Perches			}
3122d752fcc8SJoe Perches
3123365dd4eaSJoe Perches			$last_blank_line = $linenr;
3124365dd4eaSJoe Perches		}
3125365dd4eaSJoe Perches
31263b617e3bSJoe Perches# check for missing blank lines after declarations
31273f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
31283f7bac03SJoe Perches			# actual declarations
31293f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
31305a4e1fd3SJoe Perches			# function pointer declarations
31315a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
31323f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
31333f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
31343f7bac03SJoe Perches			# known declaration macros
31353f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
31363f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
31373f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
31383f7bac03SJoe Perches			# other possible extensions of declaration lines
31393f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
31403f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
31413f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
31423f7bac03SJoe Perches			# looks like a declaration
31433f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
31445a4e1fd3SJoe Perches			# function pointer declarations
31455a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
31463f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
31473f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
31483f7bac03SJoe Perches			# known declaration macros
31493f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
31503f7bac03SJoe Perches			# start of struct or union or enum
31513b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
31523f7bac03SJoe Perches			# start or end of block or continuation of declaration
31533f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
31543f7bac03SJoe Perches			# bitfield continuation
31553f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
31563f7bac03SJoe Perches			# other possible extensions of declaration lines
31573f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
31583f7bac03SJoe Perches			# indentation of previous and current line are the same
31593f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3160d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3161d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3162d752fcc8SJoe Perches			    $fix) {
3163f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3164d752fcc8SJoe Perches			}
31653b617e3bSJoe Perches		}
31663b617e3bSJoe Perches
31675f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
31686b4c5bebSAndy Whitcroft# Exceptions:
31696b4c5bebSAndy Whitcroft#  1) within comments
31706b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
31716b4c5bebSAndy Whitcroft#  3) hanging labels
31723705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
31735f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
31743705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
31753705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
31763705ce5bSJoe Perches			    $fix) {
3177194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
31783705ce5bSJoe Perches			}
31795f7ddae6SRaffaele Recalcati		}
31805f7ddae6SRaffaele Recalcati
3181b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3182b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3183b9ea10d6SAndy Whitcroft
31844dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
31854dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
31864dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
31874dbed76fSJoe Perches			$context_function = $1;
31884dbed76fSJoe Perches		}
31894dbed76fSJoe Perches
31904dbed76fSJoe Perches# check if this appears to be the end of function declaration
31914dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
31924dbed76fSJoe Perches			undef $context_function;
31934dbed76fSJoe Perches		}
31944dbed76fSJoe Perches
3195032a4c0fSJoe Perches# check indentation of any line with a bare else
3196840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3197032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3198032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3199032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3200840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3201840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3202840080a0SJoe Perches			     defined $lines[$linenr] &&
3203840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3204032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3205032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3206032a4c0fSJoe Perches			}
3207032a4c0fSJoe Perches		}
3208032a4c0fSJoe Perches
3209c00df19aSJoe Perches# check indentation of a line with a break;
3210c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3211c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3212c00df19aSJoe Perches			my $tabs = $1;
3213c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3214c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3215c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3216c00df19aSJoe Perches			}
3217c00df19aSJoe Perches		}
3218c00df19aSJoe Perches
3219c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3220cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3221000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3222000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3223c2fdda0dSAndy Whitcroft		}
322422f2a2efSAndy Whitcroft
322542e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
322642e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
322742e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3228000d1cc1SJoe Perches			ERROR("CSYNC",
3229000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
323042e41c54SMike Frysinger		}
323142e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
323242e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3233000d1cc1SJoe Perches			ERROR("SSYNC",
3234000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
323542e41c54SMike Frysinger		}
323642e41c54SMike Frysinger
323756e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
323856e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
323956e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
324056e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
324156e77d70SJoe Perches		}
324256e77d70SJoe Perches
32439c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
32442b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
32452b474a1aSAndy Whitcroft		    $realline_next);
32463e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3247ca819864SJoe Perches		if ($linenr > $suppress_statement &&
32481b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3249170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3250f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3251171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3252171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3253171ae1a4SAndy Whitcroft
32543e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
32553e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
32563e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
32573e469cdcSAndy Whitcroft			# until we hit end of it.
32583e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
32593e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
32603e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
32613e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
32623e469cdcSAndy Whitcroft			}
3263f74bd194SAndy Whitcroft
32642b474a1aSAndy Whitcroft			# Find the real next line.
32652b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
32662b474a1aSAndy Whitcroft			if (defined $realline_next &&
32672b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
32682b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
32692b474a1aSAndy Whitcroft				$realline_next++;
32702b474a1aSAndy Whitcroft			}
32712b474a1aSAndy Whitcroft
3272171ae1a4SAndy Whitcroft			my $s = $stat;
3273171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3274cf655043SAndy Whitcroft
3275c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3276171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3277c2fdda0dSAndy Whitcroft
3278c2fdda0dSAndy Whitcroft			# Ignore functions being called
3279171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3280c2fdda0dSAndy Whitcroft
3281463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3282463f2864SAndy Whitcroft
3283c45dcabdSAndy Whitcroft			# declarations always start with types
3284d2506586SAndy 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) {
3285c45dcabdSAndy Whitcroft				my $type = $1;
3286c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3287c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3288c45dcabdSAndy Whitcroft
32896c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3290a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3291c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3292c2fdda0dSAndy Whitcroft			}
32938905a67cSAndy Whitcroft
32946c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
329565863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3296c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
32979c0ca6f9SAndy Whitcroft			}
32988905a67cSAndy Whitcroft
32998905a67cSAndy Whitcroft			# Check for any sort of function declaration.
33008905a67cSAndy Whitcroft			# int foo(something bar, other baz);
33018905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3302171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
33038905a67cSAndy Whitcroft				my ($name_len) = length($1);
33048905a67cSAndy Whitcroft
3305cf655043SAndy Whitcroft				my $ctx = $s;
3306773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
33078905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3308cf655043SAndy Whitcroft
33098905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3310c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
33118905a67cSAndy Whitcroft
3312c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
33138905a67cSAndy Whitcroft					}
33148905a67cSAndy Whitcroft				}
33158905a67cSAndy Whitcroft			}
33168905a67cSAndy Whitcroft
33179c0ca6f9SAndy Whitcroft		}
33189c0ca6f9SAndy Whitcroft
331900df344fSAndy Whitcroft#
332000df344fSAndy Whitcroft# Checks which may be anchored in the context.
332100df344fSAndy Whitcroft#
332200df344fSAndy Whitcroft
332300df344fSAndy Whitcroft# Check for switch () and associated case and default
332400df344fSAndy Whitcroft# statements should be at the same indent.
332500df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
332600df344fSAndy Whitcroft			my $err = '';
332700df344fSAndy Whitcroft			my $sep = '';
332800df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
332900df344fSAndy Whitcroft			shift(@ctx);
333000df344fSAndy Whitcroft			for my $ctx (@ctx) {
333100df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
333200df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
333300df344fSAndy Whitcroft							$indent != $cindent) {
333400df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
333500df344fSAndy Whitcroft					$sep = '';
333600df344fSAndy Whitcroft				} else {
333700df344fSAndy Whitcroft					$sep = "[...]\n";
333800df344fSAndy Whitcroft				}
333900df344fSAndy Whitcroft			}
334000df344fSAndy Whitcroft			if ($err ne '') {
3341000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3342000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3343de7d4f0eSAndy Whitcroft			}
3344de7d4f0eSAndy Whitcroft		}
3345de7d4f0eSAndy Whitcroft
3346de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3347de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
33480fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3349773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3350773647a0SAndy Whitcroft
33519c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
33528eef05ddSJoe Perches
33538eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
33548eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
33558eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
33568eef05ddSJoe Perches			}
33578eef05ddSJoe Perches
3358de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3359de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3360de7d4f0eSAndy Whitcroft
3361548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3362548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3363de7d4f0eSAndy Whitcroft
3364548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3365548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3366548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3367548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3368548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3369773647a0SAndy Whitcroft				$ctx_ln++;
3370773647a0SAndy Whitcroft			}
3371548596d5SAndy Whitcroft
337253210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
337353210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3374773647a0SAndy Whitcroft
3375773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3376000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3377000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
337801464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
337900df344fSAndy Whitcroft			}
3380773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3381773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3382773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3383773647a0SAndy Whitcroft			{
33849c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
33859c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3386000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3387000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
338801464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
33899c0ca6f9SAndy Whitcroft				}
33909c0ca6f9SAndy Whitcroft			}
339100df344fSAndy Whitcroft		}
339200df344fSAndy Whitcroft
33934d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3394f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
33953e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
33963e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
33973e469cdcSAndy Whitcroft					if (!defined $stat);
33984d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
33994d001e4dSAndy Whitcroft
34004d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
34014d001e4dSAndy Whitcroft
34029f5af480SJoe Perches			# remove inline comments
34039f5af480SJoe Perches			$s =~ s/$;/ /g;
34049f5af480SJoe Perches			$c =~ s/$;/ /g;
34054d001e4dSAndy Whitcroft
34064d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
34076f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
34086f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
34094d001e4dSAndy Whitcroft
34109f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
34119f5af480SJoe Perches			# none on the first line, and are going to readd them
34129f5af480SJoe Perches			# where necessary.
34139f5af480SJoe Perches			$s =~ s/\n./\n/gs;
34149f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
34159f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
34169f5af480SJoe Perches			}
34179f5af480SJoe Perches
34184d001e4dSAndy Whitcroft			# We want to check the first line inside the block
34194d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
34204d001e4dSAndy Whitcroft			#  1) any blank line termination
34214d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
34224d001e4dSAndy Whitcroft			#  3) any do (...) {
34234d001e4dSAndy Whitcroft			my $continuation = 0;
34244d001e4dSAndy Whitcroft			my $check = 0;
34254d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
34264d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
34274d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
34284d001e4dSAndy Whitcroft				$continuation = 1;
34294d001e4dSAndy Whitcroft			}
34309bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
34314d001e4dSAndy Whitcroft				$check = 1;
34324d001e4dSAndy Whitcroft				$cond_lines++;
34334d001e4dSAndy Whitcroft			}
34344d001e4dSAndy Whitcroft
34354d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
34364d001e4dSAndy Whitcroft			# preprocessor statement.
34374d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
34384d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
34394d001e4dSAndy Whitcroft				$check = 0;
34404d001e4dSAndy Whitcroft			}
34414d001e4dSAndy Whitcroft
34429bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3443740504c6SAndy Whitcroft			$continuation = 0;
34449bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
34459bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
34464d001e4dSAndy Whitcroft
3447f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3448f16fa28fSAndy Whitcroft				# is not linear.
3449f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3450f16fa28fSAndy Whitcroft					$check = 0;
3451f16fa28fSAndy Whitcroft				}
3452f16fa28fSAndy Whitcroft
34539bd49efeSAndy Whitcroft				# Ignore:
34549bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
34559bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
34569bd49efeSAndy Whitcroft				#  3) labels.
3457740504c6SAndy Whitcroft				if ($continuation ||
3458740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
34599bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
34609bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3461740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
346230dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
34639bd49efeSAndy Whitcroft						$cond_lines++;
34649bd49efeSAndy Whitcroft					}
34654d001e4dSAndy Whitcroft				}
346630dad6ebSAndy Whitcroft			}
34674d001e4dSAndy Whitcroft
34684d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
34694d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
34704d001e4dSAndy Whitcroft
34714d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
34724d001e4dSAndy Whitcroft			# this is not this patch's fault.
34734d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
34744d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
34754d001e4dSAndy Whitcroft				$check = 0;
34764d001e4dSAndy Whitcroft			}
34774d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
34784d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
34794d001e4dSAndy Whitcroft			}
34804d001e4dSAndy Whitcroft
34819bd49efeSAndy 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";
34824d001e4dSAndy Whitcroft
34839f5af480SJoe Perches			if ($check && $s ne '' &&
34849f5af480SJoe Perches			    (($sindent % 8) != 0 ||
34859f5af480SJoe Perches			     ($sindent < $indent) ||
3486f6950a73SJoe Perches			     ($sindent == $indent &&
3487f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
34889f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3489000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3490000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
34914d001e4dSAndy Whitcroft			}
34924d001e4dSAndy Whitcroft		}
34934d001e4dSAndy Whitcroft
34946c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
34956c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
34961f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
34971f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
34986c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3499c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3500c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3501cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3502cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
35031f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3504c2fdda0dSAndy Whitcroft		}
35056c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
35066c72ffaaSAndy Whitcroft
350700df344fSAndy Whitcroft#ignore lines not being added
35083705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
350900df344fSAndy Whitcroft
351011ca40a0SJoe Perches# check for dereferences that span multiple lines
351111ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
351211ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
351311ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
351411ca40a0SJoe Perches			my $ref = $1;
351511ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
351611ca40a0SJoe Perches			$ref .= $1;
351711ca40a0SJoe Perches			$ref =~ s/\s//g;
351811ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
351911ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
352011ca40a0SJoe Perches		}
352111ca40a0SJoe Perches
3522a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3523c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3524a1ce18e4SJoe Perches			my $type = $1;
3525a1ce18e4SJoe Perches			my $var = $2;
3526207a8e84SJoe Perches			$var = "" if (!defined $var);
3527207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3528a1ce18e4SJoe Perches				my $sign = $1;
3529a1ce18e4SJoe Perches				my $pointer = $2;
3530a1ce18e4SJoe Perches
3531a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3532a1ce18e4SJoe Perches
3533a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3534a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3535a1ce18e4SJoe Perches				    $fix) {
3536a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3537207a8e84SJoe Perches					my $comp_pointer = $pointer;
3538207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3539207a8e84SJoe Perches					$decl .= $comp_pointer;
3540207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3541207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3542a1ce18e4SJoe Perches				}
3543a1ce18e4SJoe Perches			}
3544a1ce18e4SJoe Perches		}
3545a1ce18e4SJoe Perches
3546653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
35477429c690SAndy Whitcroft		if ($dbg_type) {
35487429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3549000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3550000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
35517429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3552000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3553000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
35547429c690SAndy Whitcroft			}
3555653d4876SAndy Whitcroft			next;
3556653d4876SAndy Whitcroft		}
3557a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3558a1ef277eSAndy Whitcroft		if ($dbg_attr) {
35599360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3560000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3561000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
35629360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3563000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3564000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3565a1ef277eSAndy Whitcroft			}
3566a1ef277eSAndy Whitcroft			next;
3567a1ef277eSAndy Whitcroft		}
3568653d4876SAndy Whitcroft
3569f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
357099423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
357199423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3572d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3573d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3574f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3575f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3576f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3577d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3578d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3579f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3580d752fcc8SJoe Perches				$fixedline = $line;
35818d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3582f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3583d752fcc8SJoe Perches			}
3584f0a594c1SAndy Whitcroft		}
3585f0a594c1SAndy Whitcroft
358600df344fSAndy Whitcroft#
358700df344fSAndy Whitcroft# Checks which are anchored on the added line.
358800df344fSAndy Whitcroft#
358900df344fSAndy Whitcroft
3590653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3591c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3592653d4876SAndy Whitcroft			my $path = $1;
3593653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3594000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3595495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3596495e9d84SJoe Perches			}
3597495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3598495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3599495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3600653d4876SAndy Whitcroft			}
3601653d4876SAndy Whitcroft		}
3602653d4876SAndy Whitcroft
360300df344fSAndy Whitcroft# no C99 // comments
360400df344fSAndy Whitcroft		if ($line =~ m{//}) {
36053705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
36063705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
36073705ce5bSJoe Perches			    $fix) {
3608194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
36093705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
36103705ce5bSJoe Perches					my $comment = trim($1);
3611194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
36123705ce5bSJoe Perches				}
36133705ce5bSJoe Perches			}
361400df344fSAndy Whitcroft		}
361500df344fSAndy Whitcroft		# Remove C99 comments.
36160a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
36176c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
36180a920b5bSAndy Whitcroft
36192b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
36202b474a1aSAndy Whitcroft# the whole statement.
36212b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
36222b474a1aSAndy Whitcroft		if (defined $realline_next &&
36232b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
36242b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
36252b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
36262b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
36273cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
36283cbf62dfSAndy Whitcroft			# a prefix:
36293cbf62dfSAndy Whitcroft			#   XXX(foo);
36303cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3631653d4876SAndy Whitcroft			my $name = $1;
363287a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
36333cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
36343cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
36353cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
36363cbf62dfSAndy Whitcroft
36373cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
36382b474a1aSAndy Whitcroft				\n.}\s*$|
363948012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
364048012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
364148012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
36422b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
36432b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
364448012058SAndy Whitcroft			    )/x) {
36452b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
36462b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
36472b474a1aSAndy Whitcroft			} else {
36482b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
36490a920b5bSAndy Whitcroft			}
36500a920b5bSAndy Whitcroft		}
36512b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
36522b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
36532b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
36542b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
36552b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
36562b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
36572b474a1aSAndy Whitcroft		}
36582b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
36592b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3660000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3661000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
36622b474a1aSAndy Whitcroft		}
36630a920b5bSAndy Whitcroft
36645150bda4SJoe Eloff# check for global initialisers.
36656d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3666d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
36676d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3668d5e616fcSJoe Perches			    $fix) {
36696d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3670d5e616fcSJoe Perches			}
3671f0a594c1SAndy Whitcroft		}
36720a920b5bSAndy Whitcroft# check for static initialisers.
36736d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3674d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
36756d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3676d5e616fcSJoe Perches				      $herecurr) &&
3677d5e616fcSJoe Perches			    $fix) {
36786d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3679d5e616fcSJoe Perches			}
36800a920b5bSAndy Whitcroft		}
36810a920b5bSAndy Whitcroft
36821813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
36831813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
36841813087dSJoe Perches			my $tmp = trim($1);
36851813087dSJoe Perches			WARN("MISORDERED_TYPE",
36861813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
36871813087dSJoe Perches		}
36881813087dSJoe Perches
3689cb710ecaSJoe Perches# check for static const char * arrays.
3690cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3691000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3692000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3693cb710ecaSJoe Perches				$herecurr);
3694cb710ecaSJoe Perches               }
3695cb710ecaSJoe Perches
3696cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3697cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3698000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3699000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3700cb710ecaSJoe Perches				$herecurr);
3701cb710ecaSJoe Perches               }
3702cb710ecaSJoe Perches
3703ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3704ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3705ab7e23f3SJoe Perches			my $found = $1;
3706ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3707ab7e23f3SJoe Perches				WARN("CONST_CONST",
3708ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3709ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3710ab7e23f3SJoe Perches				WARN("CONST_CONST",
3711ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3712ab7e23f3SJoe Perches			}
3713ab7e23f3SJoe Perches		}
3714ab7e23f3SJoe Perches
37159b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
37169b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
37179b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
37189b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
37199b0fa60dSJoe Perches				$herecurr);
37209b0fa60dSJoe Perches               }
37219b0fa60dSJoe Perches
3722b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3723b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3724b598b670SJoe Perches			my $array = $1;
3725b598b670SJoe 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*\))@) {
3726b598b670SJoe Perches				my $array_div = $1;
3727b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3728b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3729b598b670SJoe Perches				    $fix) {
3730b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3731b598b670SJoe Perches				}
3732b598b670SJoe Perches			}
3733b598b670SJoe Perches		}
3734b598b670SJoe Perches
3735b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3736b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3737b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3738b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3739b36190c5SJoe Perches			    $fix) {
3740194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3741b36190c5SJoe Perches			}
3742b36190c5SJoe Perches		}
3743b36190c5SJoe Perches
3744653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3745653d4876SAndy Whitcroft# make sense.
3746653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
37478054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3748c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
37498ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
375046d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
3751000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3752000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
37530a920b5bSAndy Whitcroft		}
37540a920b5bSAndy Whitcroft
37550a920b5bSAndy Whitcroft# * goes on variable not on type
375665863862SAndy Whitcroft		# (char*[ const])
3757bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3758bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
37593705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3760d8aaf121SAndy Whitcroft
376165863862SAndy Whitcroft			# Should start with a space.
376265863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
376365863862SAndy Whitcroft			# Should not end with a space.
376465863862SAndy Whitcroft			$to =~ s/\s+$//;
376565863862SAndy Whitcroft			# '*'s should not have spaces between.
3766f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
376765863862SAndy Whitcroft			}
3768d8aaf121SAndy Whitcroft
37693705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
377065863862SAndy Whitcroft			if ($from ne $to) {
37713705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
37723705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
37733705ce5bSJoe Perches				    $fix) {
37743705ce5bSJoe Perches					my $sub_from = $ident;
37753705ce5bSJoe Perches					my $sub_to = $ident;
37763705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3777194f66fcSJoe Perches					$fixed[$fixlinenr] =~
37783705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
37793705ce5bSJoe Perches				}
378065863862SAndy Whitcroft			}
3781bfcb2cc7SAndy Whitcroft		}
3782bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3783bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
37843705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3785d8aaf121SAndy Whitcroft
378665863862SAndy Whitcroft			# Should start with a space.
378765863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
378865863862SAndy Whitcroft			# Should not end with a space.
378965863862SAndy Whitcroft			$to =~ s/\s+$//;
379065863862SAndy Whitcroft			# '*'s should not have spaces between.
3791f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
379265863862SAndy Whitcroft			}
379365863862SAndy Whitcroft			# Modifiers should have spaces.
379465863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
379565863862SAndy Whitcroft
37963705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3797667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
37983705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
37993705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
38003705ce5bSJoe Perches				    $fix) {
38013705ce5bSJoe Perches
38023705ce5bSJoe Perches					my $sub_from = $match;
38033705ce5bSJoe Perches					my $sub_to = $match;
38043705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3805194f66fcSJoe Perches					$fixed[$fixlinenr] =~
38063705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
38073705ce5bSJoe Perches				}
380865863862SAndy Whitcroft			}
38090a920b5bSAndy Whitcroft		}
38100a920b5bSAndy Whitcroft
38119d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
38129d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
38139d3e3c70SJoe Perches			my $msg_type = \&WARN;
38149d3e3c70SJoe Perches			$msg_type = \&CHK if ($file);
38159d3e3c70SJoe Perches			&{$msg_type}("AVOID_BUG",
38169d3e3c70SJoe Perches				     "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
38179d3e3c70SJoe Perches		}
38180a920b5bSAndy Whitcroft
38199d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
38208905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3821000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3822000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
38238905a67cSAndy Whitcroft		}
38248905a67cSAndy Whitcroft
382517441227SJoe Perches# check for uses of printk_ratelimit
382617441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3827000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3828000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
382917441227SJoe Perches		}
383017441227SJoe Perches
383100df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
383200df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
383300df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
383425985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
383500df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3836f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
383700df344fSAndy Whitcroft			my $ok = 0;
383800df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
383900df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
384025985edcSLucas De Marchi				# we have a preceding printk if it ends
384100df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
384200df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
384300df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
384400df344fSAndy Whitcroft						$ok = 1;
384500df344fSAndy Whitcroft					}
384600df344fSAndy Whitcroft					last;
384700df344fSAndy Whitcroft				}
384800df344fSAndy Whitcroft			}
384900df344fSAndy Whitcroft			if ($ok == 0) {
3850000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3851000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
38520a920b5bSAndy Whitcroft			}
385300df344fSAndy Whitcroft		}
38540a920b5bSAndy Whitcroft
3855243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3856243f3803SJoe Perches			my $orig = $1;
3857243f3803SJoe Perches			my $level = lc($orig);
3858243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
38598f26b837SJoe Perches			my $level2 = $level;
38608f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3861243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3862daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3863243f3803SJoe Perches		}
3864243f3803SJoe Perches
3865243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3866d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3867d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3868d5e616fcSJoe Perches			    $fix) {
3869194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3870d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3871d5e616fcSJoe Perches			}
3872243f3803SJoe Perches		}
3873243f3803SJoe Perches
3874dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3875dc139313SJoe Perches			my $orig = $1;
3876dc139313SJoe Perches			my $level = lc($orig);
3877dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3878dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3879dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3880dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3881dc139313SJoe Perches		}
3882dc139313SJoe Perches
388391c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
388491c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
388591c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
388691c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
388791c9afafSAndy Lutomirski			WARN("ENOSYS",
388891c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
388991c9afafSAndy Lutomirski		}
389091c9afafSAndy Lutomirski
3891653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3892653d4876SAndy Whitcroft# or if closed on same line
38938d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
38944e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
38958d182478SJoe Perches			if (ERROR("OPEN_BRACE",
38968d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
38978d182478SJoe Perches			    $fix) {
38988d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
38998d182478SJoe Perches				my $fixed_line = $rawline;
39008d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
39018d182478SJoe Perches				my $line1 = $1;
39028d182478SJoe Perches				my $line2 = $2;
39038d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
39048d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
39058d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
39068d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
39078d182478SJoe Perches				}
39088d182478SJoe Perches			}
39090a920b5bSAndy Whitcroft		}
3910653d4876SAndy Whitcroft
39118905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
39128905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
39138905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
39148d182478SJoe Perches			if (ERROR("OPEN_BRACE",
39158d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
39168d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
39178d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
39188d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
39198d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
39208d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
39218d182478SJoe Perches				$fixedline = $rawline;
39228d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
39238d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
39248d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
39258d182478SJoe Perches				}
39268d182478SJoe Perches			}
39278905a67cSAndy Whitcroft		}
39288905a67cSAndy Whitcroft
39290c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
39303705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
39313705ce5bSJoe Perches			if (WARN("SPACING",
39323705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
39333705ce5bSJoe Perches			    $fix) {
3934194f66fcSJoe Perches				$fixed[$fixlinenr] =~
39353705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
39363705ce5bSJoe Perches			}
39370c73b4ebSAndy Whitcroft		}
39380c73b4ebSAndy Whitcroft
393931070b5dSJoe Perches# Function pointer declarations
394031070b5dSJoe Perches# check spacing between type, funcptr, and args
394131070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
394291f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
394331070b5dSJoe Perches			my $declare = $1;
394431070b5dSJoe Perches			my $pre_pointer_space = $2;
394531070b5dSJoe Perches			my $post_pointer_space = $3;
394631070b5dSJoe Perches			my $funcname = $4;
394731070b5dSJoe Perches			my $post_funcname_space = $5;
394831070b5dSJoe Perches			my $pre_args_space = $6;
394931070b5dSJoe Perches
395091f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
395191f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
395291f72e9cSJoe Perches# don't need a space so don't warn for those.
395391f72e9cSJoe Perches			my $post_declare_space = "";
395491f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
395591f72e9cSJoe Perches				$post_declare_space = $1;
395691f72e9cSJoe Perches				$declare = rtrim($declare);
395791f72e9cSJoe Perches			}
395891f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
395931070b5dSJoe Perches				WARN("SPACING",
396031070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
396191f72e9cSJoe Perches				$post_declare_space = " ";
396231070b5dSJoe Perches			}
396331070b5dSJoe Perches
396431070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
396591f72e9cSJoe Perches# This test is not currently implemented because these declarations are
396691f72e9cSJoe Perches# equivalent to
396791f72e9cSJoe Perches#	int  foo(int bar, ...)
396891f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
396991f72e9cSJoe Perches#
397091f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
397191f72e9cSJoe Perches#				WARN("SPACING",
397291f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
397391f72e9cSJoe Perches#			}
397431070b5dSJoe Perches
397531070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
397631070b5dSJoe Perches			if (defined $pre_pointer_space &&
397731070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
397831070b5dSJoe Perches				WARN("SPACING",
397931070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
398031070b5dSJoe Perches			}
398131070b5dSJoe Perches
398231070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
398331070b5dSJoe Perches			if (defined $post_pointer_space &&
398431070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
398531070b5dSJoe Perches				WARN("SPACING",
398631070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
398731070b5dSJoe Perches			}
398831070b5dSJoe Perches
398931070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
399031070b5dSJoe Perches			if (defined $post_funcname_space &&
399131070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
399231070b5dSJoe Perches				WARN("SPACING",
399331070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
399431070b5dSJoe Perches			}
399531070b5dSJoe Perches
399631070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
399731070b5dSJoe Perches			if (defined $pre_args_space &&
399831070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
399931070b5dSJoe Perches				WARN("SPACING",
400031070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
400131070b5dSJoe Perches			}
400231070b5dSJoe Perches
400331070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4004194f66fcSJoe Perches				$fixed[$fixlinenr] =~
400591f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
400631070b5dSJoe Perches			}
400731070b5dSJoe Perches		}
400831070b5dSJoe Perches
40098d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
40108d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4011fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4012fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
40138d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
40148d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
40158d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4016fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
4017daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
40183705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
40193705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
40203705ce5bSJoe Perches				    $fix) {
4021194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
40223705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
40233705ce5bSJoe Perches				}
40248d31cfceSAndy Whitcroft			}
40258d31cfceSAndy Whitcroft		}
40268d31cfceSAndy Whitcroft
4027f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
40286c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4029c2fdda0dSAndy Whitcroft			my $name = $1;
4030773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4031773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4032c2fdda0dSAndy Whitcroft
4033c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4034773647a0SAndy Whitcroft			if ($name =~ /^(?:
4035773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4036773647a0SAndy Whitcroft				volatile|__volatile__|
4037773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4038773647a0SAndy Whitcroft				asm|__asm__)$/x)
4039773647a0SAndy Whitcroft			{
4040c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4041c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4042c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4043c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4044773647a0SAndy Whitcroft
4045773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4046c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4047c2fdda0dSAndy Whitcroft
4048c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4049c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4050773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4051c2fdda0dSAndy Whitcroft
4052c2fdda0dSAndy Whitcroft			} else {
40533705ce5bSJoe Perches				if (WARN("SPACING",
40543705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
40553705ce5bSJoe Perches					     $fix) {
4056194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40573705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
40583705ce5bSJoe Perches				}
4059f0a594c1SAndy Whitcroft			}
40606c72ffaaSAndy Whitcroft		}
40619a4cad4eSEric Nelson
4062653d4876SAndy Whitcroft# Check operator spacing.
40630a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
40643705ce5bSJoe Perches			my $fixed_line = "";
40653705ce5bSJoe Perches			my $line_fixed = 0;
40663705ce5bSJoe Perches
40679c0ca6f9SAndy Whitcroft			my $ops = qr{
40689c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
40699c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
40709c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
40711f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
407284731623SJoe Perches				\?:|\?|:
40739c0ca6f9SAndy Whitcroft			}x;
4074cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
40753705ce5bSJoe Perches
40763705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
40773705ce5bSJoe Perches##			foreach my $el (@elements) {
40783705ce5bSJoe Perches##				print("el: <$el>\n");
40793705ce5bSJoe Perches##			}
40803705ce5bSJoe Perches
40813705ce5bSJoe Perches			my @fix_elements = ();
408200df344fSAndy Whitcroft			my $off = 0;
40836c72ffaaSAndy Whitcroft
40843705ce5bSJoe Perches			foreach my $el (@elements) {
40853705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
40863705ce5bSJoe Perches				$off += length($el);
40873705ce5bSJoe Perches			}
40883705ce5bSJoe Perches
40893705ce5bSJoe Perches			$off = 0;
40903705ce5bSJoe Perches
40916c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4092b34c648bSJoe Perches			my $last_after = -1;
40936c72ffaaSAndy Whitcroft
40940a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
40953705ce5bSJoe Perches
40963705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
40973705ce5bSJoe Perches
40983705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
40993705ce5bSJoe Perches
41004a0df2efSAndy Whitcroft				$off += length($elements[$n]);
41014a0df2efSAndy Whitcroft
410225985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4103773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4104773647a0SAndy Whitcroft				my $cc = '';
4105773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4106773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4107773647a0SAndy Whitcroft				}
4108773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4109773647a0SAndy Whitcroft
41104a0df2efSAndy Whitcroft				my $a = '';
41114a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
41124a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4113cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
41144a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
41154a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4116773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
41174a0df2efSAndy Whitcroft
41180a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
41194a0df2efSAndy Whitcroft
41204a0df2efSAndy Whitcroft				my $c = '';
41210a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
41224a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
41234a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4124cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
41254a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
41264a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
41278b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
41284a0df2efSAndy Whitcroft				} else {
41294a0df2efSAndy Whitcroft					$c = 'E';
41300a920b5bSAndy Whitcroft				}
41310a920b5bSAndy Whitcroft
41324a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
41334a0df2efSAndy Whitcroft
41344a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
41354a0df2efSAndy Whitcroft
41366c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4137de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
41380a920b5bSAndy Whitcroft
413974048ed8SAndy Whitcroft				# Pull out the value of this operator.
41406c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
41410a920b5bSAndy Whitcroft
41421f65f947SAndy Whitcroft				# Get the full operator variant.
41431f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
41441f65f947SAndy Whitcroft
414513214adfSAndy Whitcroft				# Ignore operators passed as parameters.
414613214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4147d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
414813214adfSAndy Whitcroft
4149cf655043SAndy Whitcroft#				# Ignore comments
4150cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
415113214adfSAndy Whitcroft
4152d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
415313214adfSAndy Whitcroft				} elsif ($op eq ';') {
4154cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4155cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
41563705ce5bSJoe Perches						if (ERROR("SPACING",
41573705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4158b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
41593705ce5bSJoe Perches							$line_fixed = 1;
41603705ce5bSJoe Perches						}
4161d8aaf121SAndy Whitcroft					}
4162d8aaf121SAndy Whitcroft
4163d8aaf121SAndy Whitcroft				# // is a comment
4164d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
41650a920b5bSAndy Whitcroft
4166b00e4814SJoe Perches				#   :   when part of a bitfield
4167b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4168b00e4814SJoe Perches					# skip the bitfield test for now
4169b00e4814SJoe Perches
41701f65f947SAndy Whitcroft				# No spaces for:
41711f65f947SAndy Whitcroft				#   ->
4172b00e4814SJoe Perches				} elsif ($op eq '->') {
41734a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
41743705ce5bSJoe Perches						if (ERROR("SPACING",
41753705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4176b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
41773705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
41783705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
41793705ce5bSJoe Perches							}
4180b34c648bSJoe Perches							$line_fixed = 1;
41813705ce5bSJoe Perches						}
41820a920b5bSAndy Whitcroft					}
41830a920b5bSAndy Whitcroft
41842381097bSJoe Perches				# , must not have a space before and must have a space on the right.
41850a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
41862381097bSJoe Perches					my $rtrim_before = 0;
41872381097bSJoe Perches					my $space_after = 0;
41882381097bSJoe Perches					if ($ctx =~ /Wx./) {
41892381097bSJoe Perches						if (ERROR("SPACING",
41902381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
41912381097bSJoe Perches							$line_fixed = 1;
41922381097bSJoe Perches							$rtrim_before = 1;
41932381097bSJoe Perches						}
41942381097bSJoe Perches					}
4195cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
41963705ce5bSJoe Perches						if (ERROR("SPACING",
41973705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
41983705ce5bSJoe Perches							$line_fixed = 1;
4199b34c648bSJoe Perches							$last_after = $n;
42002381097bSJoe Perches							$space_after = 1;
42012381097bSJoe Perches						}
42022381097bSJoe Perches					}
42032381097bSJoe Perches					if ($rtrim_before || $space_after) {
42042381097bSJoe Perches						if ($rtrim_before) {
42052381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42062381097bSJoe Perches						} else {
42072381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
42082381097bSJoe Perches						}
42092381097bSJoe Perches						if ($space_after) {
42102381097bSJoe Perches							$good .= " ";
42113705ce5bSJoe Perches						}
42120a920b5bSAndy Whitcroft					}
42130a920b5bSAndy Whitcroft
42149c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
421574048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
42169c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
42179c0ca6f9SAndy Whitcroft
42189c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
42199c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
42209c0ca6f9SAndy Whitcroft				# unary operator, or a cast
42219c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
422274048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
42230d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4224cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
42253705ce5bSJoe Perches						if (ERROR("SPACING",
42263705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4227b34c648bSJoe Perches							if ($n != $last_after + 2) {
4228b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
42293705ce5bSJoe Perches								$line_fixed = 1;
42303705ce5bSJoe Perches							}
42310a920b5bSAndy Whitcroft						}
4232b34c648bSJoe Perches					}
4233a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4234171ae1a4SAndy Whitcroft						# A unary '*' may be const
4235171ae1a4SAndy Whitcroft
4236171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
42373705ce5bSJoe Perches						if (ERROR("SPACING",
42383705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4239b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
42403705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42413705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
42423705ce5bSJoe Perches							}
4243b34c648bSJoe Perches							$line_fixed = 1;
42443705ce5bSJoe Perches						}
42450a920b5bSAndy Whitcroft					}
42460a920b5bSAndy Whitcroft
42470a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
42480a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4249773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
42503705ce5bSJoe Perches						if (ERROR("SPACING",
42513705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4252b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
42533705ce5bSJoe Perches							$line_fixed = 1;
42543705ce5bSJoe Perches						}
42550a920b5bSAndy Whitcroft					}
4256773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4257773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
42583705ce5bSJoe Perches						if (ERROR("SPACING",
42593705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4260b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42613705ce5bSJoe Perches							$line_fixed = 1;
42623705ce5bSJoe Perches						}
4263653d4876SAndy Whitcroft					}
4264773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
42653705ce5bSJoe Perches						if (ERROR("SPACING",
42663705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4267b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
42683705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42693705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4270773647a0SAndy Whitcroft							}
4271b34c648bSJoe Perches							$line_fixed = 1;
42723705ce5bSJoe Perches						}
42733705ce5bSJoe Perches					}
42740a920b5bSAndy Whitcroft
42750a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
42769c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
42779c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
42789c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4279c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4280c2fdda0dSAndy Whitcroft					 $op eq '%')
42810a920b5bSAndy Whitcroft				{
4282d2e025f3SJoe Perches					if ($check) {
4283d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4284d2e025f3SJoe Perches							if (CHK("SPACING",
4285d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4286d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4287d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4288d2e025f3SJoe Perches								$line_fixed = 1;
4289d2e025f3SJoe Perches							}
4290d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4291d2e025f3SJoe Perches							if (CHK("SPACING",
4292d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4293d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4294d2e025f3SJoe Perches								$line_fixed = 1;
4295d2e025f3SJoe Perches							}
4296d2e025f3SJoe Perches						}
4297d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
42983705ce5bSJoe Perches						if (ERROR("SPACING",
42993705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4300b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4301b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4302b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4303b34c648bSJoe Perches							}
43043705ce5bSJoe Perches							$line_fixed = 1;
43053705ce5bSJoe Perches						}
43060a920b5bSAndy Whitcroft					}
43070a920b5bSAndy Whitcroft
43081f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
43091f65f947SAndy Whitcroft				# terminating a case value or a label.
43101f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
43111f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
43123705ce5bSJoe Perches						if (ERROR("SPACING",
43133705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4314b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
43153705ce5bSJoe Perches							$line_fixed = 1;
43163705ce5bSJoe Perches						}
43171f65f947SAndy Whitcroft					}
43181f65f947SAndy Whitcroft
43190a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4320cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
43211f65f947SAndy Whitcroft					my $ok = 0;
43221f65f947SAndy Whitcroft
432322f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
43241f65f947SAndy Whitcroft					if (($op eq '<' &&
43251f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
43261f65f947SAndy Whitcroft					    ($op eq '>' &&
43271f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
43281f65f947SAndy Whitcroft					{
43291f65f947SAndy Whitcroft					    	$ok = 1;
43301f65f947SAndy Whitcroft					}
43311f65f947SAndy Whitcroft
4332e0df7e1fSJoe Perches					# for asm volatile statements
4333e0df7e1fSJoe Perches					# ignore a colon with another
4334e0df7e1fSJoe Perches					# colon immediately before or after
4335e0df7e1fSJoe Perches					if (($op eq ':') &&
4336e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4337e0df7e1fSJoe Perches						$ok = 1;
4338e0df7e1fSJoe Perches					}
4339e0df7e1fSJoe Perches
434084731623SJoe Perches					# messages are ERROR, but ?: are CHK
43411f65f947SAndy Whitcroft					if ($ok == 0) {
434284731623SJoe Perches						my $msg_type = \&ERROR;
434384731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
434484731623SJoe Perches
434584731623SJoe Perches						if (&{$msg_type}("SPACING",
43463705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
4347b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4348b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4349b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4350b34c648bSJoe Perches							}
43513705ce5bSJoe Perches							$line_fixed = 1;
43523705ce5bSJoe Perches						}
43530a920b5bSAndy Whitcroft					}
435422f2a2efSAndy Whitcroft				}
43554a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
43563705ce5bSJoe Perches
43573705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
43583705ce5bSJoe Perches
43593705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
43600a920b5bSAndy Whitcroft			}
43613705ce5bSJoe Perches
43623705ce5bSJoe Perches			if (($#elements % 2) == 0) {
43633705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
43643705ce5bSJoe Perches			}
43653705ce5bSJoe Perches
4366194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4367194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
43683705ce5bSJoe Perches			}
43693705ce5bSJoe Perches
43703705ce5bSJoe Perches
43710a920b5bSAndy Whitcroft		}
43720a920b5bSAndy Whitcroft
4373786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4374d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4375786b6326SJoe Perches			if (WARN("SPACING",
4376786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4377786b6326SJoe Perches			    $fix) {
4378194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4379786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4380786b6326SJoe Perches			}
4381786b6326SJoe Perches		}
4382786b6326SJoe Perches
4383f0a594c1SAndy Whitcroft# check for multiple assignments
4384f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4385000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4386000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4387f0a594c1SAndy Whitcroft		}
4388f0a594c1SAndy Whitcroft
438922f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
439022f2a2efSAndy Whitcroft## # continuation.
439122f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
439222f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
439322f2a2efSAndy Whitcroft##
439422f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
439522f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
439622f2a2efSAndy Whitcroft## 			my $ln = $line;
439722f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
439822f2a2efSAndy Whitcroft## 			}
439922f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4400000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4401000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
440222f2a2efSAndy Whitcroft## 			}
440322f2a2efSAndy Whitcroft## 		}
4404f0a594c1SAndy Whitcroft
44050a920b5bSAndy Whitcroft#need space before brace following if, while, etc
44066b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
44074e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
44083705ce5bSJoe Perches			if (ERROR("SPACING",
44093705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
44103705ce5bSJoe Perches			    $fix) {
44118d81ae05SCyril Bur				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
44123705ce5bSJoe Perches			}
4413de7d4f0eSAndy Whitcroft		}
4414de7d4f0eSAndy Whitcroft
4415c4a62ef9SJoe Perches## # check for blank lines before declarations
4416c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4417c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4418c4a62ef9SJoe Perches##			WARN("SPACING",
4419c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4420c4a62ef9SJoe Perches##		}
4421c4a62ef9SJoe Perches##
4422c4a62ef9SJoe Perches
4423de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4424de7d4f0eSAndy Whitcroft# on the line
4425de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4426d5e616fcSJoe Perches			if (ERROR("SPACING",
4427d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4428d5e616fcSJoe Perches			    $fix) {
4429194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4430d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4431d5e616fcSJoe Perches			}
44320a920b5bSAndy Whitcroft		}
44330a920b5bSAndy Whitcroft
443422f2a2efSAndy Whitcroft# check spacing on square brackets
443522f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
44363705ce5bSJoe Perches			if (ERROR("SPACING",
44373705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
44383705ce5bSJoe Perches			    $fix) {
4439194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44403705ce5bSJoe Perches				    s/\[\s+/\[/;
44413705ce5bSJoe Perches			}
444222f2a2efSAndy Whitcroft		}
444322f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
44443705ce5bSJoe Perches			if (ERROR("SPACING",
44453705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
44463705ce5bSJoe Perches			    $fix) {
4447194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44483705ce5bSJoe Perches				    s/\s+\]/\]/;
44493705ce5bSJoe Perches			}
445022f2a2efSAndy Whitcroft		}
445122f2a2efSAndy Whitcroft
4452c45dcabdSAndy Whitcroft# check spacing on parentheses
44539c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
44549c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
44553705ce5bSJoe Perches			if (ERROR("SPACING",
44563705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
44573705ce5bSJoe Perches			    $fix) {
4458194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44593705ce5bSJoe Perches				    s/\(\s+/\(/;
44603705ce5bSJoe Perches			}
446122f2a2efSAndy Whitcroft		}
446213214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4463c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4464c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
44653705ce5bSJoe Perches			if (ERROR("SPACING",
44663705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
44673705ce5bSJoe Perches			    $fix) {
4468194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44693705ce5bSJoe Perches				    s/\s+\)/\)/;
44703705ce5bSJoe Perches			}
447122f2a2efSAndy Whitcroft		}
447222f2a2efSAndy Whitcroft
4473e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4474e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4475e2826fd0SJoe Perches
4476e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4477ea4acbb1SJoe Perches			my $var = $1;
4478ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4479ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4480ea4acbb1SJoe Perches			    $fix) {
4481ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4482ea4acbb1SJoe Perches			}
4483ea4acbb1SJoe Perches		}
4484ea4acbb1SJoe Perches
4485ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4486ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4487ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4488ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4489ea4acbb1SJoe Perches			my $var = $2;
4490ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4491ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4492ea4acbb1SJoe Perches			    $fix) {
4493ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4494ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4495ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4496ea4acbb1SJoe Perches			}
4497e2826fd0SJoe Perches		}
4498e2826fd0SJoe Perches
44990a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
45004a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
45010a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
45023705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
45033705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
45043705ce5bSJoe Perches			    $fix) {
4505194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45063705ce5bSJoe Perches				    s/^(.)\s+/$1/;
45073705ce5bSJoe Perches			}
45080a920b5bSAndy Whitcroft		}
45090a920b5bSAndy Whitcroft
45105b9553abSJoe Perches# return is not a function
4511507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4512c45dcabdSAndy Whitcroft			my $spacing = $1;
4513507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
45145b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
45155b9553abSJoe Perches				my $value = $1;
45165b9553abSJoe Perches				$value = deparenthesize($value);
45175b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4518000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4519000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
45205b9553abSJoe Perches				}
4521c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4522000d1cc1SJoe Perches				ERROR("SPACING",
4523000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4524c45dcabdSAndy Whitcroft			}
4525c45dcabdSAndy Whitcroft		}
4526507e5141SJoe Perches
4527b43ae21bSJoe Perches# unnecessary return in a void function
4528b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4529b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4530b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4531b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4532b43ae21bSJoe Perches		    $linenr >= 3 &&
4533b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4534b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
45359819cf25SJoe Perches			WARN("RETURN_VOID",
4536b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
45379819cf25SJoe Perches               }
45389819cf25SJoe Perches
4539189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4540189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4541189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4542189248d8SJoe Perches			my $openparens = $1;
4543189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4544189248d8SJoe Perches			my $msg = "";
4545189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4546189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4547189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4548189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4549189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4550189248d8SJoe Perches			}
4551189248d8SJoe Perches		}
4552189248d8SJoe Perches
4553c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4554c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4555c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4556c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4557c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4558c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4559c5595fa2SJoe Perches			my $lead = $1;
4560c5595fa2SJoe Perches			my $const = $2;
4561c5595fa2SJoe Perches			my $comp = $3;
4562c5595fa2SJoe Perches			my $to = $4;
4563c5595fa2SJoe Perches			my $newcomp = $comp;
4564f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4565c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4566c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4567c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4568c5595fa2SJoe Perches			    $fix) {
4569c5595fa2SJoe Perches				if ($comp eq "<") {
4570c5595fa2SJoe Perches					$newcomp = ">";
4571c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4572c5595fa2SJoe Perches					$newcomp = ">=";
4573c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4574c5595fa2SJoe Perches					$newcomp = "<";
4575c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4576c5595fa2SJoe Perches					$newcomp = "<=";
4577c5595fa2SJoe Perches				}
4578c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4579c5595fa2SJoe Perches			}
4580c5595fa2SJoe Perches		}
4581c5595fa2SJoe Perches
4582f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4583f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
458453a3c448SAndy Whitcroft			my $name = $1;
458553a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4586000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4587f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
458853a3c448SAndy Whitcroft			}
458953a3c448SAndy Whitcroft		}
4590c45dcabdSAndy Whitcroft
45910a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
45924a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
45933705ce5bSJoe Perches			if (ERROR("SPACING",
45943705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
45953705ce5bSJoe Perches			    $fix) {
4596194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45973705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
45983705ce5bSJoe Perches			}
45990a920b5bSAndy Whitcroft		}
46000a920b5bSAndy Whitcroft
4601f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4602f5fe35ddSAndy Whitcroft# statements after the conditional.
4603170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
46043e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
46053e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
46063e469cdcSAndy Whitcroft					if (!defined $stat);
4607170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4608170d3a22SAndy Whitcroft						$remain_next, $off_next);
4609170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4610170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4611170d3a22SAndy Whitcroft
4612170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4613170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4614170d3a22SAndy Whitcroft				# then count those as offsets.
4615170d3a22SAndy Whitcroft				my ($whitespace) =
4616170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4617170d3a22SAndy Whitcroft				my $offset =
4618170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4619170d3a22SAndy Whitcroft
4620170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4621170d3a22SAndy Whitcroft								$offset} = 1;
4622170d3a22SAndy Whitcroft			}
4623170d3a22SAndy Whitcroft		}
4624170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4625c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4626170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4627171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
46288905a67cSAndy Whitcroft
4629b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4630000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4631000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
46328905a67cSAndy Whitcroft			}
46338905a67cSAndy Whitcroft
46348905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
46358905a67cSAndy Whitcroft			# conditional.
4636773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
46378905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
463813214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
463953210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
464053210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4641773647a0SAndy Whitcroft			{
4642bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4643bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4644bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
464542bdf74cSHidetoshi Seto				my $stat_real = '';
4646bb44ad39SAndy Whitcroft
464742bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
464842bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4649bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4650bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4651bb44ad39SAndy Whitcroft				}
4652bb44ad39SAndy Whitcroft
4653000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4654000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
46558905a67cSAndy Whitcroft			}
46568905a67cSAndy Whitcroft		}
46578905a67cSAndy Whitcroft
465813214adfSAndy Whitcroft# Check for bitwise tests written as boolean
465913214adfSAndy Whitcroft		if ($line =~ /
466013214adfSAndy Whitcroft			(?:
466113214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
466213214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
466313214adfSAndy Whitcroft				(?:\&\&|\|\|)
466413214adfSAndy Whitcroft			|
466513214adfSAndy Whitcroft				(?:\&\&|\|\|)
466613214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
466713214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
466813214adfSAndy Whitcroft			)/x)
466913214adfSAndy Whitcroft		{
4670000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4671000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
467213214adfSAndy Whitcroft		}
467313214adfSAndy Whitcroft
46748905a67cSAndy Whitcroft# if and else should not have general statements after it
467513214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
467613214adfSAndy Whitcroft			my $s = $1;
467713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
467813214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4679000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4680000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
46810a920b5bSAndy Whitcroft			}
468213214adfSAndy Whitcroft		}
468339667782SAndy Whitcroft# if should not continue a brace
468439667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4685000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4686048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
468739667782SAndy Whitcroft				$herecurr);
468839667782SAndy Whitcroft		}
4689a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4690a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4691a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
46923fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4693a1080bf8SAndy Whitcroft			\s*return\s+
4694a1080bf8SAndy Whitcroft		    )/xg)
4695a1080bf8SAndy Whitcroft		{
4696000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4697000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4698a1080bf8SAndy Whitcroft		}
46990a920b5bSAndy Whitcroft
47000a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
47010a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
47028b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
47030a920b5bSAndy Whitcroft		    $previndent == $indent) {
47048b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
47058b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
47068b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
47078b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
47088b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
47098b8856f4SJoe Perches				my $fixedline = $prevrawline;
47108b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
47118b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
47128b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
47138b8856f4SJoe Perches				}
47148b8856f4SJoe Perches				$fixedline = $rawline;
47158b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
47168b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
47178b8856f4SJoe Perches			}
47180a920b5bSAndy Whitcroft		}
47190a920b5bSAndy Whitcroft
47208b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4721c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4722c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4723c2fdda0dSAndy Whitcroft
4724c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4725c2fdda0dSAndy Whitcroft			# conditional.
4726773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4727c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4728c2fdda0dSAndy Whitcroft
4729c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
47308b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
47318b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
47328b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
47338b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
47348b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
47358b8856f4SJoe Perches					my $fixedline = $prevrawline;
47368b8856f4SJoe Perches					my $trailing = $rawline;
47378b8856f4SJoe Perches					$trailing =~ s/^\+//;
47388b8856f4SJoe Perches					$trailing = trim($trailing);
47398b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
47408b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
47418b8856f4SJoe Perches				}
4742c2fdda0dSAndy Whitcroft			}
4743c2fdda0dSAndy Whitcroft		}
4744c2fdda0dSAndy Whitcroft
474595e2c602SJoe Perches#Specific variable tests
4746323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4747323c1260SJoe Perches			my $var = $1;
474895e2c602SJoe Perches
474995e2c602SJoe Perches#gcc binary extension
475095e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4751d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4752d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4753d5e616fcSJoe Perches				    $fix) {
4754d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4755194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4756d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4757d5e616fcSJoe Perches				}
475895e2c602SJoe Perches			}
475995e2c602SJoe Perches
476095e2c602SJoe Perches#CamelCase
4761807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4762be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
476322735ce8SJoe Perches#Ignore Page<foo> variants
4764807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
476522735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4766f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4767f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4768f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
47697e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
47707e781f67SJoe Perches					my $word = $1;
47717e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4772d8b07710SJoe Perches					if ($check) {
4773d8b07710SJoe Perches						seed_camelcase_includes();
4774d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4775d8b07710SJoe Perches							seed_camelcase_file($realfile);
4776d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4777d8b07710SJoe Perches						}
4778d8b07710SJoe Perches					}
47797e781f67SJoe Perches					if (!defined $camelcase{$word}) {
47807e781f67SJoe Perches						$camelcase{$word} = 1;
4781be79794bSJoe Perches						CHK("CAMELCASE",
47827e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
47837e781f67SJoe Perches					}
4784323c1260SJoe Perches				}
4785323c1260SJoe Perches			}
47863445686aSJoe Perches		}
47870a920b5bSAndy Whitcroft
47880a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4789d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4790d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4791d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4792d5e616fcSJoe Perches			    $fix) {
4793194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4794d5e616fcSJoe Perches			}
47950a920b5bSAndy Whitcroft		}
47960a920b5bSAndy Whitcroft
47970e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
47980e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4799c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4800e09dec48SAndy Whitcroft			my $file = "$1.h";
4801e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4802e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4803e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
48047840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4805c45dcabdSAndy Whitcroft			{
48060e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
48070e212e0aSFabian Frederick				if ($asminclude > 0) {
4808e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4809000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4810000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4811e09dec48SAndy Whitcroft					} else {
4812000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4813000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4814e09dec48SAndy Whitcroft					}
48150a920b5bSAndy Whitcroft				}
48160a920b5bSAndy Whitcroft			}
48170e212e0aSFabian Frederick		}
48180a920b5bSAndy Whitcroft
4819653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4820653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4821cf655043SAndy Whitcroft# in a known good container
4822b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4823b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4824d8aaf121SAndy Whitcroft			my $ln = $linenr;
4825d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4826c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4827c45dcabdSAndy Whitcroft			my $ctx = '';
482808a2843eSJoe Perches			my $has_flow_statement = 0;
482908a2843eSJoe Perches			my $has_arg_concat = 0;
4830c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4831f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4832f74bd194SAndy Whitcroft			$ctx = $dstat;
4833c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4834a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4835c45dcabdSAndy Whitcroft
483608a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
483762e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
483808a2843eSJoe Perches
4839f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4840f59b64bfSJoe Perches			my $define_args = $1;
4841f59b64bfSJoe Perches			my $define_stmt = $dstat;
4842f59b64bfSJoe Perches			my @def_args = ();
4843f59b64bfSJoe Perches
4844f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
4845f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
4846f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
4847f59b64bfSJoe Perches				@def_args = split(",", $define_args);
4848f59b64bfSJoe Perches			}
4849f59b64bfSJoe Perches
4850292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4851c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4852c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4853c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4854c45dcabdSAndy Whitcroft
4855c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4856bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4857bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
48586b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4859bf30d6edSAndy Whitcroft			{
4860c45dcabdSAndy Whitcroft			}
4861c45dcabdSAndy Whitcroft
4862e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
486333acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
486433acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4865e45bab8eSAndy Whitcroft			{
4866e45bab8eSAndy Whitcroft			}
4867e45bab8eSAndy Whitcroft
486842e15293SJoe Perches			# Make asm volatile uses seem like a generic function
486942e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
487042e15293SJoe Perches
4871c45dcabdSAndy Whitcroft			my $exceptions = qr{
4872c45dcabdSAndy Whitcroft				$Declare|
4873c45dcabdSAndy Whitcroft				module_param_named|
4874a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4875c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4876c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4877383099fdSAndy Whitcroft				__typeof__\(|
487822fd2d3eSStefani Seibold				union|
487922fd2d3eSStefani Seibold				struct|
4880ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
48816b10df42SVladimir Zapolskiy				^\"|\"$|
48826b10df42SVladimir Zapolskiy				^\[
4883c45dcabdSAndy Whitcroft			}x;
48845eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4885f59b64bfSJoe Perches
4886f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
4887f59b64bfSJoe Perches			my $herectx = $here . "\n";
4888f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
4889f59b64bfSJoe Perches
4890f59b64bfSJoe Perches			for (my $n = 0; $n < $stmt_cnt; $n++) {
4891f59b64bfSJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4892f59b64bfSJoe Perches			}
4893f59b64bfSJoe Perches
4894f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4895f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4896f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
48973cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4898356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4899f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4900f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4901e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
490272f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4903f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4904f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4905f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
49064e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4907f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4908c45dcabdSAndy Whitcroft			{
4909e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
4910e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4911e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4912e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
4913f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4914f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4915f74bd194SAndy Whitcroft				} else {
4916000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4917388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4918d8aaf121SAndy Whitcroft				}
4919f59b64bfSJoe Perches
4920f59b64bfSJoe Perches			}
49215207649bSJoe Perches
49225207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
49235207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
49245207649bSJoe Perches			my $first = 1;
49255207649bSJoe Perches			$define_stmt = "";
49265207649bSJoe Perches			foreach my $l (@stmt_array) {
49275207649bSJoe Perches				$l =~ s/\\$//;
49285207649bSJoe Perches				if ($first) {
49295207649bSJoe Perches					$define_stmt = $l;
49305207649bSJoe Perches					$first = 0;
49315207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
49325207649bSJoe Perches					$define_stmt .= substr($l, 1);
49335207649bSJoe Perches				}
49345207649bSJoe Perches			}
49355207649bSJoe Perches			$define_stmt =~ s/$;//g;
49365207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
49375207649bSJoe Perches			$define_stmt = trim($define_stmt);
49385207649bSJoe Perches
4939f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
4940f59b64bfSJoe Perches			foreach my $arg (@def_args) {
4941f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
49429192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
49437fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
49447fe528a2SJoe Perches				$tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
49457fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
49467fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
49477fe528a2SJoe Perches				my $use_cnt = $tmp_stmt =~ s/\b$arg\b//g;
4948f59b64bfSJoe Perches				if ($use_cnt > 1) {
4949f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
4950f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
4951f59b64bfSJoe Perches				    }
49529192d41aSJoe Perches# check if any macro arguments may have other precedence issues
49537fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
49549192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
49559192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
49569192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
49579192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
49589192d41aSJoe Perches				}
49590a920b5bSAndy Whitcroft			}
49605023d347SJoe Perches
496108a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
496208a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
496308a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
496408a2843eSJoe Perches				my $herectx = $here . "\n";
496508a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
496608a2843eSJoe Perches
496708a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
496808a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
496908a2843eSJoe Perches				}
497008a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
497108a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
497208a2843eSJoe Perches			}
497308a2843eSJoe Perches
4974481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
49755023d347SJoe Perches
49765023d347SJoe Perches		} else {
49775023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4978481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4979481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
49805023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
49815023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
49825023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
49835023d347SJoe Perches			}
4984653d4876SAndy Whitcroft		}
49850a920b5bSAndy Whitcroft
4986b13edf7fSJoe Perches# do {} while (0) macro tests:
4987b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4988b13edf7fSJoe Perches# macro should not end with a semicolon
4989b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4990b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4991b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4992b13edf7fSJoe Perches			my $ln = $linenr;
4993b13edf7fSJoe Perches			my $cnt = $realcnt;
4994b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4995b13edf7fSJoe Perches			my $ctx = '';
4996b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4997b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4998b13edf7fSJoe Perches			$ctx = $dstat;
4999b13edf7fSJoe Perches
5000b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
50011b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5002b13edf7fSJoe Perches
5003b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5004b13edf7fSJoe Perches				my $stmts = $2;
5005b13edf7fSJoe Perches				my $semis = $3;
5006b13edf7fSJoe Perches
5007b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5008b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5009b13edf7fSJoe Perches				my $herectx = $here . "\n";
5010b13edf7fSJoe Perches
5011b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
5012b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
5013b13edf7fSJoe Perches				}
5014b13edf7fSJoe Perches
5015ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5016ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5017b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5018b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5019b13edf7fSJoe Perches				}
5020b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5021b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5022b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5023b13edf7fSJoe Perches				}
5024f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5025f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5026f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5027f5ef95b1SJoe Perches				my $herectx = $here . "\n";
5028f5ef95b1SJoe Perches
5029f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
5030f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
5031f5ef95b1SJoe Perches				}
5032f5ef95b1SJoe Perches
5033f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5034f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5035b13edf7fSJoe Perches			}
5036b13edf7fSJoe Perches		}
5037b13edf7fSJoe Perches
5038080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5039080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
5040080ba929SMike Frysinger#	.
5041080ba929SMike Frysinger#	ALIGN(...)
5042080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
5043080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
5044000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
5045000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
5046080ba929SMike Frysinger		}
5047080ba929SMike Frysinger
5048f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
504913214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
505013214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5051cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
505213214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5053cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5054cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5055aad4f614SJoe Perches				my @allowed = ();
5056aad4f614SJoe Perches				my $allow = 0;
505713214adfSAndy Whitcroft				my $seen = 0;
5058773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5059cf655043SAndy Whitcroft				my $ln = $linenr - 1;
506013214adfSAndy Whitcroft				for my $chunk (@chunks) {
506113214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
506213214adfSAndy Whitcroft
5063773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5064773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5065773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5066773647a0SAndy Whitcroft
5067aad4f614SJoe Perches					$allowed[$allow] = 0;
5068773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5069773647a0SAndy Whitcroft
5070773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5071773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5072773647a0SAndy Whitcroft
5073773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5074cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5075cf655043SAndy Whitcroft
5076773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
507713214adfSAndy Whitcroft
507813214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
507913214adfSAndy Whitcroft
5080aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5081cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5082cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5083aad4f614SJoe Perches						$allowed[$allow] = 1;
508413214adfSAndy Whitcroft					}
508513214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5086cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5087aad4f614SJoe Perches						$allowed[$allow] = 1;
508813214adfSAndy Whitcroft					}
5089cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5090cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5091aad4f614SJoe Perches						$allowed[$allow] = 1;
509213214adfSAndy Whitcroft					}
5093aad4f614SJoe Perches					$allow++;
509413214adfSAndy Whitcroft				}
5095aad4f614SJoe Perches				if ($seen) {
5096aad4f614SJoe Perches					my $sum_allowed = 0;
5097aad4f614SJoe Perches					foreach (@allowed) {
5098aad4f614SJoe Perches						$sum_allowed += $_;
5099aad4f614SJoe Perches					}
5100aad4f614SJoe Perches					if ($sum_allowed == 0) {
5101000d1cc1SJoe Perches						WARN("BRACES",
5102000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5103aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5104aad4f614SJoe Perches						 $seen != $allow) {
5105aad4f614SJoe Perches						CHK("BRACES",
5106aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5107aad4f614SJoe Perches					}
510813214adfSAndy Whitcroft				}
510913214adfSAndy Whitcroft			}
511013214adfSAndy Whitcroft		}
5111773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
511213214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5113cf655043SAndy Whitcroft			my $allowed = 0;
5114f0a594c1SAndy Whitcroft
5115cf655043SAndy Whitcroft			# Check the pre-context.
5116cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5117cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5118cf655043SAndy Whitcroft				$allowed = 1;
5119f0a594c1SAndy Whitcroft			}
5120773647a0SAndy Whitcroft
5121773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5122773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5123773647a0SAndy Whitcroft
5124cf655043SAndy Whitcroft			# Check the condition.
5125cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5126773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5127cf655043SAndy Whitcroft			if (defined $cond) {
5128773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5129cf655043SAndy Whitcroft			}
5130cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5131cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5132cf655043SAndy Whitcroft				$allowed = 1;
5133cf655043SAndy Whitcroft			}
5134cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5135cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5136cf655043SAndy Whitcroft				$allowed = 1;
5137cf655043SAndy Whitcroft			}
5138cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5139cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5140cf655043SAndy Whitcroft				$allowed = 1;
5141cf655043SAndy Whitcroft			}
5142cf655043SAndy Whitcroft			# Check the post-context.
5143cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5144cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5145cf655043SAndy Whitcroft				if (defined $cond) {
5146773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5147cf655043SAndy Whitcroft				}
5148cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5149cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5150cf655043SAndy Whitcroft					$allowed = 1;
5151cf655043SAndy Whitcroft				}
5152cf655043SAndy Whitcroft			}
5153cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
515469932487SJustin P. Mattock				my $herectx = $here . "\n";
5155f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5156cf655043SAndy Whitcroft
5157f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
515869932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
5159cf655043SAndy Whitcroft				}
5160cf655043SAndy Whitcroft
5161000d1cc1SJoe Perches				WARN("BRACES",
5162000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5163f0a594c1SAndy Whitcroft			}
5164f0a594c1SAndy Whitcroft		}
5165f0a594c1SAndy Whitcroft
5166e4c5babdSJoe Perches# check for single line unbalanced braces
516795330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
516895330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5169e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5170e4c5babdSJoe Perches		}
5171e4c5babdSJoe Perches
51720979ae66SJoe Perches# check for unnecessary blank lines around braces
517377b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5174f8e58219SJoe Perches			if (CHK("BRACES",
5175f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5176f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5177f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5178f8e58219SJoe Perches			}
51790979ae66SJoe Perches		}
518077b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5181f8e58219SJoe Perches			if (CHK("BRACES",
5182f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5183f8e58219SJoe Perches			    $fix) {
5184f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5185f8e58219SJoe Perches			}
51860979ae66SJoe Perches		}
51870979ae66SJoe Perches
51884a0df2efSAndy Whitcroft# no volatiles please
51896c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
51906c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5191000d1cc1SJoe Perches			WARN("VOLATILE",
51928c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
51934a0df2efSAndy Whitcroft		}
51944a0df2efSAndy Whitcroft
51955e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
51965e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
51975e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
51985e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
519933acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
52005e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
52015e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
52025e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
52035e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
52045e4f6ba5SJoe Perches				     $fix &&
52055e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
52065e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
52075e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
52085e4f6ba5SJoe Perches				my $comma_close = "";
52095e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
52105e4f6ba5SJoe Perches					$comma_close = $1;
52115e4f6ba5SJoe Perches				}
52125e4f6ba5SJoe Perches
52135e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
52145e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
52155e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
52165e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
52175e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
52185e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
52195e4f6ba5SJoe Perches				$fixedline = $rawline;
52205e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
52215e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
52225e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
52235e4f6ba5SJoe Perches				}
52245e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
52255e4f6ba5SJoe Perches			}
52265e4f6ba5SJoe Perches		}
52275e4f6ba5SJoe Perches
52285e4f6ba5SJoe Perches# check for missing a space in a string concatenation
52295e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
52305e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
52315e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
52325e4f6ba5SJoe Perches		}
52335e4f6ba5SJoe Perches
523477cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5235e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5236e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5237e4b7d309SJoe Perches# function declarations
523877cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
523977cb8546SJoe Perches		    defined($context_function) &&
5240e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5241e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
524277cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5243e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
524477cb8546SJoe Perches		}
524577cb8546SJoe Perches
52465e4f6ba5SJoe Perches# check for spaces before a quoted newline
52475e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
52485e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
52495e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
52505e4f6ba5SJoe Perches			    $fix) {
52515e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
52525e4f6ba5SJoe Perches			}
52535e4f6ba5SJoe Perches
52545e4f6ba5SJoe Perches		}
52555e4f6ba5SJoe Perches
5256f17dba4fSJoe Perches# concatenated string without spaces between elements
525733acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5258f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
5259f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
5260f17dba4fSJoe Perches		}
5261f17dba4fSJoe Perches
526290ad30e5SJoe Perches# uncoalesced string fragments
526333acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
526490ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
526590ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
526690ad30e5SJoe Perches		}
526790ad30e5SJoe Perches
5268522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5269522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5270522b837cSAlexey Dobriyan		my $show_Z = 1;
52715e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5272522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
52735e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5274522b837cSAlexey Dobriyan			# check for %L
5275522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
52765e4f6ba5SJoe Perches				WARN("PRINTF_L",
5277522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5278522b837cSAlexey Dobriyan				$show_L = 0;
52795e4f6ba5SJoe Perches			}
5280522b837cSAlexey Dobriyan			# check for %Z
5281522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5282522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5283522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5284522b837cSAlexey Dobriyan				$show_Z = 0;
5285522b837cSAlexey Dobriyan			}
5286522b837cSAlexey Dobriyan			# check for 0x<decimal>
5287522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5288522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
52896e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
52906e300757SJoe Perches			}
52915e4f6ba5SJoe Perches		}
52925e4f6ba5SJoe Perches
52935e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
52945e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
52955e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
52965e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
52975e4f6ba5SJoe Perches		}
52985e4f6ba5SJoe Perches
529900df344fSAndy Whitcroft# warn about #if 0
5300c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5301000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
5302000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
5303de7d4f0eSAndy Whitcroft				$herecurr);
53044a0df2efSAndy Whitcroft		}
53054a0df2efSAndy Whitcroft
530603df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
530703df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5308100425deSJoe Perches			my $tested = quotemeta($1);
5309100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5310100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5311100425deSJoe Perches				my $func = $1;
5312100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5313100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5314100425deSJoe Perches				    $fix) {
5315100425deSJoe Perches					my $do_fix = 1;
5316100425deSJoe Perches					my $leading_tabs = "";
5317100425deSJoe Perches					my $new_leading_tabs = "";
5318100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5319100425deSJoe Perches						$leading_tabs = $1;
5320100425deSJoe Perches					} else {
5321100425deSJoe Perches						$do_fix = 0;
5322100425deSJoe Perches					}
5323100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5324100425deSJoe Perches						$new_leading_tabs = $1;
5325100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5326100425deSJoe Perches							$do_fix = 0;
5327100425deSJoe Perches						}
5328100425deSJoe Perches					} else {
5329100425deSJoe Perches						$do_fix = 0;
5330100425deSJoe Perches					}
5331100425deSJoe Perches					if ($do_fix) {
5332100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5333100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5334100425deSJoe Perches					}
5335100425deSJoe Perches				}
53364c432a8fSGreg Kroah-Hartman			}
53374c432a8fSGreg Kroah-Hartman		}
5338f0a594c1SAndy Whitcroft
5339ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5340ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5341ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5342ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5343ebfdc409SJoe Perches		    $linenr > 3) {
5344ebfdc409SJoe Perches			my $testval = $2;
5345ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5346ebfdc409SJoe Perches
5347ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5348ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5349ebfdc409SJoe Perches
5350fb0d0e08SJoe 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)/) {
5351ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5352ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5353ebfdc409SJoe Perches			}
5354ebfdc409SJoe Perches		}
5355ebfdc409SJoe Perches
5356f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5357dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5358f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5359f78d98f6SJoe Perches			my $level = $1;
5360f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5361f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5362f78d98f6SJoe Perches			    $fix) {
5363f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5364f78d98f6SJoe Perches			}
5365f78d98f6SJoe Perches		}
5366f78d98f6SJoe Perches
536745c55e92SJoe Perches# check for logging continuations
536845c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
536945c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
537045c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
537145c55e92SJoe Perches		}
537245c55e92SJoe Perches
5373abb08a53SJoe Perches# check for mask then right shift without a parentheses
5374abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5375abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5376abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5377abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5378abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5379abb08a53SJoe Perches		}
5380abb08a53SJoe Perches
5381b75ac618SJoe Perches# check for pointer comparisons to NULL
5382b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
5383b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5384b75ac618SJoe Perches				my $val = $1;
5385b75ac618SJoe Perches				my $equal = "!";
5386b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5387b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5388b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5389b75ac618SJoe Perches					    $fix) {
5390b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5391b75ac618SJoe Perches				}
5392b75ac618SJoe Perches			}
5393b75ac618SJoe Perches		}
5394b75ac618SJoe Perches
53958716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
53968716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
53978716de38SJoe Perches			my $attr = $1;
53988716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
53998716de38SJoe Perches				my $ptr = $1;
54008716de38SJoe Perches				my $var = $2;
54018716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
54028716de38SJoe Perches				      ERROR("MISPLACED_INIT",
54038716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
54048716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
54058716de38SJoe Perches				      WARN("MISPLACED_INIT",
54068716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
54078716de38SJoe Perches				    $fix) {
5408194f66fcSJoe 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;
54098716de38SJoe Perches				}
54108716de38SJoe Perches			}
54118716de38SJoe Perches		}
54128716de38SJoe Perches
5413e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5414e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5415e970b884SJoe Perches			my $attr = $1;
5416e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5417e970b884SJoe Perches			my $attr_prefix = $1;
5418e970b884SJoe Perches			my $attr_type = $2;
5419e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5420e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5421e970b884SJoe Perches			    $fix) {
5422194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5423e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5424e970b884SJoe Perches			}
5425e970b884SJoe Perches		}
5426e970b884SJoe Perches
5427e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5428e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5429e970b884SJoe Perches			my $attr = $1;
5430e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5431e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5432e970b884SJoe Perches			    $fix) {
5433194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5434e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5435e970b884SJoe Perches				$lead = rtrim($1);
5436e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5437e970b884SJoe Perches				$lead = "${lead}const ";
5438194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5439e970b884SJoe Perches			}
5440e970b884SJoe Perches		}
5441e970b884SJoe Perches
5442c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5443c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5444c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5445c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5446c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5447c17893c7SJoe Perches			    $fix) {
5448c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5449c17893c7SJoe Perches			}
5450c17893c7SJoe Perches		}
5451c17893c7SJoe Perches
5452fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5453fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5454fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5455fbdb8138SJoe Perches			my $constant_func = $1;
5456fbdb8138SJoe Perches			my $func = $constant_func;
5457fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5458fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5459fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5460fbdb8138SJoe Perches			    $fix) {
5461194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5462fbdb8138SJoe Perches			}
5463fbdb8138SJoe Perches		}
5464fbdb8138SJoe Perches
54651a15a250SPatrick Pannuto# prefer usleep_range over udelay
546637581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
546743c1d77cSJoe Perches			my $delay = $1;
54681a15a250SPatrick Pannuto			# ignore udelay's < 10, however
546943c1d77cSJoe Perches			if (! ($delay < 10) ) {
5470000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
547143c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
547243c1d77cSJoe Perches			}
547343c1d77cSJoe Perches			if ($delay > 2000) {
547443c1d77cSJoe Perches				WARN("LONG_UDELAY",
547543c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
54761a15a250SPatrick Pannuto			}
54771a15a250SPatrick Pannuto		}
54781a15a250SPatrick Pannuto
547909ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
548009ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
548109ef8725SPatrick Pannuto			if ($1 < 20) {
5482000d1cc1SJoe Perches				WARN("MSLEEP",
548343c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
548409ef8725SPatrick Pannuto			}
548509ef8725SPatrick Pannuto		}
548609ef8725SPatrick Pannuto
548736ec1939SJoe Perches# check for comparisons of jiffies
548836ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
548936ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
549036ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
549136ec1939SJoe Perches		}
549236ec1939SJoe Perches
54939d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
54949d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
54959d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
54969d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
54979d7a34a5SJoe Perches		}
54989d7a34a5SJoe Perches
549900df344fSAndy Whitcroft# warn about #ifdefs in C files
5500c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
550100df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
550200df344fSAndy Whitcroft#			print "$herecurr";
550300df344fSAndy Whitcroft#			$clean = 0;
550400df344fSAndy Whitcroft#		}
550500df344fSAndy Whitcroft
550622f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5507c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
55083705ce5bSJoe Perches			if (ERROR("SPACING",
55093705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
55103705ce5bSJoe Perches			    $fix) {
5511194f66fcSJoe Perches				$fixed[$fixlinenr] =~
55123705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
55133705ce5bSJoe Perches			}
55143705ce5bSJoe Perches
551522f2a2efSAndy Whitcroft		}
551622f2a2efSAndy Whitcroft
55174a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5518171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5519171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
55204a0df2efSAndy Whitcroft			my $which = $1;
55214a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5522000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5523000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
55244a0df2efSAndy Whitcroft			}
55254a0df2efSAndy Whitcroft		}
55264a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5527402c2553SMichael S. Tsirkin
5528402c2553SMichael S. Tsirkin		my $barriers = qr{
5529402c2553SMichael S. Tsirkin			mb|
5530402c2553SMichael S. Tsirkin			rmb|
5531402c2553SMichael S. Tsirkin			wmb|
5532402c2553SMichael S. Tsirkin			read_barrier_depends
5533402c2553SMichael S. Tsirkin		}x;
5534402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5535402c2553SMichael S. Tsirkin			mb__before_atomic|
5536402c2553SMichael S. Tsirkin			mb__after_atomic|
5537402c2553SMichael S. Tsirkin			store_release|
5538402c2553SMichael S. Tsirkin			load_acquire|
5539402c2553SMichael S. Tsirkin			store_mb|
5540402c2553SMichael S. Tsirkin			(?:$barriers)
5541402c2553SMichael S. Tsirkin		}x;
5542402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5543402c2553SMichael S. Tsirkin			(?:$barriers)|
554443e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
554543e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5546402c2553SMichael S. Tsirkin		}x;
5547402c2553SMichael S. Tsirkin
5548402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
55494a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5550c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5551000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
55524a0df2efSAndy Whitcroft			}
55534a0df2efSAndy Whitcroft		}
55543ad81779SPaul E. McKenney
5555f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5556f4073b0fSMichael S. Tsirkin
5557f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5558f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5559f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5560f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5561f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5562f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5563f4073b0fSMichael S. Tsirkin		}
5564f4073b0fSMichael S. Tsirkin
5565cb426e99SJoe Perches# check for waitqueue_active without a comment.
5566cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5567cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5568cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5569cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5570cb426e99SJoe Perches			}
5571cb426e99SJoe Perches		}
55723ad81779SPaul E. McKenney
55734a0df2efSAndy Whitcroft# check of hardware specific defines
5574c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5575000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5576000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
55770a920b5bSAndy Whitcroft		}
5578653d4876SAndy Whitcroft
5579*596ed45bSJoe Perches# check that the storage class is not after a type
5580*596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5581000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5582*596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5583*596ed45bSJoe Perches		}
5584*596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5585*596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5586*596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5587*596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5588*596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5589*596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5590*596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5591d4977c78STobias Klauser		}
5592d4977c78STobias Klauser
5593de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5594de7d4f0eSAndy Whitcroft# storage class and type.
55959c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
55969c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5597000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5598000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5599de7d4f0eSAndy Whitcroft		}
5600de7d4f0eSAndy Whitcroft
56018905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
56022b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56032b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5604d5e616fcSJoe Perches			if (WARN("INLINE",
5605d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5606d5e616fcSJoe Perches			    $fix) {
5607194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5608d5e616fcSJoe Perches
5609d5e616fcSJoe Perches			}
56108905a67cSAndy Whitcroft		}
56118905a67cSAndy Whitcroft
56123d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
56132b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56142b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5615000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5616000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
56173d130fd0SJoe Perches		}
56183d130fd0SJoe Perches
561939b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
56202b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56212b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5622000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5623000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
562439b7e287SJoe Perches		}
562539b7e287SJoe Perches
56265f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
56272b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56282b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5629d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5630d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5631d5e616fcSJoe Perches			    $fix) {
5632194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5633d5e616fcSJoe Perches
5634d5e616fcSJoe Perches			}
56355f14d3bdSJoe Perches		}
56365f14d3bdSJoe Perches
56376061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
56382b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
56392b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5640d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5641d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5642d5e616fcSJoe Perches			    $fix) {
5643194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5644d5e616fcSJoe Perches			}
56456061d949SJoe Perches		}
56466061d949SJoe Perches
5647619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5648619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5649619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5650619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5651619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5652619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5653619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5654619a908aSJoe Perches		}
5655619a908aSJoe Perches
5656fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5657e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5658fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5659e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5660e6176fa4SJoe Perches			my $type = $1;
5661e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5662e6176fa4SJoe Perches				$type = $1;
5663e6176fa4SJoe Perches				my $kernel_type = 'u';
5664e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5665e6176fa4SJoe Perches				$type =~ /(\d+)/;
5666e6176fa4SJoe Perches				$kernel_type .= $1;
5667e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5668e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5669e6176fa4SJoe Perches				    $fix) {
5670e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5671e6176fa4SJoe Perches				}
5672e6176fa4SJoe Perches			}
5673e6176fa4SJoe Perches		}
5674e6176fa4SJoe Perches
5675938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5676938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5677938224b5SJoe Perches			my $cast = $1;
5678938224b5SJoe Perches			my $const = $2;
5679938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5680938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5681938224b5SJoe Perches			    $fix) {
5682938224b5SJoe Perches				my $suffix = "";
5683938224b5SJoe Perches				my $newconst = $const;
5684938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5685938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5686938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5687938224b5SJoe Perches					$suffix .= 'LL';
5688938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5689938224b5SJoe Perches					$suffix .= 'L';
5690938224b5SJoe Perches				}
5691938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5692938224b5SJoe Perches			}
5693938224b5SJoe Perches		}
5694938224b5SJoe Perches
56958f53a9b8SJoe Perches# check for sizeof(&)
56968f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5697000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5698000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
56998f53a9b8SJoe Perches		}
57008f53a9b8SJoe Perches
570166c80b60SJoe Perches# check for sizeof without parenthesis
570266c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5703d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5704d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5705d5e616fcSJoe Perches			    $fix) {
5706194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5707d5e616fcSJoe Perches			}
570866c80b60SJoe Perches		}
570966c80b60SJoe Perches
571088982feaSJoe Perches# check for struct spinlock declarations
571188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
571288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
571388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
571488982feaSJoe Perches		}
571588982feaSJoe Perches
5716a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
571706668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5718a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5719caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5720caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5721d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5722d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5723d5e616fcSJoe Perches				    $fix) {
5724194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5725d5e616fcSJoe Perches				}
5726a6962d72SJoe Perches			}
5727a6962d72SJoe Perches		}
5728a6962d72SJoe Perches
57290b523769SJoe Perches		# check for vsprintf extension %p<foo> misuses
57300b523769SJoe Perches		if ($^V && $^V ge 5.10.0 &&
57310b523769SJoe Perches		    defined $stat &&
57320b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
57330b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
57340b523769SJoe Perches			my $bad_extension = "";
57350b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
57360b523769SJoe Perches			$lc = $lc + $linenr;
57370b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
57380b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
57390b523769SJoe Perches				$fmt =~ s/%%//g;
5740ce4fecf1SPantelis Antoniou				if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNO]).)/) {
57410b523769SJoe Perches					$bad_extension = $1;
57420b523769SJoe Perches					last;
57430b523769SJoe Perches				}
57440b523769SJoe Perches			}
57450b523769SJoe Perches			if ($bad_extension ne "") {
57460b523769SJoe Perches				my $stat_real = raw_line($linenr, 0);
57470b523769SJoe Perches				for (my $count = $linenr + 1; $count <= $lc; $count++) {
57480b523769SJoe Perches					$stat_real = $stat_real . "\n" . raw_line($count, 0);
57490b523769SJoe Perches				}
57500b523769SJoe Perches				WARN("VSPRINTF_POINTER_EXTENSION",
57510b523769SJoe Perches				     "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
57520b523769SJoe Perches			}
57530b523769SJoe Perches		}
57540b523769SJoe Perches
5755554e165cSAndy Whitcroft# Check for misused memsets
5756d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5757d1fe9c09SJoe Perches		    defined $stat &&
57589e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5759554e165cSAndy Whitcroft
5760d7c76ba7SJoe Perches			my $ms_addr = $2;
5761d1fe9c09SJoe Perches			my $ms_val = $7;
5762d1fe9c09SJoe Perches			my $ms_size = $12;
5763d7c76ba7SJoe Perches
5764554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5765554e165cSAndy Whitcroft				ERROR("MEMSET",
5766d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5767554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5768554e165cSAndy Whitcroft				WARN("MEMSET",
5769d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5770d7c76ba7SJoe Perches			}
5771d7c76ba7SJoe Perches		}
5772d7c76ba7SJoe Perches
577398a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5774f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5775f333195dSJoe Perches#		    defined $stat &&
5776f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5777f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
5778f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5779f333195dSJoe Perches#			    $fix) {
5780f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5781f333195dSJoe Perches#			}
5782f333195dSJoe Perches#		}
578398a9bba5SJoe Perches
5784b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5785f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5786f333195dSJoe Perches#		    defined $stat &&
5787f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5788f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
5789f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5790f333195dSJoe Perches#		}
5791b6117d17SMateusz Kulikowski
57928617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
57938617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5794f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5795f333195dSJoe Perches#		    defined $stat &&
5796f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5797f333195dSJoe Perches#
5798f333195dSJoe Perches#			my $ms_val = $7;
5799f333195dSJoe Perches#
5800f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
5801f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
5802f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5803f333195dSJoe Perches#				    $fix) {
5804f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5805f333195dSJoe Perches#				}
5806f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5807f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
5808f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5809f333195dSJoe Perches#				    $fix) {
5810f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5811f333195dSJoe Perches#				}
5812f333195dSJoe Perches#			}
5813f333195dSJoe Perches#		}
58148617cd09SMateusz Kulikowski
5815d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5816d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5817d1fe9c09SJoe Perches		    defined $stat &&
5818d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5819d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5820d7c76ba7SJoe Perches				my $call = $1;
5821d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5822d7c76ba7SJoe Perches				my $arg1 = $3;
5823d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5824d1fe9c09SJoe Perches				my $arg2 = $8;
5825d7c76ba7SJoe Perches				my $cast;
5826d7c76ba7SJoe Perches
5827d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5828d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5829d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5830d7c76ba7SJoe Perches					$cast = $cast1;
5831d7c76ba7SJoe Perches				} else {
5832d7c76ba7SJoe Perches					$cast = $cast2;
5833d7c76ba7SJoe Perches				}
5834d7c76ba7SJoe Perches				WARN("MINMAX",
5835d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5836554e165cSAndy Whitcroft			}
5837554e165cSAndy Whitcroft		}
5838554e165cSAndy Whitcroft
58394a273195SJoe Perches# check usleep_range arguments
58404a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
58414a273195SJoe Perches		    defined $stat &&
58424a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
58434a273195SJoe Perches			my $min = $1;
58444a273195SJoe Perches			my $max = $7;
58454a273195SJoe Perches			if ($min eq $max) {
58464a273195SJoe Perches				WARN("USLEEP_RANGE",
58474a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
58484a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
58494a273195SJoe Perches				 $min > $max) {
58504a273195SJoe Perches				WARN("USLEEP_RANGE",
58514a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
58524a273195SJoe Perches			}
58534a273195SJoe Perches		}
58544a273195SJoe Perches
5855823b794cSJoe Perches# check for naked sscanf
5856823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5857823b794cSJoe Perches		    defined $stat &&
58586c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5859823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5860823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5861823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5862823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5863823b794cSJoe Perches			$lc = $lc + $linenr;
5864823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5865823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5866823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5867823b794cSJoe Perches			}
5868823b794cSJoe Perches			WARN("NAKED_SSCANF",
5869823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5870823b794cSJoe Perches		}
5871823b794cSJoe Perches
5872afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5873afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5874afc819abSJoe Perches		    defined $stat &&
5875afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5876afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5877afc819abSJoe Perches			$lc = $lc + $linenr;
5878afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5879afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5880afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5881afc819abSJoe Perches			}
5882afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5883afc819abSJoe Perches				my $format = $6;
5884afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5885afc819abSJoe Perches				if ($count == 1 &&
5886afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5887afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5888afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5889afc819abSJoe Perches				}
5890afc819abSJoe Perches			}
5891afc819abSJoe Perches		}
5892afc819abSJoe Perches
589370dc8a48SJoe Perches# check for new externs in .h files.
589470dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
589570dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5896d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
589770dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
589870dc8a48SJoe Perches			    $fix) {
5899194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
590070dc8a48SJoe Perches			}
590170dc8a48SJoe Perches		}
590270dc8a48SJoe Perches
5903de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5904171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5905c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5906171ae1a4SAndy Whitcroft		{
5907c45dcabdSAndy Whitcroft			my $function_name = $1;
5908c45dcabdSAndy Whitcroft			my $paren_space = $2;
5909171ae1a4SAndy Whitcroft
5910171ae1a4SAndy Whitcroft			my $s = $stat;
5911171ae1a4SAndy Whitcroft			if (defined $cond) {
5912171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5913171ae1a4SAndy Whitcroft			}
5914c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5915c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5916c45dcabdSAndy Whitcroft			{
5917000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5918000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5919de7d4f0eSAndy Whitcroft			}
5920de7d4f0eSAndy Whitcroft
5921171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5922000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5923000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5924171ae1a4SAndy Whitcroft			}
59259c9ba34eSAndy Whitcroft
59269c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
59279c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
59289c9ba34eSAndy Whitcroft		{
5929000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5930000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5931171ae1a4SAndy Whitcroft		}
5932171ae1a4SAndy Whitcroft
5933a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
5934a0ad7596SJoe Perches		if (defined $stat &&
5935ca0d8929SJoe Perches		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
5936ca0d8929SJoe Perches		    $1 ne "void") {
5937ca0d8929SJoe Perches			my $args = trim($1);
5938ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5939ca0d8929SJoe Perches				my $arg = trim($1);
5940ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5941ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
5942ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5943ca0d8929SJoe Perches				}
5944ca0d8929SJoe Perches			}
5945ca0d8929SJoe Perches		}
5946ca0d8929SJoe Perches
5947a0ad7596SJoe Perches# check for function definitions
5948a0ad7596SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5949a0ad7596SJoe Perches		    defined $stat &&
5950a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
5951a0ad7596SJoe Perches			$context_function = $1;
5952a0ad7596SJoe Perches
5953a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
5954a0ad7596SJoe Perches			my $ok = 0;
5955a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
5956a0ad7596SJoe Perches			my $herectx = $here . "\n";
5957a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5958a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
5959a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
5960a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
5961a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
5962a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
5963a0ad7596SJoe Perches			}
5964a0ad7596SJoe Perches			if (!$ok) {
5965a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
5966a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
5967a0ad7596SJoe Perches			}
5968a0ad7596SJoe Perches		}
5969a0ad7596SJoe Perches
5970de7d4f0eSAndy Whitcroft# checks for new __setup's
5971de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5972de7d4f0eSAndy Whitcroft			my $name = $1;
5973de7d4f0eSAndy Whitcroft
5974de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5975000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
59768c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
5977de7d4f0eSAndy Whitcroft			}
5978653d4876SAndy Whitcroft		}
59799c0ca6f9SAndy Whitcroft
59809c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5981caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5982000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5983000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
59849c0ca6f9SAndy Whitcroft		}
598513214adfSAndy Whitcroft
5986a640d25cSJoe Perches# alloc style
5987a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5988a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5989a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5990a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5991a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5992a640d25cSJoe Perches		}
5993a640d25cSJoe Perches
599460a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
599560a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
59961b4a2ed4SJoe Perches		    defined $stat &&
59971b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
599860a55369SJoe Perches			my $oldfunc = $3;
599960a55369SJoe Perches			my $a1 = $4;
600060a55369SJoe Perches			my $a2 = $10;
600160a55369SJoe Perches			my $newfunc = "kmalloc_array";
600260a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
600360a55369SJoe Perches			my $r1 = $a1;
600460a55369SJoe Perches			my $r2 = $a2;
600560a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
600660a55369SJoe Perches				$r1 = $a2;
600760a55369SJoe Perches				$r2 = $a1;
600860a55369SJoe Perches			}
6009e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6010e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
60111b4a2ed4SJoe Perches				my $ctx = '';
60121b4a2ed4SJoe Perches				my $herectx = $here . "\n";
60131b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
60141b4a2ed4SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
60151b4a2ed4SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
60161b4a2ed4SJoe Perches				}
6017e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
60181b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
60191b4a2ed4SJoe Perches				    $cnt == 1 &&
6020e367455aSJoe Perches				    $fix) {
6021194f66fcSJoe 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;
602260a55369SJoe Perches				}
602360a55369SJoe Perches			}
602460a55369SJoe Perches		}
602560a55369SJoe Perches
6026972fdea2SJoe Perches# check for krealloc arg reuse
6027972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
6028972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6029972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6030972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6031972fdea2SJoe Perches		}
6032972fdea2SJoe Perches
60335ce59ae0SJoe Perches# check for alloc argument mismatch
60345ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
60355ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
60365ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
60375ce59ae0SJoe Perches		}
60385ce59ae0SJoe Perches
6039caf2a54fSJoe Perches# check for multiple semicolons
6040caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6041d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6042d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6043d5e616fcSJoe Perches			    $fix) {
6044194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6045d5e616fcSJoe Perches			}
6046d1e2ad07SJoe Perches		}
6047d1e2ad07SJoe Perches
6048cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6049cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6050cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
60510ab90191SJoe Perches			my $ull = "";
60520ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
60530ab90191SJoe Perches			if (CHK("BIT_MACRO",
60540ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
60550ab90191SJoe Perches			    $fix) {
60560ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
60570ab90191SJoe Perches			}
60580ab90191SJoe Perches		}
60590ab90191SJoe Perches
60602d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
60612d632745SJoe 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*$/) {
60622d632745SJoe Perches			my $config = $1;
60632d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
60642d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
60652d632745SJoe Perches			    $fix) {
60662d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
60672d632745SJoe Perches			}
60682d632745SJoe Perches		}
60692d632745SJoe Perches
6070e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6071c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6072c34c09a8SJoe Perches			my $has_break = 0;
6073c34c09a8SJoe Perches			my $has_statement = 0;
6074c34c09a8SJoe Perches			my $count = 0;
6075c34c09a8SJoe Perches			my $prevline = $linenr;
6076e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6077c34c09a8SJoe Perches				$prevline--;
6078c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6079c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6080c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6081c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6082c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6083c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6084c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6085c34c09a8SJoe Perches				$has_statement = 1;
6086c34c09a8SJoe Perches				$count++;
6087c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6088c34c09a8SJoe Perches			}
6089c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6090c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6091224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6092c34c09a8SJoe Perches			}
6093c34c09a8SJoe Perches		}
6094c34c09a8SJoe Perches
6095d1e2ad07SJoe Perches# check for switch/default statements without a break;
6096d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
6097d1e2ad07SJoe Perches		    defined $stat &&
6098d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6099d1e2ad07SJoe Perches			my $ctx = '';
6100d1e2ad07SJoe Perches			my $herectx = $here . "\n";
6101d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6102d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6103d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
6104d1e2ad07SJoe Perches			}
6105d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6106d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6107caf2a54fSJoe Perches		}
6108caf2a54fSJoe Perches
610913214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6110d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6111d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6112d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6113d5e616fcSJoe Perches			    $fix) {
6114194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6115d5e616fcSJoe Perches			}
611613214adfSAndy Whitcroft		}
6117773647a0SAndy Whitcroft
611862ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
611962ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
612062ec818fSJoe Perches			ERROR("DATE_TIME",
612162ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
612262ec818fSJoe Perches		}
612362ec818fSJoe Perches
61242c92488aSJoe Perches# check for use of yield()
61252c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
61262c92488aSJoe Perches			WARN("YIELD",
61272c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
61282c92488aSJoe Perches		}
61292c92488aSJoe Perches
6130179f8f40SJoe Perches# check for comparisons against true and false
6131179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6132179f8f40SJoe Perches			my $lead = $1;
6133179f8f40SJoe Perches			my $arg = $2;
6134179f8f40SJoe Perches			my $test = $3;
6135179f8f40SJoe Perches			my $otype = $4;
6136179f8f40SJoe Perches			my $trail = $5;
6137179f8f40SJoe Perches			my $op = "!";
6138179f8f40SJoe Perches
6139179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6140179f8f40SJoe Perches
6141179f8f40SJoe Perches			my $type = lc($otype);
6142179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6143179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6144179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6145179f8f40SJoe Perches					$op = "";
6146179f8f40SJoe Perches				}
6147179f8f40SJoe Perches
6148179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6149179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6150179f8f40SJoe Perches
6151179f8f40SJoe Perches## maybe suggesting a correct construct would better
6152179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6153179f8f40SJoe Perches
6154179f8f40SJoe Perches			}
6155179f8f40SJoe Perches		}
6156179f8f40SJoe Perches
61574882720bSThomas Gleixner# check for semaphores initialized locked
61584882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6159000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6160000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6161773647a0SAndy Whitcroft		}
61626712d858SJoe Perches
616367d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
616467d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6165000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
616667d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6167773647a0SAndy Whitcroft		}
61686712d858SJoe Perches
6169ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6170f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6171000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6172ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6173f3db6639SMichael Ellerman		}
61746712d858SJoe Perches
61750f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6176d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
61776903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6178d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6179000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6180d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
61812b6db5cbSAndy Whitcroft		}
6182773647a0SAndy Whitcroft
6183773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6184773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6185773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6186c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6187c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6188171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6189171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6190171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6191773647a0SAndy Whitcroft		{
6192000d1cc1SJoe Perches			WARN("NR_CPUS",
6193000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6194773647a0SAndy Whitcroft		}
61959c9ba34eSAndy Whitcroft
619652ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
619752ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
619852ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
619952ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
620052ea8506SJoe Perches		}
620152ea8506SJoe Perches
6202acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6203acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6204acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6205acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6206acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6207acd9362cSJoe Perches		}
6208acd9362cSJoe Perches
6209691d77b6SAndy Whitcroft# whine mightly about in_atomic
6210691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6211691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6212000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6213000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6214f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6215000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6216000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6217691d77b6SAndy Whitcroft			}
6218691d77b6SAndy Whitcroft		}
62191704f47bSPeter Zijlstra
6220481aea5cSJoe Perches# whine about ACCESS_ONCE
6221481aea5cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6222481aea5cSJoe Perches		    $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6223481aea5cSJoe Perches			my $par = $1;
6224481aea5cSJoe Perches			my $eq = $2;
6225481aea5cSJoe Perches			my $fun = $3;
6226481aea5cSJoe Perches			$par =~ s/^\(\s*(.*)\s*\)$/$1/;
6227481aea5cSJoe Perches			if (defined($eq)) {
6228481aea5cSJoe Perches				if (WARN("PREFER_WRITE_ONCE",
6229481aea5cSJoe Perches					 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6230481aea5cSJoe Perches				    $fix) {
6231481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6232481aea5cSJoe Perches				}
6233481aea5cSJoe Perches			} else {
6234481aea5cSJoe Perches				if (WARN("PREFER_READ_ONCE",
6235481aea5cSJoe Perches					 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6236481aea5cSJoe Perches				    $fix) {
6237481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6238481aea5cSJoe Perches				}
6239481aea5cSJoe Perches			}
6240481aea5cSJoe Perches		}
6241481aea5cSJoe Perches
62420f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
62430f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
62440f5225b0SPeter Zijlstra			ERROR("LOCKING",
62450f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
62460f5225b0SPeter Zijlstra		}
62470f5225b0SPeter Zijlstra
62481704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
62491704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
62501704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
62511704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
62521704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
62531704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6254000d1cc1SJoe Perches				ERROR("LOCKDEP",
6255000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
62561704f47bSPeter Zijlstra			}
62571704f47bSPeter Zijlstra		}
625888f8831cSDave Jones
6259b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6260b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6261000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6262000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
626388f8831cSDave Jones		}
62642435880fSJoe Perches
6265515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6266515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6267515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6268459cf0aeSJoe Perches		    defined $stat &&
6269515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
62702435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
62712435880fSJoe Perches				my $func = $entry->[0];
62722435880fSJoe Perches				my $arg_pos = $entry->[1];
62732435880fSJoe Perches
6274459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6275459cf0aeSJoe Perches				$lc = $lc + $linenr;
6276459cf0aeSJoe Perches				my $stat_real = raw_line($linenr, 0);
6277459cf0aeSJoe Perches				for (my $count = $linenr + 1; $count <= $lc; $count++) {
6278459cf0aeSJoe Perches					$stat_real = $stat_real . "\n" . raw_line($count, 0);
6279459cf0aeSJoe Perches				}
6280459cf0aeSJoe Perches
62812435880fSJoe Perches				my $skip_args = "";
62822435880fSJoe Perches				if ($arg_pos > 1) {
62832435880fSJoe Perches					$arg_pos--;
62842435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
62852435880fSJoe Perches				}
6286f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6287459cf0aeSJoe Perches				if ($stat =~ /$test/) {
62882435880fSJoe Perches					my $val = $1;
62892435880fSJoe Perches					$val = $6 if ($skip_args ne "");
6290f90774e1SJoe Perches					if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6291f90774e1SJoe Perches					    ($val =~ /^$Octal$/ && length($val) ne 4)) {
62922435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6293459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6294f90774e1SJoe Perches					}
6295f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6296c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6297459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
62982435880fSJoe Perches					}
6299459cf0aeSJoe Perches				}
6300459cf0aeSJoe Perches			}
6301459cf0aeSJoe Perches		}
6302459cf0aeSJoe Perches
6303459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6304459cf0aeSJoe Perches		if ($line =~ /\b$mode_perms_string_search\b/) {
6305459cf0aeSJoe Perches			my $val = "";
6306459cf0aeSJoe Perches			my $oval = "";
6307f90774e1SJoe Perches			my $to = 0;
6308459cf0aeSJoe Perches			my $curpos = 0;
6309459cf0aeSJoe Perches			my $lastpos = 0;
6310459cf0aeSJoe Perches			while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6311459cf0aeSJoe Perches				$curpos = pos($line);
6312459cf0aeSJoe Perches				my $match = $2;
6313459cf0aeSJoe Perches				my $omatch = $1;
6314459cf0aeSJoe Perches				last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6315459cf0aeSJoe Perches				$lastpos = $curpos;
6316459cf0aeSJoe Perches				$to |= $mode_permission_string_types{$match};
6317459cf0aeSJoe Perches				$val .= '\s*\|\s*' if ($val ne "");
6318459cf0aeSJoe Perches				$val .= $match;
6319459cf0aeSJoe Perches				$oval .= $omatch;
6320f90774e1SJoe Perches			}
6321459cf0aeSJoe Perches			$oval =~ s/^\s*\|\s*//;
6322459cf0aeSJoe Perches			$oval =~ s/\s*\|\s*$//;
6323459cf0aeSJoe Perches			my $octal = sprintf("%04o", $to);
6324f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6325459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6326f90774e1SJoe Perches			    $fix) {
6327459cf0aeSJoe Perches				$fixed[$fixlinenr] =~ s/$val/$octal/;
63282435880fSJoe Perches			}
632913214adfSAndy Whitcroft		}
63305a6d20ceSBjorn Andersson
63315a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
63325a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
63335a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
63345a6d20ceSBjorn Andersson			my $valid_licenses = qr{
63355a6d20ceSBjorn Andersson						GPL|
63365a6d20ceSBjorn Andersson						GPL\ v2|
63375a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
63385a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
63395a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
63405a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
63415a6d20ceSBjorn Andersson						Proprietary
63425a6d20ceSBjorn Andersson					}x;
63435a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
63445a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
63455a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
63465a6d20ceSBjorn Andersson			}
63475a6d20ceSBjorn Andersson		}
6348515a235eSJoe Perches	}
634913214adfSAndy Whitcroft
635013214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
635113214adfSAndy Whitcroft	# so just keep quiet.
635213214adfSAndy Whitcroft	if ($#rawlines == -1) {
635313214adfSAndy Whitcroft		exit(0);
63540a920b5bSAndy Whitcroft	}
63550a920b5bSAndy Whitcroft
63568905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
63578905a67cSAndy Whitcroft	# things that appear to be patches.
63588905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
63598905a67cSAndy Whitcroft		exit(0);
63608905a67cSAndy Whitcroft	}
63618905a67cSAndy Whitcroft
63628905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
63638905a67cSAndy Whitcroft	# just keep quiet.
63648905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
63658905a67cSAndy Whitcroft		exit(0);
63668905a67cSAndy Whitcroft	}
63678905a67cSAndy Whitcroft
636806330fc4SJoe Perches	if (!$is_patch && $file !~ /cover-letter\.patch$/) {
6369000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6370000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
63710a920b5bSAndy Whitcroft	}
6372ed43c4e5SAllen Hubbe	if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6373000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
6374000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
63750a920b5bSAndy Whitcroft	}
63760a920b5bSAndy Whitcroft
6377f0a594c1SAndy Whitcroft	print report_dump();
637813214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
637913214adfSAndy Whitcroft		print "$filename " if ($summary_file);
63806c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
63816c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
63826c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
63836c72ffaaSAndy Whitcroft	}
63848905a67cSAndy Whitcroft
6385d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6386ef212196SJoe Perches		# If there were any defects found and not already fixing them
6387ef212196SJoe Perches		if (!$clean and !$fix) {
6388ef212196SJoe Perches			print << "EOM"
6389ef212196SJoe Perches
6390ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6391ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6392ef212196SJoe PerchesEOM
6393ef212196SJoe Perches		}
6394d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6395d2c0a235SAndy Whitcroft		# then suggest that.
6396d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6397b0781216SMike Frysinger			$rpt_cleaners = 0;
6398d8469f16SJoe Perches			print << "EOM"
6399d8469f16SJoe Perches
6400d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6401d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6402d8469f16SJoe PerchesEOM
6403d2c0a235SAndy Whitcroft		}
6404d2c0a235SAndy Whitcroft	}
6405d2c0a235SAndy Whitcroft
6406d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6407d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6408d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
64099624b8d6SJoe Perches		my $newfile = $filename;
64109624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
64113705ce5bSJoe Perches		my $linecount = 0;
64123705ce5bSJoe Perches		my $f;
64133705ce5bSJoe Perches
6414d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6415d752fcc8SJoe Perches
64163705ce5bSJoe Perches		open($f, '>', $newfile)
64173705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
64183705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
64193705ce5bSJoe Perches			$linecount++;
64203705ce5bSJoe Perches			if ($file) {
64213705ce5bSJoe Perches				if ($linecount > 3) {
64223705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
64233705ce5bSJoe Perches					print $f $fixed_line . "\n";
64243705ce5bSJoe Perches				}
64253705ce5bSJoe Perches			} else {
64263705ce5bSJoe Perches				print $f $fixed_line . "\n";
64273705ce5bSJoe Perches			}
64283705ce5bSJoe Perches		}
64293705ce5bSJoe Perches		close($f);
64303705ce5bSJoe Perches
64313705ce5bSJoe Perches		if (!$quiet) {
64323705ce5bSJoe Perches			print << "EOM";
6433d8469f16SJoe Perches
64343705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
64353705ce5bSJoe Perches
64363705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
64373705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
64383705ce5bSJoe Perches
64393705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
64403705ce5bSJoe PerchesNo warranties, expressed or implied...
64413705ce5bSJoe PerchesEOM
64423705ce5bSJoe Perches		}
64433705ce5bSJoe Perches	}
64443705ce5bSJoe Perches
6445d8469f16SJoe Perches	if ($quiet == 0) {
6446d8469f16SJoe Perches		print "\n";
6447d8469f16SJoe Perches		if ($clean == 1) {
6448d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6449d8469f16SJoe Perches		} else {
6450d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
64510a920b5bSAndy Whitcroft		}
64520a920b5bSAndy Whitcroft	}
64530a920b5bSAndy Whitcroft	return $clean;
64540a920b5bSAndy Whitcroft}
6455