xref: /linux-6.15/scripts/checkpatch.pl (revision 77cb8546)
10a920b5bSAndy Whitcroft#!/usr/bin/perl -w
2dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
300df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
42a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
5015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
60a920b5bSAndy Whitcroft# Licensed under the terms of the GNU GPL License version 2
70a920b5bSAndy Whitcroft
80a920b5bSAndy Whitcroftuse strict;
9c707a81dSJoe Perchesuse POSIX;
1036061e38SJoe Perchesuse File::Basename;
1136061e38SJoe Perchesuse Cwd 'abs_path';
1257230297SJoe Perchesuse Term::ANSIColor qw(:constants);
130a920b5bSAndy Whitcroft
140a920b5bSAndy Whitcroftmy $P = $0;
1536061e38SJoe Perchesmy $D = dirname(abs_path($P));
160a920b5bSAndy Whitcroft
17000d1cc1SJoe Perchesmy $V = '0.32';
180a920b5bSAndy Whitcroft
190a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
200a920b5bSAndy Whitcroft
210a920b5bSAndy Whitcroftmy $quiet = 0;
220a920b5bSAndy Whitcroftmy $tree = 1;
230a920b5bSAndy Whitcroftmy $chk_signoff = 1;
240a920b5bSAndy Whitcroftmy $chk_patch = 1;
25773647a0SAndy Whitcroftmy $tst_only;
266c72ffaaSAndy Whitcroftmy $emacs = 0;
278905a67cSAndy Whitcroftmy $terse = 0;
2834d8815fSJoe Perchesmy $showfile = 0;
296c72ffaaSAndy Whitcroftmy $file = 0;
304a593c34SDu, Changbinmy $git = 0;
310dea9f1eSJoe Perchesmy %git_commits = ();
326c72ffaaSAndy Whitcroftmy $check = 0;
332ac73b4fSJoe Perchesmy $check_orig = 0;
348905a67cSAndy Whitcroftmy $summary = 1;
358905a67cSAndy Whitcroftmy $mailback = 0;
3613214adfSAndy Whitcroftmy $summary_file = 0;
37000d1cc1SJoe Perchesmy $show_types = 0;
383beb42ecSJoe Perchesmy $list_types = 0;
393705ce5bSJoe Perchesmy $fix = 0;
409624b8d6SJoe Perchesmy $fix_inplace = 0;
416c72ffaaSAndy Whitcroftmy $root;
42c2fdda0dSAndy Whitcroftmy %debug;
433445686aSJoe Perchesmy %camelcase = ();
4491bfe484SJoe Perchesmy %use_type = ();
4591bfe484SJoe Perchesmy @use = ();
4691bfe484SJoe Perchesmy %ignore_type = ();
47000d1cc1SJoe Perchesmy @ignore = ();
4877f5b10aSHannes Edermy $help = 0;
49000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
506cd7f386SJoe Perchesmy $max_line_length = 80;
51d62a201fSDave Hansenmy $ignore_perl_version = 0;
52d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5356193274SVadim Bendeburymy $min_conf_desc_length = 4;
5466b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
55ebfd7d62SJoe Perchesmy $codespell = 0;
56f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
57bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
5857230297SJoe Perchesmy $color = 1;
59dadf680dSJoe Perchesmy $allow_c99_comments = 1;
6077f5b10aSHannes Eder
6177f5b10aSHannes Edersub help {
6277f5b10aSHannes Eder	my ($exitcode) = @_;
6377f5b10aSHannes Eder
6477f5b10aSHannes Eder	print << "EOM";
6577f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
6677f5b10aSHannes EderVersion: $V
6777f5b10aSHannes Eder
6877f5b10aSHannes EderOptions:
6977f5b10aSHannes Eder  -q, --quiet                quiet
7077f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7177f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
7277f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
7377f5b10aSHannes Eder  --emacs                    emacs compile window format
7477f5b10aSHannes Eder  --terse                    one line per report
7534d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
764a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
774a593c34SDu, Changbin                             single git commit with:
784a593c34SDu, Changbin                               <rev>
794a593c34SDu, Changbin                               <rev>^
804a593c34SDu, Changbin                               <rev>~n
814a593c34SDu, Changbin                             multiple git commits with:
824a593c34SDu, Changbin                               <rev1>..<rev2>
834a593c34SDu, Changbin                               <rev1>...<rev2>
844a593c34SDu, Changbin                               <rev>-<count>
854a593c34SDu, Changbin                             git merges are ignored
8677f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
8777f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
883beb42ecSJoe Perches  --list-types               list the possible message types
8991bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
90000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
913beb42ecSJoe Perches  --show-types               show the specific message type in the output
926cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
9356193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
9477f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
9577f5b10aSHannes Eder  --no-summary               suppress the per-file summary
9677f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
9777f5b10aSHannes Eder  --summary-file             include the filename in summary
9877f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
9977f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
10077f5b10aSHannes Eder                             is all off)
10177f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
10277f5b10aSHannes Eder                             literally
1033705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1043705ce5bSJoe Perches                             If correctable single-line errors exist, create
1053705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1063705ce5bSJoe Perches                             with potential errors corrected to the preferred
1073705ce5bSJoe Perches                             checkpatch style
1089624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1099624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1109624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
111d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
112d62a201fSDave Hansen                             runtime errors.
113ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
114f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
115ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
11657230297SJoe Perches  --color                    Use colors when output is STDOUT (default: on)
11777f5b10aSHannes Eder  -h, --help, --version      display this help and exit
11877f5b10aSHannes Eder
11977f5b10aSHannes EderWhen FILE is - read standard input.
12077f5b10aSHannes EderEOM
12177f5b10aSHannes Eder
12277f5b10aSHannes Eder	exit($exitcode);
12377f5b10aSHannes Eder}
12477f5b10aSHannes Eder
1253beb42ecSJoe Perchessub uniq {
1263beb42ecSJoe Perches	my %seen;
1273beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1283beb42ecSJoe Perches}
1293beb42ecSJoe Perches
1303beb42ecSJoe Perchessub list_types {
1313beb42ecSJoe Perches	my ($exitcode) = @_;
1323beb42ecSJoe Perches
1333beb42ecSJoe Perches	my $count = 0;
1343beb42ecSJoe Perches
1353beb42ecSJoe Perches	local $/ = undef;
1363beb42ecSJoe Perches
1373beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1383beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1393beb42ecSJoe Perches
1403beb42ecSJoe Perches	my $text = <$script>;
1413beb42ecSJoe Perches	close($script);
1423beb42ecSJoe Perches
1433beb42ecSJoe Perches	my @types = ();
1443beb42ecSJoe Perches	for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
1453beb42ecSJoe Perches		push (@types, $_);
1463beb42ecSJoe Perches	}
1473beb42ecSJoe Perches	@types = sort(uniq(@types));
1483beb42ecSJoe Perches	print("#\tMessage type\n\n");
1493beb42ecSJoe Perches	foreach my $type (@types) {
1503beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1513beb42ecSJoe Perches	}
1523beb42ecSJoe Perches
1533beb42ecSJoe Perches	exit($exitcode);
1543beb42ecSJoe Perches}
1553beb42ecSJoe Perches
156000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
157000d1cc1SJoe Perchesif (-f $conf) {
158000d1cc1SJoe Perches	my @conf_args;
159000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
160000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
161000d1cc1SJoe Perches
162000d1cc1SJoe Perches	while (<$conffile>) {
163000d1cc1SJoe Perches		my $line = $_;
164000d1cc1SJoe Perches
165000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
166000d1cc1SJoe Perches		$line =~ s/^\s*//g;
167000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
168000d1cc1SJoe Perches
169000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
170000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
171000d1cc1SJoe Perches
172000d1cc1SJoe Perches		my @words = split(" ", $line);
173000d1cc1SJoe Perches		foreach my $word (@words) {
174000d1cc1SJoe Perches			last if ($word =~ m/^#/);
175000d1cc1SJoe Perches			push (@conf_args, $word);
176000d1cc1SJoe Perches		}
177000d1cc1SJoe Perches	}
178000d1cc1SJoe Perches	close($conffile);
179000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
180000d1cc1SJoe Perches}
181000d1cc1SJoe Perches
1820a920b5bSAndy WhitcroftGetOptions(
1836c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1840a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1850a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1860a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1876c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1888905a67cSAndy Whitcroft	'terse!'	=> \$terse,
18934d8815fSJoe Perches	'showfile!'	=> \$showfile,
19077f5b10aSHannes Eder	'f|file!'	=> \$file,
1914a593c34SDu, Changbin	'g|git!'	=> \$git,
1926c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1936c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
194000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
19591bfe484SJoe Perches	'types=s'	=> \@use,
196000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1973beb42ecSJoe Perches	'list-types!'	=> \$list_types,
1986cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
19956193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
2006c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2018905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2028905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
20313214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2043705ce5bSJoe Perches	'fix!'		=> \$fix,
2059624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
206d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
207c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
208773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
209ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
210ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
21157230297SJoe Perches	'color!'	=> \$color,
21277f5b10aSHannes Eder	'h|help'	=> \$help,
21377f5b10aSHannes Eder	'version'	=> \$help
21477f5b10aSHannes Eder) or help(1);
21577f5b10aSHannes Eder
21677f5b10aSHannes Ederhelp(0) if ($help);
2170a920b5bSAndy Whitcroft
2183beb42ecSJoe Percheslist_types(0) if ($list_types);
2193beb42ecSJoe Perches
2209624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2212ac73b4fSJoe Perches$check_orig = $check;
2229624b8d6SJoe Perches
2230a920b5bSAndy Whitcroftmy $exit = 0;
2240a920b5bSAndy Whitcroft
225d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
226d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
227d62a201fSDave Hansen	if (!$ignore_perl_version) {
228d62a201fSDave Hansen		exit(1);
229d62a201fSDave Hansen	}
230d62a201fSDave Hansen}
231d62a201fSDave Hansen
23245107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2330a920b5bSAndy Whitcroftif ($#ARGV < 0) {
23445107ff6SAllen Hubbe	push(@ARGV, '-');
2350a920b5bSAndy Whitcroft}
2360a920b5bSAndy Whitcroft
23791bfe484SJoe Perchessub hash_save_array_words {
23891bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
23991bfe484SJoe Perches
24091bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
24191bfe484SJoe Perches	foreach my $word (@array) {
242000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
243000d1cc1SJoe Perches		$word =~ s/^\s*//g;
244000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
245000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
246000d1cc1SJoe Perches
247000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
248000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
249000d1cc1SJoe Perches
25091bfe484SJoe Perches		$hashRef->{$word}++;
251000d1cc1SJoe Perches	}
25291bfe484SJoe Perches}
25391bfe484SJoe Perches
25491bfe484SJoe Perchessub hash_show_words {
25591bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
25691bfe484SJoe Perches
2573c816e49SJoe Perches	if (keys %$hashRef) {
258d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
25958cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
26091bfe484SJoe Perches			print " $word";
26191bfe484SJoe Perches		}
262d8469f16SJoe Perches		print "\n";
26391bfe484SJoe Perches	}
26491bfe484SJoe Perches}
26591bfe484SJoe Perches
26691bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
26791bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
268000d1cc1SJoe Perches
269c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
270c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2717429c690SAndy Whitcroftmy $dbg_type = 0;
272a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
273c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
27421caa13cSAndy Whitcroft	## no critic
27521caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
27621caa13cSAndy Whitcroft	die "$@" if ($@);
277c2fdda0dSAndy Whitcroft}
278c2fdda0dSAndy Whitcroft
279d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
280d2c0a235SAndy Whitcroft
2818905a67cSAndy Whitcroftif ($terse) {
2828905a67cSAndy Whitcroft	$emacs = 1;
2838905a67cSAndy Whitcroft	$quiet++;
2848905a67cSAndy Whitcroft}
2858905a67cSAndy Whitcroft
2866c72ffaaSAndy Whitcroftif ($tree) {
2876c72ffaaSAndy Whitcroft	if (defined $root) {
2886c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2896c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2906c72ffaaSAndy Whitcroft		}
2916c72ffaaSAndy Whitcroft	} else {
2926c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2936c72ffaaSAndy Whitcroft			$root = '.';
2946c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2956c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2966c72ffaaSAndy Whitcroft			$root = $1;
2976c72ffaaSAndy Whitcroft		}
2986c72ffaaSAndy Whitcroft	}
2996c72ffaaSAndy Whitcroft
3006c72ffaaSAndy Whitcroft	if (!defined $root) {
3010a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3020a920b5bSAndy Whitcroft		exit(2);
3030a920b5bSAndy Whitcroft	}
3046c72ffaaSAndy Whitcroft}
3056c72ffaaSAndy Whitcroft
3066c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3076c72ffaaSAndy Whitcroft
3082ceb532bSAndy Whitcroftour $Ident	= qr{
3092ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3102ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3112ceb532bSAndy Whitcroft		}x;
3126c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3136c72ffaaSAndy Whitcroftour $Sparse	= qr{
3146c72ffaaSAndy Whitcroft			__user|
3156c72ffaaSAndy Whitcroft			__kernel|
3166c72ffaaSAndy Whitcroft			__force|
3176c72ffaaSAndy Whitcroft			__iomem|
3186c72ffaaSAndy Whitcroft			__must_check|
3196c72ffaaSAndy Whitcroft			__init_refok|
320417495edSAndy Whitcroft			__kprobes|
321165e72a6SSven Eckelmann			__ref|
322ad315455SBoqun Feng			__rcu|
323ad315455SBoqun Feng			__private
3246c72ffaaSAndy Whitcroft		}x;
325e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
326e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
327e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
328e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
329e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3308716de38SJoe Perches
33152131292SWolfram Sang# Notes to $Attribute:
33252131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3336c72ffaaSAndy Whitcroftour $Attribute	= qr{
3346c72ffaaSAndy Whitcroft			const|
33503f1df7dSJoe Perches			__percpu|
33603f1df7dSJoe Perches			__nocast|
33703f1df7dSJoe Perches			__safe|
33846d832f5SMichael S. Tsirkin			__bitwise|
33903f1df7dSJoe Perches			__packed__|
34003f1df7dSJoe Perches			__packed2__|
34103f1df7dSJoe Perches			__naked|
34203f1df7dSJoe Perches			__maybe_unused|
34303f1df7dSJoe Perches			__always_unused|
34403f1df7dSJoe Perches			__noreturn|
34503f1df7dSJoe Perches			__used|
34603f1df7dSJoe Perches			__cold|
347e23ef1f3SJoe Perches			__pure|
34803f1df7dSJoe Perches			__noclone|
34903f1df7dSJoe Perches			__deprecated|
3506c72ffaaSAndy Whitcroft			__read_mostly|
3516c72ffaaSAndy Whitcroft			__kprobes|
3528716de38SJoe Perches			$InitAttribute|
35324e1d81aSAndy Whitcroft			____cacheline_aligned|
35424e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3555fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3565fe3af11SAndy Whitcroft			__weak
3576c72ffaaSAndy Whitcroft		  }x;
358c45dcabdSAndy Whitcroftour $Modifier;
35991cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3606c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3616c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3626c72ffaaSAndy Whitcroft
36395e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
36495e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
36595e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
36695e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3672435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
368c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
369326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
370326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
371326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
37274349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3732435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
374326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
375447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
37623f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3776c72ffaaSAndy Whitcroftour $Operators	= qr{
3786c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3796c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
38023f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3816c72ffaaSAndy Whitcroft		  }x;
3826c72ffaaSAndy Whitcroft
38391cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
38491cb5195SJoe Perches
385ab7e23f3SJoe Perchesour $BasicType;
3868905a67cSAndy Whitcroftour $NonptrType;
3871813087dSJoe Perchesour $NonptrTypeMisordered;
3888716de38SJoe Perchesour $NonptrTypeWithAttr;
3898905a67cSAndy Whitcroftour $Type;
3901813087dSJoe Perchesour $TypeMisordered;
3918905a67cSAndy Whitcroftour $Declare;
3921813087dSJoe Perchesour $DeclareMisordered;
3938905a67cSAndy Whitcroft
39415662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
39515662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
396171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
397171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
398171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
399171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
400171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
401171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
402171ae1a4SAndy Whitcroft}x;
403171ae1a4SAndy Whitcroft
40415662b3eSJoe Perchesour $UTF8	= qr{
40515662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
40615662b3eSJoe Perches	| $NON_ASCII_UTF8
40715662b3eSJoe Perches}x;
40815662b3eSJoe Perches
409e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
410021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
411021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
412021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
413021158b4SJoe Perches)};
414e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
415fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4168ed22cadSAndy Whitcroft	atomic_t
4178ed22cadSAndy Whitcroft)};
418e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
419e6176fa4SJoe Perches	$typeC99Typedefs\b|
420e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
421e6176fa4SJoe Perches	$typeKernelTypedefs\b
422e6176fa4SJoe Perches)};
4238ed22cadSAndy Whitcroft
4246d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4256d32f7a3SJoe Perches
426691e669bSJoe Perchesour $logFunctions = qr{(?x:
4276e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
4287d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
4296e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
430b0531722SJoe Perches	panic|
43106668727SJoe Perches	MODULE_[A-Z_]+|
43206668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
433691e669bSJoe Perches)};
434691e669bSJoe Perches
43520112475SJoe Perchesour $signature_tags = qr{(?xi:
43620112475SJoe Perches	Signed-off-by:|
43720112475SJoe Perches	Acked-by:|
43820112475SJoe Perches	Tested-by:|
43920112475SJoe Perches	Reviewed-by:|
44020112475SJoe Perches	Reported-by:|
4418543ae12SMugunthan V N	Suggested-by:|
44220112475SJoe Perches	To:|
44320112475SJoe Perches	Cc:
44420112475SJoe Perches)};
44520112475SJoe Perches
4461813087dSJoe Perchesour @typeListMisordered = (
4471813087dSJoe Perches	qr{char\s+(?:un)?signed},
4481813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4491813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4501813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4511813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4521813087dSJoe Perches	qr{short\s+(?:un)?signed},
4531813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4541813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4551813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4561813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4571813087dSJoe Perches	qr{int\s+(?:un)?signed},
4581813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4591813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4601813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4611813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4621813087dSJoe Perches	qr{long\s+(?:un)?signed},
4631813087dSJoe Perches);
4641813087dSJoe Perches
4658905a67cSAndy Whitcroftour @typeList = (
4668905a67cSAndy Whitcroft	qr{void},
4670c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4680c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4690c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4700c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4710c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4720c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
4730c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4740c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4750c773d9dSJoe Perches	qr{(?:un)?signed},
4768905a67cSAndy Whitcroft	qr{float},
4778905a67cSAndy Whitcroft	qr{double},
4788905a67cSAndy Whitcroft	qr{bool},
4798905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4808905a67cSAndy Whitcroft	qr{union\s+$Ident},
4818905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4828905a67cSAndy Whitcroft	qr{${Ident}_t},
4838905a67cSAndy Whitcroft	qr{${Ident}_handler},
4848905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4851813087dSJoe Perches	@typeListMisordered,
4868905a67cSAndy Whitcroft);
487938224b5SJoe Perches
488938224b5SJoe Perchesour $C90_int_types = qr{(?x:
489938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
490938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
491938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
492938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
493938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
494938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
495938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
496938224b5SJoe Perches
497938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
498938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
499938224b5SJoe Perches	long\s+(?:un)?signed|
500938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
501938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
502938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
503938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
504938224b5SJoe Perches
505938224b5SJoe Perches	int\s+(?:un)?signed|
506938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
507938224b5SJoe Perches)};
508938224b5SJoe Perches
509485ff23eSAlex Dowadour @typeListFile = ();
5108716de38SJoe Perchesour @typeListWithAttr = (
5118716de38SJoe Perches	@typeList,
5128716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5138716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5148716de38SJoe Perches);
5158716de38SJoe Perches
516c45dcabdSAndy Whitcroftour @modifierList = (
517c45dcabdSAndy Whitcroft	qr{fastcall},
518c45dcabdSAndy Whitcroft);
519485ff23eSAlex Dowadour @modifierListFile = ();
5208905a67cSAndy Whitcroft
5212435880fSJoe Perchesour @mode_permission_funcs = (
5222435880fSJoe Perches	["module_param", 3],
5232435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5242435880fSJoe Perches	["module_param_array_named", 5],
5252435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5262435880fSJoe Perches	["proc_create(?:_data|)", 2],
527459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
528459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
529459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
530459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
531459cf0aeSJoe Perches	["__ATTR", 2],
5322435880fSJoe Perches);
5332435880fSJoe Perches
534515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
535515a235eSJoe Perchesour $mode_perms_search = "";
536515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
537515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
538515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
539515a235eSJoe Perches}
540515a235eSJoe Perches
541b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
542b392c64fSJoe Perches	S_IWUGO		|
543b392c64fSJoe Perches	S_IWOTH		|
544b392c64fSJoe Perches	S_IRWXUGO	|
545b392c64fSJoe Perches	S_IALLUGO	|
546b392c64fSJoe Perches	0[0-7][0-7][2367]
547b392c64fSJoe Perches}x;
548b392c64fSJoe Perches
549f90774e1SJoe Perchesour %mode_permission_string_types = (
550f90774e1SJoe Perches	"S_IRWXU" => 0700,
551f90774e1SJoe Perches	"S_IRUSR" => 0400,
552f90774e1SJoe Perches	"S_IWUSR" => 0200,
553f90774e1SJoe Perches	"S_IXUSR" => 0100,
554f90774e1SJoe Perches	"S_IRWXG" => 0070,
555f90774e1SJoe Perches	"S_IRGRP" => 0040,
556f90774e1SJoe Perches	"S_IWGRP" => 0020,
557f90774e1SJoe Perches	"S_IXGRP" => 0010,
558f90774e1SJoe Perches	"S_IRWXO" => 0007,
559f90774e1SJoe Perches	"S_IROTH" => 0004,
560f90774e1SJoe Perches	"S_IWOTH" => 0002,
561f90774e1SJoe Perches	"S_IXOTH" => 0001,
562f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
563f90774e1SJoe Perches	"S_IRUGO" => 0444,
564f90774e1SJoe Perches	"S_IWUGO" => 0222,
565f90774e1SJoe Perches	"S_IXUGO" => 0111,
566f90774e1SJoe Perches);
567f90774e1SJoe Perches
568f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
569f90774e1SJoe Perchesour $mode_perms_string_search = "";
570f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
571f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
572f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
573f90774e1SJoe Perches}
574f90774e1SJoe Perches
5757840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
5767840a94cSWolfram Sang	irq|
577cdcee686SSergey Ryazanov	memory|
578cdcee686SSergey Ryazanov	time|
579cdcee686SSergey Ryazanov	reboot
5807840a94cSWolfram Sang)};
5817840a94cSWolfram Sang# memory.h: ARM has a custom one
5827840a94cSWolfram Sang
58366b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
58466b47b4aSKees Cookmy $misspellings;
58566b47b4aSKees Cookmy %spelling_fix;
58636061e38SJoe Perches
58736061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
58866b47b4aSKees Cook	while (<$spelling>) {
58966b47b4aSKees Cook		my $line = $_;
59066b47b4aSKees Cook
59166b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
59266b47b4aSKees Cook		$line =~ s/^\s*//g;
59366b47b4aSKees Cook
59466b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
59566b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
59666b47b4aSKees Cook
59766b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
59866b47b4aSKees Cook
59966b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
60066b47b4aSKees Cook	}
60166b47b4aSKees Cook	close($spelling);
60236061e38SJoe Perches} else {
60336061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
60436061e38SJoe Perches}
60566b47b4aSKees Cook
606ebfd7d62SJoe Perchesif ($codespell) {
607ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
608ebfd7d62SJoe Perches		while (<$spelling>) {
609ebfd7d62SJoe Perches			my $line = $_;
610ebfd7d62SJoe Perches
611ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
612ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
613ebfd7d62SJoe Perches
614ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
615ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
616ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
617ebfd7d62SJoe Perches
618ebfd7d62SJoe Perches			$line =~ s/,.*$//;
619ebfd7d62SJoe Perches
620ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
621ebfd7d62SJoe Perches
622ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
623ebfd7d62SJoe Perches		}
624ebfd7d62SJoe Perches		close($spelling);
625ebfd7d62SJoe Perches	} else {
626ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
627ebfd7d62SJoe Perches	}
628ebfd7d62SJoe Perches}
629ebfd7d62SJoe Perches
630ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
631ebfd7d62SJoe Perches
632bf1fa1daSJoe Perchesmy $const_structs = "";
633bf1fa1daSJoe Perchesif (open(my $conststructs, '<', $conststructsfile)) {
634bf1fa1daSJoe Perches	while (<$conststructs>) {
635bf1fa1daSJoe Perches		my $line = $_;
636bf1fa1daSJoe Perches
637bf1fa1daSJoe Perches		$line =~ s/\s*\n?$//g;
638bf1fa1daSJoe Perches		$line =~ s/^\s*//g;
639bf1fa1daSJoe Perches
640bf1fa1daSJoe Perches		next if ($line =~ m/^\s*#/);
641bf1fa1daSJoe Perches		next if ($line =~ m/^\s*$/);
642bf1fa1daSJoe Perches		if ($line =~ /\s/) {
643bf1fa1daSJoe Perches			print("$conststructsfile: '$line' invalid - ignored\n");
644bf1fa1daSJoe Perches			next;
645bf1fa1daSJoe Perches		}
646bf1fa1daSJoe Perches
647bf1fa1daSJoe Perches		$const_structs .= '|' if ($const_structs ne "");
648bf1fa1daSJoe Perches		$const_structs .= $line;
649bf1fa1daSJoe Perches	}
650bf1fa1daSJoe Perches	close($conststructsfile);
651bf1fa1daSJoe Perches} else {
652bf1fa1daSJoe Perches	warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
653bf1fa1daSJoe Perches}
654bf1fa1daSJoe Perches
6558905a67cSAndy Whitcroftsub build_types {
656485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
657485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
6581813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
6598716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
660c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
661ab7e23f3SJoe Perches	$BasicType	= qr{
662ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
663ab7e23f3SJoe Perches				(?:${all}\b)
664ab7e23f3SJoe Perches		}x;
6658905a67cSAndy Whitcroft	$NonptrType	= qr{
666d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
667cf655043SAndy Whitcroft			(?:
6686b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
6698ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
670c45dcabdSAndy Whitcroft				(?:${all}\b)
671cf655043SAndy Whitcroft			)
672c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
6738905a67cSAndy Whitcroft		  }x;
6741813087dSJoe Perches	$NonptrTypeMisordered	= qr{
6751813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
6761813087dSJoe Perches			(?:
6771813087dSJoe Perches				(?:${Misordered}\b)
6781813087dSJoe Perches			)
6791813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
6801813087dSJoe Perches		  }x;
6818716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
6828716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
6838716de38SJoe Perches			(?:
6848716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
6858716de38SJoe Perches				(?:$typeTypedefs\b)|
6868716de38SJoe Perches				(?:${allWithAttr}\b)
6878716de38SJoe Perches			)
6888716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
6898716de38SJoe Perches		  }x;
6908905a67cSAndy Whitcroft	$Type	= qr{
691c45dcabdSAndy Whitcroft			$NonptrType
6921574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
693c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
6948905a67cSAndy Whitcroft		  }x;
6951813087dSJoe Perches	$TypeMisordered	= qr{
6961813087dSJoe Perches			$NonptrTypeMisordered
6971813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
6981813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
6991813087dSJoe Perches		  }x;
70091cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
7011813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
7028905a67cSAndy Whitcroft}
7038905a67cSAndy Whitcroftbuild_types();
7046c72ffaaSAndy Whitcroft
7057d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
706d1fe9c09SJoe Perches
707d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
708d1fe9c09SJoe Perches# requires at least perl version v5.10.0
709d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
710d1fe9c09SJoe Perches
711d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
7122435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
713c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
7147d2367afSJoe Perches
715f8422308SJoe Perchesour $declaration_macros = qr{(?x:
7163e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
717f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
718f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
719f8422308SJoe Perches)};
720f8422308SJoe Perches
7217d2367afSJoe Perchessub deparenthesize {
7227d2367afSJoe Perches	my ($string) = @_;
7237d2367afSJoe Perches	return "" if (!defined($string));
7245b9553abSJoe Perches
7255b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
7265b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
7275b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
7285b9553abSJoe Perches	}
7295b9553abSJoe Perches
7307d2367afSJoe Perches	$string =~ s@\s+@ @g;
7315b9553abSJoe Perches
7327d2367afSJoe Perches	return $string;
7337d2367afSJoe Perches}
7347d2367afSJoe Perches
7353445686aSJoe Perchessub seed_camelcase_file {
7363445686aSJoe Perches	my ($file) = @_;
7373445686aSJoe Perches
7383445686aSJoe Perches	return if (!(-f $file));
7393445686aSJoe Perches
7403445686aSJoe Perches	local $/;
7413445686aSJoe Perches
7423445686aSJoe Perches	open(my $include_file, '<', "$file")
7433445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
7443445686aSJoe Perches	my $text = <$include_file>;
7453445686aSJoe Perches	close($include_file);
7463445686aSJoe Perches
7473445686aSJoe Perches	my @lines = split('\n', $text);
7483445686aSJoe Perches
7493445686aSJoe Perches	foreach my $line (@lines) {
7503445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
7513445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
7523445686aSJoe Perches			$camelcase{$1} = 1;
75311ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
75411ea516aSJoe Perches			$camelcase{$1} = 1;
75511ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
7563445686aSJoe Perches			$camelcase{$1} = 1;
7573445686aSJoe Perches		}
7583445686aSJoe Perches	}
7593445686aSJoe Perches}
7603445686aSJoe Perches
76185b0ee18SJoe Perchessub is_maintained_obsolete {
76285b0ee18SJoe Perches	my ($filename) = @_;
76385b0ee18SJoe Perches
764f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
76585b0ee18SJoe Perches
7660616efa4SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
76785b0ee18SJoe Perches
76885b0ee18SJoe Perches	return $status =~ /obsolete/i;
76985b0ee18SJoe Perches}
77085b0ee18SJoe Perches
7713445686aSJoe Perchesmy $camelcase_seeded = 0;
7723445686aSJoe Perchessub seed_camelcase_includes {
7733445686aSJoe Perches	return if ($camelcase_seeded);
7743445686aSJoe Perches
7753445686aSJoe Perches	my $files;
776c707a81dSJoe Perches	my $camelcase_cache = "";
777c707a81dSJoe Perches	my @include_files = ();
778c707a81dSJoe Perches
779c707a81dSJoe Perches	$camelcase_seeded = 1;
780351b2a1fSJoe Perches
7813645e328SRichard Genoud	if (-e ".git") {
782351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
783351b2a1fSJoe Perches		chomp $git_last_include_commit;
784c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
785c707a81dSJoe Perches	} else {
786c707a81dSJoe Perches		my $last_mod_date = 0;
787c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
788c707a81dSJoe Perches		@include_files = split('\n', $files);
789c707a81dSJoe Perches		foreach my $file (@include_files) {
790c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
791c707a81dSJoe Perches						   localtime((stat $file)[9]));
792c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
793c707a81dSJoe Perches		}
794c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
795c707a81dSJoe Perches	}
796c707a81dSJoe Perches
797c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
798c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
799c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
800351b2a1fSJoe Perches		while (<$camelcase_file>) {
801351b2a1fSJoe Perches			chomp;
802351b2a1fSJoe Perches			$camelcase{$_} = 1;
803351b2a1fSJoe Perches		}
804351b2a1fSJoe Perches		close($camelcase_file);
805351b2a1fSJoe Perches
806351b2a1fSJoe Perches		return;
807351b2a1fSJoe Perches	}
808c707a81dSJoe Perches
8093645e328SRichard Genoud	if (-e ".git") {
810c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
811c707a81dSJoe Perches		@include_files = split('\n', $files);
8123445686aSJoe Perches	}
813c707a81dSJoe Perches
8143445686aSJoe Perches	foreach my $file (@include_files) {
8153445686aSJoe Perches		seed_camelcase_file($file);
8163445686aSJoe Perches	}
817351b2a1fSJoe Perches
818c707a81dSJoe Perches	if ($camelcase_cache ne "") {
819351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
820c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
821c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
822351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
823351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
824351b2a1fSJoe Perches		}
825351b2a1fSJoe Perches		close($camelcase_file);
826351b2a1fSJoe Perches	}
8273445686aSJoe Perches}
8283445686aSJoe Perches
829d311cd44SJoe Perchessub git_commit_info {
830d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
831d311cd44SJoe Perches
832d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
833d311cd44SJoe Perches
834d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
835d311cd44SJoe Perches	$output =~ s/^\s*//gm;
836d311cd44SJoe Perches	my @lines = split("\n", $output);
837d311cd44SJoe Perches
8380d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
8390d7835fcSJoe Perches
840d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
841d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
842d311cd44SJoe Perches# all matching commit ids, but it's very slow...
843d311cd44SJoe Perches#
844d311cd44SJoe Perches#		echo "checking commits $1..."
845d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
846d311cd44SJoe Perches#		while read line ; do
847d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
848d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
849d311cd44SJoe Perches#		done
850d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
851d311cd44SJoe Perches	} else {
852d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
853d311cd44SJoe Perches		$desc = substr($lines[0], 41);
854d311cd44SJoe Perches	}
855d311cd44SJoe Perches
856d311cd44SJoe Perches	return ($id, $desc);
857d311cd44SJoe Perches}
858d311cd44SJoe Perches
8596c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
8600a920b5bSAndy Whitcroft
86100df344fSAndy Whitcroftmy @rawlines = ();
862c2fdda0dSAndy Whitcroftmy @lines = ();
8633705ce5bSJoe Perchesmy @fixed = ();
864d752fcc8SJoe Perchesmy @fixed_inserted = ();
865d752fcc8SJoe Perchesmy @fixed_deleted = ();
866194f66fcSJoe Perchesmy $fixlinenr = -1;
867194f66fcSJoe Perches
8684a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
8694a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
8704a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
8714a593c34SDu, Changbin
8724a593c34SDu, Changbinif ($git) {
8734a593c34SDu, Changbin	my @commits = ();
8740dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
8754a593c34SDu, Changbin		my $git_range;
87628898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
87728898fd1SJoe Perches			$git_range = "-$2 $1";
8784a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
8794a593c34SDu, Changbin			$git_range = "$commit_expr";
8804a593c34SDu, Changbin		} else {
8810dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
8820dea9f1eSJoe Perches		}
8830dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
8840dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
88528898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
88628898fd1SJoe Perches			next if (!defined($1) || !defined($2));
8870dea9f1eSJoe Perches			my $sha1 = $1;
8880dea9f1eSJoe Perches			my $subject = $2;
8890dea9f1eSJoe Perches			unshift(@commits, $sha1);
8900dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
8914a593c34SDu, Changbin		}
8924a593c34SDu, Changbin	}
8934a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
8944a593c34SDu, Changbin	@ARGV = @commits;
8954a593c34SDu, Changbin}
8964a593c34SDu, Changbin
897c2fdda0dSAndy Whitcroftmy $vname;
8986c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
89921caa13cSAndy Whitcroft	my $FILE;
9004a593c34SDu, Changbin	if ($git) {
9014a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
9024a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
9034a593c34SDu, Changbin	} elsif ($file) {
90421caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
9056c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
90621caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
90721caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
9086c72ffaaSAndy Whitcroft	} else {
90921caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
9106c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
9116c72ffaaSAndy Whitcroft	}
912c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
913c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
9144a593c34SDu, Changbin	} elsif ($git) {
9150dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
916c2fdda0dSAndy Whitcroft	} else {
917c2fdda0dSAndy Whitcroft		$vname = $filename;
918c2fdda0dSAndy Whitcroft	}
91921caa13cSAndy Whitcroft	while (<$FILE>) {
9200a920b5bSAndy Whitcroft		chomp;
92100df344fSAndy Whitcroft		push(@rawlines, $_);
9226c72ffaaSAndy Whitcroft	}
92321caa13cSAndy Whitcroft	close($FILE);
924d8469f16SJoe Perches
925d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
926d8469f16SJoe Perches		print '-' x length($vname) . "\n";
927d8469f16SJoe Perches		print "$vname\n";
928d8469f16SJoe Perches		print '-' x length($vname) . "\n";
929d8469f16SJoe Perches	}
930d8469f16SJoe Perches
931c2fdda0dSAndy Whitcroft	if (!process($filename)) {
9320a920b5bSAndy Whitcroft		$exit = 1;
9330a920b5bSAndy Whitcroft	}
93400df344fSAndy Whitcroft	@rawlines = ();
93513214adfSAndy Whitcroft	@lines = ();
9363705ce5bSJoe Perches	@fixed = ();
937d752fcc8SJoe Perches	@fixed_inserted = ();
938d752fcc8SJoe Perches	@fixed_deleted = ();
939194f66fcSJoe Perches	$fixlinenr = -1;
940485ff23eSAlex Dowad	@modifierListFile = ();
941485ff23eSAlex Dowad	@typeListFile = ();
942485ff23eSAlex Dowad	build_types();
9430a920b5bSAndy Whitcroft}
9440a920b5bSAndy Whitcroft
945d8469f16SJoe Perchesif (!$quiet) {
9463c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
9473c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
9483c816e49SJoe Perches
949d8469f16SJoe Perches	if ($^V lt 5.10.0) {
950d8469f16SJoe Perches		print << "EOM"
951d8469f16SJoe Perches
952d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
953d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
954d8469f16SJoe PerchesEOM
955d8469f16SJoe Perches	}
956d8469f16SJoe Perches	if ($exit) {
957d8469f16SJoe Perches		print << "EOM"
958d8469f16SJoe Perches
959d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
960d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
961d8469f16SJoe PerchesEOM
962d8469f16SJoe Perches	}
963d8469f16SJoe Perches}
964d8469f16SJoe Perches
9650a920b5bSAndy Whitcroftexit($exit);
9660a920b5bSAndy Whitcroft
9670a920b5bSAndy Whitcroftsub top_of_kernel_tree {
9686c72ffaaSAndy Whitcroft	my ($root) = @_;
9696c72ffaaSAndy Whitcroft
9706c72ffaaSAndy Whitcroft	my @tree_check = (
9716c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
9726c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
9736c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
9746c72ffaaSAndy Whitcroft	);
9756c72ffaaSAndy Whitcroft
9766c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
9776c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
9780a920b5bSAndy Whitcroft			return 0;
9790a920b5bSAndy Whitcroft		}
9806c72ffaaSAndy Whitcroft	}
9816c72ffaaSAndy Whitcroft	return 1;
9826c72ffaaSAndy Whitcroft}
9830a920b5bSAndy Whitcroft
98420112475SJoe Perchessub parse_email {
98520112475SJoe Perches	my ($formatted_email) = @_;
98620112475SJoe Perches
98720112475SJoe Perches	my $name = "";
98820112475SJoe Perches	my $address = "";
98920112475SJoe Perches	my $comment = "";
99020112475SJoe Perches
99120112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
99220112475SJoe Perches		$name = $1;
99320112475SJoe Perches		$address = $2;
99420112475SJoe Perches		$comment = $3 if defined $3;
99520112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
99620112475SJoe Perches		$address = $1;
99720112475SJoe Perches		$comment = $2 if defined $2;
99820112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
99920112475SJoe Perches		$address = $1;
100020112475SJoe Perches		$comment = $2 if defined $2;
100120112475SJoe Perches		$formatted_email =~ s/$address.*$//;
100220112475SJoe Perches		$name = $formatted_email;
10033705ce5bSJoe Perches		$name = trim($name);
100420112475SJoe Perches		$name =~ s/^\"|\"$//g;
100520112475SJoe Perches		# If there's a name left after stripping spaces and
100620112475SJoe Perches		# leading quotes, and the address doesn't have both
100720112475SJoe Perches		# leading and trailing angle brackets, the address
100820112475SJoe Perches		# is invalid. ie:
100920112475SJoe Perches		#   "joe smith [email protected]" bad
101020112475SJoe Perches		#   "joe smith <[email protected]" bad
101120112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
101220112475SJoe Perches			$name = "";
101320112475SJoe Perches			$address = "";
101420112475SJoe Perches			$comment = "";
101520112475SJoe Perches		}
101620112475SJoe Perches	}
101720112475SJoe Perches
10183705ce5bSJoe Perches	$name = trim($name);
101920112475SJoe Perches	$name =~ s/^\"|\"$//g;
10203705ce5bSJoe Perches	$address = trim($address);
102120112475SJoe Perches	$address =~ s/^\<|\>$//g;
102220112475SJoe Perches
102320112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
102420112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
102520112475SJoe Perches		$name = "\"$name\"";
102620112475SJoe Perches	}
102720112475SJoe Perches
102820112475SJoe Perches	return ($name, $address, $comment);
102920112475SJoe Perches}
103020112475SJoe Perches
103120112475SJoe Perchessub format_email {
103220112475SJoe Perches	my ($name, $address) = @_;
103320112475SJoe Perches
103420112475SJoe Perches	my $formatted_email;
103520112475SJoe Perches
10363705ce5bSJoe Perches	$name = trim($name);
103720112475SJoe Perches	$name =~ s/^\"|\"$//g;
10383705ce5bSJoe Perches	$address = trim($address);
103920112475SJoe Perches
104020112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
104120112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
104220112475SJoe Perches		$name = "\"$name\"";
104320112475SJoe Perches	}
104420112475SJoe Perches
104520112475SJoe Perches	if ("$name" eq "") {
104620112475SJoe Perches		$formatted_email = "$address";
104720112475SJoe Perches	} else {
104820112475SJoe Perches		$formatted_email = "$name <$address>";
104920112475SJoe Perches	}
105020112475SJoe Perches
105120112475SJoe Perches	return $formatted_email;
105220112475SJoe Perches}
105320112475SJoe Perches
1054d311cd44SJoe Perchessub which {
1055d311cd44SJoe Perches	my ($bin) = @_;
1056d311cd44SJoe Perches
1057d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1058d311cd44SJoe Perches		if (-e "$path/$bin") {
1059d311cd44SJoe Perches			return "$path/$bin";
1060d311cd44SJoe Perches		}
1061d311cd44SJoe Perches	}
1062d311cd44SJoe Perches
1063d311cd44SJoe Perches	return "";
1064d311cd44SJoe Perches}
1065d311cd44SJoe Perches
1066000d1cc1SJoe Perchessub which_conf {
1067000d1cc1SJoe Perches	my ($conf) = @_;
1068000d1cc1SJoe Perches
1069000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1070000d1cc1SJoe Perches		if (-e "$path/$conf") {
1071000d1cc1SJoe Perches			return "$path/$conf";
1072000d1cc1SJoe Perches		}
1073000d1cc1SJoe Perches	}
1074000d1cc1SJoe Perches
1075000d1cc1SJoe Perches	return "";
1076000d1cc1SJoe Perches}
1077000d1cc1SJoe Perches
10780a920b5bSAndy Whitcroftsub expand_tabs {
10790a920b5bSAndy Whitcroft	my ($str) = @_;
10800a920b5bSAndy Whitcroft
10810a920b5bSAndy Whitcroft	my $res = '';
10820a920b5bSAndy Whitcroft	my $n = 0;
10830a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
10840a920b5bSAndy Whitcroft		if ($c eq "\t") {
10850a920b5bSAndy Whitcroft			$res .= ' ';
10860a920b5bSAndy Whitcroft			$n++;
10870a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
10880a920b5bSAndy Whitcroft				$res .= ' ';
10890a920b5bSAndy Whitcroft			}
10900a920b5bSAndy Whitcroft			next;
10910a920b5bSAndy Whitcroft		}
10920a920b5bSAndy Whitcroft		$res .= $c;
10930a920b5bSAndy Whitcroft		$n++;
10940a920b5bSAndy Whitcroft	}
10950a920b5bSAndy Whitcroft
10960a920b5bSAndy Whitcroft	return $res;
10970a920b5bSAndy Whitcroft}
10986c72ffaaSAndy Whitcroftsub copy_spacing {
1099773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
11006c72ffaaSAndy Whitcroft	return $res;
11016c72ffaaSAndy Whitcroft}
11020a920b5bSAndy Whitcroft
11034a0df2efSAndy Whitcroftsub line_stats {
11044a0df2efSAndy Whitcroft	my ($line) = @_;
11054a0df2efSAndy Whitcroft
11064a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
11074a0df2efSAndy Whitcroft	$line =~ s/^.//;
11084a0df2efSAndy Whitcroft	$line = expand_tabs($line);
11094a0df2efSAndy Whitcroft
11104a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
11114a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
11124a0df2efSAndy Whitcroft
11134a0df2efSAndy Whitcroft	return (length($line), length($white));
11144a0df2efSAndy Whitcroft}
11154a0df2efSAndy Whitcroft
1116773647a0SAndy Whitcroftmy $sanitise_quote = '';
1117773647a0SAndy Whitcroft
1118773647a0SAndy Whitcroftsub sanitise_line_reset {
1119773647a0SAndy Whitcroft	my ($in_comment) = @_;
1120773647a0SAndy Whitcroft
1121773647a0SAndy Whitcroft	if ($in_comment) {
1122773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1123773647a0SAndy Whitcroft	} else {
1124773647a0SAndy Whitcroft		$sanitise_quote = '';
1125773647a0SAndy Whitcroft	}
1126773647a0SAndy Whitcroft}
112700df344fSAndy Whitcroftsub sanitise_line {
112800df344fSAndy Whitcroft	my ($line) = @_;
112900df344fSAndy Whitcroft
113000df344fSAndy Whitcroft	my $res = '';
113100df344fSAndy Whitcroft	my $l = '';
113200df344fSAndy Whitcroft
1133c2fdda0dSAndy Whitcroft	my $qlen = 0;
1134773647a0SAndy Whitcroft	my $off = 0;
1135773647a0SAndy Whitcroft	my $c;
113600df344fSAndy Whitcroft
1137773647a0SAndy Whitcroft	# Always copy over the diff marker.
1138773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1139773647a0SAndy Whitcroft
1140773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1141773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1142773647a0SAndy Whitcroft
1143773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
1144773647a0SAndy Whitcroft		# and end, all to $;.
1145773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1146773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1147773647a0SAndy Whitcroft
1148773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1149773647a0SAndy Whitcroft			$off++;
115000df344fSAndy Whitcroft			next;
1151773647a0SAndy Whitcroft		}
115281bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1153773647a0SAndy Whitcroft			$sanitise_quote = '';
1154773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1155773647a0SAndy Whitcroft			$off++;
1156773647a0SAndy Whitcroft			next;
1157773647a0SAndy Whitcroft		}
1158113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1159113f04a8SDaniel Walker			$sanitise_quote = '//';
1160113f04a8SDaniel Walker
1161113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1162113f04a8SDaniel Walker			$off++;
1163113f04a8SDaniel Walker			next;
1164113f04a8SDaniel Walker		}
1165773647a0SAndy Whitcroft
1166773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1167773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1168773647a0SAndy Whitcroft		    $c eq "\\") {
1169773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1170773647a0SAndy Whitcroft			$off++;
1171773647a0SAndy Whitcroft			next;
1172773647a0SAndy Whitcroft		}
1173773647a0SAndy Whitcroft		# Regular quotes.
1174773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1175773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1176773647a0SAndy Whitcroft				$sanitise_quote = $c;
1177773647a0SAndy Whitcroft
1178773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1179773647a0SAndy Whitcroft				next;
1180773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1181773647a0SAndy Whitcroft				$sanitise_quote = '';
118200df344fSAndy Whitcroft			}
118300df344fSAndy Whitcroft		}
1184773647a0SAndy Whitcroft
1185fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1186773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1187773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1188113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1189113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1190773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1191773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
119200df344fSAndy Whitcroft		} else {
1193773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
119400df344fSAndy Whitcroft		}
1195c2fdda0dSAndy Whitcroft	}
1196c2fdda0dSAndy Whitcroft
1197113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1198113f04a8SDaniel Walker		$sanitise_quote = '';
1199113f04a8SDaniel Walker	}
1200113f04a8SDaniel Walker
1201c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1202c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1203c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1204c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1205c2fdda0dSAndy Whitcroft
1206c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1207c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1208c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1209c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1210c2fdda0dSAndy Whitcroft	}
1211c2fdda0dSAndy Whitcroft
1212dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1213dadf680dSJoe Perches		my $match = $1;
1214dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1215dadf680dSJoe Perches	}
1216dadf680dSJoe Perches
121700df344fSAndy Whitcroft	return $res;
121800df344fSAndy Whitcroft}
121900df344fSAndy Whitcroft
1220a6962d72SJoe Perchessub get_quoted_string {
1221a6962d72SJoe Perches	my ($line, $rawline) = @_;
1222a6962d72SJoe Perches
122333acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1224a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1225a6962d72SJoe Perches}
1226a6962d72SJoe Perches
12278905a67cSAndy Whitcroftsub ctx_statement_block {
12288905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12298905a67cSAndy Whitcroft	my $line = $linenr - 1;
12308905a67cSAndy Whitcroft	my $blk = '';
12318905a67cSAndy Whitcroft	my $soff = $off;
12328905a67cSAndy Whitcroft	my $coff = $off - 1;
1233773647a0SAndy Whitcroft	my $coff_set = 0;
12348905a67cSAndy Whitcroft
123513214adfSAndy Whitcroft	my $loff = 0;
123613214adfSAndy Whitcroft
12378905a67cSAndy Whitcroft	my $type = '';
12388905a67cSAndy Whitcroft	my $level = 0;
1239a2750645SAndy Whitcroft	my @stack = ();
1240cf655043SAndy Whitcroft	my $p;
12418905a67cSAndy Whitcroft	my $c;
12428905a67cSAndy Whitcroft	my $len = 0;
124313214adfSAndy Whitcroft
124413214adfSAndy Whitcroft	my $remainder;
12458905a67cSAndy Whitcroft	while (1) {
1246a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1247a2750645SAndy Whitcroft
1248773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
12498905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
12508905a67cSAndy Whitcroft		# context.
12518905a67cSAndy Whitcroft		if ($off >= $len) {
12528905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1253dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1254c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
12558905a67cSAndy Whitcroft				$remain--;
125613214adfSAndy Whitcroft				$loff = $len;
1257c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
12588905a67cSAndy Whitcroft				$len = length($blk);
12598905a67cSAndy Whitcroft				$line++;
12608905a67cSAndy Whitcroft				last;
12618905a67cSAndy Whitcroft			}
12628905a67cSAndy Whitcroft			# Bail if there is no further context.
12638905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
126413214adfSAndy Whitcroft			if ($off >= $len) {
12658905a67cSAndy Whitcroft				last;
12668905a67cSAndy Whitcroft			}
1267f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1268f74bd194SAndy Whitcroft				$level++;
1269f74bd194SAndy Whitcroft				$type = '#';
1270f74bd194SAndy Whitcroft			}
12718905a67cSAndy Whitcroft		}
1272cf655043SAndy Whitcroft		$p = $c;
12738905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
127413214adfSAndy Whitcroft		$remainder = substr($blk, $off);
12758905a67cSAndy Whitcroft
1276773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
12774635f4fbSAndy Whitcroft
12784635f4fbSAndy Whitcroft		# Handle nested #if/#else.
12794635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
12804635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
12814635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
12824635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
12834635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
12844635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
12854635f4fbSAndy Whitcroft		}
12864635f4fbSAndy Whitcroft
12878905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
12888905a67cSAndy Whitcroft		# outermost level.
12898905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
12908905a67cSAndy Whitcroft			last;
12918905a67cSAndy Whitcroft		}
12928905a67cSAndy Whitcroft
129313214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1294773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1295773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1296773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1297773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1298773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1299773647a0SAndy Whitcroft			$coff_set = 1;
1300773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1301773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
130213214adfSAndy Whitcroft		}
130313214adfSAndy Whitcroft
13048905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
13058905a67cSAndy Whitcroft			$level++;
13068905a67cSAndy Whitcroft			$type = '(';
13078905a67cSAndy Whitcroft		}
13088905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
13098905a67cSAndy Whitcroft			$level--;
13108905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
13118905a67cSAndy Whitcroft
13128905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
13138905a67cSAndy Whitcroft				$coff = $off;
1314773647a0SAndy Whitcroft				$coff_set = 1;
1315773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
13168905a67cSAndy Whitcroft			}
13178905a67cSAndy Whitcroft		}
13188905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
13198905a67cSAndy Whitcroft			$level++;
13208905a67cSAndy Whitcroft			$type = '{';
13218905a67cSAndy Whitcroft		}
13228905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
13238905a67cSAndy Whitcroft			$level--;
13248905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
13258905a67cSAndy Whitcroft
13268905a67cSAndy Whitcroft			if ($level == 0) {
1327b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1328b998e001SPatrick Pannuto					$off++;
1329b998e001SPatrick Pannuto				}
13308905a67cSAndy Whitcroft				last;
13318905a67cSAndy Whitcroft			}
13328905a67cSAndy Whitcroft		}
1333f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1334f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1335f74bd194SAndy Whitcroft			$level--;
1336f74bd194SAndy Whitcroft			$type = '';
1337f74bd194SAndy Whitcroft			$off++;
1338f74bd194SAndy Whitcroft			last;
1339f74bd194SAndy Whitcroft		}
13408905a67cSAndy Whitcroft		$off++;
13418905a67cSAndy Whitcroft	}
1342a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
134313214adfSAndy Whitcroft	if ($off == $len) {
1344a3bb97a7SAndy Whitcroft		$loff = $len + 1;
134513214adfSAndy Whitcroft		$line++;
134613214adfSAndy Whitcroft		$remain--;
134713214adfSAndy Whitcroft	}
13488905a67cSAndy Whitcroft
13498905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
13508905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
13518905a67cSAndy Whitcroft
13528905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
13538905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
13548905a67cSAndy Whitcroft
1355773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
135613214adfSAndy Whitcroft
135713214adfSAndy Whitcroft	return ($statement, $condition,
135813214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
135913214adfSAndy Whitcroft}
136013214adfSAndy Whitcroft
1361cf655043SAndy Whitcroftsub statement_lines {
1362cf655043SAndy Whitcroft	my ($stmt) = @_;
1363cf655043SAndy Whitcroft
1364cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1365cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1366cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1367cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1368cf655043SAndy Whitcroft
1369cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1370cf655043SAndy Whitcroft
1371cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1372cf655043SAndy Whitcroft}
1373cf655043SAndy Whitcroft
1374cf655043SAndy Whitcroftsub statement_rawlines {
1375cf655043SAndy Whitcroft	my ($stmt) = @_;
1376cf655043SAndy Whitcroft
1377cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1378cf655043SAndy Whitcroft
1379cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1380cf655043SAndy Whitcroft}
1381cf655043SAndy Whitcroft
1382cf655043SAndy Whitcroftsub statement_block_size {
1383cf655043SAndy Whitcroft	my ($stmt) = @_;
1384cf655043SAndy Whitcroft
1385cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1386cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1387cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1388cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1389cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1390cf655043SAndy Whitcroft
1391cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1392cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1393cf655043SAndy Whitcroft
1394cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1395cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1396cf655043SAndy Whitcroft
1397cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1398cf655043SAndy Whitcroft		return $stmt_lines;
1399cf655043SAndy Whitcroft	} else {
1400cf655043SAndy Whitcroft		return $stmt_statements;
1401cf655043SAndy Whitcroft	}
1402cf655043SAndy Whitcroft}
1403cf655043SAndy Whitcroft
140413214adfSAndy Whitcroftsub ctx_statement_full {
140513214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
140613214adfSAndy Whitcroft	my ($statement, $condition, $level);
140713214adfSAndy Whitcroft
140813214adfSAndy Whitcroft	my (@chunks);
140913214adfSAndy Whitcroft
1410cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
141113214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
141213214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1413773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
141413214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1415cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1416cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1417cf655043SAndy Whitcroft	}
1418cf655043SAndy Whitcroft
1419cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1420cf655043SAndy Whitcroft	# could continue the statement.
1421cf655043SAndy Whitcroft	for (;;) {
142213214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
142313214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1424cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1425773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1426cf655043SAndy Whitcroft		#print "C: push\n";
1427cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
142813214adfSAndy Whitcroft	}
142913214adfSAndy Whitcroft
143013214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
14318905a67cSAndy Whitcroft}
14328905a67cSAndy Whitcroft
14334a0df2efSAndy Whitcroftsub ctx_block_get {
1434f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
14354a0df2efSAndy Whitcroft	my $line;
14364a0df2efSAndy Whitcroft	my $start = $linenr - 1;
14374a0df2efSAndy Whitcroft	my $blk = '';
14384a0df2efSAndy Whitcroft	my @o;
14394a0df2efSAndy Whitcroft	my @c;
14404a0df2efSAndy Whitcroft	my @res = ();
14414a0df2efSAndy Whitcroft
1442f0a594c1SAndy Whitcroft	my $level = 0;
14434635f4fbSAndy Whitcroft	my @stack = ($level);
144400df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
144500df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
144600df344fSAndy Whitcroft		$remain--;
144700df344fSAndy Whitcroft
144800df344fSAndy Whitcroft		$blk .= $rawlines[$line];
14494635f4fbSAndy Whitcroft
14504635f4fbSAndy Whitcroft		# Handle nested #if/#else.
145101464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
14524635f4fbSAndy Whitcroft			push(@stack, $level);
145301464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
14544635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
145501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
14564635f4fbSAndy Whitcroft			$level = pop(@stack);
14574635f4fbSAndy Whitcroft		}
14584635f4fbSAndy Whitcroft
145901464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1460f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1461f0a594c1SAndy Whitcroft			if ($off > 0) {
1462f0a594c1SAndy Whitcroft				$off--;
1463f0a594c1SAndy Whitcroft				next;
1464f0a594c1SAndy Whitcroft			}
14654a0df2efSAndy Whitcroft
1466f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1467f0a594c1SAndy Whitcroft				$level--;
1468f0a594c1SAndy Whitcroft				last if ($level == 0);
1469f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1470f0a594c1SAndy Whitcroft				$level++;
1471f0a594c1SAndy Whitcroft			}
1472f0a594c1SAndy Whitcroft		}
14734a0df2efSAndy Whitcroft
1474f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
147500df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
14764a0df2efSAndy Whitcroft		}
14774a0df2efSAndy Whitcroft
1478f0a594c1SAndy Whitcroft		last if ($level == 0);
14794a0df2efSAndy Whitcroft	}
14804a0df2efSAndy Whitcroft
1481f0a594c1SAndy Whitcroft	return ($level, @res);
14824a0df2efSAndy Whitcroft}
14834a0df2efSAndy Whitcroftsub ctx_block_outer {
14844a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
14854a0df2efSAndy Whitcroft
1486f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1487f0a594c1SAndy Whitcroft	return @r;
14884a0df2efSAndy Whitcroft}
14894a0df2efSAndy Whitcroftsub ctx_block {
14904a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
14914a0df2efSAndy Whitcroft
1492f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1493f0a594c1SAndy Whitcroft	return @r;
1494653d4876SAndy Whitcroft}
1495653d4876SAndy Whitcroftsub ctx_statement {
1496f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1497f0a594c1SAndy Whitcroft
1498f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1499f0a594c1SAndy Whitcroft	return @r;
1500f0a594c1SAndy Whitcroft}
1501f0a594c1SAndy Whitcroftsub ctx_block_level {
1502653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1503653d4876SAndy Whitcroft
1504f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
15054a0df2efSAndy Whitcroft}
15069c0ca6f9SAndy Whitcroftsub ctx_statement_level {
15079c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
15089c0ca6f9SAndy Whitcroft
15099c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
15109c0ca6f9SAndy Whitcroft}
15114a0df2efSAndy Whitcroft
15124a0df2efSAndy Whitcroftsub ctx_locate_comment {
15134a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15144a0df2efSAndy Whitcroft
15154a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1516beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
15174a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
15184a0df2efSAndy Whitcroft
15194a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
15204a0df2efSAndy Whitcroft	# comment.
15214a0df2efSAndy Whitcroft	my $in_comment = 0;
15224a0df2efSAndy Whitcroft	$current_comment = '';
15234a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
152400df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
152500df344fSAndy Whitcroft		#warn "           $line\n";
15264a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
15274a0df2efSAndy Whitcroft			$in_comment = 1;
15284a0df2efSAndy Whitcroft		}
15294a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
15304a0df2efSAndy Whitcroft			$in_comment = 1;
15314a0df2efSAndy Whitcroft		}
15324a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
15334a0df2efSAndy Whitcroft			$current_comment = '';
15344a0df2efSAndy Whitcroft		}
15354a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
15364a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
15374a0df2efSAndy Whitcroft			$in_comment = 0;
15384a0df2efSAndy Whitcroft		}
15394a0df2efSAndy Whitcroft	}
15404a0df2efSAndy Whitcroft
15414a0df2efSAndy Whitcroft	chomp($current_comment);
15424a0df2efSAndy Whitcroft	return($current_comment);
15434a0df2efSAndy Whitcroft}
15444a0df2efSAndy Whitcroftsub ctx_has_comment {
15454a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15464a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
15474a0df2efSAndy Whitcroft
154800df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
15494a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
15504a0df2efSAndy Whitcroft
15514a0df2efSAndy Whitcroft	return ($cmt ne '');
15524a0df2efSAndy Whitcroft}
15534a0df2efSAndy Whitcroft
15544d001e4dSAndy Whitcroftsub raw_line {
15554d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
15564d001e4dSAndy Whitcroft
15574d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
15584d001e4dSAndy Whitcroft	$cnt++;
15594d001e4dSAndy Whitcroft
15604d001e4dSAndy Whitcroft	my $line;
15614d001e4dSAndy Whitcroft	while ($cnt) {
15624d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
15634d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
15644d001e4dSAndy Whitcroft		$cnt--;
15654d001e4dSAndy Whitcroft	}
15664d001e4dSAndy Whitcroft
15674d001e4dSAndy Whitcroft	return $line;
15684d001e4dSAndy Whitcroft}
15694d001e4dSAndy Whitcroft
15700a920b5bSAndy Whitcroftsub cat_vet {
15710a920b5bSAndy Whitcroft	my ($vet) = @_;
15729c0ca6f9SAndy Whitcroft	my ($res, $coded);
15730a920b5bSAndy Whitcroft
15749c0ca6f9SAndy Whitcroft	$res = '';
15756c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
15766c72ffaaSAndy Whitcroft		$res .= $1;
15776c72ffaaSAndy Whitcroft		if ($2 ne '') {
15789c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
15796c72ffaaSAndy Whitcroft			$res .= $coded;
15806c72ffaaSAndy Whitcroft		}
15819c0ca6f9SAndy Whitcroft	}
15829c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
15830a920b5bSAndy Whitcroft
15849c0ca6f9SAndy Whitcroft	return $res;
15850a920b5bSAndy Whitcroft}
15860a920b5bSAndy Whitcroft
1587c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1588cf655043SAndy Whitcroftmy $av_pending;
1589c2fdda0dSAndy Whitcroftmy @av_paren_type;
15901f65f947SAndy Whitcroftmy $av_pend_colon;
1591c2fdda0dSAndy Whitcroft
1592c2fdda0dSAndy Whitcroftsub annotate_reset {
1593c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1594cf655043SAndy Whitcroft	$av_pending = '_';
1595cf655043SAndy Whitcroft	@av_paren_type = ('E');
15961f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1597c2fdda0dSAndy Whitcroft}
1598c2fdda0dSAndy Whitcroft
15996c72ffaaSAndy Whitcroftsub annotate_values {
16006c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
16016c72ffaaSAndy Whitcroft
16026c72ffaaSAndy Whitcroft	my $res;
16031f65f947SAndy Whitcroft	my $var = '_' x length($stream);
16046c72ffaaSAndy Whitcroft	my $cur = $stream;
16056c72ffaaSAndy Whitcroft
1606c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
16076c72ffaaSAndy Whitcroft
16086c72ffaaSAndy Whitcroft	while (length($cur)) {
1609773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1610cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1611171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
16126c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1613c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1614c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1615cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1616c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
16176c72ffaaSAndy Whitcroft			}
16186c72ffaaSAndy Whitcroft
1619c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
16209446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
16219446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1622addcdceaSAndy Whitcroft			$type = 'c';
16239446ef56SAndy Whitcroft
1624e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1625c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
16266c72ffaaSAndy Whitcroft			$type = 'T';
16276c72ffaaSAndy Whitcroft
1628389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1629389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1630389a2fe5SAndy Whitcroft			$type = 'T';
1631389a2fe5SAndy Whitcroft
1632c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1633171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1634c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1635171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1636171ae1a4SAndy Whitcroft			if ($2 ne '') {
1637cf655043SAndy Whitcroft				$av_pending = 'N';
1638171ae1a4SAndy Whitcroft			}
1639171ae1a4SAndy Whitcroft			$type = 'E';
1640171ae1a4SAndy Whitcroft
1641c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1642171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1643171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1644171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
16456c72ffaaSAndy Whitcroft
1646c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1647cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1648c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1649cf655043SAndy Whitcroft
1650cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1651cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1652171ae1a4SAndy Whitcroft			$type = 'E';
1653cf655043SAndy Whitcroft
1654c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1655cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1656cf655043SAndy Whitcroft			$av_preprocessor = 1;
1657cf655043SAndy Whitcroft
1658cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1659cf655043SAndy Whitcroft
1660171ae1a4SAndy Whitcroft			$type = 'E';
1661cf655043SAndy Whitcroft
1662c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1663cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1664cf655043SAndy Whitcroft
1665cf655043SAndy Whitcroft			$av_preprocessor = 1;
1666cf655043SAndy Whitcroft
1667cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1668cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1669cf655043SAndy Whitcroft			pop(@av_paren_type);
1670cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1671171ae1a4SAndy Whitcroft			$type = 'E';
16726c72ffaaSAndy Whitcroft
16736c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1674c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
16756c72ffaaSAndy Whitcroft
1676171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1677171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1678171ae1a4SAndy Whitcroft			$av_pending = $type;
1679171ae1a4SAndy Whitcroft			$type = 'N';
1680171ae1a4SAndy Whitcroft
16816c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1682c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
16836c72ffaaSAndy Whitcroft			if (defined $2) {
1684cf655043SAndy Whitcroft				$av_pending = 'V';
16856c72ffaaSAndy Whitcroft			}
16866c72ffaaSAndy Whitcroft			$type = 'N';
16876c72ffaaSAndy Whitcroft
168814b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1689c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
169014b111c1SAndy Whitcroft			$av_pending = 'E';
16916c72ffaaSAndy Whitcroft			$type = 'N';
16926c72ffaaSAndy Whitcroft
16931f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
16941f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
16951f65f947SAndy Whitcroft			$av_pend_colon = 'C';
16961f65f947SAndy Whitcroft			$type = 'N';
16971f65f947SAndy Whitcroft
169814b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1699c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
17006c72ffaaSAndy Whitcroft			$type = 'N';
17016c72ffaaSAndy Whitcroft
17026c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1703c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1704cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1705cf655043SAndy Whitcroft			$av_pending = '_';
17066c72ffaaSAndy Whitcroft			$type = 'N';
17076c72ffaaSAndy Whitcroft
17086c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1709cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1710cf655043SAndy Whitcroft			if ($new_type ne '_') {
1711cf655043SAndy Whitcroft				$type = $new_type;
1712c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1713c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
17146c72ffaaSAndy Whitcroft			} else {
1715c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
17166c72ffaaSAndy Whitcroft			}
17176c72ffaaSAndy Whitcroft
1718c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1719c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1720c8cb2ca3SAndy Whitcroft			$type = 'V';
1721cf655043SAndy Whitcroft			$av_pending = 'V';
17226c72ffaaSAndy Whitcroft
17238e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
17248e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
17251f65f947SAndy Whitcroft				$av_pend_colon = 'B';
17268e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
17278e761b04SAndy Whitcroft				$av_pend_colon = 'L';
17281f65f947SAndy Whitcroft			}
17291f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
17301f65f947SAndy Whitcroft			$type = 'V';
17311f65f947SAndy Whitcroft
17326c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1733c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
17346c72ffaaSAndy Whitcroft			$type = 'V';
17356c72ffaaSAndy Whitcroft
17366c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1737c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
17386c72ffaaSAndy Whitcroft			$type = 'N';
17396c72ffaaSAndy Whitcroft
1740cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1741c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
174213214adfSAndy Whitcroft			$type = 'E';
17431f65f947SAndy Whitcroft			$av_pend_colon = 'O';
174413214adfSAndy Whitcroft
17458e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
17468e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
17478e761b04SAndy Whitcroft			$type = 'C';
17488e761b04SAndy Whitcroft
17491f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
17501f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
17511f65f947SAndy Whitcroft			$type = 'N';
17521f65f947SAndy Whitcroft
17531f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
17541f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
17551f65f947SAndy Whitcroft
17561f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
17571f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
17581f65f947SAndy Whitcroft				$type = 'E';
17591f65f947SAndy Whitcroft			} else {
17601f65f947SAndy Whitcroft				$type = 'N';
17611f65f947SAndy Whitcroft			}
17621f65f947SAndy Whitcroft			$av_pend_colon = 'O';
17631f65f947SAndy Whitcroft
17648e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
176513214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
17666c72ffaaSAndy Whitcroft			$type = 'N';
17676c72ffaaSAndy Whitcroft
17680d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
176974048ed8SAndy Whitcroft			my $variant;
177074048ed8SAndy Whitcroft
177174048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
177274048ed8SAndy Whitcroft			if ($type eq 'V') {
177374048ed8SAndy Whitcroft				$variant = 'B';
177474048ed8SAndy Whitcroft			} else {
177574048ed8SAndy Whitcroft				$variant = 'U';
177674048ed8SAndy Whitcroft			}
177774048ed8SAndy Whitcroft
177874048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
177974048ed8SAndy Whitcroft			$type = 'N';
178074048ed8SAndy Whitcroft
17816c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1782c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
17836c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
17846c72ffaaSAndy Whitcroft				$type = 'N';
17856c72ffaaSAndy Whitcroft			}
17866c72ffaaSAndy Whitcroft
17876c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1788c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
17896c72ffaaSAndy Whitcroft		}
17906c72ffaaSAndy Whitcroft		if (defined $1) {
17916c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
17926c72ffaaSAndy Whitcroft			$res .= $type x length($1);
17936c72ffaaSAndy Whitcroft		}
17946c72ffaaSAndy Whitcroft	}
17956c72ffaaSAndy Whitcroft
17961f65f947SAndy Whitcroft	return ($res, $var);
17976c72ffaaSAndy Whitcroft}
17986c72ffaaSAndy Whitcroft
17998905a67cSAndy Whitcroftsub possible {
180013214adfSAndy Whitcroft	my ($possible, $line) = @_;
18019a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
18020776e594SAndy Whitcroft		^(?:
18030776e594SAndy Whitcroft			$Modifier|
18040776e594SAndy Whitcroft			$Storage|
18050776e594SAndy Whitcroft			$Type|
18069a974fdbSAndy Whitcroft			DEFINE_\S+
18079a974fdbSAndy Whitcroft		)$|
18089a974fdbSAndy Whitcroft		^(?:
18090776e594SAndy Whitcroft			goto|
18100776e594SAndy Whitcroft			return|
18110776e594SAndy Whitcroft			case|
18120776e594SAndy Whitcroft			else|
18130776e594SAndy Whitcroft			asm|__asm__|
181489a88353SAndy Whitcroft			do|
181589a88353SAndy Whitcroft			\#|
181689a88353SAndy Whitcroft			\#\#|
18179a974fdbSAndy Whitcroft		)(?:\s|$)|
18180776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
18199a974fdbSAndy Whitcroft	    )}x;
18209a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
18219a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1822c45dcabdSAndy Whitcroft		# Check for modifiers.
1823c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1824c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1825c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1826c45dcabdSAndy Whitcroft
1827c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1828c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1829d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
18309a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1831d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1832485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1833d2506586SAndy Whitcroft				}
18349a974fdbSAndy Whitcroft			}
1835c45dcabdSAndy Whitcroft
1836c45dcabdSAndy Whitcroft		} else {
183713214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1838485ff23eSAlex Dowad			push(@typeListFile, $possible);
1839c45dcabdSAndy Whitcroft		}
18408905a67cSAndy Whitcroft		build_types();
18410776e594SAndy Whitcroft	} else {
18420776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
18438905a67cSAndy Whitcroft	}
18448905a67cSAndy Whitcroft}
18458905a67cSAndy Whitcroft
18466c72ffaaSAndy Whitcroftmy $prefix = '';
18476c72ffaaSAndy Whitcroft
1848000d1cc1SJoe Perchessub show_type {
1849cbec18afSJoe Perches	my ($type) = @_;
185091bfe484SJoe Perches
1851cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1852cbec18afSJoe Perches
1853cbec18afSJoe Perches	return !defined $ignore_type{$type};
1854000d1cc1SJoe Perches}
1855000d1cc1SJoe Perches
1856f0a594c1SAndy Whitcroftsub report {
1857cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1858cbec18afSJoe Perches
1859cbec18afSJoe Perches	if (!show_type($type) ||
1860cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1861773647a0SAndy Whitcroft		return 0;
1862773647a0SAndy Whitcroft	}
186357230297SJoe Perches	my $output = '';
186457230297SJoe Perches	if (-t STDOUT && $color) {
186557230297SJoe Perches		if ($level eq 'ERROR') {
186657230297SJoe Perches			$output .= RED;
186757230297SJoe Perches		} elsif ($level eq 'WARNING') {
186857230297SJoe Perches			$output .= YELLOW;
1869000d1cc1SJoe Perches		} else {
187057230297SJoe Perches			$output .= GREEN;
1871000d1cc1SJoe Perches		}
187257230297SJoe Perches	}
187357230297SJoe Perches	$output .= $prefix . $level . ':';
187457230297SJoe Perches	if ($show_types) {
187557230297SJoe Perches		$output .= BLUE if (-t STDOUT && $color);
187657230297SJoe Perches		$output .= "$type:";
187757230297SJoe Perches	}
187857230297SJoe Perches	$output .= RESET if (-t STDOUT && $color);
187957230297SJoe Perches	$output .= ' ' . $msg . "\n";
188034d8815fSJoe Perches
188134d8815fSJoe Perches	if ($showfile) {
188234d8815fSJoe Perches		my @lines = split("\n", $output, -1);
188334d8815fSJoe Perches		splice(@lines, 1, 1);
188434d8815fSJoe Perches		$output = join("\n", @lines);
188534d8815fSJoe Perches	}
188657230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
18878905a67cSAndy Whitcroft
188857230297SJoe Perches	push(our @report, $output);
1889773647a0SAndy Whitcroft
1890773647a0SAndy Whitcroft	return 1;
1891f0a594c1SAndy Whitcroft}
1892cbec18afSJoe Perches
1893f0a594c1SAndy Whitcroftsub report_dump {
189413214adfSAndy Whitcroft	our @report;
1895f0a594c1SAndy Whitcroft}
1896000d1cc1SJoe Perches
1897d752fcc8SJoe Perchessub fixup_current_range {
1898d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1899d752fcc8SJoe Perches
1900d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1901d752fcc8SJoe Perches		my $o = $1;
1902d752fcc8SJoe Perches		my $l = $2;
1903d752fcc8SJoe Perches		my $no = $o + $offset;
1904d752fcc8SJoe Perches		my $nl = $l + $length;
1905d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1906d752fcc8SJoe Perches	}
1907d752fcc8SJoe Perches}
1908d752fcc8SJoe Perches
1909d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1910d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1911d752fcc8SJoe Perches
1912d752fcc8SJoe Perches	my $range_last_linenr = 0;
1913d752fcc8SJoe Perches	my $delta_offset = 0;
1914d752fcc8SJoe Perches
1915d752fcc8SJoe Perches	my $old_linenr = 0;
1916d752fcc8SJoe Perches	my $new_linenr = 0;
1917d752fcc8SJoe Perches
1918d752fcc8SJoe Perches	my $next_insert = 0;
1919d752fcc8SJoe Perches	my $next_delete = 0;
1920d752fcc8SJoe Perches
1921d752fcc8SJoe Perches	my @lines = ();
1922d752fcc8SJoe Perches
1923d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1924d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1925d752fcc8SJoe Perches
1926d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1927d752fcc8SJoe Perches		my $save_line = 1;
1928d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1929323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1930d752fcc8SJoe Perches			$delta_offset = 0;
1931d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1932d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1933d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1934d752fcc8SJoe Perches		}
1935d752fcc8SJoe Perches
1936d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1937d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1938d752fcc8SJoe Perches			$save_line = 0;
1939d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1940d752fcc8SJoe Perches		}
1941d752fcc8SJoe Perches
1942d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1943d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1944d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1945d752fcc8SJoe Perches			$new_linenr++;
1946d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1947d752fcc8SJoe Perches		}
1948d752fcc8SJoe Perches
1949d752fcc8SJoe Perches		if ($save_line) {
1950d752fcc8SJoe Perches			push(@lines, $line);
1951d752fcc8SJoe Perches			$new_linenr++;
1952d752fcc8SJoe Perches		}
1953d752fcc8SJoe Perches
1954d752fcc8SJoe Perches		$old_linenr++;
1955d752fcc8SJoe Perches	}
1956d752fcc8SJoe Perches
1957d752fcc8SJoe Perches	return @lines;
1958d752fcc8SJoe Perches}
1959d752fcc8SJoe Perches
1960f2d7e4d4SJoe Perchessub fix_insert_line {
1961f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1962f2d7e4d4SJoe Perches
1963f2d7e4d4SJoe Perches	my $inserted = {
1964f2d7e4d4SJoe Perches		LINENR => $linenr,
1965f2d7e4d4SJoe Perches		LINE => $line,
1966f2d7e4d4SJoe Perches	};
1967f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1968f2d7e4d4SJoe Perches}
1969f2d7e4d4SJoe Perches
1970f2d7e4d4SJoe Perchessub fix_delete_line {
1971f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1972f2d7e4d4SJoe Perches
1973f2d7e4d4SJoe Perches	my $deleted = {
1974f2d7e4d4SJoe Perches		LINENR => $linenr,
1975f2d7e4d4SJoe Perches		LINE => $line,
1976f2d7e4d4SJoe Perches	};
1977f2d7e4d4SJoe Perches
1978f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1979f2d7e4d4SJoe Perches}
1980f2d7e4d4SJoe Perches
1981de7d4f0eSAndy Whitcroftsub ERROR {
1982cbec18afSJoe Perches	my ($type, $msg) = @_;
1983cbec18afSJoe Perches
1984cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1985de7d4f0eSAndy Whitcroft		our $clean = 0;
19866c72ffaaSAndy Whitcroft		our $cnt_error++;
19873705ce5bSJoe Perches		return 1;
1988de7d4f0eSAndy Whitcroft	}
19893705ce5bSJoe Perches	return 0;
1990773647a0SAndy Whitcroft}
1991de7d4f0eSAndy Whitcroftsub WARN {
1992cbec18afSJoe Perches	my ($type, $msg) = @_;
1993cbec18afSJoe Perches
1994cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1995de7d4f0eSAndy Whitcroft		our $clean = 0;
19966c72ffaaSAndy Whitcroft		our $cnt_warn++;
19973705ce5bSJoe Perches		return 1;
1998de7d4f0eSAndy Whitcroft	}
19993705ce5bSJoe Perches	return 0;
2000773647a0SAndy Whitcroft}
2001de7d4f0eSAndy Whitcroftsub CHK {
2002cbec18afSJoe Perches	my ($type, $msg) = @_;
2003cbec18afSJoe Perches
2004cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2005de7d4f0eSAndy Whitcroft		our $clean = 0;
20066c72ffaaSAndy Whitcroft		our $cnt_chk++;
20073705ce5bSJoe Perches		return 1;
20086c72ffaaSAndy Whitcroft	}
20093705ce5bSJoe Perches	return 0;
2010de7d4f0eSAndy Whitcroft}
2011de7d4f0eSAndy Whitcroft
20126ecd9674SAndy Whitcroftsub check_absolute_file {
20136ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
20146ecd9674SAndy Whitcroft	my $file = $absolute;
20156ecd9674SAndy Whitcroft
20166ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
20176ecd9674SAndy Whitcroft
20186ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
20196ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
20206ecd9674SAndy Whitcroft		if (-f "$root/$file") {
20216ecd9674SAndy Whitcroft			##print "file<$file>\n";
20226ecd9674SAndy Whitcroft			last;
20236ecd9674SAndy Whitcroft		}
20246ecd9674SAndy Whitcroft	}
20256ecd9674SAndy Whitcroft	if (! -f _)  {
20266ecd9674SAndy Whitcroft		return 0;
20276ecd9674SAndy Whitcroft	}
20286ecd9674SAndy Whitcroft
20296ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
20306ecd9674SAndy Whitcroft	my $prefix = $absolute;
20316ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
20326ecd9674SAndy Whitcroft
20336ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
20346ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2035000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2036000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
20376ecd9674SAndy Whitcroft	}
20386ecd9674SAndy Whitcroft}
20396ecd9674SAndy Whitcroft
20403705ce5bSJoe Perchessub trim {
20413705ce5bSJoe Perches	my ($string) = @_;
20423705ce5bSJoe Perches
2043b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2044b34c648bSJoe Perches
2045b34c648bSJoe Perches	return $string;
2046b34c648bSJoe Perches}
2047b34c648bSJoe Perches
2048b34c648bSJoe Perchessub ltrim {
2049b34c648bSJoe Perches	my ($string) = @_;
2050b34c648bSJoe Perches
2051b34c648bSJoe Perches	$string =~ s/^\s+//;
2052b34c648bSJoe Perches
2053b34c648bSJoe Perches	return $string;
2054b34c648bSJoe Perches}
2055b34c648bSJoe Perches
2056b34c648bSJoe Perchessub rtrim {
2057b34c648bSJoe Perches	my ($string) = @_;
2058b34c648bSJoe Perches
2059b34c648bSJoe Perches	$string =~ s/\s+$//;
20603705ce5bSJoe Perches
20613705ce5bSJoe Perches	return $string;
20623705ce5bSJoe Perches}
20633705ce5bSJoe Perches
206452ea8506SJoe Perchessub string_find_replace {
206552ea8506SJoe Perches	my ($string, $find, $replace) = @_;
206652ea8506SJoe Perches
206752ea8506SJoe Perches	$string =~ s/$find/$replace/g;
206852ea8506SJoe Perches
206952ea8506SJoe Perches	return $string;
207052ea8506SJoe Perches}
207152ea8506SJoe Perches
20723705ce5bSJoe Perchessub tabify {
20733705ce5bSJoe Perches	my ($leading) = @_;
20743705ce5bSJoe Perches
20753705ce5bSJoe Perches	my $source_indent = 8;
20763705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
20773705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
20783705ce5bSJoe Perches
20793705ce5bSJoe Perches	#convert leading spaces to tabs
20803705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
20813705ce5bSJoe Perches	#Remove spaces before a tab
20823705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
20833705ce5bSJoe Perches
20843705ce5bSJoe Perches	return "$leading";
20853705ce5bSJoe Perches}
20863705ce5bSJoe Perches
2087d1fe9c09SJoe Perchessub pos_last_openparen {
2088d1fe9c09SJoe Perches	my ($line) = @_;
2089d1fe9c09SJoe Perches
2090d1fe9c09SJoe Perches	my $pos = 0;
2091d1fe9c09SJoe Perches
2092d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2093d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2094d1fe9c09SJoe Perches
2095d1fe9c09SJoe Perches	my $last_openparen = 0;
2096d1fe9c09SJoe Perches
2097d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2098d1fe9c09SJoe Perches		return -1;
2099d1fe9c09SJoe Perches	}
2100d1fe9c09SJoe Perches
2101d1fe9c09SJoe Perches	my $len = length($line);
2102d1fe9c09SJoe Perches
2103d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2104d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2105d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2106d1fe9c09SJoe Perches			$pos += length($1) - 1;
2107d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2108d1fe9c09SJoe Perches			$last_openparen = $pos;
2109d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2110d1fe9c09SJoe Perches			last;
2111d1fe9c09SJoe Perches		}
2112d1fe9c09SJoe Perches	}
2113d1fe9c09SJoe Perches
211491cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2115d1fe9c09SJoe Perches}
2116d1fe9c09SJoe Perches
21170a920b5bSAndy Whitcroftsub process {
21180a920b5bSAndy Whitcroft	my $filename = shift;
21190a920b5bSAndy Whitcroft
21200a920b5bSAndy Whitcroft	my $linenr=0;
21210a920b5bSAndy Whitcroft	my $prevline="";
2122c2fdda0dSAndy Whitcroft	my $prevrawline="";
21230a920b5bSAndy Whitcroft	my $stashline="";
2124c2fdda0dSAndy Whitcroft	my $stashrawline="";
21250a920b5bSAndy Whitcroft
21264a0df2efSAndy Whitcroft	my $length;
21270a920b5bSAndy Whitcroft	my $indent;
21280a920b5bSAndy Whitcroft	my $previndent=0;
21290a920b5bSAndy Whitcroft	my $stashindent=0;
21300a920b5bSAndy Whitcroft
2131de7d4f0eSAndy Whitcroft	our $clean = 1;
21320a920b5bSAndy Whitcroft	my $signoff = 0;
21330a920b5bSAndy Whitcroft	my $is_patch = 0;
213429ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
213515662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2136ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2137bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
21382a076f40SJoe Perches	my $commit_log_long_line = 0;
2139e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
214013f1937eSJoe Perches	my $reported_maintainer_file = 0;
2141fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2142fa64205dSPasi Savanainen
2143365dd4eaSJoe Perches	my $last_blank_line = 0;
21445e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2145365dd4eaSJoe Perches
214613214adfSAndy Whitcroft	our @report = ();
21476c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
21486c72ffaaSAndy Whitcroft	our $cnt_error = 0;
21496c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
21506c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
21516c72ffaaSAndy Whitcroft
21520a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
21530a920b5bSAndy Whitcroft	my $realfile = '';
21540a920b5bSAndy Whitcroft	my $realline = 0;
21550a920b5bSAndy Whitcroft	my $realcnt = 0;
21560a920b5bSAndy Whitcroft	my $here = '';
2157*77cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
21580a920b5bSAndy Whitcroft	my $in_comment = 0;
2159c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
21600a920b5bSAndy Whitcroft	my $first_line = 0;
21611e855726SWolfram Sang	my $p1_prefix = '';
21620a920b5bSAndy Whitcroft
216313214adfSAndy Whitcroft	my $prev_values = 'E';
216413214adfSAndy Whitcroft
216513214adfSAndy Whitcroft	# suppression flags
2166773647a0SAndy Whitcroft	my %suppress_ifbraces;
2167170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
21682b474a1aSAndy Whitcroft	my %suppress_export;
21693e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2170653d4876SAndy Whitcroft
21717e51f197SJoe Perches	my %signatures = ();
2172323c1260SJoe Perches
2173c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2174de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2175c2fdda0dSAndy Whitcroft	#
2176de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2177de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2178773647a0SAndy Whitcroft
2179d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2180d8b07710SJoe Perches
2181773647a0SAndy Whitcroft	sanitise_line_reset();
2182c2fdda0dSAndy Whitcroft	my $line;
2183c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2184773647a0SAndy Whitcroft		$linenr++;
2185773647a0SAndy Whitcroft		$line = $rawline;
2186c2fdda0dSAndy Whitcroft
21873705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
21883705ce5bSJoe Perches
2189773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2190de7d4f0eSAndy Whitcroft			$setup_docs = 0;
21918c27ceffSMauro Carvalho Chehab			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
2192de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2193de7d4f0eSAndy Whitcroft			}
2194773647a0SAndy Whitcroft			#next;
2195de7d4f0eSAndy Whitcroft		}
2196*77cb8546SJoe Perches		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2197*77cb8546SJoe Perches			my $context = $4;
2198773647a0SAndy Whitcroft			$realline=$1-1;
2199773647a0SAndy Whitcroft			if (defined $2) {
2200773647a0SAndy Whitcroft				$realcnt=$3+1;
2201773647a0SAndy Whitcroft			} else {
2202773647a0SAndy Whitcroft				$realcnt=1+1;
2203773647a0SAndy Whitcroft			}
2204c45dcabdSAndy Whitcroft			$in_comment = 0;
2205773647a0SAndy Whitcroft
2206*77cb8546SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
2207*77cb8546SJoe Perches				$context_function = $1;
2208*77cb8546SJoe Perches			} else {
2209*77cb8546SJoe Perches				undef $context_function;
2210*77cb8546SJoe Perches			}
2211*77cb8546SJoe Perches
2212773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2213773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2214773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2215773647a0SAndy Whitcroft			# at context start.
2216773647a0SAndy Whitcroft			my $edge;
221701fa9147SAndy Whitcroft			my $cnt = $realcnt;
221801fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
221901fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
222001fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
222101fa9147SAndy Whitcroft				$cnt--;
222201fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2223721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2224fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2225fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2226fae17daeSAndy Whitcroft					($edge) = $1;
2227fae17daeSAndy Whitcroft					last;
2228fae17daeSAndy Whitcroft				}
2229773647a0SAndy Whitcroft			}
2230773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2231773647a0SAndy Whitcroft				$in_comment = 1;
2232773647a0SAndy Whitcroft			}
2233773647a0SAndy Whitcroft
2234773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2235773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2236773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2237773647a0SAndy Whitcroft			if (!defined $edge &&
223883242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2239773647a0SAndy Whitcroft			{
2240773647a0SAndy Whitcroft				$in_comment = 1;
2241773647a0SAndy Whitcroft			}
2242773647a0SAndy Whitcroft
2243773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2244773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2245773647a0SAndy Whitcroft
2246171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2247773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2248171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2249773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2250773647a0SAndy Whitcroft		}
2251773647a0SAndy Whitcroft		push(@lines, $line);
2252773647a0SAndy Whitcroft
2253773647a0SAndy Whitcroft		if ($realcnt > 1) {
2254773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2255773647a0SAndy Whitcroft		} else {
2256773647a0SAndy Whitcroft			$realcnt = 0;
2257773647a0SAndy Whitcroft		}
2258773647a0SAndy Whitcroft
2259773647a0SAndy Whitcroft		#print "==>$rawline\n";
2260773647a0SAndy Whitcroft		#print "-->$line\n";
2261de7d4f0eSAndy Whitcroft
2262de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2263de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2264de7d4f0eSAndy Whitcroft		}
2265de7d4f0eSAndy Whitcroft	}
2266de7d4f0eSAndy Whitcroft
22676c72ffaaSAndy Whitcroft	$prefix = '';
22686c72ffaaSAndy Whitcroft
2269773647a0SAndy Whitcroft	$realcnt = 0;
2270773647a0SAndy Whitcroft	$linenr = 0;
2271194f66fcSJoe Perches	$fixlinenr = -1;
22720a920b5bSAndy Whitcroft	foreach my $line (@lines) {
22730a920b5bSAndy Whitcroft		$linenr++;
2274194f66fcSJoe Perches		$fixlinenr++;
22751b5539b1SJoe Perches		my $sline = $line;	#copy of $line
22761b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
22770a920b5bSAndy Whitcroft
2278c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
22796c72ffaaSAndy Whitcroft
22800a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2281e518e9a5SJoe Perches		if (!$in_commit_log &&
2282e518e9a5SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
22830a920b5bSAndy Whitcroft			$is_patch = 1;
22844a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
22850a920b5bSAndy Whitcroft			$realline=$1-1;
22860a920b5bSAndy Whitcroft			if (defined $2) {
22870a920b5bSAndy Whitcroft				$realcnt=$3+1;
22880a920b5bSAndy Whitcroft			} else {
22890a920b5bSAndy Whitcroft				$realcnt=1+1;
22900a920b5bSAndy Whitcroft			}
2291c2fdda0dSAndy Whitcroft			annotate_reset();
229213214adfSAndy Whitcroft			$prev_values = 'E';
229313214adfSAndy Whitcroft
2294773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2295170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
22962b474a1aSAndy Whitcroft			%suppress_export = ();
22973e469cdcSAndy Whitcroft			$suppress_statement = 0;
22980a920b5bSAndy Whitcroft			next;
22990a920b5bSAndy Whitcroft
23004a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
23014a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
23024a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2303773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
23040a920b5bSAndy Whitcroft			$realline++;
2305d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
23060a920b5bSAndy Whitcroft
23074a0df2efSAndy Whitcroft			# Measure the line length and indent.
2308c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
23090a920b5bSAndy Whitcroft
23100a920b5bSAndy Whitcroft			# Track the previous line.
23110a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
23120a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2313c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2314c2fdda0dSAndy Whitcroft
2315773647a0SAndy Whitcroft			#warn "line<$line>\n";
23166c72ffaaSAndy Whitcroft
2317d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2318d8aaf121SAndy Whitcroft			$realcnt--;
23190a920b5bSAndy Whitcroft		}
23200a920b5bSAndy Whitcroft
2321cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2322cc77cdcaSAndy Whitcroft
23236c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
23246c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2325773647a0SAndy Whitcroft
23262ac73b4fSJoe Perches		my $found_file = 0;
2327773647a0SAndy Whitcroft		# extract the filename as it passes
23283bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
23293bf9a009SRabin Vincent			$realfile = $1;
23302b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2331270c49a0SJoe Perches			$in_commit_log = 0;
23322ac73b4fSJoe Perches			$found_file = 1;
23333bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2334773647a0SAndy Whitcroft			$realfile = $1;
23352b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2336270c49a0SJoe Perches			$in_commit_log = 0;
23371e855726SWolfram Sang
23381e855726SWolfram Sang			$p1_prefix = $1;
2339e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2340e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2341000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2342000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
23431e855726SWolfram Sang			}
2344773647a0SAndy Whitcroft
2345c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2346000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2347000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2348773647a0SAndy Whitcroft			}
23492ac73b4fSJoe Perches			$found_file = 1;
23502ac73b4fSJoe Perches		}
23512ac73b4fSJoe Perches
235234d8815fSJoe Perches#make up the handle for any error we report on this line
235334d8815fSJoe Perches		if ($showfile) {
235434d8815fSJoe Perches			$prefix = "$realfile:$realline: "
235534d8815fSJoe Perches		} elsif ($emacs) {
23567d3a9f67SJoe Perches			if ($file) {
23577d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
23587d3a9f67SJoe Perches			} else {
235934d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
236034d8815fSJoe Perches			}
23617d3a9f67SJoe Perches		}
236234d8815fSJoe Perches
23632ac73b4fSJoe Perches		if ($found_file) {
236485b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
236585b0ee18SJoe Perches				WARN("OBSOLETE",
236685b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
236785b0ee18SJoe Perches			}
23687bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
23692ac73b4fSJoe Perches				$check = 1;
23702ac73b4fSJoe Perches			} else {
23712ac73b4fSJoe Perches				$check = $check_orig;
23722ac73b4fSJoe Perches			}
2373773647a0SAndy Whitcroft			next;
2374773647a0SAndy Whitcroft		}
2375773647a0SAndy Whitcroft
2376389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
23770a920b5bSAndy Whitcroft
2378c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2379c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2380c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
23810a920b5bSAndy Whitcroft
23826c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
23836c72ffaaSAndy Whitcroft
2384e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2385e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2386e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2387e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2388e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2389e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2390e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2391e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2392e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2393e518e9a5SJoe Perches		}
2394e518e9a5SJoe Perches
23953bf9a009SRabin Vincent# Check for incorrect file permissions
23963bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
23973bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
239804db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
239904db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2400000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2401000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
24023bf9a009SRabin Vincent			}
24033bf9a009SRabin Vincent		}
24043bf9a009SRabin Vincent
240520112475SJoe Perches# Check the patch for a signoff:
2406d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
24074a0df2efSAndy Whitcroft			$signoff++;
240815662b3eSJoe Perches			$in_commit_log = 0;
24090a920b5bSAndy Whitcroft		}
241020112475SJoe Perches
2411e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2412e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2413e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2414e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2415e0d975b1SJoe Perches		}
2416e0d975b1SJoe Perches
241720112475SJoe Perches# Check signature styles
2418270c49a0SJoe Perches		if (!$in_header_lines &&
2419ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
242020112475SJoe Perches			my $space_before = $1;
242120112475SJoe Perches			my $sign_off = $2;
242220112475SJoe Perches			my $space_after = $3;
242320112475SJoe Perches			my $email = $4;
242420112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
242520112475SJoe Perches
2426ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2427ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2428ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2429ce0338dfSJoe Perches			}
243020112475SJoe Perches			if (defined $space_before && $space_before ne "") {
24313705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24323705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
24333705ce5bSJoe Perches				    $fix) {
2434194f66fcSJoe Perches					$fixed[$fixlinenr] =
24353705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24363705ce5bSJoe Perches				}
243720112475SJoe Perches			}
243820112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
24393705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24403705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
24413705ce5bSJoe Perches				    $fix) {
2442194f66fcSJoe Perches					$fixed[$fixlinenr] =
24433705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24443705ce5bSJoe Perches				}
24453705ce5bSJoe Perches
244620112475SJoe Perches			}
244720112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
24483705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24493705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
24503705ce5bSJoe Perches				    $fix) {
2451194f66fcSJoe Perches					$fixed[$fixlinenr] =
24523705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24533705ce5bSJoe Perches				}
245420112475SJoe Perches			}
245520112475SJoe Perches
245620112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
245720112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
245820112475SJoe Perches			if ($suggested_email eq "") {
2459000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2460000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
246120112475SJoe Perches			} else {
246220112475SJoe Perches				my $dequoted = $suggested_email;
246320112475SJoe Perches				$dequoted =~ s/^"//;
246420112475SJoe Perches				$dequoted =~ s/" </ </;
246520112475SJoe Perches				# Don't force email to have quotes
246620112475SJoe Perches				# Allow just an angle bracketed address
246720112475SJoe Perches				if ("$dequoted$comment" ne $email &&
246820112475SJoe Perches				    "<$email_address>$comment" ne $email &&
246920112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2470000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2471000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
247220112475SJoe Perches				}
24730a920b5bSAndy Whitcroft			}
24747e51f197SJoe Perches
24757e51f197SJoe Perches# Check for duplicate signatures
24767e51f197SJoe Perches			my $sig_nospace = $line;
24777e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
24787e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
24797e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
24807e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
24817e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
24827e51f197SJoe Perches			} else {
24837e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
24847e51f197SJoe Perches			}
24850a920b5bSAndy Whitcroft		}
24860a920b5bSAndy Whitcroft
2487a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2488a2fe16b9SJoe Perches		if ($in_header_lines &&
2489a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2490a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2491a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2492a2fe16b9SJoe Perches		}
2493a2fe16b9SJoe Perches
24949b3189ebSJoe Perches# Check for old stable address
24959b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
24969b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
24979b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
24989b3189ebSJoe Perches		}
24999b3189ebSJoe Perches
25007ebd05efSChristopher Covington# Check for unwanted Gerrit info
25017ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
25027ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
25037ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
25047ebd05efSChristopher Covington		}
25057ebd05efSChristopher Covington
2506369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2507369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2508369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2509369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2510369c8dd3SJoe Perches					# timestamp
2511369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2512369c8dd3SJoe Perches					# stack dump address
2513369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2514369c8dd3SJoe Perches		}
2515369c8dd3SJoe Perches
25162a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
25172a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2518bf4daf12SJoe Perches		    length($line) > 75 &&
2519bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2520bf4daf12SJoe Perches					# file delta changes
2521bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2522bf4daf12SJoe Perches					# filename then :
2523bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2524bf4daf12SJoe Perches					# A Fixes: or Link: line
2525bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
25262a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
25272a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
25282a076f40SJoe Perches			$commit_log_long_line = 1;
25292a076f40SJoe Perches		}
25302a076f40SJoe Perches
2531bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2532bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2533bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2534bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2535bf4daf12SJoe Perches		}
2536bf4daf12SJoe Perches
25370d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2538369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2539aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2540fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2541aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2542369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2543bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2544fe043ea1SJoe Perches			my $init_char = "c";
2545fe043ea1SJoe Perches			my $orig_commit = "";
25460d7835fcSJoe Perches			my $short = 1;
25470d7835fcSJoe Perches			my $long = 0;
25480d7835fcSJoe Perches			my $case = 1;
25490d7835fcSJoe Perches			my $space = 1;
25500d7835fcSJoe Perches			my $hasdesc = 0;
255119c146a6SJoe Perches			my $hasparens = 0;
25520d7835fcSJoe Perches			my $id = '0123456789ab';
25530d7835fcSJoe Perches			my $orig_desc = "commit description";
25540d7835fcSJoe Perches			my $description = "";
25550d7835fcSJoe Perches
2556fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2557fe043ea1SJoe Perches				$init_char = $1;
2558fe043ea1SJoe Perches				$orig_commit = lc($2);
2559fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2560fe043ea1SJoe Perches				$orig_commit = lc($1);
2561fe043ea1SJoe Perches			}
2562fe043ea1SJoe Perches
25630d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
25640d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
25650d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
25660d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
25670d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
25680d7835fcSJoe Perches				$orig_desc = $1;
256919c146a6SJoe Perches				$hasparens = 1;
25700d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
25710d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
25720d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
25730d7835fcSJoe Perches				$orig_desc = $1;
257419c146a6SJoe Perches				$hasparens = 1;
2575b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2576b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2577b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2578b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2579b671fde0SJoe Perches				$orig_desc = $1;
2580b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2581b671fde0SJoe Perches				$orig_desc .= " " . $1;
258219c146a6SJoe Perches				$hasparens = 1;
25830d7835fcSJoe Perches			}
25840d7835fcSJoe Perches
25850d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
25860d7835fcSJoe Perches							      $id, $orig_desc);
25870d7835fcSJoe Perches
258819c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2589d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
25900d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
25910d7835fcSJoe Perches			}
2592d311cd44SJoe Perches		}
2593d311cd44SJoe Perches
259413f1937eSJoe Perches# Check for added, moved or deleted files
259513f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
259613f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
259713f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
259813f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
259913f1937eSJoe Perches		      (defined($1) || defined($2))))) {
2600a82603a8SAndrew Jeffery			$is_patch = 1;
260113f1937eSJoe Perches			$reported_maintainer_file = 1;
260213f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
260313f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
260413f1937eSJoe Perches		}
260513f1937eSJoe Perches
260600df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
26078905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2608000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2609000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
26106c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2611de7d4f0eSAndy Whitcroft		}
2612de7d4f0eSAndy Whitcroft
2613de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2614de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2615171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2616171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2617171ae1a4SAndy Whitcroft
2618171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2619171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2620171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2621171ae1a4SAndy Whitcroft
262234d99219SJoe Perches			CHK("INVALID_UTF8",
2623000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
262400df344fSAndy Whitcroft		}
26250a920b5bSAndy Whitcroft
262615662b3eSJoe Perches# Check if it's the start of a commit log
262715662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
262815662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
262929ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
263029ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
263115662b3eSJoe Perches			$in_header_lines = 0;
263215662b3eSJoe Perches			$in_commit_log = 1;
2633ed43c4e5SAllen Hubbe			$has_commit_log = 1;
263415662b3eSJoe Perches		}
263515662b3eSJoe Perches
2636fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2637fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2638fa64205dSPasi Savanainen		if ($in_header_lines &&
2639fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2640fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2641fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2642fa64205dSPasi Savanainen		}
2643fa64205dSPasi Savanainen
2644fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
264515662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2646fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
264715662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
264815662b3eSJoe Perches		}
264915662b3eSJoe Perches
2650d6430f71SJoe Perches# Check for absolute kernel paths in commit message
2651d6430f71SJoe Perches		if ($tree && $in_commit_log) {
2652d6430f71SJoe Perches			while ($line =~ m{(?:^|\s)(/\S*)}g) {
2653d6430f71SJoe Perches				my $file = $1;
2654d6430f71SJoe Perches
2655d6430f71SJoe Perches				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2656d6430f71SJoe Perches				    check_absolute_file($1, $herecurr)) {
2657d6430f71SJoe Perches					#
2658d6430f71SJoe Perches				} else {
2659d6430f71SJoe Perches					check_absolute_file($file, $herecurr);
2660d6430f71SJoe Perches				}
2661d6430f71SJoe Perches			}
2662d6430f71SJoe Perches		}
2663d6430f71SJoe Perches
266466b47b4aSKees Cook# Check for various typo / spelling mistakes
266566d7a382SJoe Perches		if (defined($misspellings) &&
266666d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2667ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
266866b47b4aSKees Cook				my $typo = $1;
266966b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
267066b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
267166b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
267266b47b4aSKees Cook				my $msg_type = \&WARN;
267366b47b4aSKees Cook				$msg_type = \&CHK if ($file);
267466b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
267566b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
267666b47b4aSKees Cook				    $fix) {
267766b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
267866b47b4aSKees Cook				}
267966b47b4aSKees Cook			}
268066b47b4aSKees Cook		}
268166b47b4aSKees Cook
268230670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
268330670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
268400df344fSAndy Whitcroft
26850a920b5bSAndy Whitcroft#trailing whitespace
26869c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2687c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2688d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2689d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2690d5e616fcSJoe Perches			    $fix) {
2691194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2692d5e616fcSJoe Perches			}
2693c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2694c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26953705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
26963705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
26973705ce5bSJoe Perches			    $fix) {
2698194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
26993705ce5bSJoe Perches			}
27003705ce5bSJoe Perches
2701d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
27020a920b5bSAndy Whitcroft		}
27035368df20SAndy Whitcroft
27044783f894SJosh Triplett# Check for FSF mailing addresses.
2705109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
27063e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
27073e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
27084783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27094783f894SJosh Triplett			my $msg_type = \&ERROR;
27104783f894SJosh Triplett			$msg_type = \&CHK if ($file);
27114783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
27124783f894SJosh 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)
27134783f894SJosh Triplett		}
27144783f894SJosh Triplett
27153354957aSAndi Kleen# check for Kconfig help text having a real description
27169fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
27179fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
27183354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
27198d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
27203354957aSAndi Kleen			my $length = 0;
27219fe287d7SAndy Whitcroft			my $cnt = $realcnt;
27229fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
27239fe287d7SAndy Whitcroft			my $f;
2724a1385803SAndy Whitcroft			my $is_start = 0;
27259fe287d7SAndy Whitcroft			my $is_end = 0;
2726a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
27279fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
27289fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
27299fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
27309fe287d7SAndy Whitcroft
27319fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
27328d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2733a1385803SAndy Whitcroft
27348d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2735a1385803SAndy Whitcroft					$is_start = 1;
27368d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2737a1385803SAndy Whitcroft					$length = -1;
2738a1385803SAndy Whitcroft				}
2739a1385803SAndy Whitcroft
27409fe287d7SAndy Whitcroft				$f =~ s/^.//;
27413354957aSAndi Kleen				$f =~ s/#.*//;
27423354957aSAndi Kleen				$f =~ s/^\s+//;
27433354957aSAndi Kleen				next if ($f =~ /^$/);
27449fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
27459fe287d7SAndy Whitcroft					$is_end = 1;
27469fe287d7SAndy Whitcroft					last;
27479fe287d7SAndy Whitcroft				}
27483354957aSAndi Kleen				$length++;
27493354957aSAndi Kleen			}
275056193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2751000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
275256193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
275356193274SVadim Bendebury			}
2754a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
27553354957aSAndi Kleen		}
27563354957aSAndi Kleen
27571ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
27581ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
27591ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
27601ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
27611ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
27621ba8dfd1SKees Cook		}
27631ba8dfd1SKees Cook
2764327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2765327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2766327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2767327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2768327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2769327953e9SChristoph Jaeger		}
2770327953e9SChristoph Jaeger
2771c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2772c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2773c68e5878SArnaud Lacombe			my $flag = $1;
2774c68e5878SArnaud Lacombe			my $replacement = {
2775c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2776c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2777c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2778c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2779c68e5878SArnaud Lacombe			};
2780c68e5878SArnaud Lacombe
2781c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2782c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2783c68e5878SArnaud Lacombe		}
2784c68e5878SArnaud Lacombe
2785bff5da43SRob Herring# check for DT compatible documentation
27867dd05b38SFlorian Vaussard		if (defined $root &&
27877dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
27887dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
27897dd05b38SFlorian Vaussard
2790bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2791bff5da43SRob Herring
2792cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2793cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2794cc93319bSFlorian Vaussard
2795bff5da43SRob Herring			foreach my $compat (@compats) {
2796bff5da43SRob Herring				my $compat2 = $compat;
2797185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2798185d566bSRob Herring				my $compat3 = $compat;
2799185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2800185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2801bff5da43SRob Herring				if ( $? >> 8 ) {
2802bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2803bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2804bff5da43SRob Herring				}
2805bff5da43SRob Herring
28064fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
28074fbf32a6SFlorian Vaussard				my $vendor = $1;
2808cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2809bff5da43SRob Herring				if ( $? >> 8 ) {
2810bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2811cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2812bff5da43SRob Herring				}
2813bff5da43SRob Herring			}
2814bff5da43SRob Herring		}
2815bff5da43SRob Herring
28165368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2817d6430f71SJoe Perches		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
28185368df20SAndy Whitcroft
281947e0c88bSJoe Perches# line length limit (with some exclusions)
282047e0c88bSJoe Perches#
282147e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
282247e0c88bSJoe Perches#	logging functions like pr_info that end in a string
282347e0c88bSJoe Perches#	lines with a single string
282447e0c88bSJoe Perches#	#defines that are a single string
282547e0c88bSJoe Perches#
282647e0c88bSJoe Perches# There are 3 different line length message types:
282747e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_linelength
282847e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
282947e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
283047e0c88bSJoe Perches#
283147e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
283247e0c88bSJoe Perches#
283347e0c88bSJoe Perches
2834b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
283547e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
283647e0c88bSJoe Perches
283747e0c88bSJoe Perches			# Check the allowed long line types first
283847e0c88bSJoe Perches
283947e0c88bSJoe Perches			# logging functions that end in a string that starts
284047e0c88bSJoe Perches			# before $max_line_length
284147e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
284247e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
284347e0c88bSJoe Perches				$msg_type = "";
284447e0c88bSJoe Perches
284547e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
284647e0c88bSJoe Perches			# #defines with only strings
284747e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
284847e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
284947e0c88bSJoe Perches				$msg_type = "";
285047e0c88bSJoe Perches
2851d560a5f8SJoe Perches			# EFI_GUID is another special case
2852d560a5f8SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2853d560a5f8SJoe Perches				$msg_type = "";
2854d560a5f8SJoe Perches
285547e0c88bSJoe Perches			# Otherwise set the alternate message types
285647e0c88bSJoe Perches
285747e0c88bSJoe Perches			# a comment starts before $max_line_length
285847e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
285947e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
286047e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
286147e0c88bSJoe Perches
286247e0c88bSJoe Perches			# a quoted string starts before $max_line_length
286347e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
286447e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
286547e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
286647e0c88bSJoe Perches			}
286747e0c88bSJoe Perches
286847e0c88bSJoe Perches			if ($msg_type ne "" &&
286947e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
287047e0c88bSJoe Perches				WARN($msg_type,
28716cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
28720a920b5bSAndy Whitcroft			}
287347e0c88bSJoe Perches		}
28740a920b5bSAndy Whitcroft
28758905a67cSAndy Whitcroft# check for adding lines without a newline.
28768905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2877000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2878000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
28798905a67cSAndy Whitcroft		}
28808905a67cSAndy Whitcroft
288142e41c54SMike Frysinger# Blackfin: use hi/lo macros
288242e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
288342e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
288442e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2885000d1cc1SJoe Perches				ERROR("LO_MACRO",
2886000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
288742e41c54SMike Frysinger			}
288842e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
288942e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2890000d1cc1SJoe Perches				ERROR("HI_MACRO",
2891000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
289242e41c54SMike Frysinger			}
289342e41c54SMike Frysinger		}
289442e41c54SMike Frysinger
2895b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2896de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
28970a920b5bSAndy Whitcroft
28980a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
28990a920b5bSAndy Whitcroft# more than 8 must use tabs.
2900c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2901c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2902c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2903d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
29043705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
29053705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
29063705ce5bSJoe Perches			    $fix) {
2907194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
29083705ce5bSJoe Perches			}
29090a920b5bSAndy Whitcroft		}
29100a920b5bSAndy Whitcroft
291108e44365SAlberto Panizzo# check for space before tabs.
291208e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
291308e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29143705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
29153705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
29163705ce5bSJoe Perches			    $fix) {
2917194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2918d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2919194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2920c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
29213705ce5bSJoe Perches			}
292208e44365SAlberto Panizzo		}
292308e44365SAlberto Panizzo
2924d1fe9c09SJoe Perches# check for && or || at the start of a line
2925d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2926d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2927d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2928d1fe9c09SJoe Perches		}
2929d1fe9c09SJoe Perches
2930a91e8994SJoe Perches# check indentation starts on a tab stop
2931a91e8994SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2932a91e8994SJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2933a91e8994SJoe Perches			my $indent = length($1);
2934a91e8994SJoe Perches			if ($indent % 8) {
2935a91e8994SJoe Perches				if (WARN("TABSTOP",
2936a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
2937a91e8994SJoe Perches				    $fix) {
2938a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2939a91e8994SJoe Perches				}
2940a91e8994SJoe Perches			}
2941a91e8994SJoe Perches		}
2942a91e8994SJoe Perches
2943d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2944d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
294591cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2946d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2947d1fe9c09SJoe Perches			my $oldindent = $1;
2948d1fe9c09SJoe Perches			my $rest = $2;
2949d1fe9c09SJoe Perches
2950d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2951d1fe9c09SJoe Perches			if ($pos >= 0) {
2952b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2953b34a26f3SJoe Perches				my $newindent = $2;
2954d1fe9c09SJoe Perches
2955d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2956d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2957d1fe9c09SJoe Perches					" "  x ($pos % 8);
2958d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2959d1fe9c09SJoe Perches
2960d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2961d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
29623705ce5bSJoe Perches
29633705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
29643705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
29653705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2966194f66fcSJoe Perches						$fixed[$fixlinenr] =~
29673705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
29683705ce5bSJoe Perches					}
2969d1fe9c09SJoe Perches				}
2970d1fe9c09SJoe Perches			}
2971d1fe9c09SJoe Perches		}
2972d1fe9c09SJoe Perches
29736ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
29746ab3a970SJoe Perches# avoid checking a few false positives:
29756ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
29766ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
29776ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
29786ab3a970SJoe Perches#   multiline macros that define functions
29796ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
29806ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
29816ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
29823705ce5bSJoe Perches			if (CHK("SPACING",
2983f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
29843705ce5bSJoe Perches			    $fix) {
2985194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2986f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
29873705ce5bSJoe Perches			}
2988aad4f614SJoe Perches		}
2989aad4f614SJoe Perches
299086406b1cSJoe Perches# Block comment styles
299186406b1cSJoe Perches# Networking with an initial /*
299205880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2993fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
299485ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
299585ad978cSJoe Perches		    $realline > 2) {
299605880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
299705880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
299805880600SJoe Perches		}
299905880600SJoe Perches
300086406b1cSJoe Perches# Block comments use * on subsequent lines
300186406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
300286406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3003a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
300461135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
3005a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
300686406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
300786406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
3008a605e32eSJoe Perches		}
3009a605e32eSJoe Perches
301086406b1cSJoe Perches# Block comments use */ on trailing lines
301186406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3012c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3013c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3014c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
301586406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
301686406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
301705880600SJoe Perches		}
301805880600SJoe Perches
301908eb9b80SJoe Perches# Block comment * alignment
302008eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3021af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3022af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3023af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
302408eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3025af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3026af207524SJoe Perches			my $oldindent;
302708eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3028af207524SJoe Perches			if (defined($1)) {
3029af207524SJoe Perches				$oldindent = expand_tabs($1);
3030af207524SJoe Perches			} else {
3031af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
3032af207524SJoe Perches				$oldindent = expand_tabs($1);
3033af207524SJoe Perches			}
303408eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
303508eb9b80SJoe Perches			my $newindent = $1;
303608eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
3037af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
303808eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
303908eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
304008eb9b80SJoe Perches			}
304108eb9b80SJoe Perches		}
304208eb9b80SJoe Perches
30437f619191SJoe Perches# check for missing blank lines after struct/union declarations
30447f619191SJoe Perches# with exceptions for various attributes and macros
30457f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
30467f619191SJoe Perches		    $line =~ /^\+/ &&
30477f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
30487f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
30497f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
30507f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
30517f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
30527f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
30537f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
30547f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3055d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3056d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3057d752fcc8SJoe Perches			    $fix) {
3058f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3059d752fcc8SJoe Perches			}
30607f619191SJoe Perches		}
30617f619191SJoe Perches
3062365dd4eaSJoe Perches# check for multiple consecutive blank lines
3063365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3064365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3065365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3066d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3067d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3068d752fcc8SJoe Perches			    $fix) {
3069f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3070d752fcc8SJoe Perches			}
3071d752fcc8SJoe Perches
3072365dd4eaSJoe Perches			$last_blank_line = $linenr;
3073365dd4eaSJoe Perches		}
3074365dd4eaSJoe Perches
30753b617e3bSJoe Perches# check for missing blank lines after declarations
30763f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
30773f7bac03SJoe Perches			# actual declarations
30783f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
30795a4e1fd3SJoe Perches			# function pointer declarations
30805a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
30813f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
30823f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
30833f7bac03SJoe Perches			# known declaration macros
30843f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
30853f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
30863f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
30873f7bac03SJoe Perches			# other possible extensions of declaration lines
30883f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
30893f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
30903f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
30913f7bac03SJoe Perches			# looks like a declaration
30923f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
30935a4e1fd3SJoe Perches			# function pointer declarations
30945a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
30953f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
30963f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
30973f7bac03SJoe Perches			# known declaration macros
30983f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
30993f7bac03SJoe Perches			# start of struct or union or enum
31003b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
31013f7bac03SJoe Perches			# start or end of block or continuation of declaration
31023f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
31033f7bac03SJoe Perches			# bitfield continuation
31043f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
31053f7bac03SJoe Perches			# other possible extensions of declaration lines
31063f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
31073f7bac03SJoe Perches			# indentation of previous and current line are the same
31083f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3109d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3110d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3111d752fcc8SJoe Perches			    $fix) {
3112f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3113d752fcc8SJoe Perches			}
31143b617e3bSJoe Perches		}
31153b617e3bSJoe Perches
31165f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
31176b4c5bebSAndy Whitcroft# Exceptions:
31186b4c5bebSAndy Whitcroft#  1) within comments
31196b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
31206b4c5bebSAndy Whitcroft#  3) hanging labels
31213705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
31225f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
31233705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
31243705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
31253705ce5bSJoe Perches			    $fix) {
3126194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
31273705ce5bSJoe Perches			}
31285f7ddae6SRaffaele Recalcati		}
31295f7ddae6SRaffaele Recalcati
3130b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3131b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3132b9ea10d6SAndy Whitcroft
3133032a4c0fSJoe Perches# check indentation of any line with a bare else
3134840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3135032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3136032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3137032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3138840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3139840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3140840080a0SJoe Perches			     defined $lines[$linenr] &&
3141840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3142032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3143032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3144032a4c0fSJoe Perches			}
3145032a4c0fSJoe Perches		}
3146032a4c0fSJoe Perches
3147c00df19aSJoe Perches# check indentation of a line with a break;
3148c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3149c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3150c00df19aSJoe Perches			my $tabs = $1;
3151c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3152c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3153c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3154c00df19aSJoe Perches			}
3155c00df19aSJoe Perches		}
3156c00df19aSJoe Perches
31571ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
31581ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
31591ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
31601ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
31611ba8dfd1SKees Cook		}
31621ba8dfd1SKees Cook
3163c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3164cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3165000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3166000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3167c2fdda0dSAndy Whitcroft		}
316822f2a2efSAndy Whitcroft
316942e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
317042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
317142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3172000d1cc1SJoe Perches			ERROR("CSYNC",
3173000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
317442e41c54SMike Frysinger		}
317542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
317642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3177000d1cc1SJoe Perches			ERROR("SSYNC",
3178000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
317942e41c54SMike Frysinger		}
318042e41c54SMike Frysinger
318156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
318256e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
318356e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
318456e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
318556e77d70SJoe Perches		}
318656e77d70SJoe Perches
31879c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
31882b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
31892b474a1aSAndy Whitcroft		    $realline_next);
31903e469cdcSAndy Whitcroft#print "LINE<$line>\n";
31913e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
31921b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3193170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3194f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3195171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3196171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3197171ae1a4SAndy Whitcroft
31983e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
31993e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
32003e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
32013e469cdcSAndy Whitcroft			# until we hit end of it.
32023e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
32033e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
32043e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
32053e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
32063e469cdcSAndy Whitcroft			}
3207f74bd194SAndy Whitcroft
32082b474a1aSAndy Whitcroft			# Find the real next line.
32092b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
32102b474a1aSAndy Whitcroft			if (defined $realline_next &&
32112b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
32122b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
32132b474a1aSAndy Whitcroft				$realline_next++;
32142b474a1aSAndy Whitcroft			}
32152b474a1aSAndy Whitcroft
3216171ae1a4SAndy Whitcroft			my $s = $stat;
3217171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3218cf655043SAndy Whitcroft
3219c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3220171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3221c2fdda0dSAndy Whitcroft
3222c2fdda0dSAndy Whitcroft			# Ignore functions being called
3223171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3224c2fdda0dSAndy Whitcroft
3225463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3226463f2864SAndy Whitcroft
3227c45dcabdSAndy Whitcroft			# declarations always start with types
3228d2506586SAndy 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) {
3229c45dcabdSAndy Whitcroft				my $type = $1;
3230c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3231c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3232c45dcabdSAndy Whitcroft
32336c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3234a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3235c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3236c2fdda0dSAndy Whitcroft			}
32378905a67cSAndy Whitcroft
32386c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
323965863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3240c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
32419c0ca6f9SAndy Whitcroft			}
32428905a67cSAndy Whitcroft
32438905a67cSAndy Whitcroft			# Check for any sort of function declaration.
32448905a67cSAndy Whitcroft			# int foo(something bar, other baz);
32458905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3246171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
32478905a67cSAndy Whitcroft				my ($name_len) = length($1);
32488905a67cSAndy Whitcroft
3249cf655043SAndy Whitcroft				my $ctx = $s;
3250773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
32518905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3252cf655043SAndy Whitcroft
32538905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3254c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
32558905a67cSAndy Whitcroft
3256c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
32578905a67cSAndy Whitcroft					}
32588905a67cSAndy Whitcroft				}
32598905a67cSAndy Whitcroft			}
32608905a67cSAndy Whitcroft
32619c0ca6f9SAndy Whitcroft		}
32629c0ca6f9SAndy Whitcroft
326300df344fSAndy Whitcroft#
326400df344fSAndy Whitcroft# Checks which may be anchored in the context.
326500df344fSAndy Whitcroft#
326600df344fSAndy Whitcroft
326700df344fSAndy Whitcroft# Check for switch () and associated case and default
326800df344fSAndy Whitcroft# statements should be at the same indent.
326900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
327000df344fSAndy Whitcroft			my $err = '';
327100df344fSAndy Whitcroft			my $sep = '';
327200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
327300df344fSAndy Whitcroft			shift(@ctx);
327400df344fSAndy Whitcroft			for my $ctx (@ctx) {
327500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
327600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
327700df344fSAndy Whitcroft							$indent != $cindent) {
327800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
327900df344fSAndy Whitcroft					$sep = '';
328000df344fSAndy Whitcroft				} else {
328100df344fSAndy Whitcroft					$sep = "[...]\n";
328200df344fSAndy Whitcroft				}
328300df344fSAndy Whitcroft			}
328400df344fSAndy Whitcroft			if ($err ne '') {
3285000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3286000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3287de7d4f0eSAndy Whitcroft			}
3288de7d4f0eSAndy Whitcroft		}
3289de7d4f0eSAndy Whitcroft
3290de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3291de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
32920fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3293773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3294773647a0SAndy Whitcroft
32959c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
32968eef05ddSJoe Perches
32978eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
32988eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
32998eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
33008eef05ddSJoe Perches			}
33018eef05ddSJoe Perches
3302de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3303de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3304de7d4f0eSAndy Whitcroft
3305548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3306548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3307de7d4f0eSAndy Whitcroft
3308548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3309548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3310548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3311548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3312548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3313773647a0SAndy Whitcroft				$ctx_ln++;
3314773647a0SAndy Whitcroft			}
3315548596d5SAndy Whitcroft
331653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
331753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3318773647a0SAndy Whitcroft
3319773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3320000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3321000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
332201464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
332300df344fSAndy Whitcroft			}
3324773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3325773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3326773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3327773647a0SAndy Whitcroft			{
33289c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
33299c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3330000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3331000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
333201464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
33339c0ca6f9SAndy Whitcroft				}
33349c0ca6f9SAndy Whitcroft			}
333500df344fSAndy Whitcroft		}
333600df344fSAndy Whitcroft
33374d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
33380fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
33393e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
33403e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
33413e469cdcSAndy Whitcroft					if (!defined $stat);
33424d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
33434d001e4dSAndy Whitcroft
33444d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
33454d001e4dSAndy Whitcroft
33469f5af480SJoe Perches			# remove inline comments
33479f5af480SJoe Perches			$s =~ s/$;/ /g;
33489f5af480SJoe Perches			$c =~ s/$;/ /g;
33494d001e4dSAndy Whitcroft
33504d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
33516f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
33526f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
33534d001e4dSAndy Whitcroft
33549f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
33559f5af480SJoe Perches			# none on the first line, and are going to readd them
33569f5af480SJoe Perches			# where necessary.
33579f5af480SJoe Perches			$s =~ s/\n./\n/gs;
33589f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
33599f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
33609f5af480SJoe Perches			}
33619f5af480SJoe Perches
33624d001e4dSAndy Whitcroft			# We want to check the first line inside the block
33634d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
33644d001e4dSAndy Whitcroft			#  1) any blank line termination
33654d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
33664d001e4dSAndy Whitcroft			#  3) any do (...) {
33674d001e4dSAndy Whitcroft			my $continuation = 0;
33684d001e4dSAndy Whitcroft			my $check = 0;
33694d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
33704d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
33714d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
33724d001e4dSAndy Whitcroft				$continuation = 1;
33734d001e4dSAndy Whitcroft			}
33749bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
33754d001e4dSAndy Whitcroft				$check = 1;
33764d001e4dSAndy Whitcroft				$cond_lines++;
33774d001e4dSAndy Whitcroft			}
33784d001e4dSAndy Whitcroft
33794d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
33804d001e4dSAndy Whitcroft			# preprocessor statement.
33814d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
33824d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
33834d001e4dSAndy Whitcroft				$check = 0;
33844d001e4dSAndy Whitcroft			}
33854d001e4dSAndy Whitcroft
33869bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3387740504c6SAndy Whitcroft			$continuation = 0;
33889bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
33899bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
33904d001e4dSAndy Whitcroft
3391f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3392f16fa28fSAndy Whitcroft				# is not linear.
3393f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3394f16fa28fSAndy Whitcroft					$check = 0;
3395f16fa28fSAndy Whitcroft				}
3396f16fa28fSAndy Whitcroft
33979bd49efeSAndy Whitcroft				# Ignore:
33989bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
33999bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
34009bd49efeSAndy Whitcroft				#  3) labels.
3401740504c6SAndy Whitcroft				if ($continuation ||
3402740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
34039bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
34049bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3405740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
340630dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
34079bd49efeSAndy Whitcroft						$cond_lines++;
34089bd49efeSAndy Whitcroft					}
34094d001e4dSAndy Whitcroft				}
341030dad6ebSAndy Whitcroft			}
34114d001e4dSAndy Whitcroft
34124d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
34134d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
34144d001e4dSAndy Whitcroft
34154d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
34164d001e4dSAndy Whitcroft			# this is not this patch's fault.
34174d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
34184d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
34194d001e4dSAndy Whitcroft				$check = 0;
34204d001e4dSAndy Whitcroft			}
34214d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
34224d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
34234d001e4dSAndy Whitcroft			}
34244d001e4dSAndy Whitcroft
34259bd49efeSAndy 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";
34264d001e4dSAndy Whitcroft
34279f5af480SJoe Perches			if ($check && $s ne '' &&
34289f5af480SJoe Perches			    (($sindent % 8) != 0 ||
34299f5af480SJoe Perches			     ($sindent < $indent) ||
34309f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3431000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3432000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
34334d001e4dSAndy Whitcroft			}
34344d001e4dSAndy Whitcroft		}
34354d001e4dSAndy Whitcroft
34366c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
34376c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
34381f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
34391f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
34406c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3441c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3442c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3443cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3444cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
34451f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3446c2fdda0dSAndy Whitcroft		}
34476c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
34486c72ffaaSAndy Whitcroft
344900df344fSAndy Whitcroft#ignore lines not being added
34503705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
345100df344fSAndy Whitcroft
345211ca40a0SJoe Perches# check for dereferences that span multiple lines
345311ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
345411ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
345511ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
345611ca40a0SJoe Perches			my $ref = $1;
345711ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
345811ca40a0SJoe Perches			$ref .= $1;
345911ca40a0SJoe Perches			$ref =~ s/\s//g;
346011ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
346111ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
346211ca40a0SJoe Perches		}
346311ca40a0SJoe Perches
3464a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3465c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3466a1ce18e4SJoe Perches			my $type = $1;
3467a1ce18e4SJoe Perches			my $var = $2;
3468207a8e84SJoe Perches			$var = "" if (!defined $var);
3469207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3470a1ce18e4SJoe Perches				my $sign = $1;
3471a1ce18e4SJoe Perches				my $pointer = $2;
3472a1ce18e4SJoe Perches
3473a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3474a1ce18e4SJoe Perches
3475a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3476a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3477a1ce18e4SJoe Perches				    $fix) {
3478a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3479207a8e84SJoe Perches					my $comp_pointer = $pointer;
3480207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3481207a8e84SJoe Perches					$decl .= $comp_pointer;
3482207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3483207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3484a1ce18e4SJoe Perches				}
3485a1ce18e4SJoe Perches			}
3486a1ce18e4SJoe Perches		}
3487a1ce18e4SJoe Perches
3488653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
34897429c690SAndy Whitcroft		if ($dbg_type) {
34907429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3491000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3492000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
34937429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3494000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3495000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
34967429c690SAndy Whitcroft			}
3497653d4876SAndy Whitcroft			next;
3498653d4876SAndy Whitcroft		}
3499a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3500a1ef277eSAndy Whitcroft		if ($dbg_attr) {
35019360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3502000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3503000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
35049360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3505000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3506000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3507a1ef277eSAndy Whitcroft			}
3508a1ef277eSAndy Whitcroft			next;
3509a1ef277eSAndy Whitcroft		}
3510653d4876SAndy Whitcroft
3511f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
351299423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
351399423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3514d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3515d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3516f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3517f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3518f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3519d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3520d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3521f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3522d752fcc8SJoe Perches				$fixedline = $line;
3523d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3524f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3525d752fcc8SJoe Perches			}
3526f0a594c1SAndy Whitcroft		}
3527f0a594c1SAndy Whitcroft
352800df344fSAndy Whitcroft#
352900df344fSAndy Whitcroft# Checks which are anchored on the added line.
353000df344fSAndy Whitcroft#
353100df344fSAndy Whitcroft
3532653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3533c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3534653d4876SAndy Whitcroft			my $path = $1;
3535653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3536000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3537495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3538495e9d84SJoe Perches			}
3539495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3540495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3541495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3542653d4876SAndy Whitcroft			}
3543653d4876SAndy Whitcroft		}
3544653d4876SAndy Whitcroft
354500df344fSAndy Whitcroft# no C99 // comments
354600df344fSAndy Whitcroft		if ($line =~ m{//}) {
35473705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
35483705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
35493705ce5bSJoe Perches			    $fix) {
3550194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
35513705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
35523705ce5bSJoe Perches					my $comment = trim($1);
3553194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
35543705ce5bSJoe Perches				}
35553705ce5bSJoe Perches			}
355600df344fSAndy Whitcroft		}
355700df344fSAndy Whitcroft		# Remove C99 comments.
35580a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
35596c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
35600a920b5bSAndy Whitcroft
35612b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
35622b474a1aSAndy Whitcroft# the whole statement.
35632b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
35642b474a1aSAndy Whitcroft		if (defined $realline_next &&
35652b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
35662b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
35672b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
35682b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
35693cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
35703cbf62dfSAndy Whitcroft			# a prefix:
35713cbf62dfSAndy Whitcroft			#   XXX(foo);
35723cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3573653d4876SAndy Whitcroft			my $name = $1;
357487a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
35753cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
35763cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
35773cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
35783cbf62dfSAndy Whitcroft
35793cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
35802b474a1aSAndy Whitcroft				\n.}\s*$|
358148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
358248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
358348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
35842b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
35852b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
358648012058SAndy Whitcroft			    )/x) {
35872b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
35882b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
35892b474a1aSAndy Whitcroft			} else {
35902b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
35910a920b5bSAndy Whitcroft			}
35920a920b5bSAndy Whitcroft		}
35932b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
35942b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
35952b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
35962b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
35972b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
35982b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
35992b474a1aSAndy Whitcroft		}
36002b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
36012b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3602000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3603000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
36042b474a1aSAndy Whitcroft		}
36050a920b5bSAndy Whitcroft
36065150bda4SJoe Eloff# check for global initialisers.
36076d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3608d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
36096d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3610d5e616fcSJoe Perches			    $fix) {
36116d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3612d5e616fcSJoe Perches			}
3613f0a594c1SAndy Whitcroft		}
36140a920b5bSAndy Whitcroft# check for static initialisers.
36156d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3616d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
36176d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3618d5e616fcSJoe Perches				      $herecurr) &&
3619d5e616fcSJoe Perches			    $fix) {
36206d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3621d5e616fcSJoe Perches			}
36220a920b5bSAndy Whitcroft		}
36230a920b5bSAndy Whitcroft
36241813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
36251813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
36261813087dSJoe Perches			my $tmp = trim($1);
36271813087dSJoe Perches			WARN("MISORDERED_TYPE",
36281813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
36291813087dSJoe Perches		}
36301813087dSJoe Perches
3631cb710ecaSJoe Perches# check for static const char * arrays.
3632cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3633000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3634000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3635cb710ecaSJoe Perches				$herecurr);
3636cb710ecaSJoe Perches               }
3637cb710ecaSJoe Perches
3638cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3639cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3640000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3641000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3642cb710ecaSJoe Perches				$herecurr);
3643cb710ecaSJoe Perches               }
3644cb710ecaSJoe Perches
3645ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3646ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3647ab7e23f3SJoe Perches			my $found = $1;
3648ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3649ab7e23f3SJoe Perches				WARN("CONST_CONST",
3650ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3651ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3652ab7e23f3SJoe Perches				WARN("CONST_CONST",
3653ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3654ab7e23f3SJoe Perches			}
3655ab7e23f3SJoe Perches		}
3656ab7e23f3SJoe Perches
36579b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
36589b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
36599b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
36609b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
36619b0fa60dSJoe Perches				$herecurr);
36629b0fa60dSJoe Perches               }
36639b0fa60dSJoe Perches
3664b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3665b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3666b598b670SJoe Perches			my $array = $1;
3667b598b670SJoe 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*\))@) {
3668b598b670SJoe Perches				my $array_div = $1;
3669b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3670b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3671b598b670SJoe Perches				    $fix) {
3672b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3673b598b670SJoe Perches				}
3674b598b670SJoe Perches			}
3675b598b670SJoe Perches		}
3676b598b670SJoe Perches
3677b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3678b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3679b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3680b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3681b36190c5SJoe Perches			    $fix) {
3682194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3683b36190c5SJoe Perches			}
3684b36190c5SJoe Perches		}
3685b36190c5SJoe Perches
3686653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3687653d4876SAndy Whitcroft# make sense.
3688653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
36898054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3690c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
36918ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
369246d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
3693000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3694000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
36950a920b5bSAndy Whitcroft		}
36960a920b5bSAndy Whitcroft
36970a920b5bSAndy Whitcroft# * goes on variable not on type
369865863862SAndy Whitcroft		# (char*[ const])
3699bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3700bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
37013705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3702d8aaf121SAndy Whitcroft
370365863862SAndy Whitcroft			# Should start with a space.
370465863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
370565863862SAndy Whitcroft			# Should not end with a space.
370665863862SAndy Whitcroft			$to =~ s/\s+$//;
370765863862SAndy Whitcroft			# '*'s should not have spaces between.
3708f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
370965863862SAndy Whitcroft			}
3710d8aaf121SAndy Whitcroft
37113705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
371265863862SAndy Whitcroft			if ($from ne $to) {
37133705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
37143705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
37153705ce5bSJoe Perches				    $fix) {
37163705ce5bSJoe Perches					my $sub_from = $ident;
37173705ce5bSJoe Perches					my $sub_to = $ident;
37183705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3719194f66fcSJoe Perches					$fixed[$fixlinenr] =~
37203705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
37213705ce5bSJoe Perches				}
372265863862SAndy Whitcroft			}
3723bfcb2cc7SAndy Whitcroft		}
3724bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3725bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
37263705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3727d8aaf121SAndy Whitcroft
372865863862SAndy Whitcroft			# Should start with a space.
372965863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
373065863862SAndy Whitcroft			# Should not end with a space.
373165863862SAndy Whitcroft			$to =~ s/\s+$//;
373265863862SAndy Whitcroft			# '*'s should not have spaces between.
3733f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
373465863862SAndy Whitcroft			}
373565863862SAndy Whitcroft			# Modifiers should have spaces.
373665863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
373765863862SAndy Whitcroft
37383705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3739667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
37403705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
37413705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
37423705ce5bSJoe Perches				    $fix) {
37433705ce5bSJoe Perches
37443705ce5bSJoe Perches					my $sub_from = $match;
37453705ce5bSJoe Perches					my $sub_to = $match;
37463705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3747194f66fcSJoe Perches					$fixed[$fixlinenr] =~
37483705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
37493705ce5bSJoe Perches				}
375065863862SAndy Whitcroft			}
37510a920b5bSAndy Whitcroft		}
37520a920b5bSAndy Whitcroft
37539d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
37549d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
37559d3e3c70SJoe Perches			my $msg_type = \&WARN;
37569d3e3c70SJoe Perches			$msg_type = \&CHK if ($file);
37579d3e3c70SJoe Perches			&{$msg_type}("AVOID_BUG",
37589d3e3c70SJoe Perches				     "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
37599d3e3c70SJoe Perches		}
37600a920b5bSAndy Whitcroft
37619d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
37628905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3763000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3764000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
37658905a67cSAndy Whitcroft		}
37668905a67cSAndy Whitcroft
376717441227SJoe Perches# check for uses of printk_ratelimit
376817441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3769000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3770000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
377117441227SJoe Perches		}
377217441227SJoe Perches
377300df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
377400df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
377500df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
377625985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
377700df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3778f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
377900df344fSAndy Whitcroft			my $ok = 0;
378000df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
378100df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
378225985edcSLucas De Marchi				# we have a preceding printk if it ends
378300df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
378400df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
378500df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
378600df344fSAndy Whitcroft						$ok = 1;
378700df344fSAndy Whitcroft					}
378800df344fSAndy Whitcroft					last;
378900df344fSAndy Whitcroft				}
379000df344fSAndy Whitcroft			}
379100df344fSAndy Whitcroft			if ($ok == 0) {
3792000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3793000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
37940a920b5bSAndy Whitcroft			}
379500df344fSAndy Whitcroft		}
37960a920b5bSAndy Whitcroft
3797243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3798243f3803SJoe Perches			my $orig = $1;
3799243f3803SJoe Perches			my $level = lc($orig);
3800243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
38018f26b837SJoe Perches			my $level2 = $level;
38028f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3803243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3804daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3805243f3803SJoe Perches		}
3806243f3803SJoe Perches
3807243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3808d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3809d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3810d5e616fcSJoe Perches			    $fix) {
3811194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3812d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3813d5e616fcSJoe Perches			}
3814243f3803SJoe Perches		}
3815243f3803SJoe Perches
3816dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3817dc139313SJoe Perches			my $orig = $1;
3818dc139313SJoe Perches			my $level = lc($orig);
3819dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3820dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3821dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3822dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3823dc139313SJoe Perches		}
3824dc139313SJoe Perches
382591c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
382691c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
382791c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
382891c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
382991c9afafSAndy Lutomirski			WARN("ENOSYS",
383091c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
383191c9afafSAndy Lutomirski		}
383291c9afafSAndy Lutomirski
3833653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3834653d4876SAndy Whitcroft# or if closed on same line
38358d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
38364e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
38378d182478SJoe Perches			if (ERROR("OPEN_BRACE",
38388d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
38398d182478SJoe Perches			    $fix) {
38408d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
38418d182478SJoe Perches				my $fixed_line = $rawline;
38428d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
38438d182478SJoe Perches				my $line1 = $1;
38448d182478SJoe Perches				my $line2 = $2;
38458d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
38468d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
38478d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
38488d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
38498d182478SJoe Perches				}
38508d182478SJoe Perches			}
38510a920b5bSAndy Whitcroft		}
3852653d4876SAndy Whitcroft
38538905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
38548905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
38558905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
38568d182478SJoe Perches			if (ERROR("OPEN_BRACE",
38578d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
38588d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
38598d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
38608d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
38618d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
38628d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
38638d182478SJoe Perches				$fixedline = $rawline;
38648d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
38658d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
38668d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
38678d182478SJoe Perches				}
38688d182478SJoe Perches			}
38698905a67cSAndy Whitcroft		}
38708905a67cSAndy Whitcroft
38710c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
38723705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
38733705ce5bSJoe Perches			if (WARN("SPACING",
38743705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
38753705ce5bSJoe Perches			    $fix) {
3876194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38773705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
38783705ce5bSJoe Perches			}
38790c73b4ebSAndy Whitcroft		}
38800c73b4ebSAndy Whitcroft
388131070b5dSJoe Perches# Function pointer declarations
388231070b5dSJoe Perches# check spacing between type, funcptr, and args
388331070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
388491f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
388531070b5dSJoe Perches			my $declare = $1;
388631070b5dSJoe Perches			my $pre_pointer_space = $2;
388731070b5dSJoe Perches			my $post_pointer_space = $3;
388831070b5dSJoe Perches			my $funcname = $4;
388931070b5dSJoe Perches			my $post_funcname_space = $5;
389031070b5dSJoe Perches			my $pre_args_space = $6;
389131070b5dSJoe Perches
389291f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
389391f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
389491f72e9cSJoe Perches# don't need a space so don't warn for those.
389591f72e9cSJoe Perches			my $post_declare_space = "";
389691f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
389791f72e9cSJoe Perches				$post_declare_space = $1;
389891f72e9cSJoe Perches				$declare = rtrim($declare);
389991f72e9cSJoe Perches			}
390091f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
390131070b5dSJoe Perches				WARN("SPACING",
390231070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
390391f72e9cSJoe Perches				$post_declare_space = " ";
390431070b5dSJoe Perches			}
390531070b5dSJoe Perches
390631070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
390791f72e9cSJoe Perches# This test is not currently implemented because these declarations are
390891f72e9cSJoe Perches# equivalent to
390991f72e9cSJoe Perches#	int  foo(int bar, ...)
391091f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
391191f72e9cSJoe Perches#
391291f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
391391f72e9cSJoe Perches#				WARN("SPACING",
391491f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
391591f72e9cSJoe Perches#			}
391631070b5dSJoe Perches
391731070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
391831070b5dSJoe Perches			if (defined $pre_pointer_space &&
391931070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
392031070b5dSJoe Perches				WARN("SPACING",
392131070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
392231070b5dSJoe Perches			}
392331070b5dSJoe Perches
392431070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
392531070b5dSJoe Perches			if (defined $post_pointer_space &&
392631070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
392731070b5dSJoe Perches				WARN("SPACING",
392831070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
392931070b5dSJoe Perches			}
393031070b5dSJoe Perches
393131070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
393231070b5dSJoe Perches			if (defined $post_funcname_space &&
393331070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
393431070b5dSJoe Perches				WARN("SPACING",
393531070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
393631070b5dSJoe Perches			}
393731070b5dSJoe Perches
393831070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
393931070b5dSJoe Perches			if (defined $pre_args_space &&
394031070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
394131070b5dSJoe Perches				WARN("SPACING",
394231070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
394331070b5dSJoe Perches			}
394431070b5dSJoe Perches
394531070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3946194f66fcSJoe Perches				$fixed[$fixlinenr] =~
394791f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
394831070b5dSJoe Perches			}
394931070b5dSJoe Perches		}
395031070b5dSJoe Perches
39518d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
39528d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3953fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3954fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
39558d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
39568d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
39578d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3958fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3959daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
39603705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
39613705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
39623705ce5bSJoe Perches				    $fix) {
3963194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
39643705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
39653705ce5bSJoe Perches				}
39668d31cfceSAndy Whitcroft			}
39678d31cfceSAndy Whitcroft		}
39688d31cfceSAndy Whitcroft
3969f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
39706c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3971c2fdda0dSAndy Whitcroft			my $name = $1;
3972773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3973773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3974c2fdda0dSAndy Whitcroft
3975c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3976773647a0SAndy Whitcroft			if ($name =~ /^(?:
3977773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3978773647a0SAndy Whitcroft				volatile|__volatile__|
3979773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3980773647a0SAndy Whitcroft				asm|__asm__)$/x)
3981773647a0SAndy Whitcroft			{
3982c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3983c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3984c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3985c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3986773647a0SAndy Whitcroft
3987773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3988c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3989c2fdda0dSAndy Whitcroft
3990c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3991c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3992773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3993c2fdda0dSAndy Whitcroft
3994c2fdda0dSAndy Whitcroft			} else {
39953705ce5bSJoe Perches				if (WARN("SPACING",
39963705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
39973705ce5bSJoe Perches					     $fix) {
3998194f66fcSJoe Perches					$fixed[$fixlinenr] =~
39993705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
40003705ce5bSJoe Perches				}
4001f0a594c1SAndy Whitcroft			}
40026c72ffaaSAndy Whitcroft		}
40039a4cad4eSEric Nelson
4004653d4876SAndy Whitcroft# Check operator spacing.
40050a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
40063705ce5bSJoe Perches			my $fixed_line = "";
40073705ce5bSJoe Perches			my $line_fixed = 0;
40083705ce5bSJoe Perches
40099c0ca6f9SAndy Whitcroft			my $ops = qr{
40109c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
40119c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
40129c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
40131f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
401484731623SJoe Perches				\?:|\?|:
40159c0ca6f9SAndy Whitcroft			}x;
4016cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
40173705ce5bSJoe Perches
40183705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
40193705ce5bSJoe Perches##			foreach my $el (@elements) {
40203705ce5bSJoe Perches##				print("el: <$el>\n");
40213705ce5bSJoe Perches##			}
40223705ce5bSJoe Perches
40233705ce5bSJoe Perches			my @fix_elements = ();
402400df344fSAndy Whitcroft			my $off = 0;
40256c72ffaaSAndy Whitcroft
40263705ce5bSJoe Perches			foreach my $el (@elements) {
40273705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
40283705ce5bSJoe Perches				$off += length($el);
40293705ce5bSJoe Perches			}
40303705ce5bSJoe Perches
40313705ce5bSJoe Perches			$off = 0;
40323705ce5bSJoe Perches
40336c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
4034b34c648bSJoe Perches			my $last_after = -1;
40356c72ffaaSAndy Whitcroft
40360a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
40373705ce5bSJoe Perches
40383705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
40393705ce5bSJoe Perches
40403705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
40413705ce5bSJoe Perches
40424a0df2efSAndy Whitcroft				$off += length($elements[$n]);
40434a0df2efSAndy Whitcroft
404425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
4045773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
4046773647a0SAndy Whitcroft				my $cc = '';
4047773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4048773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
4049773647a0SAndy Whitcroft				}
4050773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
4051773647a0SAndy Whitcroft
40524a0df2efSAndy Whitcroft				my $a = '';
40534a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
40544a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4055cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
40564a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
40574a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4058773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
40594a0df2efSAndy Whitcroft
40600a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
40614a0df2efSAndy Whitcroft
40624a0df2efSAndy Whitcroft				my $c = '';
40630a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
40644a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
40654a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4066cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
40674a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
40684a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
40698b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
40704a0df2efSAndy Whitcroft				} else {
40714a0df2efSAndy Whitcroft					$c = 'E';
40720a920b5bSAndy Whitcroft				}
40730a920b5bSAndy Whitcroft
40744a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
40754a0df2efSAndy Whitcroft
40764a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
40774a0df2efSAndy Whitcroft
40786c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4079de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
40800a920b5bSAndy Whitcroft
408174048ed8SAndy Whitcroft				# Pull out the value of this operator.
40826c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
40830a920b5bSAndy Whitcroft
40841f65f947SAndy Whitcroft				# Get the full operator variant.
40851f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
40861f65f947SAndy Whitcroft
408713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
408813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4089d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
409013214adfSAndy Whitcroft
4091cf655043SAndy Whitcroft#				# Ignore comments
4092cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
409313214adfSAndy Whitcroft
4094d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
409513214adfSAndy Whitcroft				} elsif ($op eq ';') {
4096cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4097cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
40983705ce5bSJoe Perches						if (ERROR("SPACING",
40993705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4100b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
41013705ce5bSJoe Perches							$line_fixed = 1;
41023705ce5bSJoe Perches						}
4103d8aaf121SAndy Whitcroft					}
4104d8aaf121SAndy Whitcroft
4105d8aaf121SAndy Whitcroft				# // is a comment
4106d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
41070a920b5bSAndy Whitcroft
4108b00e4814SJoe Perches				#   :   when part of a bitfield
4109b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4110b00e4814SJoe Perches					# skip the bitfield test for now
4111b00e4814SJoe Perches
41121f65f947SAndy Whitcroft				# No spaces for:
41131f65f947SAndy Whitcroft				#   ->
4114b00e4814SJoe Perches				} elsif ($op eq '->') {
41154a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
41163705ce5bSJoe Perches						if (ERROR("SPACING",
41173705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4118b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
41193705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
41203705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
41213705ce5bSJoe Perches							}
4122b34c648bSJoe Perches							$line_fixed = 1;
41233705ce5bSJoe Perches						}
41240a920b5bSAndy Whitcroft					}
41250a920b5bSAndy Whitcroft
41262381097bSJoe Perches				# , must not have a space before and must have a space on the right.
41270a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
41282381097bSJoe Perches					my $rtrim_before = 0;
41292381097bSJoe Perches					my $space_after = 0;
41302381097bSJoe Perches					if ($ctx =~ /Wx./) {
41312381097bSJoe Perches						if (ERROR("SPACING",
41322381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
41332381097bSJoe Perches							$line_fixed = 1;
41342381097bSJoe Perches							$rtrim_before = 1;
41352381097bSJoe Perches						}
41362381097bSJoe Perches					}
4137cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
41383705ce5bSJoe Perches						if (ERROR("SPACING",
41393705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
41403705ce5bSJoe Perches							$line_fixed = 1;
4141b34c648bSJoe Perches							$last_after = $n;
41422381097bSJoe Perches							$space_after = 1;
41432381097bSJoe Perches						}
41442381097bSJoe Perches					}
41452381097bSJoe Perches					if ($rtrim_before || $space_after) {
41462381097bSJoe Perches						if ($rtrim_before) {
41472381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
41482381097bSJoe Perches						} else {
41492381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
41502381097bSJoe Perches						}
41512381097bSJoe Perches						if ($space_after) {
41522381097bSJoe Perches							$good .= " ";
41533705ce5bSJoe Perches						}
41540a920b5bSAndy Whitcroft					}
41550a920b5bSAndy Whitcroft
41569c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
415774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
41589c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
41599c0ca6f9SAndy Whitcroft
41609c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
41619c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
41629c0ca6f9SAndy Whitcroft				# unary operator, or a cast
41639c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
416474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
41650d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4166cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
41673705ce5bSJoe Perches						if (ERROR("SPACING",
41683705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4169b34c648bSJoe Perches							if ($n != $last_after + 2) {
4170b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
41713705ce5bSJoe Perches								$line_fixed = 1;
41723705ce5bSJoe Perches							}
41730a920b5bSAndy Whitcroft						}
4174b34c648bSJoe Perches					}
4175a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4176171ae1a4SAndy Whitcroft						# A unary '*' may be const
4177171ae1a4SAndy Whitcroft
4178171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
41793705ce5bSJoe Perches						if (ERROR("SPACING",
41803705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4181b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
41823705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
41833705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
41843705ce5bSJoe Perches							}
4185b34c648bSJoe Perches							$line_fixed = 1;
41863705ce5bSJoe Perches						}
41870a920b5bSAndy Whitcroft					}
41880a920b5bSAndy Whitcroft
41890a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
41900a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4191773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
41923705ce5bSJoe Perches						if (ERROR("SPACING",
41933705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4194b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
41953705ce5bSJoe Perches							$line_fixed = 1;
41963705ce5bSJoe Perches						}
41970a920b5bSAndy Whitcroft					}
4198773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4199773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
42003705ce5bSJoe Perches						if (ERROR("SPACING",
42013705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4202b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42033705ce5bSJoe Perches							$line_fixed = 1;
42043705ce5bSJoe Perches						}
4205653d4876SAndy Whitcroft					}
4206773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
42073705ce5bSJoe Perches						if (ERROR("SPACING",
42083705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4209b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
42103705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
42113705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4212773647a0SAndy Whitcroft							}
4213b34c648bSJoe Perches							$line_fixed = 1;
42143705ce5bSJoe Perches						}
42153705ce5bSJoe Perches					}
42160a920b5bSAndy Whitcroft
42170a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
42189c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
42199c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
42209c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4221c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4222c2fdda0dSAndy Whitcroft					 $op eq '%')
42230a920b5bSAndy Whitcroft				{
4224d2e025f3SJoe Perches					if ($check) {
4225d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4226d2e025f3SJoe Perches							if (CHK("SPACING",
4227d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4228d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4229d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4230d2e025f3SJoe Perches								$line_fixed = 1;
4231d2e025f3SJoe Perches							}
4232d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4233d2e025f3SJoe Perches							if (CHK("SPACING",
4234d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4235d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4236d2e025f3SJoe Perches								$line_fixed = 1;
4237d2e025f3SJoe Perches							}
4238d2e025f3SJoe Perches						}
4239d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
42403705ce5bSJoe Perches						if (ERROR("SPACING",
42413705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4242b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4243b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4244b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4245b34c648bSJoe Perches							}
42463705ce5bSJoe Perches							$line_fixed = 1;
42473705ce5bSJoe Perches						}
42480a920b5bSAndy Whitcroft					}
42490a920b5bSAndy Whitcroft
42501f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
42511f65f947SAndy Whitcroft				# terminating a case value or a label.
42521f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
42531f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
42543705ce5bSJoe Perches						if (ERROR("SPACING",
42553705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4256b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
42573705ce5bSJoe Perches							$line_fixed = 1;
42583705ce5bSJoe Perches						}
42591f65f947SAndy Whitcroft					}
42601f65f947SAndy Whitcroft
42610a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4262cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
42631f65f947SAndy Whitcroft					my $ok = 0;
42641f65f947SAndy Whitcroft
426522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
42661f65f947SAndy Whitcroft					if (($op eq '<' &&
42671f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
42681f65f947SAndy Whitcroft					    ($op eq '>' &&
42691f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
42701f65f947SAndy Whitcroft					{
42711f65f947SAndy Whitcroft					    	$ok = 1;
42721f65f947SAndy Whitcroft					}
42731f65f947SAndy Whitcroft
4274e0df7e1fSJoe Perches					# for asm volatile statements
4275e0df7e1fSJoe Perches					# ignore a colon with another
4276e0df7e1fSJoe Perches					# colon immediately before or after
4277e0df7e1fSJoe Perches					if (($op eq ':') &&
4278e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4279e0df7e1fSJoe Perches						$ok = 1;
4280e0df7e1fSJoe Perches					}
4281e0df7e1fSJoe Perches
428284731623SJoe Perches					# messages are ERROR, but ?: are CHK
42831f65f947SAndy Whitcroft					if ($ok == 0) {
428484731623SJoe Perches						my $msg_type = \&ERROR;
428584731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
428684731623SJoe Perches
428784731623SJoe Perches						if (&{$msg_type}("SPACING",
42883705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
4289b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4290b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4291b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4292b34c648bSJoe Perches							}
42933705ce5bSJoe Perches							$line_fixed = 1;
42943705ce5bSJoe Perches						}
42950a920b5bSAndy Whitcroft					}
429622f2a2efSAndy Whitcroft				}
42974a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
42983705ce5bSJoe Perches
42993705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
43003705ce5bSJoe Perches
43013705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
43020a920b5bSAndy Whitcroft			}
43033705ce5bSJoe Perches
43043705ce5bSJoe Perches			if (($#elements % 2) == 0) {
43053705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
43063705ce5bSJoe Perches			}
43073705ce5bSJoe Perches
4308194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4309194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
43103705ce5bSJoe Perches			}
43113705ce5bSJoe Perches
43123705ce5bSJoe Perches
43130a920b5bSAndy Whitcroft		}
43140a920b5bSAndy Whitcroft
4315786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4316d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4317786b6326SJoe Perches			if (WARN("SPACING",
4318786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4319786b6326SJoe Perches			    $fix) {
4320194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4321786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4322786b6326SJoe Perches			}
4323786b6326SJoe Perches		}
4324786b6326SJoe Perches
4325f0a594c1SAndy Whitcroft# check for multiple assignments
4326f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4327000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4328000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4329f0a594c1SAndy Whitcroft		}
4330f0a594c1SAndy Whitcroft
433122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
433222f2a2efSAndy Whitcroft## # continuation.
433322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
433422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
433522f2a2efSAndy Whitcroft##
433622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
433722f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
433822f2a2efSAndy Whitcroft## 			my $ln = $line;
433922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
434022f2a2efSAndy Whitcroft## 			}
434122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4342000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4343000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
434422f2a2efSAndy Whitcroft## 			}
434522f2a2efSAndy Whitcroft## 		}
4346f0a594c1SAndy Whitcroft
43470a920b5bSAndy Whitcroft#need space before brace following if, while, etc
43486b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
43494e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
43503705ce5bSJoe Perches			if (ERROR("SPACING",
43513705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
43523705ce5bSJoe Perches			    $fix) {
4353194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
43543705ce5bSJoe Perches			}
4355de7d4f0eSAndy Whitcroft		}
4356de7d4f0eSAndy Whitcroft
4357c4a62ef9SJoe Perches## # check for blank lines before declarations
4358c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4359c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4360c4a62ef9SJoe Perches##			WARN("SPACING",
4361c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4362c4a62ef9SJoe Perches##		}
4363c4a62ef9SJoe Perches##
4364c4a62ef9SJoe Perches
4365de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4366de7d4f0eSAndy Whitcroft# on the line
4367de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4368d5e616fcSJoe Perches			if (ERROR("SPACING",
4369d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4370d5e616fcSJoe Perches			    $fix) {
4371194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4372d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4373d5e616fcSJoe Perches			}
43740a920b5bSAndy Whitcroft		}
43750a920b5bSAndy Whitcroft
437622f2a2efSAndy Whitcroft# check spacing on square brackets
437722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
43783705ce5bSJoe Perches			if (ERROR("SPACING",
43793705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
43803705ce5bSJoe Perches			    $fix) {
4381194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43823705ce5bSJoe Perches				    s/\[\s+/\[/;
43833705ce5bSJoe Perches			}
438422f2a2efSAndy Whitcroft		}
438522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
43863705ce5bSJoe Perches			if (ERROR("SPACING",
43873705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
43883705ce5bSJoe Perches			    $fix) {
4389194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43903705ce5bSJoe Perches				    s/\s+\]/\]/;
43913705ce5bSJoe Perches			}
439222f2a2efSAndy Whitcroft		}
439322f2a2efSAndy Whitcroft
4394c45dcabdSAndy Whitcroft# check spacing on parentheses
43959c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
43969c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
43973705ce5bSJoe Perches			if (ERROR("SPACING",
43983705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
43993705ce5bSJoe Perches			    $fix) {
4400194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44013705ce5bSJoe Perches				    s/\(\s+/\(/;
44023705ce5bSJoe Perches			}
440322f2a2efSAndy Whitcroft		}
440413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4405c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4406c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
44073705ce5bSJoe Perches			if (ERROR("SPACING",
44083705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
44093705ce5bSJoe Perches			    $fix) {
4410194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44113705ce5bSJoe Perches				    s/\s+\)/\)/;
44123705ce5bSJoe Perches			}
441322f2a2efSAndy Whitcroft		}
441422f2a2efSAndy Whitcroft
4415e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4416e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4417e2826fd0SJoe Perches
4418e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4419ea4acbb1SJoe Perches			my $var = $1;
4420ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4421ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4422ea4acbb1SJoe Perches			    $fix) {
4423ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4424ea4acbb1SJoe Perches			}
4425ea4acbb1SJoe Perches		}
4426ea4acbb1SJoe Perches
4427ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4428ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4429ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4430ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4431ea4acbb1SJoe Perches			my $var = $2;
4432ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4433ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4434ea4acbb1SJoe Perches			    $fix) {
4435ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4436ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4437ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4438ea4acbb1SJoe Perches			}
4439e2826fd0SJoe Perches		}
4440e2826fd0SJoe Perches
44410a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
44424a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
44430a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
44443705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
44453705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
44463705ce5bSJoe Perches			    $fix) {
4447194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44483705ce5bSJoe Perches				    s/^(.)\s+/$1/;
44493705ce5bSJoe Perches			}
44500a920b5bSAndy Whitcroft		}
44510a920b5bSAndy Whitcroft
44525b9553abSJoe Perches# return is not a function
4453507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4454c45dcabdSAndy Whitcroft			my $spacing = $1;
4455507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
44565b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
44575b9553abSJoe Perches				my $value = $1;
44585b9553abSJoe Perches				$value = deparenthesize($value);
44595b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4460000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4461000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
44625b9553abSJoe Perches				}
4463c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4464000d1cc1SJoe Perches				ERROR("SPACING",
4465000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4466c45dcabdSAndy Whitcroft			}
4467c45dcabdSAndy Whitcroft		}
4468507e5141SJoe Perches
4469b43ae21bSJoe Perches# unnecessary return in a void function
4470b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4471b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4472b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4473b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4474b43ae21bSJoe Perches		    $linenr >= 3 &&
4475b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4476b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
44779819cf25SJoe Perches			WARN("RETURN_VOID",
4478b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
44799819cf25SJoe Perches               }
44809819cf25SJoe Perches
4481189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4482189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4483189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4484189248d8SJoe Perches			my $openparens = $1;
4485189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4486189248d8SJoe Perches			my $msg = "";
4487189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4488189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4489189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4490189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4491189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4492189248d8SJoe Perches			}
4493189248d8SJoe Perches		}
4494189248d8SJoe Perches
4495c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4496c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4497c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4498c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4499c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4500c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4501c5595fa2SJoe Perches			my $lead = $1;
4502c5595fa2SJoe Perches			my $const = $2;
4503c5595fa2SJoe Perches			my $comp = $3;
4504c5595fa2SJoe Perches			my $to = $4;
4505c5595fa2SJoe Perches			my $newcomp = $comp;
4506f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4507c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4508c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4509c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4510c5595fa2SJoe Perches			    $fix) {
4511c5595fa2SJoe Perches				if ($comp eq "<") {
4512c5595fa2SJoe Perches					$newcomp = ">";
4513c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4514c5595fa2SJoe Perches					$newcomp = ">=";
4515c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4516c5595fa2SJoe Perches					$newcomp = "<";
4517c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4518c5595fa2SJoe Perches					$newcomp = "<=";
4519c5595fa2SJoe Perches				}
4520c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4521c5595fa2SJoe Perches			}
4522c5595fa2SJoe Perches		}
4523c5595fa2SJoe Perches
4524f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4525f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
452653a3c448SAndy Whitcroft			my $name = $1;
452753a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4528000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4529f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
453053a3c448SAndy Whitcroft			}
453153a3c448SAndy Whitcroft		}
4532c45dcabdSAndy Whitcroft
45330a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
45344a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
45353705ce5bSJoe Perches			if (ERROR("SPACING",
45363705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
45373705ce5bSJoe Perches			    $fix) {
4538194f66fcSJoe Perches				$fixed[$fixlinenr] =~
45393705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
45403705ce5bSJoe Perches			}
45410a920b5bSAndy Whitcroft		}
45420a920b5bSAndy Whitcroft
4543f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4544f5fe35ddSAndy Whitcroft# statements after the conditional.
4545170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
45463e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
45473e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
45483e469cdcSAndy Whitcroft					if (!defined $stat);
4549170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4550170d3a22SAndy Whitcroft						$remain_next, $off_next);
4551170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4552170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4553170d3a22SAndy Whitcroft
4554170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4555170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4556170d3a22SAndy Whitcroft				# then count those as offsets.
4557170d3a22SAndy Whitcroft				my ($whitespace) =
4558170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4559170d3a22SAndy Whitcroft				my $offset =
4560170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4561170d3a22SAndy Whitcroft
4562170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4563170d3a22SAndy Whitcroft								$offset} = 1;
4564170d3a22SAndy Whitcroft			}
4565170d3a22SAndy Whitcroft		}
4566170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4567c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4568170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4569171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
45708905a67cSAndy Whitcroft
4571b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4572000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4573000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
45748905a67cSAndy Whitcroft			}
45758905a67cSAndy Whitcroft
45768905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
45778905a67cSAndy Whitcroft			# conditional.
4578773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
45798905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
458013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
458153210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
458253210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4583773647a0SAndy Whitcroft			{
4584bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4585bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4586bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
458742bdf74cSHidetoshi Seto				my $stat_real = '';
4588bb44ad39SAndy Whitcroft
458942bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
459042bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4591bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4592bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4593bb44ad39SAndy Whitcroft				}
4594bb44ad39SAndy Whitcroft
4595000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4596000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
45978905a67cSAndy Whitcroft			}
45988905a67cSAndy Whitcroft		}
45998905a67cSAndy Whitcroft
460013214adfSAndy Whitcroft# Check for bitwise tests written as boolean
460113214adfSAndy Whitcroft		if ($line =~ /
460213214adfSAndy Whitcroft			(?:
460313214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
460413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
460513214adfSAndy Whitcroft				(?:\&\&|\|\|)
460613214adfSAndy Whitcroft			|
460713214adfSAndy Whitcroft				(?:\&\&|\|\|)
460813214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
460913214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
461013214adfSAndy Whitcroft			)/x)
461113214adfSAndy Whitcroft		{
4612000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4613000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
461413214adfSAndy Whitcroft		}
461513214adfSAndy Whitcroft
46168905a67cSAndy Whitcroft# if and else should not have general statements after it
461713214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
461813214adfSAndy Whitcroft			my $s = $1;
461913214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
462013214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4621000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4622000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
46230a920b5bSAndy Whitcroft			}
462413214adfSAndy Whitcroft		}
462539667782SAndy Whitcroft# if should not continue a brace
462639667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4627000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4628048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
462939667782SAndy Whitcroft				$herecurr);
463039667782SAndy Whitcroft		}
4631a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4632a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4633a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
46343fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4635a1080bf8SAndy Whitcroft			\s*return\s+
4636a1080bf8SAndy Whitcroft		    )/xg)
4637a1080bf8SAndy Whitcroft		{
4638000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4639000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4640a1080bf8SAndy Whitcroft		}
46410a920b5bSAndy Whitcroft
46420a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
46430a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
46448b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
46450a920b5bSAndy Whitcroft		    $previndent == $indent) {
46468b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
46478b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
46488b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
46498b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
46508b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
46518b8856f4SJoe Perches				my $fixedline = $prevrawline;
46528b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
46538b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
46548b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
46558b8856f4SJoe Perches				}
46568b8856f4SJoe Perches				$fixedline = $rawline;
46578b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
46588b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
46598b8856f4SJoe Perches			}
46600a920b5bSAndy Whitcroft		}
46610a920b5bSAndy Whitcroft
46628b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4663c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4664c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4665c2fdda0dSAndy Whitcroft
4666c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4667c2fdda0dSAndy Whitcroft			# conditional.
4668773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4669c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4670c2fdda0dSAndy Whitcroft
4671c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
46728b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
46738b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
46748b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
46758b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
46768b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
46778b8856f4SJoe Perches					my $fixedline = $prevrawline;
46788b8856f4SJoe Perches					my $trailing = $rawline;
46798b8856f4SJoe Perches					$trailing =~ s/^\+//;
46808b8856f4SJoe Perches					$trailing = trim($trailing);
46818b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
46828b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
46838b8856f4SJoe Perches				}
4684c2fdda0dSAndy Whitcroft			}
4685c2fdda0dSAndy Whitcroft		}
4686c2fdda0dSAndy Whitcroft
468795e2c602SJoe Perches#Specific variable tests
4688323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4689323c1260SJoe Perches			my $var = $1;
469095e2c602SJoe Perches
469195e2c602SJoe Perches#gcc binary extension
469295e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4693d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4694d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4695d5e616fcSJoe Perches				    $fix) {
4696d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4697194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4698d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4699d5e616fcSJoe Perches				}
470095e2c602SJoe Perches			}
470195e2c602SJoe Perches
470295e2c602SJoe Perches#CamelCase
4703807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4704be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
470522735ce8SJoe Perches#Ignore Page<foo> variants
4706807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
470722735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4708f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4709f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4710f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
47117e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
47127e781f67SJoe Perches					my $word = $1;
47137e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4714d8b07710SJoe Perches					if ($check) {
4715d8b07710SJoe Perches						seed_camelcase_includes();
4716d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4717d8b07710SJoe Perches							seed_camelcase_file($realfile);
4718d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4719d8b07710SJoe Perches						}
4720d8b07710SJoe Perches					}
47217e781f67SJoe Perches					if (!defined $camelcase{$word}) {
47227e781f67SJoe Perches						$camelcase{$word} = 1;
4723be79794bSJoe Perches						CHK("CAMELCASE",
47247e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
47257e781f67SJoe Perches					}
4726323c1260SJoe Perches				}
4727323c1260SJoe Perches			}
47283445686aSJoe Perches		}
47290a920b5bSAndy Whitcroft
47300a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4731d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4732d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4733d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4734d5e616fcSJoe Perches			    $fix) {
4735194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4736d5e616fcSJoe Perches			}
47370a920b5bSAndy Whitcroft		}
47380a920b5bSAndy Whitcroft
47390e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
47400e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4741c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4742e09dec48SAndy Whitcroft			my $file = "$1.h";
4743e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4744e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4745e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
47467840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4747c45dcabdSAndy Whitcroft			{
47480e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
47490e212e0aSFabian Frederick				if ($asminclude > 0) {
4750e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4751000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4752000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4753e09dec48SAndy Whitcroft					} else {
4754000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4755000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4756e09dec48SAndy Whitcroft					}
47570a920b5bSAndy Whitcroft				}
47580a920b5bSAndy Whitcroft			}
47590e212e0aSFabian Frederick		}
47600a920b5bSAndy Whitcroft
4761653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4762653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4763cf655043SAndy Whitcroft# in a known good container
4764b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4765b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4766d8aaf121SAndy Whitcroft			my $ln = $linenr;
4767d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4768c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4769c45dcabdSAndy Whitcroft			my $ctx = '';
477008a2843eSJoe Perches			my $has_flow_statement = 0;
477108a2843eSJoe Perches			my $has_arg_concat = 0;
4772c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4773f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4774f74bd194SAndy Whitcroft			$ctx = $dstat;
4775c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4776a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4777c45dcabdSAndy Whitcroft
477808a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
477962e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
478008a2843eSJoe Perches
4781f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4782f59b64bfSJoe Perches			my $define_args = $1;
4783f59b64bfSJoe Perches			my $define_stmt = $dstat;
4784f59b64bfSJoe Perches			my @def_args = ();
4785f59b64bfSJoe Perches
4786f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
4787f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
4788f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
4789f59b64bfSJoe Perches				@def_args = split(",", $define_args);
4790f59b64bfSJoe Perches			}
4791f59b64bfSJoe Perches
4792292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4793c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4794c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4795c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4796c45dcabdSAndy Whitcroft
4797c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4798bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4799bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
48006b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4801bf30d6edSAndy Whitcroft			{
4802c45dcabdSAndy Whitcroft			}
4803c45dcabdSAndy Whitcroft
4804e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
480533acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
480633acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4807e45bab8eSAndy Whitcroft			{
4808e45bab8eSAndy Whitcroft			}
4809e45bab8eSAndy Whitcroft
481042e15293SJoe Perches			# Make asm volatile uses seem like a generic function
481142e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
481242e15293SJoe Perches
4813c45dcabdSAndy Whitcroft			my $exceptions = qr{
4814c45dcabdSAndy Whitcroft				$Declare|
4815c45dcabdSAndy Whitcroft				module_param_named|
4816a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4817c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4818c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4819383099fdSAndy Whitcroft				__typeof__\(|
482022fd2d3eSStefani Seibold				union|
482122fd2d3eSStefani Seibold				struct|
4822ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
48236b10df42SVladimir Zapolskiy				^\"|\"$|
48246b10df42SVladimir Zapolskiy				^\[
4825c45dcabdSAndy Whitcroft			}x;
48265eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4827f59b64bfSJoe Perches
4828f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
4829f59b64bfSJoe Perches			my $herectx = $here . "\n";
4830f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
4831f59b64bfSJoe Perches
4832f59b64bfSJoe Perches			for (my $n = 0; $n < $stmt_cnt; $n++) {
4833f59b64bfSJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
4834f59b64bfSJoe Perches			}
4835f59b64bfSJoe Perches
4836f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4837f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4838f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
48393cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4840356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4841f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4842f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4843e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
484472f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4845f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4846f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4847f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
48484e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4849f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4850c45dcabdSAndy Whitcroft			{
4851c45dcabdSAndy Whitcroft
4852f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4853f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4854f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4855f74bd194SAndy Whitcroft				} else {
4856000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4857388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4858d8aaf121SAndy Whitcroft				}
4859f59b64bfSJoe Perches
4860f59b64bfSJoe Perches			}
48615207649bSJoe Perches
48625207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
48635207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
48645207649bSJoe Perches			my $first = 1;
48655207649bSJoe Perches			$define_stmt = "";
48665207649bSJoe Perches			foreach my $l (@stmt_array) {
48675207649bSJoe Perches				$l =~ s/\\$//;
48685207649bSJoe Perches				if ($first) {
48695207649bSJoe Perches					$define_stmt = $l;
48705207649bSJoe Perches					$first = 0;
48715207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
48725207649bSJoe Perches					$define_stmt .= substr($l, 1);
48735207649bSJoe Perches				}
48745207649bSJoe Perches			}
48755207649bSJoe Perches			$define_stmt =~ s/$;//g;
48765207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
48775207649bSJoe Perches			$define_stmt = trim($define_stmt);
48785207649bSJoe Perches
4879f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
4880f59b64bfSJoe Perches			foreach my $arg (@def_args) {
4881f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
48829192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
4883f59b64bfSJoe Perches				my $tmp = $define_stmt;
4884f59b64bfSJoe Perches				$tmp =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
48855207649bSJoe Perches				$tmp =~ s/\#+\s*$arg\b//g;
4886f59b64bfSJoe Perches				$tmp =~ s/\b$arg\s*\#\#//g;
4887f59b64bfSJoe Perches				my $use_cnt = $tmp =~ s/\b$arg\b//g;
4888f59b64bfSJoe Perches				if ($use_cnt > 1) {
4889f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
4890f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
4891f59b64bfSJoe Perches				    }
48929192d41aSJoe Perches# check if any macro arguments may have other precedence issues
48939192d41aSJoe Perches				if ($define_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
48949192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
48959192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
48969192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
48979192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
48989192d41aSJoe Perches				}
48990a920b5bSAndy Whitcroft			}
49005023d347SJoe Perches
490108a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
490208a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
490308a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
490408a2843eSJoe Perches				my $herectx = $here . "\n";
490508a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
490608a2843eSJoe Perches
490708a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
490808a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
490908a2843eSJoe Perches				}
491008a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
491108a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
491208a2843eSJoe Perches			}
491308a2843eSJoe Perches
4914481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
49155023d347SJoe Perches
49165023d347SJoe Perches		} else {
49175023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4918481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4919481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
49205023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
49215023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
49225023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
49235023d347SJoe Perches			}
4924653d4876SAndy Whitcroft		}
49250a920b5bSAndy Whitcroft
4926b13edf7fSJoe Perches# do {} while (0) macro tests:
4927b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4928b13edf7fSJoe Perches# macro should not end with a semicolon
4929b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4930b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4931b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4932b13edf7fSJoe Perches			my $ln = $linenr;
4933b13edf7fSJoe Perches			my $cnt = $realcnt;
4934b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4935b13edf7fSJoe Perches			my $ctx = '';
4936b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4937b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4938b13edf7fSJoe Perches			$ctx = $dstat;
4939b13edf7fSJoe Perches
4940b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
49411b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4942b13edf7fSJoe Perches
4943b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4944b13edf7fSJoe Perches				my $stmts = $2;
4945b13edf7fSJoe Perches				my $semis = $3;
4946b13edf7fSJoe Perches
4947b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4948b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4949b13edf7fSJoe Perches				my $herectx = $here . "\n";
4950b13edf7fSJoe Perches
4951b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4952b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4953b13edf7fSJoe Perches				}
4954b13edf7fSJoe Perches
4955ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4956ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4957b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4958b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4959b13edf7fSJoe Perches				}
4960b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4961b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4962b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4963b13edf7fSJoe Perches				}
4964f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4965f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4966f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4967f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4968f5ef95b1SJoe Perches
4969f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4970f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4971f5ef95b1SJoe Perches				}
4972f5ef95b1SJoe Perches
4973f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4974f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4975b13edf7fSJoe Perches			}
4976b13edf7fSJoe Perches		}
4977b13edf7fSJoe Perches
4978080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4979080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4980080ba929SMike Frysinger#	.
4981080ba929SMike Frysinger#	ALIGN(...)
4982080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4983080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4984000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4985000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4986080ba929SMike Frysinger		}
4987080ba929SMike Frysinger
4988f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
498913214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
499013214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4991cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
499213214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4993cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4994cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4995aad4f614SJoe Perches				my @allowed = ();
4996aad4f614SJoe Perches				my $allow = 0;
499713214adfSAndy Whitcroft				my $seen = 0;
4998773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4999cf655043SAndy Whitcroft				my $ln = $linenr - 1;
500013214adfSAndy Whitcroft				for my $chunk (@chunks) {
500113214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
500213214adfSAndy Whitcroft
5003773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
5004773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5005773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
5006773647a0SAndy Whitcroft
5007aad4f614SJoe Perches					$allowed[$allow] = 0;
5008773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5009773647a0SAndy Whitcroft
5010773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
5011773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
5012773647a0SAndy Whitcroft
5013773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5014cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
5015cf655043SAndy Whitcroft
5016773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
501713214adfSAndy Whitcroft
501813214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
501913214adfSAndy Whitcroft
5020aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5021cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
5022cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
5023aad4f614SJoe Perches						$allowed[$allow] = 1;
502413214adfSAndy Whitcroft					}
502513214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
5026cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
5027aad4f614SJoe Perches						$allowed[$allow] = 1;
502813214adfSAndy Whitcroft					}
5029cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
5030cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
5031aad4f614SJoe Perches						$allowed[$allow] = 1;
503213214adfSAndy Whitcroft					}
5033aad4f614SJoe Perches					$allow++;
503413214adfSAndy Whitcroft				}
5035aad4f614SJoe Perches				if ($seen) {
5036aad4f614SJoe Perches					my $sum_allowed = 0;
5037aad4f614SJoe Perches					foreach (@allowed) {
5038aad4f614SJoe Perches						$sum_allowed += $_;
5039aad4f614SJoe Perches					}
5040aad4f614SJoe Perches					if ($sum_allowed == 0) {
5041000d1cc1SJoe Perches						WARN("BRACES",
5042000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5043aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
5044aad4f614SJoe Perches						 $seen != $allow) {
5045aad4f614SJoe Perches						CHK("BRACES",
5046aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
5047aad4f614SJoe Perches					}
504813214adfSAndy Whitcroft				}
504913214adfSAndy Whitcroft			}
505013214adfSAndy Whitcroft		}
5051773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
505213214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
5053cf655043SAndy Whitcroft			my $allowed = 0;
5054f0a594c1SAndy Whitcroft
5055cf655043SAndy Whitcroft			# Check the pre-context.
5056cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5057cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
5058cf655043SAndy Whitcroft				$allowed = 1;
5059f0a594c1SAndy Whitcroft			}
5060773647a0SAndy Whitcroft
5061773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
5062773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
5063773647a0SAndy Whitcroft
5064cf655043SAndy Whitcroft			# Check the condition.
5065cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
5066773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
5067cf655043SAndy Whitcroft			if (defined $cond) {
5068773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
5069cf655043SAndy Whitcroft			}
5070cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
5071cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
5072cf655043SAndy Whitcroft				$allowed = 1;
5073cf655043SAndy Whitcroft			}
5074cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
5075cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
5076cf655043SAndy Whitcroft				$allowed = 1;
5077cf655043SAndy Whitcroft			}
5078cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
5079cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
5080cf655043SAndy Whitcroft				$allowed = 1;
5081cf655043SAndy Whitcroft			}
5082cf655043SAndy Whitcroft			# Check the post-context.
5083cf655043SAndy Whitcroft			if (defined $chunks[1]) {
5084cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
5085cf655043SAndy Whitcroft				if (defined $cond) {
5086773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
5087cf655043SAndy Whitcroft				}
5088cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
5089cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
5090cf655043SAndy Whitcroft					$allowed = 1;
5091cf655043SAndy Whitcroft				}
5092cf655043SAndy Whitcroft			}
5093cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
509469932487SJustin P. Mattock				my $herectx = $here . "\n";
5095f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
5096cf655043SAndy Whitcroft
5097f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
509869932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
5099cf655043SAndy Whitcroft				}
5100cf655043SAndy Whitcroft
5101000d1cc1SJoe Perches				WARN("BRACES",
5102000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
5103f0a594c1SAndy Whitcroft			}
5104f0a594c1SAndy Whitcroft		}
5105f0a594c1SAndy Whitcroft
51060979ae66SJoe Perches# check for unnecessary blank lines around braces
510777b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5108f8e58219SJoe Perches			if (CHK("BRACES",
5109f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5110f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5111f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5112f8e58219SJoe Perches			}
51130979ae66SJoe Perches		}
511477b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5115f8e58219SJoe Perches			if (CHK("BRACES",
5116f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5117f8e58219SJoe Perches			    $fix) {
5118f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5119f8e58219SJoe Perches			}
51200979ae66SJoe Perches		}
51210979ae66SJoe Perches
51224a0df2efSAndy Whitcroft# no volatiles please
51236c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
51246c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5125000d1cc1SJoe Perches			WARN("VOLATILE",
51268c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
51274a0df2efSAndy Whitcroft		}
51284a0df2efSAndy Whitcroft
51295e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
51305e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
51315e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
51325e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
513333acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
51345e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
51355e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
51365e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
51375e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
51385e4f6ba5SJoe Perches				     $fix &&
51395e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
51405e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
51415e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
51425e4f6ba5SJoe Perches				my $comma_close = "";
51435e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
51445e4f6ba5SJoe Perches					$comma_close = $1;
51455e4f6ba5SJoe Perches				}
51465e4f6ba5SJoe Perches
51475e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
51485e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
51495e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
51505e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
51515e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
51525e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
51535e4f6ba5SJoe Perches				$fixedline = $rawline;
51545e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
51555e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
51565e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
51575e4f6ba5SJoe Perches				}
51585e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
51595e4f6ba5SJoe Perches			}
51605e4f6ba5SJoe Perches		}
51615e4f6ba5SJoe Perches
51625e4f6ba5SJoe Perches# check for missing a space in a string concatenation
51635e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
51645e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
51655e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
51665e4f6ba5SJoe Perches		}
51675e4f6ba5SJoe Perches
5168*77cb8546SJoe Perches#check for an embedded function name in a string when the function is known
5169*77cb8546SJoe Perches# as part of a diff.  This does not work for -f --file checking as it
5170*77cb8546SJoe Perches#depends on patch context providing the function name
5171*77cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
5172*77cb8546SJoe Perches		    defined($context_function) &&
5173*77cb8546SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/) {
5174*77cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
5175*77cb8546SJoe Perches			     "Prefer using \"%s\", __func__ to embedded function names\n" . $herecurr);
5176*77cb8546SJoe Perches		}
5177*77cb8546SJoe Perches
51785e4f6ba5SJoe Perches# check for spaces before a quoted newline
51795e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
51805e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
51815e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
51825e4f6ba5SJoe Perches			    $fix) {
51835e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
51845e4f6ba5SJoe Perches			}
51855e4f6ba5SJoe Perches
51865e4f6ba5SJoe Perches		}
51875e4f6ba5SJoe Perches
5188f17dba4fSJoe Perches# concatenated string without spaces between elements
518933acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5190f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
5191f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
5192f17dba4fSJoe Perches		}
5193f17dba4fSJoe Perches
519490ad30e5SJoe Perches# uncoalesced string fragments
519533acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
519690ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
519790ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
519890ad30e5SJoe Perches		}
519990ad30e5SJoe Perches
52006e300757SJoe Perches# check for %L{u,d,i} and 0x%[udi] in strings
52015e4f6ba5SJoe Perches		my $string;
52025e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
52035e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
52045e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
52056e300757SJoe Perches			if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
52065e4f6ba5SJoe Perches				WARN("PRINTF_L",
52075e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
52085e4f6ba5SJoe Perches				last;
52095e4f6ba5SJoe Perches			}
52106e300757SJoe Perches			if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
52116e300757SJoe Perches				ERROR("PRINTF_0xDECIMAL",
52126e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
52136e300757SJoe Perches			}
52145e4f6ba5SJoe Perches		}
52155e4f6ba5SJoe Perches
52165e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
52175e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
52185e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
52195e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
52205e4f6ba5SJoe Perches		}
52215e4f6ba5SJoe Perches
522200df344fSAndy Whitcroft# warn about #if 0
5223c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5224000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
5225000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
5226de7d4f0eSAndy Whitcroft				$herecurr);
52274a0df2efSAndy Whitcroft		}
52284a0df2efSAndy Whitcroft
522903df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
523003df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5231100425deSJoe Perches			my $tested = quotemeta($1);
5232100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5233100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5234100425deSJoe Perches				my $func = $1;
5235100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5236100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5237100425deSJoe Perches				    $fix) {
5238100425deSJoe Perches					my $do_fix = 1;
5239100425deSJoe Perches					my $leading_tabs = "";
5240100425deSJoe Perches					my $new_leading_tabs = "";
5241100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5242100425deSJoe Perches						$leading_tabs = $1;
5243100425deSJoe Perches					} else {
5244100425deSJoe Perches						$do_fix = 0;
5245100425deSJoe Perches					}
5246100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5247100425deSJoe Perches						$new_leading_tabs = $1;
5248100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5249100425deSJoe Perches							$do_fix = 0;
5250100425deSJoe Perches						}
5251100425deSJoe Perches					} else {
5252100425deSJoe Perches						$do_fix = 0;
5253100425deSJoe Perches					}
5254100425deSJoe Perches					if ($do_fix) {
5255100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5256100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5257100425deSJoe Perches					}
5258100425deSJoe Perches				}
52594c432a8fSGreg Kroah-Hartman			}
52604c432a8fSGreg Kroah-Hartman		}
5261f0a594c1SAndy Whitcroft
5262ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5263ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5264ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5265ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5266ebfdc409SJoe Perches		    $linenr > 3) {
5267ebfdc409SJoe Perches			my $testval = $2;
5268ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5269ebfdc409SJoe Perches
5270ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5271ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5272ebfdc409SJoe Perches
5273ebfdc409SJoe Perches			if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5274ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5275ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5276ebfdc409SJoe Perches			}
5277ebfdc409SJoe Perches		}
5278ebfdc409SJoe Perches
5279f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5280dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5281f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5282f78d98f6SJoe Perches			my $level = $1;
5283f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5284f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5285f78d98f6SJoe Perches			    $fix) {
5286f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5287f78d98f6SJoe Perches			}
5288f78d98f6SJoe Perches		}
5289f78d98f6SJoe Perches
5290abb08a53SJoe Perches# check for mask then right shift without a parentheses
5291abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5292abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5293abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5294abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5295abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5296abb08a53SJoe Perches		}
5297abb08a53SJoe Perches
5298b75ac618SJoe Perches# check for pointer comparisons to NULL
5299b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
5300b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5301b75ac618SJoe Perches				my $val = $1;
5302b75ac618SJoe Perches				my $equal = "!";
5303b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5304b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5305b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5306b75ac618SJoe Perches					    $fix) {
5307b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5308b75ac618SJoe Perches				}
5309b75ac618SJoe Perches			}
5310b75ac618SJoe Perches		}
5311b75ac618SJoe Perches
53128716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
53138716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
53148716de38SJoe Perches			my $attr = $1;
53158716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
53168716de38SJoe Perches				my $ptr = $1;
53178716de38SJoe Perches				my $var = $2;
53188716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
53198716de38SJoe Perches				      ERROR("MISPLACED_INIT",
53208716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
53218716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
53228716de38SJoe Perches				      WARN("MISPLACED_INIT",
53238716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
53248716de38SJoe Perches				    $fix) {
5325194f66fcSJoe 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;
53268716de38SJoe Perches				}
53278716de38SJoe Perches			}
53288716de38SJoe Perches		}
53298716de38SJoe Perches
5330e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5331e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5332e970b884SJoe Perches			my $attr = $1;
5333e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5334e970b884SJoe Perches			my $attr_prefix = $1;
5335e970b884SJoe Perches			my $attr_type = $2;
5336e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5337e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5338e970b884SJoe Perches			    $fix) {
5339194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5340e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5341e970b884SJoe Perches			}
5342e970b884SJoe Perches		}
5343e970b884SJoe Perches
5344e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5345e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5346e970b884SJoe Perches			my $attr = $1;
5347e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5348e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5349e970b884SJoe Perches			    $fix) {
5350194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5351e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5352e970b884SJoe Perches				$lead = rtrim($1);
5353e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5354e970b884SJoe Perches				$lead = "${lead}const ";
5355194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5356e970b884SJoe Perches			}
5357e970b884SJoe Perches		}
5358e970b884SJoe Perches
5359c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5360c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5361c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5362c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5363c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5364c17893c7SJoe Perches			    $fix) {
5365c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5366c17893c7SJoe Perches			}
5367c17893c7SJoe Perches		}
5368c17893c7SJoe Perches
5369fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5370fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5371fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5372fbdb8138SJoe Perches			my $constant_func = $1;
5373fbdb8138SJoe Perches			my $func = $constant_func;
5374fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5375fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5376fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5377fbdb8138SJoe Perches			    $fix) {
5378194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5379fbdb8138SJoe Perches			}
5380fbdb8138SJoe Perches		}
5381fbdb8138SJoe Perches
53821a15a250SPatrick Pannuto# prefer usleep_range over udelay
538337581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
538443c1d77cSJoe Perches			my $delay = $1;
53851a15a250SPatrick Pannuto			# ignore udelay's < 10, however
538643c1d77cSJoe Perches			if (! ($delay < 10) ) {
5387000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
538843c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
538943c1d77cSJoe Perches			}
539043c1d77cSJoe Perches			if ($delay > 2000) {
539143c1d77cSJoe Perches				WARN("LONG_UDELAY",
539243c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
53931a15a250SPatrick Pannuto			}
53941a15a250SPatrick Pannuto		}
53951a15a250SPatrick Pannuto
539609ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
539709ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
539809ef8725SPatrick Pannuto			if ($1 < 20) {
5399000d1cc1SJoe Perches				WARN("MSLEEP",
540043c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
540109ef8725SPatrick Pannuto			}
540209ef8725SPatrick Pannuto		}
540309ef8725SPatrick Pannuto
540436ec1939SJoe Perches# check for comparisons of jiffies
540536ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
540636ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
540736ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
540836ec1939SJoe Perches		}
540936ec1939SJoe Perches
54109d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
54119d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
54129d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
54139d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
54149d7a34a5SJoe Perches		}
54159d7a34a5SJoe Perches
541600df344fSAndy Whitcroft# warn about #ifdefs in C files
5417c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
541800df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
541900df344fSAndy Whitcroft#			print "$herecurr";
542000df344fSAndy Whitcroft#			$clean = 0;
542100df344fSAndy Whitcroft#		}
542200df344fSAndy Whitcroft
542322f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5424c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
54253705ce5bSJoe Perches			if (ERROR("SPACING",
54263705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
54273705ce5bSJoe Perches			    $fix) {
5428194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54293705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
54303705ce5bSJoe Perches			}
54313705ce5bSJoe Perches
543222f2a2efSAndy Whitcroft		}
543322f2a2efSAndy Whitcroft
54344a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5435171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5436171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
54374a0df2efSAndy Whitcroft			my $which = $1;
54384a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5439000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5440000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
54414a0df2efSAndy Whitcroft			}
54424a0df2efSAndy Whitcroft		}
54434a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5444402c2553SMichael S. Tsirkin
5445402c2553SMichael S. Tsirkin		my $barriers = qr{
5446402c2553SMichael S. Tsirkin			mb|
5447402c2553SMichael S. Tsirkin			rmb|
5448402c2553SMichael S. Tsirkin			wmb|
5449402c2553SMichael S. Tsirkin			read_barrier_depends
5450402c2553SMichael S. Tsirkin		}x;
5451402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5452402c2553SMichael S. Tsirkin			mb__before_atomic|
5453402c2553SMichael S. Tsirkin			mb__after_atomic|
5454402c2553SMichael S. Tsirkin			store_release|
5455402c2553SMichael S. Tsirkin			load_acquire|
5456402c2553SMichael S. Tsirkin			store_mb|
5457402c2553SMichael S. Tsirkin			(?:$barriers)
5458402c2553SMichael S. Tsirkin		}x;
5459402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5460402c2553SMichael S. Tsirkin			(?:$barriers)|
546143e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
546243e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5463402c2553SMichael S. Tsirkin		}x;
5464402c2553SMichael S. Tsirkin
5465402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
54664a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5467c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5468000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
54694a0df2efSAndy Whitcroft			}
54704a0df2efSAndy Whitcroft		}
54713ad81779SPaul E. McKenney
5472f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5473f4073b0fSMichael S. Tsirkin
5474f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5475f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5476f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5477f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5478f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5479f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5480f4073b0fSMichael S. Tsirkin		}
5481f4073b0fSMichael S. Tsirkin
5482cb426e99SJoe Perches# check for waitqueue_active without a comment.
5483cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5484cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5485cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5486cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5487cb426e99SJoe Perches			}
5488cb426e99SJoe Perches		}
54893ad81779SPaul E. McKenney
54903ad81779SPaul E. McKenney# Check for expedited grace periods that interrupt non-idle non-nohz
54913ad81779SPaul E. McKenney# online CPUs.  These expedited can therefore degrade real-time response
54923ad81779SPaul E. McKenney# if used carelessly, and should be avoided where not absolutely
54933ad81779SPaul E. McKenney# needed.  It is always OK to use synchronize_rcu_expedited() and
54943ad81779SPaul E. McKenney# synchronize_sched_expedited() at boot time (before real-time applications
54953ad81779SPaul E. McKenney# start) and in error situations where real-time response is compromised in
54963ad81779SPaul E. McKenney# any case.  Note that synchronize_srcu_expedited() does -not- interrupt
54973ad81779SPaul E. McKenney# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
54983ad81779SPaul E. McKenney# Of course, nothing comes for free, and srcu_read_lock() and
54993ad81779SPaul E. McKenney# srcu_read_unlock() do contain full memory barriers in payment for
55003ad81779SPaul E. McKenney# synchronize_srcu_expedited() non-interruption properties.
55013ad81779SPaul E. McKenney		if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
55023ad81779SPaul E. McKenney			WARN("EXPEDITED_RCU_GRACE_PERIOD",
55033ad81779SPaul E. McKenney			     "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
55043ad81779SPaul E. McKenney
55053ad81779SPaul E. McKenney		}
55063ad81779SPaul E. McKenney
55074a0df2efSAndy Whitcroft# check of hardware specific defines
5508c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5509000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5510000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
55110a920b5bSAndy Whitcroft		}
5512653d4876SAndy Whitcroft
5513d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
5514d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5515000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5516000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
5517d4977c78STobias Klauser		}
5518d4977c78STobias Klauser
5519de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5520de7d4f0eSAndy Whitcroft# storage class and type.
55219c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
55229c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5523000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5524000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5525de7d4f0eSAndy Whitcroft		}
5526de7d4f0eSAndy Whitcroft
55278905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
55282b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
55292b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5530d5e616fcSJoe Perches			if (WARN("INLINE",
5531d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5532d5e616fcSJoe Perches			    $fix) {
5533194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5534d5e616fcSJoe Perches
5535d5e616fcSJoe Perches			}
55368905a67cSAndy Whitcroft		}
55378905a67cSAndy Whitcroft
55383d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
55392b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
55402b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5541000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5542000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
55433d130fd0SJoe Perches		}
55443d130fd0SJoe Perches
554539b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
55462b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
55472b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5548000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5549000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
555039b7e287SJoe Perches		}
555139b7e287SJoe Perches
55525f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
55532b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
55542b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5555d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5556d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5557d5e616fcSJoe Perches			    $fix) {
5558194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5559d5e616fcSJoe Perches
5560d5e616fcSJoe Perches			}
55615f14d3bdSJoe Perches		}
55625f14d3bdSJoe Perches
55636061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
55642b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
55652b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5566d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5567d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5568d5e616fcSJoe Perches			    $fix) {
5569194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5570d5e616fcSJoe Perches			}
55716061d949SJoe Perches		}
55726061d949SJoe Perches
5573619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5574619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5575619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5576619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5577619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5578619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5579619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5580619a908aSJoe Perches		}
5581619a908aSJoe Perches
5582fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
5583e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5584fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
5585e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5586e6176fa4SJoe Perches			my $type = $1;
5587e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5588e6176fa4SJoe Perches				$type = $1;
5589e6176fa4SJoe Perches				my $kernel_type = 'u';
5590e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5591e6176fa4SJoe Perches				$type =~ /(\d+)/;
5592e6176fa4SJoe Perches				$kernel_type .= $1;
5593e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5594e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5595e6176fa4SJoe Perches				    $fix) {
5596e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5597e6176fa4SJoe Perches				}
5598e6176fa4SJoe Perches			}
5599e6176fa4SJoe Perches		}
5600e6176fa4SJoe Perches
5601938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5602938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5603938224b5SJoe Perches			my $cast = $1;
5604938224b5SJoe Perches			my $const = $2;
5605938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5606938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5607938224b5SJoe Perches			    $fix) {
5608938224b5SJoe Perches				my $suffix = "";
5609938224b5SJoe Perches				my $newconst = $const;
5610938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5611938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5612938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5613938224b5SJoe Perches					$suffix .= 'LL';
5614938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5615938224b5SJoe Perches					$suffix .= 'L';
5616938224b5SJoe Perches				}
5617938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5618938224b5SJoe Perches			}
5619938224b5SJoe Perches		}
5620938224b5SJoe Perches
56218f53a9b8SJoe Perches# check for sizeof(&)
56228f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5623000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5624000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
56258f53a9b8SJoe Perches		}
56268f53a9b8SJoe Perches
562766c80b60SJoe Perches# check for sizeof without parenthesis
562866c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5629d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5630d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5631d5e616fcSJoe Perches			    $fix) {
5632194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5633d5e616fcSJoe Perches			}
563466c80b60SJoe Perches		}
563566c80b60SJoe Perches
563688982feaSJoe Perches# check for struct spinlock declarations
563788982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
563888982feaSJoe Perches			WARN("USE_SPINLOCK_T",
563988982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
564088982feaSJoe Perches		}
564188982feaSJoe Perches
5642a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
564306668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5644a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5645caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5646caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5647d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5648d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5649d5e616fcSJoe Perches				    $fix) {
5650194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5651d5e616fcSJoe Perches				}
5652a6962d72SJoe Perches			}
5653a6962d72SJoe Perches		}
5654a6962d72SJoe Perches
5655554e165cSAndy Whitcroft# Check for misused memsets
5656d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5657d1fe9c09SJoe Perches		    defined $stat &&
56589e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5659554e165cSAndy Whitcroft
5660d7c76ba7SJoe Perches			my $ms_addr = $2;
5661d1fe9c09SJoe Perches			my $ms_val = $7;
5662d1fe9c09SJoe Perches			my $ms_size = $12;
5663d7c76ba7SJoe Perches
5664554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5665554e165cSAndy Whitcroft				ERROR("MEMSET",
5666d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5667554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5668554e165cSAndy Whitcroft				WARN("MEMSET",
5669d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5670d7c76ba7SJoe Perches			}
5671d7c76ba7SJoe Perches		}
5672d7c76ba7SJoe Perches
567398a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5674f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5675f333195dSJoe Perches#		    defined $stat &&
5676f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5677f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
5678f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5679f333195dSJoe Perches#			    $fix) {
5680f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5681f333195dSJoe Perches#			}
5682f333195dSJoe Perches#		}
568398a9bba5SJoe Perches
5684b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5685f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5686f333195dSJoe Perches#		    defined $stat &&
5687f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5688f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
5689f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5690f333195dSJoe Perches#		}
5691b6117d17SMateusz Kulikowski
56928617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
56938617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5694f333195dSJoe Perches#		if ($^V && $^V ge 5.10.0 &&
5695f333195dSJoe Perches#		    defined $stat &&
5696f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5697f333195dSJoe Perches#
5698f333195dSJoe Perches#			my $ms_val = $7;
5699f333195dSJoe Perches#
5700f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
5701f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
5702f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5703f333195dSJoe Perches#				    $fix) {
5704f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5705f333195dSJoe Perches#				}
5706f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5707f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
5708f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5709f333195dSJoe Perches#				    $fix) {
5710f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5711f333195dSJoe Perches#				}
5712f333195dSJoe Perches#			}
5713f333195dSJoe Perches#		}
57148617cd09SMateusz Kulikowski
5715d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5716d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5717d1fe9c09SJoe Perches		    defined $stat &&
5718d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5719d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5720d7c76ba7SJoe Perches				my $call = $1;
5721d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5722d7c76ba7SJoe Perches				my $arg1 = $3;
5723d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5724d1fe9c09SJoe Perches				my $arg2 = $8;
5725d7c76ba7SJoe Perches				my $cast;
5726d7c76ba7SJoe Perches
5727d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5728d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5729d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5730d7c76ba7SJoe Perches					$cast = $cast1;
5731d7c76ba7SJoe Perches				} else {
5732d7c76ba7SJoe Perches					$cast = $cast2;
5733d7c76ba7SJoe Perches				}
5734d7c76ba7SJoe Perches				WARN("MINMAX",
5735d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5736554e165cSAndy Whitcroft			}
5737554e165cSAndy Whitcroft		}
5738554e165cSAndy Whitcroft
57394a273195SJoe Perches# check usleep_range arguments
57404a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
57414a273195SJoe Perches		    defined $stat &&
57424a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
57434a273195SJoe Perches			my $min = $1;
57444a273195SJoe Perches			my $max = $7;
57454a273195SJoe Perches			if ($min eq $max) {
57464a273195SJoe Perches				WARN("USLEEP_RANGE",
57474a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
57484a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
57494a273195SJoe Perches				 $min > $max) {
57504a273195SJoe Perches				WARN("USLEEP_RANGE",
57514a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
57524a273195SJoe Perches			}
57534a273195SJoe Perches		}
57544a273195SJoe Perches
5755823b794cSJoe Perches# check for naked sscanf
5756823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5757823b794cSJoe Perches		    defined $stat &&
57586c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5759823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5760823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5761823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5762823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5763823b794cSJoe Perches			$lc = $lc + $linenr;
5764823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5765823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5766823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5767823b794cSJoe Perches			}
5768823b794cSJoe Perches			WARN("NAKED_SSCANF",
5769823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5770823b794cSJoe Perches		}
5771823b794cSJoe Perches
5772afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5773afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5774afc819abSJoe Perches		    defined $stat &&
5775afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5776afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5777afc819abSJoe Perches			$lc = $lc + $linenr;
5778afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5779afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5780afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5781afc819abSJoe Perches			}
5782afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5783afc819abSJoe Perches				my $format = $6;
5784afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5785afc819abSJoe Perches				if ($count == 1 &&
5786afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5787afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5788afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5789afc819abSJoe Perches				}
5790afc819abSJoe Perches			}
5791afc819abSJoe Perches		}
5792afc819abSJoe Perches
579370dc8a48SJoe Perches# check for new externs in .h files.
579470dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
579570dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5796d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
579770dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
579870dc8a48SJoe Perches			    $fix) {
5799194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
580070dc8a48SJoe Perches			}
580170dc8a48SJoe Perches		}
580270dc8a48SJoe Perches
5803de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5804171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5805c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5806171ae1a4SAndy Whitcroft		{
5807c45dcabdSAndy Whitcroft			my $function_name = $1;
5808c45dcabdSAndy Whitcroft			my $paren_space = $2;
5809171ae1a4SAndy Whitcroft
5810171ae1a4SAndy Whitcroft			my $s = $stat;
5811171ae1a4SAndy Whitcroft			if (defined $cond) {
5812171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5813171ae1a4SAndy Whitcroft			}
5814c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5815c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5816c45dcabdSAndy Whitcroft			{
5817000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5818000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5819de7d4f0eSAndy Whitcroft			}
5820de7d4f0eSAndy Whitcroft
5821171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5822000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5823000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5824171ae1a4SAndy Whitcroft			}
58259c9ba34eSAndy Whitcroft
58269c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
58279c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
58289c9ba34eSAndy Whitcroft		{
5829000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5830000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5831171ae1a4SAndy Whitcroft		}
5832171ae1a4SAndy Whitcroft
5833ca0d8929SJoe Perches		if ($realfile =~ /\.[ch]$/ && defined $stat &&
5834ca0d8929SJoe Perches		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
5835ca0d8929SJoe Perches		    $1 ne "void") {
5836ca0d8929SJoe Perches			my $args = trim($1);
5837ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5838ca0d8929SJoe Perches				my $arg = trim($1);
5839ca0d8929SJoe Perches				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5840ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
5841ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5842ca0d8929SJoe Perches				}
5843ca0d8929SJoe Perches			}
5844ca0d8929SJoe Perches		}
5845ca0d8929SJoe Perches
5846de7d4f0eSAndy Whitcroft# checks for new __setup's
5847de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5848de7d4f0eSAndy Whitcroft			my $name = $1;
5849de7d4f0eSAndy Whitcroft
5850de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5851000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
58528c27ceffSMauro Carvalho Chehab				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
5853de7d4f0eSAndy Whitcroft			}
5854653d4876SAndy Whitcroft		}
58559c0ca6f9SAndy Whitcroft
58569c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5857caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5858000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5859000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
58609c0ca6f9SAndy Whitcroft		}
586113214adfSAndy Whitcroft
5862a640d25cSJoe Perches# alloc style
5863a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5864a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5865a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5866a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5867a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5868a640d25cSJoe Perches		}
5869a640d25cSJoe Perches
587060a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
587160a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5872e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
587360a55369SJoe Perches			my $oldfunc = $3;
587460a55369SJoe Perches			my $a1 = $4;
587560a55369SJoe Perches			my $a2 = $10;
587660a55369SJoe Perches			my $newfunc = "kmalloc_array";
587760a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
587860a55369SJoe Perches			my $r1 = $a1;
587960a55369SJoe Perches			my $r2 = $a2;
588060a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
588160a55369SJoe Perches				$r1 = $a2;
588260a55369SJoe Perches				$r2 = $a1;
588360a55369SJoe Perches			}
5884e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5885e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5886e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5887e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5888e367455aSJoe Perches				    $fix) {
5889194f66fcSJoe 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;
589060a55369SJoe Perches
589160a55369SJoe Perches				}
589260a55369SJoe Perches			}
589360a55369SJoe Perches		}
589460a55369SJoe Perches
5895972fdea2SJoe Perches# check for krealloc arg reuse
5896972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5897972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5898972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5899972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5900972fdea2SJoe Perches		}
5901972fdea2SJoe Perches
59025ce59ae0SJoe Perches# check for alloc argument mismatch
59035ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
59045ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
59055ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
59065ce59ae0SJoe Perches		}
59075ce59ae0SJoe Perches
5908caf2a54fSJoe Perches# check for multiple semicolons
5909caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5910d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5911d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5912d5e616fcSJoe Perches			    $fix) {
5913194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5914d5e616fcSJoe Perches			}
5915d1e2ad07SJoe Perches		}
5916d1e2ad07SJoe Perches
5917cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5918cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
5919cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
59200ab90191SJoe Perches			my $ull = "";
59210ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
59220ab90191SJoe Perches			if (CHK("BIT_MACRO",
59230ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
59240ab90191SJoe Perches			    $fix) {
59250ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
59260ab90191SJoe Perches			}
59270ab90191SJoe Perches		}
59280ab90191SJoe Perches
59292d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
59302d632745SJoe 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*$/) {
59312d632745SJoe Perches			my $config = $1;
59322d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
59332d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
59342d632745SJoe Perches			    $fix) {
59352d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
59362d632745SJoe Perches			}
59372d632745SJoe Perches		}
59382d632745SJoe Perches
5939e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5940c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5941c34c09a8SJoe Perches			my $has_break = 0;
5942c34c09a8SJoe Perches			my $has_statement = 0;
5943c34c09a8SJoe Perches			my $count = 0;
5944c34c09a8SJoe Perches			my $prevline = $linenr;
5945e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5946c34c09a8SJoe Perches				$prevline--;
5947c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5948c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5949c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5950c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5951c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5952c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5953c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5954c34c09a8SJoe Perches				$has_statement = 1;
5955c34c09a8SJoe Perches				$count++;
5956c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5957c34c09a8SJoe Perches			}
5958c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5959c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5960224236d9SAndrew Morton				     "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
5961c34c09a8SJoe Perches			}
5962c34c09a8SJoe Perches		}
5963c34c09a8SJoe Perches
5964d1e2ad07SJoe Perches# check for switch/default statements without a break;
5965d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5966d1e2ad07SJoe Perches		    defined $stat &&
5967d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5968d1e2ad07SJoe Perches			my $ctx = '';
5969d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5970d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5971d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5972d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5973d1e2ad07SJoe Perches			}
5974d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5975d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5976caf2a54fSJoe Perches		}
5977caf2a54fSJoe Perches
597813214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5979d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5980d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5981d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5982d5e616fcSJoe Perches			    $fix) {
5983194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5984d5e616fcSJoe Perches			}
598513214adfSAndy Whitcroft		}
5986773647a0SAndy Whitcroft
598762ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
598862ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
598962ec818fSJoe Perches			ERROR("DATE_TIME",
599062ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
599162ec818fSJoe Perches		}
599262ec818fSJoe Perches
59932c92488aSJoe Perches# check for use of yield()
59942c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
59952c92488aSJoe Perches			WARN("YIELD",
59962c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
59972c92488aSJoe Perches		}
59982c92488aSJoe Perches
5999179f8f40SJoe Perches# check for comparisons against true and false
6000179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6001179f8f40SJoe Perches			my $lead = $1;
6002179f8f40SJoe Perches			my $arg = $2;
6003179f8f40SJoe Perches			my $test = $3;
6004179f8f40SJoe Perches			my $otype = $4;
6005179f8f40SJoe Perches			my $trail = $5;
6006179f8f40SJoe Perches			my $op = "!";
6007179f8f40SJoe Perches
6008179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6009179f8f40SJoe Perches
6010179f8f40SJoe Perches			my $type = lc($otype);
6011179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
6012179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
6013179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
6014179f8f40SJoe Perches					$op = "";
6015179f8f40SJoe Perches				}
6016179f8f40SJoe Perches
6017179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
6018179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
6019179f8f40SJoe Perches
6020179f8f40SJoe Perches## maybe suggesting a correct construct would better
6021179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6022179f8f40SJoe Perches
6023179f8f40SJoe Perches			}
6024179f8f40SJoe Perches		}
6025179f8f40SJoe Perches
60264882720bSThomas Gleixner# check for semaphores initialized locked
60274882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
6028000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
6029000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
6030773647a0SAndy Whitcroft		}
60316712d858SJoe Perches
603267d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
603367d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
6034000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
603567d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
6036773647a0SAndy Whitcroft		}
60376712d858SJoe Perches
6038ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
6039f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
6040000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
6041ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
6042f3db6639SMichael Ellerman		}
60436712d858SJoe Perches
60440f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
60456903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
60460f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
6047000d1cc1SJoe Perches			WARN("CONST_STRUCT",
6048000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
60496903ffb2SAndy Whitcroft				$herecurr);
60502b6db5cbSAndy Whitcroft		}
6051773647a0SAndy Whitcroft
6052773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
6053773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
6054773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
6055c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6056c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
6057171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6058171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6059171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
6060773647a0SAndy Whitcroft		{
6061000d1cc1SJoe Perches			WARN("NR_CPUS",
6062000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
6063773647a0SAndy Whitcroft		}
60649c9ba34eSAndy Whitcroft
606552ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
606652ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
606752ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
606852ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
606952ea8506SJoe Perches		}
607052ea8506SJoe Perches
6071acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6072acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6073acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6074acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
6075acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6076acd9362cSJoe Perches		}
6077acd9362cSJoe Perches
6078691d77b6SAndy Whitcroft# whine mightly about in_atomic
6079691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
6080691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
6081000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
6082000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
6083f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
6084000d1cc1SJoe Perches				WARN("IN_ATOMIC",
6085000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
6086691d77b6SAndy Whitcroft			}
6087691d77b6SAndy Whitcroft		}
60881704f47bSPeter Zijlstra
6089481aea5cSJoe Perches# whine about ACCESS_ONCE
6090481aea5cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6091481aea5cSJoe Perches		    $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6092481aea5cSJoe Perches			my $par = $1;
6093481aea5cSJoe Perches			my $eq = $2;
6094481aea5cSJoe Perches			my $fun = $3;
6095481aea5cSJoe Perches			$par =~ s/^\(\s*(.*)\s*\)$/$1/;
6096481aea5cSJoe Perches			if (defined($eq)) {
6097481aea5cSJoe Perches				if (WARN("PREFER_WRITE_ONCE",
6098481aea5cSJoe Perches					 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6099481aea5cSJoe Perches				    $fix) {
6100481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6101481aea5cSJoe Perches				}
6102481aea5cSJoe Perches			} else {
6103481aea5cSJoe Perches				if (WARN("PREFER_READ_ONCE",
6104481aea5cSJoe Perches					 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6105481aea5cSJoe Perches				    $fix) {
6106481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6107481aea5cSJoe Perches				}
6108481aea5cSJoe Perches			}
6109481aea5cSJoe Perches		}
6110481aea5cSJoe Perches
61110f5225b0SPeter Zijlstra# check for mutex_trylock_recursive usage
61120f5225b0SPeter Zijlstra		if ($line =~ /mutex_trylock_recursive/) {
61130f5225b0SPeter Zijlstra			ERROR("LOCKING",
61140f5225b0SPeter Zijlstra			      "recursive locking is bad, do not use this ever.\n" . $herecurr);
61150f5225b0SPeter Zijlstra		}
61160f5225b0SPeter Zijlstra
61171704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
61181704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
61191704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
61201704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
61211704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
61221704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6123000d1cc1SJoe Perches				ERROR("LOCKDEP",
6124000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
61251704f47bSPeter Zijlstra			}
61261704f47bSPeter Zijlstra		}
612788f8831cSDave Jones
6128b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6129b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6130000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6131000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
613288f8831cSDave Jones		}
61332435880fSJoe Perches
6134515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6135515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6136515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6137459cf0aeSJoe Perches		    defined $stat &&
6138515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
61392435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
61402435880fSJoe Perches				my $func = $entry->[0];
61412435880fSJoe Perches				my $arg_pos = $entry->[1];
61422435880fSJoe Perches
6143459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
6144459cf0aeSJoe Perches				$lc = $lc + $linenr;
6145459cf0aeSJoe Perches				my $stat_real = raw_line($linenr, 0);
6146459cf0aeSJoe Perches				for (my $count = $linenr + 1; $count <= $lc; $count++) {
6147459cf0aeSJoe Perches					$stat_real = $stat_real . "\n" . raw_line($count, 0);
6148459cf0aeSJoe Perches				}
6149459cf0aeSJoe Perches
61502435880fSJoe Perches				my $skip_args = "";
61512435880fSJoe Perches				if ($arg_pos > 1) {
61522435880fSJoe Perches					$arg_pos--;
61532435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
61542435880fSJoe Perches				}
6155f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6156459cf0aeSJoe Perches				if ($stat =~ /$test/) {
61572435880fSJoe Perches					my $val = $1;
61582435880fSJoe Perches					$val = $6 if ($skip_args ne "");
6159f90774e1SJoe Perches					if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6160f90774e1SJoe Perches					    ($val =~ /^$Octal$/ && length($val) ne 4)) {
61612435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
6162459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
6163f90774e1SJoe Perches					}
6164f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6165c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6166459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
61672435880fSJoe Perches					}
6168459cf0aeSJoe Perches				}
6169459cf0aeSJoe Perches			}
6170459cf0aeSJoe Perches		}
6171459cf0aeSJoe Perches
6172459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
6173459cf0aeSJoe Perches		if ($line =~ /\b$mode_perms_string_search\b/) {
6174459cf0aeSJoe Perches			my $val = "";
6175459cf0aeSJoe Perches			my $oval = "";
6176f90774e1SJoe Perches			my $to = 0;
6177459cf0aeSJoe Perches			my $curpos = 0;
6178459cf0aeSJoe Perches			my $lastpos = 0;
6179459cf0aeSJoe Perches			while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6180459cf0aeSJoe Perches				$curpos = pos($line);
6181459cf0aeSJoe Perches				my $match = $2;
6182459cf0aeSJoe Perches				my $omatch = $1;
6183459cf0aeSJoe Perches				last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6184459cf0aeSJoe Perches				$lastpos = $curpos;
6185459cf0aeSJoe Perches				$to |= $mode_permission_string_types{$match};
6186459cf0aeSJoe Perches				$val .= '\s*\|\s*' if ($val ne "");
6187459cf0aeSJoe Perches				$val .= $match;
6188459cf0aeSJoe Perches				$oval .= $omatch;
6189f90774e1SJoe Perches			}
6190459cf0aeSJoe Perches			$oval =~ s/^\s*\|\s*//;
6191459cf0aeSJoe Perches			$oval =~ s/\s*\|\s*$//;
6192459cf0aeSJoe Perches			my $octal = sprintf("%04o", $to);
6193f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
6194459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6195f90774e1SJoe Perches			    $fix) {
6196459cf0aeSJoe Perches				$fixed[$fixlinenr] =~ s/$val/$octal/;
61972435880fSJoe Perches			}
619813214adfSAndy Whitcroft		}
61995a6d20ceSBjorn Andersson
62005a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
62015a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
62025a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
62035a6d20ceSBjorn Andersson			my $valid_licenses = qr{
62045a6d20ceSBjorn Andersson						GPL|
62055a6d20ceSBjorn Andersson						GPL\ v2|
62065a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
62075a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
62085a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
62095a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
62105a6d20ceSBjorn Andersson						Proprietary
62115a6d20ceSBjorn Andersson					}x;
62125a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
62135a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
62145a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
62155a6d20ceSBjorn Andersson			}
62165a6d20ceSBjorn Andersson		}
6217515a235eSJoe Perches	}
621813214adfSAndy Whitcroft
621913214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
622013214adfSAndy Whitcroft	# so just keep quiet.
622113214adfSAndy Whitcroft	if ($#rawlines == -1) {
622213214adfSAndy Whitcroft		exit(0);
62230a920b5bSAndy Whitcroft	}
62240a920b5bSAndy Whitcroft
62258905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
62268905a67cSAndy Whitcroft	# things that appear to be patches.
62278905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
62288905a67cSAndy Whitcroft		exit(0);
62298905a67cSAndy Whitcroft	}
62308905a67cSAndy Whitcroft
62318905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
62328905a67cSAndy Whitcroft	# just keep quiet.
62338905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
62348905a67cSAndy Whitcroft		exit(0);
62358905a67cSAndy Whitcroft	}
62368905a67cSAndy Whitcroft
623706330fc4SJoe Perches	if (!$is_patch && $file !~ /cover-letter\.patch$/) {
6238000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6239000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
62400a920b5bSAndy Whitcroft	}
6241ed43c4e5SAllen Hubbe	if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6242000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
6243000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
62440a920b5bSAndy Whitcroft	}
62450a920b5bSAndy Whitcroft
6246f0a594c1SAndy Whitcroft	print report_dump();
624713214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
624813214adfSAndy Whitcroft		print "$filename " if ($summary_file);
62496c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
62506c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
62516c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
62526c72ffaaSAndy Whitcroft	}
62538905a67cSAndy Whitcroft
6254d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6255ef212196SJoe Perches		# If there were any defects found and not already fixing them
6256ef212196SJoe Perches		if (!$clean and !$fix) {
6257ef212196SJoe Perches			print << "EOM"
6258ef212196SJoe Perches
6259ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6260ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6261ef212196SJoe PerchesEOM
6262ef212196SJoe Perches		}
6263d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6264d2c0a235SAndy Whitcroft		# then suggest that.
6265d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6266b0781216SMike Frysinger			$rpt_cleaners = 0;
6267d8469f16SJoe Perches			print << "EOM"
6268d8469f16SJoe Perches
6269d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6270d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6271d8469f16SJoe PerchesEOM
6272d2c0a235SAndy Whitcroft		}
6273d2c0a235SAndy Whitcroft	}
6274d2c0a235SAndy Whitcroft
6275d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6276d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6277d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
62789624b8d6SJoe Perches		my $newfile = $filename;
62799624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
62803705ce5bSJoe Perches		my $linecount = 0;
62813705ce5bSJoe Perches		my $f;
62823705ce5bSJoe Perches
6283d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6284d752fcc8SJoe Perches
62853705ce5bSJoe Perches		open($f, '>', $newfile)
62863705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
62873705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
62883705ce5bSJoe Perches			$linecount++;
62893705ce5bSJoe Perches			if ($file) {
62903705ce5bSJoe Perches				if ($linecount > 3) {
62913705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
62923705ce5bSJoe Perches					print $f $fixed_line . "\n";
62933705ce5bSJoe Perches				}
62943705ce5bSJoe Perches			} else {
62953705ce5bSJoe Perches				print $f $fixed_line . "\n";
62963705ce5bSJoe Perches			}
62973705ce5bSJoe Perches		}
62983705ce5bSJoe Perches		close($f);
62993705ce5bSJoe Perches
63003705ce5bSJoe Perches		if (!$quiet) {
63013705ce5bSJoe Perches			print << "EOM";
6302d8469f16SJoe Perches
63033705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
63043705ce5bSJoe Perches
63053705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
63063705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
63073705ce5bSJoe Perches
63083705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
63093705ce5bSJoe PerchesNo warranties, expressed or implied...
63103705ce5bSJoe PerchesEOM
63113705ce5bSJoe Perches		}
63123705ce5bSJoe Perches	}
63133705ce5bSJoe Perches
6314d8469f16SJoe Perches	if ($quiet == 0) {
6315d8469f16SJoe Perches		print "\n";
6316d8469f16SJoe Perches		if ($clean == 1) {
6317d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6318d8469f16SJoe Perches		} else {
6319d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
63200a920b5bSAndy Whitcroft		}
63210a920b5bSAndy Whitcroft	}
63220a920b5bSAndy Whitcroft	return $clean;
63230a920b5bSAndy Whitcroft}
6324