xref: /linux-6.15/scripts/checkpatch.pl (revision 08eb9b80)
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";
5757230297SJoe Perchesmy $color = 1;
58dadf680dSJoe Perchesmy $allow_c99_comments = 1;
5977f5b10aSHannes Eder
6077f5b10aSHannes Edersub help {
6177f5b10aSHannes Eder	my ($exitcode) = @_;
6277f5b10aSHannes Eder
6377f5b10aSHannes Eder	print << "EOM";
6477f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
6577f5b10aSHannes EderVersion: $V
6677f5b10aSHannes Eder
6777f5b10aSHannes EderOptions:
6877f5b10aSHannes Eder  -q, --quiet                quiet
6977f5b10aSHannes Eder  --no-tree                  run without a kernel tree
7077f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
7177f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
7277f5b10aSHannes Eder  --emacs                    emacs compile window format
7377f5b10aSHannes Eder  --terse                    one line per report
7434d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
754a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
764a593c34SDu, Changbin                             single git commit with:
774a593c34SDu, Changbin                               <rev>
784a593c34SDu, Changbin                               <rev>^
794a593c34SDu, Changbin                               <rev>~n
804a593c34SDu, Changbin                             multiple git commits with:
814a593c34SDu, Changbin                               <rev1>..<rev2>
824a593c34SDu, Changbin                               <rev1>...<rev2>
834a593c34SDu, Changbin                               <rev>-<count>
844a593c34SDu, Changbin                             git merges are ignored
8577f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
8677f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
873beb42ecSJoe Perches  --list-types               list the possible message types
8891bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
89000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
903beb42ecSJoe Perches  --show-types               show the specific message type in the output
916cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
9256193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
9377f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
9477f5b10aSHannes Eder  --no-summary               suppress the per-file summary
9577f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
9677f5b10aSHannes Eder  --summary-file             include the filename in summary
9777f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
9877f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
9977f5b10aSHannes Eder                             is all off)
10077f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
10177f5b10aSHannes Eder                             literally
1023705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1033705ce5bSJoe Perches                             If correctable single-line errors exist, create
1043705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1053705ce5bSJoe Perches                             with potential errors corrected to the preferred
1063705ce5bSJoe Perches                             checkpatch style
1079624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1089624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1099624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
110d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
111d62a201fSDave Hansen                             runtime errors.
112ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
113f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
114ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
11557230297SJoe Perches  --color                    Use colors when output is STDOUT (default: on)
11677f5b10aSHannes Eder  -h, --help, --version      display this help and exit
11777f5b10aSHannes Eder
11877f5b10aSHannes EderWhen FILE is - read standard input.
11977f5b10aSHannes EderEOM
12077f5b10aSHannes Eder
12177f5b10aSHannes Eder	exit($exitcode);
12277f5b10aSHannes Eder}
12377f5b10aSHannes Eder
1243beb42ecSJoe Perchessub uniq {
1253beb42ecSJoe Perches	my %seen;
1263beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1273beb42ecSJoe Perches}
1283beb42ecSJoe Perches
1293beb42ecSJoe Perchessub list_types {
1303beb42ecSJoe Perches	my ($exitcode) = @_;
1313beb42ecSJoe Perches
1323beb42ecSJoe Perches	my $count = 0;
1333beb42ecSJoe Perches
1343beb42ecSJoe Perches	local $/ = undef;
1353beb42ecSJoe Perches
1363beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1373beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1383beb42ecSJoe Perches
1393beb42ecSJoe Perches	my $text = <$script>;
1403beb42ecSJoe Perches	close($script);
1413beb42ecSJoe Perches
1423beb42ecSJoe Perches	my @types = ();
1433beb42ecSJoe Perches	for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
1443beb42ecSJoe Perches		push (@types, $_);
1453beb42ecSJoe Perches	}
1463beb42ecSJoe Perches	@types = sort(uniq(@types));
1473beb42ecSJoe Perches	print("#\tMessage type\n\n");
1483beb42ecSJoe Perches	foreach my $type (@types) {
1493beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
1503beb42ecSJoe Perches	}
1513beb42ecSJoe Perches
1523beb42ecSJoe Perches	exit($exitcode);
1533beb42ecSJoe Perches}
1543beb42ecSJoe Perches
155000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
156000d1cc1SJoe Perchesif (-f $conf) {
157000d1cc1SJoe Perches	my @conf_args;
158000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
159000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
160000d1cc1SJoe Perches
161000d1cc1SJoe Perches	while (<$conffile>) {
162000d1cc1SJoe Perches		my $line = $_;
163000d1cc1SJoe Perches
164000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
165000d1cc1SJoe Perches		$line =~ s/^\s*//g;
166000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
167000d1cc1SJoe Perches
168000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
169000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
170000d1cc1SJoe Perches
171000d1cc1SJoe Perches		my @words = split(" ", $line);
172000d1cc1SJoe Perches		foreach my $word (@words) {
173000d1cc1SJoe Perches			last if ($word =~ m/^#/);
174000d1cc1SJoe Perches			push (@conf_args, $word);
175000d1cc1SJoe Perches		}
176000d1cc1SJoe Perches	}
177000d1cc1SJoe Perches	close($conffile);
178000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
179000d1cc1SJoe Perches}
180000d1cc1SJoe Perches
1810a920b5bSAndy WhitcroftGetOptions(
1826c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1830a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1840a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1850a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1866c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1878905a67cSAndy Whitcroft	'terse!'	=> \$terse,
18834d8815fSJoe Perches	'showfile!'	=> \$showfile,
18977f5b10aSHannes Eder	'f|file!'	=> \$file,
1904a593c34SDu, Changbin	'g|git!'	=> \$git,
1916c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1926c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
193000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
19491bfe484SJoe Perches	'types=s'	=> \@use,
195000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
1963beb42ecSJoe Perches	'list-types!'	=> \$list_types,
1976cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
19856193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
1996c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
2008905a67cSAndy Whitcroft	'summary!'	=> \$summary,
2018905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
20213214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
2033705ce5bSJoe Perches	'fix!'		=> \$fix,
2049624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
205d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
206c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
207773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
208ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
209ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
21057230297SJoe Perches	'color!'	=> \$color,
21177f5b10aSHannes Eder	'h|help'	=> \$help,
21277f5b10aSHannes Eder	'version'	=> \$help
21377f5b10aSHannes Eder) or help(1);
21477f5b10aSHannes Eder
21577f5b10aSHannes Ederhelp(0) if ($help);
2160a920b5bSAndy Whitcroft
2173beb42ecSJoe Percheslist_types(0) if ($list_types);
2183beb42ecSJoe Perches
2199624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2202ac73b4fSJoe Perches$check_orig = $check;
2219624b8d6SJoe Perches
2220a920b5bSAndy Whitcroftmy $exit = 0;
2230a920b5bSAndy Whitcroft
224d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
225d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
226d62a201fSDave Hansen	if (!$ignore_perl_version) {
227d62a201fSDave Hansen		exit(1);
228d62a201fSDave Hansen	}
229d62a201fSDave Hansen}
230d62a201fSDave Hansen
23145107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
2320a920b5bSAndy Whitcroftif ($#ARGV < 0) {
23345107ff6SAllen Hubbe	push(@ARGV, '-');
2340a920b5bSAndy Whitcroft}
2350a920b5bSAndy Whitcroft
23691bfe484SJoe Perchessub hash_save_array_words {
23791bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
23891bfe484SJoe Perches
23991bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
24091bfe484SJoe Perches	foreach my $word (@array) {
241000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
242000d1cc1SJoe Perches		$word =~ s/^\s*//g;
243000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
244000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
245000d1cc1SJoe Perches
246000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
247000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
248000d1cc1SJoe Perches
24991bfe484SJoe Perches		$hashRef->{$word}++;
250000d1cc1SJoe Perches	}
25191bfe484SJoe Perches}
25291bfe484SJoe Perches
25391bfe484SJoe Perchessub hash_show_words {
25491bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
25591bfe484SJoe Perches
2563c816e49SJoe Perches	if (keys %$hashRef) {
257d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
25858cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
25991bfe484SJoe Perches			print " $word";
26091bfe484SJoe Perches		}
261d8469f16SJoe Perches		print "\n";
26291bfe484SJoe Perches	}
26391bfe484SJoe Perches}
26491bfe484SJoe Perches
26591bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
26691bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
267000d1cc1SJoe Perches
268c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
269c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2707429c690SAndy Whitcroftmy $dbg_type = 0;
271a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
272c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
27321caa13cSAndy Whitcroft	## no critic
27421caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
27521caa13cSAndy Whitcroft	die "$@" if ($@);
276c2fdda0dSAndy Whitcroft}
277c2fdda0dSAndy Whitcroft
278d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
279d2c0a235SAndy Whitcroft
2808905a67cSAndy Whitcroftif ($terse) {
2818905a67cSAndy Whitcroft	$emacs = 1;
2828905a67cSAndy Whitcroft	$quiet++;
2838905a67cSAndy Whitcroft}
2848905a67cSAndy Whitcroft
2856c72ffaaSAndy Whitcroftif ($tree) {
2866c72ffaaSAndy Whitcroft	if (defined $root) {
2876c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2886c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2896c72ffaaSAndy Whitcroft		}
2906c72ffaaSAndy Whitcroft	} else {
2916c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2926c72ffaaSAndy Whitcroft			$root = '.';
2936c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2946c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2956c72ffaaSAndy Whitcroft			$root = $1;
2966c72ffaaSAndy Whitcroft		}
2976c72ffaaSAndy Whitcroft	}
2986c72ffaaSAndy Whitcroft
2996c72ffaaSAndy Whitcroft	if (!defined $root) {
3000a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
3010a920b5bSAndy Whitcroft		exit(2);
3020a920b5bSAndy Whitcroft	}
3036c72ffaaSAndy Whitcroft}
3046c72ffaaSAndy Whitcroft
3056c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
3066c72ffaaSAndy Whitcroft
3072ceb532bSAndy Whitcroftour $Ident	= qr{
3082ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
3092ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
3102ceb532bSAndy Whitcroft		}x;
3116c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
3126c72ffaaSAndy Whitcroftour $Sparse	= qr{
3136c72ffaaSAndy Whitcroft			__user|
3146c72ffaaSAndy Whitcroft			__kernel|
3156c72ffaaSAndy Whitcroft			__force|
3166c72ffaaSAndy Whitcroft			__iomem|
3176c72ffaaSAndy Whitcroft			__must_check|
3186c72ffaaSAndy Whitcroft			__init_refok|
319417495edSAndy Whitcroft			__kprobes|
320165e72a6SSven Eckelmann			__ref|
321ad315455SBoqun Feng			__rcu|
322ad315455SBoqun Feng			__private
3236c72ffaaSAndy Whitcroft		}x;
324e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
325e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
326e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
327e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
328e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3298716de38SJoe Perches
33052131292SWolfram Sang# Notes to $Attribute:
33152131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3326c72ffaaSAndy Whitcroftour $Attribute	= qr{
3336c72ffaaSAndy Whitcroft			const|
33403f1df7dSJoe Perches			__percpu|
33503f1df7dSJoe Perches			__nocast|
33603f1df7dSJoe Perches			__safe|
33703f1df7dSJoe Perches			__bitwise__|
33803f1df7dSJoe Perches			__packed__|
33903f1df7dSJoe Perches			__packed2__|
34003f1df7dSJoe Perches			__naked|
34103f1df7dSJoe Perches			__maybe_unused|
34203f1df7dSJoe Perches			__always_unused|
34303f1df7dSJoe Perches			__noreturn|
34403f1df7dSJoe Perches			__used|
34503f1df7dSJoe Perches			__cold|
346e23ef1f3SJoe Perches			__pure|
34703f1df7dSJoe Perches			__noclone|
34803f1df7dSJoe Perches			__deprecated|
3496c72ffaaSAndy Whitcroft			__read_mostly|
3506c72ffaaSAndy Whitcroft			__kprobes|
3518716de38SJoe Perches			$InitAttribute|
35224e1d81aSAndy Whitcroft			____cacheline_aligned|
35324e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3545fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3555fe3af11SAndy Whitcroft			__weak
3566c72ffaaSAndy Whitcroft		  }x;
357c45dcabdSAndy Whitcroftour $Modifier;
35891cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3596c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3606c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3616c72ffaaSAndy Whitcroft
36295e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
36395e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
36495e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
36595e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3662435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
367c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
368326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
369326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
370326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
37174349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3722435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
373326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
374447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
37523f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3766c72ffaaSAndy Whitcroftour $Operators	= qr{
3776c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3786c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
37923f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3806c72ffaaSAndy Whitcroft		  }x;
3816c72ffaaSAndy Whitcroft
38291cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
38391cb5195SJoe Perches
384ab7e23f3SJoe Perchesour $BasicType;
3858905a67cSAndy Whitcroftour $NonptrType;
3861813087dSJoe Perchesour $NonptrTypeMisordered;
3878716de38SJoe Perchesour $NonptrTypeWithAttr;
3888905a67cSAndy Whitcroftour $Type;
3891813087dSJoe Perchesour $TypeMisordered;
3908905a67cSAndy Whitcroftour $Declare;
3911813087dSJoe Perchesour $DeclareMisordered;
3928905a67cSAndy Whitcroft
39315662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
39415662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
395171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
396171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
397171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
398171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
399171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
400171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
401171ae1a4SAndy Whitcroft}x;
402171ae1a4SAndy Whitcroft
40315662b3eSJoe Perchesour $UTF8	= qr{
40415662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
40515662b3eSJoe Perches	| $NON_ASCII_UTF8
40615662b3eSJoe Perches}x;
40715662b3eSJoe Perches
408e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
409021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
410021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
411021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
412021158b4SJoe Perches)};
413e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
414fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4158ed22cadSAndy Whitcroft	atomic_t
4168ed22cadSAndy Whitcroft)};
417e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
418e6176fa4SJoe Perches	$typeC99Typedefs\b|
419e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
420e6176fa4SJoe Perches	$typeKernelTypedefs\b
421e6176fa4SJoe Perches)};
4228ed22cadSAndy Whitcroft
4236d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4246d32f7a3SJoe Perches
425691e669bSJoe Perchesour $logFunctions = qr{(?x:
4266e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
4277d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
4286e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
429b0531722SJoe Perches	panic|
43006668727SJoe Perches	MODULE_[A-Z_]+|
43106668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
432691e669bSJoe Perches)};
433691e669bSJoe Perches
43420112475SJoe Perchesour $signature_tags = qr{(?xi:
43520112475SJoe Perches	Signed-off-by:|
43620112475SJoe Perches	Acked-by:|
43720112475SJoe Perches	Tested-by:|
43820112475SJoe Perches	Reviewed-by:|
43920112475SJoe Perches	Reported-by:|
4408543ae12SMugunthan V N	Suggested-by:|
44120112475SJoe Perches	To:|
44220112475SJoe Perches	Cc:
44320112475SJoe Perches)};
44420112475SJoe Perches
4451813087dSJoe Perchesour @typeListMisordered = (
4461813087dSJoe Perches	qr{char\s+(?:un)?signed},
4471813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4481813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4491813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4501813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4511813087dSJoe Perches	qr{short\s+(?:un)?signed},
4521813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4531813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4541813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4551813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4561813087dSJoe Perches	qr{int\s+(?:un)?signed},
4571813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4581813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4591813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4601813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4611813087dSJoe Perches	qr{long\s+(?:un)?signed},
4621813087dSJoe Perches);
4631813087dSJoe Perches
4648905a67cSAndy Whitcroftour @typeList = (
4658905a67cSAndy Whitcroft	qr{void},
4660c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4670c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4680c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4690c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4700c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4710c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
4720c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4730c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4740c773d9dSJoe Perches	qr{(?:un)?signed},
4758905a67cSAndy Whitcroft	qr{float},
4768905a67cSAndy Whitcroft	qr{double},
4778905a67cSAndy Whitcroft	qr{bool},
4788905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4798905a67cSAndy Whitcroft	qr{union\s+$Ident},
4808905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4818905a67cSAndy Whitcroft	qr{${Ident}_t},
4828905a67cSAndy Whitcroft	qr{${Ident}_handler},
4838905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4841813087dSJoe Perches	@typeListMisordered,
4858905a67cSAndy Whitcroft);
486938224b5SJoe Perches
487938224b5SJoe Perchesour $C90_int_types = qr{(?x:
488938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
489938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
490938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
491938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
492938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
493938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
494938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
495938224b5SJoe Perches
496938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
497938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
498938224b5SJoe Perches	long\s+(?:un)?signed|
499938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
500938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
501938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
502938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
503938224b5SJoe Perches
504938224b5SJoe Perches	int\s+(?:un)?signed|
505938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
506938224b5SJoe Perches)};
507938224b5SJoe Perches
508485ff23eSAlex Dowadour @typeListFile = ();
5098716de38SJoe Perchesour @typeListWithAttr = (
5108716de38SJoe Perches	@typeList,
5118716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
5128716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5138716de38SJoe Perches);
5148716de38SJoe Perches
515c45dcabdSAndy Whitcroftour @modifierList = (
516c45dcabdSAndy Whitcroft	qr{fastcall},
517c45dcabdSAndy Whitcroft);
518485ff23eSAlex Dowadour @modifierListFile = ();
5198905a67cSAndy Whitcroft
5202435880fSJoe Perchesour @mode_permission_funcs = (
5212435880fSJoe Perches	["module_param", 3],
5222435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5232435880fSJoe Perches	["module_param_array_named", 5],
5242435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5252435880fSJoe Perches	["proc_create(?:_data|)", 2],
5262435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
5272435880fSJoe Perches);
5282435880fSJoe Perches
529515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
530515a235eSJoe Perchesour $mode_perms_search = "";
531515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
532515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
533515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
534515a235eSJoe Perches}
535515a235eSJoe Perches
536b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
537b392c64fSJoe Perches	S_IWUGO		|
538b392c64fSJoe Perches	S_IWOTH		|
539b392c64fSJoe Perches	S_IRWXUGO	|
540b392c64fSJoe Perches	S_IALLUGO	|
541b392c64fSJoe Perches	0[0-7][0-7][2367]
542b392c64fSJoe Perches}x;
543b392c64fSJoe Perches
544f90774e1SJoe Perchesour %mode_permission_string_types = (
545f90774e1SJoe Perches	"S_IRWXU" => 0700,
546f90774e1SJoe Perches	"S_IRUSR" => 0400,
547f90774e1SJoe Perches	"S_IWUSR" => 0200,
548f90774e1SJoe Perches	"S_IXUSR" => 0100,
549f90774e1SJoe Perches	"S_IRWXG" => 0070,
550f90774e1SJoe Perches	"S_IRGRP" => 0040,
551f90774e1SJoe Perches	"S_IWGRP" => 0020,
552f90774e1SJoe Perches	"S_IXGRP" => 0010,
553f90774e1SJoe Perches	"S_IRWXO" => 0007,
554f90774e1SJoe Perches	"S_IROTH" => 0004,
555f90774e1SJoe Perches	"S_IWOTH" => 0002,
556f90774e1SJoe Perches	"S_IXOTH" => 0001,
557f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
558f90774e1SJoe Perches	"S_IRUGO" => 0444,
559f90774e1SJoe Perches	"S_IWUGO" => 0222,
560f90774e1SJoe Perches	"S_IXUGO" => 0111,
561f90774e1SJoe Perches);
562f90774e1SJoe Perches
563f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
564f90774e1SJoe Perchesour $mode_perms_string_search = "";
565f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
566f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
567f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
568f90774e1SJoe Perches}
569f90774e1SJoe Perches
5707840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
5717840a94cSWolfram Sang	irq|
572cdcee686SSergey Ryazanov	memory|
573cdcee686SSergey Ryazanov	time|
574cdcee686SSergey Ryazanov	reboot
5757840a94cSWolfram Sang)};
5767840a94cSWolfram Sang# memory.h: ARM has a custom one
5777840a94cSWolfram Sang
57866b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
57966b47b4aSKees Cookmy $misspellings;
58066b47b4aSKees Cookmy %spelling_fix;
58136061e38SJoe Perches
58236061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
58366b47b4aSKees Cook	while (<$spelling>) {
58466b47b4aSKees Cook		my $line = $_;
58566b47b4aSKees Cook
58666b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
58766b47b4aSKees Cook		$line =~ s/^\s*//g;
58866b47b4aSKees Cook
58966b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
59066b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
59166b47b4aSKees Cook
59266b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
59366b47b4aSKees Cook
59466b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
59566b47b4aSKees Cook	}
59666b47b4aSKees Cook	close($spelling);
59736061e38SJoe Perches} else {
59836061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
59936061e38SJoe Perches}
60066b47b4aSKees Cook
601ebfd7d62SJoe Perchesif ($codespell) {
602ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
603ebfd7d62SJoe Perches		while (<$spelling>) {
604ebfd7d62SJoe Perches			my $line = $_;
605ebfd7d62SJoe Perches
606ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
607ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
608ebfd7d62SJoe Perches
609ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
610ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
611ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
612ebfd7d62SJoe Perches
613ebfd7d62SJoe Perches			$line =~ s/,.*$//;
614ebfd7d62SJoe Perches
615ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
616ebfd7d62SJoe Perches
617ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
618ebfd7d62SJoe Perches		}
619ebfd7d62SJoe Perches		close($spelling);
620ebfd7d62SJoe Perches	} else {
621ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
622ebfd7d62SJoe Perches	}
623ebfd7d62SJoe Perches}
624ebfd7d62SJoe Perches
625ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
626ebfd7d62SJoe Perches
6278905a67cSAndy Whitcroftsub build_types {
628485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
629485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
6301813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
6318716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
632c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
633ab7e23f3SJoe Perches	$BasicType	= qr{
634ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
635ab7e23f3SJoe Perches				(?:${all}\b)
636ab7e23f3SJoe Perches		}x;
6378905a67cSAndy Whitcroft	$NonptrType	= qr{
638d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
639cf655043SAndy Whitcroft			(?:
6406b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
6418ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
642c45dcabdSAndy Whitcroft				(?:${all}\b)
643cf655043SAndy Whitcroft			)
644c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
6458905a67cSAndy Whitcroft		  }x;
6461813087dSJoe Perches	$NonptrTypeMisordered	= qr{
6471813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
6481813087dSJoe Perches			(?:
6491813087dSJoe Perches				(?:${Misordered}\b)
6501813087dSJoe Perches			)
6511813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
6521813087dSJoe Perches		  }x;
6538716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
6548716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
6558716de38SJoe Perches			(?:
6568716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
6578716de38SJoe Perches				(?:$typeTypedefs\b)|
6588716de38SJoe Perches				(?:${allWithAttr}\b)
6598716de38SJoe Perches			)
6608716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
6618716de38SJoe Perches		  }x;
6628905a67cSAndy Whitcroft	$Type	= qr{
663c45dcabdSAndy Whitcroft			$NonptrType
6641574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
665c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
6668905a67cSAndy Whitcroft		  }x;
6671813087dSJoe Perches	$TypeMisordered	= qr{
6681813087dSJoe Perches			$NonptrTypeMisordered
6691813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
6701813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
6711813087dSJoe Perches		  }x;
67291cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
6731813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
6748905a67cSAndy Whitcroft}
6758905a67cSAndy Whitcroftbuild_types();
6766c72ffaaSAndy Whitcroft
6777d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
678d1fe9c09SJoe Perches
679d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
680d1fe9c09SJoe Perches# requires at least perl version v5.10.0
681d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
682d1fe9c09SJoe Perches
683d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
6842435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
685c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
6867d2367afSJoe Perches
687f8422308SJoe Perchesour $declaration_macros = qr{(?x:
6883e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
689f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
690f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
691f8422308SJoe Perches)};
692f8422308SJoe Perches
6937d2367afSJoe Perchessub deparenthesize {
6947d2367afSJoe Perches	my ($string) = @_;
6957d2367afSJoe Perches	return "" if (!defined($string));
6965b9553abSJoe Perches
6975b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
6985b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
6995b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
7005b9553abSJoe Perches	}
7015b9553abSJoe Perches
7027d2367afSJoe Perches	$string =~ s@\s+@ @g;
7035b9553abSJoe Perches
7047d2367afSJoe Perches	return $string;
7057d2367afSJoe Perches}
7067d2367afSJoe Perches
7073445686aSJoe Perchessub seed_camelcase_file {
7083445686aSJoe Perches	my ($file) = @_;
7093445686aSJoe Perches
7103445686aSJoe Perches	return if (!(-f $file));
7113445686aSJoe Perches
7123445686aSJoe Perches	local $/;
7133445686aSJoe Perches
7143445686aSJoe Perches	open(my $include_file, '<', "$file")
7153445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
7163445686aSJoe Perches	my $text = <$include_file>;
7173445686aSJoe Perches	close($include_file);
7183445686aSJoe Perches
7193445686aSJoe Perches	my @lines = split('\n', $text);
7203445686aSJoe Perches
7213445686aSJoe Perches	foreach my $line (@lines) {
7223445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
7233445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
7243445686aSJoe Perches			$camelcase{$1} = 1;
72511ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
72611ea516aSJoe Perches			$camelcase{$1} = 1;
72711ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
7283445686aSJoe Perches			$camelcase{$1} = 1;
7293445686aSJoe Perches		}
7303445686aSJoe Perches	}
7313445686aSJoe Perches}
7323445686aSJoe Perches
73385b0ee18SJoe Perchessub is_maintained_obsolete {
73485b0ee18SJoe Perches	my ($filename) = @_;
73585b0ee18SJoe Perches
73685b0ee18SJoe Perches	return 0 if (!(-e "$root/scripts/get_maintainer.pl"));
73785b0ee18SJoe Perches
73885b0ee18SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback $filename 2>&1`;
73985b0ee18SJoe Perches
74085b0ee18SJoe Perches	return $status =~ /obsolete/i;
74185b0ee18SJoe Perches}
74285b0ee18SJoe Perches
7433445686aSJoe Perchesmy $camelcase_seeded = 0;
7443445686aSJoe Perchessub seed_camelcase_includes {
7453445686aSJoe Perches	return if ($camelcase_seeded);
7463445686aSJoe Perches
7473445686aSJoe Perches	my $files;
748c707a81dSJoe Perches	my $camelcase_cache = "";
749c707a81dSJoe Perches	my @include_files = ();
750c707a81dSJoe Perches
751c707a81dSJoe Perches	$camelcase_seeded = 1;
752351b2a1fSJoe Perches
7533645e328SRichard Genoud	if (-e ".git") {
754351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
755351b2a1fSJoe Perches		chomp $git_last_include_commit;
756c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
757c707a81dSJoe Perches	} else {
758c707a81dSJoe Perches		my $last_mod_date = 0;
759c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
760c707a81dSJoe Perches		@include_files = split('\n', $files);
761c707a81dSJoe Perches		foreach my $file (@include_files) {
762c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
763c707a81dSJoe Perches						   localtime((stat $file)[9]));
764c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
765c707a81dSJoe Perches		}
766c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
767c707a81dSJoe Perches	}
768c707a81dSJoe Perches
769c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
770c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
771c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
772351b2a1fSJoe Perches		while (<$camelcase_file>) {
773351b2a1fSJoe Perches			chomp;
774351b2a1fSJoe Perches			$camelcase{$_} = 1;
775351b2a1fSJoe Perches		}
776351b2a1fSJoe Perches		close($camelcase_file);
777351b2a1fSJoe Perches
778351b2a1fSJoe Perches		return;
779351b2a1fSJoe Perches	}
780c707a81dSJoe Perches
7813645e328SRichard Genoud	if (-e ".git") {
782c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
783c707a81dSJoe Perches		@include_files = split('\n', $files);
7843445686aSJoe Perches	}
785c707a81dSJoe Perches
7863445686aSJoe Perches	foreach my $file (@include_files) {
7873445686aSJoe Perches		seed_camelcase_file($file);
7883445686aSJoe Perches	}
789351b2a1fSJoe Perches
790c707a81dSJoe Perches	if ($camelcase_cache ne "") {
791351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
792c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
793c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
794351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
795351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
796351b2a1fSJoe Perches		}
797351b2a1fSJoe Perches		close($camelcase_file);
798351b2a1fSJoe Perches	}
7993445686aSJoe Perches}
8003445686aSJoe Perches
801d311cd44SJoe Perchessub git_commit_info {
802d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
803d311cd44SJoe Perches
804d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
805d311cd44SJoe Perches
806d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
807d311cd44SJoe Perches	$output =~ s/^\s*//gm;
808d311cd44SJoe Perches	my @lines = split("\n", $output);
809d311cd44SJoe Perches
8100d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
8110d7835fcSJoe Perches
812d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
813d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
814d311cd44SJoe Perches# all matching commit ids, but it's very slow...
815d311cd44SJoe Perches#
816d311cd44SJoe Perches#		echo "checking commits $1..."
817d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
818d311cd44SJoe Perches#		while read line ; do
819d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
820d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
821d311cd44SJoe Perches#		done
822d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
823d311cd44SJoe Perches	} else {
824d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
825d311cd44SJoe Perches		$desc = substr($lines[0], 41);
826d311cd44SJoe Perches	}
827d311cd44SJoe Perches
828d311cd44SJoe Perches	return ($id, $desc);
829d311cd44SJoe Perches}
830d311cd44SJoe Perches
8316c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
8320a920b5bSAndy Whitcroft
83300df344fSAndy Whitcroftmy @rawlines = ();
834c2fdda0dSAndy Whitcroftmy @lines = ();
8353705ce5bSJoe Perchesmy @fixed = ();
836d752fcc8SJoe Perchesmy @fixed_inserted = ();
837d752fcc8SJoe Perchesmy @fixed_deleted = ();
838194f66fcSJoe Perchesmy $fixlinenr = -1;
839194f66fcSJoe Perches
8404a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
8414a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
8424a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
8434a593c34SDu, Changbin
8444a593c34SDu, Changbinif ($git) {
8454a593c34SDu, Changbin	my @commits = ();
8460dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
8474a593c34SDu, Changbin		my $git_range;
84828898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
84928898fd1SJoe Perches			$git_range = "-$2 $1";
8504a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
8514a593c34SDu, Changbin			$git_range = "$commit_expr";
8524a593c34SDu, Changbin		} else {
8530dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
8540dea9f1eSJoe Perches		}
8550dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
8560dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
85728898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
85828898fd1SJoe Perches			next if (!defined($1) || !defined($2));
8590dea9f1eSJoe Perches			my $sha1 = $1;
8600dea9f1eSJoe Perches			my $subject = $2;
8610dea9f1eSJoe Perches			unshift(@commits, $sha1);
8620dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
8634a593c34SDu, Changbin		}
8644a593c34SDu, Changbin	}
8654a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
8664a593c34SDu, Changbin	@ARGV = @commits;
8674a593c34SDu, Changbin}
8684a593c34SDu, Changbin
869c2fdda0dSAndy Whitcroftmy $vname;
8706c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
87121caa13cSAndy Whitcroft	my $FILE;
8724a593c34SDu, Changbin	if ($git) {
8734a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
8744a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
8754a593c34SDu, Changbin	} elsif ($file) {
87621caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
8776c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
87821caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
87921caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
8806c72ffaaSAndy Whitcroft	} else {
88121caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
8826c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
8836c72ffaaSAndy Whitcroft	}
884c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
885c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
8864a593c34SDu, Changbin	} elsif ($git) {
8870dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
888c2fdda0dSAndy Whitcroft	} else {
889c2fdda0dSAndy Whitcroft		$vname = $filename;
890c2fdda0dSAndy Whitcroft	}
89121caa13cSAndy Whitcroft	while (<$FILE>) {
8920a920b5bSAndy Whitcroft		chomp;
89300df344fSAndy Whitcroft		push(@rawlines, $_);
8946c72ffaaSAndy Whitcroft	}
89521caa13cSAndy Whitcroft	close($FILE);
896d8469f16SJoe Perches
897d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
898d8469f16SJoe Perches		print '-' x length($vname) . "\n";
899d8469f16SJoe Perches		print "$vname\n";
900d8469f16SJoe Perches		print '-' x length($vname) . "\n";
901d8469f16SJoe Perches	}
902d8469f16SJoe Perches
903c2fdda0dSAndy Whitcroft	if (!process($filename)) {
9040a920b5bSAndy Whitcroft		$exit = 1;
9050a920b5bSAndy Whitcroft	}
90600df344fSAndy Whitcroft	@rawlines = ();
90713214adfSAndy Whitcroft	@lines = ();
9083705ce5bSJoe Perches	@fixed = ();
909d752fcc8SJoe Perches	@fixed_inserted = ();
910d752fcc8SJoe Perches	@fixed_deleted = ();
911194f66fcSJoe Perches	$fixlinenr = -1;
912485ff23eSAlex Dowad	@modifierListFile = ();
913485ff23eSAlex Dowad	@typeListFile = ();
914485ff23eSAlex Dowad	build_types();
9150a920b5bSAndy Whitcroft}
9160a920b5bSAndy Whitcroft
917d8469f16SJoe Perchesif (!$quiet) {
9183c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
9193c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
9203c816e49SJoe Perches
921d8469f16SJoe Perches	if ($^V lt 5.10.0) {
922d8469f16SJoe Perches		print << "EOM"
923d8469f16SJoe Perches
924d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
925d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
926d8469f16SJoe PerchesEOM
927d8469f16SJoe Perches	}
928d8469f16SJoe Perches	if ($exit) {
929d8469f16SJoe Perches		print << "EOM"
930d8469f16SJoe Perches
931d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
932d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
933d8469f16SJoe PerchesEOM
934d8469f16SJoe Perches	}
935d8469f16SJoe Perches}
936d8469f16SJoe Perches
9370a920b5bSAndy Whitcroftexit($exit);
9380a920b5bSAndy Whitcroft
9390a920b5bSAndy Whitcroftsub top_of_kernel_tree {
9406c72ffaaSAndy Whitcroft	my ($root) = @_;
9416c72ffaaSAndy Whitcroft
9426c72ffaaSAndy Whitcroft	my @tree_check = (
9436c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
9446c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
9456c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
9466c72ffaaSAndy Whitcroft	);
9476c72ffaaSAndy Whitcroft
9486c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
9496c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
9500a920b5bSAndy Whitcroft			return 0;
9510a920b5bSAndy Whitcroft		}
9526c72ffaaSAndy Whitcroft	}
9536c72ffaaSAndy Whitcroft	return 1;
9546c72ffaaSAndy Whitcroft}
9550a920b5bSAndy Whitcroft
95620112475SJoe Perchessub parse_email {
95720112475SJoe Perches	my ($formatted_email) = @_;
95820112475SJoe Perches
95920112475SJoe Perches	my $name = "";
96020112475SJoe Perches	my $address = "";
96120112475SJoe Perches	my $comment = "";
96220112475SJoe Perches
96320112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
96420112475SJoe Perches		$name = $1;
96520112475SJoe Perches		$address = $2;
96620112475SJoe Perches		$comment = $3 if defined $3;
96720112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
96820112475SJoe Perches		$address = $1;
96920112475SJoe Perches		$comment = $2 if defined $2;
97020112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
97120112475SJoe Perches		$address = $1;
97220112475SJoe Perches		$comment = $2 if defined $2;
97320112475SJoe Perches		$formatted_email =~ s/$address.*$//;
97420112475SJoe Perches		$name = $formatted_email;
9753705ce5bSJoe Perches		$name = trim($name);
97620112475SJoe Perches		$name =~ s/^\"|\"$//g;
97720112475SJoe Perches		# If there's a name left after stripping spaces and
97820112475SJoe Perches		# leading quotes, and the address doesn't have both
97920112475SJoe Perches		# leading and trailing angle brackets, the address
98020112475SJoe Perches		# is invalid. ie:
98120112475SJoe Perches		#   "joe smith [email protected]" bad
98220112475SJoe Perches		#   "joe smith <[email protected]" bad
98320112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
98420112475SJoe Perches			$name = "";
98520112475SJoe Perches			$address = "";
98620112475SJoe Perches			$comment = "";
98720112475SJoe Perches		}
98820112475SJoe Perches	}
98920112475SJoe Perches
9903705ce5bSJoe Perches	$name = trim($name);
99120112475SJoe Perches	$name =~ s/^\"|\"$//g;
9923705ce5bSJoe Perches	$address = trim($address);
99320112475SJoe Perches	$address =~ s/^\<|\>$//g;
99420112475SJoe Perches
99520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
99620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
99720112475SJoe Perches		$name = "\"$name\"";
99820112475SJoe Perches	}
99920112475SJoe Perches
100020112475SJoe Perches	return ($name, $address, $comment);
100120112475SJoe Perches}
100220112475SJoe Perches
100320112475SJoe Perchessub format_email {
100420112475SJoe Perches	my ($name, $address) = @_;
100520112475SJoe Perches
100620112475SJoe Perches	my $formatted_email;
100720112475SJoe Perches
10083705ce5bSJoe Perches	$name = trim($name);
100920112475SJoe Perches	$name =~ s/^\"|\"$//g;
10103705ce5bSJoe Perches	$address = trim($address);
101120112475SJoe Perches
101220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
101320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
101420112475SJoe Perches		$name = "\"$name\"";
101520112475SJoe Perches	}
101620112475SJoe Perches
101720112475SJoe Perches	if ("$name" eq "") {
101820112475SJoe Perches		$formatted_email = "$address";
101920112475SJoe Perches	} else {
102020112475SJoe Perches		$formatted_email = "$name <$address>";
102120112475SJoe Perches	}
102220112475SJoe Perches
102320112475SJoe Perches	return $formatted_email;
102420112475SJoe Perches}
102520112475SJoe Perches
1026d311cd44SJoe Perchessub which {
1027d311cd44SJoe Perches	my ($bin) = @_;
1028d311cd44SJoe Perches
1029d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1030d311cd44SJoe Perches		if (-e "$path/$bin") {
1031d311cd44SJoe Perches			return "$path/$bin";
1032d311cd44SJoe Perches		}
1033d311cd44SJoe Perches	}
1034d311cd44SJoe Perches
1035d311cd44SJoe Perches	return "";
1036d311cd44SJoe Perches}
1037d311cd44SJoe Perches
1038000d1cc1SJoe Perchessub which_conf {
1039000d1cc1SJoe Perches	my ($conf) = @_;
1040000d1cc1SJoe Perches
1041000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1042000d1cc1SJoe Perches		if (-e "$path/$conf") {
1043000d1cc1SJoe Perches			return "$path/$conf";
1044000d1cc1SJoe Perches		}
1045000d1cc1SJoe Perches	}
1046000d1cc1SJoe Perches
1047000d1cc1SJoe Perches	return "";
1048000d1cc1SJoe Perches}
1049000d1cc1SJoe Perches
10500a920b5bSAndy Whitcroftsub expand_tabs {
10510a920b5bSAndy Whitcroft	my ($str) = @_;
10520a920b5bSAndy Whitcroft
10530a920b5bSAndy Whitcroft	my $res = '';
10540a920b5bSAndy Whitcroft	my $n = 0;
10550a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
10560a920b5bSAndy Whitcroft		if ($c eq "\t") {
10570a920b5bSAndy Whitcroft			$res .= ' ';
10580a920b5bSAndy Whitcroft			$n++;
10590a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
10600a920b5bSAndy Whitcroft				$res .= ' ';
10610a920b5bSAndy Whitcroft			}
10620a920b5bSAndy Whitcroft			next;
10630a920b5bSAndy Whitcroft		}
10640a920b5bSAndy Whitcroft		$res .= $c;
10650a920b5bSAndy Whitcroft		$n++;
10660a920b5bSAndy Whitcroft	}
10670a920b5bSAndy Whitcroft
10680a920b5bSAndy Whitcroft	return $res;
10690a920b5bSAndy Whitcroft}
10706c72ffaaSAndy Whitcroftsub copy_spacing {
1071773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
10726c72ffaaSAndy Whitcroft	return $res;
10736c72ffaaSAndy Whitcroft}
10740a920b5bSAndy Whitcroft
10754a0df2efSAndy Whitcroftsub line_stats {
10764a0df2efSAndy Whitcroft	my ($line) = @_;
10774a0df2efSAndy Whitcroft
10784a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
10794a0df2efSAndy Whitcroft	$line =~ s/^.//;
10804a0df2efSAndy Whitcroft	$line = expand_tabs($line);
10814a0df2efSAndy Whitcroft
10824a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
10834a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
10844a0df2efSAndy Whitcroft
10854a0df2efSAndy Whitcroft	return (length($line), length($white));
10864a0df2efSAndy Whitcroft}
10874a0df2efSAndy Whitcroft
1088773647a0SAndy Whitcroftmy $sanitise_quote = '';
1089773647a0SAndy Whitcroft
1090773647a0SAndy Whitcroftsub sanitise_line_reset {
1091773647a0SAndy Whitcroft	my ($in_comment) = @_;
1092773647a0SAndy Whitcroft
1093773647a0SAndy Whitcroft	if ($in_comment) {
1094773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1095773647a0SAndy Whitcroft	} else {
1096773647a0SAndy Whitcroft		$sanitise_quote = '';
1097773647a0SAndy Whitcroft	}
1098773647a0SAndy Whitcroft}
109900df344fSAndy Whitcroftsub sanitise_line {
110000df344fSAndy Whitcroft	my ($line) = @_;
110100df344fSAndy Whitcroft
110200df344fSAndy Whitcroft	my $res = '';
110300df344fSAndy Whitcroft	my $l = '';
110400df344fSAndy Whitcroft
1105c2fdda0dSAndy Whitcroft	my $qlen = 0;
1106773647a0SAndy Whitcroft	my $off = 0;
1107773647a0SAndy Whitcroft	my $c;
110800df344fSAndy Whitcroft
1109773647a0SAndy Whitcroft	# Always copy over the diff marker.
1110773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1111773647a0SAndy Whitcroft
1112773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1113773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1114773647a0SAndy Whitcroft
1115773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
1116773647a0SAndy Whitcroft		# and end, all to $;.
1117773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1118773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1119773647a0SAndy Whitcroft
1120773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1121773647a0SAndy Whitcroft			$off++;
112200df344fSAndy Whitcroft			next;
1123773647a0SAndy Whitcroft		}
112481bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1125773647a0SAndy Whitcroft			$sanitise_quote = '';
1126773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1127773647a0SAndy Whitcroft			$off++;
1128773647a0SAndy Whitcroft			next;
1129773647a0SAndy Whitcroft		}
1130113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1131113f04a8SDaniel Walker			$sanitise_quote = '//';
1132113f04a8SDaniel Walker
1133113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1134113f04a8SDaniel Walker			$off++;
1135113f04a8SDaniel Walker			next;
1136113f04a8SDaniel Walker		}
1137773647a0SAndy Whitcroft
1138773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1139773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1140773647a0SAndy Whitcroft		    $c eq "\\") {
1141773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1142773647a0SAndy Whitcroft			$off++;
1143773647a0SAndy Whitcroft			next;
1144773647a0SAndy Whitcroft		}
1145773647a0SAndy Whitcroft		# Regular quotes.
1146773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1147773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1148773647a0SAndy Whitcroft				$sanitise_quote = $c;
1149773647a0SAndy Whitcroft
1150773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1151773647a0SAndy Whitcroft				next;
1152773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1153773647a0SAndy Whitcroft				$sanitise_quote = '';
115400df344fSAndy Whitcroft			}
115500df344fSAndy Whitcroft		}
1156773647a0SAndy Whitcroft
1157fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1158773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1159773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1160113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1161113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1162773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1163773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
116400df344fSAndy Whitcroft		} else {
1165773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
116600df344fSAndy Whitcroft		}
1167c2fdda0dSAndy Whitcroft	}
1168c2fdda0dSAndy Whitcroft
1169113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1170113f04a8SDaniel Walker		$sanitise_quote = '';
1171113f04a8SDaniel Walker	}
1172113f04a8SDaniel Walker
1173c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1174c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1175c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1176c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1177c2fdda0dSAndy Whitcroft
1178c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1179c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1180c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1181c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1182c2fdda0dSAndy Whitcroft	}
1183c2fdda0dSAndy Whitcroft
1184dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1185dadf680dSJoe Perches		my $match = $1;
1186dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1187dadf680dSJoe Perches	}
1188dadf680dSJoe Perches
118900df344fSAndy Whitcroft	return $res;
119000df344fSAndy Whitcroft}
119100df344fSAndy Whitcroft
1192a6962d72SJoe Perchessub get_quoted_string {
1193a6962d72SJoe Perches	my ($line, $rawline) = @_;
1194a6962d72SJoe Perches
119533acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1196a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1197a6962d72SJoe Perches}
1198a6962d72SJoe Perches
11998905a67cSAndy Whitcroftsub ctx_statement_block {
12008905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
12018905a67cSAndy Whitcroft	my $line = $linenr - 1;
12028905a67cSAndy Whitcroft	my $blk = '';
12038905a67cSAndy Whitcroft	my $soff = $off;
12048905a67cSAndy Whitcroft	my $coff = $off - 1;
1205773647a0SAndy Whitcroft	my $coff_set = 0;
12068905a67cSAndy Whitcroft
120713214adfSAndy Whitcroft	my $loff = 0;
120813214adfSAndy Whitcroft
12098905a67cSAndy Whitcroft	my $type = '';
12108905a67cSAndy Whitcroft	my $level = 0;
1211a2750645SAndy Whitcroft	my @stack = ();
1212cf655043SAndy Whitcroft	my $p;
12138905a67cSAndy Whitcroft	my $c;
12148905a67cSAndy Whitcroft	my $len = 0;
121513214adfSAndy Whitcroft
121613214adfSAndy Whitcroft	my $remainder;
12178905a67cSAndy Whitcroft	while (1) {
1218a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1219a2750645SAndy Whitcroft
1220773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
12218905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
12228905a67cSAndy Whitcroft		# context.
12238905a67cSAndy Whitcroft		if ($off >= $len) {
12248905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1225dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1226c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
12278905a67cSAndy Whitcroft				$remain--;
122813214adfSAndy Whitcroft				$loff = $len;
1229c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
12308905a67cSAndy Whitcroft				$len = length($blk);
12318905a67cSAndy Whitcroft				$line++;
12328905a67cSAndy Whitcroft				last;
12338905a67cSAndy Whitcroft			}
12348905a67cSAndy Whitcroft			# Bail if there is no further context.
12358905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
123613214adfSAndy Whitcroft			if ($off >= $len) {
12378905a67cSAndy Whitcroft				last;
12388905a67cSAndy Whitcroft			}
1239f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1240f74bd194SAndy Whitcroft				$level++;
1241f74bd194SAndy Whitcroft				$type = '#';
1242f74bd194SAndy Whitcroft			}
12438905a67cSAndy Whitcroft		}
1244cf655043SAndy Whitcroft		$p = $c;
12458905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
124613214adfSAndy Whitcroft		$remainder = substr($blk, $off);
12478905a67cSAndy Whitcroft
1248773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
12494635f4fbSAndy Whitcroft
12504635f4fbSAndy Whitcroft		# Handle nested #if/#else.
12514635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
12524635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
12534635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
12544635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
12554635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
12564635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
12574635f4fbSAndy Whitcroft		}
12584635f4fbSAndy Whitcroft
12598905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
12608905a67cSAndy Whitcroft		# outermost level.
12618905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
12628905a67cSAndy Whitcroft			last;
12638905a67cSAndy Whitcroft		}
12648905a67cSAndy Whitcroft
126513214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1266773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1267773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1268773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1269773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1270773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1271773647a0SAndy Whitcroft			$coff_set = 1;
1272773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1273773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
127413214adfSAndy Whitcroft		}
127513214adfSAndy Whitcroft
12768905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
12778905a67cSAndy Whitcroft			$level++;
12788905a67cSAndy Whitcroft			$type = '(';
12798905a67cSAndy Whitcroft		}
12808905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
12818905a67cSAndy Whitcroft			$level--;
12828905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
12838905a67cSAndy Whitcroft
12848905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
12858905a67cSAndy Whitcroft				$coff = $off;
1286773647a0SAndy Whitcroft				$coff_set = 1;
1287773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
12888905a67cSAndy Whitcroft			}
12898905a67cSAndy Whitcroft		}
12908905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
12918905a67cSAndy Whitcroft			$level++;
12928905a67cSAndy Whitcroft			$type = '{';
12938905a67cSAndy Whitcroft		}
12948905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
12958905a67cSAndy Whitcroft			$level--;
12968905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
12978905a67cSAndy Whitcroft
12988905a67cSAndy Whitcroft			if ($level == 0) {
1299b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1300b998e001SPatrick Pannuto					$off++;
1301b998e001SPatrick Pannuto				}
13028905a67cSAndy Whitcroft				last;
13038905a67cSAndy Whitcroft			}
13048905a67cSAndy Whitcroft		}
1305f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1306f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1307f74bd194SAndy Whitcroft			$level--;
1308f74bd194SAndy Whitcroft			$type = '';
1309f74bd194SAndy Whitcroft			$off++;
1310f74bd194SAndy Whitcroft			last;
1311f74bd194SAndy Whitcroft		}
13128905a67cSAndy Whitcroft		$off++;
13138905a67cSAndy Whitcroft	}
1314a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
131513214adfSAndy Whitcroft	if ($off == $len) {
1316a3bb97a7SAndy Whitcroft		$loff = $len + 1;
131713214adfSAndy Whitcroft		$line++;
131813214adfSAndy Whitcroft		$remain--;
131913214adfSAndy Whitcroft	}
13208905a67cSAndy Whitcroft
13218905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
13228905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
13238905a67cSAndy Whitcroft
13248905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
13258905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
13268905a67cSAndy Whitcroft
1327773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
132813214adfSAndy Whitcroft
132913214adfSAndy Whitcroft	return ($statement, $condition,
133013214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
133113214adfSAndy Whitcroft}
133213214adfSAndy Whitcroft
1333cf655043SAndy Whitcroftsub statement_lines {
1334cf655043SAndy Whitcroft	my ($stmt) = @_;
1335cf655043SAndy Whitcroft
1336cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1337cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1338cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1339cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1340cf655043SAndy Whitcroft
1341cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1342cf655043SAndy Whitcroft
1343cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1344cf655043SAndy Whitcroft}
1345cf655043SAndy Whitcroft
1346cf655043SAndy Whitcroftsub statement_rawlines {
1347cf655043SAndy Whitcroft	my ($stmt) = @_;
1348cf655043SAndy Whitcroft
1349cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1350cf655043SAndy Whitcroft
1351cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1352cf655043SAndy Whitcroft}
1353cf655043SAndy Whitcroft
1354cf655043SAndy Whitcroftsub statement_block_size {
1355cf655043SAndy Whitcroft	my ($stmt) = @_;
1356cf655043SAndy Whitcroft
1357cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1358cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1359cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1360cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1361cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1362cf655043SAndy Whitcroft
1363cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1364cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1365cf655043SAndy Whitcroft
1366cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1367cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1368cf655043SAndy Whitcroft
1369cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1370cf655043SAndy Whitcroft		return $stmt_lines;
1371cf655043SAndy Whitcroft	} else {
1372cf655043SAndy Whitcroft		return $stmt_statements;
1373cf655043SAndy Whitcroft	}
1374cf655043SAndy Whitcroft}
1375cf655043SAndy Whitcroft
137613214adfSAndy Whitcroftsub ctx_statement_full {
137713214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
137813214adfSAndy Whitcroft	my ($statement, $condition, $level);
137913214adfSAndy Whitcroft
138013214adfSAndy Whitcroft	my (@chunks);
138113214adfSAndy Whitcroft
1382cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
138313214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
138413214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1385773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
138613214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1387cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1388cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1389cf655043SAndy Whitcroft	}
1390cf655043SAndy Whitcroft
1391cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1392cf655043SAndy Whitcroft	# could continue the statement.
1393cf655043SAndy Whitcroft	for (;;) {
139413214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
139513214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1396cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1397773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1398cf655043SAndy Whitcroft		#print "C: push\n";
1399cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
140013214adfSAndy Whitcroft	}
140113214adfSAndy Whitcroft
140213214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
14038905a67cSAndy Whitcroft}
14048905a67cSAndy Whitcroft
14054a0df2efSAndy Whitcroftsub ctx_block_get {
1406f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
14074a0df2efSAndy Whitcroft	my $line;
14084a0df2efSAndy Whitcroft	my $start = $linenr - 1;
14094a0df2efSAndy Whitcroft	my $blk = '';
14104a0df2efSAndy Whitcroft	my @o;
14114a0df2efSAndy Whitcroft	my @c;
14124a0df2efSAndy Whitcroft	my @res = ();
14134a0df2efSAndy Whitcroft
1414f0a594c1SAndy Whitcroft	my $level = 0;
14154635f4fbSAndy Whitcroft	my @stack = ($level);
141600df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
141700df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
141800df344fSAndy Whitcroft		$remain--;
141900df344fSAndy Whitcroft
142000df344fSAndy Whitcroft		$blk .= $rawlines[$line];
14214635f4fbSAndy Whitcroft
14224635f4fbSAndy Whitcroft		# Handle nested #if/#else.
142301464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
14244635f4fbSAndy Whitcroft			push(@stack, $level);
142501464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
14264635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
142701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
14284635f4fbSAndy Whitcroft			$level = pop(@stack);
14294635f4fbSAndy Whitcroft		}
14304635f4fbSAndy Whitcroft
143101464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1432f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1433f0a594c1SAndy Whitcroft			if ($off > 0) {
1434f0a594c1SAndy Whitcroft				$off--;
1435f0a594c1SAndy Whitcroft				next;
1436f0a594c1SAndy Whitcroft			}
14374a0df2efSAndy Whitcroft
1438f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1439f0a594c1SAndy Whitcroft				$level--;
1440f0a594c1SAndy Whitcroft				last if ($level == 0);
1441f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1442f0a594c1SAndy Whitcroft				$level++;
1443f0a594c1SAndy Whitcroft			}
1444f0a594c1SAndy Whitcroft		}
14454a0df2efSAndy Whitcroft
1446f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
144700df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
14484a0df2efSAndy Whitcroft		}
14494a0df2efSAndy Whitcroft
1450f0a594c1SAndy Whitcroft		last if ($level == 0);
14514a0df2efSAndy Whitcroft	}
14524a0df2efSAndy Whitcroft
1453f0a594c1SAndy Whitcroft	return ($level, @res);
14544a0df2efSAndy Whitcroft}
14554a0df2efSAndy Whitcroftsub ctx_block_outer {
14564a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
14574a0df2efSAndy Whitcroft
1458f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1459f0a594c1SAndy Whitcroft	return @r;
14604a0df2efSAndy Whitcroft}
14614a0df2efSAndy Whitcroftsub ctx_block {
14624a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
14634a0df2efSAndy Whitcroft
1464f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1465f0a594c1SAndy Whitcroft	return @r;
1466653d4876SAndy Whitcroft}
1467653d4876SAndy Whitcroftsub ctx_statement {
1468f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1469f0a594c1SAndy Whitcroft
1470f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1471f0a594c1SAndy Whitcroft	return @r;
1472f0a594c1SAndy Whitcroft}
1473f0a594c1SAndy Whitcroftsub ctx_block_level {
1474653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1475653d4876SAndy Whitcroft
1476f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
14774a0df2efSAndy Whitcroft}
14789c0ca6f9SAndy Whitcroftsub ctx_statement_level {
14799c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
14809c0ca6f9SAndy Whitcroft
14819c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
14829c0ca6f9SAndy Whitcroft}
14834a0df2efSAndy Whitcroft
14844a0df2efSAndy Whitcroftsub ctx_locate_comment {
14854a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
14864a0df2efSAndy Whitcroft
14874a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1488beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
14894a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
14904a0df2efSAndy Whitcroft
14914a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
14924a0df2efSAndy Whitcroft	# comment.
14934a0df2efSAndy Whitcroft	my $in_comment = 0;
14944a0df2efSAndy Whitcroft	$current_comment = '';
14954a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
149600df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
149700df344fSAndy Whitcroft		#warn "           $line\n";
14984a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
14994a0df2efSAndy Whitcroft			$in_comment = 1;
15004a0df2efSAndy Whitcroft		}
15014a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
15024a0df2efSAndy Whitcroft			$in_comment = 1;
15034a0df2efSAndy Whitcroft		}
15044a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
15054a0df2efSAndy Whitcroft			$current_comment = '';
15064a0df2efSAndy Whitcroft		}
15074a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
15084a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
15094a0df2efSAndy Whitcroft			$in_comment = 0;
15104a0df2efSAndy Whitcroft		}
15114a0df2efSAndy Whitcroft	}
15124a0df2efSAndy Whitcroft
15134a0df2efSAndy Whitcroft	chomp($current_comment);
15144a0df2efSAndy Whitcroft	return($current_comment);
15154a0df2efSAndy Whitcroft}
15164a0df2efSAndy Whitcroftsub ctx_has_comment {
15174a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
15184a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
15194a0df2efSAndy Whitcroft
152000df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
15214a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
15224a0df2efSAndy Whitcroft
15234a0df2efSAndy Whitcroft	return ($cmt ne '');
15244a0df2efSAndy Whitcroft}
15254a0df2efSAndy Whitcroft
15264d001e4dSAndy Whitcroftsub raw_line {
15274d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
15284d001e4dSAndy Whitcroft
15294d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
15304d001e4dSAndy Whitcroft	$cnt++;
15314d001e4dSAndy Whitcroft
15324d001e4dSAndy Whitcroft	my $line;
15334d001e4dSAndy Whitcroft	while ($cnt) {
15344d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
15354d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
15364d001e4dSAndy Whitcroft		$cnt--;
15374d001e4dSAndy Whitcroft	}
15384d001e4dSAndy Whitcroft
15394d001e4dSAndy Whitcroft	return $line;
15404d001e4dSAndy Whitcroft}
15414d001e4dSAndy Whitcroft
15420a920b5bSAndy Whitcroftsub cat_vet {
15430a920b5bSAndy Whitcroft	my ($vet) = @_;
15449c0ca6f9SAndy Whitcroft	my ($res, $coded);
15450a920b5bSAndy Whitcroft
15469c0ca6f9SAndy Whitcroft	$res = '';
15476c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
15486c72ffaaSAndy Whitcroft		$res .= $1;
15496c72ffaaSAndy Whitcroft		if ($2 ne '') {
15509c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
15516c72ffaaSAndy Whitcroft			$res .= $coded;
15526c72ffaaSAndy Whitcroft		}
15539c0ca6f9SAndy Whitcroft	}
15549c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
15550a920b5bSAndy Whitcroft
15569c0ca6f9SAndy Whitcroft	return $res;
15570a920b5bSAndy Whitcroft}
15580a920b5bSAndy Whitcroft
1559c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1560cf655043SAndy Whitcroftmy $av_pending;
1561c2fdda0dSAndy Whitcroftmy @av_paren_type;
15621f65f947SAndy Whitcroftmy $av_pend_colon;
1563c2fdda0dSAndy Whitcroft
1564c2fdda0dSAndy Whitcroftsub annotate_reset {
1565c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1566cf655043SAndy Whitcroft	$av_pending = '_';
1567cf655043SAndy Whitcroft	@av_paren_type = ('E');
15681f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1569c2fdda0dSAndy Whitcroft}
1570c2fdda0dSAndy Whitcroft
15716c72ffaaSAndy Whitcroftsub annotate_values {
15726c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
15736c72ffaaSAndy Whitcroft
15746c72ffaaSAndy Whitcroft	my $res;
15751f65f947SAndy Whitcroft	my $var = '_' x length($stream);
15766c72ffaaSAndy Whitcroft	my $cur = $stream;
15776c72ffaaSAndy Whitcroft
1578c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
15796c72ffaaSAndy Whitcroft
15806c72ffaaSAndy Whitcroft	while (length($cur)) {
1581773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1582cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1583171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
15846c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1585c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1586c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1587cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1588c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
15896c72ffaaSAndy Whitcroft			}
15906c72ffaaSAndy Whitcroft
1591c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
15929446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
15939446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1594addcdceaSAndy Whitcroft			$type = 'c';
15959446ef56SAndy Whitcroft
1596e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1597c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
15986c72ffaaSAndy Whitcroft			$type = 'T';
15996c72ffaaSAndy Whitcroft
1600389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1601389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1602389a2fe5SAndy Whitcroft			$type = 'T';
1603389a2fe5SAndy Whitcroft
1604c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1605171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1606c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1607171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1608171ae1a4SAndy Whitcroft			if ($2 ne '') {
1609cf655043SAndy Whitcroft				$av_pending = 'N';
1610171ae1a4SAndy Whitcroft			}
1611171ae1a4SAndy Whitcroft			$type = 'E';
1612171ae1a4SAndy Whitcroft
1613c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1614171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1615171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1616171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
16176c72ffaaSAndy Whitcroft
1618c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1619cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1620c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1621cf655043SAndy Whitcroft
1622cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1623cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1624171ae1a4SAndy Whitcroft			$type = 'E';
1625cf655043SAndy Whitcroft
1626c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1627cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1628cf655043SAndy Whitcroft			$av_preprocessor = 1;
1629cf655043SAndy Whitcroft
1630cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1631cf655043SAndy Whitcroft
1632171ae1a4SAndy Whitcroft			$type = 'E';
1633cf655043SAndy Whitcroft
1634c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1635cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1636cf655043SAndy Whitcroft
1637cf655043SAndy Whitcroft			$av_preprocessor = 1;
1638cf655043SAndy Whitcroft
1639cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1640cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1641cf655043SAndy Whitcroft			pop(@av_paren_type);
1642cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1643171ae1a4SAndy Whitcroft			$type = 'E';
16446c72ffaaSAndy Whitcroft
16456c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1646c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
16476c72ffaaSAndy Whitcroft
1648171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1649171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1650171ae1a4SAndy Whitcroft			$av_pending = $type;
1651171ae1a4SAndy Whitcroft			$type = 'N';
1652171ae1a4SAndy Whitcroft
16536c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1654c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
16556c72ffaaSAndy Whitcroft			if (defined $2) {
1656cf655043SAndy Whitcroft				$av_pending = 'V';
16576c72ffaaSAndy Whitcroft			}
16586c72ffaaSAndy Whitcroft			$type = 'N';
16596c72ffaaSAndy Whitcroft
166014b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1661c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
166214b111c1SAndy Whitcroft			$av_pending = 'E';
16636c72ffaaSAndy Whitcroft			$type = 'N';
16646c72ffaaSAndy Whitcroft
16651f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
16661f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
16671f65f947SAndy Whitcroft			$av_pend_colon = 'C';
16681f65f947SAndy Whitcroft			$type = 'N';
16691f65f947SAndy Whitcroft
167014b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1671c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
16726c72ffaaSAndy Whitcroft			$type = 'N';
16736c72ffaaSAndy Whitcroft
16746c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1675c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1676cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1677cf655043SAndy Whitcroft			$av_pending = '_';
16786c72ffaaSAndy Whitcroft			$type = 'N';
16796c72ffaaSAndy Whitcroft
16806c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1681cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1682cf655043SAndy Whitcroft			if ($new_type ne '_') {
1683cf655043SAndy Whitcroft				$type = $new_type;
1684c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1685c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
16866c72ffaaSAndy Whitcroft			} else {
1687c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
16886c72ffaaSAndy Whitcroft			}
16896c72ffaaSAndy Whitcroft
1690c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1691c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1692c8cb2ca3SAndy Whitcroft			$type = 'V';
1693cf655043SAndy Whitcroft			$av_pending = 'V';
16946c72ffaaSAndy Whitcroft
16958e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
16968e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
16971f65f947SAndy Whitcroft				$av_pend_colon = 'B';
16988e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
16998e761b04SAndy Whitcroft				$av_pend_colon = 'L';
17001f65f947SAndy Whitcroft			}
17011f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
17021f65f947SAndy Whitcroft			$type = 'V';
17031f65f947SAndy Whitcroft
17046c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1705c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
17066c72ffaaSAndy Whitcroft			$type = 'V';
17076c72ffaaSAndy Whitcroft
17086c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1709c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
17106c72ffaaSAndy Whitcroft			$type = 'N';
17116c72ffaaSAndy Whitcroft
1712cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1713c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
171413214adfSAndy Whitcroft			$type = 'E';
17151f65f947SAndy Whitcroft			$av_pend_colon = 'O';
171613214adfSAndy Whitcroft
17178e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
17188e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
17198e761b04SAndy Whitcroft			$type = 'C';
17208e761b04SAndy Whitcroft
17211f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
17221f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
17231f65f947SAndy Whitcroft			$type = 'N';
17241f65f947SAndy Whitcroft
17251f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
17261f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
17271f65f947SAndy Whitcroft
17281f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
17291f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
17301f65f947SAndy Whitcroft				$type = 'E';
17311f65f947SAndy Whitcroft			} else {
17321f65f947SAndy Whitcroft				$type = 'N';
17331f65f947SAndy Whitcroft			}
17341f65f947SAndy Whitcroft			$av_pend_colon = 'O';
17351f65f947SAndy Whitcroft
17368e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
173713214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
17386c72ffaaSAndy Whitcroft			$type = 'N';
17396c72ffaaSAndy Whitcroft
17400d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
174174048ed8SAndy Whitcroft			my $variant;
174274048ed8SAndy Whitcroft
174374048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
174474048ed8SAndy Whitcroft			if ($type eq 'V') {
174574048ed8SAndy Whitcroft				$variant = 'B';
174674048ed8SAndy Whitcroft			} else {
174774048ed8SAndy Whitcroft				$variant = 'U';
174874048ed8SAndy Whitcroft			}
174974048ed8SAndy Whitcroft
175074048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
175174048ed8SAndy Whitcroft			$type = 'N';
175274048ed8SAndy Whitcroft
17536c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1754c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
17556c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
17566c72ffaaSAndy Whitcroft				$type = 'N';
17576c72ffaaSAndy Whitcroft			}
17586c72ffaaSAndy Whitcroft
17596c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1760c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
17616c72ffaaSAndy Whitcroft		}
17626c72ffaaSAndy Whitcroft		if (defined $1) {
17636c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
17646c72ffaaSAndy Whitcroft			$res .= $type x length($1);
17656c72ffaaSAndy Whitcroft		}
17666c72ffaaSAndy Whitcroft	}
17676c72ffaaSAndy Whitcroft
17681f65f947SAndy Whitcroft	return ($res, $var);
17696c72ffaaSAndy Whitcroft}
17706c72ffaaSAndy Whitcroft
17718905a67cSAndy Whitcroftsub possible {
177213214adfSAndy Whitcroft	my ($possible, $line) = @_;
17739a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
17740776e594SAndy Whitcroft		^(?:
17750776e594SAndy Whitcroft			$Modifier|
17760776e594SAndy Whitcroft			$Storage|
17770776e594SAndy Whitcroft			$Type|
17789a974fdbSAndy Whitcroft			DEFINE_\S+
17799a974fdbSAndy Whitcroft		)$|
17809a974fdbSAndy Whitcroft		^(?:
17810776e594SAndy Whitcroft			goto|
17820776e594SAndy Whitcroft			return|
17830776e594SAndy Whitcroft			case|
17840776e594SAndy Whitcroft			else|
17850776e594SAndy Whitcroft			asm|__asm__|
178689a88353SAndy Whitcroft			do|
178789a88353SAndy Whitcroft			\#|
178889a88353SAndy Whitcroft			\#\#|
17899a974fdbSAndy Whitcroft		)(?:\s|$)|
17900776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
17919a974fdbSAndy Whitcroft	    )}x;
17929a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
17939a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1794c45dcabdSAndy Whitcroft		# Check for modifiers.
1795c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1796c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1797c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1798c45dcabdSAndy Whitcroft
1799c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1800c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1801d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
18029a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1803d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1804485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1805d2506586SAndy Whitcroft				}
18069a974fdbSAndy Whitcroft			}
1807c45dcabdSAndy Whitcroft
1808c45dcabdSAndy Whitcroft		} else {
180913214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1810485ff23eSAlex Dowad			push(@typeListFile, $possible);
1811c45dcabdSAndy Whitcroft		}
18128905a67cSAndy Whitcroft		build_types();
18130776e594SAndy Whitcroft	} else {
18140776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
18158905a67cSAndy Whitcroft	}
18168905a67cSAndy Whitcroft}
18178905a67cSAndy Whitcroft
18186c72ffaaSAndy Whitcroftmy $prefix = '';
18196c72ffaaSAndy Whitcroft
1820000d1cc1SJoe Perchessub show_type {
1821cbec18afSJoe Perches	my ($type) = @_;
182291bfe484SJoe Perches
1823cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1824cbec18afSJoe Perches
1825cbec18afSJoe Perches	return !defined $ignore_type{$type};
1826000d1cc1SJoe Perches}
1827000d1cc1SJoe Perches
1828f0a594c1SAndy Whitcroftsub report {
1829cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1830cbec18afSJoe Perches
1831cbec18afSJoe Perches	if (!show_type($type) ||
1832cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1833773647a0SAndy Whitcroft		return 0;
1834773647a0SAndy Whitcroft	}
183557230297SJoe Perches	my $output = '';
183657230297SJoe Perches	if (-t STDOUT && $color) {
183757230297SJoe Perches		if ($level eq 'ERROR') {
183857230297SJoe Perches			$output .= RED;
183957230297SJoe Perches		} elsif ($level eq 'WARNING') {
184057230297SJoe Perches			$output .= YELLOW;
1841000d1cc1SJoe Perches		} else {
184257230297SJoe Perches			$output .= GREEN;
1843000d1cc1SJoe Perches		}
184457230297SJoe Perches	}
184557230297SJoe Perches	$output .= $prefix . $level . ':';
184657230297SJoe Perches	if ($show_types) {
184757230297SJoe Perches		$output .= BLUE if (-t STDOUT && $color);
184857230297SJoe Perches		$output .= "$type:";
184957230297SJoe Perches	}
185057230297SJoe Perches	$output .= RESET if (-t STDOUT && $color);
185157230297SJoe Perches	$output .= ' ' . $msg . "\n";
185234d8815fSJoe Perches
185334d8815fSJoe Perches	if ($showfile) {
185434d8815fSJoe Perches		my @lines = split("\n", $output, -1);
185534d8815fSJoe Perches		splice(@lines, 1, 1);
185634d8815fSJoe Perches		$output = join("\n", @lines);
185734d8815fSJoe Perches	}
185857230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
18598905a67cSAndy Whitcroft
186057230297SJoe Perches	push(our @report, $output);
1861773647a0SAndy Whitcroft
1862773647a0SAndy Whitcroft	return 1;
1863f0a594c1SAndy Whitcroft}
1864cbec18afSJoe Perches
1865f0a594c1SAndy Whitcroftsub report_dump {
186613214adfSAndy Whitcroft	our @report;
1867f0a594c1SAndy Whitcroft}
1868000d1cc1SJoe Perches
1869d752fcc8SJoe Perchessub fixup_current_range {
1870d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1871d752fcc8SJoe Perches
1872d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1873d752fcc8SJoe Perches		my $o = $1;
1874d752fcc8SJoe Perches		my $l = $2;
1875d752fcc8SJoe Perches		my $no = $o + $offset;
1876d752fcc8SJoe Perches		my $nl = $l + $length;
1877d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1878d752fcc8SJoe Perches	}
1879d752fcc8SJoe Perches}
1880d752fcc8SJoe Perches
1881d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1882d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1883d752fcc8SJoe Perches
1884d752fcc8SJoe Perches	my $range_last_linenr = 0;
1885d752fcc8SJoe Perches	my $delta_offset = 0;
1886d752fcc8SJoe Perches
1887d752fcc8SJoe Perches	my $old_linenr = 0;
1888d752fcc8SJoe Perches	my $new_linenr = 0;
1889d752fcc8SJoe Perches
1890d752fcc8SJoe Perches	my $next_insert = 0;
1891d752fcc8SJoe Perches	my $next_delete = 0;
1892d752fcc8SJoe Perches
1893d752fcc8SJoe Perches	my @lines = ();
1894d752fcc8SJoe Perches
1895d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1896d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1897d752fcc8SJoe Perches
1898d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1899d752fcc8SJoe Perches		my $save_line = 1;
1900d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1901323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1902d752fcc8SJoe Perches			$delta_offset = 0;
1903d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1904d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1905d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1906d752fcc8SJoe Perches		}
1907d752fcc8SJoe Perches
1908d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1909d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1910d752fcc8SJoe Perches			$save_line = 0;
1911d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1912d752fcc8SJoe Perches		}
1913d752fcc8SJoe Perches
1914d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1915d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1916d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1917d752fcc8SJoe Perches			$new_linenr++;
1918d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1919d752fcc8SJoe Perches		}
1920d752fcc8SJoe Perches
1921d752fcc8SJoe Perches		if ($save_line) {
1922d752fcc8SJoe Perches			push(@lines, $line);
1923d752fcc8SJoe Perches			$new_linenr++;
1924d752fcc8SJoe Perches		}
1925d752fcc8SJoe Perches
1926d752fcc8SJoe Perches		$old_linenr++;
1927d752fcc8SJoe Perches	}
1928d752fcc8SJoe Perches
1929d752fcc8SJoe Perches	return @lines;
1930d752fcc8SJoe Perches}
1931d752fcc8SJoe Perches
1932f2d7e4d4SJoe Perchessub fix_insert_line {
1933f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1934f2d7e4d4SJoe Perches
1935f2d7e4d4SJoe Perches	my $inserted = {
1936f2d7e4d4SJoe Perches		LINENR => $linenr,
1937f2d7e4d4SJoe Perches		LINE => $line,
1938f2d7e4d4SJoe Perches	};
1939f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1940f2d7e4d4SJoe Perches}
1941f2d7e4d4SJoe Perches
1942f2d7e4d4SJoe Perchessub fix_delete_line {
1943f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1944f2d7e4d4SJoe Perches
1945f2d7e4d4SJoe Perches	my $deleted = {
1946f2d7e4d4SJoe Perches		LINENR => $linenr,
1947f2d7e4d4SJoe Perches		LINE => $line,
1948f2d7e4d4SJoe Perches	};
1949f2d7e4d4SJoe Perches
1950f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1951f2d7e4d4SJoe Perches}
1952f2d7e4d4SJoe Perches
1953de7d4f0eSAndy Whitcroftsub ERROR {
1954cbec18afSJoe Perches	my ($type, $msg) = @_;
1955cbec18afSJoe Perches
1956cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1957de7d4f0eSAndy Whitcroft		our $clean = 0;
19586c72ffaaSAndy Whitcroft		our $cnt_error++;
19593705ce5bSJoe Perches		return 1;
1960de7d4f0eSAndy Whitcroft	}
19613705ce5bSJoe Perches	return 0;
1962773647a0SAndy Whitcroft}
1963de7d4f0eSAndy Whitcroftsub WARN {
1964cbec18afSJoe Perches	my ($type, $msg) = @_;
1965cbec18afSJoe Perches
1966cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1967de7d4f0eSAndy Whitcroft		our $clean = 0;
19686c72ffaaSAndy Whitcroft		our $cnt_warn++;
19693705ce5bSJoe Perches		return 1;
1970de7d4f0eSAndy Whitcroft	}
19713705ce5bSJoe Perches	return 0;
1972773647a0SAndy Whitcroft}
1973de7d4f0eSAndy Whitcroftsub CHK {
1974cbec18afSJoe Perches	my ($type, $msg) = @_;
1975cbec18afSJoe Perches
1976cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1977de7d4f0eSAndy Whitcroft		our $clean = 0;
19786c72ffaaSAndy Whitcroft		our $cnt_chk++;
19793705ce5bSJoe Perches		return 1;
19806c72ffaaSAndy Whitcroft	}
19813705ce5bSJoe Perches	return 0;
1982de7d4f0eSAndy Whitcroft}
1983de7d4f0eSAndy Whitcroft
19846ecd9674SAndy Whitcroftsub check_absolute_file {
19856ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
19866ecd9674SAndy Whitcroft	my $file = $absolute;
19876ecd9674SAndy Whitcroft
19886ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
19896ecd9674SAndy Whitcroft
19906ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
19916ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
19926ecd9674SAndy Whitcroft		if (-f "$root/$file") {
19936ecd9674SAndy Whitcroft			##print "file<$file>\n";
19946ecd9674SAndy Whitcroft			last;
19956ecd9674SAndy Whitcroft		}
19966ecd9674SAndy Whitcroft	}
19976ecd9674SAndy Whitcroft	if (! -f _)  {
19986ecd9674SAndy Whitcroft		return 0;
19996ecd9674SAndy Whitcroft	}
20006ecd9674SAndy Whitcroft
20016ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
20026ecd9674SAndy Whitcroft	my $prefix = $absolute;
20036ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
20046ecd9674SAndy Whitcroft
20056ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
20066ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2007000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2008000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
20096ecd9674SAndy Whitcroft	}
20106ecd9674SAndy Whitcroft}
20116ecd9674SAndy Whitcroft
20123705ce5bSJoe Perchessub trim {
20133705ce5bSJoe Perches	my ($string) = @_;
20143705ce5bSJoe Perches
2015b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2016b34c648bSJoe Perches
2017b34c648bSJoe Perches	return $string;
2018b34c648bSJoe Perches}
2019b34c648bSJoe Perches
2020b34c648bSJoe Perchessub ltrim {
2021b34c648bSJoe Perches	my ($string) = @_;
2022b34c648bSJoe Perches
2023b34c648bSJoe Perches	$string =~ s/^\s+//;
2024b34c648bSJoe Perches
2025b34c648bSJoe Perches	return $string;
2026b34c648bSJoe Perches}
2027b34c648bSJoe Perches
2028b34c648bSJoe Perchessub rtrim {
2029b34c648bSJoe Perches	my ($string) = @_;
2030b34c648bSJoe Perches
2031b34c648bSJoe Perches	$string =~ s/\s+$//;
20323705ce5bSJoe Perches
20333705ce5bSJoe Perches	return $string;
20343705ce5bSJoe Perches}
20353705ce5bSJoe Perches
203652ea8506SJoe Perchessub string_find_replace {
203752ea8506SJoe Perches	my ($string, $find, $replace) = @_;
203852ea8506SJoe Perches
203952ea8506SJoe Perches	$string =~ s/$find/$replace/g;
204052ea8506SJoe Perches
204152ea8506SJoe Perches	return $string;
204252ea8506SJoe Perches}
204352ea8506SJoe Perches
20443705ce5bSJoe Perchessub tabify {
20453705ce5bSJoe Perches	my ($leading) = @_;
20463705ce5bSJoe Perches
20473705ce5bSJoe Perches	my $source_indent = 8;
20483705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
20493705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
20503705ce5bSJoe Perches
20513705ce5bSJoe Perches	#convert leading spaces to tabs
20523705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
20533705ce5bSJoe Perches	#Remove spaces before a tab
20543705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
20553705ce5bSJoe Perches
20563705ce5bSJoe Perches	return "$leading";
20573705ce5bSJoe Perches}
20583705ce5bSJoe Perches
2059d1fe9c09SJoe Perchessub pos_last_openparen {
2060d1fe9c09SJoe Perches	my ($line) = @_;
2061d1fe9c09SJoe Perches
2062d1fe9c09SJoe Perches	my $pos = 0;
2063d1fe9c09SJoe Perches
2064d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2065d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2066d1fe9c09SJoe Perches
2067d1fe9c09SJoe Perches	my $last_openparen = 0;
2068d1fe9c09SJoe Perches
2069d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2070d1fe9c09SJoe Perches		return -1;
2071d1fe9c09SJoe Perches	}
2072d1fe9c09SJoe Perches
2073d1fe9c09SJoe Perches	my $len = length($line);
2074d1fe9c09SJoe Perches
2075d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2076d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2077d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2078d1fe9c09SJoe Perches			$pos += length($1) - 1;
2079d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2080d1fe9c09SJoe Perches			$last_openparen = $pos;
2081d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2082d1fe9c09SJoe Perches			last;
2083d1fe9c09SJoe Perches		}
2084d1fe9c09SJoe Perches	}
2085d1fe9c09SJoe Perches
208691cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2087d1fe9c09SJoe Perches}
2088d1fe9c09SJoe Perches
20890a920b5bSAndy Whitcroftsub process {
20900a920b5bSAndy Whitcroft	my $filename = shift;
20910a920b5bSAndy Whitcroft
20920a920b5bSAndy Whitcroft	my $linenr=0;
20930a920b5bSAndy Whitcroft	my $prevline="";
2094c2fdda0dSAndy Whitcroft	my $prevrawline="";
20950a920b5bSAndy Whitcroft	my $stashline="";
2096c2fdda0dSAndy Whitcroft	my $stashrawline="";
20970a920b5bSAndy Whitcroft
20984a0df2efSAndy Whitcroft	my $length;
20990a920b5bSAndy Whitcroft	my $indent;
21000a920b5bSAndy Whitcroft	my $previndent=0;
21010a920b5bSAndy Whitcroft	my $stashindent=0;
21020a920b5bSAndy Whitcroft
2103de7d4f0eSAndy Whitcroft	our $clean = 1;
21040a920b5bSAndy Whitcroft	my $signoff = 0;
21050a920b5bSAndy Whitcroft	my $is_patch = 0;
210629ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
210715662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2108ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2109bf4daf12SJoe Perches       my $commit_log_possible_stack_dump = 0;
21102a076f40SJoe Perches	my $commit_log_long_line = 0;
2111e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
211213f1937eSJoe Perches	my $reported_maintainer_file = 0;
2113fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2114fa64205dSPasi Savanainen
2115365dd4eaSJoe Perches	my $last_blank_line = 0;
21165e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2117365dd4eaSJoe Perches
211813214adfSAndy Whitcroft	our @report = ();
21196c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
21206c72ffaaSAndy Whitcroft	our $cnt_error = 0;
21216c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
21226c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
21236c72ffaaSAndy Whitcroft
21240a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
21250a920b5bSAndy Whitcroft	my $realfile = '';
21260a920b5bSAndy Whitcroft	my $realline = 0;
21270a920b5bSAndy Whitcroft	my $realcnt = 0;
21280a920b5bSAndy Whitcroft	my $here = '';
21290a920b5bSAndy Whitcroft	my $in_comment = 0;
2130c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
21310a920b5bSAndy Whitcroft	my $first_line = 0;
21321e855726SWolfram Sang	my $p1_prefix = '';
21330a920b5bSAndy Whitcroft
213413214adfSAndy Whitcroft	my $prev_values = 'E';
213513214adfSAndy Whitcroft
213613214adfSAndy Whitcroft	# suppression flags
2137773647a0SAndy Whitcroft	my %suppress_ifbraces;
2138170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
21392b474a1aSAndy Whitcroft	my %suppress_export;
21403e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2141653d4876SAndy Whitcroft
21427e51f197SJoe Perches	my %signatures = ();
2143323c1260SJoe Perches
2144c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2145de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2146c2fdda0dSAndy Whitcroft	#
2147de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2148de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2149773647a0SAndy Whitcroft
2150d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2151d8b07710SJoe Perches
2152773647a0SAndy Whitcroft	sanitise_line_reset();
2153c2fdda0dSAndy Whitcroft	my $line;
2154c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2155773647a0SAndy Whitcroft		$linenr++;
2156773647a0SAndy Whitcroft		$line = $rawline;
2157c2fdda0dSAndy Whitcroft
21583705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
21593705ce5bSJoe Perches
2160773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2161de7d4f0eSAndy Whitcroft			$setup_docs = 0;
2162de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2163de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2164de7d4f0eSAndy Whitcroft			}
2165773647a0SAndy Whitcroft			#next;
2166de7d4f0eSAndy Whitcroft		}
2167773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2168773647a0SAndy Whitcroft			$realline=$1-1;
2169773647a0SAndy Whitcroft			if (defined $2) {
2170773647a0SAndy Whitcroft				$realcnt=$3+1;
2171773647a0SAndy Whitcroft			} else {
2172773647a0SAndy Whitcroft				$realcnt=1+1;
2173773647a0SAndy Whitcroft			}
2174c45dcabdSAndy Whitcroft			$in_comment = 0;
2175773647a0SAndy Whitcroft
2176773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2177773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2178773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2179773647a0SAndy Whitcroft			# at context start.
2180773647a0SAndy Whitcroft			my $edge;
218101fa9147SAndy Whitcroft			my $cnt = $realcnt;
218201fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
218301fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
218401fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
218501fa9147SAndy Whitcroft				$cnt--;
218601fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2187721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2188fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2189fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2190fae17daeSAndy Whitcroft					($edge) = $1;
2191fae17daeSAndy Whitcroft					last;
2192fae17daeSAndy Whitcroft				}
2193773647a0SAndy Whitcroft			}
2194773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2195773647a0SAndy Whitcroft				$in_comment = 1;
2196773647a0SAndy Whitcroft			}
2197773647a0SAndy Whitcroft
2198773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2199773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2200773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2201773647a0SAndy Whitcroft			if (!defined $edge &&
220283242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2203773647a0SAndy Whitcroft			{
2204773647a0SAndy Whitcroft				$in_comment = 1;
2205773647a0SAndy Whitcroft			}
2206773647a0SAndy Whitcroft
2207773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2208773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2209773647a0SAndy Whitcroft
2210171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2211773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2212171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2213773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2214773647a0SAndy Whitcroft		}
2215773647a0SAndy Whitcroft		push(@lines, $line);
2216773647a0SAndy Whitcroft
2217773647a0SAndy Whitcroft		if ($realcnt > 1) {
2218773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2219773647a0SAndy Whitcroft		} else {
2220773647a0SAndy Whitcroft			$realcnt = 0;
2221773647a0SAndy Whitcroft		}
2222773647a0SAndy Whitcroft
2223773647a0SAndy Whitcroft		#print "==>$rawline\n";
2224773647a0SAndy Whitcroft		#print "-->$line\n";
2225de7d4f0eSAndy Whitcroft
2226de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2227de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2228de7d4f0eSAndy Whitcroft		}
2229de7d4f0eSAndy Whitcroft	}
2230de7d4f0eSAndy Whitcroft
22316c72ffaaSAndy Whitcroft	$prefix = '';
22326c72ffaaSAndy Whitcroft
2233773647a0SAndy Whitcroft	$realcnt = 0;
2234773647a0SAndy Whitcroft	$linenr = 0;
2235194f66fcSJoe Perches	$fixlinenr = -1;
22360a920b5bSAndy Whitcroft	foreach my $line (@lines) {
22370a920b5bSAndy Whitcroft		$linenr++;
2238194f66fcSJoe Perches		$fixlinenr++;
22391b5539b1SJoe Perches		my $sline = $line;	#copy of $line
22401b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
22410a920b5bSAndy Whitcroft
2242c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
22436c72ffaaSAndy Whitcroft
22440a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2245e518e9a5SJoe Perches		if (!$in_commit_log &&
2246e518e9a5SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
22470a920b5bSAndy Whitcroft			$is_patch = 1;
22484a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
22490a920b5bSAndy Whitcroft			$realline=$1-1;
22500a920b5bSAndy Whitcroft			if (defined $2) {
22510a920b5bSAndy Whitcroft				$realcnt=$3+1;
22520a920b5bSAndy Whitcroft			} else {
22530a920b5bSAndy Whitcroft				$realcnt=1+1;
22540a920b5bSAndy Whitcroft			}
2255c2fdda0dSAndy Whitcroft			annotate_reset();
225613214adfSAndy Whitcroft			$prev_values = 'E';
225713214adfSAndy Whitcroft
2258773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2259170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
22602b474a1aSAndy Whitcroft			%suppress_export = ();
22613e469cdcSAndy Whitcroft			$suppress_statement = 0;
22620a920b5bSAndy Whitcroft			next;
22630a920b5bSAndy Whitcroft
22644a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
22654a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
22664a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2267773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
22680a920b5bSAndy Whitcroft			$realline++;
2269d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
22700a920b5bSAndy Whitcroft
22714a0df2efSAndy Whitcroft			# Measure the line length and indent.
2272c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
22730a920b5bSAndy Whitcroft
22740a920b5bSAndy Whitcroft			# Track the previous line.
22750a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
22760a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2277c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2278c2fdda0dSAndy Whitcroft
2279773647a0SAndy Whitcroft			#warn "line<$line>\n";
22806c72ffaaSAndy Whitcroft
2281d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2282d8aaf121SAndy Whitcroft			$realcnt--;
22830a920b5bSAndy Whitcroft		}
22840a920b5bSAndy Whitcroft
2285cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2286cc77cdcaSAndy Whitcroft
22876c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
22886c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2289773647a0SAndy Whitcroft
22902ac73b4fSJoe Perches		my $found_file = 0;
2291773647a0SAndy Whitcroft		# extract the filename as it passes
22923bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
22933bf9a009SRabin Vincent			$realfile = $1;
22942b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2295270c49a0SJoe Perches			$in_commit_log = 0;
22962ac73b4fSJoe Perches			$found_file = 1;
22973bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2298773647a0SAndy Whitcroft			$realfile = $1;
22992b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2300270c49a0SJoe Perches			$in_commit_log = 0;
23011e855726SWolfram Sang
23021e855726SWolfram Sang			$p1_prefix = $1;
2303e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2304e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2305000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2306000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
23071e855726SWolfram Sang			}
2308773647a0SAndy Whitcroft
2309c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2310000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2311000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2312773647a0SAndy Whitcroft			}
23132ac73b4fSJoe Perches			$found_file = 1;
23142ac73b4fSJoe Perches		}
23152ac73b4fSJoe Perches
231634d8815fSJoe Perches#make up the handle for any error we report on this line
231734d8815fSJoe Perches		if ($showfile) {
231834d8815fSJoe Perches			$prefix = "$realfile:$realline: "
231934d8815fSJoe Perches		} elsif ($emacs) {
23207d3a9f67SJoe Perches			if ($file) {
23217d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
23227d3a9f67SJoe Perches			} else {
232334d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
232434d8815fSJoe Perches			}
23257d3a9f67SJoe Perches		}
232634d8815fSJoe Perches
23272ac73b4fSJoe Perches		if ($found_file) {
232885b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
232985b0ee18SJoe Perches				WARN("OBSOLETE",
233085b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
233185b0ee18SJoe Perches			}
23327bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
23332ac73b4fSJoe Perches				$check = 1;
23342ac73b4fSJoe Perches			} else {
23352ac73b4fSJoe Perches				$check = $check_orig;
23362ac73b4fSJoe Perches			}
2337773647a0SAndy Whitcroft			next;
2338773647a0SAndy Whitcroft		}
2339773647a0SAndy Whitcroft
2340389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
23410a920b5bSAndy Whitcroft
2342c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2343c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2344c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
23450a920b5bSAndy Whitcroft
23466c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
23476c72ffaaSAndy Whitcroft
2348e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2349e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2350e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2351e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2352e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2353e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2354e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2355e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2356e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2357e518e9a5SJoe Perches		}
2358e518e9a5SJoe Perches
23593bf9a009SRabin Vincent# Check for incorrect file permissions
23603bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
23613bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
236204db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
236304db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2364000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2365000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
23663bf9a009SRabin Vincent			}
23673bf9a009SRabin Vincent		}
23683bf9a009SRabin Vincent
236920112475SJoe Perches# Check the patch for a signoff:
2370d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
23714a0df2efSAndy Whitcroft			$signoff++;
237215662b3eSJoe Perches			$in_commit_log = 0;
23730a920b5bSAndy Whitcroft		}
237420112475SJoe Perches
2375e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2376e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2377e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2378e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2379e0d975b1SJoe Perches		}
2380e0d975b1SJoe Perches
238120112475SJoe Perches# Check signature styles
2382270c49a0SJoe Perches		if (!$in_header_lines &&
2383ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
238420112475SJoe Perches			my $space_before = $1;
238520112475SJoe Perches			my $sign_off = $2;
238620112475SJoe Perches			my $space_after = $3;
238720112475SJoe Perches			my $email = $4;
238820112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
238920112475SJoe Perches
2390ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2391ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2392ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2393ce0338dfSJoe Perches			}
239420112475SJoe Perches			if (defined $space_before && $space_before ne "") {
23953705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
23963705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
23973705ce5bSJoe Perches				    $fix) {
2398194f66fcSJoe Perches					$fixed[$fixlinenr] =
23993705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24003705ce5bSJoe Perches				}
240120112475SJoe Perches			}
240220112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
24033705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24043705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
24053705ce5bSJoe Perches				    $fix) {
2406194f66fcSJoe Perches					$fixed[$fixlinenr] =
24073705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24083705ce5bSJoe Perches				}
24093705ce5bSJoe Perches
241020112475SJoe Perches			}
241120112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
24123705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
24133705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
24143705ce5bSJoe Perches				    $fix) {
2415194f66fcSJoe Perches					$fixed[$fixlinenr] =
24163705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
24173705ce5bSJoe Perches				}
241820112475SJoe Perches			}
241920112475SJoe Perches
242020112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
242120112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
242220112475SJoe Perches			if ($suggested_email eq "") {
2423000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2424000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
242520112475SJoe Perches			} else {
242620112475SJoe Perches				my $dequoted = $suggested_email;
242720112475SJoe Perches				$dequoted =~ s/^"//;
242820112475SJoe Perches				$dequoted =~ s/" </ </;
242920112475SJoe Perches				# Don't force email to have quotes
243020112475SJoe Perches				# Allow just an angle bracketed address
243120112475SJoe Perches				if ("$dequoted$comment" ne $email &&
243220112475SJoe Perches				    "<$email_address>$comment" ne $email &&
243320112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2434000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2435000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
243620112475SJoe Perches				}
24370a920b5bSAndy Whitcroft			}
24387e51f197SJoe Perches
24397e51f197SJoe Perches# Check for duplicate signatures
24407e51f197SJoe Perches			my $sig_nospace = $line;
24417e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
24427e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
24437e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
24447e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
24457e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
24467e51f197SJoe Perches			} else {
24477e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
24487e51f197SJoe Perches			}
24490a920b5bSAndy Whitcroft		}
24500a920b5bSAndy Whitcroft
2451a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2452a2fe16b9SJoe Perches		if ($in_header_lines &&
2453a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2454a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2455a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2456a2fe16b9SJoe Perches		}
2457a2fe16b9SJoe Perches
24589b3189ebSJoe Perches# Check for old stable address
24599b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
24609b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
24619b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
24629b3189ebSJoe Perches		}
24639b3189ebSJoe Perches
24647ebd05efSChristopher Covington# Check for unwanted Gerrit info
24657ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
24667ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
24677ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
24687ebd05efSChristopher Covington		}
24697ebd05efSChristopher Covington
2470369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2471369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2472369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2473369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2474369c8dd3SJoe Perches					# timestamp
2475369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2476369c8dd3SJoe Perches					# stack dump address
2477369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2478369c8dd3SJoe Perches		}
2479369c8dd3SJoe Perches
24802a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
24812a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2482bf4daf12SJoe Perches		    length($line) > 75 &&
2483bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2484bf4daf12SJoe Perches					# file delta changes
2485bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2486bf4daf12SJoe Perches					# filename then :
2487bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2488bf4daf12SJoe Perches					# A Fixes: or Link: line
2489bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
24902a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
24912a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
24922a076f40SJoe Perches			$commit_log_long_line = 1;
24932a076f40SJoe Perches		}
24942a076f40SJoe Perches
2495bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2496bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2497bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2498bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2499bf4daf12SJoe Perches		}
2500bf4daf12SJoe Perches
25010d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2502369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2503aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2504fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2505aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2506369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2507bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2508fe043ea1SJoe Perches			my $init_char = "c";
2509fe043ea1SJoe Perches			my $orig_commit = "";
25100d7835fcSJoe Perches			my $short = 1;
25110d7835fcSJoe Perches			my $long = 0;
25120d7835fcSJoe Perches			my $case = 1;
25130d7835fcSJoe Perches			my $space = 1;
25140d7835fcSJoe Perches			my $hasdesc = 0;
251519c146a6SJoe Perches			my $hasparens = 0;
25160d7835fcSJoe Perches			my $id = '0123456789ab';
25170d7835fcSJoe Perches			my $orig_desc = "commit description";
25180d7835fcSJoe Perches			my $description = "";
25190d7835fcSJoe Perches
2520fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2521fe043ea1SJoe Perches				$init_char = $1;
2522fe043ea1SJoe Perches				$orig_commit = lc($2);
2523fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2524fe043ea1SJoe Perches				$orig_commit = lc($1);
2525fe043ea1SJoe Perches			}
2526fe043ea1SJoe Perches
25270d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
25280d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
25290d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
25300d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
25310d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
25320d7835fcSJoe Perches				$orig_desc = $1;
253319c146a6SJoe Perches				$hasparens = 1;
25340d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
25350d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
25360d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
25370d7835fcSJoe Perches				$orig_desc = $1;
253819c146a6SJoe Perches				$hasparens = 1;
2539b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2540b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2541b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2542b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2543b671fde0SJoe Perches				$orig_desc = $1;
2544b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2545b671fde0SJoe Perches				$orig_desc .= " " . $1;
254619c146a6SJoe Perches				$hasparens = 1;
25470d7835fcSJoe Perches			}
25480d7835fcSJoe Perches
25490d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
25500d7835fcSJoe Perches							      $id, $orig_desc);
25510d7835fcSJoe Perches
255219c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2553d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
25540d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
25550d7835fcSJoe Perches			}
2556d311cd44SJoe Perches		}
2557d311cd44SJoe Perches
255813f1937eSJoe Perches# Check for added, moved or deleted files
255913f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
256013f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
256113f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
256213f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
256313f1937eSJoe Perches		      (defined($1) || defined($2))))) {
256413f1937eSJoe Perches			$reported_maintainer_file = 1;
256513f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
256613f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
256713f1937eSJoe Perches		}
256813f1937eSJoe Perches
256900df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
25708905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2571000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2572000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
25736c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2574de7d4f0eSAndy Whitcroft		}
2575de7d4f0eSAndy Whitcroft
25766ecd9674SAndy Whitcroft# Check for absolute kernel paths.
25776ecd9674SAndy Whitcroft		if ($tree) {
25786ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
25796ecd9674SAndy Whitcroft				my $file = $1;
25806ecd9674SAndy Whitcroft
25816ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
25826ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
25836ecd9674SAndy Whitcroft					#
25846ecd9674SAndy Whitcroft				} else {
25856ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
25866ecd9674SAndy Whitcroft				}
25876ecd9674SAndy Whitcroft			}
25886ecd9674SAndy Whitcroft		}
25896ecd9674SAndy Whitcroft
2590de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2591de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2592171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2593171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2594171ae1a4SAndy Whitcroft
2595171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2596171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2597171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2598171ae1a4SAndy Whitcroft
259934d99219SJoe Perches			CHK("INVALID_UTF8",
2600000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
260100df344fSAndy Whitcroft		}
26020a920b5bSAndy Whitcroft
260315662b3eSJoe Perches# Check if it's the start of a commit log
260415662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
260515662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
260629ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
260729ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
260815662b3eSJoe Perches			$in_header_lines = 0;
260915662b3eSJoe Perches			$in_commit_log = 1;
2610ed43c4e5SAllen Hubbe			$has_commit_log = 1;
261115662b3eSJoe Perches		}
261215662b3eSJoe Perches
2613fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2614fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2615fa64205dSPasi Savanainen		if ($in_header_lines &&
2616fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2617fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2618fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2619fa64205dSPasi Savanainen		}
2620fa64205dSPasi Savanainen
2621fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
262215662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2623fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
262415662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
262515662b3eSJoe Perches		}
262615662b3eSJoe Perches
262766b47b4aSKees Cook# Check for various typo / spelling mistakes
262866d7a382SJoe Perches		if (defined($misspellings) &&
262966d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2630ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
263166b47b4aSKees Cook				my $typo = $1;
263266b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
263366b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
263466b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
263566b47b4aSKees Cook				my $msg_type = \&WARN;
263666b47b4aSKees Cook				$msg_type = \&CHK if ($file);
263766b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
263866b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
263966b47b4aSKees Cook				    $fix) {
264066b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
264166b47b4aSKees Cook				}
264266b47b4aSKees Cook			}
264366b47b4aSKees Cook		}
264466b47b4aSKees Cook
264530670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
264630670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
264700df344fSAndy Whitcroft
26480a920b5bSAndy Whitcroft#trailing whitespace
26499c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2650c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2651d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2652d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2653d5e616fcSJoe Perches			    $fix) {
2654194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2655d5e616fcSJoe Perches			}
2656c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2657c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26583705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
26593705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
26603705ce5bSJoe Perches			    $fix) {
2661194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
26623705ce5bSJoe Perches			}
26633705ce5bSJoe Perches
2664d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
26650a920b5bSAndy Whitcroft		}
26665368df20SAndy Whitcroft
26674783f894SJosh Triplett# Check for FSF mailing addresses.
2668109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
26693e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
26703e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
26714783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26724783f894SJosh Triplett			my $msg_type = \&ERROR;
26734783f894SJosh Triplett			$msg_type = \&CHK if ($file);
26744783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
26754783f894SJosh 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)
26764783f894SJosh Triplett		}
26774783f894SJosh Triplett
26783354957aSAndi Kleen# check for Kconfig help text having a real description
26799fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
26809fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
26813354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
26828d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
26833354957aSAndi Kleen			my $length = 0;
26849fe287d7SAndy Whitcroft			my $cnt = $realcnt;
26859fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
26869fe287d7SAndy Whitcroft			my $f;
2687a1385803SAndy Whitcroft			my $is_start = 0;
26889fe287d7SAndy Whitcroft			my $is_end = 0;
2689a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
26909fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
26919fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
26929fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
26939fe287d7SAndy Whitcroft
26949fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
26958d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2696a1385803SAndy Whitcroft
26978d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2698a1385803SAndy Whitcroft					$is_start = 1;
26998d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2700a1385803SAndy Whitcroft					$length = -1;
2701a1385803SAndy Whitcroft				}
2702a1385803SAndy Whitcroft
27039fe287d7SAndy Whitcroft				$f =~ s/^.//;
27043354957aSAndi Kleen				$f =~ s/#.*//;
27053354957aSAndi Kleen				$f =~ s/^\s+//;
27063354957aSAndi Kleen				next if ($f =~ /^$/);
27079fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
27089fe287d7SAndy Whitcroft					$is_end = 1;
27099fe287d7SAndy Whitcroft					last;
27109fe287d7SAndy Whitcroft				}
27113354957aSAndi Kleen				$length++;
27123354957aSAndi Kleen			}
271356193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2714000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
271556193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
271656193274SVadim Bendebury			}
2717a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
27183354957aSAndi Kleen		}
27193354957aSAndi Kleen
27201ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
27211ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
27221ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
27231ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
27241ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
27251ba8dfd1SKees Cook		}
27261ba8dfd1SKees Cook
2727327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2728327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2729327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2730327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2731327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2732327953e9SChristoph Jaeger		}
2733327953e9SChristoph Jaeger
2734c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2735c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2736c68e5878SArnaud Lacombe			my $flag = $1;
2737c68e5878SArnaud Lacombe			my $replacement = {
2738c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2739c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2740c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2741c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2742c68e5878SArnaud Lacombe			};
2743c68e5878SArnaud Lacombe
2744c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2745c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2746c68e5878SArnaud Lacombe		}
2747c68e5878SArnaud Lacombe
2748bff5da43SRob Herring# check for DT compatible documentation
27497dd05b38SFlorian Vaussard		if (defined $root &&
27507dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
27517dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
27527dd05b38SFlorian Vaussard
2753bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2754bff5da43SRob Herring
2755cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2756cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2757cc93319bSFlorian Vaussard
2758bff5da43SRob Herring			foreach my $compat (@compats) {
2759bff5da43SRob Herring				my $compat2 = $compat;
2760185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2761185d566bSRob Herring				my $compat3 = $compat;
2762185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2763185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2764bff5da43SRob Herring				if ( $? >> 8 ) {
2765bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2766bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2767bff5da43SRob Herring				}
2768bff5da43SRob Herring
27694fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
27704fbf32a6SFlorian Vaussard				my $vendor = $1;
2771cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2772bff5da43SRob Herring				if ( $? >> 8 ) {
2773bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2774cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2775bff5da43SRob Herring				}
2776bff5da43SRob Herring			}
2777bff5da43SRob Herring		}
2778bff5da43SRob Herring
27795368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2780de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
27815368df20SAndy Whitcroft
278247e0c88bSJoe Perches# line length limit (with some exclusions)
278347e0c88bSJoe Perches#
278447e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
278547e0c88bSJoe Perches#	logging functions like pr_info that end in a string
278647e0c88bSJoe Perches#	lines with a single string
278747e0c88bSJoe Perches#	#defines that are a single string
278847e0c88bSJoe Perches#
278947e0c88bSJoe Perches# There are 3 different line length message types:
279047e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_linelength
279147e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
279247e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
279347e0c88bSJoe Perches#
279447e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
279547e0c88bSJoe Perches#
279647e0c88bSJoe Perches
2797b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
279847e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
279947e0c88bSJoe Perches
280047e0c88bSJoe Perches			# Check the allowed long line types first
280147e0c88bSJoe Perches
280247e0c88bSJoe Perches			# logging functions that end in a string that starts
280347e0c88bSJoe Perches			# before $max_line_length
280447e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
280547e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
280647e0c88bSJoe Perches				$msg_type = "";
280747e0c88bSJoe Perches
280847e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
280947e0c88bSJoe Perches			# #defines with only strings
281047e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
281147e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
281247e0c88bSJoe Perches				$msg_type = "";
281347e0c88bSJoe Perches
2814d560a5f8SJoe Perches			# EFI_GUID is another special case
2815d560a5f8SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2816d560a5f8SJoe Perches				$msg_type = "";
2817d560a5f8SJoe Perches
281847e0c88bSJoe Perches			# Otherwise set the alternate message types
281947e0c88bSJoe Perches
282047e0c88bSJoe Perches			# a comment starts before $max_line_length
282147e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
282247e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
282347e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
282447e0c88bSJoe Perches
282547e0c88bSJoe Perches			# a quoted string starts before $max_line_length
282647e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
282747e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
282847e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
282947e0c88bSJoe Perches			}
283047e0c88bSJoe Perches
283147e0c88bSJoe Perches			if ($msg_type ne "" &&
283247e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
283347e0c88bSJoe Perches				WARN($msg_type,
28346cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
28350a920b5bSAndy Whitcroft			}
283647e0c88bSJoe Perches		}
28370a920b5bSAndy Whitcroft
28388905a67cSAndy Whitcroft# check for adding lines without a newline.
28398905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2840000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2841000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
28428905a67cSAndy Whitcroft		}
28438905a67cSAndy Whitcroft
284442e41c54SMike Frysinger# Blackfin: use hi/lo macros
284542e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
284642e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
284742e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2848000d1cc1SJoe Perches				ERROR("LO_MACRO",
2849000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
285042e41c54SMike Frysinger			}
285142e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
285242e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2853000d1cc1SJoe Perches				ERROR("HI_MACRO",
2854000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
285542e41c54SMike Frysinger			}
285642e41c54SMike Frysinger		}
285742e41c54SMike Frysinger
2858b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2859de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
28600a920b5bSAndy Whitcroft
28610a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
28620a920b5bSAndy Whitcroft# more than 8 must use tabs.
2863c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2864c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2865c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2866d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
28673705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
28683705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
28693705ce5bSJoe Perches			    $fix) {
2870194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
28713705ce5bSJoe Perches			}
28720a920b5bSAndy Whitcroft		}
28730a920b5bSAndy Whitcroft
287408e44365SAlberto Panizzo# check for space before tabs.
287508e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
287608e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28773705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
28783705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
28793705ce5bSJoe Perches			    $fix) {
2880194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2881d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2882194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2883c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
28843705ce5bSJoe Perches			}
288508e44365SAlberto Panizzo		}
288608e44365SAlberto Panizzo
2887d1fe9c09SJoe Perches# check for && or || at the start of a line
2888d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2889d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2890d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2891d1fe9c09SJoe Perches		}
2892d1fe9c09SJoe Perches
2893a91e8994SJoe Perches# check indentation starts on a tab stop
2894a91e8994SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2895a91e8994SJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2896a91e8994SJoe Perches			my $indent = length($1);
2897a91e8994SJoe Perches			if ($indent % 8) {
2898a91e8994SJoe Perches				if (WARN("TABSTOP",
2899a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
2900a91e8994SJoe Perches				    $fix) {
2901a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2902a91e8994SJoe Perches				}
2903a91e8994SJoe Perches			}
2904a91e8994SJoe Perches		}
2905a91e8994SJoe Perches
2906d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2907d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
290891cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2909d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2910d1fe9c09SJoe Perches			my $oldindent = $1;
2911d1fe9c09SJoe Perches			my $rest = $2;
2912d1fe9c09SJoe Perches
2913d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2914d1fe9c09SJoe Perches			if ($pos >= 0) {
2915b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2916b34a26f3SJoe Perches				my $newindent = $2;
2917d1fe9c09SJoe Perches
2918d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2919d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2920d1fe9c09SJoe Perches					" "  x ($pos % 8);
2921d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2922d1fe9c09SJoe Perches
2923d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2924d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
29253705ce5bSJoe Perches
29263705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
29273705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
29283705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2929194f66fcSJoe Perches						$fixed[$fixlinenr] =~
29303705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
29313705ce5bSJoe Perches					}
2932d1fe9c09SJoe Perches				}
2933d1fe9c09SJoe Perches			}
2934d1fe9c09SJoe Perches		}
2935d1fe9c09SJoe Perches
29366ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
29376ab3a970SJoe Perches# avoid checking a few false positives:
29386ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
29396ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
29406ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
29416ab3a970SJoe Perches#   multiline macros that define functions
29426ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
29436ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
29446ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
29453705ce5bSJoe Perches			if (CHK("SPACING",
2946f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
29473705ce5bSJoe Perches			    $fix) {
2948194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2949f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
29503705ce5bSJoe Perches			}
2951aad4f614SJoe Perches		}
2952aad4f614SJoe Perches
295386406b1cSJoe Perches# Block comment styles
295486406b1cSJoe Perches# Networking with an initial /*
295505880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2956fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
295785ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
295885ad978cSJoe Perches		    $realline > 2) {
295905880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
296005880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
296105880600SJoe Perches		}
296205880600SJoe Perches
296386406b1cSJoe Perches# Block comments use * on subsequent lines
296486406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
296586406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
2966a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
296761135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2968a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
296986406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
297086406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
2971a605e32eSJoe Perches		}
2972a605e32eSJoe Perches
297386406b1cSJoe Perches# Block comments use */ on trailing lines
297486406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2975c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2976c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2977c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
297886406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
297986406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
298005880600SJoe Perches		}
298105880600SJoe Perches
2982*08eb9b80SJoe Perches# Block comment * alignment
2983*08eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
2984*08eb9b80SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
2985*08eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
2986*08eb9b80SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/) &&		#starting *
2987*08eb9b80SJoe Perches		    $rawline =~ /^\+[ \t]*\*/) {		#rawline *
2988*08eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
2989*08eb9b80SJoe Perches			my $oldindent = expand_tabs($1);
2990*08eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
2991*08eb9b80SJoe Perches			my $newindent = $1;
2992*08eb9b80SJoe Perches			my $test_comment = '^\\+' . "$;" x (length($newindent) + 1);
2993*08eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
2994*08eb9b80SJoe Perches			if ($line =~ /$test_comment/ &&
2995*08eb9b80SJoe Perches			    length($oldindent) ne length($newindent)) {
2996*08eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
2997*08eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
2998*08eb9b80SJoe Perches			}
2999*08eb9b80SJoe Perches		}
3000*08eb9b80SJoe Perches
30017f619191SJoe Perches# check for missing blank lines after struct/union declarations
30027f619191SJoe Perches# with exceptions for various attributes and macros
30037f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
30047f619191SJoe Perches		    $line =~ /^\+/ &&
30057f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
30067f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
30077f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
30087f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
30097f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
30107f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
30117f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
30127f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
3013d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3014d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3015d752fcc8SJoe Perches			    $fix) {
3016f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3017d752fcc8SJoe Perches			}
30187f619191SJoe Perches		}
30197f619191SJoe Perches
3020365dd4eaSJoe Perches# check for multiple consecutive blank lines
3021365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
3022365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
3023365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
3024d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
3025d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
3026d752fcc8SJoe Perches			    $fix) {
3027f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3028d752fcc8SJoe Perches			}
3029d752fcc8SJoe Perches
3030365dd4eaSJoe Perches			$last_blank_line = $linenr;
3031365dd4eaSJoe Perches		}
3032365dd4eaSJoe Perches
30333b617e3bSJoe Perches# check for missing blank lines after declarations
30343f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
30353f7bac03SJoe Perches			# actual declarations
30363f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
30375a4e1fd3SJoe Perches			# function pointer declarations
30385a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
30393f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
30403f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
30413f7bac03SJoe Perches			# known declaration macros
30423f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
30433f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
30443f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
30453f7bac03SJoe Perches			# other possible extensions of declaration lines
30463f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
30473f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
30483f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
30493f7bac03SJoe Perches			# looks like a declaration
30503f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
30515a4e1fd3SJoe Perches			# function pointer declarations
30525a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
30533f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
30543f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
30553f7bac03SJoe Perches			# known declaration macros
30563f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
30573f7bac03SJoe Perches			# start of struct or union or enum
30583b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
30593f7bac03SJoe Perches			# start or end of block or continuation of declaration
30603f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
30613f7bac03SJoe Perches			# bitfield continuation
30623f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
30633f7bac03SJoe Perches			# other possible extensions of declaration lines
30643f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
30653f7bac03SJoe Perches			# indentation of previous and current line are the same
30663f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3067d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3068d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3069d752fcc8SJoe Perches			    $fix) {
3070f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3071d752fcc8SJoe Perches			}
30723b617e3bSJoe Perches		}
30733b617e3bSJoe Perches
30745f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
30756b4c5bebSAndy Whitcroft# Exceptions:
30766b4c5bebSAndy Whitcroft#  1) within comments
30776b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
30786b4c5bebSAndy Whitcroft#  3) hanging labels
30793705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
30805f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30813705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
30823705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
30833705ce5bSJoe Perches			    $fix) {
3084194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
30853705ce5bSJoe Perches			}
30865f7ddae6SRaffaele Recalcati		}
30875f7ddae6SRaffaele Recalcati
3088b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3089b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3090b9ea10d6SAndy Whitcroft
3091032a4c0fSJoe Perches# check indentation of any line with a bare else
3092840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3093032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3094032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3095032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3096840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3097840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3098840080a0SJoe Perches			     defined $lines[$linenr] &&
3099840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3100032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3101032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3102032a4c0fSJoe Perches			}
3103032a4c0fSJoe Perches		}
3104032a4c0fSJoe Perches
3105c00df19aSJoe Perches# check indentation of a line with a break;
3106c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3107c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3108c00df19aSJoe Perches			my $tabs = $1;
3109c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3110c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3111c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3112c00df19aSJoe Perches			}
3113c00df19aSJoe Perches		}
3114c00df19aSJoe Perches
31151ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
31161ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
31171ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
31181ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
31191ba8dfd1SKees Cook		}
31201ba8dfd1SKees Cook
3121c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3122cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3123000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3124000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3125c2fdda0dSAndy Whitcroft		}
312622f2a2efSAndy Whitcroft
312742e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
312842e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
312942e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3130000d1cc1SJoe Perches			ERROR("CSYNC",
3131000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
313242e41c54SMike Frysinger		}
313342e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
313442e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3135000d1cc1SJoe Perches			ERROR("SSYNC",
3136000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
313742e41c54SMike Frysinger		}
313842e41c54SMike Frysinger
313956e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
314056e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
314156e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
314256e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
314356e77d70SJoe Perches		}
314456e77d70SJoe Perches
31459c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
31462b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
31472b474a1aSAndy Whitcroft		    $realline_next);
31483e469cdcSAndy Whitcroft#print "LINE<$line>\n";
31493e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
31501b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3151170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3152f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3153171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3154171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3155171ae1a4SAndy Whitcroft
31563e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
31573e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
31583e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
31593e469cdcSAndy Whitcroft			# until we hit end of it.
31603e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
31613e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
31623e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
31633e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
31643e469cdcSAndy Whitcroft			}
3165f74bd194SAndy Whitcroft
31662b474a1aSAndy Whitcroft			# Find the real next line.
31672b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
31682b474a1aSAndy Whitcroft			if (defined $realline_next &&
31692b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
31702b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
31712b474a1aSAndy Whitcroft				$realline_next++;
31722b474a1aSAndy Whitcroft			}
31732b474a1aSAndy Whitcroft
3174171ae1a4SAndy Whitcroft			my $s = $stat;
3175171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3176cf655043SAndy Whitcroft
3177c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3178171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3179c2fdda0dSAndy Whitcroft
3180c2fdda0dSAndy Whitcroft			# Ignore functions being called
3181171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3182c2fdda0dSAndy Whitcroft
3183463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3184463f2864SAndy Whitcroft
3185c45dcabdSAndy Whitcroft			# declarations always start with types
3186d2506586SAndy 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) {
3187c45dcabdSAndy Whitcroft				my $type = $1;
3188c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3189c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3190c45dcabdSAndy Whitcroft
31916c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3192a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3193c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3194c2fdda0dSAndy Whitcroft			}
31958905a67cSAndy Whitcroft
31966c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
319765863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3198c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
31999c0ca6f9SAndy Whitcroft			}
32008905a67cSAndy Whitcroft
32018905a67cSAndy Whitcroft			# Check for any sort of function declaration.
32028905a67cSAndy Whitcroft			# int foo(something bar, other baz);
32038905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3204171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
32058905a67cSAndy Whitcroft				my ($name_len) = length($1);
32068905a67cSAndy Whitcroft
3207cf655043SAndy Whitcroft				my $ctx = $s;
3208773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
32098905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3210cf655043SAndy Whitcroft
32118905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3212c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
32138905a67cSAndy Whitcroft
3214c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
32158905a67cSAndy Whitcroft					}
32168905a67cSAndy Whitcroft				}
32178905a67cSAndy Whitcroft			}
32188905a67cSAndy Whitcroft
32199c0ca6f9SAndy Whitcroft		}
32209c0ca6f9SAndy Whitcroft
322100df344fSAndy Whitcroft#
322200df344fSAndy Whitcroft# Checks which may be anchored in the context.
322300df344fSAndy Whitcroft#
322400df344fSAndy Whitcroft
322500df344fSAndy Whitcroft# Check for switch () and associated case and default
322600df344fSAndy Whitcroft# statements should be at the same indent.
322700df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
322800df344fSAndy Whitcroft			my $err = '';
322900df344fSAndy Whitcroft			my $sep = '';
323000df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
323100df344fSAndy Whitcroft			shift(@ctx);
323200df344fSAndy Whitcroft			for my $ctx (@ctx) {
323300df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
323400df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
323500df344fSAndy Whitcroft							$indent != $cindent) {
323600df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
323700df344fSAndy Whitcroft					$sep = '';
323800df344fSAndy Whitcroft				} else {
323900df344fSAndy Whitcroft					$sep = "[...]\n";
324000df344fSAndy Whitcroft				}
324100df344fSAndy Whitcroft			}
324200df344fSAndy Whitcroft			if ($err ne '') {
3243000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3244000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3245de7d4f0eSAndy Whitcroft			}
3246de7d4f0eSAndy Whitcroft		}
3247de7d4f0eSAndy Whitcroft
3248de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3249de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
32500fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3251773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3252773647a0SAndy Whitcroft
32539c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
32548eef05ddSJoe Perches
32558eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
32568eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
32578eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
32588eef05ddSJoe Perches			}
32598eef05ddSJoe Perches
3260de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3261de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3262de7d4f0eSAndy Whitcroft
3263548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3264548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3265de7d4f0eSAndy Whitcroft
3266548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3267548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3268548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3269548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3270548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3271773647a0SAndy Whitcroft				$ctx_ln++;
3272773647a0SAndy Whitcroft			}
3273548596d5SAndy Whitcroft
327453210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
327553210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3276773647a0SAndy Whitcroft
3277773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3278000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3279000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
328001464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
328100df344fSAndy Whitcroft			}
3282773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3283773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3284773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3285773647a0SAndy Whitcroft			{
32869c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
32879c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3288000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3289000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
329001464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
32919c0ca6f9SAndy Whitcroft				}
32929c0ca6f9SAndy Whitcroft			}
329300df344fSAndy Whitcroft		}
329400df344fSAndy Whitcroft
32954d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
32960fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
32973e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
32983e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
32993e469cdcSAndy Whitcroft					if (!defined $stat);
33004d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
33014d001e4dSAndy Whitcroft
33024d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
33034d001e4dSAndy Whitcroft
33049f5af480SJoe Perches			# remove inline comments
33059f5af480SJoe Perches			$s =~ s/$;/ /g;
33069f5af480SJoe Perches			$c =~ s/$;/ /g;
33074d001e4dSAndy Whitcroft
33084d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
33096f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
33106f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
33114d001e4dSAndy Whitcroft
33129f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
33139f5af480SJoe Perches			# none on the first line, and are going to readd them
33149f5af480SJoe Perches			# where necessary.
33159f5af480SJoe Perches			$s =~ s/\n./\n/gs;
33169f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
33179f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
33189f5af480SJoe Perches			}
33199f5af480SJoe Perches
33204d001e4dSAndy Whitcroft			# We want to check the first line inside the block
33214d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
33224d001e4dSAndy Whitcroft			#  1) any blank line termination
33234d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
33244d001e4dSAndy Whitcroft			#  3) any do (...) {
33254d001e4dSAndy Whitcroft			my $continuation = 0;
33264d001e4dSAndy Whitcroft			my $check = 0;
33274d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
33284d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
33294d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
33304d001e4dSAndy Whitcroft				$continuation = 1;
33314d001e4dSAndy Whitcroft			}
33329bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
33334d001e4dSAndy Whitcroft				$check = 1;
33344d001e4dSAndy Whitcroft				$cond_lines++;
33354d001e4dSAndy Whitcroft			}
33364d001e4dSAndy Whitcroft
33374d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
33384d001e4dSAndy Whitcroft			# preprocessor statement.
33394d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
33404d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
33414d001e4dSAndy Whitcroft				$check = 0;
33424d001e4dSAndy Whitcroft			}
33434d001e4dSAndy Whitcroft
33449bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3345740504c6SAndy Whitcroft			$continuation = 0;
33469bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
33479bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
33484d001e4dSAndy Whitcroft
3349f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3350f16fa28fSAndy Whitcroft				# is not linear.
3351f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3352f16fa28fSAndy Whitcroft					$check = 0;
3353f16fa28fSAndy Whitcroft				}
3354f16fa28fSAndy Whitcroft
33559bd49efeSAndy Whitcroft				# Ignore:
33569bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
33579bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
33589bd49efeSAndy Whitcroft				#  3) labels.
3359740504c6SAndy Whitcroft				if ($continuation ||
3360740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
33619bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
33629bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3363740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
336430dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
33659bd49efeSAndy Whitcroft						$cond_lines++;
33669bd49efeSAndy Whitcroft					}
33674d001e4dSAndy Whitcroft				}
336830dad6ebSAndy Whitcroft			}
33694d001e4dSAndy Whitcroft
33704d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
33714d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
33724d001e4dSAndy Whitcroft
33734d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
33744d001e4dSAndy Whitcroft			# this is not this patch's fault.
33754d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
33764d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
33774d001e4dSAndy Whitcroft				$check = 0;
33784d001e4dSAndy Whitcroft			}
33794d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
33804d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
33814d001e4dSAndy Whitcroft			}
33824d001e4dSAndy Whitcroft
33839bd49efeSAndy 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";
33844d001e4dSAndy Whitcroft
33859f5af480SJoe Perches			if ($check && $s ne '' &&
33869f5af480SJoe Perches			    (($sindent % 8) != 0 ||
33879f5af480SJoe Perches			     ($sindent < $indent) ||
33889f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3389000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3390000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
33914d001e4dSAndy Whitcroft			}
33924d001e4dSAndy Whitcroft		}
33934d001e4dSAndy Whitcroft
33946c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
33956c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
33961f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
33971f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
33986c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3399c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3400c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3401cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3402cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
34031f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3404c2fdda0dSAndy Whitcroft		}
34056c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
34066c72ffaaSAndy Whitcroft
340700df344fSAndy Whitcroft#ignore lines not being added
34083705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
340900df344fSAndy Whitcroft
3410a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3411c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3412a1ce18e4SJoe Perches			my $type = $1;
3413a1ce18e4SJoe Perches			my $var = $2;
3414207a8e84SJoe Perches			$var = "" if (!defined $var);
3415207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3416a1ce18e4SJoe Perches				my $sign = $1;
3417a1ce18e4SJoe Perches				my $pointer = $2;
3418a1ce18e4SJoe Perches
3419a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3420a1ce18e4SJoe Perches
3421a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3422a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3423a1ce18e4SJoe Perches				    $fix) {
3424a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3425207a8e84SJoe Perches					my $comp_pointer = $pointer;
3426207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3427207a8e84SJoe Perches					$decl .= $comp_pointer;
3428207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3429207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3430a1ce18e4SJoe Perches				}
3431a1ce18e4SJoe Perches			}
3432a1ce18e4SJoe Perches		}
3433a1ce18e4SJoe Perches
3434653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
34357429c690SAndy Whitcroft		if ($dbg_type) {
34367429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3437000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3438000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
34397429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3440000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3441000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
34427429c690SAndy Whitcroft			}
3443653d4876SAndy Whitcroft			next;
3444653d4876SAndy Whitcroft		}
3445a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3446a1ef277eSAndy Whitcroft		if ($dbg_attr) {
34479360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3448000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3449000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
34509360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3451000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3452000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3453a1ef277eSAndy Whitcroft			}
3454a1ef277eSAndy Whitcroft			next;
3455a1ef277eSAndy Whitcroft		}
3456653d4876SAndy Whitcroft
3457f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
345899423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
345999423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3460d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3461d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3462f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3463f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3464f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3465d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3466d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3467f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3468d752fcc8SJoe Perches				$fixedline = $line;
3469d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3470f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3471d752fcc8SJoe Perches			}
3472f0a594c1SAndy Whitcroft		}
3473f0a594c1SAndy Whitcroft
347400df344fSAndy Whitcroft#
347500df344fSAndy Whitcroft# Checks which are anchored on the added line.
347600df344fSAndy Whitcroft#
347700df344fSAndy Whitcroft
3478653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3479c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3480653d4876SAndy Whitcroft			my $path = $1;
3481653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3482000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3483495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3484495e9d84SJoe Perches			}
3485495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3486495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3487495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3488653d4876SAndy Whitcroft			}
3489653d4876SAndy Whitcroft		}
3490653d4876SAndy Whitcroft
349100df344fSAndy Whitcroft# no C99 // comments
349200df344fSAndy Whitcroft		if ($line =~ m{//}) {
34933705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
34943705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
34953705ce5bSJoe Perches			    $fix) {
3496194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
34973705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
34983705ce5bSJoe Perches					my $comment = trim($1);
3499194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
35003705ce5bSJoe Perches				}
35013705ce5bSJoe Perches			}
350200df344fSAndy Whitcroft		}
350300df344fSAndy Whitcroft		# Remove C99 comments.
35040a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
35056c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
35060a920b5bSAndy Whitcroft
35072b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
35082b474a1aSAndy Whitcroft# the whole statement.
35092b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
35102b474a1aSAndy Whitcroft		if (defined $realline_next &&
35112b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
35122b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
35132b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
35142b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
35153cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
35163cbf62dfSAndy Whitcroft			# a prefix:
35173cbf62dfSAndy Whitcroft			#   XXX(foo);
35183cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3519653d4876SAndy Whitcroft			my $name = $1;
352087a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
35213cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
35223cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
35233cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
35243cbf62dfSAndy Whitcroft
35253cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
35262b474a1aSAndy Whitcroft				\n.}\s*$|
352748012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
352848012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
352948012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
35302b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
35312b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
353248012058SAndy Whitcroft			    )/x) {
35332b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
35342b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
35352b474a1aSAndy Whitcroft			} else {
35362b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
35370a920b5bSAndy Whitcroft			}
35380a920b5bSAndy Whitcroft		}
35392b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
35402b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
35412b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
35422b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
35432b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
35442b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
35452b474a1aSAndy Whitcroft		}
35462b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
35472b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3548000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3549000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
35502b474a1aSAndy Whitcroft		}
35510a920b5bSAndy Whitcroft
35525150bda4SJoe Eloff# check for global initialisers.
35536d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3554d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
35556d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3556d5e616fcSJoe Perches			    $fix) {
35576d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3558d5e616fcSJoe Perches			}
3559f0a594c1SAndy Whitcroft		}
35600a920b5bSAndy Whitcroft# check for static initialisers.
35616d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3562d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
35636d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3564d5e616fcSJoe Perches				      $herecurr) &&
3565d5e616fcSJoe Perches			    $fix) {
35666d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3567d5e616fcSJoe Perches			}
35680a920b5bSAndy Whitcroft		}
35690a920b5bSAndy Whitcroft
35701813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
35711813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
35721813087dSJoe Perches			my $tmp = trim($1);
35731813087dSJoe Perches			WARN("MISORDERED_TYPE",
35741813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
35751813087dSJoe Perches		}
35761813087dSJoe Perches
3577cb710ecaSJoe Perches# check for static const char * arrays.
3578cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3579000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3580000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3581cb710ecaSJoe Perches				$herecurr);
3582cb710ecaSJoe Perches               }
3583cb710ecaSJoe Perches
3584cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3585cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3586000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3587000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3588cb710ecaSJoe Perches				$herecurr);
3589cb710ecaSJoe Perches               }
3590cb710ecaSJoe Perches
3591ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3592ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3593ab7e23f3SJoe Perches			my $found = $1;
3594ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3595ab7e23f3SJoe Perches				WARN("CONST_CONST",
3596ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3597ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3598ab7e23f3SJoe Perches				WARN("CONST_CONST",
3599ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3600ab7e23f3SJoe Perches			}
3601ab7e23f3SJoe Perches		}
3602ab7e23f3SJoe Perches
36039b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
36049b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
36059b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
36069b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
36079b0fa60dSJoe Perches				$herecurr);
36089b0fa60dSJoe Perches               }
36099b0fa60dSJoe Perches
3610b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3611b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3612b598b670SJoe Perches			my $array = $1;
3613b598b670SJoe 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*\))@) {
3614b598b670SJoe Perches				my $array_div = $1;
3615b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3616b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3617b598b670SJoe Perches				    $fix) {
3618b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3619b598b670SJoe Perches				}
3620b598b670SJoe Perches			}
3621b598b670SJoe Perches		}
3622b598b670SJoe Perches
3623b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3624b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3625b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3626b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3627b36190c5SJoe Perches			    $fix) {
3628194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3629b36190c5SJoe Perches			}
3630b36190c5SJoe Perches		}
3631b36190c5SJoe Perches
3632653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3633653d4876SAndy Whitcroft# make sense.
3634653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
36358054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3636c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
36378ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3638653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3639000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3640000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
36410a920b5bSAndy Whitcroft		}
36420a920b5bSAndy Whitcroft
36430a920b5bSAndy Whitcroft# * goes on variable not on type
364465863862SAndy Whitcroft		# (char*[ const])
3645bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3646bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
36473705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3648d8aaf121SAndy Whitcroft
364965863862SAndy Whitcroft			# Should start with a space.
365065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
365165863862SAndy Whitcroft			# Should not end with a space.
365265863862SAndy Whitcroft			$to =~ s/\s+$//;
365365863862SAndy Whitcroft			# '*'s should not have spaces between.
3654f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
365565863862SAndy Whitcroft			}
3656d8aaf121SAndy Whitcroft
36573705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
365865863862SAndy Whitcroft			if ($from ne $to) {
36593705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
36603705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
36613705ce5bSJoe Perches				    $fix) {
36623705ce5bSJoe Perches					my $sub_from = $ident;
36633705ce5bSJoe Perches					my $sub_to = $ident;
36643705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3665194f66fcSJoe Perches					$fixed[$fixlinenr] =~
36663705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
36673705ce5bSJoe Perches				}
366865863862SAndy Whitcroft			}
3669bfcb2cc7SAndy Whitcroft		}
3670bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3671bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
36723705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3673d8aaf121SAndy Whitcroft
367465863862SAndy Whitcroft			# Should start with a space.
367565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
367665863862SAndy Whitcroft			# Should not end with a space.
367765863862SAndy Whitcroft			$to =~ s/\s+$//;
367865863862SAndy Whitcroft			# '*'s should not have spaces between.
3679f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
368065863862SAndy Whitcroft			}
368165863862SAndy Whitcroft			# Modifiers should have spaces.
368265863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
368365863862SAndy Whitcroft
36843705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3685667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
36863705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
36873705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
36883705ce5bSJoe Perches				    $fix) {
36893705ce5bSJoe Perches
36903705ce5bSJoe Perches					my $sub_from = $match;
36913705ce5bSJoe Perches					my $sub_to = $match;
36923705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3693194f66fcSJoe Perches					$fixed[$fixlinenr] =~
36943705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
36953705ce5bSJoe Perches				}
369665863862SAndy Whitcroft			}
36970a920b5bSAndy Whitcroft		}
36980a920b5bSAndy Whitcroft
36999d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
37009d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
37019d3e3c70SJoe Perches			my $msg_type = \&WARN;
37029d3e3c70SJoe Perches			$msg_type = \&CHK if ($file);
37039d3e3c70SJoe Perches			&{$msg_type}("AVOID_BUG",
37049d3e3c70SJoe Perches				     "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
37059d3e3c70SJoe Perches		}
37060a920b5bSAndy Whitcroft
37079d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
37088905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3709000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3710000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
37118905a67cSAndy Whitcroft		}
37128905a67cSAndy Whitcroft
371317441227SJoe Perches# check for uses of printk_ratelimit
371417441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3715000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3716000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
371717441227SJoe Perches		}
371817441227SJoe Perches
371900df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
372000df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
372100df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
372225985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
372300df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3724f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
372500df344fSAndy Whitcroft			my $ok = 0;
372600df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
372700df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
372825985edcSLucas De Marchi				# we have a preceding printk if it ends
372900df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
373000df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
373100df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
373200df344fSAndy Whitcroft						$ok = 1;
373300df344fSAndy Whitcroft					}
373400df344fSAndy Whitcroft					last;
373500df344fSAndy Whitcroft				}
373600df344fSAndy Whitcroft			}
373700df344fSAndy Whitcroft			if ($ok == 0) {
3738000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3739000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
37400a920b5bSAndy Whitcroft			}
374100df344fSAndy Whitcroft		}
37420a920b5bSAndy Whitcroft
3743243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3744243f3803SJoe Perches			my $orig = $1;
3745243f3803SJoe Perches			my $level = lc($orig);
3746243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
37478f26b837SJoe Perches			my $level2 = $level;
37488f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3749243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3750daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3751243f3803SJoe Perches		}
3752243f3803SJoe Perches
3753243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3754d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3755d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3756d5e616fcSJoe Perches			    $fix) {
3757194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3758d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3759d5e616fcSJoe Perches			}
3760243f3803SJoe Perches		}
3761243f3803SJoe Perches
3762dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3763dc139313SJoe Perches			my $orig = $1;
3764dc139313SJoe Perches			my $level = lc($orig);
3765dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3766dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3767dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3768dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3769dc139313SJoe Perches		}
3770dc139313SJoe Perches
377191c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
377291c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
377391c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
377491c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
377591c9afafSAndy Lutomirski			WARN("ENOSYS",
377691c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
377791c9afafSAndy Lutomirski		}
377891c9afafSAndy Lutomirski
3779653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3780653d4876SAndy Whitcroft# or if closed on same line
37818d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
37824e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
37838d182478SJoe Perches			if (ERROR("OPEN_BRACE",
37848d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
37858d182478SJoe Perches			    $fix) {
37868d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
37878d182478SJoe Perches				my $fixed_line = $rawline;
37888d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
37898d182478SJoe Perches				my $line1 = $1;
37908d182478SJoe Perches				my $line2 = $2;
37918d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
37928d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
37938d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
37948d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
37958d182478SJoe Perches				}
37968d182478SJoe Perches			}
37970a920b5bSAndy Whitcroft		}
3798653d4876SAndy Whitcroft
37998905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
38008905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
38018905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
38028d182478SJoe Perches			if (ERROR("OPEN_BRACE",
38038d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
38048d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
38058d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
38068d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
38078d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
38088d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
38098d182478SJoe Perches				$fixedline = $rawline;
38108d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
38118d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
38128d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
38138d182478SJoe Perches				}
38148d182478SJoe Perches			}
38158905a67cSAndy Whitcroft		}
38168905a67cSAndy Whitcroft
38170c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
38183705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
38193705ce5bSJoe Perches			if (WARN("SPACING",
38203705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
38213705ce5bSJoe Perches			    $fix) {
3822194f66fcSJoe Perches				$fixed[$fixlinenr] =~
38233705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
38243705ce5bSJoe Perches			}
38250c73b4ebSAndy Whitcroft		}
38260c73b4ebSAndy Whitcroft
382731070b5dSJoe Perches# Function pointer declarations
382831070b5dSJoe Perches# check spacing between type, funcptr, and args
382931070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
383091f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
383131070b5dSJoe Perches			my $declare = $1;
383231070b5dSJoe Perches			my $pre_pointer_space = $2;
383331070b5dSJoe Perches			my $post_pointer_space = $3;
383431070b5dSJoe Perches			my $funcname = $4;
383531070b5dSJoe Perches			my $post_funcname_space = $5;
383631070b5dSJoe Perches			my $pre_args_space = $6;
383731070b5dSJoe Perches
383891f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
383991f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
384091f72e9cSJoe Perches# don't need a space so don't warn for those.
384191f72e9cSJoe Perches			my $post_declare_space = "";
384291f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
384391f72e9cSJoe Perches				$post_declare_space = $1;
384491f72e9cSJoe Perches				$declare = rtrim($declare);
384591f72e9cSJoe Perches			}
384691f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
384731070b5dSJoe Perches				WARN("SPACING",
384831070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
384991f72e9cSJoe Perches				$post_declare_space = " ";
385031070b5dSJoe Perches			}
385131070b5dSJoe Perches
385231070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
385391f72e9cSJoe Perches# This test is not currently implemented because these declarations are
385491f72e9cSJoe Perches# equivalent to
385591f72e9cSJoe Perches#	int  foo(int bar, ...)
385691f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
385791f72e9cSJoe Perches#
385891f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
385991f72e9cSJoe Perches#				WARN("SPACING",
386091f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
386191f72e9cSJoe Perches#			}
386231070b5dSJoe Perches
386331070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
386431070b5dSJoe Perches			if (defined $pre_pointer_space &&
386531070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
386631070b5dSJoe Perches				WARN("SPACING",
386731070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
386831070b5dSJoe Perches			}
386931070b5dSJoe Perches
387031070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
387131070b5dSJoe Perches			if (defined $post_pointer_space &&
387231070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
387331070b5dSJoe Perches				WARN("SPACING",
387431070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
387531070b5dSJoe Perches			}
387631070b5dSJoe Perches
387731070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
387831070b5dSJoe Perches			if (defined $post_funcname_space &&
387931070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
388031070b5dSJoe Perches				WARN("SPACING",
388131070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
388231070b5dSJoe Perches			}
388331070b5dSJoe Perches
388431070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
388531070b5dSJoe Perches			if (defined $pre_args_space &&
388631070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
388731070b5dSJoe Perches				WARN("SPACING",
388831070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
388931070b5dSJoe Perches			}
389031070b5dSJoe Perches
389131070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3892194f66fcSJoe Perches				$fixed[$fixlinenr] =~
389391f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
389431070b5dSJoe Perches			}
389531070b5dSJoe Perches		}
389631070b5dSJoe Perches
38978d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
38988d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3899fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3900fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
39018d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
39028d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
39038d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3904fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3905daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
39063705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
39073705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
39083705ce5bSJoe Perches				    $fix) {
3909194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
39103705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
39113705ce5bSJoe Perches				}
39128d31cfceSAndy Whitcroft			}
39138d31cfceSAndy Whitcroft		}
39148d31cfceSAndy Whitcroft
3915f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
39166c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3917c2fdda0dSAndy Whitcroft			my $name = $1;
3918773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3919773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3920c2fdda0dSAndy Whitcroft
3921c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3922773647a0SAndy Whitcroft			if ($name =~ /^(?:
3923773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3924773647a0SAndy Whitcroft				volatile|__volatile__|
3925773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3926773647a0SAndy Whitcroft				asm|__asm__)$/x)
3927773647a0SAndy Whitcroft			{
3928c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3929c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3930c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3931c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3932773647a0SAndy Whitcroft
3933773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3934c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3935c2fdda0dSAndy Whitcroft
3936c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3937c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3938773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3939c2fdda0dSAndy Whitcroft
3940c2fdda0dSAndy Whitcroft			} else {
39413705ce5bSJoe Perches				if (WARN("SPACING",
39423705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
39433705ce5bSJoe Perches					     $fix) {
3944194f66fcSJoe Perches					$fixed[$fixlinenr] =~
39453705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
39463705ce5bSJoe Perches				}
3947f0a594c1SAndy Whitcroft			}
39486c72ffaaSAndy Whitcroft		}
39499a4cad4eSEric Nelson
3950653d4876SAndy Whitcroft# Check operator spacing.
39510a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
39523705ce5bSJoe Perches			my $fixed_line = "";
39533705ce5bSJoe Perches			my $line_fixed = 0;
39543705ce5bSJoe Perches
39559c0ca6f9SAndy Whitcroft			my $ops = qr{
39569c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
39579c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
39589c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
39591f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
396084731623SJoe Perches				\?:|\?|:
39619c0ca6f9SAndy Whitcroft			}x;
3962cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
39633705ce5bSJoe Perches
39643705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
39653705ce5bSJoe Perches##			foreach my $el (@elements) {
39663705ce5bSJoe Perches##				print("el: <$el>\n");
39673705ce5bSJoe Perches##			}
39683705ce5bSJoe Perches
39693705ce5bSJoe Perches			my @fix_elements = ();
397000df344fSAndy Whitcroft			my $off = 0;
39716c72ffaaSAndy Whitcroft
39723705ce5bSJoe Perches			foreach my $el (@elements) {
39733705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
39743705ce5bSJoe Perches				$off += length($el);
39753705ce5bSJoe Perches			}
39763705ce5bSJoe Perches
39773705ce5bSJoe Perches			$off = 0;
39783705ce5bSJoe Perches
39796c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3980b34c648bSJoe Perches			my $last_after = -1;
39816c72ffaaSAndy Whitcroft
39820a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
39833705ce5bSJoe Perches
39843705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
39853705ce5bSJoe Perches
39863705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
39873705ce5bSJoe Perches
39884a0df2efSAndy Whitcroft				$off += length($elements[$n]);
39894a0df2efSAndy Whitcroft
399025985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3991773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3992773647a0SAndy Whitcroft				my $cc = '';
3993773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3994773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3995773647a0SAndy Whitcroft				}
3996773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3997773647a0SAndy Whitcroft
39984a0df2efSAndy Whitcroft				my $a = '';
39994a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
40004a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
4001cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
40024a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
40034a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
4004773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
40054a0df2efSAndy Whitcroft
40060a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
40074a0df2efSAndy Whitcroft
40084a0df2efSAndy Whitcroft				my $c = '';
40090a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
40104a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
40114a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4012cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
40134a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
40144a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
40158b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
40164a0df2efSAndy Whitcroft				} else {
40174a0df2efSAndy Whitcroft					$c = 'E';
40180a920b5bSAndy Whitcroft				}
40190a920b5bSAndy Whitcroft
40204a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
40214a0df2efSAndy Whitcroft
40224a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
40234a0df2efSAndy Whitcroft
40246c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
4025de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
40260a920b5bSAndy Whitcroft
402774048ed8SAndy Whitcroft				# Pull out the value of this operator.
40286c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
40290a920b5bSAndy Whitcroft
40301f65f947SAndy Whitcroft				# Get the full operator variant.
40311f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
40321f65f947SAndy Whitcroft
403313214adfSAndy Whitcroft				# Ignore operators passed as parameters.
403413214adfSAndy Whitcroft				if ($op_type ne 'V' &&
4035d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
403613214adfSAndy Whitcroft
4037cf655043SAndy Whitcroft#				# Ignore comments
4038cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
403913214adfSAndy Whitcroft
4040d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
404113214adfSAndy Whitcroft				} elsif ($op eq ';') {
4042cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
4043cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
40443705ce5bSJoe Perches						if (ERROR("SPACING",
40453705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4046b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
40473705ce5bSJoe Perches							$line_fixed = 1;
40483705ce5bSJoe Perches						}
4049d8aaf121SAndy Whitcroft					}
4050d8aaf121SAndy Whitcroft
4051d8aaf121SAndy Whitcroft				# // is a comment
4052d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
40530a920b5bSAndy Whitcroft
4054b00e4814SJoe Perches				#   :   when part of a bitfield
4055b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4056b00e4814SJoe Perches					# skip the bitfield test for now
4057b00e4814SJoe Perches
40581f65f947SAndy Whitcroft				# No spaces for:
40591f65f947SAndy Whitcroft				#   ->
4060b00e4814SJoe Perches				} elsif ($op eq '->') {
40614a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
40623705ce5bSJoe Perches						if (ERROR("SPACING",
40633705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4064b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
40653705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
40663705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
40673705ce5bSJoe Perches							}
4068b34c648bSJoe Perches							$line_fixed = 1;
40693705ce5bSJoe Perches						}
40700a920b5bSAndy Whitcroft					}
40710a920b5bSAndy Whitcroft
40722381097bSJoe Perches				# , must not have a space before and must have a space on the right.
40730a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
40742381097bSJoe Perches					my $rtrim_before = 0;
40752381097bSJoe Perches					my $space_after = 0;
40762381097bSJoe Perches					if ($ctx =~ /Wx./) {
40772381097bSJoe Perches						if (ERROR("SPACING",
40782381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
40792381097bSJoe Perches							$line_fixed = 1;
40802381097bSJoe Perches							$rtrim_before = 1;
40812381097bSJoe Perches						}
40822381097bSJoe Perches					}
4083cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
40843705ce5bSJoe Perches						if (ERROR("SPACING",
40853705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
40863705ce5bSJoe Perches							$line_fixed = 1;
4087b34c648bSJoe Perches							$last_after = $n;
40882381097bSJoe Perches							$space_after = 1;
40892381097bSJoe Perches						}
40902381097bSJoe Perches					}
40912381097bSJoe Perches					if ($rtrim_before || $space_after) {
40922381097bSJoe Perches						if ($rtrim_before) {
40932381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
40942381097bSJoe Perches						} else {
40952381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
40962381097bSJoe Perches						}
40972381097bSJoe Perches						if ($space_after) {
40982381097bSJoe Perches							$good .= " ";
40993705ce5bSJoe Perches						}
41000a920b5bSAndy Whitcroft					}
41010a920b5bSAndy Whitcroft
41029c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
410374048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
41049c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
41059c0ca6f9SAndy Whitcroft
41069c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
41079c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
41089c0ca6f9SAndy Whitcroft				# unary operator, or a cast
41099c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
411074048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
41110d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4112cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
41133705ce5bSJoe Perches						if (ERROR("SPACING",
41143705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4115b34c648bSJoe Perches							if ($n != $last_after + 2) {
4116b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
41173705ce5bSJoe Perches								$line_fixed = 1;
41183705ce5bSJoe Perches							}
41190a920b5bSAndy Whitcroft						}
4120b34c648bSJoe Perches					}
4121a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4122171ae1a4SAndy Whitcroft						# A unary '*' may be const
4123171ae1a4SAndy Whitcroft
4124171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
41253705ce5bSJoe Perches						if (ERROR("SPACING",
41263705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4127b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
41283705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
41293705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
41303705ce5bSJoe Perches							}
4131b34c648bSJoe Perches							$line_fixed = 1;
41323705ce5bSJoe Perches						}
41330a920b5bSAndy Whitcroft					}
41340a920b5bSAndy Whitcroft
41350a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
41360a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4137773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
41383705ce5bSJoe Perches						if (ERROR("SPACING",
41393705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4140b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
41413705ce5bSJoe Perches							$line_fixed = 1;
41423705ce5bSJoe Perches						}
41430a920b5bSAndy Whitcroft					}
4144773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4145773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
41463705ce5bSJoe Perches						if (ERROR("SPACING",
41473705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4148b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
41493705ce5bSJoe Perches							$line_fixed = 1;
41503705ce5bSJoe Perches						}
4151653d4876SAndy Whitcroft					}
4152773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
41533705ce5bSJoe Perches						if (ERROR("SPACING",
41543705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4155b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
41563705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
41573705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4158773647a0SAndy Whitcroft							}
4159b34c648bSJoe Perches							$line_fixed = 1;
41603705ce5bSJoe Perches						}
41613705ce5bSJoe Perches					}
41620a920b5bSAndy Whitcroft
41630a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
41649c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
41659c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
41669c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4167c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4168c2fdda0dSAndy Whitcroft					 $op eq '%')
41690a920b5bSAndy Whitcroft				{
4170d2e025f3SJoe Perches					if ($check) {
4171d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4172d2e025f3SJoe Perches							if (CHK("SPACING",
4173d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4174d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4175d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4176d2e025f3SJoe Perches								$line_fixed = 1;
4177d2e025f3SJoe Perches							}
4178d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4179d2e025f3SJoe Perches							if (CHK("SPACING",
4180d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4181d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4182d2e025f3SJoe Perches								$line_fixed = 1;
4183d2e025f3SJoe Perches							}
4184d2e025f3SJoe Perches						}
4185d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
41863705ce5bSJoe Perches						if (ERROR("SPACING",
41873705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4188b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4189b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4190b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4191b34c648bSJoe Perches							}
41923705ce5bSJoe Perches							$line_fixed = 1;
41933705ce5bSJoe Perches						}
41940a920b5bSAndy Whitcroft					}
41950a920b5bSAndy Whitcroft
41961f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
41971f65f947SAndy Whitcroft				# terminating a case value or a label.
41981f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
41991f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
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						}
42051f65f947SAndy Whitcroft					}
42061f65f947SAndy Whitcroft
42070a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4208cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
42091f65f947SAndy Whitcroft					my $ok = 0;
42101f65f947SAndy Whitcroft
421122f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
42121f65f947SAndy Whitcroft					if (($op eq '<' &&
42131f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
42141f65f947SAndy Whitcroft					    ($op eq '>' &&
42151f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
42161f65f947SAndy Whitcroft					{
42171f65f947SAndy Whitcroft					    	$ok = 1;
42181f65f947SAndy Whitcroft					}
42191f65f947SAndy Whitcroft
4220e0df7e1fSJoe Perches					# for asm volatile statements
4221e0df7e1fSJoe Perches					# ignore a colon with another
4222e0df7e1fSJoe Perches					# colon immediately before or after
4223e0df7e1fSJoe Perches					if (($op eq ':') &&
4224e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4225e0df7e1fSJoe Perches						$ok = 1;
4226e0df7e1fSJoe Perches					}
4227e0df7e1fSJoe Perches
422884731623SJoe Perches					# messages are ERROR, but ?: are CHK
42291f65f947SAndy Whitcroft					if ($ok == 0) {
423084731623SJoe Perches						my $msg_type = \&ERROR;
423184731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
423284731623SJoe Perches
423384731623SJoe Perches						if (&{$msg_type}("SPACING",
42343705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
4235b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4236b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4237b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4238b34c648bSJoe Perches							}
42393705ce5bSJoe Perches							$line_fixed = 1;
42403705ce5bSJoe Perches						}
42410a920b5bSAndy Whitcroft					}
424222f2a2efSAndy Whitcroft				}
42434a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
42443705ce5bSJoe Perches
42453705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
42463705ce5bSJoe Perches
42473705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
42480a920b5bSAndy Whitcroft			}
42493705ce5bSJoe Perches
42503705ce5bSJoe Perches			if (($#elements % 2) == 0) {
42513705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
42523705ce5bSJoe Perches			}
42533705ce5bSJoe Perches
4254194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4255194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
42563705ce5bSJoe Perches			}
42573705ce5bSJoe Perches
42583705ce5bSJoe Perches
42590a920b5bSAndy Whitcroft		}
42600a920b5bSAndy Whitcroft
4261786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4262d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4263786b6326SJoe Perches			if (WARN("SPACING",
4264786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4265786b6326SJoe Perches			    $fix) {
4266194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4267786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4268786b6326SJoe Perches			}
4269786b6326SJoe Perches		}
4270786b6326SJoe Perches
4271f0a594c1SAndy Whitcroft# check for multiple assignments
4272f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4273000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4274000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4275f0a594c1SAndy Whitcroft		}
4276f0a594c1SAndy Whitcroft
427722f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
427822f2a2efSAndy Whitcroft## # continuation.
427922f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
428022f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
428122f2a2efSAndy Whitcroft##
428222f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
428322f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
428422f2a2efSAndy Whitcroft## 			my $ln = $line;
428522f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
428622f2a2efSAndy Whitcroft## 			}
428722f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4288000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4289000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
429022f2a2efSAndy Whitcroft## 			}
429122f2a2efSAndy Whitcroft## 		}
4292f0a594c1SAndy Whitcroft
42930a920b5bSAndy Whitcroft#need space before brace following if, while, etc
42946b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
42954e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
42963705ce5bSJoe Perches			if (ERROR("SPACING",
42973705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
42983705ce5bSJoe Perches			    $fix) {
4299194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
43003705ce5bSJoe Perches			}
4301de7d4f0eSAndy Whitcroft		}
4302de7d4f0eSAndy Whitcroft
4303c4a62ef9SJoe Perches## # check for blank lines before declarations
4304c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4305c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4306c4a62ef9SJoe Perches##			WARN("SPACING",
4307c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4308c4a62ef9SJoe Perches##		}
4309c4a62ef9SJoe Perches##
4310c4a62ef9SJoe Perches
4311de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4312de7d4f0eSAndy Whitcroft# on the line
4313de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4314d5e616fcSJoe Perches			if (ERROR("SPACING",
4315d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4316d5e616fcSJoe Perches			    $fix) {
4317194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4318d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4319d5e616fcSJoe Perches			}
43200a920b5bSAndy Whitcroft		}
43210a920b5bSAndy Whitcroft
432222f2a2efSAndy Whitcroft# check spacing on square brackets
432322f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
43243705ce5bSJoe Perches			if (ERROR("SPACING",
43253705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
43263705ce5bSJoe Perches			    $fix) {
4327194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43283705ce5bSJoe Perches				    s/\[\s+/\[/;
43293705ce5bSJoe Perches			}
433022f2a2efSAndy Whitcroft		}
433122f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
43323705ce5bSJoe Perches			if (ERROR("SPACING",
43333705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
43343705ce5bSJoe Perches			    $fix) {
4335194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43363705ce5bSJoe Perches				    s/\s+\]/\]/;
43373705ce5bSJoe Perches			}
433822f2a2efSAndy Whitcroft		}
433922f2a2efSAndy Whitcroft
4340c45dcabdSAndy Whitcroft# check spacing on parentheses
43419c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
43429c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
43433705ce5bSJoe Perches			if (ERROR("SPACING",
43443705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
43453705ce5bSJoe Perches			    $fix) {
4346194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43473705ce5bSJoe Perches				    s/\(\s+/\(/;
43483705ce5bSJoe Perches			}
434922f2a2efSAndy Whitcroft		}
435013214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4351c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4352c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
43533705ce5bSJoe Perches			if (ERROR("SPACING",
43543705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
43553705ce5bSJoe Perches			    $fix) {
4356194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43573705ce5bSJoe Perches				    s/\s+\)/\)/;
43583705ce5bSJoe Perches			}
435922f2a2efSAndy Whitcroft		}
436022f2a2efSAndy Whitcroft
4361e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4362e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4363e2826fd0SJoe Perches
4364e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4365ea4acbb1SJoe Perches			my $var = $1;
4366ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4367ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4368ea4acbb1SJoe Perches			    $fix) {
4369ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4370ea4acbb1SJoe Perches			}
4371ea4acbb1SJoe Perches		}
4372ea4acbb1SJoe Perches
4373ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4374ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4375ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4376ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4377ea4acbb1SJoe Perches			my $var = $2;
4378ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4379ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4380ea4acbb1SJoe Perches			    $fix) {
4381ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4382ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4383ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4384ea4acbb1SJoe Perches			}
4385e2826fd0SJoe Perches		}
4386e2826fd0SJoe Perches
43870a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
43884a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
43890a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
43903705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
43913705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
43923705ce5bSJoe Perches			    $fix) {
4393194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43943705ce5bSJoe Perches				    s/^(.)\s+/$1/;
43953705ce5bSJoe Perches			}
43960a920b5bSAndy Whitcroft		}
43970a920b5bSAndy Whitcroft
43985b9553abSJoe Perches# return is not a function
4399507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4400c45dcabdSAndy Whitcroft			my $spacing = $1;
4401507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
44025b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
44035b9553abSJoe Perches				my $value = $1;
44045b9553abSJoe Perches				$value = deparenthesize($value);
44055b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4406000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4407000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
44085b9553abSJoe Perches				}
4409c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4410000d1cc1SJoe Perches				ERROR("SPACING",
4411000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4412c45dcabdSAndy Whitcroft			}
4413c45dcabdSAndy Whitcroft		}
4414507e5141SJoe Perches
4415b43ae21bSJoe Perches# unnecessary return in a void function
4416b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4417b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4418b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4419b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4420b43ae21bSJoe Perches		    $linenr >= 3 &&
4421b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4422b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
44239819cf25SJoe Perches			WARN("RETURN_VOID",
4424b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
44259819cf25SJoe Perches               }
44269819cf25SJoe Perches
4427189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4428189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4429189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4430189248d8SJoe Perches			my $openparens = $1;
4431189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4432189248d8SJoe Perches			my $msg = "";
4433189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4434189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4435189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4436189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4437189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4438189248d8SJoe Perches			}
4439189248d8SJoe Perches		}
4440189248d8SJoe Perches
4441c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4442c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4443c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4444c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4445c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4446c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4447c5595fa2SJoe Perches			my $lead = $1;
4448c5595fa2SJoe Perches			my $const = $2;
4449c5595fa2SJoe Perches			my $comp = $3;
4450c5595fa2SJoe Perches			my $to = $4;
4451c5595fa2SJoe Perches			my $newcomp = $comp;
4452f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4453c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4454c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4455c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4456c5595fa2SJoe Perches			    $fix) {
4457c5595fa2SJoe Perches				if ($comp eq "<") {
4458c5595fa2SJoe Perches					$newcomp = ">";
4459c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4460c5595fa2SJoe Perches					$newcomp = ">=";
4461c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4462c5595fa2SJoe Perches					$newcomp = "<";
4463c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4464c5595fa2SJoe Perches					$newcomp = "<=";
4465c5595fa2SJoe Perches				}
4466c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4467c5595fa2SJoe Perches			}
4468c5595fa2SJoe Perches		}
4469c5595fa2SJoe Perches
4470f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4471f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
447253a3c448SAndy Whitcroft			my $name = $1;
447353a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4474000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4475f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
447653a3c448SAndy Whitcroft			}
447753a3c448SAndy Whitcroft		}
4478c45dcabdSAndy Whitcroft
44790a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
44804a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
44813705ce5bSJoe Perches			if (ERROR("SPACING",
44823705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
44833705ce5bSJoe Perches			    $fix) {
4484194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44853705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
44863705ce5bSJoe Perches			}
44870a920b5bSAndy Whitcroft		}
44880a920b5bSAndy Whitcroft
4489f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4490f5fe35ddSAndy Whitcroft# statements after the conditional.
4491170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
44923e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
44933e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
44943e469cdcSAndy Whitcroft					if (!defined $stat);
4495170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4496170d3a22SAndy Whitcroft						$remain_next, $off_next);
4497170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4498170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4499170d3a22SAndy Whitcroft
4500170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4501170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4502170d3a22SAndy Whitcroft				# then count those as offsets.
4503170d3a22SAndy Whitcroft				my ($whitespace) =
4504170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4505170d3a22SAndy Whitcroft				my $offset =
4506170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4507170d3a22SAndy Whitcroft
4508170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4509170d3a22SAndy Whitcroft								$offset} = 1;
4510170d3a22SAndy Whitcroft			}
4511170d3a22SAndy Whitcroft		}
4512170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4513c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4514170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4515171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
45168905a67cSAndy Whitcroft
4517b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4518000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4519000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
45208905a67cSAndy Whitcroft			}
45218905a67cSAndy Whitcroft
45228905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
45238905a67cSAndy Whitcroft			# conditional.
4524773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
45258905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
452613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
452753210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
452853210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4529773647a0SAndy Whitcroft			{
4530bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4531bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4532bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
453342bdf74cSHidetoshi Seto				my $stat_real = '';
4534bb44ad39SAndy Whitcroft
453542bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
453642bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4537bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4538bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4539bb44ad39SAndy Whitcroft				}
4540bb44ad39SAndy Whitcroft
4541000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4542000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
45438905a67cSAndy Whitcroft			}
45448905a67cSAndy Whitcroft		}
45458905a67cSAndy Whitcroft
454613214adfSAndy Whitcroft# Check for bitwise tests written as boolean
454713214adfSAndy Whitcroft		if ($line =~ /
454813214adfSAndy Whitcroft			(?:
454913214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
455013214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
455113214adfSAndy Whitcroft				(?:\&\&|\|\|)
455213214adfSAndy Whitcroft			|
455313214adfSAndy Whitcroft				(?:\&\&|\|\|)
455413214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
455513214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
455613214adfSAndy Whitcroft			)/x)
455713214adfSAndy Whitcroft		{
4558000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4559000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
456013214adfSAndy Whitcroft		}
456113214adfSAndy Whitcroft
45628905a67cSAndy Whitcroft# if and else should not have general statements after it
456313214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
456413214adfSAndy Whitcroft			my $s = $1;
456513214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
456613214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4567000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4568000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
45690a920b5bSAndy Whitcroft			}
457013214adfSAndy Whitcroft		}
457139667782SAndy Whitcroft# if should not continue a brace
457239667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4573000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4574048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
457539667782SAndy Whitcroft				$herecurr);
457639667782SAndy Whitcroft		}
4577a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4578a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4579a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
45803fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4581a1080bf8SAndy Whitcroft			\s*return\s+
4582a1080bf8SAndy Whitcroft		    )/xg)
4583a1080bf8SAndy Whitcroft		{
4584000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4585000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4586a1080bf8SAndy Whitcroft		}
45870a920b5bSAndy Whitcroft
45880a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
45890a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
45908b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
45910a920b5bSAndy Whitcroft		    $previndent == $indent) {
45928b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
45938b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
45948b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
45958b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
45968b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
45978b8856f4SJoe Perches				my $fixedline = $prevrawline;
45988b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
45998b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
46008b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
46018b8856f4SJoe Perches				}
46028b8856f4SJoe Perches				$fixedline = $rawline;
46038b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
46048b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
46058b8856f4SJoe Perches			}
46060a920b5bSAndy Whitcroft		}
46070a920b5bSAndy Whitcroft
46088b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4609c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4610c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4611c2fdda0dSAndy Whitcroft
4612c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4613c2fdda0dSAndy Whitcroft			# conditional.
4614773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4615c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4616c2fdda0dSAndy Whitcroft
4617c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
46188b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
46198b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
46208b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
46218b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
46228b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
46238b8856f4SJoe Perches					my $fixedline = $prevrawline;
46248b8856f4SJoe Perches					my $trailing = $rawline;
46258b8856f4SJoe Perches					$trailing =~ s/^\+//;
46268b8856f4SJoe Perches					$trailing = trim($trailing);
46278b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
46288b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
46298b8856f4SJoe Perches				}
4630c2fdda0dSAndy Whitcroft			}
4631c2fdda0dSAndy Whitcroft		}
4632c2fdda0dSAndy Whitcroft
463395e2c602SJoe Perches#Specific variable tests
4634323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4635323c1260SJoe Perches			my $var = $1;
463695e2c602SJoe Perches
463795e2c602SJoe Perches#gcc binary extension
463895e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4639d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4640d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4641d5e616fcSJoe Perches				    $fix) {
4642d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4643194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4644d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4645d5e616fcSJoe Perches				}
464695e2c602SJoe Perches			}
464795e2c602SJoe Perches
464895e2c602SJoe Perches#CamelCase
4649807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4650be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
465122735ce8SJoe Perches#Ignore Page<foo> variants
4652807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
465322735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4654f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4655f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4656f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
46577e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
46587e781f67SJoe Perches					my $word = $1;
46597e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4660d8b07710SJoe Perches					if ($check) {
4661d8b07710SJoe Perches						seed_camelcase_includes();
4662d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4663d8b07710SJoe Perches							seed_camelcase_file($realfile);
4664d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4665d8b07710SJoe Perches						}
4666d8b07710SJoe Perches					}
46677e781f67SJoe Perches					if (!defined $camelcase{$word}) {
46687e781f67SJoe Perches						$camelcase{$word} = 1;
4669be79794bSJoe Perches						CHK("CAMELCASE",
46707e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
46717e781f67SJoe Perches					}
4672323c1260SJoe Perches				}
4673323c1260SJoe Perches			}
46743445686aSJoe Perches		}
46750a920b5bSAndy Whitcroft
46760a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4677d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4678d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4679d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4680d5e616fcSJoe Perches			    $fix) {
4681194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4682d5e616fcSJoe Perches			}
46830a920b5bSAndy Whitcroft		}
46840a920b5bSAndy Whitcroft
46850e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
46860e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4687c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4688e09dec48SAndy Whitcroft			my $file = "$1.h";
4689e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4690e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4691e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
46927840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4693c45dcabdSAndy Whitcroft			{
46940e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
46950e212e0aSFabian Frederick				if ($asminclude > 0) {
4696e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4697000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4698000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4699e09dec48SAndy Whitcroft					} else {
4700000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4701000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4702e09dec48SAndy Whitcroft					}
47030a920b5bSAndy Whitcroft				}
47040a920b5bSAndy Whitcroft			}
47050e212e0aSFabian Frederick		}
47060a920b5bSAndy Whitcroft
4707653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4708653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4709cf655043SAndy Whitcroft# in a known good container
4710b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4711b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4712d8aaf121SAndy Whitcroft			my $ln = $linenr;
4713d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4714c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4715c45dcabdSAndy Whitcroft			my $ctx = '';
471608a2843eSJoe Perches			my $has_flow_statement = 0;
471708a2843eSJoe Perches			my $has_arg_concat = 0;
4718c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4719f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4720f74bd194SAndy Whitcroft			$ctx = $dstat;
4721c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4722a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4723c45dcabdSAndy Whitcroft
472408a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
472562e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
472608a2843eSJoe Perches
4727f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4728292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4729c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4730c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4731c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4732c45dcabdSAndy Whitcroft
4733c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4734bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4735bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
47366b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4737bf30d6edSAndy Whitcroft			{
4738c45dcabdSAndy Whitcroft			}
4739c45dcabdSAndy Whitcroft
4740e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
474133acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
474233acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4743e45bab8eSAndy Whitcroft			{
4744e45bab8eSAndy Whitcroft			}
4745e45bab8eSAndy Whitcroft
474642e15293SJoe Perches			# Make asm volatile uses seem like a generic function
474742e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
474842e15293SJoe Perches
4749c45dcabdSAndy Whitcroft			my $exceptions = qr{
4750c45dcabdSAndy Whitcroft				$Declare|
4751c45dcabdSAndy Whitcroft				module_param_named|
4752a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4753c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4754c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4755383099fdSAndy Whitcroft				__typeof__\(|
475622fd2d3eSStefani Seibold				union|
475722fd2d3eSStefani Seibold				struct|
4758ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
47596b10df42SVladimir Zapolskiy				^\"|\"$|
47606b10df42SVladimir Zapolskiy				^\[
4761c45dcabdSAndy Whitcroft			}x;
47625eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4763f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4764f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4765f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
47663cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4767356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4768f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4769f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4770e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
477172f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4772f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4773f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4774f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
47754e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4776f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4777c45dcabdSAndy Whitcroft			{
4778f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4779f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4780f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4781f74bd194SAndy Whitcroft
4782f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4783f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4784c45dcabdSAndy Whitcroft				}
4785c45dcabdSAndy Whitcroft
4786f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4787f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4788f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4789f74bd194SAndy Whitcroft				} else {
4790000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4791388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4792d8aaf121SAndy Whitcroft				}
47930a920b5bSAndy Whitcroft			}
47945023d347SJoe Perches
479508a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
479608a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
479708a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
479808a2843eSJoe Perches				my $herectx = $here . "\n";
479908a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
480008a2843eSJoe Perches
480108a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
480208a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
480308a2843eSJoe Perches				}
480408a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
480508a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
480608a2843eSJoe Perches			}
480708a2843eSJoe Perches
4808481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
48095023d347SJoe Perches
48105023d347SJoe Perches		} else {
48115023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4812481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4813481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
48145023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
48155023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
48165023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
48175023d347SJoe Perches			}
4818653d4876SAndy Whitcroft		}
48190a920b5bSAndy Whitcroft
4820b13edf7fSJoe Perches# do {} while (0) macro tests:
4821b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4822b13edf7fSJoe Perches# macro should not end with a semicolon
4823b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4824b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4825b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4826b13edf7fSJoe Perches			my $ln = $linenr;
4827b13edf7fSJoe Perches			my $cnt = $realcnt;
4828b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4829b13edf7fSJoe Perches			my $ctx = '';
4830b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4831b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4832b13edf7fSJoe Perches			$ctx = $dstat;
4833b13edf7fSJoe Perches
4834b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
48351b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4836b13edf7fSJoe Perches
4837b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4838b13edf7fSJoe Perches				my $stmts = $2;
4839b13edf7fSJoe Perches				my $semis = $3;
4840b13edf7fSJoe Perches
4841b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4842b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4843b13edf7fSJoe Perches				my $herectx = $here . "\n";
4844b13edf7fSJoe Perches
4845b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4846b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4847b13edf7fSJoe Perches				}
4848b13edf7fSJoe Perches
4849ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4850ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4851b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4852b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4853b13edf7fSJoe Perches				}
4854b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4855b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4856b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4857b13edf7fSJoe Perches				}
4858f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4859f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4860f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4861f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4862f5ef95b1SJoe Perches
4863f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4864f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4865f5ef95b1SJoe Perches				}
4866f5ef95b1SJoe Perches
4867f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4868f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4869b13edf7fSJoe Perches			}
4870b13edf7fSJoe Perches		}
4871b13edf7fSJoe Perches
4872080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4873080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4874080ba929SMike Frysinger#	.
4875080ba929SMike Frysinger#	ALIGN(...)
4876080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4877080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4878000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4879000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4880080ba929SMike Frysinger		}
4881080ba929SMike Frysinger
4882f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
488313214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
488413214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4885cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
488613214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4887cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4888cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4889aad4f614SJoe Perches				my @allowed = ();
4890aad4f614SJoe Perches				my $allow = 0;
489113214adfSAndy Whitcroft				my $seen = 0;
4892773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4893cf655043SAndy Whitcroft				my $ln = $linenr - 1;
489413214adfSAndy Whitcroft				for my $chunk (@chunks) {
489513214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
489613214adfSAndy Whitcroft
4897773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4898773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4899773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4900773647a0SAndy Whitcroft
4901aad4f614SJoe Perches					$allowed[$allow] = 0;
4902773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4903773647a0SAndy Whitcroft
4904773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4905773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4906773647a0SAndy Whitcroft
4907773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4908cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4909cf655043SAndy Whitcroft
4910773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
491113214adfSAndy Whitcroft
491213214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
491313214adfSAndy Whitcroft
4914aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4915cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4916cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4917aad4f614SJoe Perches						$allowed[$allow] = 1;
491813214adfSAndy Whitcroft					}
491913214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4920cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4921aad4f614SJoe Perches						$allowed[$allow] = 1;
492213214adfSAndy Whitcroft					}
4923cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4924cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4925aad4f614SJoe Perches						$allowed[$allow] = 1;
492613214adfSAndy Whitcroft					}
4927aad4f614SJoe Perches					$allow++;
492813214adfSAndy Whitcroft				}
4929aad4f614SJoe Perches				if ($seen) {
4930aad4f614SJoe Perches					my $sum_allowed = 0;
4931aad4f614SJoe Perches					foreach (@allowed) {
4932aad4f614SJoe Perches						$sum_allowed += $_;
4933aad4f614SJoe Perches					}
4934aad4f614SJoe Perches					if ($sum_allowed == 0) {
4935000d1cc1SJoe Perches						WARN("BRACES",
4936000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4937aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4938aad4f614SJoe Perches						 $seen != $allow) {
4939aad4f614SJoe Perches						CHK("BRACES",
4940aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4941aad4f614SJoe Perches					}
494213214adfSAndy Whitcroft				}
494313214adfSAndy Whitcroft			}
494413214adfSAndy Whitcroft		}
4945773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
494613214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4947cf655043SAndy Whitcroft			my $allowed = 0;
4948f0a594c1SAndy Whitcroft
4949cf655043SAndy Whitcroft			# Check the pre-context.
4950cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4951cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4952cf655043SAndy Whitcroft				$allowed = 1;
4953f0a594c1SAndy Whitcroft			}
4954773647a0SAndy Whitcroft
4955773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4956773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4957773647a0SAndy Whitcroft
4958cf655043SAndy Whitcroft			# Check the condition.
4959cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4960773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4961cf655043SAndy Whitcroft			if (defined $cond) {
4962773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4963cf655043SAndy Whitcroft			}
4964cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4965cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4966cf655043SAndy Whitcroft				$allowed = 1;
4967cf655043SAndy Whitcroft			}
4968cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4969cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4970cf655043SAndy Whitcroft				$allowed = 1;
4971cf655043SAndy Whitcroft			}
4972cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4973cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4974cf655043SAndy Whitcroft				$allowed = 1;
4975cf655043SAndy Whitcroft			}
4976cf655043SAndy Whitcroft			# Check the post-context.
4977cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4978cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4979cf655043SAndy Whitcroft				if (defined $cond) {
4980773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4981cf655043SAndy Whitcroft				}
4982cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4983cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4984cf655043SAndy Whitcroft					$allowed = 1;
4985cf655043SAndy Whitcroft				}
4986cf655043SAndy Whitcroft			}
4987cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
498869932487SJustin P. Mattock				my $herectx = $here . "\n";
4989f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4990cf655043SAndy Whitcroft
4991f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
499269932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4993cf655043SAndy Whitcroft				}
4994cf655043SAndy Whitcroft
4995000d1cc1SJoe Perches				WARN("BRACES",
4996000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4997f0a594c1SAndy Whitcroft			}
4998f0a594c1SAndy Whitcroft		}
4999f0a594c1SAndy Whitcroft
50000979ae66SJoe Perches# check for unnecessary blank lines around braces
500177b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
5002f8e58219SJoe Perches			if (CHK("BRACES",
5003f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5004f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
5005f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
5006f8e58219SJoe Perches			}
50070979ae66SJoe Perches		}
500877b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
5009f8e58219SJoe Perches			if (CHK("BRACES",
5010f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5011f8e58219SJoe Perches			    $fix) {
5012f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
5013f8e58219SJoe Perches			}
50140979ae66SJoe Perches		}
50150979ae66SJoe Perches
50164a0df2efSAndy Whitcroft# no volatiles please
50176c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
50186c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
5019000d1cc1SJoe Perches			WARN("VOLATILE",
5020000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
50214a0df2efSAndy Whitcroft		}
50224a0df2efSAndy Whitcroft
50235e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
50245e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
50255e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
50265e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
502733acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
50285e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
50295e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
50305e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
50315e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
50325e4f6ba5SJoe Perches				     $fix &&
50335e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
50345e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
50355e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
50365e4f6ba5SJoe Perches				my $comma_close = "";
50375e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
50385e4f6ba5SJoe Perches					$comma_close = $1;
50395e4f6ba5SJoe Perches				}
50405e4f6ba5SJoe Perches
50415e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
50425e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
50435e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
50445e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
50455e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
50465e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
50475e4f6ba5SJoe Perches				$fixedline = $rawline;
50485e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
50495e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
50505e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50515e4f6ba5SJoe Perches				}
50525e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
50535e4f6ba5SJoe Perches			}
50545e4f6ba5SJoe Perches		}
50555e4f6ba5SJoe Perches
50565e4f6ba5SJoe Perches# check for missing a space in a string concatenation
50575e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
50585e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
50595e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
50605e4f6ba5SJoe Perches		}
50615e4f6ba5SJoe Perches
50625e4f6ba5SJoe Perches# check for spaces before a quoted newline
50635e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
50645e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
50655e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
50665e4f6ba5SJoe Perches			    $fix) {
50675e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
50685e4f6ba5SJoe Perches			}
50695e4f6ba5SJoe Perches
50705e4f6ba5SJoe Perches		}
50715e4f6ba5SJoe Perches
5072f17dba4fSJoe Perches# concatenated string without spaces between elements
507333acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5074f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
5075f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
5076f17dba4fSJoe Perches		}
5077f17dba4fSJoe Perches
507890ad30e5SJoe Perches# uncoalesced string fragments
507933acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
508090ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
508190ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
508290ad30e5SJoe Perches		}
508390ad30e5SJoe Perches
50846e300757SJoe Perches# check for %L{u,d,i} and 0x%[udi] in strings
50855e4f6ba5SJoe Perches		my $string;
50865e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
50875e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
50885e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
50896e300757SJoe Perches			if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
50905e4f6ba5SJoe Perches				WARN("PRINTF_L",
50915e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
50925e4f6ba5SJoe Perches				last;
50935e4f6ba5SJoe Perches			}
50946e300757SJoe Perches			if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
50956e300757SJoe Perches				ERROR("PRINTF_0xDECIMAL",
50966e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
50976e300757SJoe Perches			}
50985e4f6ba5SJoe Perches		}
50995e4f6ba5SJoe Perches
51005e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
51015e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
51025e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
51035e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
51045e4f6ba5SJoe Perches		}
51055e4f6ba5SJoe Perches
510600df344fSAndy Whitcroft# warn about #if 0
5107c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5108000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
5109000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
5110de7d4f0eSAndy Whitcroft				$herecurr);
51114a0df2efSAndy Whitcroft		}
51124a0df2efSAndy Whitcroft
511303df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
511403df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5115100425deSJoe Perches			my $tested = quotemeta($1);
5116100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5117100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5118100425deSJoe Perches				my $func = $1;
5119100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5120100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5121100425deSJoe Perches				    $fix) {
5122100425deSJoe Perches					my $do_fix = 1;
5123100425deSJoe Perches					my $leading_tabs = "";
5124100425deSJoe Perches					my $new_leading_tabs = "";
5125100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5126100425deSJoe Perches						$leading_tabs = $1;
5127100425deSJoe Perches					} else {
5128100425deSJoe Perches						$do_fix = 0;
5129100425deSJoe Perches					}
5130100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5131100425deSJoe Perches						$new_leading_tabs = $1;
5132100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5133100425deSJoe Perches							$do_fix = 0;
5134100425deSJoe Perches						}
5135100425deSJoe Perches					} else {
5136100425deSJoe Perches						$do_fix = 0;
5137100425deSJoe Perches					}
5138100425deSJoe Perches					if ($do_fix) {
5139100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5140100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5141100425deSJoe Perches					}
5142100425deSJoe Perches				}
51434c432a8fSGreg Kroah-Hartman			}
51444c432a8fSGreg Kroah-Hartman		}
5145f0a594c1SAndy Whitcroft
5146ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5147ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5148ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5149ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5150ebfdc409SJoe Perches		    $linenr > 3) {
5151ebfdc409SJoe Perches			my $testval = $2;
5152ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5153ebfdc409SJoe Perches
5154ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5155ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5156ebfdc409SJoe Perches
5157ebfdc409SJoe 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)/) {
5158ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5159ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5160ebfdc409SJoe Perches			}
5161ebfdc409SJoe Perches		}
5162ebfdc409SJoe Perches
5163f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5164dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5165f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5166f78d98f6SJoe Perches			my $level = $1;
5167f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5168f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5169f78d98f6SJoe Perches			    $fix) {
5170f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5171f78d98f6SJoe Perches			}
5172f78d98f6SJoe Perches		}
5173f78d98f6SJoe Perches
5174abb08a53SJoe Perches# check for mask then right shift without a parentheses
5175abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5176abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5177abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5178abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5179abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5180abb08a53SJoe Perches		}
5181abb08a53SJoe Perches
5182b75ac618SJoe Perches# check for pointer comparisons to NULL
5183b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
5184b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5185b75ac618SJoe Perches				my $val = $1;
5186b75ac618SJoe Perches				my $equal = "!";
5187b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5188b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5189b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5190b75ac618SJoe Perches					    $fix) {
5191b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5192b75ac618SJoe Perches				}
5193b75ac618SJoe Perches			}
5194b75ac618SJoe Perches		}
5195b75ac618SJoe Perches
51968716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
51978716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
51988716de38SJoe Perches			my $attr = $1;
51998716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
52008716de38SJoe Perches				my $ptr = $1;
52018716de38SJoe Perches				my $var = $2;
52028716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
52038716de38SJoe Perches				      ERROR("MISPLACED_INIT",
52048716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
52058716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
52068716de38SJoe Perches				      WARN("MISPLACED_INIT",
52078716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
52088716de38SJoe Perches				    $fix) {
5209194f66fcSJoe 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;
52108716de38SJoe Perches				}
52118716de38SJoe Perches			}
52128716de38SJoe Perches		}
52138716de38SJoe Perches
5214e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5215e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5216e970b884SJoe Perches			my $attr = $1;
5217e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5218e970b884SJoe Perches			my $attr_prefix = $1;
5219e970b884SJoe Perches			my $attr_type = $2;
5220e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5221e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5222e970b884SJoe Perches			    $fix) {
5223194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5224e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5225e970b884SJoe Perches			}
5226e970b884SJoe Perches		}
5227e970b884SJoe Perches
5228e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5229e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5230e970b884SJoe Perches			my $attr = $1;
5231e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5232e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5233e970b884SJoe Perches			    $fix) {
5234194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5235e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5236e970b884SJoe Perches				$lead = rtrim($1);
5237e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5238e970b884SJoe Perches				$lead = "${lead}const ";
5239194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5240e970b884SJoe Perches			}
5241e970b884SJoe Perches		}
5242e970b884SJoe Perches
5243c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5244c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5245c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5246c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5247c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5248c17893c7SJoe Perches			    $fix) {
5249c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5250c17893c7SJoe Perches			}
5251c17893c7SJoe Perches		}
5252c17893c7SJoe Perches
5253fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5254fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5255fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5256fbdb8138SJoe Perches			my $constant_func = $1;
5257fbdb8138SJoe Perches			my $func = $constant_func;
5258fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5259fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5260fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5261fbdb8138SJoe Perches			    $fix) {
5262194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5263fbdb8138SJoe Perches			}
5264fbdb8138SJoe Perches		}
5265fbdb8138SJoe Perches
52661a15a250SPatrick Pannuto# prefer usleep_range over udelay
526737581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
526843c1d77cSJoe Perches			my $delay = $1;
52691a15a250SPatrick Pannuto			# ignore udelay's < 10, however
527043c1d77cSJoe Perches			if (! ($delay < 10) ) {
5271000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
527243c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
527343c1d77cSJoe Perches			}
527443c1d77cSJoe Perches			if ($delay > 2000) {
527543c1d77cSJoe Perches				WARN("LONG_UDELAY",
527643c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
52771a15a250SPatrick Pannuto			}
52781a15a250SPatrick Pannuto		}
52791a15a250SPatrick Pannuto
528009ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
528109ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
528209ef8725SPatrick Pannuto			if ($1 < 20) {
5283000d1cc1SJoe Perches				WARN("MSLEEP",
528443c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
528509ef8725SPatrick Pannuto			}
528609ef8725SPatrick Pannuto		}
528709ef8725SPatrick Pannuto
528836ec1939SJoe Perches# check for comparisons of jiffies
528936ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
529036ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
529136ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
529236ec1939SJoe Perches		}
529336ec1939SJoe Perches
52949d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
52959d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
52969d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
52979d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
52989d7a34a5SJoe Perches		}
52999d7a34a5SJoe Perches
530000df344fSAndy Whitcroft# warn about #ifdefs in C files
5301c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
530200df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
530300df344fSAndy Whitcroft#			print "$herecurr";
530400df344fSAndy Whitcroft#			$clean = 0;
530500df344fSAndy Whitcroft#		}
530600df344fSAndy Whitcroft
530722f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5308c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
53093705ce5bSJoe Perches			if (ERROR("SPACING",
53103705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
53113705ce5bSJoe Perches			    $fix) {
5312194f66fcSJoe Perches				$fixed[$fixlinenr] =~
53133705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
53143705ce5bSJoe Perches			}
53153705ce5bSJoe Perches
531622f2a2efSAndy Whitcroft		}
531722f2a2efSAndy Whitcroft
53184a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5319171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5320171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
53214a0df2efSAndy Whitcroft			my $which = $1;
53224a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5323000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5324000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
53254a0df2efSAndy Whitcroft			}
53264a0df2efSAndy Whitcroft		}
53274a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5328402c2553SMichael S. Tsirkin
5329402c2553SMichael S. Tsirkin		my $barriers = qr{
5330402c2553SMichael S. Tsirkin			mb|
5331402c2553SMichael S. Tsirkin			rmb|
5332402c2553SMichael S. Tsirkin			wmb|
5333402c2553SMichael S. Tsirkin			read_barrier_depends
5334402c2553SMichael S. Tsirkin		}x;
5335402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5336402c2553SMichael S. Tsirkin			mb__before_atomic|
5337402c2553SMichael S. Tsirkin			mb__after_atomic|
5338402c2553SMichael S. Tsirkin			store_release|
5339402c2553SMichael S. Tsirkin			load_acquire|
5340402c2553SMichael S. Tsirkin			store_mb|
5341402c2553SMichael S. Tsirkin			(?:$barriers)
5342402c2553SMichael S. Tsirkin		}x;
5343402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5344402c2553SMichael S. Tsirkin			(?:$barriers)|
534543e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
534643e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5347402c2553SMichael S. Tsirkin		}x;
5348402c2553SMichael S. Tsirkin
5349402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
53504a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5351c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5352000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
53534a0df2efSAndy Whitcroft			}
53544a0df2efSAndy Whitcroft		}
53553ad81779SPaul E. McKenney
5356f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5357f4073b0fSMichael S. Tsirkin
5358f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5359f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5360f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5361f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5362f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5363f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5364f4073b0fSMichael S. Tsirkin		}
5365f4073b0fSMichael S. Tsirkin
5366cb426e99SJoe Perches# check for waitqueue_active without a comment.
5367cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5368cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5369cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5370cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5371cb426e99SJoe Perches			}
5372cb426e99SJoe Perches		}
53733ad81779SPaul E. McKenney
53743ad81779SPaul E. McKenney# Check for expedited grace periods that interrupt non-idle non-nohz
53753ad81779SPaul E. McKenney# online CPUs.  These expedited can therefore degrade real-time response
53763ad81779SPaul E. McKenney# if used carelessly, and should be avoided where not absolutely
53773ad81779SPaul E. McKenney# needed.  It is always OK to use synchronize_rcu_expedited() and
53783ad81779SPaul E. McKenney# synchronize_sched_expedited() at boot time (before real-time applications
53793ad81779SPaul E. McKenney# start) and in error situations where real-time response is compromised in
53803ad81779SPaul E. McKenney# any case.  Note that synchronize_srcu_expedited() does -not- interrupt
53813ad81779SPaul E. McKenney# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
53823ad81779SPaul E. McKenney# Of course, nothing comes for free, and srcu_read_lock() and
53833ad81779SPaul E. McKenney# srcu_read_unlock() do contain full memory barriers in payment for
53843ad81779SPaul E. McKenney# synchronize_srcu_expedited() non-interruption properties.
53853ad81779SPaul E. McKenney		if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
53863ad81779SPaul E. McKenney			WARN("EXPEDITED_RCU_GRACE_PERIOD",
53873ad81779SPaul E. McKenney			     "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
53883ad81779SPaul E. McKenney
53893ad81779SPaul E. McKenney		}
53903ad81779SPaul E. McKenney
53914a0df2efSAndy Whitcroft# check of hardware specific defines
5392c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5393000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5394000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
53950a920b5bSAndy Whitcroft		}
5396653d4876SAndy Whitcroft
5397d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
5398d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5399000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5400000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
5401d4977c78STobias Klauser		}
5402d4977c78STobias Klauser
5403de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5404de7d4f0eSAndy Whitcroft# storage class and type.
54059c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
54069c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5407000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5408000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5409de7d4f0eSAndy Whitcroft		}
5410de7d4f0eSAndy Whitcroft
54118905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
54122b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
54132b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5414d5e616fcSJoe Perches			if (WARN("INLINE",
5415d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5416d5e616fcSJoe Perches			    $fix) {
5417194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5418d5e616fcSJoe Perches
5419d5e616fcSJoe Perches			}
54208905a67cSAndy Whitcroft		}
54218905a67cSAndy Whitcroft
54223d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
54232b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
54242b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5425000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5426000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
54273d130fd0SJoe Perches		}
54283d130fd0SJoe Perches
542939b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
54302b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
54312b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5432000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5433000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
543439b7e287SJoe Perches		}
543539b7e287SJoe Perches
54365f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
54372b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
54382b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5439d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5440d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5441d5e616fcSJoe Perches			    $fix) {
5442194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5443d5e616fcSJoe Perches
5444d5e616fcSJoe Perches			}
54455f14d3bdSJoe Perches		}
54465f14d3bdSJoe Perches
54476061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
54482b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
54492b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5450d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5451d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5452d5e616fcSJoe Perches			    $fix) {
5453194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5454d5e616fcSJoe Perches			}
54556061d949SJoe Perches		}
54566061d949SJoe Perches
5457619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5458619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5459619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5460619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5461619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5462619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5463619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5464619a908aSJoe Perches		}
5465619a908aSJoe Perches
5466e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/
5467e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5468e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5469e6176fa4SJoe Perches			my $type = $1;
5470e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5471e6176fa4SJoe Perches				$type = $1;
5472e6176fa4SJoe Perches				my $kernel_type = 'u';
5473e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5474e6176fa4SJoe Perches				$type =~ /(\d+)/;
5475e6176fa4SJoe Perches				$kernel_type .= $1;
5476e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5477e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5478e6176fa4SJoe Perches				    $fix) {
5479e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5480e6176fa4SJoe Perches				}
5481e6176fa4SJoe Perches			}
5482e6176fa4SJoe Perches		}
5483e6176fa4SJoe Perches
5484938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5485938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5486938224b5SJoe Perches			my $cast = $1;
5487938224b5SJoe Perches			my $const = $2;
5488938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5489938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5490938224b5SJoe Perches			    $fix) {
5491938224b5SJoe Perches				my $suffix = "";
5492938224b5SJoe Perches				my $newconst = $const;
5493938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5494938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5495938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5496938224b5SJoe Perches					$suffix .= 'LL';
5497938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5498938224b5SJoe Perches					$suffix .= 'L';
5499938224b5SJoe Perches				}
5500938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5501938224b5SJoe Perches			}
5502938224b5SJoe Perches		}
5503938224b5SJoe Perches
55048f53a9b8SJoe Perches# check for sizeof(&)
55058f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5506000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5507000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
55088f53a9b8SJoe Perches		}
55098f53a9b8SJoe Perches
551066c80b60SJoe Perches# check for sizeof without parenthesis
551166c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5512d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5513d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5514d5e616fcSJoe Perches			    $fix) {
5515194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5516d5e616fcSJoe Perches			}
551766c80b60SJoe Perches		}
551866c80b60SJoe Perches
551988982feaSJoe Perches# check for struct spinlock declarations
552088982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
552188982feaSJoe Perches			WARN("USE_SPINLOCK_T",
552288982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
552388982feaSJoe Perches		}
552488982feaSJoe Perches
5525a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
552606668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5527a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5528caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5529caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5530d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5531d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5532d5e616fcSJoe Perches				    $fix) {
5533194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5534d5e616fcSJoe Perches				}
5535a6962d72SJoe Perches			}
5536a6962d72SJoe Perches		}
5537a6962d72SJoe Perches
5538554e165cSAndy Whitcroft# Check for misused memsets
5539d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5540d1fe9c09SJoe Perches		    defined $stat &&
55419e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5542554e165cSAndy Whitcroft
5543d7c76ba7SJoe Perches			my $ms_addr = $2;
5544d1fe9c09SJoe Perches			my $ms_val = $7;
5545d1fe9c09SJoe Perches			my $ms_size = $12;
5546d7c76ba7SJoe Perches
5547554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5548554e165cSAndy Whitcroft				ERROR("MEMSET",
5549d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5550554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5551554e165cSAndy Whitcroft				WARN("MEMSET",
5552d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5553d7c76ba7SJoe Perches			}
5554d7c76ba7SJoe Perches		}
5555d7c76ba7SJoe Perches
555698a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
555798a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
555810895d2cSMateusz Kulikowski		    defined $stat &&
555910895d2cSMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
556098a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
556110895d2cSMateusz Kulikowski				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
556298a9bba5SJoe Perches			    $fix) {
5563194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
556498a9bba5SJoe Perches			}
556598a9bba5SJoe Perches		}
556698a9bba5SJoe Perches
5567b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5568b6117d17SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
5569b6117d17SMateusz Kulikowski		    defined $stat &&
5570b6117d17SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5571b6117d17SMateusz Kulikowski			WARN("PREFER_ETHER_ADDR_EQUAL",
5572b6117d17SMateusz Kulikowski			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5573b6117d17SMateusz Kulikowski		}
5574b6117d17SMateusz Kulikowski
55758617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
55768617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
55778617cd09SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
55788617cd09SMateusz Kulikowski		    defined $stat &&
55798617cd09SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
55808617cd09SMateusz Kulikowski
55818617cd09SMateusz Kulikowski			my $ms_val = $7;
55828617cd09SMateusz Kulikowski
55838617cd09SMateusz Kulikowski			if ($ms_val =~ /^(?:0x|)0+$/i) {
55848617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_ZERO_ADDR",
55858617cd09SMateusz Kulikowski					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
55868617cd09SMateusz Kulikowski				    $fix) {
55878617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
55888617cd09SMateusz Kulikowski				}
55898617cd09SMateusz Kulikowski			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
55908617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_BROADCAST_ADDR",
55918617cd09SMateusz Kulikowski					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
55928617cd09SMateusz Kulikowski				    $fix) {
55938617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
55948617cd09SMateusz Kulikowski				}
55958617cd09SMateusz Kulikowski			}
55968617cd09SMateusz Kulikowski		}
55978617cd09SMateusz Kulikowski
5598d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5599d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5600d1fe9c09SJoe Perches		    defined $stat &&
5601d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5602d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5603d7c76ba7SJoe Perches				my $call = $1;
5604d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5605d7c76ba7SJoe Perches				my $arg1 = $3;
5606d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5607d1fe9c09SJoe Perches				my $arg2 = $8;
5608d7c76ba7SJoe Perches				my $cast;
5609d7c76ba7SJoe Perches
5610d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5611d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5612d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5613d7c76ba7SJoe Perches					$cast = $cast1;
5614d7c76ba7SJoe Perches				} else {
5615d7c76ba7SJoe Perches					$cast = $cast2;
5616d7c76ba7SJoe Perches				}
5617d7c76ba7SJoe Perches				WARN("MINMAX",
5618d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5619554e165cSAndy Whitcroft			}
5620554e165cSAndy Whitcroft		}
5621554e165cSAndy Whitcroft
56224a273195SJoe Perches# check usleep_range arguments
56234a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
56244a273195SJoe Perches		    defined $stat &&
56254a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
56264a273195SJoe Perches			my $min = $1;
56274a273195SJoe Perches			my $max = $7;
56284a273195SJoe Perches			if ($min eq $max) {
56294a273195SJoe Perches				WARN("USLEEP_RANGE",
56304a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
56314a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
56324a273195SJoe Perches				 $min > $max) {
56334a273195SJoe Perches				WARN("USLEEP_RANGE",
56344a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
56354a273195SJoe Perches			}
56364a273195SJoe Perches		}
56374a273195SJoe Perches
5638823b794cSJoe Perches# check for naked sscanf
5639823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5640823b794cSJoe Perches		    defined $stat &&
56416c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5642823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5643823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5644823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5645823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5646823b794cSJoe Perches			$lc = $lc + $linenr;
5647823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5648823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5649823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5650823b794cSJoe Perches			}
5651823b794cSJoe Perches			WARN("NAKED_SSCANF",
5652823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5653823b794cSJoe Perches		}
5654823b794cSJoe Perches
5655afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5656afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5657afc819abSJoe Perches		    defined $stat &&
5658afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5659afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5660afc819abSJoe Perches			$lc = $lc + $linenr;
5661afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5662afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5663afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5664afc819abSJoe Perches			}
5665afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5666afc819abSJoe Perches				my $format = $6;
5667afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5668afc819abSJoe Perches				if ($count == 1 &&
5669afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5670afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5671afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5672afc819abSJoe Perches				}
5673afc819abSJoe Perches			}
5674afc819abSJoe Perches		}
5675afc819abSJoe Perches
567670dc8a48SJoe Perches# check for new externs in .h files.
567770dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
567870dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5679d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
568070dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
568170dc8a48SJoe Perches			    $fix) {
5682194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
568370dc8a48SJoe Perches			}
568470dc8a48SJoe Perches		}
568570dc8a48SJoe Perches
5686de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5687171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5688c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5689171ae1a4SAndy Whitcroft		{
5690c45dcabdSAndy Whitcroft			my $function_name = $1;
5691c45dcabdSAndy Whitcroft			my $paren_space = $2;
5692171ae1a4SAndy Whitcroft
5693171ae1a4SAndy Whitcroft			my $s = $stat;
5694171ae1a4SAndy Whitcroft			if (defined $cond) {
5695171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5696171ae1a4SAndy Whitcroft			}
5697c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5698c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5699c45dcabdSAndy Whitcroft			{
5700000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5701000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5702de7d4f0eSAndy Whitcroft			}
5703de7d4f0eSAndy Whitcroft
5704171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5705000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5706000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5707171ae1a4SAndy Whitcroft			}
57089c9ba34eSAndy Whitcroft
57099c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
57109c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
57119c9ba34eSAndy Whitcroft		{
5712000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5713000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5714171ae1a4SAndy Whitcroft		}
5715171ae1a4SAndy Whitcroft
5716de7d4f0eSAndy Whitcroft# checks for new __setup's
5717de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5718de7d4f0eSAndy Whitcroft			my $name = $1;
5719de7d4f0eSAndy Whitcroft
5720de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5721000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5722000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5723de7d4f0eSAndy Whitcroft			}
5724653d4876SAndy Whitcroft		}
57259c0ca6f9SAndy Whitcroft
57269c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5727caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5728000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5729000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
57309c0ca6f9SAndy Whitcroft		}
573113214adfSAndy Whitcroft
5732a640d25cSJoe Perches# alloc style
5733a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5734a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5735a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5736a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5737a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5738a640d25cSJoe Perches		}
5739a640d25cSJoe Perches
574060a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
574160a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5742e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
574360a55369SJoe Perches			my $oldfunc = $3;
574460a55369SJoe Perches			my $a1 = $4;
574560a55369SJoe Perches			my $a2 = $10;
574660a55369SJoe Perches			my $newfunc = "kmalloc_array";
574760a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
574860a55369SJoe Perches			my $r1 = $a1;
574960a55369SJoe Perches			my $r2 = $a2;
575060a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
575160a55369SJoe Perches				$r1 = $a2;
575260a55369SJoe Perches				$r2 = $a1;
575360a55369SJoe Perches			}
5754e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5755e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5756e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5757e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5758e367455aSJoe Perches				    $fix) {
5759194f66fcSJoe 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;
576060a55369SJoe Perches
576160a55369SJoe Perches				}
576260a55369SJoe Perches			}
576360a55369SJoe Perches		}
576460a55369SJoe Perches
5765972fdea2SJoe Perches# check for krealloc arg reuse
5766972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5767972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5768972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5769972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5770972fdea2SJoe Perches		}
5771972fdea2SJoe Perches
57725ce59ae0SJoe Perches# check for alloc argument mismatch
57735ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
57745ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
57755ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
57765ce59ae0SJoe Perches		}
57775ce59ae0SJoe Perches
5778caf2a54fSJoe Perches# check for multiple semicolons
5779caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5780d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5781d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5782d5e616fcSJoe Perches			    $fix) {
5783194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5784d5e616fcSJoe Perches			}
5785d1e2ad07SJoe Perches		}
5786d1e2ad07SJoe Perches
5787cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5788cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
5789cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
57900ab90191SJoe Perches			my $ull = "";
57910ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
57920ab90191SJoe Perches			if (CHK("BIT_MACRO",
57930ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
57940ab90191SJoe Perches			    $fix) {
57950ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
57960ab90191SJoe Perches			}
57970ab90191SJoe Perches		}
57980ab90191SJoe Perches
57992d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
58002d632745SJoe 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*$/) {
58012d632745SJoe Perches			my $config = $1;
58022d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
58032d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
58042d632745SJoe Perches			    $fix) {
58052d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
58062d632745SJoe Perches			}
58072d632745SJoe Perches		}
58082d632745SJoe Perches
5809e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5810c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5811c34c09a8SJoe Perches			my $has_break = 0;
5812c34c09a8SJoe Perches			my $has_statement = 0;
5813c34c09a8SJoe Perches			my $count = 0;
5814c34c09a8SJoe Perches			my $prevline = $linenr;
5815e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5816c34c09a8SJoe Perches				$prevline--;
5817c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5818c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5819c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5820c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5821c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5822c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5823c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5824c34c09a8SJoe Perches				$has_statement = 1;
5825c34c09a8SJoe Perches				$count++;
5826c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5827c34c09a8SJoe Perches			}
5828c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5829c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5830c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5831c34c09a8SJoe Perches			}
5832c34c09a8SJoe Perches		}
5833c34c09a8SJoe Perches
5834d1e2ad07SJoe Perches# check for switch/default statements without a break;
5835d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5836d1e2ad07SJoe Perches		    defined $stat &&
5837d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5838d1e2ad07SJoe Perches			my $ctx = '';
5839d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5840d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5841d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5842d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5843d1e2ad07SJoe Perches			}
5844d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5845d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5846caf2a54fSJoe Perches		}
5847caf2a54fSJoe Perches
584813214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5849d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5850d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5851d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5852d5e616fcSJoe Perches			    $fix) {
5853194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5854d5e616fcSJoe Perches			}
585513214adfSAndy Whitcroft		}
5856773647a0SAndy Whitcroft
585762ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
585862ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
585962ec818fSJoe Perches			ERROR("DATE_TIME",
586062ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
586162ec818fSJoe Perches		}
586262ec818fSJoe Perches
58632c92488aSJoe Perches# check for use of yield()
58642c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
58652c92488aSJoe Perches			WARN("YIELD",
58662c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
58672c92488aSJoe Perches		}
58682c92488aSJoe Perches
5869179f8f40SJoe Perches# check for comparisons against true and false
5870179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5871179f8f40SJoe Perches			my $lead = $1;
5872179f8f40SJoe Perches			my $arg = $2;
5873179f8f40SJoe Perches			my $test = $3;
5874179f8f40SJoe Perches			my $otype = $4;
5875179f8f40SJoe Perches			my $trail = $5;
5876179f8f40SJoe Perches			my $op = "!";
5877179f8f40SJoe Perches
5878179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5879179f8f40SJoe Perches
5880179f8f40SJoe Perches			my $type = lc($otype);
5881179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5882179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5883179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5884179f8f40SJoe Perches					$op = "";
5885179f8f40SJoe Perches				}
5886179f8f40SJoe Perches
5887179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5888179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5889179f8f40SJoe Perches
5890179f8f40SJoe Perches## maybe suggesting a correct construct would better
5891179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5892179f8f40SJoe Perches
5893179f8f40SJoe Perches			}
5894179f8f40SJoe Perches		}
5895179f8f40SJoe Perches
58964882720bSThomas Gleixner# check for semaphores initialized locked
58974882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5898000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5899000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5900773647a0SAndy Whitcroft		}
59016712d858SJoe Perches
590267d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
590367d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5904000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
590567d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5906773647a0SAndy Whitcroft		}
59076712d858SJoe Perches
5908ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5909f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5910000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5911ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5912f3db6639SMichael Ellerman		}
59136712d858SJoe Perches
59140f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
59150f3c5aabSJoe Perches		my $const_structs = qr{
59160f3c5aabSJoe Perches				acpi_dock_ops|
591779404849SEmese Revfy				address_space_operations|
591879404849SEmese Revfy				backlight_ops|
591979404849SEmese Revfy				block_device_operations|
592079404849SEmese Revfy				dentry_operations|
592179404849SEmese Revfy				dev_pm_ops|
592279404849SEmese Revfy				dma_map_ops|
592379404849SEmese Revfy				extent_io_ops|
592479404849SEmese Revfy				file_lock_operations|
592579404849SEmese Revfy				file_operations|
592679404849SEmese Revfy				hv_ops|
592779404849SEmese Revfy				ide_dma_ops|
592879404849SEmese Revfy				intel_dvo_dev_ops|
592979404849SEmese Revfy				item_operations|
593079404849SEmese Revfy				iwl_ops|
593179404849SEmese Revfy				kgdb_arch|
593279404849SEmese Revfy				kgdb_io|
593379404849SEmese Revfy				kset_uevent_ops|
593479404849SEmese Revfy				lock_manager_operations|
593579404849SEmese Revfy				microcode_ops|
593679404849SEmese Revfy				mtrr_ops|
593779404849SEmese Revfy				neigh_ops|
593879404849SEmese Revfy				nlmsvc_binding|
59390f3c5aabSJoe Perches				of_device_id|
594079404849SEmese Revfy				pci_raw_ops|
594179404849SEmese Revfy				pipe_buf_operations|
594279404849SEmese Revfy				platform_hibernation_ops|
594379404849SEmese Revfy				platform_suspend_ops|
594479404849SEmese Revfy				proto_ops|
594579404849SEmese Revfy				rpc_pipe_ops|
594679404849SEmese Revfy				seq_operations|
594779404849SEmese Revfy				snd_ac97_build_ops|
594879404849SEmese Revfy				soc_pcmcia_socket_ops|
594979404849SEmese Revfy				stacktrace_ops|
595079404849SEmese Revfy				sysfs_ops|
595179404849SEmese Revfy				tty_operations|
59526d07d01bSJoe Perches				uart_ops|
595379404849SEmese Revfy				usb_mon_operations|
595479404849SEmese Revfy				wd_ops}x;
59556903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
59560f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
5957000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5958000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
59596903ffb2SAndy Whitcroft				$herecurr);
59602b6db5cbSAndy Whitcroft		}
5961773647a0SAndy Whitcroft
5962773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5963773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5964773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5965c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5966c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5967171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5968171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5969171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5970773647a0SAndy Whitcroft		{
5971000d1cc1SJoe Perches			WARN("NR_CPUS",
5972000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5973773647a0SAndy Whitcroft		}
59749c9ba34eSAndy Whitcroft
597552ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
597652ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
597752ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
597852ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
597952ea8506SJoe Perches		}
598052ea8506SJoe Perches
5981acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5982acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5983acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5984acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5985acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5986acd9362cSJoe Perches		}
5987acd9362cSJoe Perches
5988691d77b6SAndy Whitcroft# whine mightly about in_atomic
5989691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5990691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5991000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5992000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5993f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5994000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5995000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5996691d77b6SAndy Whitcroft			}
5997691d77b6SAndy Whitcroft		}
59981704f47bSPeter Zijlstra
5999481aea5cSJoe Perches# whine about ACCESS_ONCE
6000481aea5cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6001481aea5cSJoe Perches		    $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6002481aea5cSJoe Perches			my $par = $1;
6003481aea5cSJoe Perches			my $eq = $2;
6004481aea5cSJoe Perches			my $fun = $3;
6005481aea5cSJoe Perches			$par =~ s/^\(\s*(.*)\s*\)$/$1/;
6006481aea5cSJoe Perches			if (defined($eq)) {
6007481aea5cSJoe Perches				if (WARN("PREFER_WRITE_ONCE",
6008481aea5cSJoe Perches					 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6009481aea5cSJoe Perches				    $fix) {
6010481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6011481aea5cSJoe Perches				}
6012481aea5cSJoe Perches			} else {
6013481aea5cSJoe Perches				if (WARN("PREFER_READ_ONCE",
6014481aea5cSJoe Perches					 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6015481aea5cSJoe Perches				    $fix) {
6016481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6017481aea5cSJoe Perches				}
6018481aea5cSJoe Perches			}
6019481aea5cSJoe Perches		}
6020481aea5cSJoe Perches
60211704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
60221704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
60231704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
60241704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
60251704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
60261704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
6027000d1cc1SJoe Perches				ERROR("LOCKDEP",
6028000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
60291704f47bSPeter Zijlstra			}
60301704f47bSPeter Zijlstra		}
603188f8831cSDave Jones
6032b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6033b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
6034000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
6035000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
603688f8831cSDave Jones		}
60372435880fSJoe Perches
6038515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
6039515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6040515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
6041515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
60422435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
60432435880fSJoe Perches				my $func = $entry->[0];
60442435880fSJoe Perches				my $arg_pos = $entry->[1];
60452435880fSJoe Perches
60462435880fSJoe Perches				my $skip_args = "";
60472435880fSJoe Perches				if ($arg_pos > 1) {
60482435880fSJoe Perches					$arg_pos--;
60492435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
60502435880fSJoe Perches				}
6051f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
6052515a235eSJoe Perches				if ($line =~ /$test/) {
60532435880fSJoe Perches					my $val = $1;
60542435880fSJoe Perches					$val = $6 if ($skip_args ne "");
6055f90774e1SJoe Perches					if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6056f90774e1SJoe Perches					    ($val =~ /^$Octal$/ && length($val) ne 4)) {
60572435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
60581727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
6059f90774e1SJoe Perches					}
6060f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6061c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6062c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
60632435880fSJoe Perches					}
6064f90774e1SJoe Perches					if ($val =~ /\b$mode_perms_string_search\b/) {
6065f90774e1SJoe Perches						my $to = 0;
6066f90774e1SJoe Perches						while ($val =~ /\b($mode_perms_string_search)\b(?:\s*\|\s*)?\s*/g) {
6067f90774e1SJoe Perches							$to |=  $mode_permission_string_types{$1};
6068f90774e1SJoe Perches						}
6069f90774e1SJoe Perches						my $new = sprintf("%04o", $to);
6070f90774e1SJoe Perches						if (WARN("SYMBOLIC_PERMS",
6071f90774e1SJoe Perches							 "Symbolic permissions are not preferred. Consider using octal permissions $new.\n" . $herecurr) &&
6072f90774e1SJoe Perches						    $fix) {
6073f90774e1SJoe Perches							$fixed[$fixlinenr] =~ s/\Q$val\E/$new/;
6074f90774e1SJoe Perches						}
6075f90774e1SJoe Perches					}
60762435880fSJoe Perches				}
60772435880fSJoe Perches			}
607813214adfSAndy Whitcroft		}
60795a6d20ceSBjorn Andersson
60805a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
60815a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
60825a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
60835a6d20ceSBjorn Andersson			my $valid_licenses = qr{
60845a6d20ceSBjorn Andersson						GPL|
60855a6d20ceSBjorn Andersson						GPL\ v2|
60865a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
60875a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
60885a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
60895a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
60905a6d20ceSBjorn Andersson						Proprietary
60915a6d20ceSBjorn Andersson					}x;
60925a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
60935a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
60945a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
60955a6d20ceSBjorn Andersson			}
60965a6d20ceSBjorn Andersson		}
6097515a235eSJoe Perches	}
609813214adfSAndy Whitcroft
609913214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
610013214adfSAndy Whitcroft	# so just keep quiet.
610113214adfSAndy Whitcroft	if ($#rawlines == -1) {
610213214adfSAndy Whitcroft		exit(0);
61030a920b5bSAndy Whitcroft	}
61040a920b5bSAndy Whitcroft
61058905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
61068905a67cSAndy Whitcroft	# things that appear to be patches.
61078905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
61088905a67cSAndy Whitcroft		exit(0);
61098905a67cSAndy Whitcroft	}
61108905a67cSAndy Whitcroft
61118905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
61128905a67cSAndy Whitcroft	# just keep quiet.
61138905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
61148905a67cSAndy Whitcroft		exit(0);
61158905a67cSAndy Whitcroft	}
61168905a67cSAndy Whitcroft
611706330fc4SJoe Perches	if (!$is_patch && $file !~ /cover-letter\.patch$/) {
6118000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6119000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
61200a920b5bSAndy Whitcroft	}
6121ed43c4e5SAllen Hubbe	if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6122000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
6123000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
61240a920b5bSAndy Whitcroft	}
61250a920b5bSAndy Whitcroft
6126f0a594c1SAndy Whitcroft	print report_dump();
612713214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
612813214adfSAndy Whitcroft		print "$filename " if ($summary_file);
61296c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
61306c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
61316c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
61326c72ffaaSAndy Whitcroft	}
61338905a67cSAndy Whitcroft
6134d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6135ef212196SJoe Perches		# If there were any defects found and not already fixing them
6136ef212196SJoe Perches		if (!$clean and !$fix) {
6137ef212196SJoe Perches			print << "EOM"
6138ef212196SJoe Perches
6139ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6140ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6141ef212196SJoe PerchesEOM
6142ef212196SJoe Perches		}
6143d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6144d2c0a235SAndy Whitcroft		# then suggest that.
6145d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6146b0781216SMike Frysinger			$rpt_cleaners = 0;
6147d8469f16SJoe Perches			print << "EOM"
6148d8469f16SJoe Perches
6149d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6150d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6151d8469f16SJoe PerchesEOM
6152d2c0a235SAndy Whitcroft		}
6153d2c0a235SAndy Whitcroft	}
6154d2c0a235SAndy Whitcroft
6155d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6156d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6157d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
61589624b8d6SJoe Perches		my $newfile = $filename;
61599624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
61603705ce5bSJoe Perches		my $linecount = 0;
61613705ce5bSJoe Perches		my $f;
61623705ce5bSJoe Perches
6163d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6164d752fcc8SJoe Perches
61653705ce5bSJoe Perches		open($f, '>', $newfile)
61663705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
61673705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
61683705ce5bSJoe Perches			$linecount++;
61693705ce5bSJoe Perches			if ($file) {
61703705ce5bSJoe Perches				if ($linecount > 3) {
61713705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
61723705ce5bSJoe Perches					print $f $fixed_line . "\n";
61733705ce5bSJoe Perches				}
61743705ce5bSJoe Perches			} else {
61753705ce5bSJoe Perches				print $f $fixed_line . "\n";
61763705ce5bSJoe Perches			}
61773705ce5bSJoe Perches		}
61783705ce5bSJoe Perches		close($f);
61793705ce5bSJoe Perches
61803705ce5bSJoe Perches		if (!$quiet) {
61813705ce5bSJoe Perches			print << "EOM";
6182d8469f16SJoe Perches
61833705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
61843705ce5bSJoe Perches
61853705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
61863705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
61873705ce5bSJoe Perches
61883705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
61893705ce5bSJoe PerchesNo warranties, expressed or implied...
61903705ce5bSJoe PerchesEOM
61913705ce5bSJoe Perches		}
61923705ce5bSJoe Perches	}
61933705ce5bSJoe Perches
6194d8469f16SJoe Perches	if ($quiet == 0) {
6195d8469f16SJoe Perches		print "\n";
6196d8469f16SJoe Perches		if ($clean == 1) {
6197d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6198d8469f16SJoe Perches		} else {
6199d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
62000a920b5bSAndy Whitcroft		}
62010a920b5bSAndy Whitcroft	}
62020a920b5bSAndy Whitcroft	return $clean;
62030a920b5bSAndy Whitcroft}
6204