xref: /linux-6.15/scripts/checkpatch.pl (revision d439e6a5)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0
3882ea1d6SJoe Perches#
4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]>
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftuse strict;
11cb77f0d6SKamil Rytarowskiuse warnings;
12c707a81dSJoe Perchesuse POSIX;
1336061e38SJoe Perchesuse File::Basename;
1436061e38SJoe Perchesuse Cwd 'abs_path';
1557230297SJoe Perchesuse Term::ANSIColor qw(:constants);
16cd261496SGeert Uytterhoevenuse Encode qw(decode encode);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $P = $0;
1936061e38SJoe Perchesmy $D = dirname(abs_path($P));
200a920b5bSAndy Whitcroft
21000d1cc1SJoe Perchesmy $V = '0.32';
220a920b5bSAndy Whitcroft
230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
240a920b5bSAndy Whitcroft
250a920b5bSAndy Whitcroftmy $quiet = 0;
260a920b5bSAndy Whitcroftmy $tree = 1;
270a920b5bSAndy Whitcroftmy $chk_signoff = 1;
280a920b5bSAndy Whitcroftmy $chk_patch = 1;
29773647a0SAndy Whitcroftmy $tst_only;
306c72ffaaSAndy Whitcroftmy $emacs = 0;
318905a67cSAndy Whitcroftmy $terse = 0;
3234d8815fSJoe Perchesmy $showfile = 0;
336c72ffaaSAndy Whitcroftmy $file = 0;
344a593c34SDu, Changbinmy $git = 0;
350dea9f1eSJoe Perchesmy %git_commits = ();
366c72ffaaSAndy Whitcroftmy $check = 0;
372ac73b4fSJoe Perchesmy $check_orig = 0;
388905a67cSAndy Whitcroftmy $summary = 1;
398905a67cSAndy Whitcroftmy $mailback = 0;
4013214adfSAndy Whitcroftmy $summary_file = 0;
41000d1cc1SJoe Perchesmy $show_types = 0;
423beb42ecSJoe Perchesmy $list_types = 0;
433705ce5bSJoe Perchesmy $fix = 0;
449624b8d6SJoe Perchesmy $fix_inplace = 0;
456c72ffaaSAndy Whitcroftmy $root;
46c2fdda0dSAndy Whitcroftmy %debug;
473445686aSJoe Perchesmy %camelcase = ();
4891bfe484SJoe Perchesmy %use_type = ();
4991bfe484SJoe Perchesmy @use = ();
5091bfe484SJoe Perchesmy %ignore_type = ();
51000d1cc1SJoe Perchesmy @ignore = ();
5277f5b10aSHannes Edermy $help = 0;
53000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
546cd7f386SJoe Perchesmy $max_line_length = 80;
55d62a201fSDave Hansenmy $ignore_perl_version = 0;
56d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5756193274SVadim Bendeburymy $min_conf_desc_length = 4;
5866b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
59ebfd7d62SJoe Perchesmy $codespell = 0;
60f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
61bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
6275ad8c57SJerome Forissiermy $typedefsfile = "";
63737c0767SJohn Brooksmy $color = "auto";
6498005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
65dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE
66dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git';
6777f5b10aSHannes Eder
6877f5b10aSHannes Edersub help {
6977f5b10aSHannes Eder	my ($exitcode) = @_;
7077f5b10aSHannes Eder
7177f5b10aSHannes Eder	print << "EOM";
7277f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
7377f5b10aSHannes EderVersion: $V
7477f5b10aSHannes Eder
7577f5b10aSHannes EderOptions:
7677f5b10aSHannes Eder  -q, --quiet                quiet
7777f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7877f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
7977f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
8077f5b10aSHannes Eder  --emacs                    emacs compile window format
8177f5b10aSHannes Eder  --terse                    one line per report
8234d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
834a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
844a593c34SDu, Changbin                             single git commit with:
854a593c34SDu, Changbin                               <rev>
864a593c34SDu, Changbin                               <rev>^
874a593c34SDu, Changbin                               <rev>~n
884a593c34SDu, Changbin                             multiple git commits with:
894a593c34SDu, Changbin                               <rev1>..<rev2>
904a593c34SDu, Changbin                               <rev1>...<rev2>
914a593c34SDu, Changbin                               <rev>-<count>
924a593c34SDu, Changbin                             git merges are ignored
9377f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
9477f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
953beb42ecSJoe Perches  --list-types               list the possible message types
9691bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
97000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
983beb42ecSJoe Perches  --show-types               show the specific message type in the output
996cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
10056193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
10177f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
10277f5b10aSHannes Eder  --no-summary               suppress the per-file summary
10377f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
10477f5b10aSHannes Eder  --summary-file             include the filename in summary
10577f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
10677f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
10777f5b10aSHannes Eder                             is all off)
10877f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
10977f5b10aSHannes Eder                             literally
1103705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1113705ce5bSJoe Perches                             If correctable single-line errors exist, create
1123705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1133705ce5bSJoe Perches                             with potential errors corrected to the preferred
1143705ce5bSJoe Perches                             checkpatch style
1159624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1169624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1179624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
118d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
119d62a201fSDave Hansen                             runtime errors.
120ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
121f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
122ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
12375ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
124737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
125737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
12677f5b10aSHannes Eder  -h, --help, --version      display this help and exit
12777f5b10aSHannes Eder
12877f5b10aSHannes EderWhen FILE is - read standard input.
12977f5b10aSHannes EderEOM
13077f5b10aSHannes Eder
13177f5b10aSHannes Eder	exit($exitcode);
13277f5b10aSHannes Eder}
13377f5b10aSHannes Eder
1343beb42ecSJoe Perchessub uniq {
1353beb42ecSJoe Perches	my %seen;
1363beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1373beb42ecSJoe Perches}
1383beb42ecSJoe Perches
1393beb42ecSJoe Perchessub list_types {
1403beb42ecSJoe Perches	my ($exitcode) = @_;
1413beb42ecSJoe Perches
1423beb42ecSJoe Perches	my $count = 0;
1433beb42ecSJoe Perches
1443beb42ecSJoe Perches	local $/ = undef;
1453beb42ecSJoe Perches
1463beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1473beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1483beb42ecSJoe Perches
1493beb42ecSJoe Perches	my $text = <$script>;
1503beb42ecSJoe Perches	close($script);
1513beb42ecSJoe Perches
1523beb42ecSJoe Perches	my @types = ();
1530547fa58SJean Delvare	# Also catch when type or level is passed through a variable
1540547fa58SJean Delvare	for ($text =~ /(?:(?:\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
1553beb42ecSJoe Perches		push (@types, $_);
1563beb42ecSJoe Perches	}
1573beb42ecSJoe Perches	@types = sort(uniq(@types));
1583beb42ecSJoe Perches	print("#\tMessage type\n\n");
1593beb42ecSJoe Perches	foreach my $type (@types) {
1603beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1613beb42ecSJoe Perches	}
1623beb42ecSJoe Perches
1633beb42ecSJoe Perches	exit($exitcode);
1643beb42ecSJoe Perches}
1653beb42ecSJoe Perches
166000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
167000d1cc1SJoe Perchesif (-f $conf) {
168000d1cc1SJoe Perches	my @conf_args;
169000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
170000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
171000d1cc1SJoe Perches
172000d1cc1SJoe Perches	while (<$conffile>) {
173000d1cc1SJoe Perches		my $line = $_;
174000d1cc1SJoe Perches
175000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
176000d1cc1SJoe Perches		$line =~ s/^\s*//g;
177000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
178000d1cc1SJoe Perches
179000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
180000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
181000d1cc1SJoe Perches
182000d1cc1SJoe Perches		my @words = split(" ", $line);
183000d1cc1SJoe Perches		foreach my $word (@words) {
184000d1cc1SJoe Perches			last if ($word =~ m/^#/);
185000d1cc1SJoe Perches			push (@conf_args, $word);
186000d1cc1SJoe Perches		}
187000d1cc1SJoe Perches	}
188000d1cc1SJoe Perches	close($conffile);
189000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
190000d1cc1SJoe Perches}
191000d1cc1SJoe Perches
192737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
193737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
194737c0767SJohn Brooksforeach (@ARGV) {
195737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
196737c0767SJohn Brooks		$_ = "--color=$color";
197737c0767SJohn Brooks	}
198737c0767SJohn Brooks}
199737c0767SJohn Brooks
2000a920b5bSAndy WhitcroftGetOptions(
2016c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
2020a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2030a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
2040a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
2056c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
2068905a67cSAndy Whitcroft	'terse!'	=> \$terse,
20734d8815fSJoe Perches	'showfile!'	=> \$showfile,
20877f5b10aSHannes Eder	'f|file!'	=> \$file,
2094a593c34SDu, Changbin	'g|git!'	=> \$git,
2106c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
2116c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
212000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
21391bfe484SJoe Perches	'types=s'	=> \@use,
214000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
2153beb42ecSJoe Perches	'list-types!'	=> \$list_types,
2166cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
21756193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
2186c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2198905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2208905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
22113214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2223705ce5bSJoe Perches	'fix!'		=> \$fix,
2239624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
224d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
225c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
226773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
227ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
228ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
22975ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
230737c0767SJohn Brooks	'color=s'	=> \$color,
231737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
232737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
23377f5b10aSHannes Eder	'h|help'	=> \$help,
23477f5b10aSHannes Eder	'version'	=> \$help
23577f5b10aSHannes Eder) or help(1);
23677f5b10aSHannes Eder
23777f5b10aSHannes Ederhelp(0) if ($help);
2380a920b5bSAndy Whitcroft
2393beb42ecSJoe Percheslist_types(0) if ($list_types);
2403beb42ecSJoe Perches
2419624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2422ac73b4fSJoe Perches$check_orig = $check;
2439624b8d6SJoe Perches
2440a920b5bSAndy Whitcroftmy $exit = 0;
2450a920b5bSAndy Whitcroft
2465b57980dSJoe Perchesmy $perl_version_ok = 1;
247d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
2485b57980dSJoe Perches	$perl_version_ok = 0;
249d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
2505b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
251d62a201fSDave Hansen}
252d62a201fSDave Hansen
25345107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2540a920b5bSAndy Whitcroftif ($#ARGV < 0) {
25545107ff6SAllen Hubbe	push(@ARGV, '-');
2560a920b5bSAndy Whitcroft}
2570a920b5bSAndy Whitcroft
258737c0767SJohn Brooksif ($color =~ /^[01]$/) {
259737c0767SJohn Brooks	$color = !$color;
260737c0767SJohn Brooks} elsif ($color =~ /^always$/i) {
261737c0767SJohn Brooks	$color = 1;
262737c0767SJohn Brooks} elsif ($color =~ /^never$/i) {
263737c0767SJohn Brooks	$color = 0;
264737c0767SJohn Brooks} elsif ($color =~ /^auto$/i) {
265737c0767SJohn Brooks	$color = (-t STDOUT);
266737c0767SJohn Brooks} else {
267737c0767SJohn Brooks	die "Invalid color mode: $color\n";
268737c0767SJohn Brooks}
269737c0767SJohn Brooks
27091bfe484SJoe Perchessub hash_save_array_words {
27191bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
27291bfe484SJoe Perches
27391bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
27491bfe484SJoe Perches	foreach my $word (@array) {
275000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
276000d1cc1SJoe Perches		$word =~ s/^\s*//g;
277000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
278000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
279000d1cc1SJoe Perches
280000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
281000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
282000d1cc1SJoe Perches
28391bfe484SJoe Perches		$hashRef->{$word}++;
284000d1cc1SJoe Perches	}
28591bfe484SJoe Perches}
28691bfe484SJoe Perches
28791bfe484SJoe Perchessub hash_show_words {
28891bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
28991bfe484SJoe Perches
2903c816e49SJoe Perches	if (keys %$hashRef) {
291d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
29258cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
29391bfe484SJoe Perches			print " $word";
29491bfe484SJoe Perches		}
295d8469f16SJoe Perches		print "\n";
29691bfe484SJoe Perches	}
29791bfe484SJoe Perches}
29891bfe484SJoe Perches
29991bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
30091bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
301000d1cc1SJoe Perches
302c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
303c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
3047429c690SAndy Whitcroftmy $dbg_type = 0;
305a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
306c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
30721caa13cSAndy Whitcroft	## no critic
30821caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
30921caa13cSAndy Whitcroft	die "$@" if ($@);
310c2fdda0dSAndy Whitcroft}
311c2fdda0dSAndy Whitcroft
312d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
313d2c0a235SAndy Whitcroft
3148905a67cSAndy Whitcroftif ($terse) {
3158905a67cSAndy Whitcroft	$emacs = 1;
3168905a67cSAndy Whitcroft	$quiet++;
3178905a67cSAndy Whitcroft}
3188905a67cSAndy Whitcroft
3196c72ffaaSAndy Whitcroftif ($tree) {
3206c72ffaaSAndy Whitcroft	if (defined $root) {
3216c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
3226c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
3236c72ffaaSAndy Whitcroft		}
3246c72ffaaSAndy Whitcroft	} else {
3256c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
3266c72ffaaSAndy Whitcroft			$root = '.';
3276c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
3286c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
3296c72ffaaSAndy Whitcroft			$root = $1;
3306c72ffaaSAndy Whitcroft		}
3316c72ffaaSAndy Whitcroft	}
3326c72ffaaSAndy Whitcroft
3336c72ffaaSAndy Whitcroft	if (!defined $root) {
3340a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3350a920b5bSAndy Whitcroft		exit(2);
3360a920b5bSAndy Whitcroft	}
3376c72ffaaSAndy Whitcroft}
3386c72ffaaSAndy Whitcroft
3396c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3406c72ffaaSAndy Whitcroft
3412ceb532bSAndy Whitcroftour $Ident	= qr{
3422ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3432ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3442ceb532bSAndy Whitcroft		}x;
3456c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3466c72ffaaSAndy Whitcroftour $Sparse	= qr{
3476c72ffaaSAndy Whitcroft			__user|
3486c72ffaaSAndy Whitcroft			__kernel|
3496c72ffaaSAndy Whitcroft			__force|
3506c72ffaaSAndy Whitcroft			__iomem|
3516c72ffaaSAndy Whitcroft			__must_check|
352417495edSAndy Whitcroft			__kprobes|
353165e72a6SSven Eckelmann			__ref|
35433aa4597SGeert Uytterhoeven			__refconst|
35533aa4597SGeert Uytterhoeven			__refdata|
356ad315455SBoqun Feng			__rcu|
357ad315455SBoqun Feng			__private
3586c72ffaaSAndy Whitcroft		}x;
359e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
360e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
361e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
362e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
363e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3648716de38SJoe Perches
36552131292SWolfram Sang# Notes to $Attribute:
36652131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3676c72ffaaSAndy Whitcroftour $Attribute	= qr{
3686c72ffaaSAndy Whitcroft			const|
36903f1df7dSJoe Perches			__percpu|
37003f1df7dSJoe Perches			__nocast|
37103f1df7dSJoe Perches			__safe|
37246d832f5SMichael S. Tsirkin			__bitwise|
37303f1df7dSJoe Perches			__packed__|
37403f1df7dSJoe Perches			__packed2__|
37503f1df7dSJoe Perches			__naked|
37603f1df7dSJoe Perches			__maybe_unused|
37703f1df7dSJoe Perches			__always_unused|
37803f1df7dSJoe Perches			__noreturn|
37903f1df7dSJoe Perches			__used|
38003f1df7dSJoe Perches			__cold|
381e23ef1f3SJoe Perches			__pure|
38203f1df7dSJoe Perches			__noclone|
38303f1df7dSJoe Perches			__deprecated|
3846c72ffaaSAndy Whitcroft			__read_mostly|
385c5967e98SJoe Perches			__ro_after_init|
3866c72ffaaSAndy Whitcroft			__kprobes|
3878716de38SJoe Perches			$InitAttribute|
38824e1d81aSAndy Whitcroft			____cacheline_aligned|
38924e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3905fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3915fe3af11SAndy Whitcroft			__weak
3926c72ffaaSAndy Whitcroft		  }x;
393c45dcabdSAndy Whitcroftour $Modifier;
39491cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3956c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3966c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3976c72ffaaSAndy Whitcroft
39895e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
39995e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
40095e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
40195e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
4022435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
403c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
404326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
405326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
406326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
40774349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
4082435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
409326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
410447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
41123f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
4126c72ffaaSAndy Whitcroftour $Operators	= qr{
4136c72ffaaSAndy Whitcroft			<=|>=|==|!=|
4146c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
41523f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
4166c72ffaaSAndy Whitcroft		  }x;
4176c72ffaaSAndy Whitcroft
41891cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
41991cb5195SJoe Perches
420ab7e23f3SJoe Perchesour $BasicType;
4218905a67cSAndy Whitcroftour $NonptrType;
4221813087dSJoe Perchesour $NonptrTypeMisordered;
4238716de38SJoe Perchesour $NonptrTypeWithAttr;
4248905a67cSAndy Whitcroftour $Type;
4251813087dSJoe Perchesour $TypeMisordered;
4268905a67cSAndy Whitcroftour $Declare;
4271813087dSJoe Perchesour $DeclareMisordered;
4288905a67cSAndy Whitcroft
42915662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
43015662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
431171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
432171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
433171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
434171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
435171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
436171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
437171ae1a4SAndy Whitcroft}x;
438171ae1a4SAndy Whitcroft
43915662b3eSJoe Perchesour $UTF8	= qr{
44015662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
44115662b3eSJoe Perches	| $NON_ASCII_UTF8
44215662b3eSJoe Perches}x;
44315662b3eSJoe Perches
444e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
445021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
446021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
447021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
448021158b4SJoe Perches)};
449e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
450fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4518ed22cadSAndy Whitcroft	atomic_t
4528ed22cadSAndy Whitcroft)};
453e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
454e6176fa4SJoe Perches	$typeC99Typedefs\b|
455e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
456e6176fa4SJoe Perches	$typeKernelTypedefs\b
457e6176fa4SJoe Perches)};
4588ed22cadSAndy Whitcroft
4596d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4606d32f7a3SJoe Perches
461691e669bSJoe Perchesour $logFunctions = qr{(?x:
462758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
4637d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
46487bd499aSJoe Perches	TP_printk|
4656e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
466b0531722SJoe Perches	panic|
46706668727SJoe Perches	MODULE_[A-Z_]+|
46806668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
469691e669bSJoe Perches)};
470691e669bSJoe Perches
471e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
472e29a70f1SJoe Perches	(?:(?:devm_)?
473e29a70f1SJoe Perches		(?:kv|k|v)[czm]alloc(?:_node|_array)? |
474e29a70f1SJoe Perches		kstrdup(?:_const)? |
475e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
476e29a70f1SJoe Perches	(?:\w+)?alloc_skb(?:ip_align)? |
477e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
478e29a70f1SJoe Perches	dma_alloc_coherent
479e29a70f1SJoe Perches)};
480e29a70f1SJoe Perches
48120112475SJoe Perchesour $signature_tags = qr{(?xi:
48220112475SJoe Perches	Signed-off-by:|
483d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
48420112475SJoe Perches	Acked-by:|
48520112475SJoe Perches	Tested-by:|
48620112475SJoe Perches	Reviewed-by:|
48720112475SJoe Perches	Reported-by:|
4888543ae12SMugunthan V N	Suggested-by:|
48920112475SJoe Perches	To:|
49020112475SJoe Perches	Cc:
49120112475SJoe Perches)};
49220112475SJoe Perches
4931813087dSJoe Perchesour @typeListMisordered = (
4941813087dSJoe Perches	qr{char\s+(?:un)?signed},
4951813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4961813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4971813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4981813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4991813087dSJoe Perches	qr{short\s+(?:un)?signed},
5001813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
5011813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
5021813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
5031813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
5041813087dSJoe Perches	qr{int\s+(?:un)?signed},
5051813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
5061813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
5071813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
5081813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
5091813087dSJoe Perches	qr{long\s+(?:un)?signed},
5101813087dSJoe Perches);
5111813087dSJoe Perches
5128905a67cSAndy Whitcroftour @typeList = (
5138905a67cSAndy Whitcroft	qr{void},
5140c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
5150c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
5160c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
5170c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
5180c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
5190c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
5200c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
5210c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
5220c773d9dSJoe Perches	qr{(?:un)?signed},
5238905a67cSAndy Whitcroft	qr{float},
5248905a67cSAndy Whitcroft	qr{double},
5258905a67cSAndy Whitcroft	qr{bool},
5268905a67cSAndy Whitcroft	qr{struct\s+$Ident},
5278905a67cSAndy Whitcroft	qr{union\s+$Ident},
5288905a67cSAndy Whitcroft	qr{enum\s+$Ident},
5298905a67cSAndy Whitcroft	qr{${Ident}_t},
5308905a67cSAndy Whitcroft	qr{${Ident}_handler},
5318905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
5321813087dSJoe Perches	@typeListMisordered,
5338905a67cSAndy Whitcroft);
534938224b5SJoe Perches
535938224b5SJoe Perchesour $C90_int_types = qr{(?x:
536938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
537938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
538938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
539938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
540938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
541938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
542938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
543938224b5SJoe Perches
544938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
545938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
546938224b5SJoe Perches	long\s+(?:un)?signed|
547938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
548938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
549938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
550938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
551938224b5SJoe Perches
552938224b5SJoe Perches	int\s+(?:un)?signed|
553938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
554938224b5SJoe Perches)};
555938224b5SJoe Perches
556485ff23eSAlex Dowadour @typeListFile = ();
5578716de38SJoe Perchesour @typeListWithAttr = (
5588716de38SJoe Perches	@typeList,
5598716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5608716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5618716de38SJoe Perches);
5628716de38SJoe Perches
563c45dcabdSAndy Whitcroftour @modifierList = (
564c45dcabdSAndy Whitcroft	qr{fastcall},
565c45dcabdSAndy Whitcroft);
566485ff23eSAlex Dowadour @modifierListFile = ();
5678905a67cSAndy Whitcroft
5682435880fSJoe Perchesour @mode_permission_funcs = (
5692435880fSJoe Perches	["module_param", 3],
5702435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5712435880fSJoe Perches	["module_param_array_named", 5],
5722435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5732435880fSJoe Perches	["proc_create(?:_data|)", 2],
574459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
575459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
576459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
577459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
578459cf0aeSJoe Perches	["__ATTR", 2],
5792435880fSJoe Perches);
5802435880fSJoe Perches
581515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
582515a235eSJoe Perchesour $mode_perms_search = "";
583515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
584515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
585515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
586515a235eSJoe Perches}
58700180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
588515a235eSJoe Perches
5899189c7e7SJoe Perchesour %deprecated_apis = (
5909189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
5919189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
5929189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
5939189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
5949189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
5959189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
5969189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
5979189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
5989189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
5999189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
6009189c7e7SJoe Perches);
6019189c7e7SJoe Perches
6029189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
6039189c7e7SJoe Perchesour $deprecated_apis_search = "";
6049189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
6059189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
6069189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
6079189c7e7SJoe Perches}
6089189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
6099189c7e7SJoe Perches
610b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
611b392c64fSJoe Perches	S_IWUGO		|
612b392c64fSJoe Perches	S_IWOTH		|
613b392c64fSJoe Perches	S_IRWXUGO	|
614b392c64fSJoe Perches	S_IALLUGO	|
615b392c64fSJoe Perches	0[0-7][0-7][2367]
616b392c64fSJoe Perches}x;
617b392c64fSJoe Perches
618f90774e1SJoe Perchesour %mode_permission_string_types = (
619f90774e1SJoe Perches	"S_IRWXU" => 0700,
620f90774e1SJoe Perches	"S_IRUSR" => 0400,
621f90774e1SJoe Perches	"S_IWUSR" => 0200,
622f90774e1SJoe Perches	"S_IXUSR" => 0100,
623f90774e1SJoe Perches	"S_IRWXG" => 0070,
624f90774e1SJoe Perches	"S_IRGRP" => 0040,
625f90774e1SJoe Perches	"S_IWGRP" => 0020,
626f90774e1SJoe Perches	"S_IXGRP" => 0010,
627f90774e1SJoe Perches	"S_IRWXO" => 0007,
628f90774e1SJoe Perches	"S_IROTH" => 0004,
629f90774e1SJoe Perches	"S_IWOTH" => 0002,
630f90774e1SJoe Perches	"S_IXOTH" => 0001,
631f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
632f90774e1SJoe Perches	"S_IRUGO" => 0444,
633f90774e1SJoe Perches	"S_IWUGO" => 0222,
634f90774e1SJoe Perches	"S_IXUGO" => 0111,
635f90774e1SJoe Perches);
636f90774e1SJoe Perches
637f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
638f90774e1SJoe Perchesour $mode_perms_string_search = "";
639f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
640f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
641f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
642f90774e1SJoe Perches}
64300180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
64400180468SJoe Perchesour $multi_mode_perms_string_search = qr{
64500180468SJoe Perches	${single_mode_perms_string_search}
64600180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
64700180468SJoe Perches}x;
64800180468SJoe Perches
64900180468SJoe Perchessub perms_to_octal {
65000180468SJoe Perches	my ($string) = @_;
65100180468SJoe Perches
65200180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
65300180468SJoe Perches
65400180468SJoe Perches	my $val = "";
65500180468SJoe Perches	my $oval = "";
65600180468SJoe Perches	my $to = 0;
65700180468SJoe Perches	my $curpos = 0;
65800180468SJoe Perches	my $lastpos = 0;
65900180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
66000180468SJoe Perches		$curpos = pos($string);
66100180468SJoe Perches		my $match = $2;
66200180468SJoe Perches		my $omatch = $1;
66300180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
66400180468SJoe Perches		$lastpos = $curpos;
66500180468SJoe Perches		$to |= $mode_permission_string_types{$match};
66600180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
66700180468SJoe Perches		$val .= $match;
66800180468SJoe Perches		$oval .= $omatch;
66900180468SJoe Perches	}
67000180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
67100180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
67200180468SJoe Perches	return sprintf("%04o", $to);
67300180468SJoe Perches}
674f90774e1SJoe Perches
6757840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
6767840a94cSWolfram Sang	irq|
677cdcee686SSergey Ryazanov	memory|
678cdcee686SSergey Ryazanov	time|
679cdcee686SSergey Ryazanov	reboot
6807840a94cSWolfram Sang)};
6817840a94cSWolfram Sang# memory.h: ARM has a custom one
6827840a94cSWolfram Sang
68366b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
68466b47b4aSKees Cookmy $misspellings;
68566b47b4aSKees Cookmy %spelling_fix;
68636061e38SJoe Perches
68736061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
68866b47b4aSKees Cook	while (<$spelling>) {
68966b47b4aSKees Cook		my $line = $_;
69066b47b4aSKees Cook
69166b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
69266b47b4aSKees Cook		$line =~ s/^\s*//g;
69366b47b4aSKees Cook
69466b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
69566b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
69666b47b4aSKees Cook
69766b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
69866b47b4aSKees Cook
69966b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
70066b47b4aSKees Cook	}
70166b47b4aSKees Cook	close($spelling);
70236061e38SJoe Perches} else {
70336061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
70436061e38SJoe Perches}
70566b47b4aSKees Cook
706ebfd7d62SJoe Perchesif ($codespell) {
707ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
708ebfd7d62SJoe Perches		while (<$spelling>) {
709ebfd7d62SJoe Perches			my $line = $_;
710ebfd7d62SJoe Perches
711ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
712ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
713ebfd7d62SJoe Perches
714ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
715ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
716ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
717ebfd7d62SJoe Perches
718ebfd7d62SJoe Perches			$line =~ s/,.*$//;
719ebfd7d62SJoe Perches
720ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
721ebfd7d62SJoe Perches
722ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
723ebfd7d62SJoe Perches		}
724ebfd7d62SJoe Perches		close($spelling);
725ebfd7d62SJoe Perches	} else {
726ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
727ebfd7d62SJoe Perches	}
728ebfd7d62SJoe Perches}
729ebfd7d62SJoe Perches
730ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
731ebfd7d62SJoe Perches
73275ad8c57SJerome Forissiersub read_words {
73375ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
73475ad8c57SJerome Forissier
73575ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
73675ad8c57SJerome Forissier		while (<$words>) {
737bf1fa1daSJoe Perches			my $line = $_;
738bf1fa1daSJoe Perches
739bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
740bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
741bf1fa1daSJoe Perches
742bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
743bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
744bf1fa1daSJoe Perches			if ($line =~ /\s/) {
74575ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
746bf1fa1daSJoe Perches				next;
747bf1fa1daSJoe Perches			}
748bf1fa1daSJoe Perches
74975ad8c57SJerome Forissier			$$wordsRef .= '|' if ($$wordsRef ne "");
75075ad8c57SJerome Forissier			$$wordsRef .= $line;
751bf1fa1daSJoe Perches		}
75275ad8c57SJerome Forissier		close($file);
75375ad8c57SJerome Forissier		return 1;
754bf1fa1daSJoe Perches	}
755bf1fa1daSJoe Perches
75675ad8c57SJerome Forissier	return 0;
75775ad8c57SJerome Forissier}
75875ad8c57SJerome Forissier
75975ad8c57SJerome Forissiermy $const_structs = "";
76075ad8c57SJerome Forissierread_words(\$const_structs, $conststructsfile)
76175ad8c57SJerome Forissier    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
76275ad8c57SJerome Forissier
76375ad8c57SJerome Forissiermy $typeOtherTypedefs = "";
76475ad8c57SJerome Forissierif (length($typedefsfile)) {
76575ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
76675ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
76775ad8c57SJerome Forissier}
76875ad8c57SJerome Forissier$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
76975ad8c57SJerome Forissier
7708905a67cSAndy Whitcroftsub build_types {
771485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
772485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
7731813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
7748716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
775c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
776ab7e23f3SJoe Perches	$BasicType	= qr{
777ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
778ab7e23f3SJoe Perches				(?:${all}\b)
779ab7e23f3SJoe Perches		}x;
7808905a67cSAndy Whitcroft	$NonptrType	= qr{
781d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
782cf655043SAndy Whitcroft			(?:
7836b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
7848ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
785c45dcabdSAndy Whitcroft				(?:${all}\b)
786cf655043SAndy Whitcroft			)
787c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
7888905a67cSAndy Whitcroft		  }x;
7891813087dSJoe Perches	$NonptrTypeMisordered	= qr{
7901813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
7911813087dSJoe Perches			(?:
7921813087dSJoe Perches				(?:${Misordered}\b)
7931813087dSJoe Perches			)
7941813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
7951813087dSJoe Perches		  }x;
7968716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
7978716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
7988716de38SJoe Perches			(?:
7998716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
8008716de38SJoe Perches				(?:$typeTypedefs\b)|
8018716de38SJoe Perches				(?:${allWithAttr}\b)
8028716de38SJoe Perches			)
8038716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
8048716de38SJoe Perches		  }x;
8058905a67cSAndy Whitcroft	$Type	= qr{
806c45dcabdSAndy Whitcroft			$NonptrType
8071574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
808c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
8098905a67cSAndy Whitcroft		  }x;
8101813087dSJoe Perches	$TypeMisordered	= qr{
8111813087dSJoe Perches			$NonptrTypeMisordered
8121813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
8131813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
8141813087dSJoe Perches		  }x;
81591cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
8161813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
8178905a67cSAndy Whitcroft}
8188905a67cSAndy Whitcroftbuild_types();
8196c72ffaaSAndy Whitcroft
8207d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
821d1fe9c09SJoe Perches
822d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
823d1fe9c09SJoe Perches# requires at least perl version v5.10.0
824d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
825d1fe9c09SJoe Perches
826d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
8272435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
828c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
8297d2367afSJoe Perches
830f8422308SJoe Perchesour $declaration_macros = qr{(?x:
8313e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
832fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
8333d102fc0SGilad Ben-Yossef	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(|
8343d102fc0SGilad Ben-Yossef	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
835f8422308SJoe Perches)};
836f8422308SJoe Perches
8377d2367afSJoe Perchessub deparenthesize {
8387d2367afSJoe Perches	my ($string) = @_;
8397d2367afSJoe Perches	return "" if (!defined($string));
8405b9553abSJoe Perches
8415b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
8425b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
8435b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
8445b9553abSJoe Perches	}
8455b9553abSJoe Perches
8467d2367afSJoe Perches	$string =~ s@\s+@ @g;
8475b9553abSJoe Perches
8487d2367afSJoe Perches	return $string;
8497d2367afSJoe Perches}
8507d2367afSJoe Perches
8513445686aSJoe Perchessub seed_camelcase_file {
8523445686aSJoe Perches	my ($file) = @_;
8533445686aSJoe Perches
8543445686aSJoe Perches	return if (!(-f $file));
8553445686aSJoe Perches
8563445686aSJoe Perches	local $/;
8573445686aSJoe Perches
8583445686aSJoe Perches	open(my $include_file, '<', "$file")
8593445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
8603445686aSJoe Perches	my $text = <$include_file>;
8613445686aSJoe Perches	close($include_file);
8623445686aSJoe Perches
8633445686aSJoe Perches	my @lines = split('\n', $text);
8643445686aSJoe Perches
8653445686aSJoe Perches	foreach my $line (@lines) {
8663445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
8673445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
8683445686aSJoe Perches			$camelcase{$1} = 1;
86911ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
87011ea516aSJoe Perches			$camelcase{$1} = 1;
87111ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
8723445686aSJoe Perches			$camelcase{$1} = 1;
8733445686aSJoe Perches		}
8743445686aSJoe Perches	}
8753445686aSJoe Perches}
8763445686aSJoe Perches
87785b0ee18SJoe Perchessub is_maintained_obsolete {
87885b0ee18SJoe Perches	my ($filename) = @_;
87985b0ee18SJoe Perches
880f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
88185b0ee18SJoe Perches
8820616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
88385b0ee18SJoe Perches
88485b0ee18SJoe Perches	return $status =~ /obsolete/i;
88585b0ee18SJoe Perches}
88685b0ee18SJoe Perches
8873b6e8ac9SJoe Perchessub is_SPDX_License_valid {
8883b6e8ac9SJoe Perches	my ($license) = @_;
8893b6e8ac9SJoe Perches
89056294112SJoe Perches	return 1 if (!$tree || which("python") eq "" || !(-e "$root/scripts/spdxcheck.py") || !(-e "$root/.git"));
8913b6e8ac9SJoe Perches
89256294112SJoe Perches	my $root_path = abs_path($root);
89356294112SJoe Perches	my $status = `cd "$root_path"; echo "$license" | python scripts/spdxcheck.py -`;
8943b6e8ac9SJoe Perches	return 0 if ($status ne "");
8953b6e8ac9SJoe Perches	return 1;
8963b6e8ac9SJoe Perches}
8973b6e8ac9SJoe Perches
8983445686aSJoe Perchesmy $camelcase_seeded = 0;
8993445686aSJoe Perchessub seed_camelcase_includes {
9003445686aSJoe Perches	return if ($camelcase_seeded);
9013445686aSJoe Perches
9023445686aSJoe Perches	my $files;
903c707a81dSJoe Perches	my $camelcase_cache = "";
904c707a81dSJoe Perches	my @include_files = ();
905c707a81dSJoe Perches
906c707a81dSJoe Perches	$camelcase_seeded = 1;
907351b2a1fSJoe Perches
9083645e328SRichard Genoud	if (-e ".git") {
909dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
910351b2a1fSJoe Perches		chomp $git_last_include_commit;
911c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
912c707a81dSJoe Perches	} else {
913c707a81dSJoe Perches		my $last_mod_date = 0;
914c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
915c707a81dSJoe Perches		@include_files = split('\n', $files);
916c707a81dSJoe Perches		foreach my $file (@include_files) {
917c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
918c707a81dSJoe Perches						   localtime((stat $file)[9]));
919c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
920c707a81dSJoe Perches		}
921c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
922c707a81dSJoe Perches	}
923c707a81dSJoe Perches
924c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
925c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
926c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
927351b2a1fSJoe Perches		while (<$camelcase_file>) {
928351b2a1fSJoe Perches			chomp;
929351b2a1fSJoe Perches			$camelcase{$_} = 1;
930351b2a1fSJoe Perches		}
931351b2a1fSJoe Perches		close($camelcase_file);
932351b2a1fSJoe Perches
933351b2a1fSJoe Perches		return;
934351b2a1fSJoe Perches	}
935c707a81dSJoe Perches
9363645e328SRichard Genoud	if (-e ".git") {
937dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
938c707a81dSJoe Perches		@include_files = split('\n', $files);
9393445686aSJoe Perches	}
940c707a81dSJoe Perches
9413445686aSJoe Perches	foreach my $file (@include_files) {
9423445686aSJoe Perches		seed_camelcase_file($file);
9433445686aSJoe Perches	}
944351b2a1fSJoe Perches
945c707a81dSJoe Perches	if ($camelcase_cache ne "") {
946351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
947c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
948c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
949351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
950351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
951351b2a1fSJoe Perches		}
952351b2a1fSJoe Perches		close($camelcase_file);
953351b2a1fSJoe Perches	}
9543445686aSJoe Perches}
9553445686aSJoe Perches
956d311cd44SJoe Perchessub git_commit_info {
957d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
958d311cd44SJoe Perches
959d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
960d311cd44SJoe Perches
961dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
962d311cd44SJoe Perches	$output =~ s/^\s*//gm;
963d311cd44SJoe Perches	my @lines = split("\n", $output);
964d311cd44SJoe Perches
9650d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
9660d7835fcSJoe Perches
9675a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
968d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
969d311cd44SJoe Perches# all matching commit ids, but it's very slow...
970d311cd44SJoe Perches#
971d311cd44SJoe Perches#		echo "checking commits $1..."
972d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
973d311cd44SJoe Perches#		while read line ; do
974d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
975d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
976d311cd44SJoe Perches#		done
977d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
978948b133aSHeinrich Schuchardt		$id = undef;
979d311cd44SJoe Perches	} else {
980d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
981d311cd44SJoe Perches		$desc = substr($lines[0], 41);
982d311cd44SJoe Perches	}
983d311cd44SJoe Perches
984d311cd44SJoe Perches	return ($id, $desc);
985d311cd44SJoe Perches}
986d311cd44SJoe Perches
9876c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
9880a920b5bSAndy Whitcroft
98900df344fSAndy Whitcroftmy @rawlines = ();
990c2fdda0dSAndy Whitcroftmy @lines = ();
9913705ce5bSJoe Perchesmy @fixed = ();
992d752fcc8SJoe Perchesmy @fixed_inserted = ();
993d752fcc8SJoe Perchesmy @fixed_deleted = ();
994194f66fcSJoe Perchesmy $fixlinenr = -1;
995194f66fcSJoe Perches
9964a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
9974a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
9984a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
9994a593c34SDu, Changbin
10004a593c34SDu, Changbinif ($git) {
10014a593c34SDu, Changbin	my @commits = ();
10020dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
10034a593c34SDu, Changbin		my $git_range;
100428898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
100528898fd1SJoe Perches			$git_range = "-$2 $1";
10064a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
10074a593c34SDu, Changbin			$git_range = "$commit_expr";
10084a593c34SDu, Changbin		} else {
10090dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
10100dea9f1eSJoe Perches		}
1011dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
10120dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
101328898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
101428898fd1SJoe Perches			next if (!defined($1) || !defined($2));
10150dea9f1eSJoe Perches			my $sha1 = $1;
10160dea9f1eSJoe Perches			my $subject = $2;
10170dea9f1eSJoe Perches			unshift(@commits, $sha1);
10180dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
10194a593c34SDu, Changbin		}
10204a593c34SDu, Changbin	}
10214a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
10224a593c34SDu, Changbin	@ARGV = @commits;
10234a593c34SDu, Changbin}
10244a593c34SDu, Changbin
1025c2fdda0dSAndy Whitcroftmy $vname;
102698005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
10276c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
102821caa13cSAndy Whitcroft	my $FILE;
10294a593c34SDu, Changbin	if ($git) {
10304a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
10314a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
10324a593c34SDu, Changbin	} elsif ($file) {
103321caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
10346c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
103521caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
103621caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
10376c72ffaaSAndy Whitcroft	} else {
103821caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
10396c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
10406c72ffaaSAndy Whitcroft	}
1041c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1042c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
10434a593c34SDu, Changbin	} elsif ($git) {
10440dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1045c2fdda0dSAndy Whitcroft	} else {
1046c2fdda0dSAndy Whitcroft		$vname = $filename;
1047c2fdda0dSAndy Whitcroft	}
104821caa13cSAndy Whitcroft	while (<$FILE>) {
10490a920b5bSAndy Whitcroft		chomp;
105000df344fSAndy Whitcroft		push(@rawlines, $_);
10516c72ffaaSAndy Whitcroft	}
105221caa13cSAndy Whitcroft	close($FILE);
1053d8469f16SJoe Perches
1054d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1055d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1056d8469f16SJoe Perches		print "$vname\n";
1057d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1058d8469f16SJoe Perches	}
1059d8469f16SJoe Perches
1060c2fdda0dSAndy Whitcroft	if (!process($filename)) {
10610a920b5bSAndy Whitcroft		$exit = 1;
10620a920b5bSAndy Whitcroft	}
106300df344fSAndy Whitcroft	@rawlines = ();
106413214adfSAndy Whitcroft	@lines = ();
10653705ce5bSJoe Perches	@fixed = ();
1066d752fcc8SJoe Perches	@fixed_inserted = ();
1067d752fcc8SJoe Perches	@fixed_deleted = ();
1068194f66fcSJoe Perches	$fixlinenr = -1;
1069485ff23eSAlex Dowad	@modifierListFile = ();
1070485ff23eSAlex Dowad	@typeListFile = ();
1071485ff23eSAlex Dowad	build_types();
10720a920b5bSAndy Whitcroft}
10730a920b5bSAndy Whitcroft
1074d8469f16SJoe Perchesif (!$quiet) {
10753c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
10763c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
10773c816e49SJoe Perches
10785b57980dSJoe Perches	if (!$perl_version_ok) {
1079d8469f16SJoe Perches		print << "EOM"
1080d8469f16SJoe Perches
1081d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
10825b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1083d8469f16SJoe PerchesEOM
1084d8469f16SJoe Perches	}
1085d8469f16SJoe Perches	if ($exit) {
1086d8469f16SJoe Perches		print << "EOM"
1087d8469f16SJoe Perches
1088d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1089d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1090d8469f16SJoe PerchesEOM
1091d8469f16SJoe Perches	}
1092d8469f16SJoe Perches}
1093d8469f16SJoe Perches
10940a920b5bSAndy Whitcroftexit($exit);
10950a920b5bSAndy Whitcroft
10960a920b5bSAndy Whitcroftsub top_of_kernel_tree {
10976c72ffaaSAndy Whitcroft	my ($root) = @_;
10986c72ffaaSAndy Whitcroft
10996c72ffaaSAndy Whitcroft	my @tree_check = (
11006c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
11016c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
11026c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
11036c72ffaaSAndy Whitcroft	);
11046c72ffaaSAndy Whitcroft
11056c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
11066c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
11070a920b5bSAndy Whitcroft			return 0;
11080a920b5bSAndy Whitcroft		}
11096c72ffaaSAndy Whitcroft	}
11106c72ffaaSAndy Whitcroft	return 1;
11116c72ffaaSAndy Whitcroft}
11120a920b5bSAndy Whitcroft
111320112475SJoe Perchessub parse_email {
111420112475SJoe Perches	my ($formatted_email) = @_;
111520112475SJoe Perches
111620112475SJoe Perches	my $name = "";
111720112475SJoe Perches	my $address = "";
111820112475SJoe Perches	my $comment = "";
111920112475SJoe Perches
112020112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
112120112475SJoe Perches		$name = $1;
112220112475SJoe Perches		$address = $2;
112320112475SJoe Perches		$comment = $3 if defined $3;
112420112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
112520112475SJoe Perches		$address = $1;
112620112475SJoe Perches		$comment = $2 if defined $2;
112720112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
112820112475SJoe Perches		$address = $1;
112920112475SJoe Perches		$comment = $2 if defined $2;
113085e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
113120112475SJoe Perches		$name = $formatted_email;
11323705ce5bSJoe Perches		$name = trim($name);
113320112475SJoe Perches		$name =~ s/^\"|\"$//g;
113420112475SJoe Perches		# If there's a name left after stripping spaces and
113520112475SJoe Perches		# leading quotes, and the address doesn't have both
113620112475SJoe Perches		# leading and trailing angle brackets, the address
113720112475SJoe Perches		# is invalid. ie:
113820112475SJoe Perches		#   "joe smith [email protected]" bad
113920112475SJoe Perches		#   "joe smith <[email protected]" bad
114020112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
114120112475SJoe Perches			$name = "";
114220112475SJoe Perches			$address = "";
114320112475SJoe Perches			$comment = "";
114420112475SJoe Perches		}
114520112475SJoe Perches	}
114620112475SJoe Perches
11473705ce5bSJoe Perches	$name = trim($name);
114820112475SJoe Perches	$name =~ s/^\"|\"$//g;
11493705ce5bSJoe Perches	$address = trim($address);
115020112475SJoe Perches	$address =~ s/^\<|\>$//g;
115120112475SJoe Perches
115220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
115320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
115420112475SJoe Perches		$name = "\"$name\"";
115520112475SJoe Perches	}
115620112475SJoe Perches
115720112475SJoe Perches	return ($name, $address, $comment);
115820112475SJoe Perches}
115920112475SJoe Perches
116020112475SJoe Perchessub format_email {
116120112475SJoe Perches	my ($name, $address) = @_;
116220112475SJoe Perches
116320112475SJoe Perches	my $formatted_email;
116420112475SJoe Perches
11653705ce5bSJoe Perches	$name = trim($name);
116620112475SJoe Perches	$name =~ s/^\"|\"$//g;
11673705ce5bSJoe Perches	$address = trim($address);
116820112475SJoe Perches
116920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
117020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
117120112475SJoe Perches		$name = "\"$name\"";
117220112475SJoe Perches	}
117320112475SJoe Perches
117420112475SJoe Perches	if ("$name" eq "") {
117520112475SJoe Perches		$formatted_email = "$address";
117620112475SJoe Perches	} else {
117720112475SJoe Perches		$formatted_email = "$name <$address>";
117820112475SJoe Perches	}
117920112475SJoe Perches
118020112475SJoe Perches	return $formatted_email;
118120112475SJoe Perches}
118220112475SJoe Perches
1183d311cd44SJoe Perchessub which {
1184d311cd44SJoe Perches	my ($bin) = @_;
1185d311cd44SJoe Perches
1186d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1187d311cd44SJoe Perches		if (-e "$path/$bin") {
1188d311cd44SJoe Perches			return "$path/$bin";
1189d311cd44SJoe Perches		}
1190d311cd44SJoe Perches	}
1191d311cd44SJoe Perches
1192d311cd44SJoe Perches	return "";
1193d311cd44SJoe Perches}
1194d311cd44SJoe Perches
1195000d1cc1SJoe Perchessub which_conf {
1196000d1cc1SJoe Perches	my ($conf) = @_;
1197000d1cc1SJoe Perches
1198000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1199000d1cc1SJoe Perches		if (-e "$path/$conf") {
1200000d1cc1SJoe Perches			return "$path/$conf";
1201000d1cc1SJoe Perches		}
1202000d1cc1SJoe Perches	}
1203000d1cc1SJoe Perches
1204000d1cc1SJoe Perches	return "";
1205000d1cc1SJoe Perches}
1206000d1cc1SJoe Perches
12070a920b5bSAndy Whitcroftsub expand_tabs {
12080a920b5bSAndy Whitcroft	my ($str) = @_;
12090a920b5bSAndy Whitcroft
12100a920b5bSAndy Whitcroft	my $res = '';
12110a920b5bSAndy Whitcroft	my $n = 0;
12120a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
12130a920b5bSAndy Whitcroft		if ($c eq "\t") {
12140a920b5bSAndy Whitcroft			$res .= ' ';
12150a920b5bSAndy Whitcroft			$n++;
12160a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
12170a920b5bSAndy Whitcroft				$res .= ' ';
12180a920b5bSAndy Whitcroft			}
12190a920b5bSAndy Whitcroft			next;
12200a920b5bSAndy Whitcroft		}
12210a920b5bSAndy Whitcroft		$res .= $c;
12220a920b5bSAndy Whitcroft		$n++;
12230a920b5bSAndy Whitcroft	}
12240a920b5bSAndy Whitcroft
12250a920b5bSAndy Whitcroft	return $res;
12260a920b5bSAndy Whitcroft}
12276c72ffaaSAndy Whitcroftsub copy_spacing {
1228773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
12296c72ffaaSAndy Whitcroft	return $res;
12306c72ffaaSAndy Whitcroft}
12310a920b5bSAndy Whitcroft
12324a0df2efSAndy Whitcroftsub line_stats {
12334a0df2efSAndy Whitcroft	my ($line) = @_;
12344a0df2efSAndy Whitcroft
12354a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
12364a0df2efSAndy Whitcroft	$line =~ s/^.//;
12374a0df2efSAndy Whitcroft	$line = expand_tabs($line);
12384a0df2efSAndy Whitcroft
12394a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
12404a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
12414a0df2efSAndy Whitcroft
12424a0df2efSAndy Whitcroft	return (length($line), length($white));
12434a0df2efSAndy Whitcroft}
12444a0df2efSAndy Whitcroft
1245773647a0SAndy Whitcroftmy $sanitise_quote = '';
1246773647a0SAndy Whitcroft
1247773647a0SAndy Whitcroftsub sanitise_line_reset {
1248773647a0SAndy Whitcroft	my ($in_comment) = @_;
1249773647a0SAndy Whitcroft
1250773647a0SAndy Whitcroft	if ($in_comment) {
1251773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1252773647a0SAndy Whitcroft	} else {
1253773647a0SAndy Whitcroft		$sanitise_quote = '';
1254773647a0SAndy Whitcroft	}
1255773647a0SAndy Whitcroft}
125600df344fSAndy Whitcroftsub sanitise_line {
125700df344fSAndy Whitcroft	my ($line) = @_;
125800df344fSAndy Whitcroft
125900df344fSAndy Whitcroft	my $res = '';
126000df344fSAndy Whitcroft	my $l = '';
126100df344fSAndy Whitcroft
1262c2fdda0dSAndy Whitcroft	my $qlen = 0;
1263773647a0SAndy Whitcroft	my $off = 0;
1264773647a0SAndy Whitcroft	my $c;
126500df344fSAndy Whitcroft
1266773647a0SAndy Whitcroft	# Always copy over the diff marker.
1267773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1268773647a0SAndy Whitcroft
1269773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1270773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1271773647a0SAndy Whitcroft
12728d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1273773647a0SAndy Whitcroft		# and end, all to $;.
1274773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1275773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1276773647a0SAndy Whitcroft
1277773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1278773647a0SAndy Whitcroft			$off++;
127900df344fSAndy Whitcroft			next;
1280773647a0SAndy Whitcroft		}
128181bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1282773647a0SAndy Whitcroft			$sanitise_quote = '';
1283773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1284773647a0SAndy Whitcroft			$off++;
1285773647a0SAndy Whitcroft			next;
1286773647a0SAndy Whitcroft		}
1287113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1288113f04a8SDaniel Walker			$sanitise_quote = '//';
1289113f04a8SDaniel Walker
1290113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1291113f04a8SDaniel Walker			$off++;
1292113f04a8SDaniel Walker			next;
1293113f04a8SDaniel Walker		}
1294773647a0SAndy Whitcroft
1295773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1296773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1297773647a0SAndy Whitcroft		    $c eq "\\") {
1298773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1299773647a0SAndy Whitcroft			$off++;
1300773647a0SAndy Whitcroft			next;
1301773647a0SAndy Whitcroft		}
1302773647a0SAndy Whitcroft		# Regular quotes.
1303773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1304773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1305773647a0SAndy Whitcroft				$sanitise_quote = $c;
1306773647a0SAndy Whitcroft
1307773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1308773647a0SAndy Whitcroft				next;
1309773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1310773647a0SAndy Whitcroft				$sanitise_quote = '';
131100df344fSAndy Whitcroft			}
131200df344fSAndy Whitcroft		}
1313773647a0SAndy Whitcroft
1314fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1315773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1316773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1317113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1318113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1319773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1320773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
132100df344fSAndy Whitcroft		} else {
1322773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
132300df344fSAndy Whitcroft		}
1324c2fdda0dSAndy Whitcroft	}
1325c2fdda0dSAndy Whitcroft
1326113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1327113f04a8SDaniel Walker		$sanitise_quote = '';
1328113f04a8SDaniel Walker	}
1329113f04a8SDaniel Walker
1330c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1331c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1332c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1333c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1334c2fdda0dSAndy Whitcroft
1335c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1336c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1337c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1338c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1339c2fdda0dSAndy Whitcroft	}
1340c2fdda0dSAndy Whitcroft
1341dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1342dadf680dSJoe Perches		my $match = $1;
1343dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1344dadf680dSJoe Perches	}
1345dadf680dSJoe Perches
134600df344fSAndy Whitcroft	return $res;
134700df344fSAndy Whitcroft}
134800df344fSAndy Whitcroft
1349a6962d72SJoe Perchessub get_quoted_string {
1350a6962d72SJoe Perches	my ($line, $rawline) = @_;
1351a6962d72SJoe Perches
1352478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
135333acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1354a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1355a6962d72SJoe Perches}
1356a6962d72SJoe Perches
13578905a67cSAndy Whitcroftsub ctx_statement_block {
13588905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13598905a67cSAndy Whitcroft	my $line = $linenr - 1;
13608905a67cSAndy Whitcroft	my $blk = '';
13618905a67cSAndy Whitcroft	my $soff = $off;
13628905a67cSAndy Whitcroft	my $coff = $off - 1;
1363773647a0SAndy Whitcroft	my $coff_set = 0;
13648905a67cSAndy Whitcroft
136513214adfSAndy Whitcroft	my $loff = 0;
136613214adfSAndy Whitcroft
13678905a67cSAndy Whitcroft	my $type = '';
13688905a67cSAndy Whitcroft	my $level = 0;
1369a2750645SAndy Whitcroft	my @stack = ();
1370cf655043SAndy Whitcroft	my $p;
13718905a67cSAndy Whitcroft	my $c;
13728905a67cSAndy Whitcroft	my $len = 0;
137313214adfSAndy Whitcroft
137413214adfSAndy Whitcroft	my $remainder;
13758905a67cSAndy Whitcroft	while (1) {
1376a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1377a2750645SAndy Whitcroft
1378773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
13798905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
13808905a67cSAndy Whitcroft		# context.
13818905a67cSAndy Whitcroft		if ($off >= $len) {
13828905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1383dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1384c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
13858905a67cSAndy Whitcroft				$remain--;
138613214adfSAndy Whitcroft				$loff = $len;
1387c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
13888905a67cSAndy Whitcroft				$len = length($blk);
13898905a67cSAndy Whitcroft				$line++;
13908905a67cSAndy Whitcroft				last;
13918905a67cSAndy Whitcroft			}
13928905a67cSAndy Whitcroft			# Bail if there is no further context.
13938905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
139413214adfSAndy Whitcroft			if ($off >= $len) {
13958905a67cSAndy Whitcroft				last;
13968905a67cSAndy Whitcroft			}
1397f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1398f74bd194SAndy Whitcroft				$level++;
1399f74bd194SAndy Whitcroft				$type = '#';
1400f74bd194SAndy Whitcroft			}
14018905a67cSAndy Whitcroft		}
1402cf655043SAndy Whitcroft		$p = $c;
14038905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
140413214adfSAndy Whitcroft		$remainder = substr($blk, $off);
14058905a67cSAndy Whitcroft
1406773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
14074635f4fbSAndy Whitcroft
14084635f4fbSAndy Whitcroft		# Handle nested #if/#else.
14094635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
14104635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
14114635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
14124635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
14134635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
14144635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
14154635f4fbSAndy Whitcroft		}
14164635f4fbSAndy Whitcroft
14178905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
14188905a67cSAndy Whitcroft		# outermost level.
14198905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
14208905a67cSAndy Whitcroft			last;
14218905a67cSAndy Whitcroft		}
14228905a67cSAndy Whitcroft
142313214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1424773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1425773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1426773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1427773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1428773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1429773647a0SAndy Whitcroft			$coff_set = 1;
1430773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1431773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
143213214adfSAndy Whitcroft		}
143313214adfSAndy Whitcroft
14348905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
14358905a67cSAndy Whitcroft			$level++;
14368905a67cSAndy Whitcroft			$type = '(';
14378905a67cSAndy Whitcroft		}
14388905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
14398905a67cSAndy Whitcroft			$level--;
14408905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
14418905a67cSAndy Whitcroft
14428905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
14438905a67cSAndy Whitcroft				$coff = $off;
1444773647a0SAndy Whitcroft				$coff_set = 1;
1445773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
14468905a67cSAndy Whitcroft			}
14478905a67cSAndy Whitcroft		}
14488905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
14498905a67cSAndy Whitcroft			$level++;
14508905a67cSAndy Whitcroft			$type = '{';
14518905a67cSAndy Whitcroft		}
14528905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
14538905a67cSAndy Whitcroft			$level--;
14548905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
14558905a67cSAndy Whitcroft
14568905a67cSAndy Whitcroft			if ($level == 0) {
1457b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1458b998e001SPatrick Pannuto					$off++;
1459b998e001SPatrick Pannuto				}
14608905a67cSAndy Whitcroft				last;
14618905a67cSAndy Whitcroft			}
14628905a67cSAndy Whitcroft		}
1463f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1464f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1465f74bd194SAndy Whitcroft			$level--;
1466f74bd194SAndy Whitcroft			$type = '';
1467f74bd194SAndy Whitcroft			$off++;
1468f74bd194SAndy Whitcroft			last;
1469f74bd194SAndy Whitcroft		}
14708905a67cSAndy Whitcroft		$off++;
14718905a67cSAndy Whitcroft	}
1472a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
147313214adfSAndy Whitcroft	if ($off == $len) {
1474a3bb97a7SAndy Whitcroft		$loff = $len + 1;
147513214adfSAndy Whitcroft		$line++;
147613214adfSAndy Whitcroft		$remain--;
147713214adfSAndy Whitcroft	}
14788905a67cSAndy Whitcroft
14798905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
14808905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
14818905a67cSAndy Whitcroft
14828905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
14838905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
14848905a67cSAndy Whitcroft
1485773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
148613214adfSAndy Whitcroft
148713214adfSAndy Whitcroft	return ($statement, $condition,
148813214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
148913214adfSAndy Whitcroft}
149013214adfSAndy Whitcroft
1491cf655043SAndy Whitcroftsub statement_lines {
1492cf655043SAndy Whitcroft	my ($stmt) = @_;
1493cf655043SAndy Whitcroft
1494cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1495cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1496cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1497cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1498cf655043SAndy Whitcroft
1499cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1500cf655043SAndy Whitcroft
1501cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1502cf655043SAndy Whitcroft}
1503cf655043SAndy Whitcroft
1504cf655043SAndy Whitcroftsub statement_rawlines {
1505cf655043SAndy Whitcroft	my ($stmt) = @_;
1506cf655043SAndy Whitcroft
1507cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1508cf655043SAndy Whitcroft
1509cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1510cf655043SAndy Whitcroft}
1511cf655043SAndy Whitcroft
1512cf655043SAndy Whitcroftsub statement_block_size {
1513cf655043SAndy Whitcroft	my ($stmt) = @_;
1514cf655043SAndy Whitcroft
1515cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1516cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1517cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1518cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1519cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1520cf655043SAndy Whitcroft
1521cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1522cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1523cf655043SAndy Whitcroft
1524cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1525cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1526cf655043SAndy Whitcroft
1527cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1528cf655043SAndy Whitcroft		return $stmt_lines;
1529cf655043SAndy Whitcroft	} else {
1530cf655043SAndy Whitcroft		return $stmt_statements;
1531cf655043SAndy Whitcroft	}
1532cf655043SAndy Whitcroft}
1533cf655043SAndy Whitcroft
153413214adfSAndy Whitcroftsub ctx_statement_full {
153513214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
153613214adfSAndy Whitcroft	my ($statement, $condition, $level);
153713214adfSAndy Whitcroft
153813214adfSAndy Whitcroft	my (@chunks);
153913214adfSAndy Whitcroft
1540cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
154113214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
154213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1543773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
154413214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1545cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1546cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1547cf655043SAndy Whitcroft	}
1548cf655043SAndy Whitcroft
1549cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1550cf655043SAndy Whitcroft	# could continue the statement.
1551cf655043SAndy Whitcroft	for (;;) {
155213214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
155313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1554cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1555773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1556cf655043SAndy Whitcroft		#print "C: push\n";
1557cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
155813214adfSAndy Whitcroft	}
155913214adfSAndy Whitcroft
156013214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
15618905a67cSAndy Whitcroft}
15628905a67cSAndy Whitcroft
15634a0df2efSAndy Whitcroftsub ctx_block_get {
1564f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
15654a0df2efSAndy Whitcroft	my $line;
15664a0df2efSAndy Whitcroft	my $start = $linenr - 1;
15674a0df2efSAndy Whitcroft	my $blk = '';
15684a0df2efSAndy Whitcroft	my @o;
15694a0df2efSAndy Whitcroft	my @c;
15704a0df2efSAndy Whitcroft	my @res = ();
15714a0df2efSAndy Whitcroft
1572f0a594c1SAndy Whitcroft	my $level = 0;
15734635f4fbSAndy Whitcroft	my @stack = ($level);
157400df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
157500df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
157600df344fSAndy Whitcroft		$remain--;
157700df344fSAndy Whitcroft
157800df344fSAndy Whitcroft		$blk .= $rawlines[$line];
15794635f4fbSAndy Whitcroft
15804635f4fbSAndy Whitcroft		# Handle nested #if/#else.
158101464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
15824635f4fbSAndy Whitcroft			push(@stack, $level);
158301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
15844635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
158501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
15864635f4fbSAndy Whitcroft			$level = pop(@stack);
15874635f4fbSAndy Whitcroft		}
15884635f4fbSAndy Whitcroft
158901464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1590f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1591f0a594c1SAndy Whitcroft			if ($off > 0) {
1592f0a594c1SAndy Whitcroft				$off--;
1593f0a594c1SAndy Whitcroft				next;
1594f0a594c1SAndy Whitcroft			}
15954a0df2efSAndy Whitcroft
1596f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1597f0a594c1SAndy Whitcroft				$level--;
1598f0a594c1SAndy Whitcroft				last if ($level == 0);
1599f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1600f0a594c1SAndy Whitcroft				$level++;
1601f0a594c1SAndy Whitcroft			}
1602f0a594c1SAndy Whitcroft		}
16034a0df2efSAndy Whitcroft
1604f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
160500df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
16064a0df2efSAndy Whitcroft		}
16074a0df2efSAndy Whitcroft
1608f0a594c1SAndy Whitcroft		last if ($level == 0);
16094a0df2efSAndy Whitcroft	}
16104a0df2efSAndy Whitcroft
1611f0a594c1SAndy Whitcroft	return ($level, @res);
16124a0df2efSAndy Whitcroft}
16134a0df2efSAndy Whitcroftsub ctx_block_outer {
16144a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16154a0df2efSAndy Whitcroft
1616f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1617f0a594c1SAndy Whitcroft	return @r;
16184a0df2efSAndy Whitcroft}
16194a0df2efSAndy Whitcroftsub ctx_block {
16204a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
16214a0df2efSAndy Whitcroft
1622f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1623f0a594c1SAndy Whitcroft	return @r;
1624653d4876SAndy Whitcroft}
1625653d4876SAndy Whitcroftsub ctx_statement {
1626f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1627f0a594c1SAndy Whitcroft
1628f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1629f0a594c1SAndy Whitcroft	return @r;
1630f0a594c1SAndy Whitcroft}
1631f0a594c1SAndy Whitcroftsub ctx_block_level {
1632653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1633653d4876SAndy Whitcroft
1634f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
16354a0df2efSAndy Whitcroft}
16369c0ca6f9SAndy Whitcroftsub ctx_statement_level {
16379c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16389c0ca6f9SAndy Whitcroft
16399c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
16409c0ca6f9SAndy Whitcroft}
16414a0df2efSAndy Whitcroft
16424a0df2efSAndy Whitcroftsub ctx_locate_comment {
16434a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16444a0df2efSAndy Whitcroft
16454a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1646beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
16474a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
16484a0df2efSAndy Whitcroft
16494a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
16504a0df2efSAndy Whitcroft	# comment.
16514a0df2efSAndy Whitcroft	my $in_comment = 0;
16524a0df2efSAndy Whitcroft	$current_comment = '';
16534a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
165400df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
165500df344fSAndy Whitcroft		#warn "           $line\n";
16564a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
16574a0df2efSAndy Whitcroft			$in_comment = 1;
16584a0df2efSAndy Whitcroft		}
16594a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
16604a0df2efSAndy Whitcroft			$in_comment = 1;
16614a0df2efSAndy Whitcroft		}
16624a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
16634a0df2efSAndy Whitcroft			$current_comment = '';
16644a0df2efSAndy Whitcroft		}
16654a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
16664a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
16674a0df2efSAndy Whitcroft			$in_comment = 0;
16684a0df2efSAndy Whitcroft		}
16694a0df2efSAndy Whitcroft	}
16704a0df2efSAndy Whitcroft
16714a0df2efSAndy Whitcroft	chomp($current_comment);
16724a0df2efSAndy Whitcroft	return($current_comment);
16734a0df2efSAndy Whitcroft}
16744a0df2efSAndy Whitcroftsub ctx_has_comment {
16754a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
16764a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
16774a0df2efSAndy Whitcroft
167800df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
16794a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
16804a0df2efSAndy Whitcroft
16814a0df2efSAndy Whitcroft	return ($cmt ne '');
16824a0df2efSAndy Whitcroft}
16834a0df2efSAndy Whitcroft
16844d001e4dSAndy Whitcroftsub raw_line {
16854d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
16864d001e4dSAndy Whitcroft
16874d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
16884d001e4dSAndy Whitcroft	$cnt++;
16894d001e4dSAndy Whitcroft
16904d001e4dSAndy Whitcroft	my $line;
16914d001e4dSAndy Whitcroft	while ($cnt) {
16924d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
16934d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
16944d001e4dSAndy Whitcroft		$cnt--;
16954d001e4dSAndy Whitcroft	}
16964d001e4dSAndy Whitcroft
16974d001e4dSAndy Whitcroft	return $line;
16984d001e4dSAndy Whitcroft}
16994d001e4dSAndy Whitcroft
17002a9f9d85STobin C. Hardingsub get_stat_real {
17012a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
17022a9f9d85STobin C. Harding
17032a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
17042a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
17052a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
17062a9f9d85STobin C. Harding	}
17072a9f9d85STobin C. Harding
17082a9f9d85STobin C. Harding	return $stat_real;
17092a9f9d85STobin C. Harding}
17102a9f9d85STobin C. Harding
1711e3d95a2aSTobin C. Hardingsub get_stat_here {
1712e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
1713e3d95a2aSTobin C. Harding
1714e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
1715e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
1716e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
1717e3d95a2aSTobin C. Harding	}
1718e3d95a2aSTobin C. Harding
1719e3d95a2aSTobin C. Harding	return $herectx;
1720e3d95a2aSTobin C. Harding}
1721e3d95a2aSTobin C. Harding
17220a920b5bSAndy Whitcroftsub cat_vet {
17230a920b5bSAndy Whitcroft	my ($vet) = @_;
17249c0ca6f9SAndy Whitcroft	my ($res, $coded);
17250a920b5bSAndy Whitcroft
17269c0ca6f9SAndy Whitcroft	$res = '';
17276c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
17286c72ffaaSAndy Whitcroft		$res .= $1;
17296c72ffaaSAndy Whitcroft		if ($2 ne '') {
17309c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
17316c72ffaaSAndy Whitcroft			$res .= $coded;
17326c72ffaaSAndy Whitcroft		}
17339c0ca6f9SAndy Whitcroft	}
17349c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
17350a920b5bSAndy Whitcroft
17369c0ca6f9SAndy Whitcroft	return $res;
17370a920b5bSAndy Whitcroft}
17380a920b5bSAndy Whitcroft
1739c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1740cf655043SAndy Whitcroftmy $av_pending;
1741c2fdda0dSAndy Whitcroftmy @av_paren_type;
17421f65f947SAndy Whitcroftmy $av_pend_colon;
1743c2fdda0dSAndy Whitcroft
1744c2fdda0dSAndy Whitcroftsub annotate_reset {
1745c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1746cf655043SAndy Whitcroft	$av_pending = '_';
1747cf655043SAndy Whitcroft	@av_paren_type = ('E');
17481f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1749c2fdda0dSAndy Whitcroft}
1750c2fdda0dSAndy Whitcroft
17516c72ffaaSAndy Whitcroftsub annotate_values {
17526c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
17536c72ffaaSAndy Whitcroft
17546c72ffaaSAndy Whitcroft	my $res;
17551f65f947SAndy Whitcroft	my $var = '_' x length($stream);
17566c72ffaaSAndy Whitcroft	my $cur = $stream;
17576c72ffaaSAndy Whitcroft
1758c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
17596c72ffaaSAndy Whitcroft
17606c72ffaaSAndy Whitcroft	while (length($cur)) {
1761773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1762cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1763171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
17646c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1765c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1766c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1767cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1768c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
17696c72ffaaSAndy Whitcroft			}
17706c72ffaaSAndy Whitcroft
1771c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
17729446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
17739446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1774addcdceaSAndy Whitcroft			$type = 'c';
17759446ef56SAndy Whitcroft
1776e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1777c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
17786c72ffaaSAndy Whitcroft			$type = 'T';
17796c72ffaaSAndy Whitcroft
1780389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1781389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1782389a2fe5SAndy Whitcroft			$type = 'T';
1783389a2fe5SAndy Whitcroft
1784c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1785171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1786c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1787171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1788171ae1a4SAndy Whitcroft			if ($2 ne '') {
1789cf655043SAndy Whitcroft				$av_pending = 'N';
1790171ae1a4SAndy Whitcroft			}
1791171ae1a4SAndy Whitcroft			$type = 'E';
1792171ae1a4SAndy Whitcroft
1793c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1794171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1795171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1796171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
17976c72ffaaSAndy Whitcroft
1798c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1799cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1800c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1801cf655043SAndy Whitcroft
1802cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1803cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1804171ae1a4SAndy Whitcroft			$type = 'E';
1805cf655043SAndy Whitcroft
1806c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1807cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1808cf655043SAndy Whitcroft			$av_preprocessor = 1;
1809cf655043SAndy Whitcroft
1810cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1811cf655043SAndy Whitcroft
1812171ae1a4SAndy Whitcroft			$type = 'E';
1813cf655043SAndy Whitcroft
1814c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1815cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1816cf655043SAndy Whitcroft
1817cf655043SAndy Whitcroft			$av_preprocessor = 1;
1818cf655043SAndy Whitcroft
1819cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1820cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1821cf655043SAndy Whitcroft			pop(@av_paren_type);
1822cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1823171ae1a4SAndy Whitcroft			$type = 'E';
18246c72ffaaSAndy Whitcroft
18256c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1826c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
18276c72ffaaSAndy Whitcroft
1828171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1829171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1830171ae1a4SAndy Whitcroft			$av_pending = $type;
1831171ae1a4SAndy Whitcroft			$type = 'N';
1832171ae1a4SAndy Whitcroft
18336c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1834c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
18356c72ffaaSAndy Whitcroft			if (defined $2) {
1836cf655043SAndy Whitcroft				$av_pending = 'V';
18376c72ffaaSAndy Whitcroft			}
18386c72ffaaSAndy Whitcroft			$type = 'N';
18396c72ffaaSAndy Whitcroft
184014b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1841c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
184214b111c1SAndy Whitcroft			$av_pending = 'E';
18436c72ffaaSAndy Whitcroft			$type = 'N';
18446c72ffaaSAndy Whitcroft
18451f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
18461f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
18471f65f947SAndy Whitcroft			$av_pend_colon = 'C';
18481f65f947SAndy Whitcroft			$type = 'N';
18491f65f947SAndy Whitcroft
185014b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1851c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
18526c72ffaaSAndy Whitcroft			$type = 'N';
18536c72ffaaSAndy Whitcroft
18546c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1855c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1856cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1857cf655043SAndy Whitcroft			$av_pending = '_';
18586c72ffaaSAndy Whitcroft			$type = 'N';
18596c72ffaaSAndy Whitcroft
18606c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1861cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1862cf655043SAndy Whitcroft			if ($new_type ne '_') {
1863cf655043SAndy Whitcroft				$type = $new_type;
1864c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1865c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
18666c72ffaaSAndy Whitcroft			} else {
1867c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
18686c72ffaaSAndy Whitcroft			}
18696c72ffaaSAndy Whitcroft
1870c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1871c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1872c8cb2ca3SAndy Whitcroft			$type = 'V';
1873cf655043SAndy Whitcroft			$av_pending = 'V';
18746c72ffaaSAndy Whitcroft
18758e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
18768e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
18771f65f947SAndy Whitcroft				$av_pend_colon = 'B';
18788e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
18798e761b04SAndy Whitcroft				$av_pend_colon = 'L';
18801f65f947SAndy Whitcroft			}
18811f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
18821f65f947SAndy Whitcroft			$type = 'V';
18831f65f947SAndy Whitcroft
18846c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1885c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
18866c72ffaaSAndy Whitcroft			$type = 'V';
18876c72ffaaSAndy Whitcroft
18886c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1889c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
18906c72ffaaSAndy Whitcroft			$type = 'N';
18916c72ffaaSAndy Whitcroft
1892cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1893c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
189413214adfSAndy Whitcroft			$type = 'E';
18951f65f947SAndy Whitcroft			$av_pend_colon = 'O';
189613214adfSAndy Whitcroft
18978e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
18988e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
18998e761b04SAndy Whitcroft			$type = 'C';
19008e761b04SAndy Whitcroft
19011f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
19021f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
19031f65f947SAndy Whitcroft			$type = 'N';
19041f65f947SAndy Whitcroft
19051f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
19061f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
19071f65f947SAndy Whitcroft
19081f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
19091f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
19101f65f947SAndy Whitcroft				$type = 'E';
19111f65f947SAndy Whitcroft			} else {
19121f65f947SAndy Whitcroft				$type = 'N';
19131f65f947SAndy Whitcroft			}
19141f65f947SAndy Whitcroft			$av_pend_colon = 'O';
19151f65f947SAndy Whitcroft
19168e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
191713214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
19186c72ffaaSAndy Whitcroft			$type = 'N';
19196c72ffaaSAndy Whitcroft
19200d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
192174048ed8SAndy Whitcroft			my $variant;
192274048ed8SAndy Whitcroft
192374048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
192474048ed8SAndy Whitcroft			if ($type eq 'V') {
192574048ed8SAndy Whitcroft				$variant = 'B';
192674048ed8SAndy Whitcroft			} else {
192774048ed8SAndy Whitcroft				$variant = 'U';
192874048ed8SAndy Whitcroft			}
192974048ed8SAndy Whitcroft
193074048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
193174048ed8SAndy Whitcroft			$type = 'N';
193274048ed8SAndy Whitcroft
19336c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1934c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
19356c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
19366c72ffaaSAndy Whitcroft				$type = 'N';
19376c72ffaaSAndy Whitcroft			}
19386c72ffaaSAndy Whitcroft
19396c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1940c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
19416c72ffaaSAndy Whitcroft		}
19426c72ffaaSAndy Whitcroft		if (defined $1) {
19436c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
19446c72ffaaSAndy Whitcroft			$res .= $type x length($1);
19456c72ffaaSAndy Whitcroft		}
19466c72ffaaSAndy Whitcroft	}
19476c72ffaaSAndy Whitcroft
19481f65f947SAndy Whitcroft	return ($res, $var);
19496c72ffaaSAndy Whitcroft}
19506c72ffaaSAndy Whitcroft
19518905a67cSAndy Whitcroftsub possible {
195213214adfSAndy Whitcroft	my ($possible, $line) = @_;
19539a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
19540776e594SAndy Whitcroft		^(?:
19550776e594SAndy Whitcroft			$Modifier|
19560776e594SAndy Whitcroft			$Storage|
19570776e594SAndy Whitcroft			$Type|
19589a974fdbSAndy Whitcroft			DEFINE_\S+
19599a974fdbSAndy Whitcroft		)$|
19609a974fdbSAndy Whitcroft		^(?:
19610776e594SAndy Whitcroft			goto|
19620776e594SAndy Whitcroft			return|
19630776e594SAndy Whitcroft			case|
19640776e594SAndy Whitcroft			else|
19650776e594SAndy Whitcroft			asm|__asm__|
196689a88353SAndy Whitcroft			do|
196789a88353SAndy Whitcroft			\#|
196889a88353SAndy Whitcroft			\#\#|
19699a974fdbSAndy Whitcroft		)(?:\s|$)|
19700776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
19719a974fdbSAndy Whitcroft	    )}x;
19729a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
19739a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1974c45dcabdSAndy Whitcroft		# Check for modifiers.
1975c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1976c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1977c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1978c45dcabdSAndy Whitcroft
1979c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1980c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1981d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
19829a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1983d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1984485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1985d2506586SAndy Whitcroft				}
19869a974fdbSAndy Whitcroft			}
1987c45dcabdSAndy Whitcroft
1988c45dcabdSAndy Whitcroft		} else {
198913214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1990485ff23eSAlex Dowad			push(@typeListFile, $possible);
1991c45dcabdSAndy Whitcroft		}
19928905a67cSAndy Whitcroft		build_types();
19930776e594SAndy Whitcroft	} else {
19940776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
19958905a67cSAndy Whitcroft	}
19968905a67cSAndy Whitcroft}
19978905a67cSAndy Whitcroft
19986c72ffaaSAndy Whitcroftmy $prefix = '';
19996c72ffaaSAndy Whitcroft
2000000d1cc1SJoe Perchessub show_type {
2001cbec18afSJoe Perches	my ($type) = @_;
200291bfe484SJoe Perches
2003522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2004522b837cSAlexey Dobriyan
2005cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2006cbec18afSJoe Perches
2007cbec18afSJoe Perches	return !defined $ignore_type{$type};
2008000d1cc1SJoe Perches}
2009000d1cc1SJoe Perches
2010f0a594c1SAndy Whitcroftsub report {
2011cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2012cbec18afSJoe Perches
2013cbec18afSJoe Perches	if (!show_type($type) ||
2014cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2015773647a0SAndy Whitcroft		return 0;
2016773647a0SAndy Whitcroft	}
201757230297SJoe Perches	my $output = '';
2018737c0767SJohn Brooks	if ($color) {
201957230297SJoe Perches		if ($level eq 'ERROR') {
202057230297SJoe Perches			$output .= RED;
202157230297SJoe Perches		} elsif ($level eq 'WARNING') {
202257230297SJoe Perches			$output .= YELLOW;
2023000d1cc1SJoe Perches		} else {
202457230297SJoe Perches			$output .= GREEN;
2025000d1cc1SJoe Perches		}
202657230297SJoe Perches	}
202757230297SJoe Perches	$output .= $prefix . $level . ':';
202857230297SJoe Perches	if ($show_types) {
2029737c0767SJohn Brooks		$output .= BLUE if ($color);
203057230297SJoe Perches		$output .= "$type:";
203157230297SJoe Perches	}
2032737c0767SJohn Brooks	$output .= RESET if ($color);
203357230297SJoe Perches	$output .= ' ' . $msg . "\n";
203434d8815fSJoe Perches
203534d8815fSJoe Perches	if ($showfile) {
203634d8815fSJoe Perches		my @lines = split("\n", $output, -1);
203734d8815fSJoe Perches		splice(@lines, 1, 1);
203834d8815fSJoe Perches		$output = join("\n", @lines);
203934d8815fSJoe Perches	}
204057230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
20418905a67cSAndy Whitcroft
204257230297SJoe Perches	push(our @report, $output);
2043773647a0SAndy Whitcroft
2044773647a0SAndy Whitcroft	return 1;
2045f0a594c1SAndy Whitcroft}
2046cbec18afSJoe Perches
2047f0a594c1SAndy Whitcroftsub report_dump {
204813214adfSAndy Whitcroft	our @report;
2049f0a594c1SAndy Whitcroft}
2050000d1cc1SJoe Perches
2051d752fcc8SJoe Perchessub fixup_current_range {
2052d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2053d752fcc8SJoe Perches
2054d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2055d752fcc8SJoe Perches		my $o = $1;
2056d752fcc8SJoe Perches		my $l = $2;
2057d752fcc8SJoe Perches		my $no = $o + $offset;
2058d752fcc8SJoe Perches		my $nl = $l + $length;
2059d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2060d752fcc8SJoe Perches	}
2061d752fcc8SJoe Perches}
2062d752fcc8SJoe Perches
2063d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2064d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2065d752fcc8SJoe Perches
2066d752fcc8SJoe Perches	my $range_last_linenr = 0;
2067d752fcc8SJoe Perches	my $delta_offset = 0;
2068d752fcc8SJoe Perches
2069d752fcc8SJoe Perches	my $old_linenr = 0;
2070d752fcc8SJoe Perches	my $new_linenr = 0;
2071d752fcc8SJoe Perches
2072d752fcc8SJoe Perches	my $next_insert = 0;
2073d752fcc8SJoe Perches	my $next_delete = 0;
2074d752fcc8SJoe Perches
2075d752fcc8SJoe Perches	my @lines = ();
2076d752fcc8SJoe Perches
2077d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2078d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2079d752fcc8SJoe Perches
2080d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2081d752fcc8SJoe Perches		my $save_line = 1;
2082d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2083323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2084d752fcc8SJoe Perches			$delta_offset = 0;
2085d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2086d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2087d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2088d752fcc8SJoe Perches		}
2089d752fcc8SJoe Perches
2090d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2091d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2092d752fcc8SJoe Perches			$save_line = 0;
2093d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2094d752fcc8SJoe Perches		}
2095d752fcc8SJoe Perches
2096d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2097d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2098d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2099d752fcc8SJoe Perches			$new_linenr++;
2100d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2101d752fcc8SJoe Perches		}
2102d752fcc8SJoe Perches
2103d752fcc8SJoe Perches		if ($save_line) {
2104d752fcc8SJoe Perches			push(@lines, $line);
2105d752fcc8SJoe Perches			$new_linenr++;
2106d752fcc8SJoe Perches		}
2107d752fcc8SJoe Perches
2108d752fcc8SJoe Perches		$old_linenr++;
2109d752fcc8SJoe Perches	}
2110d752fcc8SJoe Perches
2111d752fcc8SJoe Perches	return @lines;
2112d752fcc8SJoe Perches}
2113d752fcc8SJoe Perches
2114f2d7e4d4SJoe Perchessub fix_insert_line {
2115f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2116f2d7e4d4SJoe Perches
2117f2d7e4d4SJoe Perches	my $inserted = {
2118f2d7e4d4SJoe Perches		LINENR => $linenr,
2119f2d7e4d4SJoe Perches		LINE => $line,
2120f2d7e4d4SJoe Perches	};
2121f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2122f2d7e4d4SJoe Perches}
2123f2d7e4d4SJoe Perches
2124f2d7e4d4SJoe Perchessub fix_delete_line {
2125f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2126f2d7e4d4SJoe Perches
2127f2d7e4d4SJoe Perches	my $deleted = {
2128f2d7e4d4SJoe Perches		LINENR => $linenr,
2129f2d7e4d4SJoe Perches		LINE => $line,
2130f2d7e4d4SJoe Perches	};
2131f2d7e4d4SJoe Perches
2132f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2133f2d7e4d4SJoe Perches}
2134f2d7e4d4SJoe Perches
2135de7d4f0eSAndy Whitcroftsub ERROR {
2136cbec18afSJoe Perches	my ($type, $msg) = @_;
2137cbec18afSJoe Perches
2138cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2139de7d4f0eSAndy Whitcroft		our $clean = 0;
21406c72ffaaSAndy Whitcroft		our $cnt_error++;
21413705ce5bSJoe Perches		return 1;
2142de7d4f0eSAndy Whitcroft	}
21433705ce5bSJoe Perches	return 0;
2144773647a0SAndy Whitcroft}
2145de7d4f0eSAndy Whitcroftsub WARN {
2146cbec18afSJoe Perches	my ($type, $msg) = @_;
2147cbec18afSJoe Perches
2148cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2149de7d4f0eSAndy Whitcroft		our $clean = 0;
21506c72ffaaSAndy Whitcroft		our $cnt_warn++;
21513705ce5bSJoe Perches		return 1;
2152de7d4f0eSAndy Whitcroft	}
21533705ce5bSJoe Perches	return 0;
2154773647a0SAndy Whitcroft}
2155de7d4f0eSAndy Whitcroftsub CHK {
2156cbec18afSJoe Perches	my ($type, $msg) = @_;
2157cbec18afSJoe Perches
2158cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2159de7d4f0eSAndy Whitcroft		our $clean = 0;
21606c72ffaaSAndy Whitcroft		our $cnt_chk++;
21613705ce5bSJoe Perches		return 1;
21626c72ffaaSAndy Whitcroft	}
21633705ce5bSJoe Perches	return 0;
2164de7d4f0eSAndy Whitcroft}
2165de7d4f0eSAndy Whitcroft
21666ecd9674SAndy Whitcroftsub check_absolute_file {
21676ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
21686ecd9674SAndy Whitcroft	my $file = $absolute;
21696ecd9674SAndy Whitcroft
21706ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
21716ecd9674SAndy Whitcroft
21726ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
21736ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
21746ecd9674SAndy Whitcroft		if (-f "$root/$file") {
21756ecd9674SAndy Whitcroft			##print "file<$file>\n";
21766ecd9674SAndy Whitcroft			last;
21776ecd9674SAndy Whitcroft		}
21786ecd9674SAndy Whitcroft	}
21796ecd9674SAndy Whitcroft	if (! -f _)  {
21806ecd9674SAndy Whitcroft		return 0;
21816ecd9674SAndy Whitcroft	}
21826ecd9674SAndy Whitcroft
21836ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
21846ecd9674SAndy Whitcroft	my $prefix = $absolute;
21856ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
21866ecd9674SAndy Whitcroft
21876ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
21886ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2189000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2190000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
21916ecd9674SAndy Whitcroft	}
21926ecd9674SAndy Whitcroft}
21936ecd9674SAndy Whitcroft
21943705ce5bSJoe Perchessub trim {
21953705ce5bSJoe Perches	my ($string) = @_;
21963705ce5bSJoe Perches
2197b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2198b34c648bSJoe Perches
2199b34c648bSJoe Perches	return $string;
2200b34c648bSJoe Perches}
2201b34c648bSJoe Perches
2202b34c648bSJoe Perchessub ltrim {
2203b34c648bSJoe Perches	my ($string) = @_;
2204b34c648bSJoe Perches
2205b34c648bSJoe Perches	$string =~ s/^\s+//;
2206b34c648bSJoe Perches
2207b34c648bSJoe Perches	return $string;
2208b34c648bSJoe Perches}
2209b34c648bSJoe Perches
2210b34c648bSJoe Perchessub rtrim {
2211b34c648bSJoe Perches	my ($string) = @_;
2212b34c648bSJoe Perches
2213b34c648bSJoe Perches	$string =~ s/\s+$//;
22143705ce5bSJoe Perches
22153705ce5bSJoe Perches	return $string;
22163705ce5bSJoe Perches}
22173705ce5bSJoe Perches
221852ea8506SJoe Perchessub string_find_replace {
221952ea8506SJoe Perches	my ($string, $find, $replace) = @_;
222052ea8506SJoe Perches
222152ea8506SJoe Perches	$string =~ s/$find/$replace/g;
222252ea8506SJoe Perches
222352ea8506SJoe Perches	return $string;
222452ea8506SJoe Perches}
222552ea8506SJoe Perches
22263705ce5bSJoe Perchessub tabify {
22273705ce5bSJoe Perches	my ($leading) = @_;
22283705ce5bSJoe Perches
22293705ce5bSJoe Perches	my $source_indent = 8;
22303705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
22313705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
22323705ce5bSJoe Perches
22333705ce5bSJoe Perches	#convert leading spaces to tabs
22343705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
22353705ce5bSJoe Perches	#Remove spaces before a tab
22363705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
22373705ce5bSJoe Perches
22383705ce5bSJoe Perches	return "$leading";
22393705ce5bSJoe Perches}
22403705ce5bSJoe Perches
2241d1fe9c09SJoe Perchessub pos_last_openparen {
2242d1fe9c09SJoe Perches	my ($line) = @_;
2243d1fe9c09SJoe Perches
2244d1fe9c09SJoe Perches	my $pos = 0;
2245d1fe9c09SJoe Perches
2246d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2247d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2248d1fe9c09SJoe Perches
2249d1fe9c09SJoe Perches	my $last_openparen = 0;
2250d1fe9c09SJoe Perches
2251d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2252d1fe9c09SJoe Perches		return -1;
2253d1fe9c09SJoe Perches	}
2254d1fe9c09SJoe Perches
2255d1fe9c09SJoe Perches	my $len = length($line);
2256d1fe9c09SJoe Perches
2257d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2258d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2259d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2260d1fe9c09SJoe Perches			$pos += length($1) - 1;
2261d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2262d1fe9c09SJoe Perches			$last_openparen = $pos;
2263d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2264d1fe9c09SJoe Perches			last;
2265d1fe9c09SJoe Perches		}
2266d1fe9c09SJoe Perches	}
2267d1fe9c09SJoe Perches
226891cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2269d1fe9c09SJoe Perches}
2270d1fe9c09SJoe Perches
22710a920b5bSAndy Whitcroftsub process {
22720a920b5bSAndy Whitcroft	my $filename = shift;
22730a920b5bSAndy Whitcroft
22740a920b5bSAndy Whitcroft	my $linenr=0;
22750a920b5bSAndy Whitcroft	my $prevline="";
2276c2fdda0dSAndy Whitcroft	my $prevrawline="";
22770a920b5bSAndy Whitcroft	my $stashline="";
2278c2fdda0dSAndy Whitcroft	my $stashrawline="";
22790a920b5bSAndy Whitcroft
22804a0df2efSAndy Whitcroft	my $length;
22810a920b5bSAndy Whitcroft	my $indent;
22820a920b5bSAndy Whitcroft	my $previndent=0;
22830a920b5bSAndy Whitcroft	my $stashindent=0;
22840a920b5bSAndy Whitcroft
2285de7d4f0eSAndy Whitcroft	our $clean = 1;
22860a920b5bSAndy Whitcroft	my $signoff = 0;
2287cd261496SGeert Uytterhoeven	my $author = '';
2288cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
22890a920b5bSAndy Whitcroft	my $is_patch = 0;
2290133712a2SRob Herring	my $is_binding_patch = -1;
229129ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
229215662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2293ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2294490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2295bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
22962a076f40SJoe Perches	my $commit_log_long_line = 0;
2297e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
229813f1937eSJoe Perches	my $reported_maintainer_file = 0;
2299fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2300fa64205dSPasi Savanainen
2301365dd4eaSJoe Perches	my $last_blank_line = 0;
23025e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2303365dd4eaSJoe Perches
230413214adfSAndy Whitcroft	our @report = ();
23056c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
23066c72ffaaSAndy Whitcroft	our $cnt_error = 0;
23076c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
23086c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
23096c72ffaaSAndy Whitcroft
23100a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
23110a920b5bSAndy Whitcroft	my $realfile = '';
23120a920b5bSAndy Whitcroft	my $realline = 0;
23130a920b5bSAndy Whitcroft	my $realcnt = 0;
23140a920b5bSAndy Whitcroft	my $here = '';
231577cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
23160a920b5bSAndy Whitcroft	my $in_comment = 0;
2317c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
23180a920b5bSAndy Whitcroft	my $first_line = 0;
23191e855726SWolfram Sang	my $p1_prefix = '';
23200a920b5bSAndy Whitcroft
232113214adfSAndy Whitcroft	my $prev_values = 'E';
232213214adfSAndy Whitcroft
232313214adfSAndy Whitcroft	# suppression flags
2324773647a0SAndy Whitcroft	my %suppress_ifbraces;
2325170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
23262b474a1aSAndy Whitcroft	my %suppress_export;
23273e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2328653d4876SAndy Whitcroft
23297e51f197SJoe Perches	my %signatures = ();
2330323c1260SJoe Perches
2331c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2332de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2333c2fdda0dSAndy Whitcroft	#
2334de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2335de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2336773647a0SAndy Whitcroft
2337d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2338d8b07710SJoe Perches
23399f3a8992SRob Herring	my $checklicenseline = 1;
23409f3a8992SRob Herring
2341773647a0SAndy Whitcroft	sanitise_line_reset();
2342c2fdda0dSAndy Whitcroft	my $line;
2343c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2344773647a0SAndy Whitcroft		$linenr++;
2345773647a0SAndy Whitcroft		$line = $rawline;
2346c2fdda0dSAndy Whitcroft
23473705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
23483705ce5bSJoe Perches
2349773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2350de7d4f0eSAndy Whitcroft			$setup_docs = 0;
23518c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2352de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2353de7d4f0eSAndy Whitcroft			}
2354773647a0SAndy Whitcroft			#next;
2355de7d4f0eSAndy Whitcroft		}
235674fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2357773647a0SAndy Whitcroft			$realline=$1-1;
2358773647a0SAndy Whitcroft			if (defined $2) {
2359773647a0SAndy Whitcroft				$realcnt=$3+1;
2360773647a0SAndy Whitcroft			} else {
2361773647a0SAndy Whitcroft				$realcnt=1+1;
2362773647a0SAndy Whitcroft			}
2363c45dcabdSAndy Whitcroft			$in_comment = 0;
2364773647a0SAndy Whitcroft
2365773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2366773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2367773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2368773647a0SAndy Whitcroft			# at context start.
2369773647a0SAndy Whitcroft			my $edge;
237001fa9147SAndy Whitcroft			my $cnt = $realcnt;
237101fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
237201fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
237301fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
237401fa9147SAndy Whitcroft				$cnt--;
237501fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2376721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2377fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2378fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2379fae17daeSAndy Whitcroft					($edge) = $1;
2380fae17daeSAndy Whitcroft					last;
2381fae17daeSAndy Whitcroft				}
2382773647a0SAndy Whitcroft			}
2383773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2384773647a0SAndy Whitcroft				$in_comment = 1;
2385773647a0SAndy Whitcroft			}
2386773647a0SAndy Whitcroft
2387773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2388773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2389773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2390773647a0SAndy Whitcroft			if (!defined $edge &&
239183242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2392773647a0SAndy Whitcroft			{
2393773647a0SAndy Whitcroft				$in_comment = 1;
2394773647a0SAndy Whitcroft			}
2395773647a0SAndy Whitcroft
2396773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2397773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2398773647a0SAndy Whitcroft
2399171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2400773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2401171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2402773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2403773647a0SAndy Whitcroft		}
2404773647a0SAndy Whitcroft		push(@lines, $line);
2405773647a0SAndy Whitcroft
2406773647a0SAndy Whitcroft		if ($realcnt > 1) {
2407773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2408773647a0SAndy Whitcroft		} else {
2409773647a0SAndy Whitcroft			$realcnt = 0;
2410773647a0SAndy Whitcroft		}
2411773647a0SAndy Whitcroft
2412773647a0SAndy Whitcroft		#print "==>$rawline\n";
2413773647a0SAndy Whitcroft		#print "-->$line\n";
2414de7d4f0eSAndy Whitcroft
2415de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2416de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2417de7d4f0eSAndy Whitcroft		}
2418de7d4f0eSAndy Whitcroft	}
2419de7d4f0eSAndy Whitcroft
24206c72ffaaSAndy Whitcroft	$prefix = '';
24216c72ffaaSAndy Whitcroft
2422773647a0SAndy Whitcroft	$realcnt = 0;
2423773647a0SAndy Whitcroft	$linenr = 0;
2424194f66fcSJoe Perches	$fixlinenr = -1;
24250a920b5bSAndy Whitcroft	foreach my $line (@lines) {
24260a920b5bSAndy Whitcroft		$linenr++;
2427194f66fcSJoe Perches		$fixlinenr++;
24281b5539b1SJoe Perches		my $sline = $line;	#copy of $line
24291b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
24300a920b5bSAndy Whitcroft
2431c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
24326c72ffaaSAndy Whitcroft
243312c253abSJoe Perches# check if it's a mode change, rename or start of a patch
243412c253abSJoe Perches		if (!$in_commit_log &&
243512c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
243612c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
243712c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
243812c253abSJoe Perches			$is_patch = 1;
243912c253abSJoe Perches		}
244012c253abSJoe Perches
24410a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2442e518e9a5SJoe Perches		if (!$in_commit_log &&
244374fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
244474fd4f34SJoe Perches			my $context = $4;
24450a920b5bSAndy Whitcroft			$is_patch = 1;
24464a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
24470a920b5bSAndy Whitcroft			$realline=$1-1;
24480a920b5bSAndy Whitcroft			if (defined $2) {
24490a920b5bSAndy Whitcroft				$realcnt=$3+1;
24500a920b5bSAndy Whitcroft			} else {
24510a920b5bSAndy Whitcroft				$realcnt=1+1;
24520a920b5bSAndy Whitcroft			}
2453c2fdda0dSAndy Whitcroft			annotate_reset();
245413214adfSAndy Whitcroft			$prev_values = 'E';
245513214adfSAndy Whitcroft
2456773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2457170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
24582b474a1aSAndy Whitcroft			%suppress_export = ();
24593e469cdcSAndy Whitcroft			$suppress_statement = 0;
246074fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
246174fd4f34SJoe Perches				$context_function = $1;
246274fd4f34SJoe Perches			} else {
246374fd4f34SJoe Perches				undef $context_function;
246474fd4f34SJoe Perches			}
24650a920b5bSAndy Whitcroft			next;
24660a920b5bSAndy Whitcroft
24674a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
24684a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
24694a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2470773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
24710a920b5bSAndy Whitcroft			$realline++;
2472d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
24730a920b5bSAndy Whitcroft
24744a0df2efSAndy Whitcroft			# Measure the line length and indent.
2475c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
24760a920b5bSAndy Whitcroft
24770a920b5bSAndy Whitcroft			# Track the previous line.
24780a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
24790a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2480c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2481c2fdda0dSAndy Whitcroft
2482773647a0SAndy Whitcroft			#warn "line<$line>\n";
24836c72ffaaSAndy Whitcroft
2484d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2485d8aaf121SAndy Whitcroft			$realcnt--;
24860a920b5bSAndy Whitcroft		}
24870a920b5bSAndy Whitcroft
2488cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2489cc77cdcaSAndy Whitcroft
24906c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
24916c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2492773647a0SAndy Whitcroft
24932ac73b4fSJoe Perches		my $found_file = 0;
2494773647a0SAndy Whitcroft		# extract the filename as it passes
24953bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
24963bf9a009SRabin Vincent			$realfile = $1;
24972b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2498270c49a0SJoe Perches			$in_commit_log = 0;
24992ac73b4fSJoe Perches			$found_file = 1;
25003bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2501773647a0SAndy Whitcroft			$realfile = $1;
25022b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2503270c49a0SJoe Perches			$in_commit_log = 0;
25041e855726SWolfram Sang
25051e855726SWolfram Sang			$p1_prefix = $1;
2506e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2507e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2508000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2509000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
25101e855726SWolfram Sang			}
2511773647a0SAndy Whitcroft
2512c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2513000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2514000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2515773647a0SAndy Whitcroft			}
25162ac73b4fSJoe Perches			$found_file = 1;
25172ac73b4fSJoe Perches		}
25182ac73b4fSJoe Perches
251934d8815fSJoe Perches#make up the handle for any error we report on this line
252034d8815fSJoe Perches		if ($showfile) {
252134d8815fSJoe Perches			$prefix = "$realfile:$realline: "
252234d8815fSJoe Perches		} elsif ($emacs) {
25237d3a9f67SJoe Perches			if ($file) {
25247d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
25257d3a9f67SJoe Perches			} else {
252634d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
252734d8815fSJoe Perches			}
25287d3a9f67SJoe Perches		}
252934d8815fSJoe Perches
25302ac73b4fSJoe Perches		if ($found_file) {
253185b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
253285b0ee18SJoe Perches				WARN("OBSOLETE",
253385b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
253485b0ee18SJoe Perches			}
25357bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
25362ac73b4fSJoe Perches				$check = 1;
25372ac73b4fSJoe Perches			} else {
25382ac73b4fSJoe Perches				$check = $check_orig;
25392ac73b4fSJoe Perches			}
25409f3a8992SRob Herring			$checklicenseline = 1;
2541133712a2SRob Herring
2542133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2543133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2544133712a2SRob Herring
2545133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2546133712a2SRob Herring
2547133712a2SRob Herring				if (($last_binding_patch != -1) &&
2548133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2549133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2550133712a2SRob Herring					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.txt\n");
2551133712a2SRob Herring				}
2552133712a2SRob Herring			}
2553133712a2SRob Herring
2554773647a0SAndy Whitcroft			next;
2555773647a0SAndy Whitcroft		}
2556773647a0SAndy Whitcroft
2557389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
25580a920b5bSAndy Whitcroft
2559c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2560c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2561c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
25620a920b5bSAndy Whitcroft
25636c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
25646c72ffaaSAndy Whitcroft
2565490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2566490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2567490b292cSJoe Perches		if ($in_commit_log) {
2568490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2569490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2570490b292cSJoe Perches			}
2571490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2572490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2573490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2574490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2575490b292cSJoe Perches		}
2576490b292cSJoe Perches
2577e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2578e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2579e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2580e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2581e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2582e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2583e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2584e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2585e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2586e518e9a5SJoe Perches		}
2587e518e9a5SJoe Perches
25883bf9a009SRabin Vincent# Check for incorrect file permissions
25893bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
25903bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
259104db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
259204db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2593000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2594000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
25953bf9a009SRabin Vincent			}
25963bf9a009SRabin Vincent		}
25973bf9a009SRabin Vincent
2598cd261496SGeert Uytterhoeven# Check the patch for a From:
2599cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2600cd261496SGeert Uytterhoeven			$author = $1;
2601cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2602cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2603cd261496SGeert Uytterhoeven		}
2604cd261496SGeert Uytterhoeven
260520112475SJoe Perches# Check the patch for a signoff:
2606d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
26074a0df2efSAndy Whitcroft			$signoff++;
260815662b3eSJoe Perches			$in_commit_log = 0;
2609cd261496SGeert Uytterhoeven			if ($author ne '') {
2610cd261496SGeert Uytterhoeven				my $l = $line;
2611cd261496SGeert Uytterhoeven				$l =~ s/"//g;
2612cd261496SGeert Uytterhoeven				if ($l =~ /^\s*signed-off-by:\s*\Q$author\E/i) {
2613cd261496SGeert Uytterhoeven				    $authorsignoff = 1;
2614cd261496SGeert Uytterhoeven				}
2615cd261496SGeert Uytterhoeven			}
26160a920b5bSAndy Whitcroft		}
261720112475SJoe Perches
2618e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2619e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2620e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2621e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2622e0d975b1SJoe Perches		}
2623e0d975b1SJoe Perches
262420112475SJoe Perches# Check signature styles
2625270c49a0SJoe Perches		if (!$in_header_lines &&
2626ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
262720112475SJoe Perches			my $space_before = $1;
262820112475SJoe Perches			my $sign_off = $2;
262920112475SJoe Perches			my $space_after = $3;
263020112475SJoe Perches			my $email = $4;
263120112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
263220112475SJoe Perches
2633ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2634ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2635ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2636ce0338dfSJoe Perches			}
263720112475SJoe Perches			if (defined $space_before && $space_before ne "") {
26383705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26393705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
26403705ce5bSJoe Perches				    $fix) {
2641194f66fcSJoe Perches					$fixed[$fixlinenr] =
26423705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26433705ce5bSJoe Perches				}
264420112475SJoe Perches			}
264520112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
26463705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26473705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
26483705ce5bSJoe Perches				    $fix) {
2649194f66fcSJoe Perches					$fixed[$fixlinenr] =
26503705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26513705ce5bSJoe Perches				}
26523705ce5bSJoe Perches
265320112475SJoe Perches			}
265420112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
26553705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
26563705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
26573705ce5bSJoe Perches				    $fix) {
2658194f66fcSJoe Perches					$fixed[$fixlinenr] =
26593705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
26603705ce5bSJoe Perches				}
266120112475SJoe Perches			}
266220112475SJoe Perches
266320112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
266420112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
266520112475SJoe Perches			if ($suggested_email eq "") {
2666000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2667000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
266820112475SJoe Perches			} else {
266920112475SJoe Perches				my $dequoted = $suggested_email;
267020112475SJoe Perches				$dequoted =~ s/^"//;
267120112475SJoe Perches				$dequoted =~ s/" </ </;
267220112475SJoe Perches				# Don't force email to have quotes
267320112475SJoe Perches				# Allow just an angle bracketed address
267420112475SJoe Perches				if ("$dequoted$comment" ne $email &&
267520112475SJoe Perches				    "<$email_address>$comment" ne $email &&
267620112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2677000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2678000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
267920112475SJoe Perches				}
26800a920b5bSAndy Whitcroft			}
26817e51f197SJoe Perches
26827e51f197SJoe Perches# Check for duplicate signatures
26837e51f197SJoe Perches			my $sig_nospace = $line;
26847e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
26857e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
26867e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
26877e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
26887e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
26897e51f197SJoe Perches			} else {
26907e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
26917e51f197SJoe Perches			}
26926c5d24eeSSean Christopherson
26936c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
26946c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
26956c5d24eeSSean Christopherson				if ($email eq $author) {
26966c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
26976c5d24eeSSean Christopherson					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
26986c5d24eeSSean Christopherson				}
26996c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
27006c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27016c5d24eeSSean Christopherson                                             "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
27026c5d24eeSSean Christopherson				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
27036c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27046c5d24eeSSean Christopherson					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27056c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
27066c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
27076c5d24eeSSean Christopherson					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
27086c5d24eeSSean Christopherson				}
27096c5d24eeSSean Christopherson			}
27100a920b5bSAndy Whitcroft		}
27110a920b5bSAndy Whitcroft
2712a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2713a2fe16b9SJoe Perches		if ($in_header_lines &&
2714a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2715a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2716a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2717a2fe16b9SJoe Perches		}
2718a2fe16b9SJoe Perches
27197ebd05efSChristopher Covington# Check for unwanted Gerrit info
27207ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
27217ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
27227ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
27237ebd05efSChristopher Covington		}
27247ebd05efSChristopher Covington
2725369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2726369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2727369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2728369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2729369c8dd3SJoe Perches					# timestamp
2730634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
2731634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
2732634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
2733634cffccSJoe Perches					# stack dump address styles
2734369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2735369c8dd3SJoe Perches		}
2736369c8dd3SJoe Perches
27372a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
27382a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2739bf4daf12SJoe Perches		    length($line) > 75 &&
2740bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2741bf4daf12SJoe Perches					# file delta changes
2742bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2743bf4daf12SJoe Perches					# filename then :
2744bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2745bf4daf12SJoe Perches					# A Fixes: or Link: line
2746bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
27472a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
27482a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
27492a076f40SJoe Perches			$commit_log_long_line = 1;
27502a076f40SJoe Perches		}
27512a076f40SJoe Perches
2752bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2753bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2754bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2755bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2756bf4daf12SJoe Perches		}
2757bf4daf12SJoe Perches
27580d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2759369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2760aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2761e882dbfcSWei Wang		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
2762fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2763aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2764369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2765bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2766fe043ea1SJoe Perches			my $init_char = "c";
2767fe043ea1SJoe Perches			my $orig_commit = "";
27680d7835fcSJoe Perches			my $short = 1;
27690d7835fcSJoe Perches			my $long = 0;
27700d7835fcSJoe Perches			my $case = 1;
27710d7835fcSJoe Perches			my $space = 1;
27720d7835fcSJoe Perches			my $hasdesc = 0;
277319c146a6SJoe Perches			my $hasparens = 0;
27740d7835fcSJoe Perches			my $id = '0123456789ab';
27750d7835fcSJoe Perches			my $orig_desc = "commit description";
27760d7835fcSJoe Perches			my $description = "";
27770d7835fcSJoe Perches
2778fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2779fe043ea1SJoe Perches				$init_char = $1;
2780fe043ea1SJoe Perches				$orig_commit = lc($2);
2781fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2782fe043ea1SJoe Perches				$orig_commit = lc($1);
2783fe043ea1SJoe Perches			}
2784fe043ea1SJoe Perches
27850d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
27860d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
27870d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
27880d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
27890d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
27900d7835fcSJoe Perches				$orig_desc = $1;
279119c146a6SJoe Perches				$hasparens = 1;
27920d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
27930d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
27940d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
27950d7835fcSJoe Perches				$orig_desc = $1;
279619c146a6SJoe Perches				$hasparens = 1;
2797b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2798b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2799b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2800b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2801b671fde0SJoe Perches				$orig_desc = $1;
2802b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2803b671fde0SJoe Perches				$orig_desc .= " " . $1;
280419c146a6SJoe Perches				$hasparens = 1;
28050d7835fcSJoe Perches			}
28060d7835fcSJoe Perches
28070d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
28080d7835fcSJoe Perches							      $id, $orig_desc);
28090d7835fcSJoe Perches
2810948b133aSHeinrich Schuchardt			if (defined($id) &&
2811948b133aSHeinrich Schuchardt			   ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
2812d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
28130d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
28140d7835fcSJoe Perches			}
2815d311cd44SJoe Perches		}
2816d311cd44SJoe Perches
281713f1937eSJoe Perches# Check for added, moved or deleted files
281813f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
281913f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
282013f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
282113f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
282213f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2823a82603a8SAndrew Jeffery			$is_patch = 1;
282413f1937eSJoe Perches			$reported_maintainer_file = 1;
282513f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
282613f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
282713f1937eSJoe Perches		}
282813f1937eSJoe Perches
2829e400edb1SRob Herring# Check for adding new DT bindings not in schema format
2830e400edb1SRob Herring		if (!$in_commit_log &&
2831e400edb1SRob Herring		    ($line =~ /^new file mode\s*\d+\s*$/) &&
2832e400edb1SRob Herring		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
2833e400edb1SRob Herring			WARN("DT_SCHEMA_BINDING_PATCH",
2834e400edb1SRob Herring			     "DT bindings should be in DT schema format. See: Documentation/devicetree/writing-schema.rst\n");
2835e400edb1SRob Herring		}
2836e400edb1SRob Herring
283700df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
28388905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2839000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2840000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
28416c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2842de7d4f0eSAndy Whitcroft		}
2843de7d4f0eSAndy Whitcroft
2844de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2845de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2846171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2847171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2848171ae1a4SAndy Whitcroft
2849171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2850171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2851171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2852171ae1a4SAndy Whitcroft
285334d99219SJoe Perches			CHK("INVALID_UTF8",
2854000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
285500df344fSAndy Whitcroft		}
28560a920b5bSAndy Whitcroft
285715662b3eSJoe Perches# Check if it's the start of a commit log
285815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
285915662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
2860eb3a58deSJoe Perches		    !($rawline =~ /^\s+(?:\S|$)/ ||
2861eb3a58deSJoe Perches		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
286215662b3eSJoe Perches			$in_header_lines = 0;
286315662b3eSJoe Perches			$in_commit_log = 1;
2864ed43c4e5SAllen Hubbe			$has_commit_log = 1;
286515662b3eSJoe Perches		}
286615662b3eSJoe Perches
2867fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2868fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2869fa64205dSPasi Savanainen		if ($in_header_lines &&
2870fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2871fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2872fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2873fa64205dSPasi Savanainen		}
2874fa64205dSPasi Savanainen
2875fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
287615662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2877fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
287815662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
287915662b3eSJoe Perches		}
288015662b3eSJoe Perches
2881d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2882d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2883d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2884d6430f71SJoe Perches				my $file = $1;
2885d6430f71SJoe Perches
2886d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2887d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2888d6430f71SJoe Perches					#
2889d6430f71SJoe Perches				} else {
2890d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2891d6430f71SJoe Perches				}
2892d6430f71SJoe Perches			}
2893d6430f71SJoe Perches		}
2894d6430f71SJoe Perches
289566b47b4aSKees Cook# Check for various typo / spelling mistakes
289666d7a382SJoe Perches		if (defined($misspellings) &&
289766d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2898ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
289966b47b4aSKees Cook				my $typo = $1;
290066b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
290166b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
290266b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
29030675a8fbSJean Delvare				my $msg_level = \&WARN;
29040675a8fbSJean Delvare				$msg_level = \&CHK if ($file);
29050675a8fbSJean Delvare				if (&{$msg_level}("TYPO_SPELLING",
290666b47b4aSKees Cook						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
290766b47b4aSKees Cook				    $fix) {
290866b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
290966b47b4aSKees Cook				}
291066b47b4aSKees Cook			}
291166b47b4aSKees Cook		}
291266b47b4aSKees Cook
2913a8dd86bfSMatteo Croce# check for invalid commit id
2914a8dd86bfSMatteo Croce		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
2915a8dd86bfSMatteo Croce			my $id;
2916a8dd86bfSMatteo Croce			my $description;
2917a8dd86bfSMatteo Croce			($id, $description) = git_commit_info($2, undef, undef);
2918a8dd86bfSMatteo Croce			if (!defined($id)) {
2919a8dd86bfSMatteo Croce				WARN("UNKNOWN_COMMIT_ID",
2920a8dd86bfSMatteo Croce				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
2921a8dd86bfSMatteo Croce			}
2922a8dd86bfSMatteo Croce		}
2923a8dd86bfSMatteo Croce
292430670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
292530670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
292600df344fSAndy Whitcroft
29270a920b5bSAndy Whitcroft#trailing whitespace
29289c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2929c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2930d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2931d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2932d5e616fcSJoe Perches			    $fix) {
2933194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2934d5e616fcSJoe Perches			}
2935c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2936c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29373705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
29383705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
29393705ce5bSJoe Perches			    $fix) {
2940194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
29413705ce5bSJoe Perches			}
29423705ce5bSJoe Perches
2943d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29440a920b5bSAndy Whitcroft		}
29455368df20SAndy Whitcroft
29464783f894SJosh Triplett# Check for FSF mailing addresses.
2947109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
29481bde561eSMatthew Wilcox		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
29493e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
29503e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
29514783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29520675a8fbSJean Delvare			my $msg_level = \&ERROR;
29530675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
29540675a8fbSJean Delvare			&{$msg_level}("FSF_MAILING_ADDRESS",
29554783f894SJosh 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)
29564783f894SJosh Triplett		}
29574783f894SJosh Triplett
29583354957aSAndi Kleen# check for Kconfig help text having a real description
29599fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
29609fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
29613354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
2962678ae162SUlf Magnusson		    # 'choice' is usually the last thing on the line (though
2963678ae162SUlf Magnusson		    # Kconfig supports named choices), so use a word boundary
2964678ae162SUlf Magnusson		    # (\b) rather than a whitespace character (\s)
2965678ae162SUlf Magnusson		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
29663354957aSAndi Kleen			my $length = 0;
29679fe287d7SAndy Whitcroft			my $cnt = $realcnt;
29689fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
29699fe287d7SAndy Whitcroft			my $f;
2970a1385803SAndy Whitcroft			my $is_start = 0;
29719fe287d7SAndy Whitcroft			my $is_end = 0;
2972a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
29739fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
29749fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
29759fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
29769fe287d7SAndy Whitcroft
29779fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
29788d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2979a1385803SAndy Whitcroft
298086adf1a0SUlf Magnusson				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
2981a1385803SAndy Whitcroft					$is_start = 1;
298284af7a61SUlf Magnusson				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:help|---help---)\s*$/) {
298384af7a61SUlf Magnusson					if ($lines[$ln - 1] =~ "---help---") {
298484af7a61SUlf Magnusson						WARN("CONFIG_DESCRIPTION",
298584af7a61SUlf Magnusson						     "prefer 'help' over '---help---' for new help texts\n" . $herecurr);
298684af7a61SUlf Magnusson					}
2987a1385803SAndy Whitcroft					$length = -1;
2988a1385803SAndy Whitcroft				}
2989a1385803SAndy Whitcroft
29909fe287d7SAndy Whitcroft				$f =~ s/^.//;
29913354957aSAndi Kleen				$f =~ s/#.*//;
29923354957aSAndi Kleen				$f =~ s/^\s+//;
29933354957aSAndi Kleen				next if ($f =~ /^$/);
2994678ae162SUlf Magnusson
2995678ae162SUlf Magnusson				# This only checks context lines in the patch
2996678ae162SUlf Magnusson				# and so hopefully shouldn't trigger false
2997678ae162SUlf Magnusson				# positives, even though some of these are
2998678ae162SUlf Magnusson				# common words in help texts
2999678ae162SUlf Magnusson				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3000678ae162SUlf Magnusson						  if|endif|menu|endmenu|source)\b/x) {
30019fe287d7SAndy Whitcroft					$is_end = 1;
30029fe287d7SAndy Whitcroft					last;
30039fe287d7SAndy Whitcroft				}
30043354957aSAndi Kleen				$length++;
30053354957aSAndi Kleen			}
300656193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3007000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
300856193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
300956193274SVadim Bendebury			}
3010a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
30113354957aSAndi Kleen		}
30123354957aSAndi Kleen
3013628f91a2SJoe Perches# check for MAINTAINERS entries that don't have the right form
3014628f91a2SJoe Perches		if ($realfile =~ /^MAINTAINERS$/ &&
3015628f91a2SJoe Perches		    $rawline =~ /^\+[A-Z]:/ &&
3016628f91a2SJoe Perches		    $rawline !~ /^\+[A-Z]:\t\S/) {
3017628f91a2SJoe Perches			if (WARN("MAINTAINERS_STYLE",
3018628f91a2SJoe Perches				 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3019628f91a2SJoe Perches			    $fix) {
3020628f91a2SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3021628f91a2SJoe Perches			}
3022628f91a2SJoe Perches		}
3023628f91a2SJoe Perches
3024327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
3025327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
3026327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
3027327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
3028327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
3029327953e9SChristoph Jaeger		}
3030327953e9SChristoph Jaeger
3031c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3032c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3033c68e5878SArnaud Lacombe			my $flag = $1;
3034c68e5878SArnaud Lacombe			my $replacement = {
3035c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
3036c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
3037c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
3038c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
3039c68e5878SArnaud Lacombe			};
3040c68e5878SArnaud Lacombe
3041c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
3042c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3043c68e5878SArnaud Lacombe		}
3044c68e5878SArnaud Lacombe
3045bff5da43SRob Herring# check for DT compatible documentation
30467dd05b38SFlorian Vaussard		if (defined $root &&
30477dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
30487dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
30497dd05b38SFlorian Vaussard
3050bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3051bff5da43SRob Herring
3052cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3053852d095dSRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3054cc93319bSFlorian Vaussard
3055bff5da43SRob Herring			foreach my $compat (@compats) {
3056bff5da43SRob Herring				my $compat2 = $compat;
3057185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3058185d566bSRob Herring				my $compat3 = $compat;
3059185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3060185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3061bff5da43SRob Herring				if ( $? >> 8 ) {
3062bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3063bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3064bff5da43SRob Herring				}
3065bff5da43SRob Herring
30664fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
30674fbf32a6SFlorian Vaussard				my $vendor = $1;
3068852d095dSRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3069bff5da43SRob Herring				if ( $? >> 8 ) {
3070bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3071cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3072bff5da43SRob Herring				}
3073bff5da43SRob Herring			}
3074bff5da43SRob Herring		}
3075bff5da43SRob Herring
30769f3a8992SRob Herring# check for using SPDX license tag at beginning of files
30779f3a8992SRob Herring		if ($realline == $checklicenseline) {
30789f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
30799f3a8992SRob Herring				$checklicenseline = 2;
30809f3a8992SRob Herring			} elsif ($rawline =~ /^\+/) {
30819f3a8992SRob Herring				my $comment = "";
30829f3a8992SRob Herring				if ($realfile =~ /\.(h|s|S)$/) {
30839f3a8992SRob Herring					$comment = '/*';
30849f3a8992SRob Herring				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
30859f3a8992SRob Herring					$comment = '//';
30869f3a8992SRob Herring				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc)$/) {
30879f3a8992SRob Herring					$comment = '#';
30889f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
30899f3a8992SRob Herring					$comment = '..';
30909f3a8992SRob Herring				}
30919f3a8992SRob Herring
3092fdf13693SJoe Perches# check SPDX comment style for .[chsS] files
3093fdf13693SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
3094fdf13693SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
3095ffbce897SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3096fdf13693SJoe Perches					WARN("SPDX_LICENSE_TAG",
3097fdf13693SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3098fdf13693SJoe Perches				}
3099fdf13693SJoe Perches
31009f3a8992SRob Herring				if ($comment !~ /^$/ &&
3101ffbce897SJoe Perches				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
31029f3a8992SRob Herring					WARN("SPDX_LICENSE_TAG",
31039f3a8992SRob Herring					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
31043b6e8ac9SJoe Perches				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
31053b6e8ac9SJoe Perches					my $spdx_license = $1;
31063b6e8ac9SJoe Perches					if (!is_SPDX_License_valid($spdx_license)) {
31073b6e8ac9SJoe Perches						WARN("SPDX_LICENSE_TAG",
31083b6e8ac9SJoe Perches						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
31093b6e8ac9SJoe Perches					}
31109f3a8992SRob Herring				}
31119f3a8992SRob Herring			}
31129f3a8992SRob Herring		}
31139f3a8992SRob Herring
31145368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
3115d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
31165368df20SAndy Whitcroft
3117a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3118a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3119a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3120a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3121a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3122a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3123a8da38a9SJoe Perches		}
3124a8da38a9SJoe Perches
312547e0c88bSJoe Perches# line length limit (with some exclusions)
312647e0c88bSJoe Perches#
312747e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
312847e0c88bSJoe Perches#	logging functions like pr_info that end in a string
312947e0c88bSJoe Perches#	lines with a single string
313047e0c88bSJoe Perches#	#defines that are a single string
31312e4bbbc5SAndreas Brauchli#	lines with an RFC3986 like URL
313247e0c88bSJoe Perches#
313347e0c88bSJoe Perches# There are 3 different line length message types:
3134ab1ecabfSJean Delvare# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
313547e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
313647e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
313747e0c88bSJoe Perches#
313847e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
313947e0c88bSJoe Perches#
314047e0c88bSJoe Perches
3141b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
314247e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
314347e0c88bSJoe Perches
314447e0c88bSJoe Perches			# Check the allowed long line types first
314547e0c88bSJoe Perches
314647e0c88bSJoe Perches			# logging functions that end in a string that starts
314747e0c88bSJoe Perches			# before $max_line_length
314847e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
314947e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
315047e0c88bSJoe Perches				$msg_type = "";
315147e0c88bSJoe Perches
315247e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
315347e0c88bSJoe Perches			# #defines with only strings
315447e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
315547e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
315647e0c88bSJoe Perches				$msg_type = "";
315747e0c88bSJoe Perches
3158cc147506SJoe Perches			# More special cases
3159cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3160cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3161d560a5f8SJoe Perches				$msg_type = "";
3162d560a5f8SJoe Perches
31632e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
31642e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
31652e4bbbc5SAndreas Brauchli				$msg_type = "";
31662e4bbbc5SAndreas Brauchli
316747e0c88bSJoe Perches			# Otherwise set the alternate message types
316847e0c88bSJoe Perches
316947e0c88bSJoe Perches			# a comment starts before $max_line_length
317047e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
317147e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
317247e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
317347e0c88bSJoe Perches
317447e0c88bSJoe Perches			# a quoted string starts before $max_line_length
317547e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
317647e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
317747e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
317847e0c88bSJoe Perches			}
317947e0c88bSJoe Perches
318047e0c88bSJoe Perches			if ($msg_type ne "" &&
318147e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
318247e0c88bSJoe Perches				WARN($msg_type,
31836cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
31840a920b5bSAndy Whitcroft			}
318547e0c88bSJoe Perches		}
31860a920b5bSAndy Whitcroft
31878905a67cSAndy Whitcroft# check for adding lines without a newline.
31888905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3189000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
3190000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
31918905a67cSAndy Whitcroft		}
31928905a67cSAndy Whitcroft
3193b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3194de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
31950a920b5bSAndy Whitcroft
31960a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
31970a920b5bSAndy Whitcroft# more than 8 must use tabs.
3198c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3199c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3200c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3201d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
32023705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
32033705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
32043705ce5bSJoe Perches			    $fix) {
3205194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
32063705ce5bSJoe Perches			}
32070a920b5bSAndy Whitcroft		}
32080a920b5bSAndy Whitcroft
320908e44365SAlberto Panizzo# check for space before tabs.
321008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
321108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
32123705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
32133705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
32143705ce5bSJoe Perches			    $fix) {
3215194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3216d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
3217194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3218c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
32193705ce5bSJoe Perches			}
322008e44365SAlberto Panizzo		}
322108e44365SAlberto Panizzo
32226a487211SJoe Perches# check for assignments on the start of a line
32236a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
32246a487211SJoe Perches			CHK("ASSIGNMENT_CONTINUATIONS",
32256a487211SJoe Perches			    "Assignment operator '$1' should be on the previous line\n" . $hereprev);
32266a487211SJoe Perches		}
32276a487211SJoe Perches
3228d1fe9c09SJoe Perches# check for && or || at the start of a line
3229d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3230d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
3231d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
3232d1fe9c09SJoe Perches		}
3233d1fe9c09SJoe Perches
3234a91e8994SJoe Perches# check indentation starts on a tab stop
32355b57980dSJoe Perches		if ($perl_version_ok &&
3236bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3237a91e8994SJoe Perches			my $indent = length($1);
3238a91e8994SJoe Perches			if ($indent % 8) {
3239a91e8994SJoe Perches				if (WARN("TABSTOP",
3240a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3241a91e8994SJoe Perches				    $fix) {
3242a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
3243a91e8994SJoe Perches				}
3244a91e8994SJoe Perches			}
3245a91e8994SJoe Perches		}
3246a91e8994SJoe Perches
3247d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
32485b57980dSJoe Perches		if ($perl_version_ok &&
3249fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3250d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3251d1fe9c09SJoe Perches			my $oldindent = $1;
3252d1fe9c09SJoe Perches			my $rest = $2;
3253d1fe9c09SJoe Perches
3254d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3255d1fe9c09SJoe Perches			if ($pos >= 0) {
3256b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3257b34a26f3SJoe Perches				my $newindent = $2;
3258d1fe9c09SJoe Perches
3259d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3260d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
3261d1fe9c09SJoe Perches					" "  x ($pos % 8);
3262d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3263d1fe9c09SJoe Perches
3264d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3265d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
32663705ce5bSJoe Perches
32673705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
32683705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
32693705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3270194f66fcSJoe Perches						$fixed[$fixlinenr] =~
32713705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
32723705ce5bSJoe Perches					}
3273d1fe9c09SJoe Perches				}
3274d1fe9c09SJoe Perches			}
3275d1fe9c09SJoe Perches		}
3276d1fe9c09SJoe Perches
32776ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
32786ab3a970SJoe Perches# avoid checking a few false positives:
32796ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
32806ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
32816ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
32826ab3a970SJoe Perches#   multiline macros that define functions
32836ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
32846ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
32856ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
32863705ce5bSJoe Perches			if (CHK("SPACING",
3287f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
32883705ce5bSJoe Perches			    $fix) {
3289194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3290f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
32913705ce5bSJoe Perches			}
3292aad4f614SJoe Perches		}
3293aad4f614SJoe Perches
329486406b1cSJoe Perches# Block comment styles
329586406b1cSJoe Perches# Networking with an initial /*
329605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3297fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
329885ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
329985ad978cSJoe Perches		    $realline > 2) {
330005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
330105880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
330205880600SJoe Perches		}
330305880600SJoe Perches
330486406b1cSJoe Perches# Block comments use * on subsequent lines
330586406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
330686406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3307a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
330861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3309a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
331086406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
331186406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3312a605e32eSJoe Perches		}
3313a605e32eSJoe Perches
331486406b1cSJoe Perches# Block comments use */ on trailing lines
331586406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3316c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3317c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3318c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
331986406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
332086406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
332105880600SJoe Perches		}
332205880600SJoe Perches
332308eb9b80SJoe Perches# Block comment * alignment
332408eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3325af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3326af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3327af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
332808eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3329af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3330af207524SJoe Perches			my $oldindent;
333108eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3332af207524SJoe Perches			if (defined($1)) {
3333af207524SJoe Perches				$oldindent = expand_tabs($1);
3334af207524SJoe Perches			} else {
3335af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3336af207524SJoe Perches				$oldindent = expand_tabs($1);
3337af207524SJoe Perches			}
333808eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
333908eb9b80SJoe Perches			my $newindent = $1;
334008eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3341af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
334208eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
334308eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
334408eb9b80SJoe Perches			}
334508eb9b80SJoe Perches		}
334608eb9b80SJoe Perches
33477f619191SJoe Perches# check for missing blank lines after struct/union declarations
33487f619191SJoe Perches# with exceptions for various attributes and macros
33497f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
33507f619191SJoe Perches		    $line =~ /^\+/ &&
33517f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
33527f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
33537f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
33547f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
33557f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
33567f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
33577f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
33580bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
33597f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3360d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3361d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3362d752fcc8SJoe Perches			    $fix) {
3363f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3364d752fcc8SJoe Perches			}
33657f619191SJoe Perches		}
33667f619191SJoe Perches
3367365dd4eaSJoe Perches# check for multiple consecutive blank lines
3368365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3369365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3370365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3371d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3372d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3373d752fcc8SJoe Perches			    $fix) {
3374f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3375d752fcc8SJoe Perches			}
3376d752fcc8SJoe Perches
3377365dd4eaSJoe Perches			$last_blank_line = $linenr;
3378365dd4eaSJoe Perches		}
3379365dd4eaSJoe Perches
33803b617e3bSJoe Perches# check for missing blank lines after declarations
33813f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
33823f7bac03SJoe Perches			# actual declarations
33833f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
33845a4e1fd3SJoe Perches			# function pointer declarations
33855a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
33863f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
33873f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
33883f7bac03SJoe Perches			# known declaration macros
33893f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
33903f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
33913f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
33923f7bac03SJoe Perches			# other possible extensions of declaration lines
33933f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
33943f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
33953f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
33963f7bac03SJoe Perches			# looks like a declaration
33973f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
33985a4e1fd3SJoe Perches			# function pointer declarations
33995a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
34003f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
34013f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
34023f7bac03SJoe Perches			# known declaration macros
34033f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
34043f7bac03SJoe Perches			# start of struct or union or enum
3405328b5f41SJoe Perches		      $sline =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
34063f7bac03SJoe Perches			# start or end of block or continuation of declaration
34073f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
34083f7bac03SJoe Perches			# bitfield continuation
34093f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
34103f7bac03SJoe Perches			# other possible extensions of declaration lines
34113f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
34123f7bac03SJoe Perches			# indentation of previous and current line are the same
34133f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3414d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3415d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3416d752fcc8SJoe Perches			    $fix) {
3417f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3418d752fcc8SJoe Perches			}
34193b617e3bSJoe Perches		}
34203b617e3bSJoe Perches
34215f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
34226b4c5bebSAndy Whitcroft# Exceptions:
34236b4c5bebSAndy Whitcroft#  1) within comments
34246b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
34256b4c5bebSAndy Whitcroft#  3) hanging labels
34263705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
34275f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
34283705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
34293705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
34303705ce5bSJoe Perches			    $fix) {
3431194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
34323705ce5bSJoe Perches			}
34335f7ddae6SRaffaele Recalcati		}
34345f7ddae6SRaffaele Recalcati
3435b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3436b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3437b9ea10d6SAndy Whitcroft
34385751a24eSJoe Perches# check for unusual line ending [ or (
34395751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
34405751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
34415751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
34425751a24eSJoe Perches		}
34435751a24eSJoe Perches
34444dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
34454dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
34464dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
34474dbed76fSJoe Perches			$context_function = $1;
34484dbed76fSJoe Perches		}
34494dbed76fSJoe Perches
34504dbed76fSJoe Perches# check if this appears to be the end of function declaration
34514dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
34524dbed76fSJoe Perches			undef $context_function;
34534dbed76fSJoe Perches		}
34544dbed76fSJoe Perches
3455032a4c0fSJoe Perches# check indentation of any line with a bare else
3456840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3457032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3458032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3459032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3460840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3461840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3462840080a0SJoe Perches			     defined $lines[$linenr] &&
3463840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3464032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3465032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3466032a4c0fSJoe Perches			}
3467032a4c0fSJoe Perches		}
3468032a4c0fSJoe Perches
3469c00df19aSJoe Perches# check indentation of a line with a break;
3470c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3471c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3472c00df19aSJoe Perches			my $tabs = $1;
3473c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3474c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3475c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3476c00df19aSJoe Perches			}
3477c00df19aSJoe Perches		}
3478c00df19aSJoe Perches
3479c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3480cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3481000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3482000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3483c2fdda0dSAndy Whitcroft		}
348422f2a2efSAndy Whitcroft
348556e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
348656e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
348756e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
348856e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
348956e77d70SJoe Perches		}
349056e77d70SJoe Perches
34919c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
34922b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
34932b474a1aSAndy Whitcroft		    $realline_next);
34943e469cdcSAndy Whitcroft#print "LINE<$line>\n";
3495ca819864SJoe Perches		if ($linenr > $suppress_statement &&
34961b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3497170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3498f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3499171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3500171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3501171ae1a4SAndy Whitcroft
35023e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
35033e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
35043e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
35053e469cdcSAndy Whitcroft			# until we hit end of it.
35063e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
35073e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
35083e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
35093e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
35103e469cdcSAndy Whitcroft			}
3511f74bd194SAndy Whitcroft
35122b474a1aSAndy Whitcroft			# Find the real next line.
35132b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
35142b474a1aSAndy Whitcroft			if (defined $realline_next &&
35152b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
35162b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
35172b474a1aSAndy Whitcroft				$realline_next++;
35182b474a1aSAndy Whitcroft			}
35192b474a1aSAndy Whitcroft
3520171ae1a4SAndy Whitcroft			my $s = $stat;
3521171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3522cf655043SAndy Whitcroft
3523c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3524171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3525c2fdda0dSAndy Whitcroft
3526c2fdda0dSAndy Whitcroft			# Ignore functions being called
3527171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3528c2fdda0dSAndy Whitcroft
3529463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3530463f2864SAndy Whitcroft
3531c45dcabdSAndy Whitcroft			# declarations always start with types
3532d2506586SAndy 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) {
3533c45dcabdSAndy Whitcroft				my $type = $1;
3534c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3535c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3536c45dcabdSAndy Whitcroft
35376c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3538a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3539c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3540c2fdda0dSAndy Whitcroft			}
35418905a67cSAndy Whitcroft
35426c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
354365863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3544c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
35459c0ca6f9SAndy Whitcroft			}
35468905a67cSAndy Whitcroft
35478905a67cSAndy Whitcroft			# Check for any sort of function declaration.
35488905a67cSAndy Whitcroft			# int foo(something bar, other baz);
35498905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3550171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
35518905a67cSAndy Whitcroft				my ($name_len) = length($1);
35528905a67cSAndy Whitcroft
3553cf655043SAndy Whitcroft				my $ctx = $s;
3554773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
35558905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3556cf655043SAndy Whitcroft
35578905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3558c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
35598905a67cSAndy Whitcroft
3560c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
35618905a67cSAndy Whitcroft					}
35628905a67cSAndy Whitcroft				}
35638905a67cSAndy Whitcroft			}
35648905a67cSAndy Whitcroft
35659c0ca6f9SAndy Whitcroft		}
35669c0ca6f9SAndy Whitcroft
356700df344fSAndy Whitcroft#
356800df344fSAndy Whitcroft# Checks which may be anchored in the context.
356900df344fSAndy Whitcroft#
357000df344fSAndy Whitcroft
357100df344fSAndy Whitcroft# Check for switch () and associated case and default
357200df344fSAndy Whitcroft# statements should be at the same indent.
357300df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
357400df344fSAndy Whitcroft			my $err = '';
357500df344fSAndy Whitcroft			my $sep = '';
357600df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
357700df344fSAndy Whitcroft			shift(@ctx);
357800df344fSAndy Whitcroft			for my $ctx (@ctx) {
357900df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
358000df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
358100df344fSAndy Whitcroft							$indent != $cindent) {
358200df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
358300df344fSAndy Whitcroft					$sep = '';
358400df344fSAndy Whitcroft				} else {
358500df344fSAndy Whitcroft					$sep = "[...]\n";
358600df344fSAndy Whitcroft				}
358700df344fSAndy Whitcroft			}
358800df344fSAndy Whitcroft			if ($err ne '') {
3589000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3590000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3591de7d4f0eSAndy Whitcroft			}
3592de7d4f0eSAndy Whitcroft		}
3593de7d4f0eSAndy Whitcroft
3594de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3595de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
35960fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3597773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3598773647a0SAndy Whitcroft
35999c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
36008eef05ddSJoe Perches
36018eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
36028eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
36038eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
36048eef05ddSJoe Perches			}
36058eef05ddSJoe Perches
3606de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3607de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3608de7d4f0eSAndy Whitcroft
3609548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3610548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3611de7d4f0eSAndy Whitcroft
3612548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3613548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3614548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3615548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3616548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3617773647a0SAndy Whitcroft				$ctx_ln++;
3618773647a0SAndy Whitcroft			}
3619548596d5SAndy Whitcroft
362053210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
362153210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3622773647a0SAndy Whitcroft
3623773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3624000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3625000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
362601464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
362700df344fSAndy Whitcroft			}
3628773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3629773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3630773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3631773647a0SAndy Whitcroft			{
36329c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
36339c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3634000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3635000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
363601464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
36379c0ca6f9SAndy Whitcroft				}
36389c0ca6f9SAndy Whitcroft			}
363900df344fSAndy Whitcroft		}
364000df344fSAndy Whitcroft
36414d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
3642f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
36433e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
36443e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
36453e469cdcSAndy Whitcroft					if (!defined $stat);
36464d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
36474d001e4dSAndy Whitcroft
36484d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
36494d001e4dSAndy Whitcroft
36509f5af480SJoe Perches			# remove inline comments
36519f5af480SJoe Perches			$s =~ s/$;/ /g;
36529f5af480SJoe Perches			$c =~ s/$;/ /g;
36534d001e4dSAndy Whitcroft
36544d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
36556f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
36566f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
36574d001e4dSAndy Whitcroft
36589f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
36599f5af480SJoe Perches			# none on the first line, and are going to readd them
36609f5af480SJoe Perches			# where necessary.
36619f5af480SJoe Perches			$s =~ s/\n./\n/gs;
36629f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
36639f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
36649f5af480SJoe Perches			}
36659f5af480SJoe Perches
36664d001e4dSAndy Whitcroft			# We want to check the first line inside the block
36674d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
36684d001e4dSAndy Whitcroft			#  1) any blank line termination
36694d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
36704d001e4dSAndy Whitcroft			#  3) any do (...) {
36714d001e4dSAndy Whitcroft			my $continuation = 0;
36724d001e4dSAndy Whitcroft			my $check = 0;
36734d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
36744d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
36754d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
36764d001e4dSAndy Whitcroft				$continuation = 1;
36774d001e4dSAndy Whitcroft			}
36789bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
36794d001e4dSAndy Whitcroft				$check = 1;
36804d001e4dSAndy Whitcroft				$cond_lines++;
36814d001e4dSAndy Whitcroft			}
36824d001e4dSAndy Whitcroft
36834d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
36844d001e4dSAndy Whitcroft			# preprocessor statement.
36854d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
36864d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
36874d001e4dSAndy Whitcroft				$check = 0;
36884d001e4dSAndy Whitcroft			}
36894d001e4dSAndy Whitcroft
36909bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3691740504c6SAndy Whitcroft			$continuation = 0;
36929bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
36939bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
36944d001e4dSAndy Whitcroft
3695f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3696f16fa28fSAndy Whitcroft				# is not linear.
3697f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3698f16fa28fSAndy Whitcroft					$check = 0;
3699f16fa28fSAndy Whitcroft				}
3700f16fa28fSAndy Whitcroft
37019bd49efeSAndy Whitcroft				# Ignore:
37029bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
37039bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
37049bd49efeSAndy Whitcroft				#  3) labels.
3705740504c6SAndy Whitcroft				if ($continuation ||
3706740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
37079bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
37089bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3709740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
371030dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
37119bd49efeSAndy Whitcroft						$cond_lines++;
37129bd49efeSAndy Whitcroft					}
37134d001e4dSAndy Whitcroft				}
371430dad6ebSAndy Whitcroft			}
37154d001e4dSAndy Whitcroft
37164d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
37174d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
37184d001e4dSAndy Whitcroft
37194d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
37204d001e4dSAndy Whitcroft			# this is not this patch's fault.
37214d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
37224d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
37234d001e4dSAndy Whitcroft				$check = 0;
37244d001e4dSAndy Whitcroft			}
37254d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
37264d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
37274d001e4dSAndy Whitcroft			}
37284d001e4dSAndy Whitcroft
37299bd49efeSAndy 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";
37304d001e4dSAndy Whitcroft
37319f5af480SJoe Perches			if ($check && $s ne '' &&
37329f5af480SJoe Perches			    (($sindent % 8) != 0 ||
37339f5af480SJoe Perches			     ($sindent < $indent) ||
3734f6950a73SJoe Perches			     ($sindent == $indent &&
3735f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
37369f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3737000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3738000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
37394d001e4dSAndy Whitcroft			}
37404d001e4dSAndy Whitcroft		}
37414d001e4dSAndy Whitcroft
37426c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
37436c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
37441f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
37451f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
37466c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3747c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3748c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3749cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3750cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
37511f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3752c2fdda0dSAndy Whitcroft		}
37536c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
37546c72ffaaSAndy Whitcroft
375500df344fSAndy Whitcroft#ignore lines not being added
37563705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
375700df344fSAndy Whitcroft
375811ca40a0SJoe Perches# check for dereferences that span multiple lines
375911ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
376011ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
376111ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
376211ca40a0SJoe Perches			my $ref = $1;
376311ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
376411ca40a0SJoe Perches			$ref .= $1;
376511ca40a0SJoe Perches			$ref =~ s/\s//g;
376611ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
376711ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
376811ca40a0SJoe Perches		}
376911ca40a0SJoe Perches
3770a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3771c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3772a1ce18e4SJoe Perches			my $type = $1;
3773a1ce18e4SJoe Perches			my $var = $2;
3774207a8e84SJoe Perches			$var = "" if (!defined $var);
3775207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3776a1ce18e4SJoe Perches				my $sign = $1;
3777a1ce18e4SJoe Perches				my $pointer = $2;
3778a1ce18e4SJoe Perches
3779a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3780a1ce18e4SJoe Perches
3781a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3782a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3783a1ce18e4SJoe Perches				    $fix) {
3784a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3785207a8e84SJoe Perches					my $comp_pointer = $pointer;
3786207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3787207a8e84SJoe Perches					$decl .= $comp_pointer;
3788207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3789207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3790a1ce18e4SJoe Perches				}
3791a1ce18e4SJoe Perches			}
3792a1ce18e4SJoe Perches		}
3793a1ce18e4SJoe Perches
3794653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
37957429c690SAndy Whitcroft		if ($dbg_type) {
37967429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3797000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3798000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
37997429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3800000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3801000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
38027429c690SAndy Whitcroft			}
3803653d4876SAndy Whitcroft			next;
3804653d4876SAndy Whitcroft		}
3805a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3806a1ef277eSAndy Whitcroft		if ($dbg_attr) {
38079360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3808000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3809000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
38109360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3811000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3812000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3813a1ef277eSAndy Whitcroft			}
3814a1ef277eSAndy Whitcroft			next;
3815a1ef277eSAndy Whitcroft		}
3816653d4876SAndy Whitcroft
3817f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
381899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
381999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3820d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3821d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3822f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3823f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3824f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3825d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3826d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3827f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3828d752fcc8SJoe Perches				$fixedline = $line;
38298d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
3830f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3831d752fcc8SJoe Perches			}
3832f0a594c1SAndy Whitcroft		}
3833f0a594c1SAndy Whitcroft
383400df344fSAndy Whitcroft#
383500df344fSAndy Whitcroft# Checks which are anchored on the added line.
383600df344fSAndy Whitcroft#
383700df344fSAndy Whitcroft
3838653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3839c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3840653d4876SAndy Whitcroft			my $path = $1;
3841653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3842000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3843495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3844495e9d84SJoe Perches			}
3845495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3846495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3847495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3848653d4876SAndy Whitcroft			}
3849653d4876SAndy Whitcroft		}
3850653d4876SAndy Whitcroft
385100df344fSAndy Whitcroft# no C99 // comments
385200df344fSAndy Whitcroft		if ($line =~ m{//}) {
38533705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
38543705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
38553705ce5bSJoe Perches			    $fix) {
3856194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
38573705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
38583705ce5bSJoe Perches					my $comment = trim($1);
3859194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
38603705ce5bSJoe Perches				}
38613705ce5bSJoe Perches			}
386200df344fSAndy Whitcroft		}
386300df344fSAndy Whitcroft		# Remove C99 comments.
38640a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
38656c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
38660a920b5bSAndy Whitcroft
38672b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
38682b474a1aSAndy Whitcroft# the whole statement.
38692b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
38702b474a1aSAndy Whitcroft		if (defined $realline_next &&
38712b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
38722b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
38732b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
38742b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
38753cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
38763cbf62dfSAndy Whitcroft			# a prefix:
38773cbf62dfSAndy Whitcroft			#   XXX(foo);
38783cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3879653d4876SAndy Whitcroft			my $name = $1;
388087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
38813cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
38823cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
38833cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
38843cbf62dfSAndy Whitcroft
38853cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
38862b474a1aSAndy Whitcroft				\n.}\s*$|
388748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
388848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
388948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
38902b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
38912b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
389248012058SAndy Whitcroft			    )/x) {
38932b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
38942b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
38952b474a1aSAndy Whitcroft			} else {
38962b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
38970a920b5bSAndy Whitcroft			}
38980a920b5bSAndy Whitcroft		}
38992b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
39002b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
39012b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
39022b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
39032b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
39042b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
39052b474a1aSAndy Whitcroft		}
39062b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
39072b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3908000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3909000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
39102b474a1aSAndy Whitcroft		}
39110a920b5bSAndy Whitcroft
39125150bda4SJoe Eloff# check for global initialisers.
39136d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3914d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
39156d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3916d5e616fcSJoe Perches			    $fix) {
39176d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3918d5e616fcSJoe Perches			}
3919f0a594c1SAndy Whitcroft		}
39200a920b5bSAndy Whitcroft# check for static initialisers.
39216d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3922d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
39236d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3924d5e616fcSJoe Perches				      $herecurr) &&
3925d5e616fcSJoe Perches			    $fix) {
39266d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3927d5e616fcSJoe Perches			}
39280a920b5bSAndy Whitcroft		}
39290a920b5bSAndy Whitcroft
39301813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
39311813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
39321813087dSJoe Perches			my $tmp = trim($1);
39331813087dSJoe Perches			WARN("MISORDERED_TYPE",
39341813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
39351813087dSJoe Perches		}
39361813087dSJoe Perches
3937809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
3938809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
3939809e082eSJoe Perches			my $type = trim($1);
3940809e082eSJoe Perches			next if ($type !~ /\bint\b/);
3941809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
3942809e082eSJoe Perches			my $new_type = $type;
3943809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
3944809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
3945809e082eSJoe Perches			$new_type =~ s/^const\s+//;
3946809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
3947809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
3948809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
3949809e082eSJoe Perches			$new_type = trim($new_type);
3950809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
3951809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
3952809e082eSJoe Perches			    $fix) {
3953809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
3954809e082eSJoe Perches			}
3955809e082eSJoe Perches		}
3956809e082eSJoe Perches
3957cb710ecaSJoe Perches# check for static const char * arrays.
3958cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3959000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3960000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3961cb710ecaSJoe Perches				$herecurr);
3962cb710ecaSJoe Perches		}
3963cb710ecaSJoe Perches
396477b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
396577b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
396677b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
396777b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
396877b8c0a8SJoe Perches			    $fix) {
396977b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
397077b8c0a8SJoe Perches			}
397177b8c0a8SJoe Perches		}
397277b8c0a8SJoe Perches
3973cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3974cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3975000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3976000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3977cb710ecaSJoe Perches				$herecurr);
3978cb710ecaSJoe Perches		}
3979cb710ecaSJoe Perches
3980ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3981ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3982ab7e23f3SJoe Perches			my $found = $1;
3983ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3984ab7e23f3SJoe Perches				WARN("CONST_CONST",
3985ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3986ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3987ab7e23f3SJoe Perches				WARN("CONST_CONST",
3988ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3989ab7e23f3SJoe Perches			}
3990ab7e23f3SJoe Perches		}
3991ab7e23f3SJoe Perches
39929b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
39939b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
39949b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
39959b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
39969b0fa60dSJoe Perches				$herecurr);
39979b0fa60dSJoe Perches               }
39989b0fa60dSJoe Perches
3999b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4000b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4001b598b670SJoe Perches			my $array = $1;
4002b598b670SJoe 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*\))@) {
4003b598b670SJoe Perches				my $array_div = $1;
4004b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4005b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4006b598b670SJoe Perches				    $fix) {
4007b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4008b598b670SJoe Perches				}
4009b598b670SJoe Perches			}
4010b598b670SJoe Perches		}
4011b598b670SJoe Perches
4012b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
4013b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
4014b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4015b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4016b36190c5SJoe Perches			    $fix) {
4017194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4018b36190c5SJoe Perches			}
4019b36190c5SJoe Perches		}
4020b36190c5SJoe Perches
4021653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4022653d4876SAndy Whitcroft# make sense.
4023653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
40248054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4025c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
40268ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
402746d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4028000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4029000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
40300a920b5bSAndy Whitcroft		}
40310a920b5bSAndy Whitcroft
40320a920b5bSAndy Whitcroft# * goes on variable not on type
403365863862SAndy Whitcroft		# (char*[ const])
4034bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4035bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
40363705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4037d8aaf121SAndy Whitcroft
403865863862SAndy Whitcroft			# Should start with a space.
403965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
404065863862SAndy Whitcroft			# Should not end with a space.
404165863862SAndy Whitcroft			$to =~ s/\s+$//;
404265863862SAndy Whitcroft			# '*'s should not have spaces between.
4043f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
404465863862SAndy Whitcroft			}
4045d8aaf121SAndy Whitcroft
40463705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
404765863862SAndy Whitcroft			if ($from ne $to) {
40483705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40493705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
40503705ce5bSJoe Perches				    $fix) {
40513705ce5bSJoe Perches					my $sub_from = $ident;
40523705ce5bSJoe Perches					my $sub_to = $ident;
40533705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4054194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40553705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
40563705ce5bSJoe Perches				}
405765863862SAndy Whitcroft			}
4058bfcb2cc7SAndy Whitcroft		}
4059bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4060bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
40613705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4062d8aaf121SAndy Whitcroft
406365863862SAndy Whitcroft			# Should start with a space.
406465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
406565863862SAndy Whitcroft			# Should not end with a space.
406665863862SAndy Whitcroft			$to =~ s/\s+$//;
406765863862SAndy Whitcroft			# '*'s should not have spaces between.
4068f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
406965863862SAndy Whitcroft			}
407065863862SAndy Whitcroft			# Modifiers should have spaces.
407165863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
407265863862SAndy Whitcroft
40733705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4074667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
40753705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
40763705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
40773705ce5bSJoe Perches				    $fix) {
40783705ce5bSJoe Perches
40793705ce5bSJoe Perches					my $sub_from = $match;
40803705ce5bSJoe Perches					my $sub_to = $match;
40813705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4082194f66fcSJoe Perches					$fixed[$fixlinenr] =~
40833705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
40843705ce5bSJoe Perches				}
408565863862SAndy Whitcroft			}
40860a920b5bSAndy Whitcroft		}
40870a920b5bSAndy Whitcroft
40889d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
40899d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
40900675a8fbSJean Delvare			my $msg_level = \&WARN;
40910675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
40920675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
40939d3e3c70SJoe Perches				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
40949d3e3c70SJoe Perches		}
40950a920b5bSAndy Whitcroft
40969d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
40978905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4098000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4099000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
41008905a67cSAndy Whitcroft		}
41018905a67cSAndy Whitcroft
410217441227SJoe Perches# check for uses of printk_ratelimit
410317441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4104000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4105000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
410617441227SJoe Perches		}
410717441227SJoe Perches
4108eeef5733SJoe Perches# printk should use KERN_* levels
4109eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4110000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4111eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
411200df344fSAndy Whitcroft		}
41130a920b5bSAndy Whitcroft
4114243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
4115243f3803SJoe Perches			my $orig = $1;
4116243f3803SJoe Perches			my $level = lc($orig);
4117243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
41188f26b837SJoe Perches			my $level2 = $level;
41198f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4120243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4121daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
4122243f3803SJoe Perches		}
4123243f3803SJoe Perches
4124243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
4125d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
4126d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
4127d5e616fcSJoe Perches			    $fix) {
4128194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4129d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
4130d5e616fcSJoe Perches			}
4131243f3803SJoe Perches		}
4132243f3803SJoe Perches
4133dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4134dc139313SJoe Perches			my $orig = $1;
4135dc139313SJoe Perches			my $level = lc($orig);
4136dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4137dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4138dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4139dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4140dc139313SJoe Perches		}
4141dc139313SJoe Perches
414291c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
414391c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
414491c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
414591c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
414691c9afafSAndy Lutomirski			WARN("ENOSYS",
414791c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
414891c9afafSAndy Lutomirski		}
414991c9afafSAndy Lutomirski
4150653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4151653d4876SAndy Whitcroft# or if closed on same line
41525b57980dSJoe Perches		if ($perl_version_ok &&
41532d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
41542d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
41552d453e3bSJoe Perches		    $sline !~ /}/) {
41568d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41572d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
41588d182478SJoe Perches			    $fix) {
41598d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41608d182478SJoe Perches				my $fixed_line = $rawline;
41618d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
41628d182478SJoe Perches				my $line1 = $1;
41638d182478SJoe Perches				my $line2 = $2;
41648d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
41658d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
41668d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
41678d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
41688d182478SJoe Perches				}
41698d182478SJoe Perches			}
41700a920b5bSAndy Whitcroft		}
4171653d4876SAndy Whitcroft
41728905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
41738905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
41748905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
41758d182478SJoe Perches			if (ERROR("OPEN_BRACE",
41768d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
41778d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
41788d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
41798d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
41808d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
41818d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
41828d182478SJoe Perches				$fixedline = $rawline;
41838d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
41848d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
41858d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
41868d182478SJoe Perches				}
41878d182478SJoe Perches			}
41888905a67cSAndy Whitcroft		}
41898905a67cSAndy Whitcroft
41900c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
41913705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
41923705ce5bSJoe Perches			if (WARN("SPACING",
41933705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
41943705ce5bSJoe Perches			    $fix) {
4195194f66fcSJoe Perches				$fixed[$fixlinenr] =~
41963705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
41973705ce5bSJoe Perches			}
41980c73b4ebSAndy Whitcroft		}
41990c73b4ebSAndy Whitcroft
420031070b5dSJoe Perches# Function pointer declarations
420131070b5dSJoe Perches# check spacing between type, funcptr, and args
420231070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
420391f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
420431070b5dSJoe Perches			my $declare = $1;
420531070b5dSJoe Perches			my $pre_pointer_space = $2;
420631070b5dSJoe Perches			my $post_pointer_space = $3;
420731070b5dSJoe Perches			my $funcname = $4;
420831070b5dSJoe Perches			my $post_funcname_space = $5;
420931070b5dSJoe Perches			my $pre_args_space = $6;
421031070b5dSJoe Perches
421191f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
421291f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
421391f72e9cSJoe Perches# don't need a space so don't warn for those.
421491f72e9cSJoe Perches			my $post_declare_space = "";
421591f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
421691f72e9cSJoe Perches				$post_declare_space = $1;
421791f72e9cSJoe Perches				$declare = rtrim($declare);
421891f72e9cSJoe Perches			}
421991f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
422031070b5dSJoe Perches				WARN("SPACING",
422131070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
422291f72e9cSJoe Perches				$post_declare_space = " ";
422331070b5dSJoe Perches			}
422431070b5dSJoe Perches
422531070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
422691f72e9cSJoe Perches# This test is not currently implemented because these declarations are
422791f72e9cSJoe Perches# equivalent to
422891f72e9cSJoe Perches#	int  foo(int bar, ...)
422991f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
423091f72e9cSJoe Perches#
423191f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
423291f72e9cSJoe Perches#				WARN("SPACING",
423391f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
423491f72e9cSJoe Perches#			}
423531070b5dSJoe Perches
423631070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
423731070b5dSJoe Perches			if (defined $pre_pointer_space &&
423831070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
423931070b5dSJoe Perches				WARN("SPACING",
424031070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
424131070b5dSJoe Perches			}
424231070b5dSJoe Perches
424331070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
424431070b5dSJoe Perches			if (defined $post_pointer_space &&
424531070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
424631070b5dSJoe Perches				WARN("SPACING",
424731070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
424831070b5dSJoe Perches			}
424931070b5dSJoe Perches
425031070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
425131070b5dSJoe Perches			if (defined $post_funcname_space &&
425231070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
425331070b5dSJoe Perches				WARN("SPACING",
425431070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
425531070b5dSJoe Perches			}
425631070b5dSJoe Perches
425731070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
425831070b5dSJoe Perches			if (defined $pre_args_space &&
425931070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
426031070b5dSJoe Perches				WARN("SPACING",
426131070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
426231070b5dSJoe Perches			}
426331070b5dSJoe Perches
426431070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
4265194f66fcSJoe Perches				$fixed[$fixlinenr] =~
426691f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
426731070b5dSJoe Perches			}
426831070b5dSJoe Perches		}
426931070b5dSJoe Perches
42708d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
42718d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
4272fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4273fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
42748d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
42758d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
42768d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
4277fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
427838dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
42793705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
42803705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
42813705ce5bSJoe Perches				    $fix) {
4282194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
42833705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
42843705ce5bSJoe Perches				}
42858d31cfceSAndy Whitcroft			}
42868d31cfceSAndy Whitcroft		}
42878d31cfceSAndy Whitcroft
4288f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
42896c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
4290c2fdda0dSAndy Whitcroft			my $name = $1;
4291773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
4292773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
4293c2fdda0dSAndy Whitcroft
4294c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
4295773647a0SAndy Whitcroft			if ($name =~ /^(?:
4296773647a0SAndy Whitcroft				if|for|while|switch|return|case|
4297773647a0SAndy Whitcroft				volatile|__volatile__|
4298773647a0SAndy Whitcroft				__attribute__|format|__extension__|
4299773647a0SAndy Whitcroft				asm|__asm__)$/x)
4300773647a0SAndy Whitcroft			{
4301c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
4302c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
4303c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
4304c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4305773647a0SAndy Whitcroft
4306773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
4307c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4308c2fdda0dSAndy Whitcroft
4309c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
4310c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
4311773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
4312c2fdda0dSAndy Whitcroft
4313c2fdda0dSAndy Whitcroft			} else {
43143705ce5bSJoe Perches				if (WARN("SPACING",
43153705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
43163705ce5bSJoe Perches					     $fix) {
4317194f66fcSJoe Perches					$fixed[$fixlinenr] =~
43183705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
43193705ce5bSJoe Perches				}
4320f0a594c1SAndy Whitcroft			}
43216c72ffaaSAndy Whitcroft		}
43229a4cad4eSEric Nelson
4323653d4876SAndy Whitcroft# Check operator spacing.
43240a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
43253705ce5bSJoe Perches			my $fixed_line = "";
43263705ce5bSJoe Perches			my $line_fixed = 0;
43273705ce5bSJoe Perches
43289c0ca6f9SAndy Whitcroft			my $ops = qr{
43299c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
43309c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
43319c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
43321f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
433384731623SJoe Perches				\?:|\?|:
43349c0ca6f9SAndy Whitcroft			}x;
4335cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
43363705ce5bSJoe Perches
43373705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
43383705ce5bSJoe Perches##			foreach my $el (@elements) {
43393705ce5bSJoe Perches##				print("el: <$el>\n");
43403705ce5bSJoe Perches##			}
43413705ce5bSJoe Perches
43423705ce5bSJoe Perches			my @fix_elements = ();
434300df344fSAndy Whitcroft			my $off = 0;
43446c72ffaaSAndy Whitcroft
43453705ce5bSJoe Perches			foreach my $el (@elements) {
43463705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
43473705ce5bSJoe Perches				$off += length($el);
43483705ce5bSJoe Perches			}
43493705ce5bSJoe Perches
43503705ce5bSJoe Perches			$off = 0;
43513705ce5bSJoe Perches
43526c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4353b34c648bSJoe Perches			my $last_after = -1;
43546c72ffaaSAndy Whitcroft
43550a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
43563705ce5bSJoe Perches
43573705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
43583705ce5bSJoe Perches
43593705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
43603705ce5bSJoe Perches
43614a0df2efSAndy Whitcroft				$off += length($elements[$n]);
43624a0df2efSAndy Whitcroft
436325985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4364773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4365773647a0SAndy Whitcroft				my $cc = '';
4366773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4367773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4368773647a0SAndy Whitcroft				}
4369773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4370773647a0SAndy Whitcroft
43714a0df2efSAndy Whitcroft				my $a = '';
43724a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
43734a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4374cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
43754a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
43764a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4377773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
43784a0df2efSAndy Whitcroft
43790a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
43804a0df2efSAndy Whitcroft
43814a0df2efSAndy Whitcroft				my $c = '';
43820a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
43834a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
43844a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4385cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
43864a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
43874a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
43888b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
43894a0df2efSAndy Whitcroft				} else {
43904a0df2efSAndy Whitcroft					$c = 'E';
43910a920b5bSAndy Whitcroft				}
43920a920b5bSAndy Whitcroft
43934a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
43944a0df2efSAndy Whitcroft
43954a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
43964a0df2efSAndy Whitcroft
43976c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4398de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
43990a920b5bSAndy Whitcroft
440074048ed8SAndy Whitcroft				# Pull out the value of this operator.
44016c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
44020a920b5bSAndy Whitcroft
44031f65f947SAndy Whitcroft				# Get the full operator variant.
44041f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
44051f65f947SAndy Whitcroft
440613214adfSAndy Whitcroft				# Ignore operators passed as parameters.
440713214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4408d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
440913214adfSAndy Whitcroft
4410cf655043SAndy Whitcroft#				# Ignore comments
4411cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
441213214adfSAndy Whitcroft
4413d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
441413214adfSAndy Whitcroft				} elsif ($op eq ';') {
4415cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4416cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
44173705ce5bSJoe Perches						if (ERROR("SPACING",
44183705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4419b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
44203705ce5bSJoe Perches							$line_fixed = 1;
44213705ce5bSJoe Perches						}
4422d8aaf121SAndy Whitcroft					}
4423d8aaf121SAndy Whitcroft
4424d8aaf121SAndy Whitcroft				# // is a comment
4425d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
44260a920b5bSAndy Whitcroft
4427b00e4814SJoe Perches				#   :   when part of a bitfield
4428b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4429b00e4814SJoe Perches					# skip the bitfield test for now
4430b00e4814SJoe Perches
44311f65f947SAndy Whitcroft				# No spaces for:
44321f65f947SAndy Whitcroft				#   ->
4433b00e4814SJoe Perches				} elsif ($op eq '->') {
44344a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
44353705ce5bSJoe Perches						if (ERROR("SPACING",
44363705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4437b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44383705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
44393705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
44403705ce5bSJoe Perches							}
4441b34c648bSJoe Perches							$line_fixed = 1;
44423705ce5bSJoe Perches						}
44430a920b5bSAndy Whitcroft					}
44440a920b5bSAndy Whitcroft
44452381097bSJoe Perches				# , must not have a space before and must have a space on the right.
44460a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
44472381097bSJoe Perches					my $rtrim_before = 0;
44482381097bSJoe Perches					my $space_after = 0;
44492381097bSJoe Perches					if ($ctx =~ /Wx./) {
44502381097bSJoe Perches						if (ERROR("SPACING",
44512381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
44522381097bSJoe Perches							$line_fixed = 1;
44532381097bSJoe Perches							$rtrim_before = 1;
44542381097bSJoe Perches						}
44552381097bSJoe Perches					}
4456cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
44573705ce5bSJoe Perches						if (ERROR("SPACING",
44583705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
44593705ce5bSJoe Perches							$line_fixed = 1;
4460b34c648bSJoe Perches							$last_after = $n;
44612381097bSJoe Perches							$space_after = 1;
44622381097bSJoe Perches						}
44632381097bSJoe Perches					}
44642381097bSJoe Perches					if ($rtrim_before || $space_after) {
44652381097bSJoe Perches						if ($rtrim_before) {
44662381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
44672381097bSJoe Perches						} else {
44682381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
44692381097bSJoe Perches						}
44702381097bSJoe Perches						if ($space_after) {
44712381097bSJoe Perches							$good .= " ";
44723705ce5bSJoe Perches						}
44730a920b5bSAndy Whitcroft					}
44740a920b5bSAndy Whitcroft
44759c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
447674048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
44779c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
44789c0ca6f9SAndy Whitcroft
44799c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
44809c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
44819c0ca6f9SAndy Whitcroft				# unary operator, or a cast
44829c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
448374048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
44840d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4485cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
44863705ce5bSJoe Perches						if (ERROR("SPACING",
44873705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4488b34c648bSJoe Perches							if ($n != $last_after + 2) {
4489b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
44903705ce5bSJoe Perches								$line_fixed = 1;
44913705ce5bSJoe Perches							}
44920a920b5bSAndy Whitcroft						}
4493b34c648bSJoe Perches					}
4494a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4495171ae1a4SAndy Whitcroft						# A unary '*' may be const
4496171ae1a4SAndy Whitcroft
4497171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
44983705ce5bSJoe Perches						if (ERROR("SPACING",
44993705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4500b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
45013705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45023705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
45033705ce5bSJoe Perches							}
4504b34c648bSJoe Perches							$line_fixed = 1;
45053705ce5bSJoe Perches						}
45060a920b5bSAndy Whitcroft					}
45070a920b5bSAndy Whitcroft
45080a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
45090a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4510773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
45113705ce5bSJoe Perches						if (ERROR("SPACING",
45123705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4513b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
45143705ce5bSJoe Perches							$line_fixed = 1;
45153705ce5bSJoe Perches						}
45160a920b5bSAndy Whitcroft					}
4517773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4518773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
45193705ce5bSJoe Perches						if (ERROR("SPACING",
45203705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4521b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45223705ce5bSJoe Perches							$line_fixed = 1;
45233705ce5bSJoe Perches						}
4524653d4876SAndy Whitcroft					}
4525773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
45263705ce5bSJoe Perches						if (ERROR("SPACING",
45273705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4528b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
45293705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
45303705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4531773647a0SAndy Whitcroft							}
4532b34c648bSJoe Perches							$line_fixed = 1;
45333705ce5bSJoe Perches						}
45343705ce5bSJoe Perches					}
45350a920b5bSAndy Whitcroft
45360a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
45379c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
45389c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
45399c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4540c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4541c2fdda0dSAndy Whitcroft					 $op eq '%')
45420a920b5bSAndy Whitcroft				{
4543d2e025f3SJoe Perches					if ($check) {
4544d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4545d2e025f3SJoe Perches							if (CHK("SPACING",
4546d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4547d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4548d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4549d2e025f3SJoe Perches								$line_fixed = 1;
4550d2e025f3SJoe Perches							}
4551d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4552d2e025f3SJoe Perches							if (CHK("SPACING",
4553d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4554d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4555d2e025f3SJoe Perches								$line_fixed = 1;
4556d2e025f3SJoe Perches							}
4557d2e025f3SJoe Perches						}
4558d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
45593705ce5bSJoe Perches						if (ERROR("SPACING",
45603705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4561b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4562b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4563b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4564b34c648bSJoe Perches							}
45653705ce5bSJoe Perches							$line_fixed = 1;
45663705ce5bSJoe Perches						}
45670a920b5bSAndy Whitcroft					}
45680a920b5bSAndy Whitcroft
45691f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
45701f65f947SAndy Whitcroft				# terminating a case value or a label.
45711f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
45721f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
45733705ce5bSJoe Perches						if (ERROR("SPACING",
45743705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4575b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
45763705ce5bSJoe Perches							$line_fixed = 1;
45773705ce5bSJoe Perches						}
45781f65f947SAndy Whitcroft					}
45791f65f947SAndy Whitcroft
45800a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4581cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
45821f65f947SAndy Whitcroft					my $ok = 0;
45831f65f947SAndy Whitcroft
458422f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
45851f65f947SAndy Whitcroft					if (($op eq '<' &&
45861f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
45871f65f947SAndy Whitcroft					    ($op eq '>' &&
45881f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
45891f65f947SAndy Whitcroft					{
45901f65f947SAndy Whitcroft					    	$ok = 1;
45911f65f947SAndy Whitcroft					}
45921f65f947SAndy Whitcroft
4593e0df7e1fSJoe Perches					# for asm volatile statements
4594e0df7e1fSJoe Perches					# ignore a colon with another
4595e0df7e1fSJoe Perches					# colon immediately before or after
4596e0df7e1fSJoe Perches					if (($op eq ':') &&
4597e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4598e0df7e1fSJoe Perches						$ok = 1;
4599e0df7e1fSJoe Perches					}
4600e0df7e1fSJoe Perches
460184731623SJoe Perches					# messages are ERROR, but ?: are CHK
46021f65f947SAndy Whitcroft					if ($ok == 0) {
46030675a8fbSJean Delvare						my $msg_level = \&ERROR;
46040675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
460584731623SJoe Perches
46060675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
46073705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
4608b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4609b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4610b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4611b34c648bSJoe Perches							}
46123705ce5bSJoe Perches							$line_fixed = 1;
46133705ce5bSJoe Perches						}
46140a920b5bSAndy Whitcroft					}
461522f2a2efSAndy Whitcroft				}
46164a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
46173705ce5bSJoe Perches
46183705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
46193705ce5bSJoe Perches
46203705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
46210a920b5bSAndy Whitcroft			}
46223705ce5bSJoe Perches
46233705ce5bSJoe Perches			if (($#elements % 2) == 0) {
46243705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
46253705ce5bSJoe Perches			}
46263705ce5bSJoe Perches
4627194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4628194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
46293705ce5bSJoe Perches			}
46303705ce5bSJoe Perches
46313705ce5bSJoe Perches
46320a920b5bSAndy Whitcroft		}
46330a920b5bSAndy Whitcroft
4634786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4635d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4636786b6326SJoe Perches			if (WARN("SPACING",
4637786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4638786b6326SJoe Perches			    $fix) {
4639194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4640786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4641786b6326SJoe Perches			}
4642786b6326SJoe Perches		}
4643786b6326SJoe Perches
4644f0a594c1SAndy Whitcroft# check for multiple assignments
4645f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4646000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4647000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4648f0a594c1SAndy Whitcroft		}
4649f0a594c1SAndy Whitcroft
465022f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
465122f2a2efSAndy Whitcroft## # continuation.
465222f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
465322f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
465422f2a2efSAndy Whitcroft##
465522f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
465622f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
465722f2a2efSAndy Whitcroft## 			my $ln = $line;
465822f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
465922f2a2efSAndy Whitcroft## 			}
466022f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4661000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4662000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
466322f2a2efSAndy Whitcroft## 			}
466422f2a2efSAndy Whitcroft## 		}
4665f0a594c1SAndy Whitcroft
46660a920b5bSAndy Whitcroft#need space before brace following if, while, etc
46676b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
46686ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
46693705ce5bSJoe Perches			if (ERROR("SPACING",
46703705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
46713705ce5bSJoe Perches			    $fix) {
46726ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
46733705ce5bSJoe Perches			}
4674de7d4f0eSAndy Whitcroft		}
4675de7d4f0eSAndy Whitcroft
4676c4a62ef9SJoe Perches## # check for blank lines before declarations
4677c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4678c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4679c4a62ef9SJoe Perches##			WARN("SPACING",
4680c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4681c4a62ef9SJoe Perches##		}
4682c4a62ef9SJoe Perches##
4683c4a62ef9SJoe Perches
4684de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4685de7d4f0eSAndy Whitcroft# on the line
468694fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
4687d5e616fcSJoe Perches			if (ERROR("SPACING",
4688d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4689d5e616fcSJoe Perches			    $fix) {
4690194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4691d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4692d5e616fcSJoe Perches			}
46930a920b5bSAndy Whitcroft		}
46940a920b5bSAndy Whitcroft
469522f2a2efSAndy Whitcroft# check spacing on square brackets
469622f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
46973705ce5bSJoe Perches			if (ERROR("SPACING",
46983705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
46993705ce5bSJoe Perches			    $fix) {
4700194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47013705ce5bSJoe Perches				    s/\[\s+/\[/;
47023705ce5bSJoe Perches			}
470322f2a2efSAndy Whitcroft		}
470422f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
47053705ce5bSJoe Perches			if (ERROR("SPACING",
47063705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
47073705ce5bSJoe Perches			    $fix) {
4708194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47093705ce5bSJoe Perches				    s/\s+\]/\]/;
47103705ce5bSJoe Perches			}
471122f2a2efSAndy Whitcroft		}
471222f2a2efSAndy Whitcroft
4713c45dcabdSAndy Whitcroft# check spacing on parentheses
47149c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
47159c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
47163705ce5bSJoe Perches			if (ERROR("SPACING",
47173705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
47183705ce5bSJoe Perches			    $fix) {
4719194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47203705ce5bSJoe Perches				    s/\(\s+/\(/;
47213705ce5bSJoe Perches			}
472222f2a2efSAndy Whitcroft		}
472313214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4724c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4725c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
47263705ce5bSJoe Perches			if (ERROR("SPACING",
47273705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
47283705ce5bSJoe Perches			    $fix) {
4729194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47303705ce5bSJoe Perches				    s/\s+\)/\)/;
47313705ce5bSJoe Perches			}
473222f2a2efSAndy Whitcroft		}
473322f2a2efSAndy Whitcroft
4734e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4735e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4736e2826fd0SJoe Perches
4737e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4738ea4acbb1SJoe Perches			my $var = $1;
4739ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4740ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4741ea4acbb1SJoe Perches			    $fix) {
4742ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4743ea4acbb1SJoe Perches			}
4744ea4acbb1SJoe Perches		}
4745ea4acbb1SJoe Perches
4746ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4747ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4748ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4749ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4750ea4acbb1SJoe Perches			my $var = $2;
4751ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4752ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4753ea4acbb1SJoe Perches			    $fix) {
4754ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4755ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4756ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4757ea4acbb1SJoe Perches			}
4758e2826fd0SJoe Perches		}
4759e2826fd0SJoe Perches
476063b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
4761a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
4762a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
47635b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
476463b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
476563b7c73eSJoe Perches			my $if_stat = $1;
476663b7c73eSJoe Perches			my $test = substr($2, 1, -1);
476763b7c73eSJoe Perches			my $herectx;
476863b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
476963b7c73eSJoe Perches				my $match = $1;
477063b7c73eSJoe Perches				# avoid parentheses around potential macro args
477163b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
477263b7c73eSJoe Perches				if (!defined($herectx)) {
477363b7c73eSJoe Perches					$herectx = $here . "\n";
477463b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
477563b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
477663b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
477763b7c73eSJoe Perches						$herectx .=  $rl . "\n";
477863b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
477963b7c73eSJoe Perches					}
478063b7c73eSJoe Perches				}
478163b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
478263b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
478363b7c73eSJoe Perches			}
478463b7c73eSJoe Perches		}
478563b7c73eSJoe Perches
47860a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
47874a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
47880a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
47893705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
47903705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
47913705ce5bSJoe Perches			    $fix) {
4792194f66fcSJoe Perches				$fixed[$fixlinenr] =~
47933705ce5bSJoe Perches				    s/^(.)\s+/$1/;
47943705ce5bSJoe Perches			}
47950a920b5bSAndy Whitcroft		}
47960a920b5bSAndy Whitcroft
47975b9553abSJoe Perches# return is not a function
4798507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4799c45dcabdSAndy Whitcroft			my $spacing = $1;
48005b57980dSJoe Perches			if ($perl_version_ok &&
48015b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
48025b9553abSJoe Perches				my $value = $1;
48035b9553abSJoe Perches				$value = deparenthesize($value);
48045b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4805000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4806000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
48075b9553abSJoe Perches				}
4808c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4809000d1cc1SJoe Perches				ERROR("SPACING",
4810000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4811c45dcabdSAndy Whitcroft			}
4812c45dcabdSAndy Whitcroft		}
4813507e5141SJoe Perches
4814b43ae21bSJoe Perches# unnecessary return in a void function
4815b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4816b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4817b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4818b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4819b43ae21bSJoe Perches		    $linenr >= 3 &&
4820b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4821b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
48229819cf25SJoe Perches			WARN("RETURN_VOID",
4823b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
48249819cf25SJoe Perches               }
48259819cf25SJoe Perches
4826189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
48275b57980dSJoe Perches		if ($perl_version_ok &&
4828189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4829189248d8SJoe Perches			my $openparens = $1;
4830189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4831189248d8SJoe Perches			my $msg = "";
4832189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4833189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4834189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4835189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4836189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4837189248d8SJoe Perches			}
4838189248d8SJoe Perches		}
4839189248d8SJoe Perches
4840c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4841c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4842c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4843c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
48445b57980dSJoe Perches		if ($perl_version_ok &&
4845c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4846c5595fa2SJoe Perches			my $lead = $1;
4847c5595fa2SJoe Perches			my $const = $2;
4848c5595fa2SJoe Perches			my $comp = $3;
4849c5595fa2SJoe Perches			my $to = $4;
4850c5595fa2SJoe Perches			my $newcomp = $comp;
4851f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4852c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4853c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4854c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4855c5595fa2SJoe Perches			    $fix) {
4856c5595fa2SJoe Perches				if ($comp eq "<") {
4857c5595fa2SJoe Perches					$newcomp = ">";
4858c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4859c5595fa2SJoe Perches					$newcomp = ">=";
4860c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4861c5595fa2SJoe Perches					$newcomp = "<";
4862c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4863c5595fa2SJoe Perches					$newcomp = "<=";
4864c5595fa2SJoe Perches				}
4865c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4866c5595fa2SJoe Perches			}
4867c5595fa2SJoe Perches		}
4868c5595fa2SJoe Perches
4869f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4870f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
487153a3c448SAndy Whitcroft			my $name = $1;
487253a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4873000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4874f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
487553a3c448SAndy Whitcroft			}
487653a3c448SAndy Whitcroft		}
4877c45dcabdSAndy Whitcroft
48780a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
48794a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
48803705ce5bSJoe Perches			if (ERROR("SPACING",
48813705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
48823705ce5bSJoe Perches			    $fix) {
4883194f66fcSJoe Perches				$fixed[$fixlinenr] =~
48843705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
48853705ce5bSJoe Perches			}
48860a920b5bSAndy Whitcroft		}
48870a920b5bSAndy Whitcroft
4888f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4889f5fe35ddSAndy Whitcroft# statements after the conditional.
4890170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
48913e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
48923e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
48933e469cdcSAndy Whitcroft					if (!defined $stat);
4894170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4895170d3a22SAndy Whitcroft						$remain_next, $off_next);
4896170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4897170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4898170d3a22SAndy Whitcroft
4899170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4900170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4901170d3a22SAndy Whitcroft				# then count those as offsets.
4902170d3a22SAndy Whitcroft				my ($whitespace) =
4903170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4904170d3a22SAndy Whitcroft				my $offset =
4905170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4906170d3a22SAndy Whitcroft
4907170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4908170d3a22SAndy Whitcroft								$offset} = 1;
4909170d3a22SAndy Whitcroft			}
4910170d3a22SAndy Whitcroft		}
4911170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4912c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4913170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4914171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
49158905a67cSAndy Whitcroft
4916b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4917000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4918000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
49198905a67cSAndy Whitcroft			}
49208905a67cSAndy Whitcroft
49218905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
49228905a67cSAndy Whitcroft			# conditional.
4923773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
49248905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
492513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
492653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
492753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4928773647a0SAndy Whitcroft			{
4929bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4930bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4931bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
493242bdf74cSHidetoshi Seto				my $stat_real = '';
4933bb44ad39SAndy Whitcroft
493442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
493542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4936bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4937bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4938bb44ad39SAndy Whitcroft				}
4939bb44ad39SAndy Whitcroft
4940000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4941000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
49428905a67cSAndy Whitcroft			}
49438905a67cSAndy Whitcroft		}
49448905a67cSAndy Whitcroft
494513214adfSAndy Whitcroft# Check for bitwise tests written as boolean
494613214adfSAndy Whitcroft		if ($line =~ /
494713214adfSAndy Whitcroft			(?:
494813214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
494913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
495013214adfSAndy Whitcroft				(?:\&\&|\|\|)
495113214adfSAndy Whitcroft			|
495213214adfSAndy Whitcroft				(?:\&\&|\|\|)
495313214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
495413214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
495513214adfSAndy Whitcroft			)/x)
495613214adfSAndy Whitcroft		{
4957000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4958000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
495913214adfSAndy Whitcroft		}
496013214adfSAndy Whitcroft
49618905a67cSAndy Whitcroft# if and else should not have general statements after it
496213214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
496313214adfSAndy Whitcroft			my $s = $1;
496413214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
496513214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4966000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4967000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
49680a920b5bSAndy Whitcroft			}
496913214adfSAndy Whitcroft		}
497039667782SAndy Whitcroft# if should not continue a brace
497139667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4972000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4973048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
497439667782SAndy Whitcroft				$herecurr);
497539667782SAndy Whitcroft		}
4976a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4977a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4978a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
49793fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4980a1080bf8SAndy Whitcroft			\s*return\s+
4981a1080bf8SAndy Whitcroft		    )/xg)
4982a1080bf8SAndy Whitcroft		{
4983000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4984000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4985a1080bf8SAndy Whitcroft		}
49860a920b5bSAndy Whitcroft
49870a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
49880a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
49898b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
49900a920b5bSAndy Whitcroft		    $previndent == $indent) {
49918b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
49928b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
49938b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49948b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
49958b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49968b8856f4SJoe Perches				my $fixedline = $prevrawline;
49978b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
49988b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
49998b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50008b8856f4SJoe Perches				}
50018b8856f4SJoe Perches				$fixedline = $rawline;
50028b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
50038b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
50048b8856f4SJoe Perches			}
50050a920b5bSAndy Whitcroft		}
50060a920b5bSAndy Whitcroft
50078b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5008c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5009c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5010c2fdda0dSAndy Whitcroft
5011c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5012c2fdda0dSAndy Whitcroft			# conditional.
5013773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5014c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5015c2fdda0dSAndy Whitcroft
5016c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
50178b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
50188b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
50198b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
50208b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
50218b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
50228b8856f4SJoe Perches					my $fixedline = $prevrawline;
50238b8856f4SJoe Perches					my $trailing = $rawline;
50248b8856f4SJoe Perches					$trailing =~ s/^\+//;
50258b8856f4SJoe Perches					$trailing = trim($trailing);
50268b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
50278b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50288b8856f4SJoe Perches				}
5029c2fdda0dSAndy Whitcroft			}
5030c2fdda0dSAndy Whitcroft		}
5031c2fdda0dSAndy Whitcroft
503295e2c602SJoe Perches#Specific variable tests
5033323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5034323c1260SJoe Perches			my $var = $1;
503595e2c602SJoe Perches
503695e2c602SJoe Perches#CamelCase
5037807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5038be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
503922735ce8SJoe Perches#Ignore Page<foo> variants
5040807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5041*d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5042*d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5043*d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5044f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5045f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
50467e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
50477e781f67SJoe Perches					my $word = $1;
50487e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5049d8b07710SJoe Perches					if ($check) {
5050d8b07710SJoe Perches						seed_camelcase_includes();
5051d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5052d8b07710SJoe Perches							seed_camelcase_file($realfile);
5053d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5054d8b07710SJoe Perches						}
5055d8b07710SJoe Perches					}
50567e781f67SJoe Perches					if (!defined $camelcase{$word}) {
50577e781f67SJoe Perches						$camelcase{$word} = 1;
5058be79794bSJoe Perches						CHK("CAMELCASE",
50597e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
50607e781f67SJoe Perches					}
5061323c1260SJoe Perches				}
5062323c1260SJoe Perches			}
50633445686aSJoe Perches		}
50640a920b5bSAndy Whitcroft
50650a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5066d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5067d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5068d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5069d5e616fcSJoe Perches			    $fix) {
5070194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5071d5e616fcSJoe Perches			}
50720a920b5bSAndy Whitcroft		}
50730a920b5bSAndy Whitcroft
50740e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
50750e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5076c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5077e09dec48SAndy Whitcroft			my $file = "$1.h";
5078e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5079e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5080e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
50817840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5082c45dcabdSAndy Whitcroft			{
50830e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
50840e212e0aSFabian Frederick				if ($asminclude > 0) {
5085e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5086000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5087000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5088e09dec48SAndy Whitcroft					} else {
5089000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5090000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5091e09dec48SAndy Whitcroft					}
50920a920b5bSAndy Whitcroft				}
50930a920b5bSAndy Whitcroft			}
50940e212e0aSFabian Frederick		}
50950a920b5bSAndy Whitcroft
5096653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5097653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5098cf655043SAndy Whitcroft# in a known good container
5099b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5100b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5101d8aaf121SAndy Whitcroft			my $ln = $linenr;
5102d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5103c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5104c45dcabdSAndy Whitcroft			my $ctx = '';
510508a2843eSJoe Perches			my $has_flow_statement = 0;
510608a2843eSJoe Perches			my $has_arg_concat = 0;
5107c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5108f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5109f74bd194SAndy Whitcroft			$ctx = $dstat;
5110c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5111a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5112c45dcabdSAndy Whitcroft
511308a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
511462e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
511508a2843eSJoe Perches
5116f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5117f59b64bfSJoe Perches			my $define_args = $1;
5118f59b64bfSJoe Perches			my $define_stmt = $dstat;
5119f59b64bfSJoe Perches			my @def_args = ();
5120f59b64bfSJoe Perches
5121f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5122f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5123f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
51248c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5125f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5126f59b64bfSJoe Perches			}
5127f59b64bfSJoe Perches
5128292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5129c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5130c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5131c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5132c45dcabdSAndy Whitcroft
5133c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
5134bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
5135bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
51366b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
5137bf30d6edSAndy Whitcroft			{
5138c45dcabdSAndy Whitcroft			}
5139c45dcabdSAndy Whitcroft
5140e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
514133acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
514233acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5143e45bab8eSAndy Whitcroft			{
5144e45bab8eSAndy Whitcroft			}
5145e45bab8eSAndy Whitcroft
514642e15293SJoe Perches			# Make asm volatile uses seem like a generic function
514742e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
514842e15293SJoe Perches
5149c45dcabdSAndy Whitcroft			my $exceptions = qr{
5150c45dcabdSAndy Whitcroft				$Declare|
5151c45dcabdSAndy Whitcroft				module_param_named|
5152a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5153c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5154c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5155383099fdSAndy Whitcroft				__typeof__\(|
515622fd2d3eSStefani Seibold				union|
515722fd2d3eSStefani Seibold				struct|
5158ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
51596b10df42SVladimir Zapolskiy				^\"|\"$|
51606b10df42SVladimir Zapolskiy				^\[
5161c45dcabdSAndy Whitcroft			}x;
51625eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5163f59b64bfSJoe Perches
5164f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5165f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5166e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5167f59b64bfSJoe Perches
5168f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5169f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5170f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
51713cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5172356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5173f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
5174f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5175e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
517672f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5177f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5178f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5179f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
51804e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
5181f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5182c45dcabdSAndy Whitcroft			{
5183e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
5184e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5185e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5186e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
5187f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5188f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5189f74bd194SAndy Whitcroft				} else {
5190000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
5191388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5192d8aaf121SAndy Whitcroft				}
5193f59b64bfSJoe Perches
5194f59b64bfSJoe Perches			}
51955207649bSJoe Perches
51965207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
51975207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
51985207649bSJoe Perches			my $first = 1;
51995207649bSJoe Perches			$define_stmt = "";
52005207649bSJoe Perches			foreach my $l (@stmt_array) {
52015207649bSJoe Perches				$l =~ s/\\$//;
52025207649bSJoe Perches				if ($first) {
52035207649bSJoe Perches					$define_stmt = $l;
52045207649bSJoe Perches					$first = 0;
52055207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
52065207649bSJoe Perches					$define_stmt .= substr($l, 1);
52075207649bSJoe Perches				}
52085207649bSJoe Perches			}
52095207649bSJoe Perches			$define_stmt =~ s/$;//g;
52105207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
52115207649bSJoe Perches			$define_stmt = trim($define_stmt);
52125207649bSJoe Perches
5213f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
5214f59b64bfSJoe Perches			foreach my $arg (@def_args) {
5215f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
52169192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
52177fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
52186dba824eSBrendan Jackman				$tmp_stmt =~ s/\b(sizeof|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
52197fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
52207fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5221d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5222f59b64bfSJoe Perches				if ($use_cnt > 1) {
5223f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
5224f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5225f59b64bfSJoe Perches				    }
52269192d41aSJoe Perches# check if any macro arguments may have other precedence issues
52277fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
52289192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
52299192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
52309192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
52319192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
52329192d41aSJoe Perches				}
52330a920b5bSAndy Whitcroft			}
52345023d347SJoe Perches
523508a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
523608a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
523708a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
523808a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
5239e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
524008a2843eSJoe Perches
524108a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
524208a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
524308a2843eSJoe Perches			}
524408a2843eSJoe Perches
5245481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
52465023d347SJoe Perches
52475023d347SJoe Perches		} else {
52485023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
5249481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5250481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
52515023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
52525023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
52535023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
52545023d347SJoe Perches			}
5255653d4876SAndy Whitcroft		}
52560a920b5bSAndy Whitcroft
5257b13edf7fSJoe Perches# do {} while (0) macro tests:
5258b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
5259b13edf7fSJoe Perches# macro should not end with a semicolon
52605b57980dSJoe Perches		if ($perl_version_ok &&
5261b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
5262b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5263b13edf7fSJoe Perches			my $ln = $linenr;
5264b13edf7fSJoe Perches			my $cnt = $realcnt;
5265b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
5266b13edf7fSJoe Perches			my $ctx = '';
5267b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
5268b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
5269b13edf7fSJoe Perches			$ctx = $dstat;
5270b13edf7fSJoe Perches
5271b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
52721b36b201SJoe Perches			$dstat =~ s/$;/ /g;
5273b13edf7fSJoe Perches
5274b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5275b13edf7fSJoe Perches				my $stmts = $2;
5276b13edf7fSJoe Perches				my $semis = $3;
5277b13edf7fSJoe Perches
5278b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
5279b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
5280e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5281b13edf7fSJoe Perches
5282ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
5283ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5284b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5285b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5286b13edf7fSJoe Perches				}
5287b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
5288b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5289b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5290b13edf7fSJoe Perches				}
5291f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5292f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
5293f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
5294e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5295f5ef95b1SJoe Perches
5296f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
5297f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
5298b13edf7fSJoe Perches			}
5299b13edf7fSJoe Perches		}
5300b13edf7fSJoe Perches
5301f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
530213214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
530313214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
5304cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
530513214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5306cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5307cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
5308aad4f614SJoe Perches				my @allowed = ();
5309aad4f614SJoe Perches				my $allow = 0;
531013214adfSAndy Whitcroft				my $seen = 0;
5311773647a0SAndy Whitcroft				my $herectx = $here . "\n";
5312cf655043SAndy Whitcroft				my $ln = $linenr - 1;
531313214adfSAndy Whitcroft				for my $chunk (@chunks) {
531413214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
531513214adfSAndy Whitcroft
5316773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5317773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5318773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5319773647a0SAndy Whitcroft
5320aad4f614SJoe Perches					$allowed[$allow] = 0;
5321773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5322773647a0SAndy Whitcroft
5323773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5324773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5325773647a0SAndy Whitcroft
5326773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5327cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5328cf655043SAndy Whitcroft
5329773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
533013214adfSAndy Whitcroft
533113214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
533213214adfSAndy Whitcroft
5333aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5334cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5335cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5336aad4f614SJoe Perches						$allowed[$allow] = 1;
533713214adfSAndy Whitcroft					}
533813214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5339cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5340aad4f614SJoe Perches						$allowed[$allow] = 1;
534113214adfSAndy Whitcroft					}
5342cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5343cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5344aad4f614SJoe Perches						$allowed[$allow] = 1;
534513214adfSAndy Whitcroft					}
5346aad4f614SJoe Perches					$allow++;
534713214adfSAndy Whitcroft				}
5348aad4f614SJoe Perches				if ($seen) {
5349aad4f614SJoe Perches					my $sum_allowed = 0;
5350aad4f614SJoe Perches					foreach (@allowed) {
5351aad4f614SJoe Perches						$sum_allowed += $_;
5352aad4f614SJoe Perches					}
5353aad4f614SJoe Perches					if ($sum_allowed == 0) {
5354000d1cc1SJoe Perches						WARN("BRACES",
5355000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5356aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5357aad4f614SJoe Perches						 $seen != $allow) {
5358aad4f614SJoe Perches						CHK("BRACES",
5359aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5360aad4f614SJoe Perches					}
536113214adfSAndy Whitcroft				}
536213214adfSAndy Whitcroft			}
536313214adfSAndy Whitcroft		}
5364773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
536513214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5366cf655043SAndy Whitcroft			my $allowed = 0;
5367f0a594c1SAndy Whitcroft
5368cf655043SAndy Whitcroft			# Check the pre-context.
5369cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5370cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5371cf655043SAndy Whitcroft				$allowed = 1;
5372f0a594c1SAndy Whitcroft			}
5373773647a0SAndy Whitcroft
5374773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5375773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5376773647a0SAndy Whitcroft
5377cf655043SAndy Whitcroft			# Check the condition.
5378cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5379773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5380cf655043SAndy Whitcroft			if (defined $cond) {
5381773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5382cf655043SAndy Whitcroft			}
5383cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5384cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5385cf655043SAndy Whitcroft				$allowed = 1;
5386cf655043SAndy Whitcroft			}
5387cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5388cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5389cf655043SAndy Whitcroft				$allowed = 1;
5390cf655043SAndy Whitcroft			}
5391cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5392cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5393cf655043SAndy Whitcroft				$allowed = 1;
5394cf655043SAndy Whitcroft			}
5395cf655043SAndy Whitcroft			# Check the post-context.
5396cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5397cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5398cf655043SAndy Whitcroft				if (defined $cond) {
5399773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5400cf655043SAndy Whitcroft				}
5401cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5402cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5403cf655043SAndy Whitcroft					$allowed = 1;
5404cf655043SAndy Whitcroft				}
5405cf655043SAndy Whitcroft			}
5406cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
5407f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5408e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
5409cf655043SAndy Whitcroft
5410000d1cc1SJoe Perches				WARN("BRACES",
5411000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5412f0a594c1SAndy Whitcroft			}
5413f0a594c1SAndy Whitcroft		}
5414f0a594c1SAndy Whitcroft
5415e4c5babdSJoe Perches# check for single line unbalanced braces
541695330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
541795330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
5418e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5419e4c5babdSJoe Perches		}
5420e4c5babdSJoe Perches
54210979ae66SJoe Perches# check for unnecessary blank lines around braces
542277b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5423f8e58219SJoe Perches			if (CHK("BRACES",
5424f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5425f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5426f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5427f8e58219SJoe Perches			}
54280979ae66SJoe Perches		}
542977b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5430f8e58219SJoe Perches			if (CHK("BRACES",
5431f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5432f8e58219SJoe Perches			    $fix) {
5433f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5434f8e58219SJoe Perches			}
54350979ae66SJoe Perches		}
54360979ae66SJoe Perches
54374a0df2efSAndy Whitcroft# no volatiles please
54386c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
54396c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5440000d1cc1SJoe Perches			WARN("VOLATILE",
54418c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
54424a0df2efSAndy Whitcroft		}
54434a0df2efSAndy Whitcroft
54445e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
54455e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
54465e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
54475e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
544833acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
54495e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
54505e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
54515e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
54525e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
54535e4f6ba5SJoe Perches				     $fix &&
54545e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
54555e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
54565e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
54575e4f6ba5SJoe Perches				my $comma_close = "";
54585e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
54595e4f6ba5SJoe Perches					$comma_close = $1;
54605e4f6ba5SJoe Perches				}
54615e4f6ba5SJoe Perches
54625e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
54635e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
54645e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
54655e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
54665e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
54675e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
54685e4f6ba5SJoe Perches				$fixedline = $rawline;
54695e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
54705e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
54715e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
54725e4f6ba5SJoe Perches				}
54735e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
54745e4f6ba5SJoe Perches			}
54755e4f6ba5SJoe Perches		}
54765e4f6ba5SJoe Perches
54775e4f6ba5SJoe Perches# check for missing a space in a string concatenation
54785e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
54795e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
54805e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
54815e4f6ba5SJoe Perches		}
54825e4f6ba5SJoe Perches
548377cb8546SJoe Perches# check for an embedded function name in a string when the function is known
5484e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
5485e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
5486e4b7d309SJoe Perches# function declarations
548777cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
548877cb8546SJoe Perches		    defined($context_function) &&
5489e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5490e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
549177cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5492e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
549377cb8546SJoe Perches		}
549477cb8546SJoe Perches
54955e4f6ba5SJoe Perches# check for spaces before a quoted newline
54965e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
54975e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
54985e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
54995e4f6ba5SJoe Perches			    $fix) {
55005e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
55015e4f6ba5SJoe Perches			}
55025e4f6ba5SJoe Perches
55035e4f6ba5SJoe Perches		}
55045e4f6ba5SJoe Perches
5505f17dba4fSJoe Perches# concatenated string without spaces between elements
550679682c0cSJoe Perches		if ($line =~ /$String[A-Za-z0-9_]/ || $line =~ /[A-Za-z0-9_]$String/) {
550779682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
550879682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
550979682c0cSJoe Perches			    $fix) {
551079682c0cSJoe Perches				while ($line =~ /($String)/g) {
551179682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
551279682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
551379682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
551479682c0cSJoe Perches				}
551579682c0cSJoe Perches			}
5516f17dba4fSJoe Perches		}
5517f17dba4fSJoe Perches
551890ad30e5SJoe Perches# uncoalesced string fragments
551933acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
552079682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
552179682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
552279682c0cSJoe Perches			    $fix) {
552379682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
552479682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
552579682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
552679682c0cSJoe Perches				}
552779682c0cSJoe Perches			}
552890ad30e5SJoe Perches		}
552990ad30e5SJoe Perches
5530522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
5531522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
5532522b837cSAlexey Dobriyan		my $show_Z = 1;
55335e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5534522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
55355e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
5536522b837cSAlexey Dobriyan			# check for %L
5537522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
55385e4f6ba5SJoe Perches				WARN("PRINTF_L",
5539522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5540522b837cSAlexey Dobriyan				$show_L = 0;
55415e4f6ba5SJoe Perches			}
5542522b837cSAlexey Dobriyan			# check for %Z
5543522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5544522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
5545522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5546522b837cSAlexey Dobriyan				$show_Z = 0;
5547522b837cSAlexey Dobriyan			}
5548522b837cSAlexey Dobriyan			# check for 0x<decimal>
5549522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5550522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
55516e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
55526e300757SJoe Perches			}
55535e4f6ba5SJoe Perches		}
55545e4f6ba5SJoe Perches
55555e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
55563f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
55575e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
55585e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
55595e4f6ba5SJoe Perches		}
55605e4f6ba5SJoe Perches
556100df344fSAndy Whitcroft# warn about #if 0
5562c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
556360f89010SPrakruthi Deepak Heragu			WARN("IF_0",
556460f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
556560f89010SPrakruthi Deepak Heragu		}
556660f89010SPrakruthi Deepak Heragu
556760f89010SPrakruthi Deepak Heragu# warn about #if 1
556860f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
556960f89010SPrakruthi Deepak Heragu			WARN("IF_1",
557060f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
55714a0df2efSAndy Whitcroft		}
55724a0df2efSAndy Whitcroft
557303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
557403df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5575100425deSJoe Perches			my $tested = quotemeta($1);
5576100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5577100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5578100425deSJoe Perches				my $func = $1;
5579100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5580100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5581100425deSJoe Perches				    $fix) {
5582100425deSJoe Perches					my $do_fix = 1;
5583100425deSJoe Perches					my $leading_tabs = "";
5584100425deSJoe Perches					my $new_leading_tabs = "";
5585100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5586100425deSJoe Perches						$leading_tabs = $1;
5587100425deSJoe Perches					} else {
5588100425deSJoe Perches						$do_fix = 0;
5589100425deSJoe Perches					}
5590100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5591100425deSJoe Perches						$new_leading_tabs = $1;
5592100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5593100425deSJoe Perches							$do_fix = 0;
5594100425deSJoe Perches						}
5595100425deSJoe Perches					} else {
5596100425deSJoe Perches						$do_fix = 0;
5597100425deSJoe Perches					}
5598100425deSJoe Perches					if ($do_fix) {
5599100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5600100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5601100425deSJoe Perches					}
5602100425deSJoe Perches				}
56034c432a8fSGreg Kroah-Hartman			}
56044c432a8fSGreg Kroah-Hartman		}
5605f0a594c1SAndy Whitcroft
5606ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5607ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5608ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5609ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5610ebfdc409SJoe Perches		    $linenr > 3) {
5611ebfdc409SJoe Perches			my $testval = $2;
5612ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5613ebfdc409SJoe Perches
5614ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5615ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5616ebfdc409SJoe Perches
5617e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
5618e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
5619ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5620ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5621ebfdc409SJoe Perches			}
5622ebfdc409SJoe Perches		}
5623ebfdc409SJoe Perches
5624f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5625dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5626f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5627f78d98f6SJoe Perches			my $level = $1;
5628f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5629f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5630f78d98f6SJoe Perches			    $fix) {
5631f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5632f78d98f6SJoe Perches			}
5633f78d98f6SJoe Perches		}
5634f78d98f6SJoe Perches
563545c55e92SJoe Perches# check for logging continuations
563645c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
563745c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
563845c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
563945c55e92SJoe Perches		}
564045c55e92SJoe Perches
5641abb08a53SJoe Perches# check for mask then right shift without a parentheses
56425b57980dSJoe Perches		if ($perl_version_ok &&
5643abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5644abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5645abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5646abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5647abb08a53SJoe Perches		}
5648abb08a53SJoe Perches
5649b75ac618SJoe Perches# check for pointer comparisons to NULL
56505b57980dSJoe Perches		if ($perl_version_ok) {
5651b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5652b75ac618SJoe Perches				my $val = $1;
5653b75ac618SJoe Perches				my $equal = "!";
5654b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5655b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5656b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5657b75ac618SJoe Perches					    $fix) {
5658b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5659b75ac618SJoe Perches				}
5660b75ac618SJoe Perches			}
5661b75ac618SJoe Perches		}
5662b75ac618SJoe Perches
56638716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
56648716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
56658716de38SJoe Perches			my $attr = $1;
56668716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
56678716de38SJoe Perches				my $ptr = $1;
56688716de38SJoe Perches				my $var = $2;
56698716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
56708716de38SJoe Perches				      ERROR("MISPLACED_INIT",
56718716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
56728716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
56738716de38SJoe Perches				      WARN("MISPLACED_INIT",
56748716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
56758716de38SJoe Perches				    $fix) {
5676194f66fcSJoe 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;
56778716de38SJoe Perches				}
56788716de38SJoe Perches			}
56798716de38SJoe Perches		}
56808716de38SJoe Perches
5681e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5682e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5683e970b884SJoe Perches			my $attr = $1;
5684e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5685e970b884SJoe Perches			my $attr_prefix = $1;
5686e970b884SJoe Perches			my $attr_type = $2;
5687e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5688e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5689e970b884SJoe Perches			    $fix) {
5690194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5691e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5692e970b884SJoe Perches			}
5693e970b884SJoe Perches		}
5694e970b884SJoe Perches
5695e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5696e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5697e970b884SJoe Perches			my $attr = $1;
5698e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5699e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5700e970b884SJoe Perches			    $fix) {
5701194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5702e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5703e970b884SJoe Perches				$lead = rtrim($1);
5704e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5705e970b884SJoe Perches				$lead = "${lead}const ";
5706194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5707e970b884SJoe Perches			}
5708e970b884SJoe Perches		}
5709e970b884SJoe Perches
5710c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5711c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5712c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5713c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5714c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5715c17893c7SJoe Perches			    $fix) {
5716c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5717c17893c7SJoe Perches			}
5718c17893c7SJoe Perches		}
5719c17893c7SJoe Perches
5720fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5721fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5722fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5723fbdb8138SJoe Perches			my $constant_func = $1;
5724fbdb8138SJoe Perches			my $func = $constant_func;
5725fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5726fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5727fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5728fbdb8138SJoe Perches			    $fix) {
5729194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5730fbdb8138SJoe Perches			}
5731fbdb8138SJoe Perches		}
5732fbdb8138SJoe Perches
57331a15a250SPatrick Pannuto# prefer usleep_range over udelay
573437581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
573543c1d77cSJoe Perches			my $delay = $1;
57361a15a250SPatrick Pannuto			# ignore udelay's < 10, however
573743c1d77cSJoe Perches			if (! ($delay < 10) ) {
5738000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
5739458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
574043c1d77cSJoe Perches			}
574143c1d77cSJoe Perches			if ($delay > 2000) {
574243c1d77cSJoe Perches				WARN("LONG_UDELAY",
574343c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
57441a15a250SPatrick Pannuto			}
57451a15a250SPatrick Pannuto		}
57461a15a250SPatrick Pannuto
574709ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
574809ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
574909ef8725SPatrick Pannuto			if ($1 < 20) {
5750000d1cc1SJoe Perches				WARN("MSLEEP",
5751458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
575209ef8725SPatrick Pannuto			}
575309ef8725SPatrick Pannuto		}
575409ef8725SPatrick Pannuto
575536ec1939SJoe Perches# check for comparisons of jiffies
575636ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
575736ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
575836ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
575936ec1939SJoe Perches		}
576036ec1939SJoe Perches
57619d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
57629d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
57639d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
57649d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
57659d7a34a5SJoe Perches		}
57669d7a34a5SJoe Perches
576700df344fSAndy Whitcroft# warn about #ifdefs in C files
5768c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
576900df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
577000df344fSAndy Whitcroft#			print "$herecurr";
577100df344fSAndy Whitcroft#			$clean = 0;
577200df344fSAndy Whitcroft#		}
577300df344fSAndy Whitcroft
577422f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5775c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
57763705ce5bSJoe Perches			if (ERROR("SPACING",
57773705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
57783705ce5bSJoe Perches			    $fix) {
5779194f66fcSJoe Perches				$fixed[$fixlinenr] =~
57803705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
57813705ce5bSJoe Perches			}
57823705ce5bSJoe Perches
578322f2a2efSAndy Whitcroft		}
578422f2a2efSAndy Whitcroft
57854a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5786171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5787171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
57884a0df2efSAndy Whitcroft			my $which = $1;
57894a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5790000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5791000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
57924a0df2efSAndy Whitcroft			}
57934a0df2efSAndy Whitcroft		}
57944a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5795402c2553SMichael S. Tsirkin
5796402c2553SMichael S. Tsirkin		my $barriers = qr{
5797402c2553SMichael S. Tsirkin			mb|
5798402c2553SMichael S. Tsirkin			rmb|
5799402c2553SMichael S. Tsirkin			wmb|
5800402c2553SMichael S. Tsirkin			read_barrier_depends
5801402c2553SMichael S. Tsirkin		}x;
5802402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5803402c2553SMichael S. Tsirkin			mb__before_atomic|
5804402c2553SMichael S. Tsirkin			mb__after_atomic|
5805402c2553SMichael S. Tsirkin			store_release|
5806402c2553SMichael S. Tsirkin			load_acquire|
5807402c2553SMichael S. Tsirkin			store_mb|
5808402c2553SMichael S. Tsirkin			(?:$barriers)
5809402c2553SMichael S. Tsirkin		}x;
5810402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5811402c2553SMichael S. Tsirkin			(?:$barriers)|
581243e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
581343e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5814402c2553SMichael S. Tsirkin		}x;
5815402c2553SMichael S. Tsirkin
5816402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
58174a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5818c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5819000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
58204a0df2efSAndy Whitcroft			}
58214a0df2efSAndy Whitcroft		}
58223ad81779SPaul E. McKenney
5823f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5824f4073b0fSMichael S. Tsirkin
5825f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5826f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5827f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5828f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5829f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5830f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5831f4073b0fSMichael S. Tsirkin		}
5832f4073b0fSMichael S. Tsirkin
5833cb426e99SJoe Perches# check for waitqueue_active without a comment.
5834cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5835cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5836cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5837cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5838cb426e99SJoe Perches			}
5839cb426e99SJoe Perches		}
58403ad81779SPaul E. McKenney
584191db2592SPaul E. McKenney# check for smp_read_barrier_depends and read_barrier_depends
584291db2592SPaul E. McKenney		if (!$file && $line =~ /\b(smp_|)read_barrier_depends\s*\(/) {
584391db2592SPaul E. McKenney			WARN("READ_BARRIER_DEPENDS",
584491db2592SPaul E. McKenney			     "$1read_barrier_depends should only be used in READ_ONCE or DEC Alpha code\n" . $herecurr);
584591db2592SPaul E. McKenney		}
584691db2592SPaul E. McKenney
58474a0df2efSAndy Whitcroft# check of hardware specific defines
5848c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5849000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5850000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
58510a920b5bSAndy Whitcroft		}
5852653d4876SAndy Whitcroft
5853596ed45bSJoe Perches# check that the storage class is not after a type
5854596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
5855000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5856596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
5857596ed45bSJoe Perches		}
5858596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
5859596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
5860596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
5861596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
5862596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
5863596ed45bSJoe Perches			WARN("STORAGE_CLASS",
5864596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
5865d4977c78STobias Klauser		}
5866d4977c78STobias Klauser
5867de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5868de7d4f0eSAndy Whitcroft# storage class and type.
58699c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
58709c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5871000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5872000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5873de7d4f0eSAndy Whitcroft		}
5874de7d4f0eSAndy Whitcroft
58758905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
58762b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58772b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5878d5e616fcSJoe Perches			if (WARN("INLINE",
5879d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5880d5e616fcSJoe Perches			    $fix) {
5881194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5882d5e616fcSJoe Perches
5883d5e616fcSJoe Perches			}
58848905a67cSAndy Whitcroft		}
58858905a67cSAndy Whitcroft
58863d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
58872b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58882b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5889000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5890000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
58913d130fd0SJoe Perches		}
58923d130fd0SJoe Perches
589339b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
58942b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
58952b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5896000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5897000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
589839b7e287SJoe Perches		}
589939b7e287SJoe Perches
5900462811d9SJoe Perches# Check for __attribute__ section, prefer __section
5901462811d9SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5902462811d9SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*_*section_*\s*\(\s*("[^"]*")/) {
5903462811d9SJoe Perches			my $old = substr($rawline, $-[1], $+[1] - $-[1]);
5904462811d9SJoe Perches			my $new = substr($old, 1, -1);
5905462811d9SJoe Perches			if (WARN("PREFER_SECTION",
5906462811d9SJoe Perches				 "__section($new) is preferred over __attribute__((section($old)))\n" . $herecurr) &&
5907462811d9SJoe Perches			    $fix) {
5908462811d9SJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*_*section_*\s*\(\s*\Q$old\E\s*\)\s*\)\s*\)/__section($new)/;
5909462811d9SJoe Perches			}
5910462811d9SJoe Perches		}
5911462811d9SJoe Perches
59125f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
59132b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59142b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5915d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5916d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5917d5e616fcSJoe Perches			    $fix) {
5918194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5919d5e616fcSJoe Perches
5920d5e616fcSJoe Perches			}
59215f14d3bdSJoe Perches		}
59225f14d3bdSJoe Perches
59236061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
59242b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
59252b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5926d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5927d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5928d5e616fcSJoe Perches			    $fix) {
5929194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5930d5e616fcSJoe Perches			}
59316061d949SJoe Perches		}
59326061d949SJoe Perches
5933619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
59345b57980dSJoe Perches		if ($perl_version_ok &&
5935619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5936619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5937619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5938619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5939619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5940619a908aSJoe Perches		}
5941619a908aSJoe Perches
5942fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5943e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5944fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5945e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5946e6176fa4SJoe Perches			my $type = $1;
5947e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5948e6176fa4SJoe Perches				$type = $1;
5949e6176fa4SJoe Perches				my $kernel_type = 'u';
5950e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5951e6176fa4SJoe Perches				$type =~ /(\d+)/;
5952e6176fa4SJoe Perches				$kernel_type .= $1;
5953e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5954e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5955e6176fa4SJoe Perches				    $fix) {
5956e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5957e6176fa4SJoe Perches				}
5958e6176fa4SJoe Perches			}
5959e6176fa4SJoe Perches		}
5960e6176fa4SJoe Perches
5961938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5962938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5963938224b5SJoe Perches			my $cast = $1;
5964938224b5SJoe Perches			my $const = $2;
5965938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5966938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5967938224b5SJoe Perches			    $fix) {
5968938224b5SJoe Perches				my $suffix = "";
5969938224b5SJoe Perches				my $newconst = $const;
5970938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5971938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5972938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5973938224b5SJoe Perches					$suffix .= 'LL';
5974938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5975938224b5SJoe Perches					$suffix .= 'L';
5976938224b5SJoe Perches				}
5977938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5978938224b5SJoe Perches			}
5979938224b5SJoe Perches		}
5980938224b5SJoe Perches
59818f53a9b8SJoe Perches# check for sizeof(&)
59828f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5983000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5984000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
59858f53a9b8SJoe Perches		}
59868f53a9b8SJoe Perches
598766c80b60SJoe Perches# check for sizeof without parenthesis
598866c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5989d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5990d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5991d5e616fcSJoe Perches			    $fix) {
5992194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5993d5e616fcSJoe Perches			}
599466c80b60SJoe Perches		}
599566c80b60SJoe Perches
599688982feaSJoe Perches# check for struct spinlock declarations
599788982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
599888982feaSJoe Perches			WARN("USE_SPINLOCK_T",
599988982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
600088982feaSJoe Perches		}
600188982feaSJoe Perches
6002a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
600306668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6004a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6005caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6006caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6007d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6008d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6009d5e616fcSJoe Perches				    $fix) {
6010194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6011d5e616fcSJoe Perches				}
6012a6962d72SJoe Perches			}
6013a6962d72SJoe Perches		}
6014a6962d72SJoe Perches
60150b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
60165b57980dSJoe Perches		if ($perl_version_ok &&
60170b523769SJoe Perches		    defined $stat &&
60180b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
60190b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6020e3c6bc95STobin C. Harding			my $stat_real;
6021e3c6bc95STobin C. Harding
60220b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
60230b523769SJoe Perches			$lc = $lc + $linenr;
60240b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6025ffe07513SJoe Perches				my $specifier;
6026ffe07513SJoe Perches				my $extension;
60273bd32d6aSSakari Ailus				my $qualifier;
6028ffe07513SJoe Perches				my $bad_specifier = "";
60290b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
60300b523769SJoe Perches				$fmt =~ s/%%//g;
6031e3c6bc95STobin C. Harding
60323bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6033e3c6bc95STobin C. Harding					$specifier = $1;
6034e3c6bc95STobin C. Harding					$extension = $2;
60353bd32d6aSSakari Ailus					$qualifier = $3;
6036361b0d28SLinus Torvalds					if ($extension !~ /[SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
60373bd32d6aSSakari Ailus					    ($extension eq "f" &&
60383bd32d6aSSakari Ailus					     defined $qualifier && $qualifier !~ /^w/)) {
6039e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
60400b523769SJoe Perches						last;
60410b523769SJoe Perches					}
6042e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6043e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6044e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
60450b523769SJoe Perches						}
6046e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6047e3c6bc95STobin C. Harding						     "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n");
6048e3c6bc95STobin C. Harding					}
6049e3c6bc95STobin C. Harding				}
6050e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
60512a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
60521df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
60531df7338aSSergey Senozhatsky					my $use = "";
6054e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
60551df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6056e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
60571df7338aSSergey Senozhatsky					}
60582a9f9d85STobin C. Harding
60590b523769SJoe Perches					WARN("VSPRINTF_POINTER_EXTENSION",
6060e3c6bc95STobin C. Harding					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6061e3c6bc95STobin C. Harding				}
60620b523769SJoe Perches			}
60630b523769SJoe Perches		}
60640b523769SJoe Perches
6065554e165cSAndy Whitcroft# Check for misused memsets
60665b57980dSJoe Perches		if ($perl_version_ok &&
6067d1fe9c09SJoe Perches		    defined $stat &&
60689e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6069554e165cSAndy Whitcroft
6070d7c76ba7SJoe Perches			my $ms_addr = $2;
6071d1fe9c09SJoe Perches			my $ms_val = $7;
6072d1fe9c09SJoe Perches			my $ms_size = $12;
6073d7c76ba7SJoe Perches
6074554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6075554e165cSAndy Whitcroft				ERROR("MEMSET",
6076d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6077554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6078554e165cSAndy Whitcroft				WARN("MEMSET",
6079d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6080d7c76ba7SJoe Perches			}
6081d7c76ba7SJoe Perches		}
6082d7c76ba7SJoe Perches
608398a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
60845b57980dSJoe Perches#		if ($perl_version_ok &&
6085f333195dSJoe Perches#		    defined $stat &&
6086f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6087f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6088f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6089f333195dSJoe Perches#			    $fix) {
6090f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6091f333195dSJoe Perches#			}
6092f333195dSJoe Perches#		}
609398a9bba5SJoe Perches
6094b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
60955b57980dSJoe Perches#		if ($perl_version_ok &&
6096f333195dSJoe Perches#		    defined $stat &&
6097f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6098f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6099f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6100f333195dSJoe Perches#		}
6101b6117d17SMateusz Kulikowski
61028617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
61038617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
61045b57980dSJoe Perches#		if ($perl_version_ok &&
6105f333195dSJoe Perches#		    defined $stat &&
6106f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6107f333195dSJoe Perches#
6108f333195dSJoe Perches#			my $ms_val = $7;
6109f333195dSJoe Perches#
6110f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6111f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
6112f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6113f333195dSJoe Perches#				    $fix) {
6114f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6115f333195dSJoe Perches#				}
6116f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6117f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6118f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6119f333195dSJoe Perches#				    $fix) {
6120f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6121f333195dSJoe Perches#				}
6122f333195dSJoe Perches#			}
6123f333195dSJoe Perches#		}
61248617cd09SMateusz Kulikowski
6125d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
61265b57980dSJoe Perches		if ($perl_version_ok &&
6127d1fe9c09SJoe Perches		    defined $stat &&
6128d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6129d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
6130d7c76ba7SJoe Perches				my $call = $1;
6131d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
6132d7c76ba7SJoe Perches				my $arg1 = $3;
6133d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
6134d1fe9c09SJoe Perches				my $arg2 = $8;
6135d7c76ba7SJoe Perches				my $cast;
6136d7c76ba7SJoe Perches
6137d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6138d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
6139d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
6140d7c76ba7SJoe Perches					$cast = $cast1;
6141d7c76ba7SJoe Perches				} else {
6142d7c76ba7SJoe Perches					$cast = $cast2;
6143d7c76ba7SJoe Perches				}
6144d7c76ba7SJoe Perches				WARN("MINMAX",
6145d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6146554e165cSAndy Whitcroft			}
6147554e165cSAndy Whitcroft		}
6148554e165cSAndy Whitcroft
61494a273195SJoe Perches# check usleep_range arguments
61505b57980dSJoe Perches		if ($perl_version_ok &&
61514a273195SJoe Perches		    defined $stat &&
61524a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
61534a273195SJoe Perches			my $min = $1;
61544a273195SJoe Perches			my $max = $7;
61554a273195SJoe Perches			if ($min eq $max) {
61564a273195SJoe Perches				WARN("USLEEP_RANGE",
6157458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61584a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
61594a273195SJoe Perches				 $min > $max) {
61604a273195SJoe Perches				WARN("USLEEP_RANGE",
6161458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
61624a273195SJoe Perches			}
61634a273195SJoe Perches		}
61644a273195SJoe Perches
6165823b794cSJoe Perches# check for naked sscanf
61665b57980dSJoe Perches		if ($perl_version_ok &&
6167823b794cSJoe Perches		    defined $stat &&
61686c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
6169823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6170823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6171823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6172823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
6173823b794cSJoe Perches			$lc = $lc + $linenr;
61742a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6175823b794cSJoe Perches			WARN("NAKED_SSCANF",
6176823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6177823b794cSJoe Perches		}
6178823b794cSJoe Perches
6179afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
61805b57980dSJoe Perches		if ($perl_version_ok &&
6181afc819abSJoe Perches		    defined $stat &&
6182afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
6183afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
6184afc819abSJoe Perches			$lc = $lc + $linenr;
61852a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
6186afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6187afc819abSJoe Perches				my $format = $6;
6188afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
6189afc819abSJoe Perches				if ($count == 1 &&
6190afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6191afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
6192afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6193afc819abSJoe Perches				}
6194afc819abSJoe Perches			}
6195afc819abSJoe Perches		}
6196afc819abSJoe Perches
619770dc8a48SJoe Perches# check for new externs in .h files.
619870dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
619970dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6200d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
620170dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
620270dc8a48SJoe Perches			    $fix) {
6203194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
620470dc8a48SJoe Perches			}
620570dc8a48SJoe Perches		}
620670dc8a48SJoe Perches
6207de7d4f0eSAndy Whitcroft# check for new externs in .c files.
6208171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
6209c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6210171ae1a4SAndy Whitcroft		{
6211c45dcabdSAndy Whitcroft			my $function_name = $1;
6212c45dcabdSAndy Whitcroft			my $paren_space = $2;
6213171ae1a4SAndy Whitcroft
6214171ae1a4SAndy Whitcroft			my $s = $stat;
6215171ae1a4SAndy Whitcroft			if (defined $cond) {
6216171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
6217171ae1a4SAndy Whitcroft			}
6218c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
6219c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
6220c45dcabdSAndy Whitcroft			{
6221000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
6222000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
6223de7d4f0eSAndy Whitcroft			}
6224de7d4f0eSAndy Whitcroft
6225171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
6226000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
6227000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
6228171ae1a4SAndy Whitcroft			}
62299c9ba34eSAndy Whitcroft
62309c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
62319c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
62329c9ba34eSAndy Whitcroft		{
6233000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
6234000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
6235171ae1a4SAndy Whitcroft		}
6236171ae1a4SAndy Whitcroft
6237a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
6238a0ad7596SJoe Perches		if (defined $stat &&
623925bdda2bSMiles Chen		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6240ca0d8929SJoe Perches		    $1 ne "void") {
6241ca0d8929SJoe Perches			my $args = trim($1);
6242ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6243ca0d8929SJoe Perches				my $arg = trim($1);
6244ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6245ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
6246ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6247ca0d8929SJoe Perches				}
6248ca0d8929SJoe Perches			}
6249ca0d8929SJoe Perches		}
6250ca0d8929SJoe Perches
6251a0ad7596SJoe Perches# check for function definitions
62525b57980dSJoe Perches		if ($perl_version_ok &&
6253a0ad7596SJoe Perches		    defined $stat &&
6254a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6255a0ad7596SJoe Perches			$context_function = $1;
6256a0ad7596SJoe Perches
6257a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
6258a0ad7596SJoe Perches			my $ok = 0;
6259a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
6260a0ad7596SJoe Perches			my $herectx = $here . "\n";
6261a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
6262a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
6263a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
6264a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
6265a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6266a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
6267a0ad7596SJoe Perches			}
6268a0ad7596SJoe Perches			if (!$ok) {
6269a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
6270a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6271a0ad7596SJoe Perches			}
6272a0ad7596SJoe Perches		}
6273a0ad7596SJoe Perches
6274de7d4f0eSAndy Whitcroft# checks for new __setup's
6275de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6276de7d4f0eSAndy Whitcroft			my $name = $1;
6277de7d4f0eSAndy Whitcroft
6278de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
6279000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
62808c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
6281de7d4f0eSAndy Whitcroft			}
6282653d4876SAndy Whitcroft		}
62839c0ca6f9SAndy Whitcroft
6284e29a70f1SJoe Perches# check for pointless casting of alloc functions
6285e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6286000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
6287000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
62889c0ca6f9SAndy Whitcroft		}
628913214adfSAndy Whitcroft
6290a640d25cSJoe Perches# alloc style
6291a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
62925b57980dSJoe Perches		if ($perl_version_ok &&
6293e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6294a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
6295a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6296a640d25cSJoe Perches		}
6297a640d25cSJoe Perches
629860a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
62995b57980dSJoe Perches		if ($perl_version_ok &&
63001b4a2ed4SJoe Perches		    defined $stat &&
63011b4a2ed4SJoe Perches		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
630260a55369SJoe Perches			my $oldfunc = $3;
630360a55369SJoe Perches			my $a1 = $4;
630460a55369SJoe Perches			my $a2 = $10;
630560a55369SJoe Perches			my $newfunc = "kmalloc_array";
630660a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
630760a55369SJoe Perches			my $r1 = $a1;
630860a55369SJoe Perches			my $r2 = $a2;
630960a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
631060a55369SJoe Perches				$r1 = $a2;
631160a55369SJoe Perches				$r2 = $a1;
631260a55369SJoe Perches			}
6313e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6314e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
63151b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
6316e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6317e3d95a2aSTobin C. Harding
6318e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
63191b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
63201b4a2ed4SJoe Perches				    $cnt == 1 &&
6321e367455aSJoe Perches				    $fix) {
6322194f66fcSJoe 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;
632360a55369SJoe Perches				}
632460a55369SJoe Perches			}
632560a55369SJoe Perches		}
632660a55369SJoe Perches
6327972fdea2SJoe Perches# check for krealloc arg reuse
63285b57980dSJoe Perches		if ($perl_version_ok &&
63294cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
63304cab63ceSJoe Perches		    $1 eq $3) {
6331972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
6332972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6333972fdea2SJoe Perches		}
6334972fdea2SJoe Perches
63355ce59ae0SJoe Perches# check for alloc argument mismatch
63365ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
63375ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
63385ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
63395ce59ae0SJoe Perches		}
63405ce59ae0SJoe Perches
6341caf2a54fSJoe Perches# check for multiple semicolons
6342caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
6343d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
6344d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6345d5e616fcSJoe Perches			    $fix) {
6346194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
6347d5e616fcSJoe Perches			}
6348d1e2ad07SJoe Perches		}
6349d1e2ad07SJoe Perches
6350cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6351cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
6352cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
63530ab90191SJoe Perches			my $ull = "";
63540ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
63550ab90191SJoe Perches			if (CHK("BIT_MACRO",
63560ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
63570ab90191SJoe Perches			    $fix) {
63580ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
63590ab90191SJoe Perches			}
63600ab90191SJoe Perches		}
63610ab90191SJoe Perches
63622d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
63632d632745SJoe 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*$/) {
63642d632745SJoe Perches			my $config = $1;
63652d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
63662d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
63672d632745SJoe Perches			    $fix) {
63682d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
63692d632745SJoe Perches			}
63702d632745SJoe Perches		}
63712d632745SJoe Perches
6372e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
6373c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6374c34c09a8SJoe Perches			my $has_break = 0;
6375c34c09a8SJoe Perches			my $has_statement = 0;
6376c34c09a8SJoe Perches			my $count = 0;
6377c34c09a8SJoe Perches			my $prevline = $linenr;
6378e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
6379c34c09a8SJoe Perches				$prevline--;
6380c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
6381c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
6382c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
6383c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
6384c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6385c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6386c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
6387c34c09a8SJoe Perches				$has_statement = 1;
6388c34c09a8SJoe Perches				$count++;
6389258f79d5SHeinrich Schuchardt				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|exit\s*\(\b|return\b|goto\b|continue\b)/);
6390c34c09a8SJoe Perches			}
6391c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
6392c34c09a8SJoe Perches				WARN("MISSING_BREAK",
6393224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
6394c34c09a8SJoe Perches			}
6395c34c09a8SJoe Perches		}
6396c34c09a8SJoe Perches
6397d1e2ad07SJoe Perches# check for switch/default statements without a break;
63985b57980dSJoe Perches		if ($perl_version_ok &&
6399d1e2ad07SJoe Perches		    defined $stat &&
6400d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6401d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
6402e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
6403e3d95a2aSTobin C. Harding
6404d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
6405d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
6406caf2a54fSJoe Perches		}
6407caf2a54fSJoe Perches
640813214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
6409d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
6410d5e616fcSJoe Perches			if (WARN("USE_FUNC",
6411d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
6412d5e616fcSJoe Perches			    $fix) {
6413194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
6414d5e616fcSJoe Perches			}
641513214adfSAndy Whitcroft		}
6416773647a0SAndy Whitcroft
641762ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
641862ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
641962ec818fSJoe Perches			ERROR("DATE_TIME",
642062ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
642162ec818fSJoe Perches		}
642262ec818fSJoe Perches
64232c92488aSJoe Perches# check for use of yield()
64242c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
64252c92488aSJoe Perches			WARN("YIELD",
64262c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
64272c92488aSJoe Perches		}
64282c92488aSJoe Perches
6429179f8f40SJoe Perches# check for comparisons against true and false
6430179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6431179f8f40SJoe Perches			my $lead = $1;
6432179f8f40SJoe Perches			my $arg = $2;
6433179f8f40SJoe Perches			my $test = $3;
6434179f8f40SJoe Perches			my $otype = $4;
6435179f8f40SJoe Perches			my $trail = $5;
6436179f8f40SJoe Perches			my $op = "!";
6437179f8f40SJoe Perches
6438179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6439179f8f40SJoe Perches
6440179f8f40SJoe Perches			my $type = lc($otype);
6441179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6442179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6443179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6444179f8f40SJoe Perches					$op = "";
6445179f8f40SJoe Perches				}
6446179f8f40SJoe Perches
6447179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6448179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6449179f8f40SJoe Perches
6450179f8f40SJoe Perches## maybe suggesting a correct construct would better
6451179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6452179f8f40SJoe Perches
6453179f8f40SJoe Perches			}
6454179f8f40SJoe Perches		}
6455179f8f40SJoe Perches
64564882720bSThomas Gleixner# check for semaphores initialized locked
64574882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6458000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6459000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6460773647a0SAndy Whitcroft		}
64616712d858SJoe Perches
646267d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
646367d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6464000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
646567d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6466773647a0SAndy Whitcroft		}
64676712d858SJoe Perches
6468ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6469f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6470000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6471ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6472f3db6639SMichael Ellerman		}
64736712d858SJoe Perches
64743d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
64753d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
64763d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
64773d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
64783d709ab5SPaul E. McKenney		}
64793d709ab5SPaul E. McKenney
64809189c7e7SJoe Perches# check for deprecated apis
64819189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
64829189c7e7SJoe Perches			my $deprecated_api = $1;
64839189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
64849189c7e7SJoe Perches			WARN("DEPRECATED_API",
64859189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
64869189c7e7SJoe Perches		}
64879189c7e7SJoe Perches
64880f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
6489d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
64906903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
6491d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
6492000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6493d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
64942b6db5cbSAndy Whitcroft		}
6495773647a0SAndy Whitcroft
6496773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6497773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6498773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6499c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6500c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6501171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6502171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6503171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6504773647a0SAndy Whitcroft		{
6505000d1cc1SJoe Perches			WARN("NR_CPUS",
6506000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6507773647a0SAndy Whitcroft		}
65089c9ba34eSAndy Whitcroft
650952ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
651052ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
651152ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
651252ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
651352ea8506SJoe Perches		}
651452ea8506SJoe Perches
6515acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
65165b57980dSJoe Perches		if ($perl_version_ok &&
6517acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6518acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6519acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6520acd9362cSJoe Perches		}
6521acd9362cSJoe Perches
6522de3f186fSDenis Efremov# nested likely/unlikely calls
6523de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
6524de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
6525de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
6526de3f186fSDenis Efremov		}
6527de3f186fSDenis Efremov
6528691d77b6SAndy Whitcroft# whine mightly about in_atomic
6529691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6530691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6531000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6532000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6533f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6534000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6535000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6536691d77b6SAndy Whitcroft			}
6537691d77b6SAndy Whitcroft		}
65381704f47bSPeter Zijlstra
65390f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
65400f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
65410f5225b0SPeter Zijlstra			ERROR("LOCKING",
65420f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
65430f5225b0SPeter Zijlstra		}
65440f5225b0SPeter Zijlstra
65451704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
65461704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
65471704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
65481704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
65491704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
65501704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6551000d1cc1SJoe Perches				ERROR("LOCKDEP",
6552000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
65531704f47bSPeter Zijlstra			}
65541704f47bSPeter Zijlstra		}
655588f8831cSDave Jones
6556b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6557b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6558000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6559000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
656088f8831cSDave Jones		}
65612435880fSJoe Perches
656200180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
656300180468SJoe Perches# and whether or not function naming is typical and if
656400180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
65655b57980dSJoe Perches		if ($perl_version_ok &&
656600180468SJoe Perches		    defined $stat &&
656700180468SJoe Perches		    $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) {
656800180468SJoe Perches			my $var = $1;
656900180468SJoe Perches			my $perms = $2;
657000180468SJoe Perches			my $show = $3;
657100180468SJoe Perches			my $store = $4;
657200180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
657300180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
657400180468SJoe Perches			    $store =~ /^${var}_store$/ &&
657500180468SJoe Perches			    $octal_perms eq "0644") {
657600180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
657700180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
657800180468SJoe Perches				    $fix) {
657900180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
658000180468SJoe Perches				}
658100180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
658200180468SJoe Perches				 $store =~ /^NULL$/ &&
658300180468SJoe Perches				 $octal_perms eq "0444") {
658400180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
658500180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
658600180468SJoe Perches				    $fix) {
658700180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
658800180468SJoe Perches				}
658900180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
659000180468SJoe Perches				 $store =~ /^${var}_store$/ &&
659100180468SJoe Perches				 $octal_perms eq "0200") {
659200180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
659300180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
659400180468SJoe Perches				    $fix) {
659500180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
659600180468SJoe Perches				}
659700180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
659800180468SJoe Perches				 $octal_perms eq "0444" ||
659900180468SJoe Perches				 $octal_perms eq "0200") {
660000180468SJoe Perches				my $newshow = "$show";
660100180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
660200180468SJoe Perches				my $newstore = $store;
660300180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
660400180468SJoe Perches				my $rename = "";
660500180468SJoe Perches				if ($show ne $newshow) {
660600180468SJoe Perches					$rename .= " '$show' to '$newshow'";
660700180468SJoe Perches				}
660800180468SJoe Perches				if ($store ne $newstore) {
660900180468SJoe Perches					$rename .= " '$store' to '$newstore'";
661000180468SJoe Perches				}
661100180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
661200180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
661300180468SJoe Perches			} else {
661400180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
661500180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
661600180468SJoe Perches			}
661700180468SJoe Perches		}
661800180468SJoe Perches
6619515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6620515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
662173121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
662273121534SJoe Perches#   specific definition of not visible in sysfs.
662373121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
662473121534SJoe Perches#   use the default permissions
66255b57980dSJoe Perches		if ($perl_version_ok &&
6626459cf0aeSJoe Perches		    defined $stat &&
6627515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
66282435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
66292435880fSJoe Perches				my $func = $entry->[0];
66302435880fSJoe Perches				my $arg_pos = $entry->[1];
66312435880fSJoe Perches
6632459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6633459cf0aeSJoe Perches				$lc = $lc + $linenr;
66342a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
6635459cf0aeSJoe Perches
66362435880fSJoe Perches				my $skip_args = "";
66372435880fSJoe Perches				if ($arg_pos > 1) {
66382435880fSJoe Perches					$arg_pos--;
66392435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
66402435880fSJoe Perches				}
6641f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6642459cf0aeSJoe Perches				if ($stat =~ /$test/) {
66432435880fSJoe Perches					my $val = $1;
66442435880fSJoe Perches					$val = $6 if ($skip_args ne "");
664573121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
664673121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
664773121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
66482435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6649459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6650f90774e1SJoe Perches					}
6651f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6652c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6653459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
66542435880fSJoe Perches					}
6655459cf0aeSJoe Perches				}
6656459cf0aeSJoe Perches			}
6657459cf0aeSJoe Perches		}
6658459cf0aeSJoe Perches
6659459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6660bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
666100180468SJoe Perches			my $oval = $1;
666200180468SJoe Perches			my $octal = perms_to_octal($oval);
6663f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6664459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6665f90774e1SJoe Perches			    $fix) {
666600180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
66672435880fSJoe Perches			}
666813214adfSAndy Whitcroft		}
66695a6d20ceSBjorn Andersson
66705a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
66715a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
66725a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
66735a6d20ceSBjorn Andersson			my $valid_licenses = qr{
66745a6d20ceSBjorn Andersson						GPL|
66755a6d20ceSBjorn Andersson						GPL\ v2|
66765a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
66775a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
66785a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
66795a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
66805a6d20ceSBjorn Andersson						Proprietary
66815a6d20ceSBjorn Andersson					}x;
66825a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
66835a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
66845a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
66855a6d20ceSBjorn Andersson			}
66865a6d20ceSBjorn Andersson		}
66876a8d76cbSMatteo Croce
66886a8d76cbSMatteo Croce# check for sysctl duplicate constants
66896a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
66906a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
66916a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
66926a8d76cbSMatteo Croce		}
6693515a235eSJoe Perches	}
669413214adfSAndy Whitcroft
669513214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
669613214adfSAndy Whitcroft	# so just keep quiet.
669713214adfSAndy Whitcroft	if ($#rawlines == -1) {
669813214adfSAndy Whitcroft		exit(0);
66990a920b5bSAndy Whitcroft	}
67000a920b5bSAndy Whitcroft
67018905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
67028905a67cSAndy Whitcroft	# things that appear to be patches.
67038905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
67048905a67cSAndy Whitcroft		exit(0);
67058905a67cSAndy Whitcroft	}
67068905a67cSAndy Whitcroft
67078905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
67088905a67cSAndy Whitcroft	# just keep quiet.
67098905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
67108905a67cSAndy Whitcroft		exit(0);
67118905a67cSAndy Whitcroft	}
67128905a67cSAndy Whitcroft
6713a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
6714000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6715000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
67160a920b5bSAndy Whitcroft	}
6717cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
6718cd261496SGeert Uytterhoeven		if ($signoff == 0) {
6719000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
6720000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
6721cd261496SGeert Uytterhoeven		} elsif (!$authorsignoff) {
6722cd261496SGeert Uytterhoeven			WARN("NO_AUTHOR_SIGN_OFF",
6723cd261496SGeert Uytterhoeven			     "Missing Signed-off-by: line by nominal patch author '$author'\n");
6724cd261496SGeert Uytterhoeven		}
67250a920b5bSAndy Whitcroft	}
67260a920b5bSAndy Whitcroft
6727f0a594c1SAndy Whitcroft	print report_dump();
672813214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
672913214adfSAndy Whitcroft		print "$filename " if ($summary_file);
67306c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
67316c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
67326c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
67336c72ffaaSAndy Whitcroft	}
67348905a67cSAndy Whitcroft
6735d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6736ef212196SJoe Perches		# If there were any defects found and not already fixing them
6737ef212196SJoe Perches		if (!$clean and !$fix) {
6738ef212196SJoe Perches			print << "EOM"
6739ef212196SJoe Perches
6740ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6741ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6742ef212196SJoe PerchesEOM
6743ef212196SJoe Perches		}
6744d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6745d2c0a235SAndy Whitcroft		# then suggest that.
6746d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6747b0781216SMike Frysinger			$rpt_cleaners = 0;
6748d8469f16SJoe Perches			print << "EOM"
6749d8469f16SJoe Perches
6750d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6751d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6752d8469f16SJoe PerchesEOM
6753d2c0a235SAndy Whitcroft		}
6754d2c0a235SAndy Whitcroft	}
6755d2c0a235SAndy Whitcroft
6756d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6757d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6758d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
67599624b8d6SJoe Perches		my $newfile = $filename;
67609624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
67613705ce5bSJoe Perches		my $linecount = 0;
67623705ce5bSJoe Perches		my $f;
67633705ce5bSJoe Perches
6764d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6765d752fcc8SJoe Perches
67663705ce5bSJoe Perches		open($f, '>', $newfile)
67673705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
67683705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
67693705ce5bSJoe Perches			$linecount++;
67703705ce5bSJoe Perches			if ($file) {
67713705ce5bSJoe Perches				if ($linecount > 3) {
67723705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
67733705ce5bSJoe Perches					print $f $fixed_line . "\n";
67743705ce5bSJoe Perches				}
67753705ce5bSJoe Perches			} else {
67763705ce5bSJoe Perches				print $f $fixed_line . "\n";
67773705ce5bSJoe Perches			}
67783705ce5bSJoe Perches		}
67793705ce5bSJoe Perches		close($f);
67803705ce5bSJoe Perches
67813705ce5bSJoe Perches		if (!$quiet) {
67823705ce5bSJoe Perches			print << "EOM";
6783d8469f16SJoe Perches
67843705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
67853705ce5bSJoe Perches
67863705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
67873705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
67883705ce5bSJoe Perches
67893705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
67903705ce5bSJoe PerchesNo warranties, expressed or implied...
67913705ce5bSJoe PerchesEOM
67923705ce5bSJoe Perches		}
67933705ce5bSJoe Perches	}
67943705ce5bSJoe Perches
6795d8469f16SJoe Perches	if ($quiet == 0) {
6796d8469f16SJoe Perches		print "\n";
6797d8469f16SJoe Perches		if ($clean == 1) {
6798d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6799d8469f16SJoe Perches		} else {
6800d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
68010a920b5bSAndy Whitcroft		}
68020a920b5bSAndy Whitcroft	}
68030a920b5bSAndy Whitcroft	return $clean;
68040a920b5bSAndy Whitcroft}
6805