xref: /linux-6.15/scripts/checkpatch.pl (revision 85b0ee18)
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
5447840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
5457840a94cSWolfram Sang	irq|
546cdcee686SSergey Ryazanov	memory|
547cdcee686SSergey Ryazanov	time|
548cdcee686SSergey Ryazanov	reboot
5497840a94cSWolfram Sang)};
5507840a94cSWolfram Sang# memory.h: ARM has a custom one
5517840a94cSWolfram Sang
55266b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
55366b47b4aSKees Cookmy $misspellings;
55466b47b4aSKees Cookmy %spelling_fix;
55536061e38SJoe Perches
55636061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
55766b47b4aSKees Cook	while (<$spelling>) {
55866b47b4aSKees Cook		my $line = $_;
55966b47b4aSKees Cook
56066b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
56166b47b4aSKees Cook		$line =~ s/^\s*//g;
56266b47b4aSKees Cook
56366b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
56466b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
56566b47b4aSKees Cook
56666b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
56766b47b4aSKees Cook
56866b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
56966b47b4aSKees Cook	}
57066b47b4aSKees Cook	close($spelling);
57136061e38SJoe Perches} else {
57236061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
57336061e38SJoe Perches}
57466b47b4aSKees Cook
575ebfd7d62SJoe Perchesif ($codespell) {
576ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
577ebfd7d62SJoe Perches		while (<$spelling>) {
578ebfd7d62SJoe Perches			my $line = $_;
579ebfd7d62SJoe Perches
580ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
581ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
582ebfd7d62SJoe Perches
583ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
584ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
585ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
586ebfd7d62SJoe Perches
587ebfd7d62SJoe Perches			$line =~ s/,.*$//;
588ebfd7d62SJoe Perches
589ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
590ebfd7d62SJoe Perches
591ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
592ebfd7d62SJoe Perches		}
593ebfd7d62SJoe Perches		close($spelling);
594ebfd7d62SJoe Perches	} else {
595ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
596ebfd7d62SJoe Perches	}
597ebfd7d62SJoe Perches}
598ebfd7d62SJoe Perches
599ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
600ebfd7d62SJoe Perches
6018905a67cSAndy Whitcroftsub build_types {
602485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
603485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
6041813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
6058716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
606c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
607ab7e23f3SJoe Perches	$BasicType	= qr{
608ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
609ab7e23f3SJoe Perches				(?:${all}\b)
610ab7e23f3SJoe Perches		}x;
6118905a67cSAndy Whitcroft	$NonptrType	= qr{
612d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
613cf655043SAndy Whitcroft			(?:
6146b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
6158ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
616c45dcabdSAndy Whitcroft				(?:${all}\b)
617cf655043SAndy Whitcroft			)
618c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
6198905a67cSAndy Whitcroft		  }x;
6201813087dSJoe Perches	$NonptrTypeMisordered	= qr{
6211813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
6221813087dSJoe Perches			(?:
6231813087dSJoe Perches				(?:${Misordered}\b)
6241813087dSJoe Perches			)
6251813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
6261813087dSJoe Perches		  }x;
6278716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
6288716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
6298716de38SJoe Perches			(?:
6308716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
6318716de38SJoe Perches				(?:$typeTypedefs\b)|
6328716de38SJoe Perches				(?:${allWithAttr}\b)
6338716de38SJoe Perches			)
6348716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
6358716de38SJoe Perches		  }x;
6368905a67cSAndy Whitcroft	$Type	= qr{
637c45dcabdSAndy Whitcroft			$NonptrType
6381574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
639c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
6408905a67cSAndy Whitcroft		  }x;
6411813087dSJoe Perches	$TypeMisordered	= qr{
6421813087dSJoe Perches			$NonptrTypeMisordered
6431813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
6441813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
6451813087dSJoe Perches		  }x;
64691cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
6471813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
6488905a67cSAndy Whitcroft}
6498905a67cSAndy Whitcroftbuild_types();
6506c72ffaaSAndy Whitcroft
6517d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
652d1fe9c09SJoe Perches
653d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
654d1fe9c09SJoe Perches# requires at least perl version v5.10.0
655d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
656d1fe9c09SJoe Perches
657d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
6582435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
659c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
6607d2367afSJoe Perches
661f8422308SJoe Perchesour $declaration_macros = qr{(?x:
6623e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
663f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
664f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
665f8422308SJoe Perches)};
666f8422308SJoe Perches
6677d2367afSJoe Perchessub deparenthesize {
6687d2367afSJoe Perches	my ($string) = @_;
6697d2367afSJoe Perches	return "" if (!defined($string));
6705b9553abSJoe Perches
6715b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
6725b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
6735b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
6745b9553abSJoe Perches	}
6755b9553abSJoe Perches
6767d2367afSJoe Perches	$string =~ s@\s+@ @g;
6775b9553abSJoe Perches
6787d2367afSJoe Perches	return $string;
6797d2367afSJoe Perches}
6807d2367afSJoe Perches
6813445686aSJoe Perchessub seed_camelcase_file {
6823445686aSJoe Perches	my ($file) = @_;
6833445686aSJoe Perches
6843445686aSJoe Perches	return if (!(-f $file));
6853445686aSJoe Perches
6863445686aSJoe Perches	local $/;
6873445686aSJoe Perches
6883445686aSJoe Perches	open(my $include_file, '<', "$file")
6893445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
6903445686aSJoe Perches	my $text = <$include_file>;
6913445686aSJoe Perches	close($include_file);
6923445686aSJoe Perches
6933445686aSJoe Perches	my @lines = split('\n', $text);
6943445686aSJoe Perches
6953445686aSJoe Perches	foreach my $line (@lines) {
6963445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
6973445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
6983445686aSJoe Perches			$camelcase{$1} = 1;
69911ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
70011ea516aSJoe Perches			$camelcase{$1} = 1;
70111ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
7023445686aSJoe Perches			$camelcase{$1} = 1;
7033445686aSJoe Perches		}
7043445686aSJoe Perches	}
7053445686aSJoe Perches}
7063445686aSJoe Perches
707*85b0ee18SJoe Perchessub is_maintained_obsolete {
708*85b0ee18SJoe Perches	my ($filename) = @_;
709*85b0ee18SJoe Perches
710*85b0ee18SJoe Perches	return 0 if (!(-e "$root/scripts/get_maintainer.pl"));
711*85b0ee18SJoe Perches
712*85b0ee18SJoe Perches	my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback $filename 2>&1`;
713*85b0ee18SJoe Perches
714*85b0ee18SJoe Perches	return $status =~ /obsolete/i;
715*85b0ee18SJoe Perches}
716*85b0ee18SJoe Perches
7173445686aSJoe Perchesmy $camelcase_seeded = 0;
7183445686aSJoe Perchessub seed_camelcase_includes {
7193445686aSJoe Perches	return if ($camelcase_seeded);
7203445686aSJoe Perches
7213445686aSJoe Perches	my $files;
722c707a81dSJoe Perches	my $camelcase_cache = "";
723c707a81dSJoe Perches	my @include_files = ();
724c707a81dSJoe Perches
725c707a81dSJoe Perches	$camelcase_seeded = 1;
726351b2a1fSJoe Perches
7273645e328SRichard Genoud	if (-e ".git") {
728351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
729351b2a1fSJoe Perches		chomp $git_last_include_commit;
730c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
731c707a81dSJoe Perches	} else {
732c707a81dSJoe Perches		my $last_mod_date = 0;
733c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
734c707a81dSJoe Perches		@include_files = split('\n', $files);
735c707a81dSJoe Perches		foreach my $file (@include_files) {
736c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
737c707a81dSJoe Perches						   localtime((stat $file)[9]));
738c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
739c707a81dSJoe Perches		}
740c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
741c707a81dSJoe Perches	}
742c707a81dSJoe Perches
743c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
744c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
745c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
746351b2a1fSJoe Perches		while (<$camelcase_file>) {
747351b2a1fSJoe Perches			chomp;
748351b2a1fSJoe Perches			$camelcase{$_} = 1;
749351b2a1fSJoe Perches		}
750351b2a1fSJoe Perches		close($camelcase_file);
751351b2a1fSJoe Perches
752351b2a1fSJoe Perches		return;
753351b2a1fSJoe Perches	}
754c707a81dSJoe Perches
7553645e328SRichard Genoud	if (-e ".git") {
756c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
757c707a81dSJoe Perches		@include_files = split('\n', $files);
7583445686aSJoe Perches	}
759c707a81dSJoe Perches
7603445686aSJoe Perches	foreach my $file (@include_files) {
7613445686aSJoe Perches		seed_camelcase_file($file);
7623445686aSJoe Perches	}
763351b2a1fSJoe Perches
764c707a81dSJoe Perches	if ($camelcase_cache ne "") {
765351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
766c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
767c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
768351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
769351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
770351b2a1fSJoe Perches		}
771351b2a1fSJoe Perches		close($camelcase_file);
772351b2a1fSJoe Perches	}
7733445686aSJoe Perches}
7743445686aSJoe Perches
775d311cd44SJoe Perchessub git_commit_info {
776d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
777d311cd44SJoe Perches
778d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
779d311cd44SJoe Perches
780d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
781d311cd44SJoe Perches	$output =~ s/^\s*//gm;
782d311cd44SJoe Perches	my @lines = split("\n", $output);
783d311cd44SJoe Perches
7840d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
7850d7835fcSJoe Perches
786d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
787d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
788d311cd44SJoe Perches# all matching commit ids, but it's very slow...
789d311cd44SJoe Perches#
790d311cd44SJoe Perches#		echo "checking commits $1..."
791d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
792d311cd44SJoe Perches#		while read line ; do
793d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
794d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
795d311cd44SJoe Perches#		done
796d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
797d311cd44SJoe Perches	} else {
798d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
799d311cd44SJoe Perches		$desc = substr($lines[0], 41);
800d311cd44SJoe Perches	}
801d311cd44SJoe Perches
802d311cd44SJoe Perches	return ($id, $desc);
803d311cd44SJoe Perches}
804d311cd44SJoe Perches
8056c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
8060a920b5bSAndy Whitcroft
80700df344fSAndy Whitcroftmy @rawlines = ();
808c2fdda0dSAndy Whitcroftmy @lines = ();
8093705ce5bSJoe Perchesmy @fixed = ();
810d752fcc8SJoe Perchesmy @fixed_inserted = ();
811d752fcc8SJoe Perchesmy @fixed_deleted = ();
812194f66fcSJoe Perchesmy $fixlinenr = -1;
813194f66fcSJoe Perches
8144a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
8154a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
8164a593c34SDu, Changbindie "$P: No git repository found\n" if ($git && !-e ".git");
8174a593c34SDu, Changbin
8184a593c34SDu, Changbinif ($git) {
8194a593c34SDu, Changbin	my @commits = ();
8200dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
8214a593c34SDu, Changbin		my $git_range;
82228898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
82328898fd1SJoe Perches			$git_range = "-$2 $1";
8244a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
8254a593c34SDu, Changbin			$git_range = "$commit_expr";
8264a593c34SDu, Changbin		} else {
8270dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
8280dea9f1eSJoe Perches		}
8290dea9f1eSJoe Perches		my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
8300dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
83128898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
83228898fd1SJoe Perches			next if (!defined($1) || !defined($2));
8330dea9f1eSJoe Perches			my $sha1 = $1;
8340dea9f1eSJoe Perches			my $subject = $2;
8350dea9f1eSJoe Perches			unshift(@commits, $sha1);
8360dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
8374a593c34SDu, Changbin		}
8384a593c34SDu, Changbin	}
8394a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
8404a593c34SDu, Changbin	@ARGV = @commits;
8414a593c34SDu, Changbin}
8424a593c34SDu, Changbin
843c2fdda0dSAndy Whitcroftmy $vname;
8446c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
84521caa13cSAndy Whitcroft	my $FILE;
8464a593c34SDu, Changbin	if ($git) {
8474a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
8484a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
8494a593c34SDu, Changbin	} elsif ($file) {
85021caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
8516c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
85221caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
85321caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
8546c72ffaaSAndy Whitcroft	} else {
85521caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
8566c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
8576c72ffaaSAndy Whitcroft	}
858c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
859c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
8604a593c34SDu, Changbin	} elsif ($git) {
8610dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
862c2fdda0dSAndy Whitcroft	} else {
863c2fdda0dSAndy Whitcroft		$vname = $filename;
864c2fdda0dSAndy Whitcroft	}
86521caa13cSAndy Whitcroft	while (<$FILE>) {
8660a920b5bSAndy Whitcroft		chomp;
86700df344fSAndy Whitcroft		push(@rawlines, $_);
8686c72ffaaSAndy Whitcroft	}
86921caa13cSAndy Whitcroft	close($FILE);
870d8469f16SJoe Perches
871d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
872d8469f16SJoe Perches		print '-' x length($vname) . "\n";
873d8469f16SJoe Perches		print "$vname\n";
874d8469f16SJoe Perches		print '-' x length($vname) . "\n";
875d8469f16SJoe Perches	}
876d8469f16SJoe Perches
877c2fdda0dSAndy Whitcroft	if (!process($filename)) {
8780a920b5bSAndy Whitcroft		$exit = 1;
8790a920b5bSAndy Whitcroft	}
88000df344fSAndy Whitcroft	@rawlines = ();
88113214adfSAndy Whitcroft	@lines = ();
8823705ce5bSJoe Perches	@fixed = ();
883d752fcc8SJoe Perches	@fixed_inserted = ();
884d752fcc8SJoe Perches	@fixed_deleted = ();
885194f66fcSJoe Perches	$fixlinenr = -1;
886485ff23eSAlex Dowad	@modifierListFile = ();
887485ff23eSAlex Dowad	@typeListFile = ();
888485ff23eSAlex Dowad	build_types();
8890a920b5bSAndy Whitcroft}
8900a920b5bSAndy Whitcroft
891d8469f16SJoe Perchesif (!$quiet) {
8923c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
8933c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
8943c816e49SJoe Perches
895d8469f16SJoe Perches	if ($^V lt 5.10.0) {
896d8469f16SJoe Perches		print << "EOM"
897d8469f16SJoe Perches
898d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
899d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
900d8469f16SJoe PerchesEOM
901d8469f16SJoe Perches	}
902d8469f16SJoe Perches	if ($exit) {
903d8469f16SJoe Perches		print << "EOM"
904d8469f16SJoe Perches
905d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
906d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
907d8469f16SJoe PerchesEOM
908d8469f16SJoe Perches	}
909d8469f16SJoe Perches}
910d8469f16SJoe Perches
9110a920b5bSAndy Whitcroftexit($exit);
9120a920b5bSAndy Whitcroft
9130a920b5bSAndy Whitcroftsub top_of_kernel_tree {
9146c72ffaaSAndy Whitcroft	my ($root) = @_;
9156c72ffaaSAndy Whitcroft
9166c72ffaaSAndy Whitcroft	my @tree_check = (
9176c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
9186c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
9196c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
9206c72ffaaSAndy Whitcroft	);
9216c72ffaaSAndy Whitcroft
9226c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
9236c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
9240a920b5bSAndy Whitcroft			return 0;
9250a920b5bSAndy Whitcroft		}
9266c72ffaaSAndy Whitcroft	}
9276c72ffaaSAndy Whitcroft	return 1;
9286c72ffaaSAndy Whitcroft}
9290a920b5bSAndy Whitcroft
93020112475SJoe Perchessub parse_email {
93120112475SJoe Perches	my ($formatted_email) = @_;
93220112475SJoe Perches
93320112475SJoe Perches	my $name = "";
93420112475SJoe Perches	my $address = "";
93520112475SJoe Perches	my $comment = "";
93620112475SJoe Perches
93720112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
93820112475SJoe Perches		$name = $1;
93920112475SJoe Perches		$address = $2;
94020112475SJoe Perches		$comment = $3 if defined $3;
94120112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
94220112475SJoe Perches		$address = $1;
94320112475SJoe Perches		$comment = $2 if defined $2;
94420112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
94520112475SJoe Perches		$address = $1;
94620112475SJoe Perches		$comment = $2 if defined $2;
94720112475SJoe Perches		$formatted_email =~ s/$address.*$//;
94820112475SJoe Perches		$name = $formatted_email;
9493705ce5bSJoe Perches		$name = trim($name);
95020112475SJoe Perches		$name =~ s/^\"|\"$//g;
95120112475SJoe Perches		# If there's a name left after stripping spaces and
95220112475SJoe Perches		# leading quotes, and the address doesn't have both
95320112475SJoe Perches		# leading and trailing angle brackets, the address
95420112475SJoe Perches		# is invalid. ie:
95520112475SJoe Perches		#   "joe smith [email protected]" bad
95620112475SJoe Perches		#   "joe smith <[email protected]" bad
95720112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
95820112475SJoe Perches			$name = "";
95920112475SJoe Perches			$address = "";
96020112475SJoe Perches			$comment = "";
96120112475SJoe Perches		}
96220112475SJoe Perches	}
96320112475SJoe Perches
9643705ce5bSJoe Perches	$name = trim($name);
96520112475SJoe Perches	$name =~ s/^\"|\"$//g;
9663705ce5bSJoe Perches	$address = trim($address);
96720112475SJoe Perches	$address =~ s/^\<|\>$//g;
96820112475SJoe Perches
96920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
97020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
97120112475SJoe Perches		$name = "\"$name\"";
97220112475SJoe Perches	}
97320112475SJoe Perches
97420112475SJoe Perches	return ($name, $address, $comment);
97520112475SJoe Perches}
97620112475SJoe Perches
97720112475SJoe Perchessub format_email {
97820112475SJoe Perches	my ($name, $address) = @_;
97920112475SJoe Perches
98020112475SJoe Perches	my $formatted_email;
98120112475SJoe Perches
9823705ce5bSJoe Perches	$name = trim($name);
98320112475SJoe Perches	$name =~ s/^\"|\"$//g;
9843705ce5bSJoe Perches	$address = trim($address);
98520112475SJoe Perches
98620112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
98720112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
98820112475SJoe Perches		$name = "\"$name\"";
98920112475SJoe Perches	}
99020112475SJoe Perches
99120112475SJoe Perches	if ("$name" eq "") {
99220112475SJoe Perches		$formatted_email = "$address";
99320112475SJoe Perches	} else {
99420112475SJoe Perches		$formatted_email = "$name <$address>";
99520112475SJoe Perches	}
99620112475SJoe Perches
99720112475SJoe Perches	return $formatted_email;
99820112475SJoe Perches}
99920112475SJoe Perches
1000d311cd44SJoe Perchessub which {
1001d311cd44SJoe Perches	my ($bin) = @_;
1002d311cd44SJoe Perches
1003d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1004d311cd44SJoe Perches		if (-e "$path/$bin") {
1005d311cd44SJoe Perches			return "$path/$bin";
1006d311cd44SJoe Perches		}
1007d311cd44SJoe Perches	}
1008d311cd44SJoe Perches
1009d311cd44SJoe Perches	return "";
1010d311cd44SJoe Perches}
1011d311cd44SJoe Perches
1012000d1cc1SJoe Perchessub which_conf {
1013000d1cc1SJoe Perches	my ($conf) = @_;
1014000d1cc1SJoe Perches
1015000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1016000d1cc1SJoe Perches		if (-e "$path/$conf") {
1017000d1cc1SJoe Perches			return "$path/$conf";
1018000d1cc1SJoe Perches		}
1019000d1cc1SJoe Perches	}
1020000d1cc1SJoe Perches
1021000d1cc1SJoe Perches	return "";
1022000d1cc1SJoe Perches}
1023000d1cc1SJoe Perches
10240a920b5bSAndy Whitcroftsub expand_tabs {
10250a920b5bSAndy Whitcroft	my ($str) = @_;
10260a920b5bSAndy Whitcroft
10270a920b5bSAndy Whitcroft	my $res = '';
10280a920b5bSAndy Whitcroft	my $n = 0;
10290a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
10300a920b5bSAndy Whitcroft		if ($c eq "\t") {
10310a920b5bSAndy Whitcroft			$res .= ' ';
10320a920b5bSAndy Whitcroft			$n++;
10330a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
10340a920b5bSAndy Whitcroft				$res .= ' ';
10350a920b5bSAndy Whitcroft			}
10360a920b5bSAndy Whitcroft			next;
10370a920b5bSAndy Whitcroft		}
10380a920b5bSAndy Whitcroft		$res .= $c;
10390a920b5bSAndy Whitcroft		$n++;
10400a920b5bSAndy Whitcroft	}
10410a920b5bSAndy Whitcroft
10420a920b5bSAndy Whitcroft	return $res;
10430a920b5bSAndy Whitcroft}
10446c72ffaaSAndy Whitcroftsub copy_spacing {
1045773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
10466c72ffaaSAndy Whitcroft	return $res;
10476c72ffaaSAndy Whitcroft}
10480a920b5bSAndy Whitcroft
10494a0df2efSAndy Whitcroftsub line_stats {
10504a0df2efSAndy Whitcroft	my ($line) = @_;
10514a0df2efSAndy Whitcroft
10524a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
10534a0df2efSAndy Whitcroft	$line =~ s/^.//;
10544a0df2efSAndy Whitcroft	$line = expand_tabs($line);
10554a0df2efSAndy Whitcroft
10564a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
10574a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
10584a0df2efSAndy Whitcroft
10594a0df2efSAndy Whitcroft	return (length($line), length($white));
10604a0df2efSAndy Whitcroft}
10614a0df2efSAndy Whitcroft
1062773647a0SAndy Whitcroftmy $sanitise_quote = '';
1063773647a0SAndy Whitcroft
1064773647a0SAndy Whitcroftsub sanitise_line_reset {
1065773647a0SAndy Whitcroft	my ($in_comment) = @_;
1066773647a0SAndy Whitcroft
1067773647a0SAndy Whitcroft	if ($in_comment) {
1068773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1069773647a0SAndy Whitcroft	} else {
1070773647a0SAndy Whitcroft		$sanitise_quote = '';
1071773647a0SAndy Whitcroft	}
1072773647a0SAndy Whitcroft}
107300df344fSAndy Whitcroftsub sanitise_line {
107400df344fSAndy Whitcroft	my ($line) = @_;
107500df344fSAndy Whitcroft
107600df344fSAndy Whitcroft	my $res = '';
107700df344fSAndy Whitcroft	my $l = '';
107800df344fSAndy Whitcroft
1079c2fdda0dSAndy Whitcroft	my $qlen = 0;
1080773647a0SAndy Whitcroft	my $off = 0;
1081773647a0SAndy Whitcroft	my $c;
108200df344fSAndy Whitcroft
1083773647a0SAndy Whitcroft	# Always copy over the diff marker.
1084773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1085773647a0SAndy Whitcroft
1086773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1087773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1088773647a0SAndy Whitcroft
1089773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
1090773647a0SAndy Whitcroft		# and end, all to $;.
1091773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1092773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1093773647a0SAndy Whitcroft
1094773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1095773647a0SAndy Whitcroft			$off++;
109600df344fSAndy Whitcroft			next;
1097773647a0SAndy Whitcroft		}
109881bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1099773647a0SAndy Whitcroft			$sanitise_quote = '';
1100773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1101773647a0SAndy Whitcroft			$off++;
1102773647a0SAndy Whitcroft			next;
1103773647a0SAndy Whitcroft		}
1104113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1105113f04a8SDaniel Walker			$sanitise_quote = '//';
1106113f04a8SDaniel Walker
1107113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1108113f04a8SDaniel Walker			$off++;
1109113f04a8SDaniel Walker			next;
1110113f04a8SDaniel Walker		}
1111773647a0SAndy Whitcroft
1112773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1113773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1114773647a0SAndy Whitcroft		    $c eq "\\") {
1115773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1116773647a0SAndy Whitcroft			$off++;
1117773647a0SAndy Whitcroft			next;
1118773647a0SAndy Whitcroft		}
1119773647a0SAndy Whitcroft		# Regular quotes.
1120773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1121773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1122773647a0SAndy Whitcroft				$sanitise_quote = $c;
1123773647a0SAndy Whitcroft
1124773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1125773647a0SAndy Whitcroft				next;
1126773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1127773647a0SAndy Whitcroft				$sanitise_quote = '';
112800df344fSAndy Whitcroft			}
112900df344fSAndy Whitcroft		}
1130773647a0SAndy Whitcroft
1131fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1132773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1133773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1134113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1135113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1136773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1137773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
113800df344fSAndy Whitcroft		} else {
1139773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
114000df344fSAndy Whitcroft		}
1141c2fdda0dSAndy Whitcroft	}
1142c2fdda0dSAndy Whitcroft
1143113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1144113f04a8SDaniel Walker		$sanitise_quote = '';
1145113f04a8SDaniel Walker	}
1146113f04a8SDaniel Walker
1147c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1148c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1149c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1150c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1151c2fdda0dSAndy Whitcroft
1152c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1153c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1154c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1155c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1156c2fdda0dSAndy Whitcroft	}
1157c2fdda0dSAndy Whitcroft
1158dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1159dadf680dSJoe Perches		my $match = $1;
1160dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1161dadf680dSJoe Perches	}
1162dadf680dSJoe Perches
116300df344fSAndy Whitcroft	return $res;
116400df344fSAndy Whitcroft}
116500df344fSAndy Whitcroft
1166a6962d72SJoe Perchessub get_quoted_string {
1167a6962d72SJoe Perches	my ($line, $rawline) = @_;
1168a6962d72SJoe Perches
116933acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1170a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1171a6962d72SJoe Perches}
1172a6962d72SJoe Perches
11738905a67cSAndy Whitcroftsub ctx_statement_block {
11748905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
11758905a67cSAndy Whitcroft	my $line = $linenr - 1;
11768905a67cSAndy Whitcroft	my $blk = '';
11778905a67cSAndy Whitcroft	my $soff = $off;
11788905a67cSAndy Whitcroft	my $coff = $off - 1;
1179773647a0SAndy Whitcroft	my $coff_set = 0;
11808905a67cSAndy Whitcroft
118113214adfSAndy Whitcroft	my $loff = 0;
118213214adfSAndy Whitcroft
11838905a67cSAndy Whitcroft	my $type = '';
11848905a67cSAndy Whitcroft	my $level = 0;
1185a2750645SAndy Whitcroft	my @stack = ();
1186cf655043SAndy Whitcroft	my $p;
11878905a67cSAndy Whitcroft	my $c;
11888905a67cSAndy Whitcroft	my $len = 0;
118913214adfSAndy Whitcroft
119013214adfSAndy Whitcroft	my $remainder;
11918905a67cSAndy Whitcroft	while (1) {
1192a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1193a2750645SAndy Whitcroft
1194773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
11958905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
11968905a67cSAndy Whitcroft		# context.
11978905a67cSAndy Whitcroft		if ($off >= $len) {
11988905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1199dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1200c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
12018905a67cSAndy Whitcroft				$remain--;
120213214adfSAndy Whitcroft				$loff = $len;
1203c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
12048905a67cSAndy Whitcroft				$len = length($blk);
12058905a67cSAndy Whitcroft				$line++;
12068905a67cSAndy Whitcroft				last;
12078905a67cSAndy Whitcroft			}
12088905a67cSAndy Whitcroft			# Bail if there is no further context.
12098905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
121013214adfSAndy Whitcroft			if ($off >= $len) {
12118905a67cSAndy Whitcroft				last;
12128905a67cSAndy Whitcroft			}
1213f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1214f74bd194SAndy Whitcroft				$level++;
1215f74bd194SAndy Whitcroft				$type = '#';
1216f74bd194SAndy Whitcroft			}
12178905a67cSAndy Whitcroft		}
1218cf655043SAndy Whitcroft		$p = $c;
12198905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
122013214adfSAndy Whitcroft		$remainder = substr($blk, $off);
12218905a67cSAndy Whitcroft
1222773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
12234635f4fbSAndy Whitcroft
12244635f4fbSAndy Whitcroft		# Handle nested #if/#else.
12254635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
12264635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
12274635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
12284635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
12294635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
12304635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
12314635f4fbSAndy Whitcroft		}
12324635f4fbSAndy Whitcroft
12338905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
12348905a67cSAndy Whitcroft		# outermost level.
12358905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
12368905a67cSAndy Whitcroft			last;
12378905a67cSAndy Whitcroft		}
12388905a67cSAndy Whitcroft
123913214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1240773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1241773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1242773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1243773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1244773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1245773647a0SAndy Whitcroft			$coff_set = 1;
1246773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1247773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
124813214adfSAndy Whitcroft		}
124913214adfSAndy Whitcroft
12508905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
12518905a67cSAndy Whitcroft			$level++;
12528905a67cSAndy Whitcroft			$type = '(';
12538905a67cSAndy Whitcroft		}
12548905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
12558905a67cSAndy Whitcroft			$level--;
12568905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
12578905a67cSAndy Whitcroft
12588905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
12598905a67cSAndy Whitcroft				$coff = $off;
1260773647a0SAndy Whitcroft				$coff_set = 1;
1261773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
12628905a67cSAndy Whitcroft			}
12638905a67cSAndy Whitcroft		}
12648905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
12658905a67cSAndy Whitcroft			$level++;
12668905a67cSAndy Whitcroft			$type = '{';
12678905a67cSAndy Whitcroft		}
12688905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
12698905a67cSAndy Whitcroft			$level--;
12708905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
12718905a67cSAndy Whitcroft
12728905a67cSAndy Whitcroft			if ($level == 0) {
1273b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1274b998e001SPatrick Pannuto					$off++;
1275b998e001SPatrick Pannuto				}
12768905a67cSAndy Whitcroft				last;
12778905a67cSAndy Whitcroft			}
12788905a67cSAndy Whitcroft		}
1279f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1280f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1281f74bd194SAndy Whitcroft			$level--;
1282f74bd194SAndy Whitcroft			$type = '';
1283f74bd194SAndy Whitcroft			$off++;
1284f74bd194SAndy Whitcroft			last;
1285f74bd194SAndy Whitcroft		}
12868905a67cSAndy Whitcroft		$off++;
12878905a67cSAndy Whitcroft	}
1288a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
128913214adfSAndy Whitcroft	if ($off == $len) {
1290a3bb97a7SAndy Whitcroft		$loff = $len + 1;
129113214adfSAndy Whitcroft		$line++;
129213214adfSAndy Whitcroft		$remain--;
129313214adfSAndy Whitcroft	}
12948905a67cSAndy Whitcroft
12958905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
12968905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
12978905a67cSAndy Whitcroft
12988905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
12998905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
13008905a67cSAndy Whitcroft
1301773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
130213214adfSAndy Whitcroft
130313214adfSAndy Whitcroft	return ($statement, $condition,
130413214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
130513214adfSAndy Whitcroft}
130613214adfSAndy Whitcroft
1307cf655043SAndy Whitcroftsub statement_lines {
1308cf655043SAndy Whitcroft	my ($stmt) = @_;
1309cf655043SAndy Whitcroft
1310cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1311cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1312cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1313cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1314cf655043SAndy Whitcroft
1315cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1316cf655043SAndy Whitcroft
1317cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1318cf655043SAndy Whitcroft}
1319cf655043SAndy Whitcroft
1320cf655043SAndy Whitcroftsub statement_rawlines {
1321cf655043SAndy Whitcroft	my ($stmt) = @_;
1322cf655043SAndy Whitcroft
1323cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1324cf655043SAndy Whitcroft
1325cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1326cf655043SAndy Whitcroft}
1327cf655043SAndy Whitcroft
1328cf655043SAndy Whitcroftsub statement_block_size {
1329cf655043SAndy Whitcroft	my ($stmt) = @_;
1330cf655043SAndy Whitcroft
1331cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1332cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1333cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1334cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1335cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1336cf655043SAndy Whitcroft
1337cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1338cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1339cf655043SAndy Whitcroft
1340cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1341cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1342cf655043SAndy Whitcroft
1343cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1344cf655043SAndy Whitcroft		return $stmt_lines;
1345cf655043SAndy Whitcroft	} else {
1346cf655043SAndy Whitcroft		return $stmt_statements;
1347cf655043SAndy Whitcroft	}
1348cf655043SAndy Whitcroft}
1349cf655043SAndy Whitcroft
135013214adfSAndy Whitcroftsub ctx_statement_full {
135113214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
135213214adfSAndy Whitcroft	my ($statement, $condition, $level);
135313214adfSAndy Whitcroft
135413214adfSAndy Whitcroft	my (@chunks);
135513214adfSAndy Whitcroft
1356cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
135713214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
135813214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1359773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
136013214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1361cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1362cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1363cf655043SAndy Whitcroft	}
1364cf655043SAndy Whitcroft
1365cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1366cf655043SAndy Whitcroft	# could continue the statement.
1367cf655043SAndy Whitcroft	for (;;) {
136813214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
136913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1370cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1371773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1372cf655043SAndy Whitcroft		#print "C: push\n";
1373cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
137413214adfSAndy Whitcroft	}
137513214adfSAndy Whitcroft
137613214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
13778905a67cSAndy Whitcroft}
13788905a67cSAndy Whitcroft
13794a0df2efSAndy Whitcroftsub ctx_block_get {
1380f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
13814a0df2efSAndy Whitcroft	my $line;
13824a0df2efSAndy Whitcroft	my $start = $linenr - 1;
13834a0df2efSAndy Whitcroft	my $blk = '';
13844a0df2efSAndy Whitcroft	my @o;
13854a0df2efSAndy Whitcroft	my @c;
13864a0df2efSAndy Whitcroft	my @res = ();
13874a0df2efSAndy Whitcroft
1388f0a594c1SAndy Whitcroft	my $level = 0;
13894635f4fbSAndy Whitcroft	my @stack = ($level);
139000df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
139100df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
139200df344fSAndy Whitcroft		$remain--;
139300df344fSAndy Whitcroft
139400df344fSAndy Whitcroft		$blk .= $rawlines[$line];
13954635f4fbSAndy Whitcroft
13964635f4fbSAndy Whitcroft		# Handle nested #if/#else.
139701464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
13984635f4fbSAndy Whitcroft			push(@stack, $level);
139901464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
14004635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
140101464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
14024635f4fbSAndy Whitcroft			$level = pop(@stack);
14034635f4fbSAndy Whitcroft		}
14044635f4fbSAndy Whitcroft
140501464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1406f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1407f0a594c1SAndy Whitcroft			if ($off > 0) {
1408f0a594c1SAndy Whitcroft				$off--;
1409f0a594c1SAndy Whitcroft				next;
1410f0a594c1SAndy Whitcroft			}
14114a0df2efSAndy Whitcroft
1412f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1413f0a594c1SAndy Whitcroft				$level--;
1414f0a594c1SAndy Whitcroft				last if ($level == 0);
1415f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1416f0a594c1SAndy Whitcroft				$level++;
1417f0a594c1SAndy Whitcroft			}
1418f0a594c1SAndy Whitcroft		}
14194a0df2efSAndy Whitcroft
1420f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
142100df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
14224a0df2efSAndy Whitcroft		}
14234a0df2efSAndy Whitcroft
1424f0a594c1SAndy Whitcroft		last if ($level == 0);
14254a0df2efSAndy Whitcroft	}
14264a0df2efSAndy Whitcroft
1427f0a594c1SAndy Whitcroft	return ($level, @res);
14284a0df2efSAndy Whitcroft}
14294a0df2efSAndy Whitcroftsub ctx_block_outer {
14304a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
14314a0df2efSAndy Whitcroft
1432f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1433f0a594c1SAndy Whitcroft	return @r;
14344a0df2efSAndy Whitcroft}
14354a0df2efSAndy Whitcroftsub ctx_block {
14364a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
14374a0df2efSAndy Whitcroft
1438f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1439f0a594c1SAndy Whitcroft	return @r;
1440653d4876SAndy Whitcroft}
1441653d4876SAndy Whitcroftsub ctx_statement {
1442f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1443f0a594c1SAndy Whitcroft
1444f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1445f0a594c1SAndy Whitcroft	return @r;
1446f0a594c1SAndy Whitcroft}
1447f0a594c1SAndy Whitcroftsub ctx_block_level {
1448653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1449653d4876SAndy Whitcroft
1450f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
14514a0df2efSAndy Whitcroft}
14529c0ca6f9SAndy Whitcroftsub ctx_statement_level {
14539c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
14549c0ca6f9SAndy Whitcroft
14559c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
14569c0ca6f9SAndy Whitcroft}
14574a0df2efSAndy Whitcroft
14584a0df2efSAndy Whitcroftsub ctx_locate_comment {
14594a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
14604a0df2efSAndy Whitcroft
14614a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1462beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
14634a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
14644a0df2efSAndy Whitcroft
14654a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
14664a0df2efSAndy Whitcroft	# comment.
14674a0df2efSAndy Whitcroft	my $in_comment = 0;
14684a0df2efSAndy Whitcroft	$current_comment = '';
14694a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
147000df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
147100df344fSAndy Whitcroft		#warn "           $line\n";
14724a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
14734a0df2efSAndy Whitcroft			$in_comment = 1;
14744a0df2efSAndy Whitcroft		}
14754a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
14764a0df2efSAndy Whitcroft			$in_comment = 1;
14774a0df2efSAndy Whitcroft		}
14784a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
14794a0df2efSAndy Whitcroft			$current_comment = '';
14804a0df2efSAndy Whitcroft		}
14814a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
14824a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
14834a0df2efSAndy Whitcroft			$in_comment = 0;
14844a0df2efSAndy Whitcroft		}
14854a0df2efSAndy Whitcroft	}
14864a0df2efSAndy Whitcroft
14874a0df2efSAndy Whitcroft	chomp($current_comment);
14884a0df2efSAndy Whitcroft	return($current_comment);
14894a0df2efSAndy Whitcroft}
14904a0df2efSAndy Whitcroftsub ctx_has_comment {
14914a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
14924a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
14934a0df2efSAndy Whitcroft
149400df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
14954a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
14964a0df2efSAndy Whitcroft
14974a0df2efSAndy Whitcroft	return ($cmt ne '');
14984a0df2efSAndy Whitcroft}
14994a0df2efSAndy Whitcroft
15004d001e4dSAndy Whitcroftsub raw_line {
15014d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
15024d001e4dSAndy Whitcroft
15034d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
15044d001e4dSAndy Whitcroft	$cnt++;
15054d001e4dSAndy Whitcroft
15064d001e4dSAndy Whitcroft	my $line;
15074d001e4dSAndy Whitcroft	while ($cnt) {
15084d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
15094d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
15104d001e4dSAndy Whitcroft		$cnt--;
15114d001e4dSAndy Whitcroft	}
15124d001e4dSAndy Whitcroft
15134d001e4dSAndy Whitcroft	return $line;
15144d001e4dSAndy Whitcroft}
15154d001e4dSAndy Whitcroft
15160a920b5bSAndy Whitcroftsub cat_vet {
15170a920b5bSAndy Whitcroft	my ($vet) = @_;
15189c0ca6f9SAndy Whitcroft	my ($res, $coded);
15190a920b5bSAndy Whitcroft
15209c0ca6f9SAndy Whitcroft	$res = '';
15216c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
15226c72ffaaSAndy Whitcroft		$res .= $1;
15236c72ffaaSAndy Whitcroft		if ($2 ne '') {
15249c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
15256c72ffaaSAndy Whitcroft			$res .= $coded;
15266c72ffaaSAndy Whitcroft		}
15279c0ca6f9SAndy Whitcroft	}
15289c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
15290a920b5bSAndy Whitcroft
15309c0ca6f9SAndy Whitcroft	return $res;
15310a920b5bSAndy Whitcroft}
15320a920b5bSAndy Whitcroft
1533c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1534cf655043SAndy Whitcroftmy $av_pending;
1535c2fdda0dSAndy Whitcroftmy @av_paren_type;
15361f65f947SAndy Whitcroftmy $av_pend_colon;
1537c2fdda0dSAndy Whitcroft
1538c2fdda0dSAndy Whitcroftsub annotate_reset {
1539c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1540cf655043SAndy Whitcroft	$av_pending = '_';
1541cf655043SAndy Whitcroft	@av_paren_type = ('E');
15421f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1543c2fdda0dSAndy Whitcroft}
1544c2fdda0dSAndy Whitcroft
15456c72ffaaSAndy Whitcroftsub annotate_values {
15466c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
15476c72ffaaSAndy Whitcroft
15486c72ffaaSAndy Whitcroft	my $res;
15491f65f947SAndy Whitcroft	my $var = '_' x length($stream);
15506c72ffaaSAndy Whitcroft	my $cur = $stream;
15516c72ffaaSAndy Whitcroft
1552c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
15536c72ffaaSAndy Whitcroft
15546c72ffaaSAndy Whitcroft	while (length($cur)) {
1555773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1556cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1557171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
15586c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1559c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1560c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1561cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1562c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
15636c72ffaaSAndy Whitcroft			}
15646c72ffaaSAndy Whitcroft
1565c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
15669446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
15679446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1568addcdceaSAndy Whitcroft			$type = 'c';
15699446ef56SAndy Whitcroft
1570e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1571c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
15726c72ffaaSAndy Whitcroft			$type = 'T';
15736c72ffaaSAndy Whitcroft
1574389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1575389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1576389a2fe5SAndy Whitcroft			$type = 'T';
1577389a2fe5SAndy Whitcroft
1578c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1579171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1580c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1581171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1582171ae1a4SAndy Whitcroft			if ($2 ne '') {
1583cf655043SAndy Whitcroft				$av_pending = 'N';
1584171ae1a4SAndy Whitcroft			}
1585171ae1a4SAndy Whitcroft			$type = 'E';
1586171ae1a4SAndy Whitcroft
1587c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1588171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1589171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1590171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
15916c72ffaaSAndy Whitcroft
1592c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1593cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1594c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1595cf655043SAndy Whitcroft
1596cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1597cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1598171ae1a4SAndy Whitcroft			$type = 'E';
1599cf655043SAndy Whitcroft
1600c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1601cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1602cf655043SAndy Whitcroft			$av_preprocessor = 1;
1603cf655043SAndy Whitcroft
1604cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1605cf655043SAndy Whitcroft
1606171ae1a4SAndy Whitcroft			$type = 'E';
1607cf655043SAndy Whitcroft
1608c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1609cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1610cf655043SAndy Whitcroft
1611cf655043SAndy Whitcroft			$av_preprocessor = 1;
1612cf655043SAndy Whitcroft
1613cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1614cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1615cf655043SAndy Whitcroft			pop(@av_paren_type);
1616cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1617171ae1a4SAndy Whitcroft			$type = 'E';
16186c72ffaaSAndy Whitcroft
16196c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1620c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
16216c72ffaaSAndy Whitcroft
1622171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1623171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1624171ae1a4SAndy Whitcroft			$av_pending = $type;
1625171ae1a4SAndy Whitcroft			$type = 'N';
1626171ae1a4SAndy Whitcroft
16276c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1628c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
16296c72ffaaSAndy Whitcroft			if (defined $2) {
1630cf655043SAndy Whitcroft				$av_pending = 'V';
16316c72ffaaSAndy Whitcroft			}
16326c72ffaaSAndy Whitcroft			$type = 'N';
16336c72ffaaSAndy Whitcroft
163414b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1635c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
163614b111c1SAndy Whitcroft			$av_pending = 'E';
16376c72ffaaSAndy Whitcroft			$type = 'N';
16386c72ffaaSAndy Whitcroft
16391f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
16401f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
16411f65f947SAndy Whitcroft			$av_pend_colon = 'C';
16421f65f947SAndy Whitcroft			$type = 'N';
16431f65f947SAndy Whitcroft
164414b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1645c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
16466c72ffaaSAndy Whitcroft			$type = 'N';
16476c72ffaaSAndy Whitcroft
16486c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1649c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1650cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1651cf655043SAndy Whitcroft			$av_pending = '_';
16526c72ffaaSAndy Whitcroft			$type = 'N';
16536c72ffaaSAndy Whitcroft
16546c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1655cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1656cf655043SAndy Whitcroft			if ($new_type ne '_') {
1657cf655043SAndy Whitcroft				$type = $new_type;
1658c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1659c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
16606c72ffaaSAndy Whitcroft			} else {
1661c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
16626c72ffaaSAndy Whitcroft			}
16636c72ffaaSAndy Whitcroft
1664c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1665c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1666c8cb2ca3SAndy Whitcroft			$type = 'V';
1667cf655043SAndy Whitcroft			$av_pending = 'V';
16686c72ffaaSAndy Whitcroft
16698e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
16708e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
16711f65f947SAndy Whitcroft				$av_pend_colon = 'B';
16728e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
16738e761b04SAndy Whitcroft				$av_pend_colon = 'L';
16741f65f947SAndy Whitcroft			}
16751f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
16761f65f947SAndy Whitcroft			$type = 'V';
16771f65f947SAndy Whitcroft
16786c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1679c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
16806c72ffaaSAndy Whitcroft			$type = 'V';
16816c72ffaaSAndy Whitcroft
16826c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1683c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
16846c72ffaaSAndy Whitcroft			$type = 'N';
16856c72ffaaSAndy Whitcroft
1686cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1687c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
168813214adfSAndy Whitcroft			$type = 'E';
16891f65f947SAndy Whitcroft			$av_pend_colon = 'O';
169013214adfSAndy Whitcroft
16918e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
16928e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
16938e761b04SAndy Whitcroft			$type = 'C';
16948e761b04SAndy Whitcroft
16951f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
16961f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
16971f65f947SAndy Whitcroft			$type = 'N';
16981f65f947SAndy Whitcroft
16991f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
17001f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
17011f65f947SAndy Whitcroft
17021f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
17031f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
17041f65f947SAndy Whitcroft				$type = 'E';
17051f65f947SAndy Whitcroft			} else {
17061f65f947SAndy Whitcroft				$type = 'N';
17071f65f947SAndy Whitcroft			}
17081f65f947SAndy Whitcroft			$av_pend_colon = 'O';
17091f65f947SAndy Whitcroft
17108e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
171113214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
17126c72ffaaSAndy Whitcroft			$type = 'N';
17136c72ffaaSAndy Whitcroft
17140d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
171574048ed8SAndy Whitcroft			my $variant;
171674048ed8SAndy Whitcroft
171774048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
171874048ed8SAndy Whitcroft			if ($type eq 'V') {
171974048ed8SAndy Whitcroft				$variant = 'B';
172074048ed8SAndy Whitcroft			} else {
172174048ed8SAndy Whitcroft				$variant = 'U';
172274048ed8SAndy Whitcroft			}
172374048ed8SAndy Whitcroft
172474048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
172574048ed8SAndy Whitcroft			$type = 'N';
172674048ed8SAndy Whitcroft
17276c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1728c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
17296c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
17306c72ffaaSAndy Whitcroft				$type = 'N';
17316c72ffaaSAndy Whitcroft			}
17326c72ffaaSAndy Whitcroft
17336c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1734c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
17356c72ffaaSAndy Whitcroft		}
17366c72ffaaSAndy Whitcroft		if (defined $1) {
17376c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
17386c72ffaaSAndy Whitcroft			$res .= $type x length($1);
17396c72ffaaSAndy Whitcroft		}
17406c72ffaaSAndy Whitcroft	}
17416c72ffaaSAndy Whitcroft
17421f65f947SAndy Whitcroft	return ($res, $var);
17436c72ffaaSAndy Whitcroft}
17446c72ffaaSAndy Whitcroft
17458905a67cSAndy Whitcroftsub possible {
174613214adfSAndy Whitcroft	my ($possible, $line) = @_;
17479a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
17480776e594SAndy Whitcroft		^(?:
17490776e594SAndy Whitcroft			$Modifier|
17500776e594SAndy Whitcroft			$Storage|
17510776e594SAndy Whitcroft			$Type|
17529a974fdbSAndy Whitcroft			DEFINE_\S+
17539a974fdbSAndy Whitcroft		)$|
17549a974fdbSAndy Whitcroft		^(?:
17550776e594SAndy Whitcroft			goto|
17560776e594SAndy Whitcroft			return|
17570776e594SAndy Whitcroft			case|
17580776e594SAndy Whitcroft			else|
17590776e594SAndy Whitcroft			asm|__asm__|
176089a88353SAndy Whitcroft			do|
176189a88353SAndy Whitcroft			\#|
176289a88353SAndy Whitcroft			\#\#|
17639a974fdbSAndy Whitcroft		)(?:\s|$)|
17640776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
17659a974fdbSAndy Whitcroft	    )}x;
17669a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
17679a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1768c45dcabdSAndy Whitcroft		# Check for modifiers.
1769c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1770c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1771c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1772c45dcabdSAndy Whitcroft
1773c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1774c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1775d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
17769a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1777d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1778485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1779d2506586SAndy Whitcroft				}
17809a974fdbSAndy Whitcroft			}
1781c45dcabdSAndy Whitcroft
1782c45dcabdSAndy Whitcroft		} else {
178313214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1784485ff23eSAlex Dowad			push(@typeListFile, $possible);
1785c45dcabdSAndy Whitcroft		}
17868905a67cSAndy Whitcroft		build_types();
17870776e594SAndy Whitcroft	} else {
17880776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
17898905a67cSAndy Whitcroft	}
17908905a67cSAndy Whitcroft}
17918905a67cSAndy Whitcroft
17926c72ffaaSAndy Whitcroftmy $prefix = '';
17936c72ffaaSAndy Whitcroft
1794000d1cc1SJoe Perchessub show_type {
1795cbec18afSJoe Perches	my ($type) = @_;
179691bfe484SJoe Perches
1797cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1798cbec18afSJoe Perches
1799cbec18afSJoe Perches	return !defined $ignore_type{$type};
1800000d1cc1SJoe Perches}
1801000d1cc1SJoe Perches
1802f0a594c1SAndy Whitcroftsub report {
1803cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1804cbec18afSJoe Perches
1805cbec18afSJoe Perches	if (!show_type($type) ||
1806cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1807773647a0SAndy Whitcroft		return 0;
1808773647a0SAndy Whitcroft	}
180957230297SJoe Perches	my $output = '';
181057230297SJoe Perches	if (-t STDOUT && $color) {
181157230297SJoe Perches		if ($level eq 'ERROR') {
181257230297SJoe Perches			$output .= RED;
181357230297SJoe Perches		} elsif ($level eq 'WARNING') {
181457230297SJoe Perches			$output .= YELLOW;
1815000d1cc1SJoe Perches		} else {
181657230297SJoe Perches			$output .= GREEN;
1817000d1cc1SJoe Perches		}
181857230297SJoe Perches	}
181957230297SJoe Perches	$output .= $prefix . $level . ':';
182057230297SJoe Perches	if ($show_types) {
182157230297SJoe Perches		$output .= BLUE if (-t STDOUT && $color);
182257230297SJoe Perches		$output .= "$type:";
182357230297SJoe Perches	}
182457230297SJoe Perches	$output .= RESET if (-t STDOUT && $color);
182557230297SJoe Perches	$output .= ' ' . $msg . "\n";
182634d8815fSJoe Perches
182734d8815fSJoe Perches	if ($showfile) {
182834d8815fSJoe Perches		my @lines = split("\n", $output, -1);
182934d8815fSJoe Perches		splice(@lines, 1, 1);
183034d8815fSJoe Perches		$output = join("\n", @lines);
183134d8815fSJoe Perches	}
183257230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
18338905a67cSAndy Whitcroft
183457230297SJoe Perches	push(our @report, $output);
1835773647a0SAndy Whitcroft
1836773647a0SAndy Whitcroft	return 1;
1837f0a594c1SAndy Whitcroft}
1838cbec18afSJoe Perches
1839f0a594c1SAndy Whitcroftsub report_dump {
184013214adfSAndy Whitcroft	our @report;
1841f0a594c1SAndy Whitcroft}
1842000d1cc1SJoe Perches
1843d752fcc8SJoe Perchessub fixup_current_range {
1844d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1845d752fcc8SJoe Perches
1846d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1847d752fcc8SJoe Perches		my $o = $1;
1848d752fcc8SJoe Perches		my $l = $2;
1849d752fcc8SJoe Perches		my $no = $o + $offset;
1850d752fcc8SJoe Perches		my $nl = $l + $length;
1851d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1852d752fcc8SJoe Perches	}
1853d752fcc8SJoe Perches}
1854d752fcc8SJoe Perches
1855d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1856d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1857d752fcc8SJoe Perches
1858d752fcc8SJoe Perches	my $range_last_linenr = 0;
1859d752fcc8SJoe Perches	my $delta_offset = 0;
1860d752fcc8SJoe Perches
1861d752fcc8SJoe Perches	my $old_linenr = 0;
1862d752fcc8SJoe Perches	my $new_linenr = 0;
1863d752fcc8SJoe Perches
1864d752fcc8SJoe Perches	my $next_insert = 0;
1865d752fcc8SJoe Perches	my $next_delete = 0;
1866d752fcc8SJoe Perches
1867d752fcc8SJoe Perches	my @lines = ();
1868d752fcc8SJoe Perches
1869d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1870d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1871d752fcc8SJoe Perches
1872d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1873d752fcc8SJoe Perches		my $save_line = 1;
1874d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1875323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1876d752fcc8SJoe Perches			$delta_offset = 0;
1877d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1878d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1879d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1880d752fcc8SJoe Perches		}
1881d752fcc8SJoe Perches
1882d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1883d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1884d752fcc8SJoe Perches			$save_line = 0;
1885d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1886d752fcc8SJoe Perches		}
1887d752fcc8SJoe Perches
1888d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1889d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1890d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1891d752fcc8SJoe Perches			$new_linenr++;
1892d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1893d752fcc8SJoe Perches		}
1894d752fcc8SJoe Perches
1895d752fcc8SJoe Perches		if ($save_line) {
1896d752fcc8SJoe Perches			push(@lines, $line);
1897d752fcc8SJoe Perches			$new_linenr++;
1898d752fcc8SJoe Perches		}
1899d752fcc8SJoe Perches
1900d752fcc8SJoe Perches		$old_linenr++;
1901d752fcc8SJoe Perches	}
1902d752fcc8SJoe Perches
1903d752fcc8SJoe Perches	return @lines;
1904d752fcc8SJoe Perches}
1905d752fcc8SJoe Perches
1906f2d7e4d4SJoe Perchessub fix_insert_line {
1907f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1908f2d7e4d4SJoe Perches
1909f2d7e4d4SJoe Perches	my $inserted = {
1910f2d7e4d4SJoe Perches		LINENR => $linenr,
1911f2d7e4d4SJoe Perches		LINE => $line,
1912f2d7e4d4SJoe Perches	};
1913f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1914f2d7e4d4SJoe Perches}
1915f2d7e4d4SJoe Perches
1916f2d7e4d4SJoe Perchessub fix_delete_line {
1917f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1918f2d7e4d4SJoe Perches
1919f2d7e4d4SJoe Perches	my $deleted = {
1920f2d7e4d4SJoe Perches		LINENR => $linenr,
1921f2d7e4d4SJoe Perches		LINE => $line,
1922f2d7e4d4SJoe Perches	};
1923f2d7e4d4SJoe Perches
1924f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1925f2d7e4d4SJoe Perches}
1926f2d7e4d4SJoe Perches
1927de7d4f0eSAndy Whitcroftsub ERROR {
1928cbec18afSJoe Perches	my ($type, $msg) = @_;
1929cbec18afSJoe Perches
1930cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1931de7d4f0eSAndy Whitcroft		our $clean = 0;
19326c72ffaaSAndy Whitcroft		our $cnt_error++;
19333705ce5bSJoe Perches		return 1;
1934de7d4f0eSAndy Whitcroft	}
19353705ce5bSJoe Perches	return 0;
1936773647a0SAndy Whitcroft}
1937de7d4f0eSAndy Whitcroftsub WARN {
1938cbec18afSJoe Perches	my ($type, $msg) = @_;
1939cbec18afSJoe Perches
1940cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1941de7d4f0eSAndy Whitcroft		our $clean = 0;
19426c72ffaaSAndy Whitcroft		our $cnt_warn++;
19433705ce5bSJoe Perches		return 1;
1944de7d4f0eSAndy Whitcroft	}
19453705ce5bSJoe Perches	return 0;
1946773647a0SAndy Whitcroft}
1947de7d4f0eSAndy Whitcroftsub CHK {
1948cbec18afSJoe Perches	my ($type, $msg) = @_;
1949cbec18afSJoe Perches
1950cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1951de7d4f0eSAndy Whitcroft		our $clean = 0;
19526c72ffaaSAndy Whitcroft		our $cnt_chk++;
19533705ce5bSJoe Perches		return 1;
19546c72ffaaSAndy Whitcroft	}
19553705ce5bSJoe Perches	return 0;
1956de7d4f0eSAndy Whitcroft}
1957de7d4f0eSAndy Whitcroft
19586ecd9674SAndy Whitcroftsub check_absolute_file {
19596ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
19606ecd9674SAndy Whitcroft	my $file = $absolute;
19616ecd9674SAndy Whitcroft
19626ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
19636ecd9674SAndy Whitcroft
19646ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
19656ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
19666ecd9674SAndy Whitcroft		if (-f "$root/$file") {
19676ecd9674SAndy Whitcroft			##print "file<$file>\n";
19686ecd9674SAndy Whitcroft			last;
19696ecd9674SAndy Whitcroft		}
19706ecd9674SAndy Whitcroft	}
19716ecd9674SAndy Whitcroft	if (! -f _)  {
19726ecd9674SAndy Whitcroft		return 0;
19736ecd9674SAndy Whitcroft	}
19746ecd9674SAndy Whitcroft
19756ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
19766ecd9674SAndy Whitcroft	my $prefix = $absolute;
19776ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
19786ecd9674SAndy Whitcroft
19796ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
19806ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1981000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1982000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
19836ecd9674SAndy Whitcroft	}
19846ecd9674SAndy Whitcroft}
19856ecd9674SAndy Whitcroft
19863705ce5bSJoe Perchessub trim {
19873705ce5bSJoe Perches	my ($string) = @_;
19883705ce5bSJoe Perches
1989b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1990b34c648bSJoe Perches
1991b34c648bSJoe Perches	return $string;
1992b34c648bSJoe Perches}
1993b34c648bSJoe Perches
1994b34c648bSJoe Perchessub ltrim {
1995b34c648bSJoe Perches	my ($string) = @_;
1996b34c648bSJoe Perches
1997b34c648bSJoe Perches	$string =~ s/^\s+//;
1998b34c648bSJoe Perches
1999b34c648bSJoe Perches	return $string;
2000b34c648bSJoe Perches}
2001b34c648bSJoe Perches
2002b34c648bSJoe Perchessub rtrim {
2003b34c648bSJoe Perches	my ($string) = @_;
2004b34c648bSJoe Perches
2005b34c648bSJoe Perches	$string =~ s/\s+$//;
20063705ce5bSJoe Perches
20073705ce5bSJoe Perches	return $string;
20083705ce5bSJoe Perches}
20093705ce5bSJoe Perches
201052ea8506SJoe Perchessub string_find_replace {
201152ea8506SJoe Perches	my ($string, $find, $replace) = @_;
201252ea8506SJoe Perches
201352ea8506SJoe Perches	$string =~ s/$find/$replace/g;
201452ea8506SJoe Perches
201552ea8506SJoe Perches	return $string;
201652ea8506SJoe Perches}
201752ea8506SJoe Perches
20183705ce5bSJoe Perchessub tabify {
20193705ce5bSJoe Perches	my ($leading) = @_;
20203705ce5bSJoe Perches
20213705ce5bSJoe Perches	my $source_indent = 8;
20223705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
20233705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
20243705ce5bSJoe Perches
20253705ce5bSJoe Perches	#convert leading spaces to tabs
20263705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
20273705ce5bSJoe Perches	#Remove spaces before a tab
20283705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
20293705ce5bSJoe Perches
20303705ce5bSJoe Perches	return "$leading";
20313705ce5bSJoe Perches}
20323705ce5bSJoe Perches
2033d1fe9c09SJoe Perchessub pos_last_openparen {
2034d1fe9c09SJoe Perches	my ($line) = @_;
2035d1fe9c09SJoe Perches
2036d1fe9c09SJoe Perches	my $pos = 0;
2037d1fe9c09SJoe Perches
2038d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2039d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2040d1fe9c09SJoe Perches
2041d1fe9c09SJoe Perches	my $last_openparen = 0;
2042d1fe9c09SJoe Perches
2043d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2044d1fe9c09SJoe Perches		return -1;
2045d1fe9c09SJoe Perches	}
2046d1fe9c09SJoe Perches
2047d1fe9c09SJoe Perches	my $len = length($line);
2048d1fe9c09SJoe Perches
2049d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2050d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2051d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2052d1fe9c09SJoe Perches			$pos += length($1) - 1;
2053d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2054d1fe9c09SJoe Perches			$last_openparen = $pos;
2055d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2056d1fe9c09SJoe Perches			last;
2057d1fe9c09SJoe Perches		}
2058d1fe9c09SJoe Perches	}
2059d1fe9c09SJoe Perches
206091cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2061d1fe9c09SJoe Perches}
2062d1fe9c09SJoe Perches
20630a920b5bSAndy Whitcroftsub process {
20640a920b5bSAndy Whitcroft	my $filename = shift;
20650a920b5bSAndy Whitcroft
20660a920b5bSAndy Whitcroft	my $linenr=0;
20670a920b5bSAndy Whitcroft	my $prevline="";
2068c2fdda0dSAndy Whitcroft	my $prevrawline="";
20690a920b5bSAndy Whitcroft	my $stashline="";
2070c2fdda0dSAndy Whitcroft	my $stashrawline="";
20710a920b5bSAndy Whitcroft
20724a0df2efSAndy Whitcroft	my $length;
20730a920b5bSAndy Whitcroft	my $indent;
20740a920b5bSAndy Whitcroft	my $previndent=0;
20750a920b5bSAndy Whitcroft	my $stashindent=0;
20760a920b5bSAndy Whitcroft
2077de7d4f0eSAndy Whitcroft	our $clean = 1;
20780a920b5bSAndy Whitcroft	my $signoff = 0;
20790a920b5bSAndy Whitcroft	my $is_patch = 0;
208029ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
208115662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2082ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2083bf4daf12SJoe Perches       my $commit_log_possible_stack_dump = 0;
20842a076f40SJoe Perches	my $commit_log_long_line = 0;
2085e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
208613f1937eSJoe Perches	my $reported_maintainer_file = 0;
2087fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2088fa64205dSPasi Savanainen
2089365dd4eaSJoe Perches	my $last_blank_line = 0;
20905e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2091365dd4eaSJoe Perches
209213214adfSAndy Whitcroft	our @report = ();
20936c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
20946c72ffaaSAndy Whitcroft	our $cnt_error = 0;
20956c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
20966c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
20976c72ffaaSAndy Whitcroft
20980a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
20990a920b5bSAndy Whitcroft	my $realfile = '';
21000a920b5bSAndy Whitcroft	my $realline = 0;
21010a920b5bSAndy Whitcroft	my $realcnt = 0;
21020a920b5bSAndy Whitcroft	my $here = '';
21030a920b5bSAndy Whitcroft	my $in_comment = 0;
2104c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
21050a920b5bSAndy Whitcroft	my $first_line = 0;
21061e855726SWolfram Sang	my $p1_prefix = '';
21070a920b5bSAndy Whitcroft
210813214adfSAndy Whitcroft	my $prev_values = 'E';
210913214adfSAndy Whitcroft
211013214adfSAndy Whitcroft	# suppression flags
2111773647a0SAndy Whitcroft	my %suppress_ifbraces;
2112170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
21132b474a1aSAndy Whitcroft	my %suppress_export;
21143e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2115653d4876SAndy Whitcroft
21167e51f197SJoe Perches	my %signatures = ();
2117323c1260SJoe Perches
2118c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2119de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2120c2fdda0dSAndy Whitcroft	#
2121de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2122de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2123773647a0SAndy Whitcroft
2124d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2125d8b07710SJoe Perches
2126773647a0SAndy Whitcroft	sanitise_line_reset();
2127c2fdda0dSAndy Whitcroft	my $line;
2128c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2129773647a0SAndy Whitcroft		$linenr++;
2130773647a0SAndy Whitcroft		$line = $rawline;
2131c2fdda0dSAndy Whitcroft
21323705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
21333705ce5bSJoe Perches
2134773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2135de7d4f0eSAndy Whitcroft			$setup_docs = 0;
2136de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2137de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2138de7d4f0eSAndy Whitcroft			}
2139773647a0SAndy Whitcroft			#next;
2140de7d4f0eSAndy Whitcroft		}
2141773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2142773647a0SAndy Whitcroft			$realline=$1-1;
2143773647a0SAndy Whitcroft			if (defined $2) {
2144773647a0SAndy Whitcroft				$realcnt=$3+1;
2145773647a0SAndy Whitcroft			} else {
2146773647a0SAndy Whitcroft				$realcnt=1+1;
2147773647a0SAndy Whitcroft			}
2148c45dcabdSAndy Whitcroft			$in_comment = 0;
2149773647a0SAndy Whitcroft
2150773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2151773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2152773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2153773647a0SAndy Whitcroft			# at context start.
2154773647a0SAndy Whitcroft			my $edge;
215501fa9147SAndy Whitcroft			my $cnt = $realcnt;
215601fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
215701fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
215801fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
215901fa9147SAndy Whitcroft				$cnt--;
216001fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2161721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2162fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2163fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2164fae17daeSAndy Whitcroft					($edge) = $1;
2165fae17daeSAndy Whitcroft					last;
2166fae17daeSAndy Whitcroft				}
2167773647a0SAndy Whitcroft			}
2168773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2169773647a0SAndy Whitcroft				$in_comment = 1;
2170773647a0SAndy Whitcroft			}
2171773647a0SAndy Whitcroft
2172773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2173773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2174773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2175773647a0SAndy Whitcroft			if (!defined $edge &&
217683242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2177773647a0SAndy Whitcroft			{
2178773647a0SAndy Whitcroft				$in_comment = 1;
2179773647a0SAndy Whitcroft			}
2180773647a0SAndy Whitcroft
2181773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2182773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2183773647a0SAndy Whitcroft
2184171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2185773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2186171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2187773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2188773647a0SAndy Whitcroft		}
2189773647a0SAndy Whitcroft		push(@lines, $line);
2190773647a0SAndy Whitcroft
2191773647a0SAndy Whitcroft		if ($realcnt > 1) {
2192773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2193773647a0SAndy Whitcroft		} else {
2194773647a0SAndy Whitcroft			$realcnt = 0;
2195773647a0SAndy Whitcroft		}
2196773647a0SAndy Whitcroft
2197773647a0SAndy Whitcroft		#print "==>$rawline\n";
2198773647a0SAndy Whitcroft		#print "-->$line\n";
2199de7d4f0eSAndy Whitcroft
2200de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2201de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2202de7d4f0eSAndy Whitcroft		}
2203de7d4f0eSAndy Whitcroft	}
2204de7d4f0eSAndy Whitcroft
22056c72ffaaSAndy Whitcroft	$prefix = '';
22066c72ffaaSAndy Whitcroft
2207773647a0SAndy Whitcroft	$realcnt = 0;
2208773647a0SAndy Whitcroft	$linenr = 0;
2209194f66fcSJoe Perches	$fixlinenr = -1;
22100a920b5bSAndy Whitcroft	foreach my $line (@lines) {
22110a920b5bSAndy Whitcroft		$linenr++;
2212194f66fcSJoe Perches		$fixlinenr++;
22131b5539b1SJoe Perches		my $sline = $line;	#copy of $line
22141b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
22150a920b5bSAndy Whitcroft
2216c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
22176c72ffaaSAndy Whitcroft
22180a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2219e518e9a5SJoe Perches		if (!$in_commit_log &&
2220e518e9a5SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
22210a920b5bSAndy Whitcroft			$is_patch = 1;
22224a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
22230a920b5bSAndy Whitcroft			$realline=$1-1;
22240a920b5bSAndy Whitcroft			if (defined $2) {
22250a920b5bSAndy Whitcroft				$realcnt=$3+1;
22260a920b5bSAndy Whitcroft			} else {
22270a920b5bSAndy Whitcroft				$realcnt=1+1;
22280a920b5bSAndy Whitcroft			}
2229c2fdda0dSAndy Whitcroft			annotate_reset();
223013214adfSAndy Whitcroft			$prev_values = 'E';
223113214adfSAndy Whitcroft
2232773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2233170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
22342b474a1aSAndy Whitcroft			%suppress_export = ();
22353e469cdcSAndy Whitcroft			$suppress_statement = 0;
22360a920b5bSAndy Whitcroft			next;
22370a920b5bSAndy Whitcroft
22384a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
22394a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
22404a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2241773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
22420a920b5bSAndy Whitcroft			$realline++;
2243d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
22440a920b5bSAndy Whitcroft
22454a0df2efSAndy Whitcroft			# Measure the line length and indent.
2246c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
22470a920b5bSAndy Whitcroft
22480a920b5bSAndy Whitcroft			# Track the previous line.
22490a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
22500a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2251c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2252c2fdda0dSAndy Whitcroft
2253773647a0SAndy Whitcroft			#warn "line<$line>\n";
22546c72ffaaSAndy Whitcroft
2255d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2256d8aaf121SAndy Whitcroft			$realcnt--;
22570a920b5bSAndy Whitcroft		}
22580a920b5bSAndy Whitcroft
2259cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2260cc77cdcaSAndy Whitcroft
22616c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
22626c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2263773647a0SAndy Whitcroft
22642ac73b4fSJoe Perches		my $found_file = 0;
2265773647a0SAndy Whitcroft		# extract the filename as it passes
22663bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
22673bf9a009SRabin Vincent			$realfile = $1;
22682b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2269270c49a0SJoe Perches			$in_commit_log = 0;
22702ac73b4fSJoe Perches			$found_file = 1;
22713bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2272773647a0SAndy Whitcroft			$realfile = $1;
22732b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2274270c49a0SJoe Perches			$in_commit_log = 0;
22751e855726SWolfram Sang
22761e855726SWolfram Sang			$p1_prefix = $1;
2277e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2278e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2279000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2280000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
22811e855726SWolfram Sang			}
2282773647a0SAndy Whitcroft
2283c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2284000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2285000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2286773647a0SAndy Whitcroft			}
22872ac73b4fSJoe Perches			$found_file = 1;
22882ac73b4fSJoe Perches		}
22892ac73b4fSJoe Perches
229034d8815fSJoe Perches#make up the handle for any error we report on this line
229134d8815fSJoe Perches		if ($showfile) {
229234d8815fSJoe Perches			$prefix = "$realfile:$realline: "
229334d8815fSJoe Perches		} elsif ($emacs) {
22947d3a9f67SJoe Perches			if ($file) {
22957d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
22967d3a9f67SJoe Perches			} else {
229734d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
229834d8815fSJoe Perches			}
22997d3a9f67SJoe Perches		}
230034d8815fSJoe Perches
23012ac73b4fSJoe Perches		if ($found_file) {
2302*85b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
2303*85b0ee18SJoe Perches				WARN("OBSOLETE",
2304*85b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
2305*85b0ee18SJoe Perches			}
23067bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
23072ac73b4fSJoe Perches				$check = 1;
23082ac73b4fSJoe Perches			} else {
23092ac73b4fSJoe Perches				$check = $check_orig;
23102ac73b4fSJoe Perches			}
2311773647a0SAndy Whitcroft			next;
2312773647a0SAndy Whitcroft		}
2313773647a0SAndy Whitcroft
2314389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
23150a920b5bSAndy Whitcroft
2316c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2317c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2318c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
23190a920b5bSAndy Whitcroft
23206c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
23216c72ffaaSAndy Whitcroft
2322e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2323e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2324e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2325e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2326e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2327e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2328e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2329e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2330e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2331e518e9a5SJoe Perches		}
2332e518e9a5SJoe Perches
23333bf9a009SRabin Vincent# Check for incorrect file permissions
23343bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
23353bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
233604db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
233704db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2338000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2339000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
23403bf9a009SRabin Vincent			}
23413bf9a009SRabin Vincent		}
23423bf9a009SRabin Vincent
234320112475SJoe Perches# Check the patch for a signoff:
2344d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
23454a0df2efSAndy Whitcroft			$signoff++;
234615662b3eSJoe Perches			$in_commit_log = 0;
23470a920b5bSAndy Whitcroft		}
234820112475SJoe Perches
2349e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2350e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2351e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2352e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2353e0d975b1SJoe Perches		}
2354e0d975b1SJoe Perches
235520112475SJoe Perches# Check signature styles
2356270c49a0SJoe Perches		if (!$in_header_lines &&
2357ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
235820112475SJoe Perches			my $space_before = $1;
235920112475SJoe Perches			my $sign_off = $2;
236020112475SJoe Perches			my $space_after = $3;
236120112475SJoe Perches			my $email = $4;
236220112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
236320112475SJoe Perches
2364ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2365ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2366ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2367ce0338dfSJoe Perches			}
236820112475SJoe Perches			if (defined $space_before && $space_before ne "") {
23693705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
23703705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
23713705ce5bSJoe Perches				    $fix) {
2372194f66fcSJoe Perches					$fixed[$fixlinenr] =
23733705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
23743705ce5bSJoe Perches				}
237520112475SJoe Perches			}
237620112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
23773705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
23783705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
23793705ce5bSJoe Perches				    $fix) {
2380194f66fcSJoe Perches					$fixed[$fixlinenr] =
23813705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
23823705ce5bSJoe Perches				}
23833705ce5bSJoe Perches
238420112475SJoe Perches			}
238520112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
23863705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
23873705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
23883705ce5bSJoe Perches				    $fix) {
2389194f66fcSJoe Perches					$fixed[$fixlinenr] =
23903705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
23913705ce5bSJoe Perches				}
239220112475SJoe Perches			}
239320112475SJoe Perches
239420112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
239520112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
239620112475SJoe Perches			if ($suggested_email eq "") {
2397000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2398000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
239920112475SJoe Perches			} else {
240020112475SJoe Perches				my $dequoted = $suggested_email;
240120112475SJoe Perches				$dequoted =~ s/^"//;
240220112475SJoe Perches				$dequoted =~ s/" </ </;
240320112475SJoe Perches				# Don't force email to have quotes
240420112475SJoe Perches				# Allow just an angle bracketed address
240520112475SJoe Perches				if ("$dequoted$comment" ne $email &&
240620112475SJoe Perches				    "<$email_address>$comment" ne $email &&
240720112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2408000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2409000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
241020112475SJoe Perches				}
24110a920b5bSAndy Whitcroft			}
24127e51f197SJoe Perches
24137e51f197SJoe Perches# Check for duplicate signatures
24147e51f197SJoe Perches			my $sig_nospace = $line;
24157e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
24167e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
24177e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
24187e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
24197e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
24207e51f197SJoe Perches			} else {
24217e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
24227e51f197SJoe Perches			}
24230a920b5bSAndy Whitcroft		}
24240a920b5bSAndy Whitcroft
2425a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2426a2fe16b9SJoe Perches		if ($in_header_lines &&
2427a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2428a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2429a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2430a2fe16b9SJoe Perches		}
2431a2fe16b9SJoe Perches
24329b3189ebSJoe Perches# Check for old stable address
24339b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
24349b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
24359b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
24369b3189ebSJoe Perches		}
24379b3189ebSJoe Perches
24387ebd05efSChristopher Covington# Check for unwanted Gerrit info
24397ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
24407ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
24417ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
24427ebd05efSChristopher Covington		}
24437ebd05efSChristopher Covington
2444369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2445369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2446369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2447369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2448369c8dd3SJoe Perches					# timestamp
2449369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2450369c8dd3SJoe Perches					# stack dump address
2451369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2452369c8dd3SJoe Perches		}
2453369c8dd3SJoe Perches
24542a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
24552a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2456bf4daf12SJoe Perches		    length($line) > 75 &&
2457bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2458bf4daf12SJoe Perches					# file delta changes
2459bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2460bf4daf12SJoe Perches					# filename then :
2461bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2462bf4daf12SJoe Perches					# A Fixes: or Link: line
2463bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
24642a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
24652a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
24662a076f40SJoe Perches			$commit_log_long_line = 1;
24672a076f40SJoe Perches		}
24682a076f40SJoe Perches
2469bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2470bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2471bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2472bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2473bf4daf12SJoe Perches		}
2474bf4daf12SJoe Perches
24750d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2476369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2477aab38f51SJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
2478fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2479aab38f51SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
2480369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2481bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2482fe043ea1SJoe Perches			my $init_char = "c";
2483fe043ea1SJoe Perches			my $orig_commit = "";
24840d7835fcSJoe Perches			my $short = 1;
24850d7835fcSJoe Perches			my $long = 0;
24860d7835fcSJoe Perches			my $case = 1;
24870d7835fcSJoe Perches			my $space = 1;
24880d7835fcSJoe Perches			my $hasdesc = 0;
248919c146a6SJoe Perches			my $hasparens = 0;
24900d7835fcSJoe Perches			my $id = '0123456789ab';
24910d7835fcSJoe Perches			my $orig_desc = "commit description";
24920d7835fcSJoe Perches			my $description = "";
24930d7835fcSJoe Perches
2494fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2495fe043ea1SJoe Perches				$init_char = $1;
2496fe043ea1SJoe Perches				$orig_commit = lc($2);
2497fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2498fe043ea1SJoe Perches				$orig_commit = lc($1);
2499fe043ea1SJoe Perches			}
2500fe043ea1SJoe Perches
25010d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
25020d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
25030d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
25040d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
25050d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
25060d7835fcSJoe Perches				$orig_desc = $1;
250719c146a6SJoe Perches				$hasparens = 1;
25080d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
25090d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
25100d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
25110d7835fcSJoe Perches				$orig_desc = $1;
251219c146a6SJoe Perches				$hasparens = 1;
2513b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2514b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2515b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2516b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2517b671fde0SJoe Perches				$orig_desc = $1;
2518b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2519b671fde0SJoe Perches				$orig_desc .= " " . $1;
252019c146a6SJoe Perches				$hasparens = 1;
25210d7835fcSJoe Perches			}
25220d7835fcSJoe Perches
25230d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
25240d7835fcSJoe Perches							      $id, $orig_desc);
25250d7835fcSJoe Perches
252619c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2527d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
25280d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
25290d7835fcSJoe Perches			}
2530d311cd44SJoe Perches		}
2531d311cd44SJoe Perches
253213f1937eSJoe Perches# Check for added, moved or deleted files
253313f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
253413f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
253513f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
253613f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
253713f1937eSJoe Perches		      (defined($1) || defined($2))))) {
253813f1937eSJoe Perches			$reported_maintainer_file = 1;
253913f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
254013f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
254113f1937eSJoe Perches		}
254213f1937eSJoe Perches
254300df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
25448905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2545000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2546000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
25476c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2548de7d4f0eSAndy Whitcroft		}
2549de7d4f0eSAndy Whitcroft
25506ecd9674SAndy Whitcroft# Check for absolute kernel paths.
25516ecd9674SAndy Whitcroft		if ($tree) {
25526ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
25536ecd9674SAndy Whitcroft				my $file = $1;
25546ecd9674SAndy Whitcroft
25556ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
25566ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
25576ecd9674SAndy Whitcroft					#
25586ecd9674SAndy Whitcroft				} else {
25596ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
25606ecd9674SAndy Whitcroft				}
25616ecd9674SAndy Whitcroft			}
25626ecd9674SAndy Whitcroft		}
25636ecd9674SAndy Whitcroft
2564de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2565de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2566171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2567171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2568171ae1a4SAndy Whitcroft
2569171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2570171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2571171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2572171ae1a4SAndy Whitcroft
257334d99219SJoe Perches			CHK("INVALID_UTF8",
2574000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
257500df344fSAndy Whitcroft		}
25760a920b5bSAndy Whitcroft
257715662b3eSJoe Perches# Check if it's the start of a commit log
257815662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
257915662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
258029ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
258129ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
258215662b3eSJoe Perches			$in_header_lines = 0;
258315662b3eSJoe Perches			$in_commit_log = 1;
2584ed43c4e5SAllen Hubbe			$has_commit_log = 1;
258515662b3eSJoe Perches		}
258615662b3eSJoe Perches
2587fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2588fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2589fa64205dSPasi Savanainen		if ($in_header_lines &&
2590fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2591fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2592fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2593fa64205dSPasi Savanainen		}
2594fa64205dSPasi Savanainen
2595fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
259615662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2597fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
259815662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
259915662b3eSJoe Perches		}
260015662b3eSJoe Perches
260166b47b4aSKees Cook# Check for various typo / spelling mistakes
260266d7a382SJoe Perches		if (defined($misspellings) &&
260366d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2604ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
260566b47b4aSKees Cook				my $typo = $1;
260666b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
260766b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
260866b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
260966b47b4aSKees Cook				my $msg_type = \&WARN;
261066b47b4aSKees Cook				$msg_type = \&CHK if ($file);
261166b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
261266b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
261366b47b4aSKees Cook				    $fix) {
261466b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
261566b47b4aSKees Cook				}
261666b47b4aSKees Cook			}
261766b47b4aSKees Cook		}
261866b47b4aSKees Cook
261930670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
262030670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
262100df344fSAndy Whitcroft
26220a920b5bSAndy Whitcroft#trailing whitespace
26239c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2624c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2625d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2626d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2627d5e616fcSJoe Perches			    $fix) {
2628194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2629d5e616fcSJoe Perches			}
2630c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2631c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26323705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
26333705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
26343705ce5bSJoe Perches			    $fix) {
2635194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
26363705ce5bSJoe Perches			}
26373705ce5bSJoe Perches
2638d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
26390a920b5bSAndy Whitcroft		}
26405368df20SAndy Whitcroft
26414783f894SJosh Triplett# Check for FSF mailing addresses.
2642109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
26433e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
26443e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
26454783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
26464783f894SJosh Triplett			my $msg_type = \&ERROR;
26474783f894SJosh Triplett			$msg_type = \&CHK if ($file);
26484783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
26494783f894SJosh 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)
26504783f894SJosh Triplett		}
26514783f894SJosh Triplett
26523354957aSAndi Kleen# check for Kconfig help text having a real description
26539fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
26549fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
26553354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
26568d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
26573354957aSAndi Kleen			my $length = 0;
26589fe287d7SAndy Whitcroft			my $cnt = $realcnt;
26599fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
26609fe287d7SAndy Whitcroft			my $f;
2661a1385803SAndy Whitcroft			my $is_start = 0;
26629fe287d7SAndy Whitcroft			my $is_end = 0;
2663a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
26649fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
26659fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
26669fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
26679fe287d7SAndy Whitcroft
26689fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
26698d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2670a1385803SAndy Whitcroft
26718d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2672a1385803SAndy Whitcroft					$is_start = 1;
26738d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2674a1385803SAndy Whitcroft					$length = -1;
2675a1385803SAndy Whitcroft				}
2676a1385803SAndy Whitcroft
26779fe287d7SAndy Whitcroft				$f =~ s/^.//;
26783354957aSAndi Kleen				$f =~ s/#.*//;
26793354957aSAndi Kleen				$f =~ s/^\s+//;
26803354957aSAndi Kleen				next if ($f =~ /^$/);
26819fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
26829fe287d7SAndy Whitcroft					$is_end = 1;
26839fe287d7SAndy Whitcroft					last;
26849fe287d7SAndy Whitcroft				}
26853354957aSAndi Kleen				$length++;
26863354957aSAndi Kleen			}
268756193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2688000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
268956193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
269056193274SVadim Bendebury			}
2691a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
26923354957aSAndi Kleen		}
26933354957aSAndi Kleen
26941ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
26951ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
26961ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
26971ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
26981ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
26991ba8dfd1SKees Cook		}
27001ba8dfd1SKees Cook
2701327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2702327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2703327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2704327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2705327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2706327953e9SChristoph Jaeger		}
2707327953e9SChristoph Jaeger
2708c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2709c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2710c68e5878SArnaud Lacombe			my $flag = $1;
2711c68e5878SArnaud Lacombe			my $replacement = {
2712c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2713c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2714c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2715c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2716c68e5878SArnaud Lacombe			};
2717c68e5878SArnaud Lacombe
2718c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2719c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2720c68e5878SArnaud Lacombe		}
2721c68e5878SArnaud Lacombe
2722bff5da43SRob Herring# check for DT compatible documentation
27237dd05b38SFlorian Vaussard		if (defined $root &&
27247dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
27257dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
27267dd05b38SFlorian Vaussard
2727bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2728bff5da43SRob Herring
2729cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2730cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2731cc93319bSFlorian Vaussard
2732bff5da43SRob Herring			foreach my $compat (@compats) {
2733bff5da43SRob Herring				my $compat2 = $compat;
2734185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2735185d566bSRob Herring				my $compat3 = $compat;
2736185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2737185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2738bff5da43SRob Herring				if ( $? >> 8 ) {
2739bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2740bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2741bff5da43SRob Herring				}
2742bff5da43SRob Herring
27434fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
27444fbf32a6SFlorian Vaussard				my $vendor = $1;
2745cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2746bff5da43SRob Herring				if ( $? >> 8 ) {
2747bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2748cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2749bff5da43SRob Herring				}
2750bff5da43SRob Herring			}
2751bff5da43SRob Herring		}
2752bff5da43SRob Herring
27535368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2754de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
27555368df20SAndy Whitcroft
275647e0c88bSJoe Perches# line length limit (with some exclusions)
275747e0c88bSJoe Perches#
275847e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
275947e0c88bSJoe Perches#	logging functions like pr_info that end in a string
276047e0c88bSJoe Perches#	lines with a single string
276147e0c88bSJoe Perches#	#defines that are a single string
276247e0c88bSJoe Perches#
276347e0c88bSJoe Perches# There are 3 different line length message types:
276447e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_linelength
276547e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
276647e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
276747e0c88bSJoe Perches#
276847e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
276947e0c88bSJoe Perches#
277047e0c88bSJoe Perches
2771b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
277247e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
277347e0c88bSJoe Perches
277447e0c88bSJoe Perches			# Check the allowed long line types first
277547e0c88bSJoe Perches
277647e0c88bSJoe Perches			# logging functions that end in a string that starts
277747e0c88bSJoe Perches			# before $max_line_length
277847e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
277947e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
278047e0c88bSJoe Perches				$msg_type = "";
278147e0c88bSJoe Perches
278247e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
278347e0c88bSJoe Perches			# #defines with only strings
278447e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
278547e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
278647e0c88bSJoe Perches				$msg_type = "";
278747e0c88bSJoe Perches
2788d560a5f8SJoe Perches			# EFI_GUID is another special case
2789d560a5f8SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2790d560a5f8SJoe Perches				$msg_type = "";
2791d560a5f8SJoe Perches
279247e0c88bSJoe Perches			# Otherwise set the alternate message types
279347e0c88bSJoe Perches
279447e0c88bSJoe Perches			# a comment starts before $max_line_length
279547e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
279647e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
279747e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
279847e0c88bSJoe Perches
279947e0c88bSJoe Perches			# a quoted string starts before $max_line_length
280047e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
280147e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
280247e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
280347e0c88bSJoe Perches			}
280447e0c88bSJoe Perches
280547e0c88bSJoe Perches			if ($msg_type ne "" &&
280647e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
280747e0c88bSJoe Perches				WARN($msg_type,
28086cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
28090a920b5bSAndy Whitcroft			}
281047e0c88bSJoe Perches		}
28110a920b5bSAndy Whitcroft
28128905a67cSAndy Whitcroft# check for adding lines without a newline.
28138905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2814000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2815000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
28168905a67cSAndy Whitcroft		}
28178905a67cSAndy Whitcroft
281842e41c54SMike Frysinger# Blackfin: use hi/lo macros
281942e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
282042e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
282142e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2822000d1cc1SJoe Perches				ERROR("LO_MACRO",
2823000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
282442e41c54SMike Frysinger			}
282542e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
282642e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2827000d1cc1SJoe Perches				ERROR("HI_MACRO",
2828000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
282942e41c54SMike Frysinger			}
283042e41c54SMike Frysinger		}
283142e41c54SMike Frysinger
2832b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2833de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
28340a920b5bSAndy Whitcroft
28350a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
28360a920b5bSAndy Whitcroft# more than 8 must use tabs.
2837c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2838c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2839c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2840d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
28413705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
28423705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
28433705ce5bSJoe Perches			    $fix) {
2844194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
28453705ce5bSJoe Perches			}
28460a920b5bSAndy Whitcroft		}
28470a920b5bSAndy Whitcroft
284808e44365SAlberto Panizzo# check for space before tabs.
284908e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
285008e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
28513705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
28523705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
28533705ce5bSJoe Perches			    $fix) {
2854194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2855d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2856194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2857c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
28583705ce5bSJoe Perches			}
285908e44365SAlberto Panizzo		}
286008e44365SAlberto Panizzo
2861d1fe9c09SJoe Perches# check for && or || at the start of a line
2862d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2863d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2864d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2865d1fe9c09SJoe Perches		}
2866d1fe9c09SJoe Perches
2867a91e8994SJoe Perches# check indentation starts on a tab stop
2868a91e8994SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2869a91e8994SJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2870a91e8994SJoe Perches			my $indent = length($1);
2871a91e8994SJoe Perches			if ($indent % 8) {
2872a91e8994SJoe Perches				if (WARN("TABSTOP",
2873a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
2874a91e8994SJoe Perches				    $fix) {
2875a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2876a91e8994SJoe Perches				}
2877a91e8994SJoe Perches			}
2878a91e8994SJoe Perches		}
2879a91e8994SJoe Perches
2880d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2881d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
288291cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2883d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2884d1fe9c09SJoe Perches			my $oldindent = $1;
2885d1fe9c09SJoe Perches			my $rest = $2;
2886d1fe9c09SJoe Perches
2887d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2888d1fe9c09SJoe Perches			if ($pos >= 0) {
2889b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2890b34a26f3SJoe Perches				my $newindent = $2;
2891d1fe9c09SJoe Perches
2892d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2893d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2894d1fe9c09SJoe Perches					" "  x ($pos % 8);
2895d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2896d1fe9c09SJoe Perches
2897d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2898d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
28993705ce5bSJoe Perches
29003705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
29013705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
29023705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2903194f66fcSJoe Perches						$fixed[$fixlinenr] =~
29043705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
29053705ce5bSJoe Perches					}
2906d1fe9c09SJoe Perches				}
2907d1fe9c09SJoe Perches			}
2908d1fe9c09SJoe Perches		}
2909d1fe9c09SJoe Perches
29106ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
29116ab3a970SJoe Perches# avoid checking a few false positives:
29126ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
29136ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
29146ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
29156ab3a970SJoe Perches#   multiline macros that define functions
29166ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
29176ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
29186ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
29193705ce5bSJoe Perches			if (CHK("SPACING",
2920f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
29213705ce5bSJoe Perches			    $fix) {
2922194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2923f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
29243705ce5bSJoe Perches			}
2925aad4f614SJoe Perches		}
2926aad4f614SJoe Perches
292786406b1cSJoe Perches# Block comment styles
292886406b1cSJoe Perches# Networking with an initial /*
292905880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2930fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
293185ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
293285ad978cSJoe Perches		    $realline > 2) {
293305880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
293405880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
293505880600SJoe Perches		}
293605880600SJoe Perches
293786406b1cSJoe Perches# Block comments use * on subsequent lines
293886406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
293986406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
2940a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
294161135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2942a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
294386406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
294486406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
2945a605e32eSJoe Perches		}
2946a605e32eSJoe Perches
294786406b1cSJoe Perches# Block comments use */ on trailing lines
294886406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2949c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2950c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2951c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
295286406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
295386406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
295405880600SJoe Perches		}
295505880600SJoe Perches
29567f619191SJoe Perches# check for missing blank lines after struct/union declarations
29577f619191SJoe Perches# with exceptions for various attributes and macros
29587f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
29597f619191SJoe Perches		    $line =~ /^\+/ &&
29607f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
29617f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
29627f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
29637f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
29647f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
29657f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
29667f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
29677f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2968d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2969d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2970d752fcc8SJoe Perches			    $fix) {
2971f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2972d752fcc8SJoe Perches			}
29737f619191SJoe Perches		}
29747f619191SJoe Perches
2975365dd4eaSJoe Perches# check for multiple consecutive blank lines
2976365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2977365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2978365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2979d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2980d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2981d752fcc8SJoe Perches			    $fix) {
2982f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2983d752fcc8SJoe Perches			}
2984d752fcc8SJoe Perches
2985365dd4eaSJoe Perches			$last_blank_line = $linenr;
2986365dd4eaSJoe Perches		}
2987365dd4eaSJoe Perches
29883b617e3bSJoe Perches# check for missing blank lines after declarations
29893f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
29903f7bac03SJoe Perches			# actual declarations
29913f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
29925a4e1fd3SJoe Perches			# function pointer declarations
29935a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
29943f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
29953f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
29963f7bac03SJoe Perches			# known declaration macros
29973f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
29983f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
29993f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
30003f7bac03SJoe Perches			# other possible extensions of declaration lines
30013f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
30023f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
30033f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
30043f7bac03SJoe Perches			# looks like a declaration
30053f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
30065a4e1fd3SJoe Perches			# function pointer declarations
30075a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
30083f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
30093f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
30103f7bac03SJoe Perches			# known declaration macros
30113f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
30123f7bac03SJoe Perches			# start of struct or union or enum
30133b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
30143f7bac03SJoe Perches			# start or end of block or continuation of declaration
30153f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
30163f7bac03SJoe Perches			# bitfield continuation
30173f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
30183f7bac03SJoe Perches			# other possible extensions of declaration lines
30193f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
30203f7bac03SJoe Perches			# indentation of previous and current line are the same
30213f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
3022d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
3023d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
3024d752fcc8SJoe Perches			    $fix) {
3025f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
3026d752fcc8SJoe Perches			}
30273b617e3bSJoe Perches		}
30283b617e3bSJoe Perches
30295f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
30306b4c5bebSAndy Whitcroft# Exceptions:
30316b4c5bebSAndy Whitcroft#  1) within comments
30326b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
30336b4c5bebSAndy Whitcroft#  3) hanging labels
30343705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
30355f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
30363705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
30373705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
30383705ce5bSJoe Perches			    $fix) {
3039194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
30403705ce5bSJoe Perches			}
30415f7ddae6SRaffaele Recalcati		}
30425f7ddae6SRaffaele Recalcati
3043b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
3044b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
3045b9ea10d6SAndy Whitcroft
3046032a4c0fSJoe Perches# check indentation of any line with a bare else
3047840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
3048032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
3049032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3050032a4c0fSJoe Perches			my $tabs = length($1) + 1;
3051840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3052840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3053840080a0SJoe Perches			     defined $lines[$linenr] &&
3054840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
3055032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
3056032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
3057032a4c0fSJoe Perches			}
3058032a4c0fSJoe Perches		}
3059032a4c0fSJoe Perches
3060c00df19aSJoe Perches# check indentation of a line with a break;
3061c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
3062c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3063c00df19aSJoe Perches			my $tabs = $1;
3064c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3065c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
3066c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
3067c00df19aSJoe Perches			}
3068c00df19aSJoe Perches		}
3069c00df19aSJoe Perches
30701ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
30711ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
30721ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
30731ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
30741ba8dfd1SKees Cook		}
30751ba8dfd1SKees Cook
3076c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3077cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3078000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3079000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3080c2fdda0dSAndy Whitcroft		}
308122f2a2efSAndy Whitcroft
308242e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
308342e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
308442e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3085000d1cc1SJoe Perches			ERROR("CSYNC",
3086000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
308742e41c54SMike Frysinger		}
308842e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
308942e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3090000d1cc1SJoe Perches			ERROR("SSYNC",
3091000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
309242e41c54SMike Frysinger		}
309342e41c54SMike Frysinger
309456e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
309556e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
309656e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
309756e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
309856e77d70SJoe Perches		}
309956e77d70SJoe Perches
31009c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
31012b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
31022b474a1aSAndy Whitcroft		    $realline_next);
31033e469cdcSAndy Whitcroft#print "LINE<$line>\n";
31043e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
31051b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3106170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3107f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3108171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3109171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3110171ae1a4SAndy Whitcroft
31113e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
31123e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
31133e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
31143e469cdcSAndy Whitcroft			# until we hit end of it.
31153e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
31163e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
31173e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
31183e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
31193e469cdcSAndy Whitcroft			}
3120f74bd194SAndy Whitcroft
31212b474a1aSAndy Whitcroft			# Find the real next line.
31222b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
31232b474a1aSAndy Whitcroft			if (defined $realline_next &&
31242b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
31252b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
31262b474a1aSAndy Whitcroft				$realline_next++;
31272b474a1aSAndy Whitcroft			}
31282b474a1aSAndy Whitcroft
3129171ae1a4SAndy Whitcroft			my $s = $stat;
3130171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3131cf655043SAndy Whitcroft
3132c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3133171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3134c2fdda0dSAndy Whitcroft
3135c2fdda0dSAndy Whitcroft			# Ignore functions being called
3136171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3137c2fdda0dSAndy Whitcroft
3138463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3139463f2864SAndy Whitcroft
3140c45dcabdSAndy Whitcroft			# declarations always start with types
3141d2506586SAndy 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) {
3142c45dcabdSAndy Whitcroft				my $type = $1;
3143c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3144c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3145c45dcabdSAndy Whitcroft
31466c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3147a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3148c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3149c2fdda0dSAndy Whitcroft			}
31508905a67cSAndy Whitcroft
31516c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
315265863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3153c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
31549c0ca6f9SAndy Whitcroft			}
31558905a67cSAndy Whitcroft
31568905a67cSAndy Whitcroft			# Check for any sort of function declaration.
31578905a67cSAndy Whitcroft			# int foo(something bar, other baz);
31588905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3159171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
31608905a67cSAndy Whitcroft				my ($name_len) = length($1);
31618905a67cSAndy Whitcroft
3162cf655043SAndy Whitcroft				my $ctx = $s;
3163773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
31648905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3165cf655043SAndy Whitcroft
31668905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3167c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
31688905a67cSAndy Whitcroft
3169c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
31708905a67cSAndy Whitcroft					}
31718905a67cSAndy Whitcroft				}
31728905a67cSAndy Whitcroft			}
31738905a67cSAndy Whitcroft
31749c0ca6f9SAndy Whitcroft		}
31759c0ca6f9SAndy Whitcroft
317600df344fSAndy Whitcroft#
317700df344fSAndy Whitcroft# Checks which may be anchored in the context.
317800df344fSAndy Whitcroft#
317900df344fSAndy Whitcroft
318000df344fSAndy Whitcroft# Check for switch () and associated case and default
318100df344fSAndy Whitcroft# statements should be at the same indent.
318200df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
318300df344fSAndy Whitcroft			my $err = '';
318400df344fSAndy Whitcroft			my $sep = '';
318500df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
318600df344fSAndy Whitcroft			shift(@ctx);
318700df344fSAndy Whitcroft			for my $ctx (@ctx) {
318800df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
318900df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
319000df344fSAndy Whitcroft							$indent != $cindent) {
319100df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
319200df344fSAndy Whitcroft					$sep = '';
319300df344fSAndy Whitcroft				} else {
319400df344fSAndy Whitcroft					$sep = "[...]\n";
319500df344fSAndy Whitcroft				}
319600df344fSAndy Whitcroft			}
319700df344fSAndy Whitcroft			if ($err ne '') {
3198000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3199000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3200de7d4f0eSAndy Whitcroft			}
3201de7d4f0eSAndy Whitcroft		}
3202de7d4f0eSAndy Whitcroft
3203de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3204de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
32050fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3206773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3207773647a0SAndy Whitcroft
32089c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
32098eef05ddSJoe Perches
32108eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
32118eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
32128eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
32138eef05ddSJoe Perches			}
32148eef05ddSJoe Perches
3215de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3216de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3217de7d4f0eSAndy Whitcroft
3218548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3219548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3220de7d4f0eSAndy Whitcroft
3221548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3222548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3223548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3224548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3225548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3226773647a0SAndy Whitcroft				$ctx_ln++;
3227773647a0SAndy Whitcroft			}
3228548596d5SAndy Whitcroft
322953210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
323053210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3231773647a0SAndy Whitcroft
3232773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3233000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3234000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
323501464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
323600df344fSAndy Whitcroft			}
3237773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3238773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3239773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3240773647a0SAndy Whitcroft			{
32419c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
32429c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3243000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3244000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
324501464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
32469c0ca6f9SAndy Whitcroft				}
32479c0ca6f9SAndy Whitcroft			}
324800df344fSAndy Whitcroft		}
324900df344fSAndy Whitcroft
32504d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
32510fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
32523e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
32533e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
32543e469cdcSAndy Whitcroft					if (!defined $stat);
32554d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
32564d001e4dSAndy Whitcroft
32574d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
32584d001e4dSAndy Whitcroft
32599f5af480SJoe Perches			# remove inline comments
32609f5af480SJoe Perches			$s =~ s/$;/ /g;
32619f5af480SJoe Perches			$c =~ s/$;/ /g;
32624d001e4dSAndy Whitcroft
32634d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
32646f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
32656f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
32664d001e4dSAndy Whitcroft
32679f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
32689f5af480SJoe Perches			# none on the first line, and are going to readd them
32699f5af480SJoe Perches			# where necessary.
32709f5af480SJoe Perches			$s =~ s/\n./\n/gs;
32719f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
32729f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
32739f5af480SJoe Perches			}
32749f5af480SJoe Perches
32754d001e4dSAndy Whitcroft			# We want to check the first line inside the block
32764d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
32774d001e4dSAndy Whitcroft			#  1) any blank line termination
32784d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
32794d001e4dSAndy Whitcroft			#  3) any do (...) {
32804d001e4dSAndy Whitcroft			my $continuation = 0;
32814d001e4dSAndy Whitcroft			my $check = 0;
32824d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
32834d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
32844d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
32854d001e4dSAndy Whitcroft				$continuation = 1;
32864d001e4dSAndy Whitcroft			}
32879bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
32884d001e4dSAndy Whitcroft				$check = 1;
32894d001e4dSAndy Whitcroft				$cond_lines++;
32904d001e4dSAndy Whitcroft			}
32914d001e4dSAndy Whitcroft
32924d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
32934d001e4dSAndy Whitcroft			# preprocessor statement.
32944d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
32954d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
32964d001e4dSAndy Whitcroft				$check = 0;
32974d001e4dSAndy Whitcroft			}
32984d001e4dSAndy Whitcroft
32999bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3300740504c6SAndy Whitcroft			$continuation = 0;
33019bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
33029bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
33034d001e4dSAndy Whitcroft
3304f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3305f16fa28fSAndy Whitcroft				# is not linear.
3306f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3307f16fa28fSAndy Whitcroft					$check = 0;
3308f16fa28fSAndy Whitcroft				}
3309f16fa28fSAndy Whitcroft
33109bd49efeSAndy Whitcroft				# Ignore:
33119bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
33129bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
33139bd49efeSAndy Whitcroft				#  3) labels.
3314740504c6SAndy Whitcroft				if ($continuation ||
3315740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
33169bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
33179bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3318740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
331930dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
33209bd49efeSAndy Whitcroft						$cond_lines++;
33219bd49efeSAndy Whitcroft					}
33224d001e4dSAndy Whitcroft				}
332330dad6ebSAndy Whitcroft			}
33244d001e4dSAndy Whitcroft
33254d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
33264d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
33274d001e4dSAndy Whitcroft
33284d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
33294d001e4dSAndy Whitcroft			# this is not this patch's fault.
33304d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
33314d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
33324d001e4dSAndy Whitcroft				$check = 0;
33334d001e4dSAndy Whitcroft			}
33344d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
33354d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
33364d001e4dSAndy Whitcroft			}
33374d001e4dSAndy Whitcroft
33389bd49efeSAndy 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";
33394d001e4dSAndy Whitcroft
33409f5af480SJoe Perches			if ($check && $s ne '' &&
33419f5af480SJoe Perches			    (($sindent % 8) != 0 ||
33429f5af480SJoe Perches			     ($sindent < $indent) ||
33439f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3344000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3345000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
33464d001e4dSAndy Whitcroft			}
33474d001e4dSAndy Whitcroft		}
33484d001e4dSAndy Whitcroft
33496c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
33506c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
33511f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
33521f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
33536c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3354c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3355c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3356cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3357cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
33581f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3359c2fdda0dSAndy Whitcroft		}
33606c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
33616c72ffaaSAndy Whitcroft
336200df344fSAndy Whitcroft#ignore lines not being added
33633705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
336400df344fSAndy Whitcroft
3365a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3366c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3367a1ce18e4SJoe Perches			my $type = $1;
3368a1ce18e4SJoe Perches			my $var = $2;
3369207a8e84SJoe Perches			$var = "" if (!defined $var);
3370207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3371a1ce18e4SJoe Perches				my $sign = $1;
3372a1ce18e4SJoe Perches				my $pointer = $2;
3373a1ce18e4SJoe Perches
3374a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3375a1ce18e4SJoe Perches
3376a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3377a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3378a1ce18e4SJoe Perches				    $fix) {
3379a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3380207a8e84SJoe Perches					my $comp_pointer = $pointer;
3381207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3382207a8e84SJoe Perches					$decl .= $comp_pointer;
3383207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3384207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3385a1ce18e4SJoe Perches				}
3386a1ce18e4SJoe Perches			}
3387a1ce18e4SJoe Perches		}
3388a1ce18e4SJoe Perches
3389653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
33907429c690SAndy Whitcroft		if ($dbg_type) {
33917429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3392000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3393000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
33947429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3395000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3396000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
33977429c690SAndy Whitcroft			}
3398653d4876SAndy Whitcroft			next;
3399653d4876SAndy Whitcroft		}
3400a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3401a1ef277eSAndy Whitcroft		if ($dbg_attr) {
34029360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3403000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3404000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
34059360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3406000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3407000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3408a1ef277eSAndy Whitcroft			}
3409a1ef277eSAndy Whitcroft			next;
3410a1ef277eSAndy Whitcroft		}
3411653d4876SAndy Whitcroft
3412f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
341399423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
341499423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3415d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3416d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3417f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3418f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3419f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3420d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3421d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3422f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3423d752fcc8SJoe Perches				$fixedline = $line;
3424d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3425f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3426d752fcc8SJoe Perches			}
3427f0a594c1SAndy Whitcroft		}
3428f0a594c1SAndy Whitcroft
342900df344fSAndy Whitcroft#
343000df344fSAndy Whitcroft# Checks which are anchored on the added line.
343100df344fSAndy Whitcroft#
343200df344fSAndy Whitcroft
3433653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3434c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3435653d4876SAndy Whitcroft			my $path = $1;
3436653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3437000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3438495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3439495e9d84SJoe Perches			}
3440495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3441495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3442495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3443653d4876SAndy Whitcroft			}
3444653d4876SAndy Whitcroft		}
3445653d4876SAndy Whitcroft
344600df344fSAndy Whitcroft# no C99 // comments
344700df344fSAndy Whitcroft		if ($line =~ m{//}) {
34483705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
34493705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
34503705ce5bSJoe Perches			    $fix) {
3451194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
34523705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
34533705ce5bSJoe Perches					my $comment = trim($1);
3454194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
34553705ce5bSJoe Perches				}
34563705ce5bSJoe Perches			}
345700df344fSAndy Whitcroft		}
345800df344fSAndy Whitcroft		# Remove C99 comments.
34590a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
34606c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
34610a920b5bSAndy Whitcroft
34622b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
34632b474a1aSAndy Whitcroft# the whole statement.
34642b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
34652b474a1aSAndy Whitcroft		if (defined $realline_next &&
34662b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
34672b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
34682b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
34692b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
34703cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
34713cbf62dfSAndy Whitcroft			# a prefix:
34723cbf62dfSAndy Whitcroft			#   XXX(foo);
34733cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3474653d4876SAndy Whitcroft			my $name = $1;
347587a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
34763cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
34773cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
34783cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
34793cbf62dfSAndy Whitcroft
34803cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
34812b474a1aSAndy Whitcroft				\n.}\s*$|
348248012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
348348012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
348448012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
34852b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
34862b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
348748012058SAndy Whitcroft			    )/x) {
34882b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
34892b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
34902b474a1aSAndy Whitcroft			} else {
34912b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
34920a920b5bSAndy Whitcroft			}
34930a920b5bSAndy Whitcroft		}
34942b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
34952b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
34962b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
34972b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
34982b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
34992b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
35002b474a1aSAndy Whitcroft		}
35012b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
35022b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3503000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3504000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
35052b474a1aSAndy Whitcroft		}
35060a920b5bSAndy Whitcroft
35075150bda4SJoe Eloff# check for global initialisers.
35086d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3509d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
35106d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3511d5e616fcSJoe Perches			    $fix) {
35126d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3513d5e616fcSJoe Perches			}
3514f0a594c1SAndy Whitcroft		}
35150a920b5bSAndy Whitcroft# check for static initialisers.
35166d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3517d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
35186d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3519d5e616fcSJoe Perches				      $herecurr) &&
3520d5e616fcSJoe Perches			    $fix) {
35216d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3522d5e616fcSJoe Perches			}
35230a920b5bSAndy Whitcroft		}
35240a920b5bSAndy Whitcroft
35251813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
35261813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
35271813087dSJoe Perches			my $tmp = trim($1);
35281813087dSJoe Perches			WARN("MISORDERED_TYPE",
35291813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
35301813087dSJoe Perches		}
35311813087dSJoe Perches
3532cb710ecaSJoe Perches# check for static const char * arrays.
3533cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3534000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3535000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3536cb710ecaSJoe Perches				$herecurr);
3537cb710ecaSJoe Perches               }
3538cb710ecaSJoe Perches
3539cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3540cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3541000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3542000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3543cb710ecaSJoe Perches				$herecurr);
3544cb710ecaSJoe Perches               }
3545cb710ecaSJoe Perches
3546ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3547ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3548ab7e23f3SJoe Perches			my $found = $1;
3549ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3550ab7e23f3SJoe Perches				WARN("CONST_CONST",
3551ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3552ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3553ab7e23f3SJoe Perches				WARN("CONST_CONST",
3554ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3555ab7e23f3SJoe Perches			}
3556ab7e23f3SJoe Perches		}
3557ab7e23f3SJoe Perches
35589b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
35599b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
35609b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
35619b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
35629b0fa60dSJoe Perches				$herecurr);
35639b0fa60dSJoe Perches               }
35649b0fa60dSJoe Perches
3565b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3566b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3567b598b670SJoe Perches			my $array = $1;
3568b598b670SJoe 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*\))@) {
3569b598b670SJoe Perches				my $array_div = $1;
3570b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3571b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3572b598b670SJoe Perches				    $fix) {
3573b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3574b598b670SJoe Perches				}
3575b598b670SJoe Perches			}
3576b598b670SJoe Perches		}
3577b598b670SJoe Perches
3578b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3579b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3580b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3581b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3582b36190c5SJoe Perches			    $fix) {
3583194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3584b36190c5SJoe Perches			}
3585b36190c5SJoe Perches		}
3586b36190c5SJoe Perches
3587653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3588653d4876SAndy Whitcroft# make sense.
3589653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
35908054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3591c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
35928ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3593653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3594000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3595000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
35960a920b5bSAndy Whitcroft		}
35970a920b5bSAndy Whitcroft
35980a920b5bSAndy Whitcroft# * goes on variable not on type
359965863862SAndy Whitcroft		# (char*[ const])
3600bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3601bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
36023705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3603d8aaf121SAndy Whitcroft
360465863862SAndy Whitcroft			# Should start with a space.
360565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
360665863862SAndy Whitcroft			# Should not end with a space.
360765863862SAndy Whitcroft			$to =~ s/\s+$//;
360865863862SAndy Whitcroft			# '*'s should not have spaces between.
3609f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
361065863862SAndy Whitcroft			}
3611d8aaf121SAndy Whitcroft
36123705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
361365863862SAndy Whitcroft			if ($from ne $to) {
36143705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
36153705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
36163705ce5bSJoe Perches				    $fix) {
36173705ce5bSJoe Perches					my $sub_from = $ident;
36183705ce5bSJoe Perches					my $sub_to = $ident;
36193705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3620194f66fcSJoe Perches					$fixed[$fixlinenr] =~
36213705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
36223705ce5bSJoe Perches				}
362365863862SAndy Whitcroft			}
3624bfcb2cc7SAndy Whitcroft		}
3625bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3626bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
36273705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3628d8aaf121SAndy Whitcroft
362965863862SAndy Whitcroft			# Should start with a space.
363065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
363165863862SAndy Whitcroft			# Should not end with a space.
363265863862SAndy Whitcroft			$to =~ s/\s+$//;
363365863862SAndy Whitcroft			# '*'s should not have spaces between.
3634f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
363565863862SAndy Whitcroft			}
363665863862SAndy Whitcroft			# Modifiers should have spaces.
363765863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
363865863862SAndy Whitcroft
36393705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3640667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
36413705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
36423705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
36433705ce5bSJoe Perches				    $fix) {
36443705ce5bSJoe Perches
36453705ce5bSJoe Perches					my $sub_from = $match;
36463705ce5bSJoe Perches					my $sub_to = $match;
36473705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3648194f66fcSJoe Perches					$fixed[$fixlinenr] =~
36493705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
36503705ce5bSJoe Perches				}
365165863862SAndy Whitcroft			}
36520a920b5bSAndy Whitcroft		}
36530a920b5bSAndy Whitcroft
36549d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
36559d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
36569d3e3c70SJoe Perches			my $msg_type = \&WARN;
36579d3e3c70SJoe Perches			$msg_type = \&CHK if ($file);
36589d3e3c70SJoe Perches			&{$msg_type}("AVOID_BUG",
36599d3e3c70SJoe Perches				     "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
36609d3e3c70SJoe Perches		}
36610a920b5bSAndy Whitcroft
36629d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
36638905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3664000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3665000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
36668905a67cSAndy Whitcroft		}
36678905a67cSAndy Whitcroft
366817441227SJoe Perches# check for uses of printk_ratelimit
366917441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3670000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3671000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
367217441227SJoe Perches		}
367317441227SJoe Perches
367400df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
367500df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
367600df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
367725985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
367800df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3679f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
368000df344fSAndy Whitcroft			my $ok = 0;
368100df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
368200df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
368325985edcSLucas De Marchi				# we have a preceding printk if it ends
368400df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
368500df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
368600df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
368700df344fSAndy Whitcroft						$ok = 1;
368800df344fSAndy Whitcroft					}
368900df344fSAndy Whitcroft					last;
369000df344fSAndy Whitcroft				}
369100df344fSAndy Whitcroft			}
369200df344fSAndy Whitcroft			if ($ok == 0) {
3693000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3694000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
36950a920b5bSAndy Whitcroft			}
369600df344fSAndy Whitcroft		}
36970a920b5bSAndy Whitcroft
3698243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3699243f3803SJoe Perches			my $orig = $1;
3700243f3803SJoe Perches			my $level = lc($orig);
3701243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
37028f26b837SJoe Perches			my $level2 = $level;
37038f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3704243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3705daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3706243f3803SJoe Perches		}
3707243f3803SJoe Perches
3708243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3709d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3710d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3711d5e616fcSJoe Perches			    $fix) {
3712194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3713d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3714d5e616fcSJoe Perches			}
3715243f3803SJoe Perches		}
3716243f3803SJoe Perches
3717dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3718dc139313SJoe Perches			my $orig = $1;
3719dc139313SJoe Perches			my $level = lc($orig);
3720dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3721dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3722dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3723dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3724dc139313SJoe Perches		}
3725dc139313SJoe Perches
372691c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
372791c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
372891c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
372991c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
373091c9afafSAndy Lutomirski			WARN("ENOSYS",
373191c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
373291c9afafSAndy Lutomirski		}
373391c9afafSAndy Lutomirski
3734653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3735653d4876SAndy Whitcroft# or if closed on same line
37368d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
37374e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
37388d182478SJoe Perches			if (ERROR("OPEN_BRACE",
37398d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
37408d182478SJoe Perches			    $fix) {
37418d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
37428d182478SJoe Perches				my $fixed_line = $rawline;
37438d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
37448d182478SJoe Perches				my $line1 = $1;
37458d182478SJoe Perches				my $line2 = $2;
37468d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
37478d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
37488d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
37498d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
37508d182478SJoe Perches				}
37518d182478SJoe Perches			}
37520a920b5bSAndy Whitcroft		}
3753653d4876SAndy Whitcroft
37548905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
37558905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
37568905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
37578d182478SJoe Perches			if (ERROR("OPEN_BRACE",
37588d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
37598d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
37608d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
37618d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
37628d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
37638d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
37648d182478SJoe Perches				$fixedline = $rawline;
37658d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
37668d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
37678d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
37688d182478SJoe Perches				}
37698d182478SJoe Perches			}
37708905a67cSAndy Whitcroft		}
37718905a67cSAndy Whitcroft
37720c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
37733705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
37743705ce5bSJoe Perches			if (WARN("SPACING",
37753705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
37763705ce5bSJoe Perches			    $fix) {
3777194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37783705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
37793705ce5bSJoe Perches			}
37800c73b4ebSAndy Whitcroft		}
37810c73b4ebSAndy Whitcroft
378231070b5dSJoe Perches# Function pointer declarations
378331070b5dSJoe Perches# check spacing between type, funcptr, and args
378431070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
378591f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
378631070b5dSJoe Perches			my $declare = $1;
378731070b5dSJoe Perches			my $pre_pointer_space = $2;
378831070b5dSJoe Perches			my $post_pointer_space = $3;
378931070b5dSJoe Perches			my $funcname = $4;
379031070b5dSJoe Perches			my $post_funcname_space = $5;
379131070b5dSJoe Perches			my $pre_args_space = $6;
379231070b5dSJoe Perches
379391f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
379491f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
379591f72e9cSJoe Perches# don't need a space so don't warn for those.
379691f72e9cSJoe Perches			my $post_declare_space = "";
379791f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
379891f72e9cSJoe Perches				$post_declare_space = $1;
379991f72e9cSJoe Perches				$declare = rtrim($declare);
380091f72e9cSJoe Perches			}
380191f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
380231070b5dSJoe Perches				WARN("SPACING",
380331070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
380491f72e9cSJoe Perches				$post_declare_space = " ";
380531070b5dSJoe Perches			}
380631070b5dSJoe Perches
380731070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
380891f72e9cSJoe Perches# This test is not currently implemented because these declarations are
380991f72e9cSJoe Perches# equivalent to
381091f72e9cSJoe Perches#	int  foo(int bar, ...)
381191f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
381291f72e9cSJoe Perches#
381391f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
381491f72e9cSJoe Perches#				WARN("SPACING",
381591f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
381691f72e9cSJoe Perches#			}
381731070b5dSJoe Perches
381831070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
381931070b5dSJoe Perches			if (defined $pre_pointer_space &&
382031070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
382131070b5dSJoe Perches				WARN("SPACING",
382231070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
382331070b5dSJoe Perches			}
382431070b5dSJoe Perches
382531070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
382631070b5dSJoe Perches			if (defined $post_pointer_space &&
382731070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
382831070b5dSJoe Perches				WARN("SPACING",
382931070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
383031070b5dSJoe Perches			}
383131070b5dSJoe Perches
383231070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
383331070b5dSJoe Perches			if (defined $post_funcname_space &&
383431070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
383531070b5dSJoe Perches				WARN("SPACING",
383631070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
383731070b5dSJoe Perches			}
383831070b5dSJoe Perches
383931070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
384031070b5dSJoe Perches			if (defined $pre_args_space &&
384131070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
384231070b5dSJoe Perches				WARN("SPACING",
384331070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
384431070b5dSJoe Perches			}
384531070b5dSJoe Perches
384631070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3847194f66fcSJoe Perches				$fixed[$fixlinenr] =~
384891f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
384931070b5dSJoe Perches			}
385031070b5dSJoe Perches		}
385131070b5dSJoe Perches
38528d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
38538d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3854fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3855fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
38568d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
38578d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
38588d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3859fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3860daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
38613705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
38623705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
38633705ce5bSJoe Perches				    $fix) {
3864194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
38653705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
38663705ce5bSJoe Perches				}
38678d31cfceSAndy Whitcroft			}
38688d31cfceSAndy Whitcroft		}
38698d31cfceSAndy Whitcroft
3870f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
38716c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3872c2fdda0dSAndy Whitcroft			my $name = $1;
3873773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3874773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3875c2fdda0dSAndy Whitcroft
3876c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3877773647a0SAndy Whitcroft			if ($name =~ /^(?:
3878773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3879773647a0SAndy Whitcroft				volatile|__volatile__|
3880773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3881773647a0SAndy Whitcroft				asm|__asm__)$/x)
3882773647a0SAndy Whitcroft			{
3883c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3884c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3885c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3886c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3887773647a0SAndy Whitcroft
3888773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3889c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3890c2fdda0dSAndy Whitcroft
3891c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3892c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3893773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3894c2fdda0dSAndy Whitcroft
3895c2fdda0dSAndy Whitcroft			} else {
38963705ce5bSJoe Perches				if (WARN("SPACING",
38973705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
38983705ce5bSJoe Perches					     $fix) {
3899194f66fcSJoe Perches					$fixed[$fixlinenr] =~
39003705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
39013705ce5bSJoe Perches				}
3902f0a594c1SAndy Whitcroft			}
39036c72ffaaSAndy Whitcroft		}
39049a4cad4eSEric Nelson
3905653d4876SAndy Whitcroft# Check operator spacing.
39060a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
39073705ce5bSJoe Perches			my $fixed_line = "";
39083705ce5bSJoe Perches			my $line_fixed = 0;
39093705ce5bSJoe Perches
39109c0ca6f9SAndy Whitcroft			my $ops = qr{
39119c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
39129c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
39139c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
39141f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
391584731623SJoe Perches				\?:|\?|:
39169c0ca6f9SAndy Whitcroft			}x;
3917cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
39183705ce5bSJoe Perches
39193705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
39203705ce5bSJoe Perches##			foreach my $el (@elements) {
39213705ce5bSJoe Perches##				print("el: <$el>\n");
39223705ce5bSJoe Perches##			}
39233705ce5bSJoe Perches
39243705ce5bSJoe Perches			my @fix_elements = ();
392500df344fSAndy Whitcroft			my $off = 0;
39266c72ffaaSAndy Whitcroft
39273705ce5bSJoe Perches			foreach my $el (@elements) {
39283705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
39293705ce5bSJoe Perches				$off += length($el);
39303705ce5bSJoe Perches			}
39313705ce5bSJoe Perches
39323705ce5bSJoe Perches			$off = 0;
39333705ce5bSJoe Perches
39346c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3935b34c648bSJoe Perches			my $last_after = -1;
39366c72ffaaSAndy Whitcroft
39370a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
39383705ce5bSJoe Perches
39393705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
39403705ce5bSJoe Perches
39413705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
39423705ce5bSJoe Perches
39434a0df2efSAndy Whitcroft				$off += length($elements[$n]);
39444a0df2efSAndy Whitcroft
394525985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3946773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3947773647a0SAndy Whitcroft				my $cc = '';
3948773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3949773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3950773647a0SAndy Whitcroft				}
3951773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3952773647a0SAndy Whitcroft
39534a0df2efSAndy Whitcroft				my $a = '';
39544a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
39554a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3956cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
39574a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
39584a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3959773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
39604a0df2efSAndy Whitcroft
39610a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
39624a0df2efSAndy Whitcroft
39634a0df2efSAndy Whitcroft				my $c = '';
39640a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
39654a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
39664a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3967cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
39684a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
39694a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
39708b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
39714a0df2efSAndy Whitcroft				} else {
39724a0df2efSAndy Whitcroft					$c = 'E';
39730a920b5bSAndy Whitcroft				}
39740a920b5bSAndy Whitcroft
39754a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
39764a0df2efSAndy Whitcroft
39774a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
39784a0df2efSAndy Whitcroft
39796c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3980de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
39810a920b5bSAndy Whitcroft
398274048ed8SAndy Whitcroft				# Pull out the value of this operator.
39836c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
39840a920b5bSAndy Whitcroft
39851f65f947SAndy Whitcroft				# Get the full operator variant.
39861f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
39871f65f947SAndy Whitcroft
398813214adfSAndy Whitcroft				# Ignore operators passed as parameters.
398913214adfSAndy Whitcroft				if ($op_type ne 'V' &&
3990d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
399113214adfSAndy Whitcroft
3992cf655043SAndy Whitcroft#				# Ignore comments
3993cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
399413214adfSAndy Whitcroft
3995d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
399613214adfSAndy Whitcroft				} elsif ($op eq ';') {
3997cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3998cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
39993705ce5bSJoe Perches						if (ERROR("SPACING",
40003705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
4001b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
40023705ce5bSJoe Perches							$line_fixed = 1;
40033705ce5bSJoe Perches						}
4004d8aaf121SAndy Whitcroft					}
4005d8aaf121SAndy Whitcroft
4006d8aaf121SAndy Whitcroft				# // is a comment
4007d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
40080a920b5bSAndy Whitcroft
4009b00e4814SJoe Perches				#   :   when part of a bitfield
4010b00e4814SJoe Perches				} elsif ($opv eq ':B') {
4011b00e4814SJoe Perches					# skip the bitfield test for now
4012b00e4814SJoe Perches
40131f65f947SAndy Whitcroft				# No spaces for:
40141f65f947SAndy Whitcroft				#   ->
4015b00e4814SJoe Perches				} elsif ($op eq '->') {
40164a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
40173705ce5bSJoe Perches						if (ERROR("SPACING",
40183705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
4019b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
40203705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
40213705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
40223705ce5bSJoe Perches							}
4023b34c648bSJoe Perches							$line_fixed = 1;
40243705ce5bSJoe Perches						}
40250a920b5bSAndy Whitcroft					}
40260a920b5bSAndy Whitcroft
40272381097bSJoe Perches				# , must not have a space before and must have a space on the right.
40280a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
40292381097bSJoe Perches					my $rtrim_before = 0;
40302381097bSJoe Perches					my $space_after = 0;
40312381097bSJoe Perches					if ($ctx =~ /Wx./) {
40322381097bSJoe Perches						if (ERROR("SPACING",
40332381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
40342381097bSJoe Perches							$line_fixed = 1;
40352381097bSJoe Perches							$rtrim_before = 1;
40362381097bSJoe Perches						}
40372381097bSJoe Perches					}
4038cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
40393705ce5bSJoe Perches						if (ERROR("SPACING",
40403705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
40413705ce5bSJoe Perches							$line_fixed = 1;
4042b34c648bSJoe Perches							$last_after = $n;
40432381097bSJoe Perches							$space_after = 1;
40442381097bSJoe Perches						}
40452381097bSJoe Perches					}
40462381097bSJoe Perches					if ($rtrim_before || $space_after) {
40472381097bSJoe Perches						if ($rtrim_before) {
40482381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
40492381097bSJoe Perches						} else {
40502381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
40512381097bSJoe Perches						}
40522381097bSJoe Perches						if ($space_after) {
40532381097bSJoe Perches							$good .= " ";
40543705ce5bSJoe Perches						}
40550a920b5bSAndy Whitcroft					}
40560a920b5bSAndy Whitcroft
40579c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
405874048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
40599c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
40609c0ca6f9SAndy Whitcroft
40619c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
40629c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
40639c0ca6f9SAndy Whitcroft				# unary operator, or a cast
40649c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
406574048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
40660d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4067cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
40683705ce5bSJoe Perches						if (ERROR("SPACING",
40693705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4070b34c648bSJoe Perches							if ($n != $last_after + 2) {
4071b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
40723705ce5bSJoe Perches								$line_fixed = 1;
40733705ce5bSJoe Perches							}
40740a920b5bSAndy Whitcroft						}
4075b34c648bSJoe Perches					}
4076a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4077171ae1a4SAndy Whitcroft						# A unary '*' may be const
4078171ae1a4SAndy Whitcroft
4079171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
40803705ce5bSJoe Perches						if (ERROR("SPACING",
40813705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4082b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
40833705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
40843705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
40853705ce5bSJoe Perches							}
4086b34c648bSJoe Perches							$line_fixed = 1;
40873705ce5bSJoe Perches						}
40880a920b5bSAndy Whitcroft					}
40890a920b5bSAndy Whitcroft
40900a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
40910a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4092773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
40933705ce5bSJoe Perches						if (ERROR("SPACING",
40943705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4095b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
40963705ce5bSJoe Perches							$line_fixed = 1;
40973705ce5bSJoe Perches						}
40980a920b5bSAndy Whitcroft					}
4099773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4100773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
41013705ce5bSJoe Perches						if (ERROR("SPACING",
41023705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4103b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
41043705ce5bSJoe Perches							$line_fixed = 1;
41053705ce5bSJoe Perches						}
4106653d4876SAndy Whitcroft					}
4107773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
41083705ce5bSJoe Perches						if (ERROR("SPACING",
41093705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4110b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
41113705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
41123705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4113773647a0SAndy Whitcroft							}
4114b34c648bSJoe Perches							$line_fixed = 1;
41153705ce5bSJoe Perches						}
41163705ce5bSJoe Perches					}
41170a920b5bSAndy Whitcroft
41180a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
41199c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
41209c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
41219c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4122c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4123c2fdda0dSAndy Whitcroft					 $op eq '%')
41240a920b5bSAndy Whitcroft				{
4125d2e025f3SJoe Perches					if ($check) {
4126d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4127d2e025f3SJoe Perches							if (CHK("SPACING",
4128d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4129d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4130d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4131d2e025f3SJoe Perches								$line_fixed = 1;
4132d2e025f3SJoe Perches							}
4133d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4134d2e025f3SJoe Perches							if (CHK("SPACING",
4135d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4136d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4137d2e025f3SJoe Perches								$line_fixed = 1;
4138d2e025f3SJoe Perches							}
4139d2e025f3SJoe Perches						}
4140d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
41413705ce5bSJoe Perches						if (ERROR("SPACING",
41423705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4143b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4144b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4145b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4146b34c648bSJoe Perches							}
41473705ce5bSJoe Perches							$line_fixed = 1;
41483705ce5bSJoe Perches						}
41490a920b5bSAndy Whitcroft					}
41500a920b5bSAndy Whitcroft
41511f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
41521f65f947SAndy Whitcroft				# terminating a case value or a label.
41531f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
41541f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
41553705ce5bSJoe Perches						if (ERROR("SPACING",
41563705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4157b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
41583705ce5bSJoe Perches							$line_fixed = 1;
41593705ce5bSJoe Perches						}
41601f65f947SAndy Whitcroft					}
41611f65f947SAndy Whitcroft
41620a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4163cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
41641f65f947SAndy Whitcroft					my $ok = 0;
41651f65f947SAndy Whitcroft
416622f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
41671f65f947SAndy Whitcroft					if (($op eq '<' &&
41681f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
41691f65f947SAndy Whitcroft					    ($op eq '>' &&
41701f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
41711f65f947SAndy Whitcroft					{
41721f65f947SAndy Whitcroft					    	$ok = 1;
41731f65f947SAndy Whitcroft					}
41741f65f947SAndy Whitcroft
4175e0df7e1fSJoe Perches					# for asm volatile statements
4176e0df7e1fSJoe Perches					# ignore a colon with another
4177e0df7e1fSJoe Perches					# colon immediately before or after
4178e0df7e1fSJoe Perches					if (($op eq ':') &&
4179e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4180e0df7e1fSJoe Perches						$ok = 1;
4181e0df7e1fSJoe Perches					}
4182e0df7e1fSJoe Perches
418384731623SJoe Perches					# messages are ERROR, but ?: are CHK
41841f65f947SAndy Whitcroft					if ($ok == 0) {
418584731623SJoe Perches						my $msg_type = \&ERROR;
418684731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
418784731623SJoe Perches
418884731623SJoe Perches						if (&{$msg_type}("SPACING",
41893705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
4190b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4191b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4192b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4193b34c648bSJoe Perches							}
41943705ce5bSJoe Perches							$line_fixed = 1;
41953705ce5bSJoe Perches						}
41960a920b5bSAndy Whitcroft					}
419722f2a2efSAndy Whitcroft				}
41984a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
41993705ce5bSJoe Perches
42003705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
42013705ce5bSJoe Perches
42023705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
42030a920b5bSAndy Whitcroft			}
42043705ce5bSJoe Perches
42053705ce5bSJoe Perches			if (($#elements % 2) == 0) {
42063705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
42073705ce5bSJoe Perches			}
42083705ce5bSJoe Perches
4209194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4210194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
42113705ce5bSJoe Perches			}
42123705ce5bSJoe Perches
42133705ce5bSJoe Perches
42140a920b5bSAndy Whitcroft		}
42150a920b5bSAndy Whitcroft
4216786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4217d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4218786b6326SJoe Perches			if (WARN("SPACING",
4219786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4220786b6326SJoe Perches			    $fix) {
4221194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4222786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4223786b6326SJoe Perches			}
4224786b6326SJoe Perches		}
4225786b6326SJoe Perches
4226f0a594c1SAndy Whitcroft# check for multiple assignments
4227f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4228000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4229000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4230f0a594c1SAndy Whitcroft		}
4231f0a594c1SAndy Whitcroft
423222f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
423322f2a2efSAndy Whitcroft## # continuation.
423422f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
423522f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
423622f2a2efSAndy Whitcroft##
423722f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
423822f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
423922f2a2efSAndy Whitcroft## 			my $ln = $line;
424022f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
424122f2a2efSAndy Whitcroft## 			}
424222f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4243000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4244000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
424522f2a2efSAndy Whitcroft## 			}
424622f2a2efSAndy Whitcroft## 		}
4247f0a594c1SAndy Whitcroft
42480a920b5bSAndy Whitcroft#need space before brace following if, while, etc
42496b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
42504e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
42513705ce5bSJoe Perches			if (ERROR("SPACING",
42523705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
42533705ce5bSJoe Perches			    $fix) {
4254194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
42553705ce5bSJoe Perches			}
4256de7d4f0eSAndy Whitcroft		}
4257de7d4f0eSAndy Whitcroft
4258c4a62ef9SJoe Perches## # check for blank lines before declarations
4259c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4260c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4261c4a62ef9SJoe Perches##			WARN("SPACING",
4262c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4263c4a62ef9SJoe Perches##		}
4264c4a62ef9SJoe Perches##
4265c4a62ef9SJoe Perches
4266de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4267de7d4f0eSAndy Whitcroft# on the line
4268de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4269d5e616fcSJoe Perches			if (ERROR("SPACING",
4270d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4271d5e616fcSJoe Perches			    $fix) {
4272194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4273d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4274d5e616fcSJoe Perches			}
42750a920b5bSAndy Whitcroft		}
42760a920b5bSAndy Whitcroft
427722f2a2efSAndy Whitcroft# check spacing on square brackets
427822f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
42793705ce5bSJoe Perches			if (ERROR("SPACING",
42803705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
42813705ce5bSJoe Perches			    $fix) {
4282194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42833705ce5bSJoe Perches				    s/\[\s+/\[/;
42843705ce5bSJoe Perches			}
428522f2a2efSAndy Whitcroft		}
428622f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
42873705ce5bSJoe Perches			if (ERROR("SPACING",
42883705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
42893705ce5bSJoe Perches			    $fix) {
4290194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42913705ce5bSJoe Perches				    s/\s+\]/\]/;
42923705ce5bSJoe Perches			}
429322f2a2efSAndy Whitcroft		}
429422f2a2efSAndy Whitcroft
4295c45dcabdSAndy Whitcroft# check spacing on parentheses
42969c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
42979c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
42983705ce5bSJoe Perches			if (ERROR("SPACING",
42993705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
43003705ce5bSJoe Perches			    $fix) {
4301194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43023705ce5bSJoe Perches				    s/\(\s+/\(/;
43033705ce5bSJoe Perches			}
430422f2a2efSAndy Whitcroft		}
430513214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4306c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4307c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
43083705ce5bSJoe Perches			if (ERROR("SPACING",
43093705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
43103705ce5bSJoe Perches			    $fix) {
4311194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43123705ce5bSJoe Perches				    s/\s+\)/\)/;
43133705ce5bSJoe Perches			}
431422f2a2efSAndy Whitcroft		}
431522f2a2efSAndy Whitcroft
4316e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4317e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4318e2826fd0SJoe Perches
4319e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4320ea4acbb1SJoe Perches			my $var = $1;
4321ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4322ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4323ea4acbb1SJoe Perches			    $fix) {
4324ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4325ea4acbb1SJoe Perches			}
4326ea4acbb1SJoe Perches		}
4327ea4acbb1SJoe Perches
4328ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4329ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4330ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4331ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4332ea4acbb1SJoe Perches			my $var = $2;
4333ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4334ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4335ea4acbb1SJoe Perches			    $fix) {
4336ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4337ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4338ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4339ea4acbb1SJoe Perches			}
4340e2826fd0SJoe Perches		}
4341e2826fd0SJoe Perches
43420a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
43434a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
43440a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
43453705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
43463705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
43473705ce5bSJoe Perches			    $fix) {
4348194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43493705ce5bSJoe Perches				    s/^(.)\s+/$1/;
43503705ce5bSJoe Perches			}
43510a920b5bSAndy Whitcroft		}
43520a920b5bSAndy Whitcroft
43535b9553abSJoe Perches# return is not a function
4354507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4355c45dcabdSAndy Whitcroft			my $spacing = $1;
4356507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
43575b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
43585b9553abSJoe Perches				my $value = $1;
43595b9553abSJoe Perches				$value = deparenthesize($value);
43605b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4361000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4362000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
43635b9553abSJoe Perches				}
4364c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4365000d1cc1SJoe Perches				ERROR("SPACING",
4366000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4367c45dcabdSAndy Whitcroft			}
4368c45dcabdSAndy Whitcroft		}
4369507e5141SJoe Perches
4370b43ae21bSJoe Perches# unnecessary return in a void function
4371b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4372b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4373b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4374b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4375b43ae21bSJoe Perches		    $linenr >= 3 &&
4376b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4377b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
43789819cf25SJoe Perches			WARN("RETURN_VOID",
4379b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
43809819cf25SJoe Perches               }
43819819cf25SJoe Perches
4382189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4383189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4384189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4385189248d8SJoe Perches			my $openparens = $1;
4386189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4387189248d8SJoe Perches			my $msg = "";
4388189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4389189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4390189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4391189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4392189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4393189248d8SJoe Perches			}
4394189248d8SJoe Perches		}
4395189248d8SJoe Perches
4396c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4397c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4398c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4399c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4400c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4401c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4402c5595fa2SJoe Perches			my $lead = $1;
4403c5595fa2SJoe Perches			my $const = $2;
4404c5595fa2SJoe Perches			my $comp = $3;
4405c5595fa2SJoe Perches			my $to = $4;
4406c5595fa2SJoe Perches			my $newcomp = $comp;
4407f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4408c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4409c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4410c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4411c5595fa2SJoe Perches			    $fix) {
4412c5595fa2SJoe Perches				if ($comp eq "<") {
4413c5595fa2SJoe Perches					$newcomp = ">";
4414c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4415c5595fa2SJoe Perches					$newcomp = ">=";
4416c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4417c5595fa2SJoe Perches					$newcomp = "<";
4418c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4419c5595fa2SJoe Perches					$newcomp = "<=";
4420c5595fa2SJoe Perches				}
4421c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4422c5595fa2SJoe Perches			}
4423c5595fa2SJoe Perches		}
4424c5595fa2SJoe Perches
4425f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4426f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
442753a3c448SAndy Whitcroft			my $name = $1;
442853a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4429000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4430f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
443153a3c448SAndy Whitcroft			}
443253a3c448SAndy Whitcroft		}
4433c45dcabdSAndy Whitcroft
44340a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
44354a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
44363705ce5bSJoe Perches			if (ERROR("SPACING",
44373705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
44383705ce5bSJoe Perches			    $fix) {
4439194f66fcSJoe Perches				$fixed[$fixlinenr] =~
44403705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
44413705ce5bSJoe Perches			}
44420a920b5bSAndy Whitcroft		}
44430a920b5bSAndy Whitcroft
4444f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4445f5fe35ddSAndy Whitcroft# statements after the conditional.
4446170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
44473e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
44483e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
44493e469cdcSAndy Whitcroft					if (!defined $stat);
4450170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4451170d3a22SAndy Whitcroft						$remain_next, $off_next);
4452170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4453170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4454170d3a22SAndy Whitcroft
4455170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4456170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4457170d3a22SAndy Whitcroft				# then count those as offsets.
4458170d3a22SAndy Whitcroft				my ($whitespace) =
4459170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4460170d3a22SAndy Whitcroft				my $offset =
4461170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4462170d3a22SAndy Whitcroft
4463170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4464170d3a22SAndy Whitcroft								$offset} = 1;
4465170d3a22SAndy Whitcroft			}
4466170d3a22SAndy Whitcroft		}
4467170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4468c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4469170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4470171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
44718905a67cSAndy Whitcroft
4472b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4473000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4474000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
44758905a67cSAndy Whitcroft			}
44768905a67cSAndy Whitcroft
44778905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
44788905a67cSAndy Whitcroft			# conditional.
4479773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
44808905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
448113214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
448253210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
448353210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4484773647a0SAndy Whitcroft			{
4485bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4486bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4487bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
448842bdf74cSHidetoshi Seto				my $stat_real = '';
4489bb44ad39SAndy Whitcroft
449042bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
449142bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4492bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4493bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4494bb44ad39SAndy Whitcroft				}
4495bb44ad39SAndy Whitcroft
4496000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4497000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
44988905a67cSAndy Whitcroft			}
44998905a67cSAndy Whitcroft		}
45008905a67cSAndy Whitcroft
450113214adfSAndy Whitcroft# Check for bitwise tests written as boolean
450213214adfSAndy Whitcroft		if ($line =~ /
450313214adfSAndy Whitcroft			(?:
450413214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
450513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
450613214adfSAndy Whitcroft				(?:\&\&|\|\|)
450713214adfSAndy Whitcroft			|
450813214adfSAndy Whitcroft				(?:\&\&|\|\|)
450913214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
451013214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
451113214adfSAndy Whitcroft			)/x)
451213214adfSAndy Whitcroft		{
4513000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4514000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
451513214adfSAndy Whitcroft		}
451613214adfSAndy Whitcroft
45178905a67cSAndy Whitcroft# if and else should not have general statements after it
451813214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
451913214adfSAndy Whitcroft			my $s = $1;
452013214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
452113214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4522000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4523000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
45240a920b5bSAndy Whitcroft			}
452513214adfSAndy Whitcroft		}
452639667782SAndy Whitcroft# if should not continue a brace
452739667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4528000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4529048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
453039667782SAndy Whitcroft				$herecurr);
453139667782SAndy Whitcroft		}
4532a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4533a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4534a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
45353fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4536a1080bf8SAndy Whitcroft			\s*return\s+
4537a1080bf8SAndy Whitcroft		    )/xg)
4538a1080bf8SAndy Whitcroft		{
4539000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4540000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4541a1080bf8SAndy Whitcroft		}
45420a920b5bSAndy Whitcroft
45430a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
45440a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
45458b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
45460a920b5bSAndy Whitcroft		    $previndent == $indent) {
45478b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
45488b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
45498b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
45508b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
45518b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
45528b8856f4SJoe Perches				my $fixedline = $prevrawline;
45538b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
45548b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
45558b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
45568b8856f4SJoe Perches				}
45578b8856f4SJoe Perches				$fixedline = $rawline;
45588b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
45598b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
45608b8856f4SJoe Perches			}
45610a920b5bSAndy Whitcroft		}
45620a920b5bSAndy Whitcroft
45638b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4564c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4565c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4566c2fdda0dSAndy Whitcroft
4567c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4568c2fdda0dSAndy Whitcroft			# conditional.
4569773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4570c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4571c2fdda0dSAndy Whitcroft
4572c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
45738b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
45748b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
45758b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
45768b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
45778b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
45788b8856f4SJoe Perches					my $fixedline = $prevrawline;
45798b8856f4SJoe Perches					my $trailing = $rawline;
45808b8856f4SJoe Perches					$trailing =~ s/^\+//;
45818b8856f4SJoe Perches					$trailing = trim($trailing);
45828b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
45838b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
45848b8856f4SJoe Perches				}
4585c2fdda0dSAndy Whitcroft			}
4586c2fdda0dSAndy Whitcroft		}
4587c2fdda0dSAndy Whitcroft
458895e2c602SJoe Perches#Specific variable tests
4589323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4590323c1260SJoe Perches			my $var = $1;
459195e2c602SJoe Perches
459295e2c602SJoe Perches#gcc binary extension
459395e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4594d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4595d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4596d5e616fcSJoe Perches				    $fix) {
4597d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4598194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4599d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4600d5e616fcSJoe Perches				}
460195e2c602SJoe Perches			}
460295e2c602SJoe Perches
460395e2c602SJoe Perches#CamelCase
4604807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4605be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
460622735ce8SJoe Perches#Ignore Page<foo> variants
4607807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
460822735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4609f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4610f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4611f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
46127e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
46137e781f67SJoe Perches					my $word = $1;
46147e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4615d8b07710SJoe Perches					if ($check) {
4616d8b07710SJoe Perches						seed_camelcase_includes();
4617d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4618d8b07710SJoe Perches							seed_camelcase_file($realfile);
4619d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4620d8b07710SJoe Perches						}
4621d8b07710SJoe Perches					}
46227e781f67SJoe Perches					if (!defined $camelcase{$word}) {
46237e781f67SJoe Perches						$camelcase{$word} = 1;
4624be79794bSJoe Perches						CHK("CAMELCASE",
46257e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
46267e781f67SJoe Perches					}
4627323c1260SJoe Perches				}
4628323c1260SJoe Perches			}
46293445686aSJoe Perches		}
46300a920b5bSAndy Whitcroft
46310a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4632d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4633d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4634d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4635d5e616fcSJoe Perches			    $fix) {
4636194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4637d5e616fcSJoe Perches			}
46380a920b5bSAndy Whitcroft		}
46390a920b5bSAndy Whitcroft
46400e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
46410e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4642c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4643e09dec48SAndy Whitcroft			my $file = "$1.h";
4644e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4645e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4646e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
46477840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4648c45dcabdSAndy Whitcroft			{
46490e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
46500e212e0aSFabian Frederick				if ($asminclude > 0) {
4651e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4652000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4653000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4654e09dec48SAndy Whitcroft					} else {
4655000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4656000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4657e09dec48SAndy Whitcroft					}
46580a920b5bSAndy Whitcroft				}
46590a920b5bSAndy Whitcroft			}
46600e212e0aSFabian Frederick		}
46610a920b5bSAndy Whitcroft
4662653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4663653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4664cf655043SAndy Whitcroft# in a known good container
4665b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4666b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4667d8aaf121SAndy Whitcroft			my $ln = $linenr;
4668d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4669c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4670c45dcabdSAndy Whitcroft			my $ctx = '';
467108a2843eSJoe Perches			my $has_flow_statement = 0;
467208a2843eSJoe Perches			my $has_arg_concat = 0;
4673c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4674f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4675f74bd194SAndy Whitcroft			$ctx = $dstat;
4676c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4677a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4678c45dcabdSAndy Whitcroft
467908a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
468062e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
468108a2843eSJoe Perches
4682f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4683292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4684c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4685c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4686c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4687c45dcabdSAndy Whitcroft
4688c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4689bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4690bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
46916b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4692bf30d6edSAndy Whitcroft			{
4693c45dcabdSAndy Whitcroft			}
4694c45dcabdSAndy Whitcroft
4695e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
469633acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
469733acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4698e45bab8eSAndy Whitcroft			{
4699e45bab8eSAndy Whitcroft			}
4700e45bab8eSAndy Whitcroft
470142e15293SJoe Perches			# Make asm volatile uses seem like a generic function
470242e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
470342e15293SJoe Perches
4704c45dcabdSAndy Whitcroft			my $exceptions = qr{
4705c45dcabdSAndy Whitcroft				$Declare|
4706c45dcabdSAndy Whitcroft				module_param_named|
4707a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4708c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4709c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4710383099fdSAndy Whitcroft				__typeof__\(|
471122fd2d3eSStefani Seibold				union|
471222fd2d3eSStefani Seibold				struct|
4713ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
47146b10df42SVladimir Zapolskiy				^\"|\"$|
47156b10df42SVladimir Zapolskiy				^\[
4716c45dcabdSAndy Whitcroft			}x;
47175eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4718f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4719f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4720f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
47213cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4722356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4723f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4724f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4725e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
472672f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4727f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4728f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4729f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
47304e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4731f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4732c45dcabdSAndy Whitcroft			{
4733f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4734f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4735f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4736f74bd194SAndy Whitcroft
4737f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4738f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4739c45dcabdSAndy Whitcroft				}
4740c45dcabdSAndy Whitcroft
4741f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4742f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4743f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4744f74bd194SAndy Whitcroft				} else {
4745000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4746388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4747d8aaf121SAndy Whitcroft				}
47480a920b5bSAndy Whitcroft			}
47495023d347SJoe Perches
475008a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
475108a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
475208a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
475308a2843eSJoe Perches				my $herectx = $here . "\n";
475408a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
475508a2843eSJoe Perches
475608a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
475708a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
475808a2843eSJoe Perches				}
475908a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
476008a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
476108a2843eSJoe Perches			}
476208a2843eSJoe Perches
4763481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
47645023d347SJoe Perches
47655023d347SJoe Perches		} else {
47665023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4767481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4768481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
47695023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
47705023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
47715023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
47725023d347SJoe Perches			}
4773653d4876SAndy Whitcroft		}
47740a920b5bSAndy Whitcroft
4775b13edf7fSJoe Perches# do {} while (0) macro tests:
4776b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4777b13edf7fSJoe Perches# macro should not end with a semicolon
4778b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4779b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4780b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4781b13edf7fSJoe Perches			my $ln = $linenr;
4782b13edf7fSJoe Perches			my $cnt = $realcnt;
4783b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4784b13edf7fSJoe Perches			my $ctx = '';
4785b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4786b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4787b13edf7fSJoe Perches			$ctx = $dstat;
4788b13edf7fSJoe Perches
4789b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
47901b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4791b13edf7fSJoe Perches
4792b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4793b13edf7fSJoe Perches				my $stmts = $2;
4794b13edf7fSJoe Perches				my $semis = $3;
4795b13edf7fSJoe Perches
4796b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4797b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4798b13edf7fSJoe Perches				my $herectx = $here . "\n";
4799b13edf7fSJoe Perches
4800b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4801b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4802b13edf7fSJoe Perches				}
4803b13edf7fSJoe Perches
4804ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4805ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4806b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4807b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4808b13edf7fSJoe Perches				}
4809b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4810b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4811b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4812b13edf7fSJoe Perches				}
4813f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4814f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4815f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4816f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4817f5ef95b1SJoe Perches
4818f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4819f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4820f5ef95b1SJoe Perches				}
4821f5ef95b1SJoe Perches
4822f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4823f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4824b13edf7fSJoe Perches			}
4825b13edf7fSJoe Perches		}
4826b13edf7fSJoe Perches
4827080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4828080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4829080ba929SMike Frysinger#	.
4830080ba929SMike Frysinger#	ALIGN(...)
4831080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4832080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4833000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4834000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4835080ba929SMike Frysinger		}
4836080ba929SMike Frysinger
4837f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
483813214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
483913214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4840cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
484113214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4842cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4843cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4844aad4f614SJoe Perches				my @allowed = ();
4845aad4f614SJoe Perches				my $allow = 0;
484613214adfSAndy Whitcroft				my $seen = 0;
4847773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4848cf655043SAndy Whitcroft				my $ln = $linenr - 1;
484913214adfSAndy Whitcroft				for my $chunk (@chunks) {
485013214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
485113214adfSAndy Whitcroft
4852773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4853773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4854773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4855773647a0SAndy Whitcroft
4856aad4f614SJoe Perches					$allowed[$allow] = 0;
4857773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4858773647a0SAndy Whitcroft
4859773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4860773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4861773647a0SAndy Whitcroft
4862773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4863cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4864cf655043SAndy Whitcroft
4865773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
486613214adfSAndy Whitcroft
486713214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
486813214adfSAndy Whitcroft
4869aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4870cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4871cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4872aad4f614SJoe Perches						$allowed[$allow] = 1;
487313214adfSAndy Whitcroft					}
487413214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4875cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4876aad4f614SJoe Perches						$allowed[$allow] = 1;
487713214adfSAndy Whitcroft					}
4878cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4879cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4880aad4f614SJoe Perches						$allowed[$allow] = 1;
488113214adfSAndy Whitcroft					}
4882aad4f614SJoe Perches					$allow++;
488313214adfSAndy Whitcroft				}
4884aad4f614SJoe Perches				if ($seen) {
4885aad4f614SJoe Perches					my $sum_allowed = 0;
4886aad4f614SJoe Perches					foreach (@allowed) {
4887aad4f614SJoe Perches						$sum_allowed += $_;
4888aad4f614SJoe Perches					}
4889aad4f614SJoe Perches					if ($sum_allowed == 0) {
4890000d1cc1SJoe Perches						WARN("BRACES",
4891000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4892aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4893aad4f614SJoe Perches						 $seen != $allow) {
4894aad4f614SJoe Perches						CHK("BRACES",
4895aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4896aad4f614SJoe Perches					}
489713214adfSAndy Whitcroft				}
489813214adfSAndy Whitcroft			}
489913214adfSAndy Whitcroft		}
4900773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
490113214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4902cf655043SAndy Whitcroft			my $allowed = 0;
4903f0a594c1SAndy Whitcroft
4904cf655043SAndy Whitcroft			# Check the pre-context.
4905cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4906cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4907cf655043SAndy Whitcroft				$allowed = 1;
4908f0a594c1SAndy Whitcroft			}
4909773647a0SAndy Whitcroft
4910773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4911773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4912773647a0SAndy Whitcroft
4913cf655043SAndy Whitcroft			# Check the condition.
4914cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4915773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4916cf655043SAndy Whitcroft			if (defined $cond) {
4917773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4918cf655043SAndy Whitcroft			}
4919cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4920cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4921cf655043SAndy Whitcroft				$allowed = 1;
4922cf655043SAndy Whitcroft			}
4923cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4924cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4925cf655043SAndy Whitcroft				$allowed = 1;
4926cf655043SAndy Whitcroft			}
4927cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4928cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4929cf655043SAndy Whitcroft				$allowed = 1;
4930cf655043SAndy Whitcroft			}
4931cf655043SAndy Whitcroft			# Check the post-context.
4932cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4933cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4934cf655043SAndy Whitcroft				if (defined $cond) {
4935773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4936cf655043SAndy Whitcroft				}
4937cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4938cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4939cf655043SAndy Whitcroft					$allowed = 1;
4940cf655043SAndy Whitcroft				}
4941cf655043SAndy Whitcroft			}
4942cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
494369932487SJustin P. Mattock				my $herectx = $here . "\n";
4944f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4945cf655043SAndy Whitcroft
4946f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
494769932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4948cf655043SAndy Whitcroft				}
4949cf655043SAndy Whitcroft
4950000d1cc1SJoe Perches				WARN("BRACES",
4951000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4952f0a594c1SAndy Whitcroft			}
4953f0a594c1SAndy Whitcroft		}
4954f0a594c1SAndy Whitcroft
49550979ae66SJoe Perches# check for unnecessary blank lines around braces
495677b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4957f8e58219SJoe Perches			if (CHK("BRACES",
4958f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4959f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
4960f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4961f8e58219SJoe Perches			}
49620979ae66SJoe Perches		}
496377b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4964f8e58219SJoe Perches			if (CHK("BRACES",
4965f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4966f8e58219SJoe Perches			    $fix) {
4967f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4968f8e58219SJoe Perches			}
49690979ae66SJoe Perches		}
49700979ae66SJoe Perches
49714a0df2efSAndy Whitcroft# no volatiles please
49726c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
49736c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4974000d1cc1SJoe Perches			WARN("VOLATILE",
4975000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
49764a0df2efSAndy Whitcroft		}
49774a0df2efSAndy Whitcroft
49785e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
49795e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
49805e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
49815e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
498233acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
49835e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
49845e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
49855e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
49865e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
49875e4f6ba5SJoe Perches				     $fix &&
49885e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
49895e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
49905e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
49915e4f6ba5SJoe Perches				my $comma_close = "";
49925e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
49935e4f6ba5SJoe Perches					$comma_close = $1;
49945e4f6ba5SJoe Perches				}
49955e4f6ba5SJoe Perches
49965e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
49975e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49985e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
49995e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
50005e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
50015e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
50025e4f6ba5SJoe Perches				$fixedline = $rawline;
50035e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
50045e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
50055e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
50065e4f6ba5SJoe Perches				}
50075e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
50085e4f6ba5SJoe Perches			}
50095e4f6ba5SJoe Perches		}
50105e4f6ba5SJoe Perches
50115e4f6ba5SJoe Perches# check for missing a space in a string concatenation
50125e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
50135e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
50145e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
50155e4f6ba5SJoe Perches		}
50165e4f6ba5SJoe Perches
50175e4f6ba5SJoe Perches# check for spaces before a quoted newline
50185e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
50195e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
50205e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
50215e4f6ba5SJoe Perches			    $fix) {
50225e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
50235e4f6ba5SJoe Perches			}
50245e4f6ba5SJoe Perches
50255e4f6ba5SJoe Perches		}
50265e4f6ba5SJoe Perches
5027f17dba4fSJoe Perches# concatenated string without spaces between elements
502833acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
5029f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
5030f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
5031f17dba4fSJoe Perches		}
5032f17dba4fSJoe Perches
503390ad30e5SJoe Perches# uncoalesced string fragments
503433acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
503590ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
503690ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
503790ad30e5SJoe Perches		}
503890ad30e5SJoe Perches
50396e300757SJoe Perches# check for %L{u,d,i} and 0x%[udi] in strings
50405e4f6ba5SJoe Perches		my $string;
50415e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
50425e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
50435e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
50446e300757SJoe Perches			if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
50455e4f6ba5SJoe Perches				WARN("PRINTF_L",
50465e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
50475e4f6ba5SJoe Perches				last;
50485e4f6ba5SJoe Perches			}
50496e300757SJoe Perches			if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
50506e300757SJoe Perches				ERROR("PRINTF_0xDECIMAL",
50516e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
50526e300757SJoe Perches			}
50535e4f6ba5SJoe Perches		}
50545e4f6ba5SJoe Perches
50555e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
50565e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
50575e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
50585e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
50595e4f6ba5SJoe Perches		}
50605e4f6ba5SJoe Perches
506100df344fSAndy Whitcroft# warn about #if 0
5062c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
5063000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
5064000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
5065de7d4f0eSAndy Whitcroft				$herecurr);
50664a0df2efSAndy Whitcroft		}
50674a0df2efSAndy Whitcroft
506803df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
506903df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5070100425deSJoe Perches			my $tested = quotemeta($1);
5071100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5072100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5073100425deSJoe Perches				my $func = $1;
5074100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5075100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5076100425deSJoe Perches				    $fix) {
5077100425deSJoe Perches					my $do_fix = 1;
5078100425deSJoe Perches					my $leading_tabs = "";
5079100425deSJoe Perches					my $new_leading_tabs = "";
5080100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5081100425deSJoe Perches						$leading_tabs = $1;
5082100425deSJoe Perches					} else {
5083100425deSJoe Perches						$do_fix = 0;
5084100425deSJoe Perches					}
5085100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5086100425deSJoe Perches						$new_leading_tabs = $1;
5087100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5088100425deSJoe Perches							$do_fix = 0;
5089100425deSJoe Perches						}
5090100425deSJoe Perches					} else {
5091100425deSJoe Perches						$do_fix = 0;
5092100425deSJoe Perches					}
5093100425deSJoe Perches					if ($do_fix) {
5094100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5095100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5096100425deSJoe Perches					}
5097100425deSJoe Perches				}
50984c432a8fSGreg Kroah-Hartman			}
50994c432a8fSGreg Kroah-Hartman		}
5100f0a594c1SAndy Whitcroft
5101ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5102ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5103ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5104ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5105ebfdc409SJoe Perches		    $linenr > 3) {
5106ebfdc409SJoe Perches			my $testval = $2;
5107ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5108ebfdc409SJoe Perches
5109ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5110ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5111ebfdc409SJoe Perches
5112ebfdc409SJoe 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)/) {
5113ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5114ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5115ebfdc409SJoe Perches			}
5116ebfdc409SJoe Perches		}
5117ebfdc409SJoe Perches
5118f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5119dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5120f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5121f78d98f6SJoe Perches			my $level = $1;
5122f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5123f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5124f78d98f6SJoe Perches			    $fix) {
5125f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5126f78d98f6SJoe Perches			}
5127f78d98f6SJoe Perches		}
5128f78d98f6SJoe Perches
5129abb08a53SJoe Perches# check for mask then right shift without a parentheses
5130abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5131abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5132abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5133abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5134abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5135abb08a53SJoe Perches		}
5136abb08a53SJoe Perches
5137b75ac618SJoe Perches# check for pointer comparisons to NULL
5138b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
5139b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5140b75ac618SJoe Perches				my $val = $1;
5141b75ac618SJoe Perches				my $equal = "!";
5142b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5143b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5144b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5145b75ac618SJoe Perches					    $fix) {
5146b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5147b75ac618SJoe Perches				}
5148b75ac618SJoe Perches			}
5149b75ac618SJoe Perches		}
5150b75ac618SJoe Perches
51518716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
51528716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
51538716de38SJoe Perches			my $attr = $1;
51548716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
51558716de38SJoe Perches				my $ptr = $1;
51568716de38SJoe Perches				my $var = $2;
51578716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
51588716de38SJoe Perches				      ERROR("MISPLACED_INIT",
51598716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
51608716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
51618716de38SJoe Perches				      WARN("MISPLACED_INIT",
51628716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
51638716de38SJoe Perches				    $fix) {
5164194f66fcSJoe 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;
51658716de38SJoe Perches				}
51668716de38SJoe Perches			}
51678716de38SJoe Perches		}
51688716de38SJoe Perches
5169e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5170e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5171e970b884SJoe Perches			my $attr = $1;
5172e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5173e970b884SJoe Perches			my $attr_prefix = $1;
5174e970b884SJoe Perches			my $attr_type = $2;
5175e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5176e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5177e970b884SJoe Perches			    $fix) {
5178194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5179e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5180e970b884SJoe Perches			}
5181e970b884SJoe Perches		}
5182e970b884SJoe Perches
5183e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5184e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5185e970b884SJoe Perches			my $attr = $1;
5186e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5187e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5188e970b884SJoe Perches			    $fix) {
5189194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5190e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5191e970b884SJoe Perches				$lead = rtrim($1);
5192e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5193e970b884SJoe Perches				$lead = "${lead}const ";
5194194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5195e970b884SJoe Perches			}
5196e970b884SJoe Perches		}
5197e970b884SJoe Perches
5198c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5199c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5200c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5201c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5202c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5203c17893c7SJoe Perches			    $fix) {
5204c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5205c17893c7SJoe Perches			}
5206c17893c7SJoe Perches		}
5207c17893c7SJoe Perches
5208fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5209fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5210fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5211fbdb8138SJoe Perches			my $constant_func = $1;
5212fbdb8138SJoe Perches			my $func = $constant_func;
5213fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5214fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5215fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5216fbdb8138SJoe Perches			    $fix) {
5217194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5218fbdb8138SJoe Perches			}
5219fbdb8138SJoe Perches		}
5220fbdb8138SJoe Perches
52211a15a250SPatrick Pannuto# prefer usleep_range over udelay
522237581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
522343c1d77cSJoe Perches			my $delay = $1;
52241a15a250SPatrick Pannuto			# ignore udelay's < 10, however
522543c1d77cSJoe Perches			if (! ($delay < 10) ) {
5226000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
522743c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
522843c1d77cSJoe Perches			}
522943c1d77cSJoe Perches			if ($delay > 2000) {
523043c1d77cSJoe Perches				WARN("LONG_UDELAY",
523143c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
52321a15a250SPatrick Pannuto			}
52331a15a250SPatrick Pannuto		}
52341a15a250SPatrick Pannuto
523509ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
523609ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
523709ef8725SPatrick Pannuto			if ($1 < 20) {
5238000d1cc1SJoe Perches				WARN("MSLEEP",
523943c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
524009ef8725SPatrick Pannuto			}
524109ef8725SPatrick Pannuto		}
524209ef8725SPatrick Pannuto
524336ec1939SJoe Perches# check for comparisons of jiffies
524436ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
524536ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
524636ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
524736ec1939SJoe Perches		}
524836ec1939SJoe Perches
52499d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
52509d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
52519d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
52529d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
52539d7a34a5SJoe Perches		}
52549d7a34a5SJoe Perches
525500df344fSAndy Whitcroft# warn about #ifdefs in C files
5256c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
525700df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
525800df344fSAndy Whitcroft#			print "$herecurr";
525900df344fSAndy Whitcroft#			$clean = 0;
526000df344fSAndy Whitcroft#		}
526100df344fSAndy Whitcroft
526222f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5263c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
52643705ce5bSJoe Perches			if (ERROR("SPACING",
52653705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
52663705ce5bSJoe Perches			    $fix) {
5267194f66fcSJoe Perches				$fixed[$fixlinenr] =~
52683705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
52693705ce5bSJoe Perches			}
52703705ce5bSJoe Perches
527122f2a2efSAndy Whitcroft		}
527222f2a2efSAndy Whitcroft
52734a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5274171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5275171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
52764a0df2efSAndy Whitcroft			my $which = $1;
52774a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5278000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5279000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
52804a0df2efSAndy Whitcroft			}
52814a0df2efSAndy Whitcroft		}
52824a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5283402c2553SMichael S. Tsirkin
5284402c2553SMichael S. Tsirkin		my $barriers = qr{
5285402c2553SMichael S. Tsirkin			mb|
5286402c2553SMichael S. Tsirkin			rmb|
5287402c2553SMichael S. Tsirkin			wmb|
5288402c2553SMichael S. Tsirkin			read_barrier_depends
5289402c2553SMichael S. Tsirkin		}x;
5290402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5291402c2553SMichael S. Tsirkin			mb__before_atomic|
5292402c2553SMichael S. Tsirkin			mb__after_atomic|
5293402c2553SMichael S. Tsirkin			store_release|
5294402c2553SMichael S. Tsirkin			load_acquire|
5295402c2553SMichael S. Tsirkin			store_mb|
5296402c2553SMichael S. Tsirkin			(?:$barriers)
5297402c2553SMichael S. Tsirkin		}x;
5298402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5299402c2553SMichael S. Tsirkin			(?:$barriers)|
530043e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
530143e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5302402c2553SMichael S. Tsirkin		}x;
5303402c2553SMichael S. Tsirkin
5304402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
53054a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5306c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5307000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
53084a0df2efSAndy Whitcroft			}
53094a0df2efSAndy Whitcroft		}
53103ad81779SPaul E. McKenney
5311f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5312f4073b0fSMichael S. Tsirkin
5313f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5314f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5315f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5316f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5317f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5318f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5319f4073b0fSMichael S. Tsirkin		}
5320f4073b0fSMichael S. Tsirkin
5321cb426e99SJoe Perches# check for waitqueue_active without a comment.
5322cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5323cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5324cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5325cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5326cb426e99SJoe Perches			}
5327cb426e99SJoe Perches		}
53283ad81779SPaul E. McKenney
53293ad81779SPaul E. McKenney# Check for expedited grace periods that interrupt non-idle non-nohz
53303ad81779SPaul E. McKenney# online CPUs.  These expedited can therefore degrade real-time response
53313ad81779SPaul E. McKenney# if used carelessly, and should be avoided where not absolutely
53323ad81779SPaul E. McKenney# needed.  It is always OK to use synchronize_rcu_expedited() and
53333ad81779SPaul E. McKenney# synchronize_sched_expedited() at boot time (before real-time applications
53343ad81779SPaul E. McKenney# start) and in error situations where real-time response is compromised in
53353ad81779SPaul E. McKenney# any case.  Note that synchronize_srcu_expedited() does -not- interrupt
53363ad81779SPaul E. McKenney# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
53373ad81779SPaul E. McKenney# Of course, nothing comes for free, and srcu_read_lock() and
53383ad81779SPaul E. McKenney# srcu_read_unlock() do contain full memory barriers in payment for
53393ad81779SPaul E. McKenney# synchronize_srcu_expedited() non-interruption properties.
53403ad81779SPaul E. McKenney		if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
53413ad81779SPaul E. McKenney			WARN("EXPEDITED_RCU_GRACE_PERIOD",
53423ad81779SPaul E. McKenney			     "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
53433ad81779SPaul E. McKenney
53443ad81779SPaul E. McKenney		}
53453ad81779SPaul E. McKenney
53464a0df2efSAndy Whitcroft# check of hardware specific defines
5347c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5348000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5349000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
53500a920b5bSAndy Whitcroft		}
5351653d4876SAndy Whitcroft
5352d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
5353d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5354000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5355000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
5356d4977c78STobias Klauser		}
5357d4977c78STobias Klauser
5358de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5359de7d4f0eSAndy Whitcroft# storage class and type.
53609c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
53619c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5362000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5363000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5364de7d4f0eSAndy Whitcroft		}
5365de7d4f0eSAndy Whitcroft
53668905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
53672b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53682b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5369d5e616fcSJoe Perches			if (WARN("INLINE",
5370d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5371d5e616fcSJoe Perches			    $fix) {
5372194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5373d5e616fcSJoe Perches
5374d5e616fcSJoe Perches			}
53758905a67cSAndy Whitcroft		}
53768905a67cSAndy Whitcroft
53773d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
53782b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53792b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5380000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5381000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
53823d130fd0SJoe Perches		}
53833d130fd0SJoe Perches
538439b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
53852b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53862b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5387000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5388000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
538939b7e287SJoe Perches		}
539039b7e287SJoe Perches
53915f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
53922b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53932b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5394d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5395d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5396d5e616fcSJoe Perches			    $fix) {
5397194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5398d5e616fcSJoe Perches
5399d5e616fcSJoe Perches			}
54005f14d3bdSJoe Perches		}
54015f14d3bdSJoe Perches
54026061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
54032b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
54042b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5405d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5406d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5407d5e616fcSJoe Perches			    $fix) {
5408194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5409d5e616fcSJoe Perches			}
54106061d949SJoe Perches		}
54116061d949SJoe Perches
5412619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5413619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5414619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5415619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5416619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5417619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5418619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5419619a908aSJoe Perches		}
5420619a908aSJoe Perches
5421e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/
5422e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5423e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5424e6176fa4SJoe Perches			my $type = $1;
5425e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5426e6176fa4SJoe Perches				$type = $1;
5427e6176fa4SJoe Perches				my $kernel_type = 'u';
5428e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5429e6176fa4SJoe Perches				$type =~ /(\d+)/;
5430e6176fa4SJoe Perches				$kernel_type .= $1;
5431e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5432e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5433e6176fa4SJoe Perches				    $fix) {
5434e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5435e6176fa4SJoe Perches				}
5436e6176fa4SJoe Perches			}
5437e6176fa4SJoe Perches		}
5438e6176fa4SJoe Perches
5439938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5440938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5441938224b5SJoe Perches			my $cast = $1;
5442938224b5SJoe Perches			my $const = $2;
5443938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5444938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5445938224b5SJoe Perches			    $fix) {
5446938224b5SJoe Perches				my $suffix = "";
5447938224b5SJoe Perches				my $newconst = $const;
5448938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5449938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5450938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5451938224b5SJoe Perches					$suffix .= 'LL';
5452938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5453938224b5SJoe Perches					$suffix .= 'L';
5454938224b5SJoe Perches				}
5455938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5456938224b5SJoe Perches			}
5457938224b5SJoe Perches		}
5458938224b5SJoe Perches
54598f53a9b8SJoe Perches# check for sizeof(&)
54608f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5461000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5462000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
54638f53a9b8SJoe Perches		}
54648f53a9b8SJoe Perches
546566c80b60SJoe Perches# check for sizeof without parenthesis
546666c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5467d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5468d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5469d5e616fcSJoe Perches			    $fix) {
5470194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5471d5e616fcSJoe Perches			}
547266c80b60SJoe Perches		}
547366c80b60SJoe Perches
547488982feaSJoe Perches# check for struct spinlock declarations
547588982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
547688982feaSJoe Perches			WARN("USE_SPINLOCK_T",
547788982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
547888982feaSJoe Perches		}
547988982feaSJoe Perches
5480a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
548106668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5482a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5483caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5484caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5485d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5486d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5487d5e616fcSJoe Perches				    $fix) {
5488194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5489d5e616fcSJoe Perches				}
5490a6962d72SJoe Perches			}
5491a6962d72SJoe Perches		}
5492a6962d72SJoe Perches
5493554e165cSAndy Whitcroft# Check for misused memsets
5494d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5495d1fe9c09SJoe Perches		    defined $stat &&
54969e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5497554e165cSAndy Whitcroft
5498d7c76ba7SJoe Perches			my $ms_addr = $2;
5499d1fe9c09SJoe Perches			my $ms_val = $7;
5500d1fe9c09SJoe Perches			my $ms_size = $12;
5501d7c76ba7SJoe Perches
5502554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5503554e165cSAndy Whitcroft				ERROR("MEMSET",
5504d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5505554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5506554e165cSAndy Whitcroft				WARN("MEMSET",
5507d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5508d7c76ba7SJoe Perches			}
5509d7c76ba7SJoe Perches		}
5510d7c76ba7SJoe Perches
551198a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
551298a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
551310895d2cSMateusz Kulikowski		    defined $stat &&
551410895d2cSMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
551598a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
551610895d2cSMateusz Kulikowski				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
551798a9bba5SJoe Perches			    $fix) {
5518194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
551998a9bba5SJoe Perches			}
552098a9bba5SJoe Perches		}
552198a9bba5SJoe Perches
5522b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5523b6117d17SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
5524b6117d17SMateusz Kulikowski		    defined $stat &&
5525b6117d17SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5526b6117d17SMateusz Kulikowski			WARN("PREFER_ETHER_ADDR_EQUAL",
5527b6117d17SMateusz Kulikowski			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5528b6117d17SMateusz Kulikowski		}
5529b6117d17SMateusz Kulikowski
55308617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
55318617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
55328617cd09SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
55338617cd09SMateusz Kulikowski		    defined $stat &&
55348617cd09SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
55358617cd09SMateusz Kulikowski
55368617cd09SMateusz Kulikowski			my $ms_val = $7;
55378617cd09SMateusz Kulikowski
55388617cd09SMateusz Kulikowski			if ($ms_val =~ /^(?:0x|)0+$/i) {
55398617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_ZERO_ADDR",
55408617cd09SMateusz Kulikowski					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
55418617cd09SMateusz Kulikowski				    $fix) {
55428617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
55438617cd09SMateusz Kulikowski				}
55448617cd09SMateusz Kulikowski			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
55458617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_BROADCAST_ADDR",
55468617cd09SMateusz Kulikowski					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
55478617cd09SMateusz Kulikowski				    $fix) {
55488617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
55498617cd09SMateusz Kulikowski				}
55508617cd09SMateusz Kulikowski			}
55518617cd09SMateusz Kulikowski		}
55528617cd09SMateusz Kulikowski
5553d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5554d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5555d1fe9c09SJoe Perches		    defined $stat &&
5556d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5557d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5558d7c76ba7SJoe Perches				my $call = $1;
5559d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5560d7c76ba7SJoe Perches				my $arg1 = $3;
5561d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5562d1fe9c09SJoe Perches				my $arg2 = $8;
5563d7c76ba7SJoe Perches				my $cast;
5564d7c76ba7SJoe Perches
5565d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5566d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5567d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5568d7c76ba7SJoe Perches					$cast = $cast1;
5569d7c76ba7SJoe Perches				} else {
5570d7c76ba7SJoe Perches					$cast = $cast2;
5571d7c76ba7SJoe Perches				}
5572d7c76ba7SJoe Perches				WARN("MINMAX",
5573d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5574554e165cSAndy Whitcroft			}
5575554e165cSAndy Whitcroft		}
5576554e165cSAndy Whitcroft
55774a273195SJoe Perches# check usleep_range arguments
55784a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
55794a273195SJoe Perches		    defined $stat &&
55804a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
55814a273195SJoe Perches			my $min = $1;
55824a273195SJoe Perches			my $max = $7;
55834a273195SJoe Perches			if ($min eq $max) {
55844a273195SJoe Perches				WARN("USLEEP_RANGE",
55854a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
55864a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
55874a273195SJoe Perches				 $min > $max) {
55884a273195SJoe Perches				WARN("USLEEP_RANGE",
55894a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
55904a273195SJoe Perches			}
55914a273195SJoe Perches		}
55924a273195SJoe Perches
5593823b794cSJoe Perches# check for naked sscanf
5594823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5595823b794cSJoe Perches		    defined $stat &&
55966c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5597823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5598823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5599823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5600823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5601823b794cSJoe Perches			$lc = $lc + $linenr;
5602823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5603823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5604823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5605823b794cSJoe Perches			}
5606823b794cSJoe Perches			WARN("NAKED_SSCANF",
5607823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5608823b794cSJoe Perches		}
5609823b794cSJoe Perches
5610afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5611afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5612afc819abSJoe Perches		    defined $stat &&
5613afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5614afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5615afc819abSJoe Perches			$lc = $lc + $linenr;
5616afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5617afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5618afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5619afc819abSJoe Perches			}
5620afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5621afc819abSJoe Perches				my $format = $6;
5622afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5623afc819abSJoe Perches				if ($count == 1 &&
5624afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5625afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5626afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5627afc819abSJoe Perches				}
5628afc819abSJoe Perches			}
5629afc819abSJoe Perches		}
5630afc819abSJoe Perches
563170dc8a48SJoe Perches# check for new externs in .h files.
563270dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
563370dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5634d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
563570dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
563670dc8a48SJoe Perches			    $fix) {
5637194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
563870dc8a48SJoe Perches			}
563970dc8a48SJoe Perches		}
564070dc8a48SJoe Perches
5641de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5642171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5643c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5644171ae1a4SAndy Whitcroft		{
5645c45dcabdSAndy Whitcroft			my $function_name = $1;
5646c45dcabdSAndy Whitcroft			my $paren_space = $2;
5647171ae1a4SAndy Whitcroft
5648171ae1a4SAndy Whitcroft			my $s = $stat;
5649171ae1a4SAndy Whitcroft			if (defined $cond) {
5650171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5651171ae1a4SAndy Whitcroft			}
5652c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5653c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5654c45dcabdSAndy Whitcroft			{
5655000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5656000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5657de7d4f0eSAndy Whitcroft			}
5658de7d4f0eSAndy Whitcroft
5659171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5660000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5661000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5662171ae1a4SAndy Whitcroft			}
56639c9ba34eSAndy Whitcroft
56649c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
56659c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
56669c9ba34eSAndy Whitcroft		{
5667000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5668000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5669171ae1a4SAndy Whitcroft		}
5670171ae1a4SAndy Whitcroft
5671de7d4f0eSAndy Whitcroft# checks for new __setup's
5672de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5673de7d4f0eSAndy Whitcroft			my $name = $1;
5674de7d4f0eSAndy Whitcroft
5675de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5676000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5677000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5678de7d4f0eSAndy Whitcroft			}
5679653d4876SAndy Whitcroft		}
56809c0ca6f9SAndy Whitcroft
56819c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5682caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5683000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5684000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
56859c0ca6f9SAndy Whitcroft		}
568613214adfSAndy Whitcroft
5687a640d25cSJoe Perches# alloc style
5688a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5689a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5690a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5691a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5692a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5693a640d25cSJoe Perches		}
5694a640d25cSJoe Perches
569560a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
569660a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5697e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
569860a55369SJoe Perches			my $oldfunc = $3;
569960a55369SJoe Perches			my $a1 = $4;
570060a55369SJoe Perches			my $a2 = $10;
570160a55369SJoe Perches			my $newfunc = "kmalloc_array";
570260a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
570360a55369SJoe Perches			my $r1 = $a1;
570460a55369SJoe Perches			my $r2 = $a2;
570560a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
570660a55369SJoe Perches				$r1 = $a2;
570760a55369SJoe Perches				$r2 = $a1;
570860a55369SJoe Perches			}
5709e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5710e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5711e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5712e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5713e367455aSJoe Perches				    $fix) {
5714194f66fcSJoe 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;
571560a55369SJoe Perches
571660a55369SJoe Perches				}
571760a55369SJoe Perches			}
571860a55369SJoe Perches		}
571960a55369SJoe Perches
5720972fdea2SJoe Perches# check for krealloc arg reuse
5721972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5722972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5723972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5724972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5725972fdea2SJoe Perches		}
5726972fdea2SJoe Perches
57275ce59ae0SJoe Perches# check for alloc argument mismatch
57285ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
57295ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
57305ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
57315ce59ae0SJoe Perches		}
57325ce59ae0SJoe Perches
5733caf2a54fSJoe Perches# check for multiple semicolons
5734caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5735d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5736d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5737d5e616fcSJoe Perches			    $fix) {
5738194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5739d5e616fcSJoe Perches			}
5740d1e2ad07SJoe Perches		}
5741d1e2ad07SJoe Perches
5742cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5743cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
5744cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
57450ab90191SJoe Perches			my $ull = "";
57460ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
57470ab90191SJoe Perches			if (CHK("BIT_MACRO",
57480ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
57490ab90191SJoe Perches			    $fix) {
57500ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
57510ab90191SJoe Perches			}
57520ab90191SJoe Perches		}
57530ab90191SJoe Perches
57542d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
57552d632745SJoe 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*$/) {
57562d632745SJoe Perches			my $config = $1;
57572d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
57582d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
57592d632745SJoe Perches			    $fix) {
57602d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
57612d632745SJoe Perches			}
57622d632745SJoe Perches		}
57632d632745SJoe Perches
5764e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5765c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5766c34c09a8SJoe Perches			my $has_break = 0;
5767c34c09a8SJoe Perches			my $has_statement = 0;
5768c34c09a8SJoe Perches			my $count = 0;
5769c34c09a8SJoe Perches			my $prevline = $linenr;
5770e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5771c34c09a8SJoe Perches				$prevline--;
5772c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5773c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5774c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5775c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5776c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5777c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5778c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5779c34c09a8SJoe Perches				$has_statement = 1;
5780c34c09a8SJoe Perches				$count++;
5781c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5782c34c09a8SJoe Perches			}
5783c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5784c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5785c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5786c34c09a8SJoe Perches			}
5787c34c09a8SJoe Perches		}
5788c34c09a8SJoe Perches
5789d1e2ad07SJoe Perches# check for switch/default statements without a break;
5790d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5791d1e2ad07SJoe Perches		    defined $stat &&
5792d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5793d1e2ad07SJoe Perches			my $ctx = '';
5794d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5795d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5796d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5797d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5798d1e2ad07SJoe Perches			}
5799d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5800d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5801caf2a54fSJoe Perches		}
5802caf2a54fSJoe Perches
580313214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5804d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5805d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5806d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5807d5e616fcSJoe Perches			    $fix) {
5808194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5809d5e616fcSJoe Perches			}
581013214adfSAndy Whitcroft		}
5811773647a0SAndy Whitcroft
581262ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
581362ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
581462ec818fSJoe Perches			ERROR("DATE_TIME",
581562ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
581662ec818fSJoe Perches		}
581762ec818fSJoe Perches
58182c92488aSJoe Perches# check for use of yield()
58192c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
58202c92488aSJoe Perches			WARN("YIELD",
58212c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
58222c92488aSJoe Perches		}
58232c92488aSJoe Perches
5824179f8f40SJoe Perches# check for comparisons against true and false
5825179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5826179f8f40SJoe Perches			my $lead = $1;
5827179f8f40SJoe Perches			my $arg = $2;
5828179f8f40SJoe Perches			my $test = $3;
5829179f8f40SJoe Perches			my $otype = $4;
5830179f8f40SJoe Perches			my $trail = $5;
5831179f8f40SJoe Perches			my $op = "!";
5832179f8f40SJoe Perches
5833179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5834179f8f40SJoe Perches
5835179f8f40SJoe Perches			my $type = lc($otype);
5836179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5837179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5838179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5839179f8f40SJoe Perches					$op = "";
5840179f8f40SJoe Perches				}
5841179f8f40SJoe Perches
5842179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5843179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5844179f8f40SJoe Perches
5845179f8f40SJoe Perches## maybe suggesting a correct construct would better
5846179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5847179f8f40SJoe Perches
5848179f8f40SJoe Perches			}
5849179f8f40SJoe Perches		}
5850179f8f40SJoe Perches
58514882720bSThomas Gleixner# check for semaphores initialized locked
58524882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5853000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5854000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5855773647a0SAndy Whitcroft		}
58566712d858SJoe Perches
585767d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
585867d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5859000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
586067d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5861773647a0SAndy Whitcroft		}
58626712d858SJoe Perches
5863ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5864f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5865000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5866ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5867f3db6639SMichael Ellerman		}
58686712d858SJoe Perches
58690f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
58700f3c5aabSJoe Perches		my $const_structs = qr{
58710f3c5aabSJoe Perches				acpi_dock_ops|
587279404849SEmese Revfy				address_space_operations|
587379404849SEmese Revfy				backlight_ops|
587479404849SEmese Revfy				block_device_operations|
587579404849SEmese Revfy				dentry_operations|
587679404849SEmese Revfy				dev_pm_ops|
587779404849SEmese Revfy				dma_map_ops|
587879404849SEmese Revfy				extent_io_ops|
587979404849SEmese Revfy				file_lock_operations|
588079404849SEmese Revfy				file_operations|
588179404849SEmese Revfy				hv_ops|
588279404849SEmese Revfy				ide_dma_ops|
588379404849SEmese Revfy				intel_dvo_dev_ops|
588479404849SEmese Revfy				item_operations|
588579404849SEmese Revfy				iwl_ops|
588679404849SEmese Revfy				kgdb_arch|
588779404849SEmese Revfy				kgdb_io|
588879404849SEmese Revfy				kset_uevent_ops|
588979404849SEmese Revfy				lock_manager_operations|
589079404849SEmese Revfy				microcode_ops|
589179404849SEmese Revfy				mtrr_ops|
589279404849SEmese Revfy				neigh_ops|
589379404849SEmese Revfy				nlmsvc_binding|
58940f3c5aabSJoe Perches				of_device_id|
589579404849SEmese Revfy				pci_raw_ops|
589679404849SEmese Revfy				pipe_buf_operations|
589779404849SEmese Revfy				platform_hibernation_ops|
589879404849SEmese Revfy				platform_suspend_ops|
589979404849SEmese Revfy				proto_ops|
590079404849SEmese Revfy				rpc_pipe_ops|
590179404849SEmese Revfy				seq_operations|
590279404849SEmese Revfy				snd_ac97_build_ops|
590379404849SEmese Revfy				soc_pcmcia_socket_ops|
590479404849SEmese Revfy				stacktrace_ops|
590579404849SEmese Revfy				sysfs_ops|
590679404849SEmese Revfy				tty_operations|
59076d07d01bSJoe Perches				uart_ops|
590879404849SEmese Revfy				usb_mon_operations|
590979404849SEmese Revfy				wd_ops}x;
59106903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
59110f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
5912000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5913000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
59146903ffb2SAndy Whitcroft				$herecurr);
59152b6db5cbSAndy Whitcroft		}
5916773647a0SAndy Whitcroft
5917773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5918773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5919773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5920c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5921c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5922171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5923171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5924171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5925773647a0SAndy Whitcroft		{
5926000d1cc1SJoe Perches			WARN("NR_CPUS",
5927000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5928773647a0SAndy Whitcroft		}
59299c9ba34eSAndy Whitcroft
593052ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
593152ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
593252ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
593352ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
593452ea8506SJoe Perches		}
593552ea8506SJoe Perches
5936acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5937acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5938acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5939acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5940acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5941acd9362cSJoe Perches		}
5942acd9362cSJoe Perches
5943691d77b6SAndy Whitcroft# whine mightly about in_atomic
5944691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5945691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5946000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5947000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5948f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5949000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5950000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5951691d77b6SAndy Whitcroft			}
5952691d77b6SAndy Whitcroft		}
59531704f47bSPeter Zijlstra
5954481aea5cSJoe Perches# whine about ACCESS_ONCE
5955481aea5cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5956481aea5cSJoe Perches		    $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
5957481aea5cSJoe Perches			my $par = $1;
5958481aea5cSJoe Perches			my $eq = $2;
5959481aea5cSJoe Perches			my $fun = $3;
5960481aea5cSJoe Perches			$par =~ s/^\(\s*(.*)\s*\)$/$1/;
5961481aea5cSJoe Perches			if (defined($eq)) {
5962481aea5cSJoe Perches				if (WARN("PREFER_WRITE_ONCE",
5963481aea5cSJoe Perches					 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
5964481aea5cSJoe Perches				    $fix) {
5965481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
5966481aea5cSJoe Perches				}
5967481aea5cSJoe Perches			} else {
5968481aea5cSJoe Perches				if (WARN("PREFER_READ_ONCE",
5969481aea5cSJoe Perches					 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
5970481aea5cSJoe Perches				    $fix) {
5971481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
5972481aea5cSJoe Perches				}
5973481aea5cSJoe Perches			}
5974481aea5cSJoe Perches		}
5975481aea5cSJoe Perches
59761704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
59771704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
59781704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
59791704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
59801704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
59811704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5982000d1cc1SJoe Perches				ERROR("LOCKDEP",
5983000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
59841704f47bSPeter Zijlstra			}
59851704f47bSPeter Zijlstra		}
598688f8831cSDave Jones
5987b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5988b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
5989000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5990000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
599188f8831cSDave Jones		}
59922435880fSJoe Perches
5993515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5994515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5995515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5996515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
59972435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
59982435880fSJoe Perches				my $func = $entry->[0];
59992435880fSJoe Perches				my $arg_pos = $entry->[1];
60002435880fSJoe Perches
60012435880fSJoe Perches				my $skip_args = "";
60022435880fSJoe Perches				if ($arg_pos > 1) {
60032435880fSJoe Perches					$arg_pos--;
60042435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
60052435880fSJoe Perches				}
60062435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
6007515a235eSJoe Perches				if ($line =~ /$test/) {
60082435880fSJoe Perches					my $val = $1;
60092435880fSJoe Perches					$val = $6 if ($skip_args ne "");
60102435880fSJoe Perches
60111727cc70SJoe Perches					if ($val !~ /^0$/ &&
60121727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
60131727cc70SJoe Perches					     length($val) ne 4)) {
60142435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
60151727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
6016c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6017c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
6018c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
60192435880fSJoe Perches					}
60202435880fSJoe Perches				}
60212435880fSJoe Perches			}
602213214adfSAndy Whitcroft		}
60235a6d20ceSBjorn Andersson
60245a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
60255a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
60265a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
60275a6d20ceSBjorn Andersson			my $valid_licenses = qr{
60285a6d20ceSBjorn Andersson						GPL|
60295a6d20ceSBjorn Andersson						GPL\ v2|
60305a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
60315a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
60325a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
60335a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
60345a6d20ceSBjorn Andersson						Proprietary
60355a6d20ceSBjorn Andersson					}x;
60365a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
60375a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
60385a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
60395a6d20ceSBjorn Andersson			}
60405a6d20ceSBjorn Andersson		}
6041515a235eSJoe Perches	}
604213214adfSAndy Whitcroft
604313214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
604413214adfSAndy Whitcroft	# so just keep quiet.
604513214adfSAndy Whitcroft	if ($#rawlines == -1) {
604613214adfSAndy Whitcroft		exit(0);
60470a920b5bSAndy Whitcroft	}
60480a920b5bSAndy Whitcroft
60498905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
60508905a67cSAndy Whitcroft	# things that appear to be patches.
60518905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
60528905a67cSAndy Whitcroft		exit(0);
60538905a67cSAndy Whitcroft	}
60548905a67cSAndy Whitcroft
60558905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
60568905a67cSAndy Whitcroft	# just keep quiet.
60578905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
60588905a67cSAndy Whitcroft		exit(0);
60598905a67cSAndy Whitcroft	}
60608905a67cSAndy Whitcroft
606106330fc4SJoe Perches	if (!$is_patch && $file !~ /cover-letter\.patch$/) {
6062000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
6063000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
60640a920b5bSAndy Whitcroft	}
6065ed43c4e5SAllen Hubbe	if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
6066000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
6067000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
60680a920b5bSAndy Whitcroft	}
60690a920b5bSAndy Whitcroft
6070f0a594c1SAndy Whitcroft	print report_dump();
607113214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
607213214adfSAndy Whitcroft		print "$filename " if ($summary_file);
60736c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
60746c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
60756c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
60766c72ffaaSAndy Whitcroft	}
60778905a67cSAndy Whitcroft
6078d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6079ef212196SJoe Perches		# If there were any defects found and not already fixing them
6080ef212196SJoe Perches		if (!$clean and !$fix) {
6081ef212196SJoe Perches			print << "EOM"
6082ef212196SJoe Perches
6083ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6084ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6085ef212196SJoe PerchesEOM
6086ef212196SJoe Perches		}
6087d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6088d2c0a235SAndy Whitcroft		# then suggest that.
6089d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6090b0781216SMike Frysinger			$rpt_cleaners = 0;
6091d8469f16SJoe Perches			print << "EOM"
6092d8469f16SJoe Perches
6093d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6094d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6095d8469f16SJoe PerchesEOM
6096d2c0a235SAndy Whitcroft		}
6097d2c0a235SAndy Whitcroft	}
6098d2c0a235SAndy Whitcroft
6099d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6100d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6101d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
61029624b8d6SJoe Perches		my $newfile = $filename;
61039624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
61043705ce5bSJoe Perches		my $linecount = 0;
61053705ce5bSJoe Perches		my $f;
61063705ce5bSJoe Perches
6107d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6108d752fcc8SJoe Perches
61093705ce5bSJoe Perches		open($f, '>', $newfile)
61103705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
61113705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
61123705ce5bSJoe Perches			$linecount++;
61133705ce5bSJoe Perches			if ($file) {
61143705ce5bSJoe Perches				if ($linecount > 3) {
61153705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
61163705ce5bSJoe Perches					print $f $fixed_line . "\n";
61173705ce5bSJoe Perches				}
61183705ce5bSJoe Perches			} else {
61193705ce5bSJoe Perches				print $f $fixed_line . "\n";
61203705ce5bSJoe Perches			}
61213705ce5bSJoe Perches		}
61223705ce5bSJoe Perches		close($f);
61233705ce5bSJoe Perches
61243705ce5bSJoe Perches		if (!$quiet) {
61253705ce5bSJoe Perches			print << "EOM";
6126d8469f16SJoe Perches
61273705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
61283705ce5bSJoe Perches
61293705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
61303705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
61313705ce5bSJoe Perches
61323705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
61333705ce5bSJoe PerchesNo warranties, expressed or implied...
61343705ce5bSJoe PerchesEOM
61353705ce5bSJoe Perches		}
61363705ce5bSJoe Perches	}
61373705ce5bSJoe Perches
6138d8469f16SJoe Perches	if ($quiet == 0) {
6139d8469f16SJoe Perches		print "\n";
6140d8469f16SJoe Perches		if ($clean == 1) {
6141d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6142d8469f16SJoe Perches		} else {
6143d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
61440a920b5bSAndy Whitcroft		}
61450a920b5bSAndy Whitcroft	}
61460a920b5bSAndy Whitcroft	return $clean;
61470a920b5bSAndy Whitcroft}
6148