xref: /linux-6.15/scripts/checkpatch.pl (revision 3beb42ec)
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;
306c72ffaaSAndy Whitcroftmy $check = 0;
312ac73b4fSJoe Perchesmy $check_orig = 0;
328905a67cSAndy Whitcroftmy $summary = 1;
338905a67cSAndy Whitcroftmy $mailback = 0;
3413214adfSAndy Whitcroftmy $summary_file = 0;
35000d1cc1SJoe Perchesmy $show_types = 0;
36*3beb42ecSJoe Perchesmy $list_types = 0;
373705ce5bSJoe Perchesmy $fix = 0;
389624b8d6SJoe Perchesmy $fix_inplace = 0;
396c72ffaaSAndy Whitcroftmy $root;
40c2fdda0dSAndy Whitcroftmy %debug;
413445686aSJoe Perchesmy %camelcase = ();
4291bfe484SJoe Perchesmy %use_type = ();
4391bfe484SJoe Perchesmy @use = ();
4491bfe484SJoe Perchesmy %ignore_type = ();
45000d1cc1SJoe Perchesmy @ignore = ();
4677f5b10aSHannes Edermy $help = 0;
47000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
486cd7f386SJoe Perchesmy $max_line_length = 80;
49d62a201fSDave Hansenmy $ignore_perl_version = 0;
50d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
5156193274SVadim Bendeburymy $min_conf_desc_length = 4;
5266b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
53ebfd7d62SJoe Perchesmy $codespell = 0;
54f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
5557230297SJoe Perchesmy $color = 1;
5677f5b10aSHannes Eder
5777f5b10aSHannes Edersub help {
5877f5b10aSHannes Eder	my ($exitcode) = @_;
5977f5b10aSHannes Eder
6077f5b10aSHannes Eder	print << "EOM";
6177f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
6277f5b10aSHannes EderVersion: $V
6377f5b10aSHannes Eder
6477f5b10aSHannes EderOptions:
6577f5b10aSHannes Eder  -q, --quiet                quiet
6677f5b10aSHannes Eder  --no-tree                  run without a kernel tree
6777f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
6877f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
6977f5b10aSHannes Eder  --emacs                    emacs compile window format
7077f5b10aSHannes Eder  --terse                    one line per report
7134d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
7277f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
7377f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
74*3beb42ecSJoe Perches  --list-types               list the possible message types
7591bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
76000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
77*3beb42ecSJoe Perches  --show-types               show the specific message type in the output
786cd7f386SJoe Perches  --max-line-length=n        set the maximum line length, if exceeded, warn
7956193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
8077f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
8177f5b10aSHannes Eder  --no-summary               suppress the per-file summary
8277f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
8377f5b10aSHannes Eder  --summary-file             include the filename in summary
8477f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
8577f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
8677f5b10aSHannes Eder                             is all off)
8777f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
8877f5b10aSHannes Eder                             literally
893705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
903705ce5bSJoe Perches                             If correctable single-line errors exist, create
913705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
923705ce5bSJoe Perches                             with potential errors corrected to the preferred
933705ce5bSJoe Perches                             checkpatch style
949624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
959624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
969624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
97d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
98d62a201fSDave Hansen                             runtime errors.
99ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
100f1a63678SMaxim Uvarov                             (default:/usr/share/codespell/dictionary.txt)
101ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
10257230297SJoe Perches  --color                    Use colors when output is STDOUT (default: on)
10377f5b10aSHannes Eder  -h, --help, --version      display this help and exit
10477f5b10aSHannes Eder
10577f5b10aSHannes EderWhen FILE is - read standard input.
10677f5b10aSHannes EderEOM
10777f5b10aSHannes Eder
10877f5b10aSHannes Eder	exit($exitcode);
10977f5b10aSHannes Eder}
11077f5b10aSHannes Eder
111*3beb42ecSJoe Perchessub uniq {
112*3beb42ecSJoe Perches	my %seen;
113*3beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
114*3beb42ecSJoe Perches}
115*3beb42ecSJoe Perches
116*3beb42ecSJoe Perchessub list_types {
117*3beb42ecSJoe Perches	my ($exitcode) = @_;
118*3beb42ecSJoe Perches
119*3beb42ecSJoe Perches	my $count = 0;
120*3beb42ecSJoe Perches
121*3beb42ecSJoe Perches	local $/ = undef;
122*3beb42ecSJoe Perches
123*3beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
124*3beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
125*3beb42ecSJoe Perches
126*3beb42ecSJoe Perches	my $text = <$script>;
127*3beb42ecSJoe Perches	close($script);
128*3beb42ecSJoe Perches
129*3beb42ecSJoe Perches	my @types = ();
130*3beb42ecSJoe Perches	for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
131*3beb42ecSJoe Perches		push (@types, $_);
132*3beb42ecSJoe Perches	}
133*3beb42ecSJoe Perches	@types = sort(uniq(@types));
134*3beb42ecSJoe Perches	print("#\tMessage type\n\n");
135*3beb42ecSJoe Perches	foreach my $type (@types) {
136*3beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
137*3beb42ecSJoe Perches	}
138*3beb42ecSJoe Perches
139*3beb42ecSJoe Perches	exit($exitcode);
140*3beb42ecSJoe Perches}
141*3beb42ecSJoe Perches
142000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
143000d1cc1SJoe Perchesif (-f $conf) {
144000d1cc1SJoe Perches	my @conf_args;
145000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
146000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
147000d1cc1SJoe Perches
148000d1cc1SJoe Perches	while (<$conffile>) {
149000d1cc1SJoe Perches		my $line = $_;
150000d1cc1SJoe Perches
151000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
152000d1cc1SJoe Perches		$line =~ s/^\s*//g;
153000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
154000d1cc1SJoe Perches
155000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
156000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
157000d1cc1SJoe Perches
158000d1cc1SJoe Perches		my @words = split(" ", $line);
159000d1cc1SJoe Perches		foreach my $word (@words) {
160000d1cc1SJoe Perches			last if ($word =~ m/^#/);
161000d1cc1SJoe Perches			push (@conf_args, $word);
162000d1cc1SJoe Perches		}
163000d1cc1SJoe Perches	}
164000d1cc1SJoe Perches	close($conffile);
165000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
166000d1cc1SJoe Perches}
167000d1cc1SJoe Perches
1680a920b5bSAndy WhitcroftGetOptions(
1696c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
1700a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
1710a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
1720a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
1736c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
1748905a67cSAndy Whitcroft	'terse!'	=> \$terse,
17534d8815fSJoe Perches	'showfile!'	=> \$showfile,
17677f5b10aSHannes Eder	'f|file!'	=> \$file,
1776c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
1786c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
179000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
18091bfe484SJoe Perches	'types=s'	=> \@use,
181000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
182*3beb42ecSJoe Perches	'list-types!'	=> \$list_types,
1836cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
18456193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
1856c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
1868905a67cSAndy Whitcroft	'summary!'	=> \$summary,
1878905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
18813214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
1893705ce5bSJoe Perches	'fix!'		=> \$fix,
1909624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
191d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
192c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
193773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
194ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
195ebfd7d62SJoe Perches	'codespellfile=s'	=> \$codespellfile,
19657230297SJoe Perches	'color!'	=> \$color,
19777f5b10aSHannes Eder	'h|help'	=> \$help,
19877f5b10aSHannes Eder	'version'	=> \$help
19977f5b10aSHannes Eder) or help(1);
20077f5b10aSHannes Eder
20177f5b10aSHannes Ederhelp(0) if ($help);
2020a920b5bSAndy Whitcroft
203*3beb42ecSJoe Percheslist_types(0) if ($list_types);
204*3beb42ecSJoe Perches
2059624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
2062ac73b4fSJoe Perches$check_orig = $check;
2079624b8d6SJoe Perches
2080a920b5bSAndy Whitcroftmy $exit = 0;
2090a920b5bSAndy Whitcroft
210d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
211d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
212d62a201fSDave Hansen	if (!$ignore_perl_version) {
213d62a201fSDave Hansen		exit(1);
214d62a201fSDave Hansen	}
215d62a201fSDave Hansen}
216d62a201fSDave Hansen
2170a920b5bSAndy Whitcroftif ($#ARGV < 0) {
21877f5b10aSHannes Eder	print "$P: no input files\n";
2190a920b5bSAndy Whitcroft	exit(1);
2200a920b5bSAndy Whitcroft}
2210a920b5bSAndy Whitcroft
22291bfe484SJoe Perchessub hash_save_array_words {
22391bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
22491bfe484SJoe Perches
22591bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
22691bfe484SJoe Perches	foreach my $word (@array) {
227000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
228000d1cc1SJoe Perches		$word =~ s/^\s*//g;
229000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
230000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
231000d1cc1SJoe Perches
232000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
233000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
234000d1cc1SJoe Perches
23591bfe484SJoe Perches		$hashRef->{$word}++;
236000d1cc1SJoe Perches	}
23791bfe484SJoe Perches}
23891bfe484SJoe Perches
23991bfe484SJoe Perchessub hash_show_words {
24091bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
24191bfe484SJoe Perches
2423c816e49SJoe Perches	if (keys %$hashRef) {
243d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
24458cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
24591bfe484SJoe Perches			print " $word";
24691bfe484SJoe Perches		}
247d8469f16SJoe Perches		print "\n";
24891bfe484SJoe Perches	}
24991bfe484SJoe Perches}
25091bfe484SJoe Perches
25191bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
25291bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
253000d1cc1SJoe Perches
254c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
255c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
2567429c690SAndy Whitcroftmy $dbg_type = 0;
257a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
258c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
25921caa13cSAndy Whitcroft	## no critic
26021caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
26121caa13cSAndy Whitcroft	die "$@" if ($@);
262c2fdda0dSAndy Whitcroft}
263c2fdda0dSAndy Whitcroft
264d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
265d2c0a235SAndy Whitcroft
2668905a67cSAndy Whitcroftif ($terse) {
2678905a67cSAndy Whitcroft	$emacs = 1;
2688905a67cSAndy Whitcroft	$quiet++;
2698905a67cSAndy Whitcroft}
2708905a67cSAndy Whitcroft
2716c72ffaaSAndy Whitcroftif ($tree) {
2726c72ffaaSAndy Whitcroft	if (defined $root) {
2736c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
2746c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
2756c72ffaaSAndy Whitcroft		}
2766c72ffaaSAndy Whitcroft	} else {
2776c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
2786c72ffaaSAndy Whitcroft			$root = '.';
2796c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
2806c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
2816c72ffaaSAndy Whitcroft			$root = $1;
2826c72ffaaSAndy Whitcroft		}
2836c72ffaaSAndy Whitcroft	}
2846c72ffaaSAndy Whitcroft
2856c72ffaaSAndy Whitcroft	if (!defined $root) {
2860a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
2870a920b5bSAndy Whitcroft		exit(2);
2880a920b5bSAndy Whitcroft	}
2896c72ffaaSAndy Whitcroft}
2906c72ffaaSAndy Whitcroft
2916c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
2926c72ffaaSAndy Whitcroft
2932ceb532bSAndy Whitcroftour $Ident	= qr{
2942ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
2952ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
2962ceb532bSAndy Whitcroft		}x;
2976c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
2986c72ffaaSAndy Whitcroftour $Sparse	= qr{
2996c72ffaaSAndy Whitcroft			__user|
3006c72ffaaSAndy Whitcroft			__kernel|
3016c72ffaaSAndy Whitcroft			__force|
3026c72ffaaSAndy Whitcroft			__iomem|
30354507b51SJoe Perches			__pmem|
3046c72ffaaSAndy Whitcroft			__must_check|
3056c72ffaaSAndy Whitcroft			__init_refok|
306417495edSAndy Whitcroft			__kprobes|
307165e72a6SSven Eckelmann			__ref|
308ad315455SBoqun Feng			__rcu|
309ad315455SBoqun Feng			__private
3106c72ffaaSAndy Whitcroft		}x;
311e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
312e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
313e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
314e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
315e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
3168716de38SJoe Perches
31752131292SWolfram Sang# Notes to $Attribute:
31852131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
3196c72ffaaSAndy Whitcroftour $Attribute	= qr{
3206c72ffaaSAndy Whitcroft			const|
32103f1df7dSJoe Perches			__percpu|
32203f1df7dSJoe Perches			__nocast|
32303f1df7dSJoe Perches			__safe|
32403f1df7dSJoe Perches			__bitwise__|
32503f1df7dSJoe Perches			__packed__|
32603f1df7dSJoe Perches			__packed2__|
32703f1df7dSJoe Perches			__naked|
32803f1df7dSJoe Perches			__maybe_unused|
32903f1df7dSJoe Perches			__always_unused|
33003f1df7dSJoe Perches			__noreturn|
33103f1df7dSJoe Perches			__used|
33203f1df7dSJoe Perches			__cold|
333e23ef1f3SJoe Perches			__pure|
33403f1df7dSJoe Perches			__noclone|
33503f1df7dSJoe Perches			__deprecated|
3366c72ffaaSAndy Whitcroft			__read_mostly|
3376c72ffaaSAndy Whitcroft			__kprobes|
3388716de38SJoe Perches			$InitAttribute|
33924e1d81aSAndy Whitcroft			____cacheline_aligned|
34024e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
3415fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
3425fe3af11SAndy Whitcroft			__weak
3436c72ffaaSAndy Whitcroft		  }x;
344c45dcabdSAndy Whitcroftour $Modifier;
34591cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
3466c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
3476c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
3486c72ffaaSAndy Whitcroft
34995e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
35095e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
35195e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
35295e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
3532435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
354c0a5c898SJoe Perchesour $String	= qr{"[X\t]*"};
355326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
356326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
357326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
35874349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
3592435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
360326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
361447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
36223f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
3636c72ffaaSAndy Whitcroftour $Operators	= qr{
3646c72ffaaSAndy Whitcroft			<=|>=|==|!=|
3656c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
36623f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
3676c72ffaaSAndy Whitcroft		  }x;
3686c72ffaaSAndy Whitcroft
36991cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
37091cb5195SJoe Perches
371ab7e23f3SJoe Perchesour $BasicType;
3728905a67cSAndy Whitcroftour $NonptrType;
3731813087dSJoe Perchesour $NonptrTypeMisordered;
3748716de38SJoe Perchesour $NonptrTypeWithAttr;
3758905a67cSAndy Whitcroftour $Type;
3761813087dSJoe Perchesour $TypeMisordered;
3778905a67cSAndy Whitcroftour $Declare;
3781813087dSJoe Perchesour $DeclareMisordered;
3798905a67cSAndy Whitcroft
38015662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
38115662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
382171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
383171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
384171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
385171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
386171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
387171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
388171ae1a4SAndy Whitcroft}x;
389171ae1a4SAndy Whitcroft
39015662b3eSJoe Perchesour $UTF8	= qr{
39115662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
39215662b3eSJoe Perches	| $NON_ASCII_UTF8
39315662b3eSJoe Perches}x;
39415662b3eSJoe Perches
395e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
396021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
397021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
398021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
399021158b4SJoe Perches)};
400e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
401fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
4028ed22cadSAndy Whitcroft	atomic_t
4038ed22cadSAndy Whitcroft)};
404e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
405e6176fa4SJoe Perches	$typeC99Typedefs\b|
406e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
407e6176fa4SJoe Perches	$typeKernelTypedefs\b
408e6176fa4SJoe Perches)};
4098ed22cadSAndy Whitcroft
4106d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
4116d32f7a3SJoe Perches
412691e669bSJoe Perchesour $logFunctions = qr{(?x:
4136e60c02eSJoe Perches	printk(?:_ratelimited|_once|)|
4147d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
4156e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
416b0531722SJoe Perches	panic|
41706668727SJoe Perches	MODULE_[A-Z_]+|
41806668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
419691e669bSJoe Perches)};
420691e669bSJoe Perches
42120112475SJoe Perchesour $signature_tags = qr{(?xi:
42220112475SJoe Perches	Signed-off-by:|
42320112475SJoe Perches	Acked-by:|
42420112475SJoe Perches	Tested-by:|
42520112475SJoe Perches	Reviewed-by:|
42620112475SJoe Perches	Reported-by:|
4278543ae12SMugunthan V N	Suggested-by:|
42820112475SJoe Perches	To:|
42920112475SJoe Perches	Cc:
43020112475SJoe Perches)};
43120112475SJoe Perches
4321813087dSJoe Perchesour @typeListMisordered = (
4331813087dSJoe Perches	qr{char\s+(?:un)?signed},
4341813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
4351813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
4361813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
4371813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
4381813087dSJoe Perches	qr{short\s+(?:un)?signed},
4391813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
4401813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
4411813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
4421813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
4431813087dSJoe Perches	qr{int\s+(?:un)?signed},
4441813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
4451813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
4461813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
4471813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
4481813087dSJoe Perches	qr{long\s+(?:un)?signed},
4491813087dSJoe Perches);
4501813087dSJoe Perches
4518905a67cSAndy Whitcroftour @typeList = (
4528905a67cSAndy Whitcroft	qr{void},
4530c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
4540c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
4550c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
4560c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
4570c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
4580c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
4590c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
4600c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
4610c773d9dSJoe Perches	qr{(?:un)?signed},
4628905a67cSAndy Whitcroft	qr{float},
4638905a67cSAndy Whitcroft	qr{double},
4648905a67cSAndy Whitcroft	qr{bool},
4658905a67cSAndy Whitcroft	qr{struct\s+$Ident},
4668905a67cSAndy Whitcroft	qr{union\s+$Ident},
4678905a67cSAndy Whitcroft	qr{enum\s+$Ident},
4688905a67cSAndy Whitcroft	qr{${Ident}_t},
4698905a67cSAndy Whitcroft	qr{${Ident}_handler},
4708905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
4711813087dSJoe Perches	@typeListMisordered,
4728905a67cSAndy Whitcroft);
473938224b5SJoe Perches
474938224b5SJoe Perchesour $C90_int_types = qr{(?x:
475938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
476938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
477938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
478938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
479938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
480938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
481938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
482938224b5SJoe Perches
483938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
484938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
485938224b5SJoe Perches	long\s+(?:un)?signed|
486938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
487938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
488938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
489938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
490938224b5SJoe Perches
491938224b5SJoe Perches	int\s+(?:un)?signed|
492938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
493938224b5SJoe Perches)};
494938224b5SJoe Perches
495485ff23eSAlex Dowadour @typeListFile = ();
4968716de38SJoe Perchesour @typeListWithAttr = (
4978716de38SJoe Perches	@typeList,
4988716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
4998716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
5008716de38SJoe Perches);
5018716de38SJoe Perches
502c45dcabdSAndy Whitcroftour @modifierList = (
503c45dcabdSAndy Whitcroft	qr{fastcall},
504c45dcabdSAndy Whitcroft);
505485ff23eSAlex Dowadour @modifierListFile = ();
5068905a67cSAndy Whitcroft
5072435880fSJoe Perchesour @mode_permission_funcs = (
5082435880fSJoe Perches	["module_param", 3],
5092435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
5102435880fSJoe Perches	["module_param_array_named", 5],
5112435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
5122435880fSJoe Perches	["proc_create(?:_data|)", 2],
5132435880fSJoe Perches	["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
5142435880fSJoe Perches);
5152435880fSJoe Perches
516515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
517515a235eSJoe Perchesour $mode_perms_search = "";
518515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
519515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
520515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
521515a235eSJoe Perches}
522515a235eSJoe Perches
523b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
524b392c64fSJoe Perches	S_IWUGO		|
525b392c64fSJoe Perches	S_IWOTH		|
526b392c64fSJoe Perches	S_IRWXUGO	|
527b392c64fSJoe Perches	S_IALLUGO	|
528b392c64fSJoe Perches	0[0-7][0-7][2367]
529b392c64fSJoe Perches}x;
530b392c64fSJoe Perches
5317840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
5327840a94cSWolfram Sang	irq|
533cdcee686SSergey Ryazanov	memory|
534cdcee686SSergey Ryazanov	time|
535cdcee686SSergey Ryazanov	reboot
5367840a94cSWolfram Sang)};
5377840a94cSWolfram Sang# memory.h: ARM has a custom one
5387840a94cSWolfram Sang
53966b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
54066b47b4aSKees Cookmy $misspellings;
54166b47b4aSKees Cookmy %spelling_fix;
54236061e38SJoe Perches
54336061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
54466b47b4aSKees Cook	while (<$spelling>) {
54566b47b4aSKees Cook		my $line = $_;
54666b47b4aSKees Cook
54766b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
54866b47b4aSKees Cook		$line =~ s/^\s*//g;
54966b47b4aSKees Cook
55066b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
55166b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
55266b47b4aSKees Cook
55366b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
55466b47b4aSKees Cook
55566b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
55666b47b4aSKees Cook	}
55766b47b4aSKees Cook	close($spelling);
55836061e38SJoe Perches} else {
55936061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
56036061e38SJoe Perches}
56166b47b4aSKees Cook
562ebfd7d62SJoe Perchesif ($codespell) {
563ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
564ebfd7d62SJoe Perches		while (<$spelling>) {
565ebfd7d62SJoe Perches			my $line = $_;
566ebfd7d62SJoe Perches
567ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
568ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
569ebfd7d62SJoe Perches
570ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
571ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
572ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
573ebfd7d62SJoe Perches
574ebfd7d62SJoe Perches			$line =~ s/,.*$//;
575ebfd7d62SJoe Perches
576ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
577ebfd7d62SJoe Perches
578ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
579ebfd7d62SJoe Perches		}
580ebfd7d62SJoe Perches		close($spelling);
581ebfd7d62SJoe Perches	} else {
582ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
583ebfd7d62SJoe Perches	}
584ebfd7d62SJoe Perches}
585ebfd7d62SJoe Perches
586ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
587ebfd7d62SJoe Perches
5888905a67cSAndy Whitcroftsub build_types {
589485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
590485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
5911813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
5928716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
593c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
594ab7e23f3SJoe Perches	$BasicType	= qr{
595ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
596ab7e23f3SJoe Perches				(?:${all}\b)
597ab7e23f3SJoe Perches		}x;
5988905a67cSAndy Whitcroft	$NonptrType	= qr{
599d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
600cf655043SAndy Whitcroft			(?:
6016b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
6028ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
603c45dcabdSAndy Whitcroft				(?:${all}\b)
604cf655043SAndy Whitcroft			)
605c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
6068905a67cSAndy Whitcroft		  }x;
6071813087dSJoe Perches	$NonptrTypeMisordered	= qr{
6081813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
6091813087dSJoe Perches			(?:
6101813087dSJoe Perches				(?:${Misordered}\b)
6111813087dSJoe Perches			)
6121813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
6131813087dSJoe Perches		  }x;
6148716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
6158716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
6168716de38SJoe Perches			(?:
6178716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
6188716de38SJoe Perches				(?:$typeTypedefs\b)|
6198716de38SJoe Perches				(?:${allWithAttr}\b)
6208716de38SJoe Perches			)
6218716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
6228716de38SJoe Perches		  }x;
6238905a67cSAndy Whitcroft	$Type	= qr{
624c45dcabdSAndy Whitcroft			$NonptrType
6251574a29fSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
626c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
6278905a67cSAndy Whitcroft		  }x;
6281813087dSJoe Perches	$TypeMisordered	= qr{
6291813087dSJoe Perches			$NonptrTypeMisordered
6301813087dSJoe Perches			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
6311813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
6321813087dSJoe Perches		  }x;
63391cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
6341813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
6358905a67cSAndy Whitcroft}
6368905a67cSAndy Whitcroftbuild_types();
6376c72ffaaSAndy Whitcroft
6387d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
639d1fe9c09SJoe Perches
640d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
641d1fe9c09SJoe Perches# requires at least perl version v5.10.0
642d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
643d1fe9c09SJoe Perches
644d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
6452435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
646c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
6477d2367afSJoe Perches
648f8422308SJoe Perchesour $declaration_macros = qr{(?x:
6493e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
650f8422308SJoe Perches	(?:$Storage\s+)?LIST_HEAD\s*\(|
651f8422308SJoe Perches	(?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
652f8422308SJoe Perches)};
653f8422308SJoe Perches
6547d2367afSJoe Perchessub deparenthesize {
6557d2367afSJoe Perches	my ($string) = @_;
6567d2367afSJoe Perches	return "" if (!defined($string));
6575b9553abSJoe Perches
6585b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
6595b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
6605b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
6615b9553abSJoe Perches	}
6625b9553abSJoe Perches
6637d2367afSJoe Perches	$string =~ s@\s+@ @g;
6645b9553abSJoe Perches
6657d2367afSJoe Perches	return $string;
6667d2367afSJoe Perches}
6677d2367afSJoe Perches
6683445686aSJoe Perchessub seed_camelcase_file {
6693445686aSJoe Perches	my ($file) = @_;
6703445686aSJoe Perches
6713445686aSJoe Perches	return if (!(-f $file));
6723445686aSJoe Perches
6733445686aSJoe Perches	local $/;
6743445686aSJoe Perches
6753445686aSJoe Perches	open(my $include_file, '<', "$file")
6763445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
6773445686aSJoe Perches	my $text = <$include_file>;
6783445686aSJoe Perches	close($include_file);
6793445686aSJoe Perches
6803445686aSJoe Perches	my @lines = split('\n', $text);
6813445686aSJoe Perches
6823445686aSJoe Perches	foreach my $line (@lines) {
6833445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
6843445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
6853445686aSJoe Perches			$camelcase{$1} = 1;
68611ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
68711ea516aSJoe Perches			$camelcase{$1} = 1;
68811ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
6893445686aSJoe Perches			$camelcase{$1} = 1;
6903445686aSJoe Perches		}
6913445686aSJoe Perches	}
6923445686aSJoe Perches}
6933445686aSJoe Perches
6943445686aSJoe Perchesmy $camelcase_seeded = 0;
6953445686aSJoe Perchessub seed_camelcase_includes {
6963445686aSJoe Perches	return if ($camelcase_seeded);
6973445686aSJoe Perches
6983445686aSJoe Perches	my $files;
699c707a81dSJoe Perches	my $camelcase_cache = "";
700c707a81dSJoe Perches	my @include_files = ();
701c707a81dSJoe Perches
702c707a81dSJoe Perches	$camelcase_seeded = 1;
703351b2a1fSJoe Perches
7043645e328SRichard Genoud	if (-e ".git") {
705351b2a1fSJoe Perches		my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
706351b2a1fSJoe Perches		chomp $git_last_include_commit;
707c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
708c707a81dSJoe Perches	} else {
709c707a81dSJoe Perches		my $last_mod_date = 0;
710c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
711c707a81dSJoe Perches		@include_files = split('\n', $files);
712c707a81dSJoe Perches		foreach my $file (@include_files) {
713c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
714c707a81dSJoe Perches						   localtime((stat $file)[9]));
715c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
716c707a81dSJoe Perches		}
717c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
718c707a81dSJoe Perches	}
719c707a81dSJoe Perches
720c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
721c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
722c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
723351b2a1fSJoe Perches		while (<$camelcase_file>) {
724351b2a1fSJoe Perches			chomp;
725351b2a1fSJoe Perches			$camelcase{$_} = 1;
726351b2a1fSJoe Perches		}
727351b2a1fSJoe Perches		close($camelcase_file);
728351b2a1fSJoe Perches
729351b2a1fSJoe Perches		return;
730351b2a1fSJoe Perches	}
731c707a81dSJoe Perches
7323645e328SRichard Genoud	if (-e ".git") {
733c707a81dSJoe Perches		$files = `git ls-files "include/*.h"`;
734c707a81dSJoe Perches		@include_files = split('\n', $files);
7353445686aSJoe Perches	}
736c707a81dSJoe Perches
7373445686aSJoe Perches	foreach my $file (@include_files) {
7383445686aSJoe Perches		seed_camelcase_file($file);
7393445686aSJoe Perches	}
740351b2a1fSJoe Perches
741c707a81dSJoe Perches	if ($camelcase_cache ne "") {
742351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
743c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
744c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
745351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
746351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
747351b2a1fSJoe Perches		}
748351b2a1fSJoe Perches		close($camelcase_file);
749351b2a1fSJoe Perches	}
7503445686aSJoe Perches}
7513445686aSJoe Perches
752d311cd44SJoe Perchessub git_commit_info {
753d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
754d311cd44SJoe Perches
755d311cd44SJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
756d311cd44SJoe Perches
757d311cd44SJoe Perches	my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
758d311cd44SJoe Perches	$output =~ s/^\s*//gm;
759d311cd44SJoe Perches	my @lines = split("\n", $output);
760d311cd44SJoe Perches
7610d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
7620d7835fcSJoe Perches
763d311cd44SJoe Perches	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
764d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
765d311cd44SJoe Perches# all matching commit ids, but it's very slow...
766d311cd44SJoe Perches#
767d311cd44SJoe Perches#		echo "checking commits $1..."
768d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
769d311cd44SJoe Perches#		while read line ; do
770d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
771d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
772d311cd44SJoe Perches#		done
773d311cd44SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
774d311cd44SJoe Perches	} else {
775d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
776d311cd44SJoe Perches		$desc = substr($lines[0], 41);
777d311cd44SJoe Perches	}
778d311cd44SJoe Perches
779d311cd44SJoe Perches	return ($id, $desc);
780d311cd44SJoe Perches}
781d311cd44SJoe Perches
7826c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
7830a920b5bSAndy Whitcroft
78400df344fSAndy Whitcroftmy @rawlines = ();
785c2fdda0dSAndy Whitcroftmy @lines = ();
7863705ce5bSJoe Perchesmy @fixed = ();
787d752fcc8SJoe Perchesmy @fixed_inserted = ();
788d752fcc8SJoe Perchesmy @fixed_deleted = ();
789194f66fcSJoe Perchesmy $fixlinenr = -1;
790194f66fcSJoe Perches
791c2fdda0dSAndy Whitcroftmy $vname;
7926c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
79321caa13cSAndy Whitcroft	my $FILE;
7946c72ffaaSAndy Whitcroft	if ($file) {
79521caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
7966c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
79721caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
79821caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
7996c72ffaaSAndy Whitcroft	} else {
80021caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
8016c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
8026c72ffaaSAndy Whitcroft	}
803c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
804c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
805c2fdda0dSAndy Whitcroft	} else {
806c2fdda0dSAndy Whitcroft		$vname = $filename;
807c2fdda0dSAndy Whitcroft	}
80821caa13cSAndy Whitcroft	while (<$FILE>) {
8090a920b5bSAndy Whitcroft		chomp;
81000df344fSAndy Whitcroft		push(@rawlines, $_);
8116c72ffaaSAndy Whitcroft	}
81221caa13cSAndy Whitcroft	close($FILE);
813d8469f16SJoe Perches
814d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
815d8469f16SJoe Perches		print '-' x length($vname) . "\n";
816d8469f16SJoe Perches		print "$vname\n";
817d8469f16SJoe Perches		print '-' x length($vname) . "\n";
818d8469f16SJoe Perches	}
819d8469f16SJoe Perches
820c2fdda0dSAndy Whitcroft	if (!process($filename)) {
8210a920b5bSAndy Whitcroft		$exit = 1;
8220a920b5bSAndy Whitcroft	}
82300df344fSAndy Whitcroft	@rawlines = ();
82413214adfSAndy Whitcroft	@lines = ();
8253705ce5bSJoe Perches	@fixed = ();
826d752fcc8SJoe Perches	@fixed_inserted = ();
827d752fcc8SJoe Perches	@fixed_deleted = ();
828194f66fcSJoe Perches	$fixlinenr = -1;
829485ff23eSAlex Dowad	@modifierListFile = ();
830485ff23eSAlex Dowad	@typeListFile = ();
831485ff23eSAlex Dowad	build_types();
8320a920b5bSAndy Whitcroft}
8330a920b5bSAndy Whitcroft
834d8469f16SJoe Perchesif (!$quiet) {
8353c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
8363c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
8373c816e49SJoe Perches
838d8469f16SJoe Perches	if ($^V lt 5.10.0) {
839d8469f16SJoe Perches		print << "EOM"
840d8469f16SJoe Perches
841d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
842d8469f16SJoe Perches      An upgrade to at least perl v5.10.0 is suggested.
843d8469f16SJoe PerchesEOM
844d8469f16SJoe Perches	}
845d8469f16SJoe Perches	if ($exit) {
846d8469f16SJoe Perches		print << "EOM"
847d8469f16SJoe Perches
848d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
849d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
850d8469f16SJoe PerchesEOM
851d8469f16SJoe Perches	}
852d8469f16SJoe Perches}
853d8469f16SJoe Perches
8540a920b5bSAndy Whitcroftexit($exit);
8550a920b5bSAndy Whitcroft
8560a920b5bSAndy Whitcroftsub top_of_kernel_tree {
8576c72ffaaSAndy Whitcroft	my ($root) = @_;
8586c72ffaaSAndy Whitcroft
8596c72ffaaSAndy Whitcroft	my @tree_check = (
8606c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
8616c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
8626c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
8636c72ffaaSAndy Whitcroft	);
8646c72ffaaSAndy Whitcroft
8656c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
8666c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
8670a920b5bSAndy Whitcroft			return 0;
8680a920b5bSAndy Whitcroft		}
8696c72ffaaSAndy Whitcroft	}
8706c72ffaaSAndy Whitcroft	return 1;
8716c72ffaaSAndy Whitcroft}
8720a920b5bSAndy Whitcroft
87320112475SJoe Perchessub parse_email {
87420112475SJoe Perches	my ($formatted_email) = @_;
87520112475SJoe Perches
87620112475SJoe Perches	my $name = "";
87720112475SJoe Perches	my $address = "";
87820112475SJoe Perches	my $comment = "";
87920112475SJoe Perches
88020112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
88120112475SJoe Perches		$name = $1;
88220112475SJoe Perches		$address = $2;
88320112475SJoe Perches		$comment = $3 if defined $3;
88420112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
88520112475SJoe Perches		$address = $1;
88620112475SJoe Perches		$comment = $2 if defined $2;
88720112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
88820112475SJoe Perches		$address = $1;
88920112475SJoe Perches		$comment = $2 if defined $2;
89020112475SJoe Perches		$formatted_email =~ s/$address.*$//;
89120112475SJoe Perches		$name = $formatted_email;
8923705ce5bSJoe Perches		$name = trim($name);
89320112475SJoe Perches		$name =~ s/^\"|\"$//g;
89420112475SJoe Perches		# If there's a name left after stripping spaces and
89520112475SJoe Perches		# leading quotes, and the address doesn't have both
89620112475SJoe Perches		# leading and trailing angle brackets, the address
89720112475SJoe Perches		# is invalid. ie:
89820112475SJoe Perches		#   "joe smith [email protected]" bad
89920112475SJoe Perches		#   "joe smith <[email protected]" bad
90020112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
90120112475SJoe Perches			$name = "";
90220112475SJoe Perches			$address = "";
90320112475SJoe Perches			$comment = "";
90420112475SJoe Perches		}
90520112475SJoe Perches	}
90620112475SJoe Perches
9073705ce5bSJoe Perches	$name = trim($name);
90820112475SJoe Perches	$name =~ s/^\"|\"$//g;
9093705ce5bSJoe Perches	$address = trim($address);
91020112475SJoe Perches	$address =~ s/^\<|\>$//g;
91120112475SJoe Perches
91220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
91320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
91420112475SJoe Perches		$name = "\"$name\"";
91520112475SJoe Perches	}
91620112475SJoe Perches
91720112475SJoe Perches	return ($name, $address, $comment);
91820112475SJoe Perches}
91920112475SJoe Perches
92020112475SJoe Perchessub format_email {
92120112475SJoe Perches	my ($name, $address) = @_;
92220112475SJoe Perches
92320112475SJoe Perches	my $formatted_email;
92420112475SJoe Perches
9253705ce5bSJoe Perches	$name = trim($name);
92620112475SJoe Perches	$name =~ s/^\"|\"$//g;
9273705ce5bSJoe Perches	$address = trim($address);
92820112475SJoe Perches
92920112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
93020112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
93120112475SJoe Perches		$name = "\"$name\"";
93220112475SJoe Perches	}
93320112475SJoe Perches
93420112475SJoe Perches	if ("$name" eq "") {
93520112475SJoe Perches		$formatted_email = "$address";
93620112475SJoe Perches	} else {
93720112475SJoe Perches		$formatted_email = "$name <$address>";
93820112475SJoe Perches	}
93920112475SJoe Perches
94020112475SJoe Perches	return $formatted_email;
94120112475SJoe Perches}
94220112475SJoe Perches
943d311cd44SJoe Perchessub which {
944d311cd44SJoe Perches	my ($bin) = @_;
945d311cd44SJoe Perches
946d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
947d311cd44SJoe Perches		if (-e "$path/$bin") {
948d311cd44SJoe Perches			return "$path/$bin";
949d311cd44SJoe Perches		}
950d311cd44SJoe Perches	}
951d311cd44SJoe Perches
952d311cd44SJoe Perches	return "";
953d311cd44SJoe Perches}
954d311cd44SJoe Perches
955000d1cc1SJoe Perchessub which_conf {
956000d1cc1SJoe Perches	my ($conf) = @_;
957000d1cc1SJoe Perches
958000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
959000d1cc1SJoe Perches		if (-e "$path/$conf") {
960000d1cc1SJoe Perches			return "$path/$conf";
961000d1cc1SJoe Perches		}
962000d1cc1SJoe Perches	}
963000d1cc1SJoe Perches
964000d1cc1SJoe Perches	return "";
965000d1cc1SJoe Perches}
966000d1cc1SJoe Perches
9670a920b5bSAndy Whitcroftsub expand_tabs {
9680a920b5bSAndy Whitcroft	my ($str) = @_;
9690a920b5bSAndy Whitcroft
9700a920b5bSAndy Whitcroft	my $res = '';
9710a920b5bSAndy Whitcroft	my $n = 0;
9720a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
9730a920b5bSAndy Whitcroft		if ($c eq "\t") {
9740a920b5bSAndy Whitcroft			$res .= ' ';
9750a920b5bSAndy Whitcroft			$n++;
9760a920b5bSAndy Whitcroft			for (; ($n % 8) != 0; $n++) {
9770a920b5bSAndy Whitcroft				$res .= ' ';
9780a920b5bSAndy Whitcroft			}
9790a920b5bSAndy Whitcroft			next;
9800a920b5bSAndy Whitcroft		}
9810a920b5bSAndy Whitcroft		$res .= $c;
9820a920b5bSAndy Whitcroft		$n++;
9830a920b5bSAndy Whitcroft	}
9840a920b5bSAndy Whitcroft
9850a920b5bSAndy Whitcroft	return $res;
9860a920b5bSAndy Whitcroft}
9876c72ffaaSAndy Whitcroftsub copy_spacing {
988773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
9896c72ffaaSAndy Whitcroft	return $res;
9906c72ffaaSAndy Whitcroft}
9910a920b5bSAndy Whitcroft
9924a0df2efSAndy Whitcroftsub line_stats {
9934a0df2efSAndy Whitcroft	my ($line) = @_;
9944a0df2efSAndy Whitcroft
9954a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
9964a0df2efSAndy Whitcroft	$line =~ s/^.//;
9974a0df2efSAndy Whitcroft	$line = expand_tabs($line);
9984a0df2efSAndy Whitcroft
9994a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
10004a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
10014a0df2efSAndy Whitcroft
10024a0df2efSAndy Whitcroft	return (length($line), length($white));
10034a0df2efSAndy Whitcroft}
10044a0df2efSAndy Whitcroft
1005773647a0SAndy Whitcroftmy $sanitise_quote = '';
1006773647a0SAndy Whitcroft
1007773647a0SAndy Whitcroftsub sanitise_line_reset {
1008773647a0SAndy Whitcroft	my ($in_comment) = @_;
1009773647a0SAndy Whitcroft
1010773647a0SAndy Whitcroft	if ($in_comment) {
1011773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1012773647a0SAndy Whitcroft	} else {
1013773647a0SAndy Whitcroft		$sanitise_quote = '';
1014773647a0SAndy Whitcroft	}
1015773647a0SAndy Whitcroft}
101600df344fSAndy Whitcroftsub sanitise_line {
101700df344fSAndy Whitcroft	my ($line) = @_;
101800df344fSAndy Whitcroft
101900df344fSAndy Whitcroft	my $res = '';
102000df344fSAndy Whitcroft	my $l = '';
102100df344fSAndy Whitcroft
1022c2fdda0dSAndy Whitcroft	my $qlen = 0;
1023773647a0SAndy Whitcroft	my $off = 0;
1024773647a0SAndy Whitcroft	my $c;
102500df344fSAndy Whitcroft
1026773647a0SAndy Whitcroft	# Always copy over the diff marker.
1027773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1028773647a0SAndy Whitcroft
1029773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1030773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1031773647a0SAndy Whitcroft
1032773647a0SAndy Whitcroft		# Comments we are wacking completly including the begin
1033773647a0SAndy Whitcroft		# and end, all to $;.
1034773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1035773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1036773647a0SAndy Whitcroft
1037773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1038773647a0SAndy Whitcroft			$off++;
103900df344fSAndy Whitcroft			next;
1040773647a0SAndy Whitcroft		}
104181bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1042773647a0SAndy Whitcroft			$sanitise_quote = '';
1043773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1044773647a0SAndy Whitcroft			$off++;
1045773647a0SAndy Whitcroft			next;
1046773647a0SAndy Whitcroft		}
1047113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1048113f04a8SDaniel Walker			$sanitise_quote = '//';
1049113f04a8SDaniel Walker
1050113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1051113f04a8SDaniel Walker			$off++;
1052113f04a8SDaniel Walker			next;
1053113f04a8SDaniel Walker		}
1054773647a0SAndy Whitcroft
1055773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1056773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1057773647a0SAndy Whitcroft		    $c eq "\\") {
1058773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1059773647a0SAndy Whitcroft			$off++;
1060773647a0SAndy Whitcroft			next;
1061773647a0SAndy Whitcroft		}
1062773647a0SAndy Whitcroft		# Regular quotes.
1063773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1064773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1065773647a0SAndy Whitcroft				$sanitise_quote = $c;
1066773647a0SAndy Whitcroft
1067773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1068773647a0SAndy Whitcroft				next;
1069773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1070773647a0SAndy Whitcroft				$sanitise_quote = '';
107100df344fSAndy Whitcroft			}
107200df344fSAndy Whitcroft		}
1073773647a0SAndy Whitcroft
1074fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1075773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1076773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1077113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1078113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1079773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1080773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
108100df344fSAndy Whitcroft		} else {
1082773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
108300df344fSAndy Whitcroft		}
1084c2fdda0dSAndy Whitcroft	}
1085c2fdda0dSAndy Whitcroft
1086113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1087113f04a8SDaniel Walker		$sanitise_quote = '';
1088113f04a8SDaniel Walker	}
1089113f04a8SDaniel Walker
1090c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1091c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1092c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1093c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1094c2fdda0dSAndy Whitcroft
1095c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1096c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1097c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1098c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1099c2fdda0dSAndy Whitcroft	}
1100c2fdda0dSAndy Whitcroft
110100df344fSAndy Whitcroft	return $res;
110200df344fSAndy Whitcroft}
110300df344fSAndy Whitcroft
1104a6962d72SJoe Perchessub get_quoted_string {
1105a6962d72SJoe Perches	my ($line, $rawline) = @_;
1106a6962d72SJoe Perches
110733acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1108a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1109a6962d72SJoe Perches}
1110a6962d72SJoe Perches
11118905a67cSAndy Whitcroftsub ctx_statement_block {
11128905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
11138905a67cSAndy Whitcroft	my $line = $linenr - 1;
11148905a67cSAndy Whitcroft	my $blk = '';
11158905a67cSAndy Whitcroft	my $soff = $off;
11168905a67cSAndy Whitcroft	my $coff = $off - 1;
1117773647a0SAndy Whitcroft	my $coff_set = 0;
11188905a67cSAndy Whitcroft
111913214adfSAndy Whitcroft	my $loff = 0;
112013214adfSAndy Whitcroft
11218905a67cSAndy Whitcroft	my $type = '';
11228905a67cSAndy Whitcroft	my $level = 0;
1123a2750645SAndy Whitcroft	my @stack = ();
1124cf655043SAndy Whitcroft	my $p;
11258905a67cSAndy Whitcroft	my $c;
11268905a67cSAndy Whitcroft	my $len = 0;
112713214adfSAndy Whitcroft
112813214adfSAndy Whitcroft	my $remainder;
11298905a67cSAndy Whitcroft	while (1) {
1130a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1131a2750645SAndy Whitcroft
1132773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
11338905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
11348905a67cSAndy Whitcroft		# context.
11358905a67cSAndy Whitcroft		if ($off >= $len) {
11368905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1137dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1138c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
11398905a67cSAndy Whitcroft				$remain--;
114013214adfSAndy Whitcroft				$loff = $len;
1141c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
11428905a67cSAndy Whitcroft				$len = length($blk);
11438905a67cSAndy Whitcroft				$line++;
11448905a67cSAndy Whitcroft				last;
11458905a67cSAndy Whitcroft			}
11468905a67cSAndy Whitcroft			# Bail if there is no further context.
11478905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
114813214adfSAndy Whitcroft			if ($off >= $len) {
11498905a67cSAndy Whitcroft				last;
11508905a67cSAndy Whitcroft			}
1151f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1152f74bd194SAndy Whitcroft				$level++;
1153f74bd194SAndy Whitcroft				$type = '#';
1154f74bd194SAndy Whitcroft			}
11558905a67cSAndy Whitcroft		}
1156cf655043SAndy Whitcroft		$p = $c;
11578905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
115813214adfSAndy Whitcroft		$remainder = substr($blk, $off);
11598905a67cSAndy Whitcroft
1160773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
11614635f4fbSAndy Whitcroft
11624635f4fbSAndy Whitcroft		# Handle nested #if/#else.
11634635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
11644635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
11654635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
11664635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
11674635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
11684635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
11694635f4fbSAndy Whitcroft		}
11704635f4fbSAndy Whitcroft
11718905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
11728905a67cSAndy Whitcroft		# outermost level.
11738905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
11748905a67cSAndy Whitcroft			last;
11758905a67cSAndy Whitcroft		}
11768905a67cSAndy Whitcroft
117713214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1178773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1179773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1180773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1181773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1182773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1183773647a0SAndy Whitcroft			$coff_set = 1;
1184773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1185773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
118613214adfSAndy Whitcroft		}
118713214adfSAndy Whitcroft
11888905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
11898905a67cSAndy Whitcroft			$level++;
11908905a67cSAndy Whitcroft			$type = '(';
11918905a67cSAndy Whitcroft		}
11928905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
11938905a67cSAndy Whitcroft			$level--;
11948905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
11958905a67cSAndy Whitcroft
11968905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
11978905a67cSAndy Whitcroft				$coff = $off;
1198773647a0SAndy Whitcroft				$coff_set = 1;
1199773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
12008905a67cSAndy Whitcroft			}
12018905a67cSAndy Whitcroft		}
12028905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
12038905a67cSAndy Whitcroft			$level++;
12048905a67cSAndy Whitcroft			$type = '{';
12058905a67cSAndy Whitcroft		}
12068905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
12078905a67cSAndy Whitcroft			$level--;
12088905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
12098905a67cSAndy Whitcroft
12108905a67cSAndy Whitcroft			if ($level == 0) {
1211b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1212b998e001SPatrick Pannuto					$off++;
1213b998e001SPatrick Pannuto				}
12148905a67cSAndy Whitcroft				last;
12158905a67cSAndy Whitcroft			}
12168905a67cSAndy Whitcroft		}
1217f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1218f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1219f74bd194SAndy Whitcroft			$level--;
1220f74bd194SAndy Whitcroft			$type = '';
1221f74bd194SAndy Whitcroft			$off++;
1222f74bd194SAndy Whitcroft			last;
1223f74bd194SAndy Whitcroft		}
12248905a67cSAndy Whitcroft		$off++;
12258905a67cSAndy Whitcroft	}
1226a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
122713214adfSAndy Whitcroft	if ($off == $len) {
1228a3bb97a7SAndy Whitcroft		$loff = $len + 1;
122913214adfSAndy Whitcroft		$line++;
123013214adfSAndy Whitcroft		$remain--;
123113214adfSAndy Whitcroft	}
12328905a67cSAndy Whitcroft
12338905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
12348905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
12358905a67cSAndy Whitcroft
12368905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
12378905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
12388905a67cSAndy Whitcroft
1239773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
124013214adfSAndy Whitcroft
124113214adfSAndy Whitcroft	return ($statement, $condition,
124213214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
124313214adfSAndy Whitcroft}
124413214adfSAndy Whitcroft
1245cf655043SAndy Whitcroftsub statement_lines {
1246cf655043SAndy Whitcroft	my ($stmt) = @_;
1247cf655043SAndy Whitcroft
1248cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1249cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1250cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1251cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1252cf655043SAndy Whitcroft
1253cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1254cf655043SAndy Whitcroft
1255cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1256cf655043SAndy Whitcroft}
1257cf655043SAndy Whitcroft
1258cf655043SAndy Whitcroftsub statement_rawlines {
1259cf655043SAndy Whitcroft	my ($stmt) = @_;
1260cf655043SAndy Whitcroft
1261cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1262cf655043SAndy Whitcroft
1263cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1264cf655043SAndy Whitcroft}
1265cf655043SAndy Whitcroft
1266cf655043SAndy Whitcroftsub statement_block_size {
1267cf655043SAndy Whitcroft	my ($stmt) = @_;
1268cf655043SAndy Whitcroft
1269cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1270cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1271cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1272cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1273cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1274cf655043SAndy Whitcroft
1275cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1276cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1277cf655043SAndy Whitcroft
1278cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1279cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1280cf655043SAndy Whitcroft
1281cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1282cf655043SAndy Whitcroft		return $stmt_lines;
1283cf655043SAndy Whitcroft	} else {
1284cf655043SAndy Whitcroft		return $stmt_statements;
1285cf655043SAndy Whitcroft	}
1286cf655043SAndy Whitcroft}
1287cf655043SAndy Whitcroft
128813214adfSAndy Whitcroftsub ctx_statement_full {
128913214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
129013214adfSAndy Whitcroft	my ($statement, $condition, $level);
129113214adfSAndy Whitcroft
129213214adfSAndy Whitcroft	my (@chunks);
129313214adfSAndy Whitcroft
1294cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
129513214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
129613214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1297773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
129813214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1299cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1300cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1301cf655043SAndy Whitcroft	}
1302cf655043SAndy Whitcroft
1303cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1304cf655043SAndy Whitcroft	# could continue the statement.
1305cf655043SAndy Whitcroft	for (;;) {
130613214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
130713214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1308cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1309773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1310cf655043SAndy Whitcroft		#print "C: push\n";
1311cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
131213214adfSAndy Whitcroft	}
131313214adfSAndy Whitcroft
131413214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
13158905a67cSAndy Whitcroft}
13168905a67cSAndy Whitcroft
13174a0df2efSAndy Whitcroftsub ctx_block_get {
1318f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
13194a0df2efSAndy Whitcroft	my $line;
13204a0df2efSAndy Whitcroft	my $start = $linenr - 1;
13214a0df2efSAndy Whitcroft	my $blk = '';
13224a0df2efSAndy Whitcroft	my @o;
13234a0df2efSAndy Whitcroft	my @c;
13244a0df2efSAndy Whitcroft	my @res = ();
13254a0df2efSAndy Whitcroft
1326f0a594c1SAndy Whitcroft	my $level = 0;
13274635f4fbSAndy Whitcroft	my @stack = ($level);
132800df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
132900df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
133000df344fSAndy Whitcroft		$remain--;
133100df344fSAndy Whitcroft
133200df344fSAndy Whitcroft		$blk .= $rawlines[$line];
13334635f4fbSAndy Whitcroft
13344635f4fbSAndy Whitcroft		# Handle nested #if/#else.
133501464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
13364635f4fbSAndy Whitcroft			push(@stack, $level);
133701464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
13384635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
133901464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
13404635f4fbSAndy Whitcroft			$level = pop(@stack);
13414635f4fbSAndy Whitcroft		}
13424635f4fbSAndy Whitcroft
134301464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1344f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1345f0a594c1SAndy Whitcroft			if ($off > 0) {
1346f0a594c1SAndy Whitcroft				$off--;
1347f0a594c1SAndy Whitcroft				next;
1348f0a594c1SAndy Whitcroft			}
13494a0df2efSAndy Whitcroft
1350f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1351f0a594c1SAndy Whitcroft				$level--;
1352f0a594c1SAndy Whitcroft				last if ($level == 0);
1353f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1354f0a594c1SAndy Whitcroft				$level++;
1355f0a594c1SAndy Whitcroft			}
1356f0a594c1SAndy Whitcroft		}
13574a0df2efSAndy Whitcroft
1358f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
135900df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
13604a0df2efSAndy Whitcroft		}
13614a0df2efSAndy Whitcroft
1362f0a594c1SAndy Whitcroft		last if ($level == 0);
13634a0df2efSAndy Whitcroft	}
13644a0df2efSAndy Whitcroft
1365f0a594c1SAndy Whitcroft	return ($level, @res);
13664a0df2efSAndy Whitcroft}
13674a0df2efSAndy Whitcroftsub ctx_block_outer {
13684a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
13694a0df2efSAndy Whitcroft
1370f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1371f0a594c1SAndy Whitcroft	return @r;
13724a0df2efSAndy Whitcroft}
13734a0df2efSAndy Whitcroftsub ctx_block {
13744a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
13754a0df2efSAndy Whitcroft
1376f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1377f0a594c1SAndy Whitcroft	return @r;
1378653d4876SAndy Whitcroft}
1379653d4876SAndy Whitcroftsub ctx_statement {
1380f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1381f0a594c1SAndy Whitcroft
1382f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1383f0a594c1SAndy Whitcroft	return @r;
1384f0a594c1SAndy Whitcroft}
1385f0a594c1SAndy Whitcroftsub ctx_block_level {
1386653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1387653d4876SAndy Whitcroft
1388f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
13894a0df2efSAndy Whitcroft}
13909c0ca6f9SAndy Whitcroftsub ctx_statement_level {
13919c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
13929c0ca6f9SAndy Whitcroft
13939c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
13949c0ca6f9SAndy Whitcroft}
13954a0df2efSAndy Whitcroft
13964a0df2efSAndy Whitcroftsub ctx_locate_comment {
13974a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
13984a0df2efSAndy Whitcroft
13994a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1400beae6332SAndy Whitcroft	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
14014a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
14024a0df2efSAndy Whitcroft
14034a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
14044a0df2efSAndy Whitcroft	# comment.
14054a0df2efSAndy Whitcroft	my $in_comment = 0;
14064a0df2efSAndy Whitcroft	$current_comment = '';
14074a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
140800df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
140900df344fSAndy Whitcroft		#warn "           $line\n";
14104a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
14114a0df2efSAndy Whitcroft			$in_comment = 1;
14124a0df2efSAndy Whitcroft		}
14134a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
14144a0df2efSAndy Whitcroft			$in_comment = 1;
14154a0df2efSAndy Whitcroft		}
14164a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
14174a0df2efSAndy Whitcroft			$current_comment = '';
14184a0df2efSAndy Whitcroft		}
14194a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
14204a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
14214a0df2efSAndy Whitcroft			$in_comment = 0;
14224a0df2efSAndy Whitcroft		}
14234a0df2efSAndy Whitcroft	}
14244a0df2efSAndy Whitcroft
14254a0df2efSAndy Whitcroft	chomp($current_comment);
14264a0df2efSAndy Whitcroft	return($current_comment);
14274a0df2efSAndy Whitcroft}
14284a0df2efSAndy Whitcroftsub ctx_has_comment {
14294a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
14304a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
14314a0df2efSAndy Whitcroft
143200df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
14334a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
14344a0df2efSAndy Whitcroft
14354a0df2efSAndy Whitcroft	return ($cmt ne '');
14364a0df2efSAndy Whitcroft}
14374a0df2efSAndy Whitcroft
14384d001e4dSAndy Whitcroftsub raw_line {
14394d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
14404d001e4dSAndy Whitcroft
14414d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
14424d001e4dSAndy Whitcroft	$cnt++;
14434d001e4dSAndy Whitcroft
14444d001e4dSAndy Whitcroft	my $line;
14454d001e4dSAndy Whitcroft	while ($cnt) {
14464d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
14474d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
14484d001e4dSAndy Whitcroft		$cnt--;
14494d001e4dSAndy Whitcroft	}
14504d001e4dSAndy Whitcroft
14514d001e4dSAndy Whitcroft	return $line;
14524d001e4dSAndy Whitcroft}
14534d001e4dSAndy Whitcroft
14540a920b5bSAndy Whitcroftsub cat_vet {
14550a920b5bSAndy Whitcroft	my ($vet) = @_;
14569c0ca6f9SAndy Whitcroft	my ($res, $coded);
14570a920b5bSAndy Whitcroft
14589c0ca6f9SAndy Whitcroft	$res = '';
14596c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
14606c72ffaaSAndy Whitcroft		$res .= $1;
14616c72ffaaSAndy Whitcroft		if ($2 ne '') {
14629c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
14636c72ffaaSAndy Whitcroft			$res .= $coded;
14646c72ffaaSAndy Whitcroft		}
14659c0ca6f9SAndy Whitcroft	}
14669c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
14670a920b5bSAndy Whitcroft
14689c0ca6f9SAndy Whitcroft	return $res;
14690a920b5bSAndy Whitcroft}
14700a920b5bSAndy Whitcroft
1471c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
1472cf655043SAndy Whitcroftmy $av_pending;
1473c2fdda0dSAndy Whitcroftmy @av_paren_type;
14741f65f947SAndy Whitcroftmy $av_pend_colon;
1475c2fdda0dSAndy Whitcroft
1476c2fdda0dSAndy Whitcroftsub annotate_reset {
1477c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
1478cf655043SAndy Whitcroft	$av_pending = '_';
1479cf655043SAndy Whitcroft	@av_paren_type = ('E');
14801f65f947SAndy Whitcroft	$av_pend_colon = 'O';
1481c2fdda0dSAndy Whitcroft}
1482c2fdda0dSAndy Whitcroft
14836c72ffaaSAndy Whitcroftsub annotate_values {
14846c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
14856c72ffaaSAndy Whitcroft
14866c72ffaaSAndy Whitcroft	my $res;
14871f65f947SAndy Whitcroft	my $var = '_' x length($stream);
14886c72ffaaSAndy Whitcroft	my $cur = $stream;
14896c72ffaaSAndy Whitcroft
1490c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
14916c72ffaaSAndy Whitcroft
14926c72ffaaSAndy Whitcroft	while (length($cur)) {
1493773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
1494cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
1495171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
14966c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
1497c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
1498c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
1499cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
1500c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
15016c72ffaaSAndy Whitcroft			}
15026c72ffaaSAndy Whitcroft
1503c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
15049446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
15059446ef56SAndy Whitcroft			push(@av_paren_type, $type);
1506addcdceaSAndy Whitcroft			$type = 'c';
15079446ef56SAndy Whitcroft
1508e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
1509c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
15106c72ffaaSAndy Whitcroft			$type = 'T';
15116c72ffaaSAndy Whitcroft
1512389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
1513389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
1514389a2fe5SAndy Whitcroft			$type = 'T';
1515389a2fe5SAndy Whitcroft
1516c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
1517171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
1518c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1519171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
1520171ae1a4SAndy Whitcroft			if ($2 ne '') {
1521cf655043SAndy Whitcroft				$av_pending = 'N';
1522171ae1a4SAndy Whitcroft			}
1523171ae1a4SAndy Whitcroft			$type = 'E';
1524171ae1a4SAndy Whitcroft
1525c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
1526171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
1527171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
1528171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
15296c72ffaaSAndy Whitcroft
1530c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
1531cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
1532c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
1533cf655043SAndy Whitcroft
1534cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1535cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1536171ae1a4SAndy Whitcroft			$type = 'E';
1537cf655043SAndy Whitcroft
1538c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
1539cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1540cf655043SAndy Whitcroft			$av_preprocessor = 1;
1541cf655043SAndy Whitcroft
1542cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1543cf655043SAndy Whitcroft
1544171ae1a4SAndy Whitcroft			$type = 'E';
1545cf655043SAndy Whitcroft
1546c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
1547cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
1548cf655043SAndy Whitcroft
1549cf655043SAndy Whitcroft			$av_preprocessor = 1;
1550cf655043SAndy Whitcroft
1551cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
1552cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
1553cf655043SAndy Whitcroft			pop(@av_paren_type);
1554cf655043SAndy Whitcroft			push(@av_paren_type, $type);
1555171ae1a4SAndy Whitcroft			$type = 'E';
15566c72ffaaSAndy Whitcroft
15576c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
1558c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
15596c72ffaaSAndy Whitcroft
1560171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1561171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
1562171ae1a4SAndy Whitcroft			$av_pending = $type;
1563171ae1a4SAndy Whitcroft			$type = 'N';
1564171ae1a4SAndy Whitcroft
15656c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
1566c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
15676c72ffaaSAndy Whitcroft			if (defined $2) {
1568cf655043SAndy Whitcroft				$av_pending = 'V';
15696c72ffaaSAndy Whitcroft			}
15706c72ffaaSAndy Whitcroft			$type = 'N';
15716c72ffaaSAndy Whitcroft
157214b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
1573c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
157414b111c1SAndy Whitcroft			$av_pending = 'E';
15756c72ffaaSAndy Whitcroft			$type = 'N';
15766c72ffaaSAndy Whitcroft
15771f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
15781f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
15791f65f947SAndy Whitcroft			$av_pend_colon = 'C';
15801f65f947SAndy Whitcroft			$type = 'N';
15811f65f947SAndy Whitcroft
158214b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
1583c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
15846c72ffaaSAndy Whitcroft			$type = 'N';
15856c72ffaaSAndy Whitcroft
15866c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
1587c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
1588cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
1589cf655043SAndy Whitcroft			$av_pending = '_';
15906c72ffaaSAndy Whitcroft			$type = 'N';
15916c72ffaaSAndy Whitcroft
15926c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
1593cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
1594cf655043SAndy Whitcroft			if ($new_type ne '_') {
1595cf655043SAndy Whitcroft				$type = $new_type;
1596c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
1597c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
15986c72ffaaSAndy Whitcroft			} else {
1599c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
16006c72ffaaSAndy Whitcroft			}
16016c72ffaaSAndy Whitcroft
1602c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
1603c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
1604c8cb2ca3SAndy Whitcroft			$type = 'V';
1605cf655043SAndy Whitcroft			$av_pending = 'V';
16066c72ffaaSAndy Whitcroft
16078e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
16088e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
16091f65f947SAndy Whitcroft				$av_pend_colon = 'B';
16108e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
16118e761b04SAndy Whitcroft				$av_pend_colon = 'L';
16121f65f947SAndy Whitcroft			}
16131f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
16141f65f947SAndy Whitcroft			$type = 'V';
16151f65f947SAndy Whitcroft
16166c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
1617c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
16186c72ffaaSAndy Whitcroft			$type = 'V';
16196c72ffaaSAndy Whitcroft
16206c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
1621c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
16226c72ffaaSAndy Whitcroft			$type = 'N';
16236c72ffaaSAndy Whitcroft
1624cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
1625c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
162613214adfSAndy Whitcroft			$type = 'E';
16271f65f947SAndy Whitcroft			$av_pend_colon = 'O';
162813214adfSAndy Whitcroft
16298e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
16308e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
16318e761b04SAndy Whitcroft			$type = 'C';
16328e761b04SAndy Whitcroft
16331f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
16341f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
16351f65f947SAndy Whitcroft			$type = 'N';
16361f65f947SAndy Whitcroft
16371f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
16381f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
16391f65f947SAndy Whitcroft
16401f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
16411f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
16421f65f947SAndy Whitcroft				$type = 'E';
16431f65f947SAndy Whitcroft			} else {
16441f65f947SAndy Whitcroft				$type = 'N';
16451f65f947SAndy Whitcroft			}
16461f65f947SAndy Whitcroft			$av_pend_colon = 'O';
16471f65f947SAndy Whitcroft
16488e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
164913214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
16506c72ffaaSAndy Whitcroft			$type = 'N';
16516c72ffaaSAndy Whitcroft
16520d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
165374048ed8SAndy Whitcroft			my $variant;
165474048ed8SAndy Whitcroft
165574048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
165674048ed8SAndy Whitcroft			if ($type eq 'V') {
165774048ed8SAndy Whitcroft				$variant = 'B';
165874048ed8SAndy Whitcroft			} else {
165974048ed8SAndy Whitcroft				$variant = 'U';
166074048ed8SAndy Whitcroft			}
166174048ed8SAndy Whitcroft
166274048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
166374048ed8SAndy Whitcroft			$type = 'N';
166474048ed8SAndy Whitcroft
16656c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
1666c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
16676c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
16686c72ffaaSAndy Whitcroft				$type = 'N';
16696c72ffaaSAndy Whitcroft			}
16706c72ffaaSAndy Whitcroft
16716c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
1672c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
16736c72ffaaSAndy Whitcroft		}
16746c72ffaaSAndy Whitcroft		if (defined $1) {
16756c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
16766c72ffaaSAndy Whitcroft			$res .= $type x length($1);
16776c72ffaaSAndy Whitcroft		}
16786c72ffaaSAndy Whitcroft	}
16796c72ffaaSAndy Whitcroft
16801f65f947SAndy Whitcroft	return ($res, $var);
16816c72ffaaSAndy Whitcroft}
16826c72ffaaSAndy Whitcroft
16838905a67cSAndy Whitcroftsub possible {
168413214adfSAndy Whitcroft	my ($possible, $line) = @_;
16859a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
16860776e594SAndy Whitcroft		^(?:
16870776e594SAndy Whitcroft			$Modifier|
16880776e594SAndy Whitcroft			$Storage|
16890776e594SAndy Whitcroft			$Type|
16909a974fdbSAndy Whitcroft			DEFINE_\S+
16919a974fdbSAndy Whitcroft		)$|
16929a974fdbSAndy Whitcroft		^(?:
16930776e594SAndy Whitcroft			goto|
16940776e594SAndy Whitcroft			return|
16950776e594SAndy Whitcroft			case|
16960776e594SAndy Whitcroft			else|
16970776e594SAndy Whitcroft			asm|__asm__|
169889a88353SAndy Whitcroft			do|
169989a88353SAndy Whitcroft			\#|
170089a88353SAndy Whitcroft			\#\#|
17019a974fdbSAndy Whitcroft		)(?:\s|$)|
17020776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
17039a974fdbSAndy Whitcroft	    )}x;
17049a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
17059a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
1706c45dcabdSAndy Whitcroft		# Check for modifiers.
1707c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
1708c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
1709c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
1710c45dcabdSAndy Whitcroft
1711c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
1712c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
1713d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
17149a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
1715d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
1716485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
1717d2506586SAndy Whitcroft				}
17189a974fdbSAndy Whitcroft			}
1719c45dcabdSAndy Whitcroft
1720c45dcabdSAndy Whitcroft		} else {
172113214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
1722485ff23eSAlex Dowad			push(@typeListFile, $possible);
1723c45dcabdSAndy Whitcroft		}
17248905a67cSAndy Whitcroft		build_types();
17250776e594SAndy Whitcroft	} else {
17260776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
17278905a67cSAndy Whitcroft	}
17288905a67cSAndy Whitcroft}
17298905a67cSAndy Whitcroft
17306c72ffaaSAndy Whitcroftmy $prefix = '';
17316c72ffaaSAndy Whitcroft
1732000d1cc1SJoe Perchessub show_type {
1733cbec18afSJoe Perches	my ($type) = @_;
173491bfe484SJoe Perches
1735cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
1736cbec18afSJoe Perches
1737cbec18afSJoe Perches	return !defined $ignore_type{$type};
1738000d1cc1SJoe Perches}
1739000d1cc1SJoe Perches
1740f0a594c1SAndy Whitcroftsub report {
1741cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
1742cbec18afSJoe Perches
1743cbec18afSJoe Perches	if (!show_type($type) ||
1744cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
1745773647a0SAndy Whitcroft		return 0;
1746773647a0SAndy Whitcroft	}
174757230297SJoe Perches	my $output = '';
174857230297SJoe Perches	if (-t STDOUT && $color) {
174957230297SJoe Perches		if ($level eq 'ERROR') {
175057230297SJoe Perches			$output .= RED;
175157230297SJoe Perches		} elsif ($level eq 'WARNING') {
175257230297SJoe Perches			$output .= YELLOW;
1753000d1cc1SJoe Perches		} else {
175457230297SJoe Perches			$output .= GREEN;
1755000d1cc1SJoe Perches		}
175657230297SJoe Perches	}
175757230297SJoe Perches	$output .= $prefix . $level . ':';
175857230297SJoe Perches	if ($show_types) {
175957230297SJoe Perches		$output .= BLUE if (-t STDOUT && $color);
176057230297SJoe Perches		$output .= "$type:";
176157230297SJoe Perches	}
176257230297SJoe Perches	$output .= RESET if (-t STDOUT && $color);
176357230297SJoe Perches	$output .= ' ' . $msg . "\n";
176434d8815fSJoe Perches
176534d8815fSJoe Perches	if ($showfile) {
176634d8815fSJoe Perches		my @lines = split("\n", $output, -1);
176734d8815fSJoe Perches		splice(@lines, 1, 1);
176834d8815fSJoe Perches		$output = join("\n", @lines);
176934d8815fSJoe Perches	}
177057230297SJoe Perches	$output = (split('\n', $output))[0] . "\n" if ($terse);
17718905a67cSAndy Whitcroft
177257230297SJoe Perches	push(our @report, $output);
1773773647a0SAndy Whitcroft
1774773647a0SAndy Whitcroft	return 1;
1775f0a594c1SAndy Whitcroft}
1776cbec18afSJoe Perches
1777f0a594c1SAndy Whitcroftsub report_dump {
177813214adfSAndy Whitcroft	our @report;
1779f0a594c1SAndy Whitcroft}
1780000d1cc1SJoe Perches
1781d752fcc8SJoe Perchessub fixup_current_range {
1782d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
1783d752fcc8SJoe Perches
1784d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1785d752fcc8SJoe Perches		my $o = $1;
1786d752fcc8SJoe Perches		my $l = $2;
1787d752fcc8SJoe Perches		my $no = $o + $offset;
1788d752fcc8SJoe Perches		my $nl = $l + $length;
1789d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1790d752fcc8SJoe Perches	}
1791d752fcc8SJoe Perches}
1792d752fcc8SJoe Perches
1793d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
1794d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
1795d752fcc8SJoe Perches
1796d752fcc8SJoe Perches	my $range_last_linenr = 0;
1797d752fcc8SJoe Perches	my $delta_offset = 0;
1798d752fcc8SJoe Perches
1799d752fcc8SJoe Perches	my $old_linenr = 0;
1800d752fcc8SJoe Perches	my $new_linenr = 0;
1801d752fcc8SJoe Perches
1802d752fcc8SJoe Perches	my $next_insert = 0;
1803d752fcc8SJoe Perches	my $next_delete = 0;
1804d752fcc8SJoe Perches
1805d752fcc8SJoe Perches	my @lines = ();
1806d752fcc8SJoe Perches
1807d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
1808d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
1809d752fcc8SJoe Perches
1810d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
1811d752fcc8SJoe Perches		my $save_line = 1;
1812d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
1813323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
1814d752fcc8SJoe Perches			$delta_offset = 0;
1815d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
1816d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
1817d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
1818d752fcc8SJoe Perches		}
1819d752fcc8SJoe Perches
1820d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1821d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
1822d752fcc8SJoe Perches			$save_line = 0;
1823d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1824d752fcc8SJoe Perches		}
1825d752fcc8SJoe Perches
1826d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1827d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
1828d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
1829d752fcc8SJoe Perches			$new_linenr++;
1830d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1831d752fcc8SJoe Perches		}
1832d752fcc8SJoe Perches
1833d752fcc8SJoe Perches		if ($save_line) {
1834d752fcc8SJoe Perches			push(@lines, $line);
1835d752fcc8SJoe Perches			$new_linenr++;
1836d752fcc8SJoe Perches		}
1837d752fcc8SJoe Perches
1838d752fcc8SJoe Perches		$old_linenr++;
1839d752fcc8SJoe Perches	}
1840d752fcc8SJoe Perches
1841d752fcc8SJoe Perches	return @lines;
1842d752fcc8SJoe Perches}
1843d752fcc8SJoe Perches
1844f2d7e4d4SJoe Perchessub fix_insert_line {
1845f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1846f2d7e4d4SJoe Perches
1847f2d7e4d4SJoe Perches	my $inserted = {
1848f2d7e4d4SJoe Perches		LINENR => $linenr,
1849f2d7e4d4SJoe Perches		LINE => $line,
1850f2d7e4d4SJoe Perches	};
1851f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
1852f2d7e4d4SJoe Perches}
1853f2d7e4d4SJoe Perches
1854f2d7e4d4SJoe Perchessub fix_delete_line {
1855f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
1856f2d7e4d4SJoe Perches
1857f2d7e4d4SJoe Perches	my $deleted = {
1858f2d7e4d4SJoe Perches		LINENR => $linenr,
1859f2d7e4d4SJoe Perches		LINE => $line,
1860f2d7e4d4SJoe Perches	};
1861f2d7e4d4SJoe Perches
1862f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
1863f2d7e4d4SJoe Perches}
1864f2d7e4d4SJoe Perches
1865de7d4f0eSAndy Whitcroftsub ERROR {
1866cbec18afSJoe Perches	my ($type, $msg) = @_;
1867cbec18afSJoe Perches
1868cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
1869de7d4f0eSAndy Whitcroft		our $clean = 0;
18706c72ffaaSAndy Whitcroft		our $cnt_error++;
18713705ce5bSJoe Perches		return 1;
1872de7d4f0eSAndy Whitcroft	}
18733705ce5bSJoe Perches	return 0;
1874773647a0SAndy Whitcroft}
1875de7d4f0eSAndy Whitcroftsub WARN {
1876cbec18afSJoe Perches	my ($type, $msg) = @_;
1877cbec18afSJoe Perches
1878cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
1879de7d4f0eSAndy Whitcroft		our $clean = 0;
18806c72ffaaSAndy Whitcroft		our $cnt_warn++;
18813705ce5bSJoe Perches		return 1;
1882de7d4f0eSAndy Whitcroft	}
18833705ce5bSJoe Perches	return 0;
1884773647a0SAndy Whitcroft}
1885de7d4f0eSAndy Whitcroftsub CHK {
1886cbec18afSJoe Perches	my ($type, $msg) = @_;
1887cbec18afSJoe Perches
1888cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
1889de7d4f0eSAndy Whitcroft		our $clean = 0;
18906c72ffaaSAndy Whitcroft		our $cnt_chk++;
18913705ce5bSJoe Perches		return 1;
18926c72ffaaSAndy Whitcroft	}
18933705ce5bSJoe Perches	return 0;
1894de7d4f0eSAndy Whitcroft}
1895de7d4f0eSAndy Whitcroft
18966ecd9674SAndy Whitcroftsub check_absolute_file {
18976ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
18986ecd9674SAndy Whitcroft	my $file = $absolute;
18996ecd9674SAndy Whitcroft
19006ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
19016ecd9674SAndy Whitcroft
19026ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
19036ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
19046ecd9674SAndy Whitcroft		if (-f "$root/$file") {
19056ecd9674SAndy Whitcroft			##print "file<$file>\n";
19066ecd9674SAndy Whitcroft			last;
19076ecd9674SAndy Whitcroft		}
19086ecd9674SAndy Whitcroft	}
19096ecd9674SAndy Whitcroft	if (! -f _)  {
19106ecd9674SAndy Whitcroft		return 0;
19116ecd9674SAndy Whitcroft	}
19126ecd9674SAndy Whitcroft
19136ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
19146ecd9674SAndy Whitcroft	my $prefix = $absolute;
19156ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
19166ecd9674SAndy Whitcroft
19176ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
19186ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
1919000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
1920000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
19216ecd9674SAndy Whitcroft	}
19226ecd9674SAndy Whitcroft}
19236ecd9674SAndy Whitcroft
19243705ce5bSJoe Perchessub trim {
19253705ce5bSJoe Perches	my ($string) = @_;
19263705ce5bSJoe Perches
1927b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
1928b34c648bSJoe Perches
1929b34c648bSJoe Perches	return $string;
1930b34c648bSJoe Perches}
1931b34c648bSJoe Perches
1932b34c648bSJoe Perchessub ltrim {
1933b34c648bSJoe Perches	my ($string) = @_;
1934b34c648bSJoe Perches
1935b34c648bSJoe Perches	$string =~ s/^\s+//;
1936b34c648bSJoe Perches
1937b34c648bSJoe Perches	return $string;
1938b34c648bSJoe Perches}
1939b34c648bSJoe Perches
1940b34c648bSJoe Perchessub rtrim {
1941b34c648bSJoe Perches	my ($string) = @_;
1942b34c648bSJoe Perches
1943b34c648bSJoe Perches	$string =~ s/\s+$//;
19443705ce5bSJoe Perches
19453705ce5bSJoe Perches	return $string;
19463705ce5bSJoe Perches}
19473705ce5bSJoe Perches
194852ea8506SJoe Perchessub string_find_replace {
194952ea8506SJoe Perches	my ($string, $find, $replace) = @_;
195052ea8506SJoe Perches
195152ea8506SJoe Perches	$string =~ s/$find/$replace/g;
195252ea8506SJoe Perches
195352ea8506SJoe Perches	return $string;
195452ea8506SJoe Perches}
195552ea8506SJoe Perches
19563705ce5bSJoe Perchessub tabify {
19573705ce5bSJoe Perches	my ($leading) = @_;
19583705ce5bSJoe Perches
19593705ce5bSJoe Perches	my $source_indent = 8;
19603705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
19613705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
19623705ce5bSJoe Perches
19633705ce5bSJoe Perches	#convert leading spaces to tabs
19643705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
19653705ce5bSJoe Perches	#Remove spaces before a tab
19663705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
19673705ce5bSJoe Perches
19683705ce5bSJoe Perches	return "$leading";
19693705ce5bSJoe Perches}
19703705ce5bSJoe Perches
1971d1fe9c09SJoe Perchessub pos_last_openparen {
1972d1fe9c09SJoe Perches	my ($line) = @_;
1973d1fe9c09SJoe Perches
1974d1fe9c09SJoe Perches	my $pos = 0;
1975d1fe9c09SJoe Perches
1976d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
1977d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
1978d1fe9c09SJoe Perches
1979d1fe9c09SJoe Perches	my $last_openparen = 0;
1980d1fe9c09SJoe Perches
1981d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
1982d1fe9c09SJoe Perches		return -1;
1983d1fe9c09SJoe Perches	}
1984d1fe9c09SJoe Perches
1985d1fe9c09SJoe Perches	my $len = length($line);
1986d1fe9c09SJoe Perches
1987d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
1988d1fe9c09SJoe Perches		my $string = substr($line, $pos);
1989d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
1990d1fe9c09SJoe Perches			$pos += length($1) - 1;
1991d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
1992d1fe9c09SJoe Perches			$last_openparen = $pos;
1993d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
1994d1fe9c09SJoe Perches			last;
1995d1fe9c09SJoe Perches		}
1996d1fe9c09SJoe Perches	}
1997d1fe9c09SJoe Perches
199891cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
1999d1fe9c09SJoe Perches}
2000d1fe9c09SJoe Perches
20010a920b5bSAndy Whitcroftsub process {
20020a920b5bSAndy Whitcroft	my $filename = shift;
20030a920b5bSAndy Whitcroft
20040a920b5bSAndy Whitcroft	my $linenr=0;
20050a920b5bSAndy Whitcroft	my $prevline="";
2006c2fdda0dSAndy Whitcroft	my $prevrawline="";
20070a920b5bSAndy Whitcroft	my $stashline="";
2008c2fdda0dSAndy Whitcroft	my $stashrawline="";
20090a920b5bSAndy Whitcroft
20104a0df2efSAndy Whitcroft	my $length;
20110a920b5bSAndy Whitcroft	my $indent;
20120a920b5bSAndy Whitcroft	my $previndent=0;
20130a920b5bSAndy Whitcroft	my $stashindent=0;
20140a920b5bSAndy Whitcroft
2015de7d4f0eSAndy Whitcroft	our $clean = 1;
20160a920b5bSAndy Whitcroft	my $signoff = 0;
20170a920b5bSAndy Whitcroft	my $is_patch = 0;
201829ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
201915662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
2020bf4daf12SJoe Perches       my $commit_log_possible_stack_dump = 0;
20212a076f40SJoe Perches	my $commit_log_long_line = 0;
2022e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
202313f1937eSJoe Perches	my $reported_maintainer_file = 0;
2024fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2025fa64205dSPasi Savanainen
2026365dd4eaSJoe Perches	my $last_blank_line = 0;
20275e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2028365dd4eaSJoe Perches
202913214adfSAndy Whitcroft	our @report = ();
20306c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
20316c72ffaaSAndy Whitcroft	our $cnt_error = 0;
20326c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
20336c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
20346c72ffaaSAndy Whitcroft
20350a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
20360a920b5bSAndy Whitcroft	my $realfile = '';
20370a920b5bSAndy Whitcroft	my $realline = 0;
20380a920b5bSAndy Whitcroft	my $realcnt = 0;
20390a920b5bSAndy Whitcroft	my $here = '';
20400a920b5bSAndy Whitcroft	my $in_comment = 0;
2041c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
20420a920b5bSAndy Whitcroft	my $first_line = 0;
20431e855726SWolfram Sang	my $p1_prefix = '';
20440a920b5bSAndy Whitcroft
204513214adfSAndy Whitcroft	my $prev_values = 'E';
204613214adfSAndy Whitcroft
204713214adfSAndy Whitcroft	# suppression flags
2048773647a0SAndy Whitcroft	my %suppress_ifbraces;
2049170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
20502b474a1aSAndy Whitcroft	my %suppress_export;
20513e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2052653d4876SAndy Whitcroft
20537e51f197SJoe Perches	my %signatures = ();
2054323c1260SJoe Perches
2055c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2056de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2057c2fdda0dSAndy Whitcroft	#
2058de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2059de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2060773647a0SAndy Whitcroft
2061d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2062d8b07710SJoe Perches
2063773647a0SAndy Whitcroft	sanitise_line_reset();
2064c2fdda0dSAndy Whitcroft	my $line;
2065c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2066773647a0SAndy Whitcroft		$linenr++;
2067773647a0SAndy Whitcroft		$line = $rawline;
2068c2fdda0dSAndy Whitcroft
20693705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
20703705ce5bSJoe Perches
2071773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2072de7d4f0eSAndy Whitcroft			$setup_docs = 0;
2073de7d4f0eSAndy Whitcroft			if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2074de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2075de7d4f0eSAndy Whitcroft			}
2076773647a0SAndy Whitcroft			#next;
2077de7d4f0eSAndy Whitcroft		}
2078773647a0SAndy Whitcroft		if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2079773647a0SAndy Whitcroft			$realline=$1-1;
2080773647a0SAndy Whitcroft			if (defined $2) {
2081773647a0SAndy Whitcroft				$realcnt=$3+1;
2082773647a0SAndy Whitcroft			} else {
2083773647a0SAndy Whitcroft				$realcnt=1+1;
2084773647a0SAndy Whitcroft			}
2085c45dcabdSAndy Whitcroft			$in_comment = 0;
2086773647a0SAndy Whitcroft
2087773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2088773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2089773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2090773647a0SAndy Whitcroft			# at context start.
2091773647a0SAndy Whitcroft			my $edge;
209201fa9147SAndy Whitcroft			my $cnt = $realcnt;
209301fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
209401fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
209501fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
209601fa9147SAndy Whitcroft				$cnt--;
209701fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2098721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2099fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2100fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2101fae17daeSAndy Whitcroft					($edge) = $1;
2102fae17daeSAndy Whitcroft					last;
2103fae17daeSAndy Whitcroft				}
2104773647a0SAndy Whitcroft			}
2105773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2106773647a0SAndy Whitcroft				$in_comment = 1;
2107773647a0SAndy Whitcroft			}
2108773647a0SAndy Whitcroft
2109773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2110773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2111773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2112773647a0SAndy Whitcroft			if (!defined $edge &&
211383242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2114773647a0SAndy Whitcroft			{
2115773647a0SAndy Whitcroft				$in_comment = 1;
2116773647a0SAndy Whitcroft			}
2117773647a0SAndy Whitcroft
2118773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2119773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2120773647a0SAndy Whitcroft
2121171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2122773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2123171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2124773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2125773647a0SAndy Whitcroft		}
2126773647a0SAndy Whitcroft		push(@lines, $line);
2127773647a0SAndy Whitcroft
2128773647a0SAndy Whitcroft		if ($realcnt > 1) {
2129773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2130773647a0SAndy Whitcroft		} else {
2131773647a0SAndy Whitcroft			$realcnt = 0;
2132773647a0SAndy Whitcroft		}
2133773647a0SAndy Whitcroft
2134773647a0SAndy Whitcroft		#print "==>$rawline\n";
2135773647a0SAndy Whitcroft		#print "-->$line\n";
2136de7d4f0eSAndy Whitcroft
2137de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2138de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2139de7d4f0eSAndy Whitcroft		}
2140de7d4f0eSAndy Whitcroft	}
2141de7d4f0eSAndy Whitcroft
21426c72ffaaSAndy Whitcroft	$prefix = '';
21436c72ffaaSAndy Whitcroft
2144773647a0SAndy Whitcroft	$realcnt = 0;
2145773647a0SAndy Whitcroft	$linenr = 0;
2146194f66fcSJoe Perches	$fixlinenr = -1;
21470a920b5bSAndy Whitcroft	foreach my $line (@lines) {
21480a920b5bSAndy Whitcroft		$linenr++;
2149194f66fcSJoe Perches		$fixlinenr++;
21501b5539b1SJoe Perches		my $sline = $line;	#copy of $line
21511b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
21520a920b5bSAndy Whitcroft
2153c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
21546c72ffaaSAndy Whitcroft
21550a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2156e518e9a5SJoe Perches		if (!$in_commit_log &&
2157e518e9a5SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
21580a920b5bSAndy Whitcroft			$is_patch = 1;
21594a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
21600a920b5bSAndy Whitcroft			$realline=$1-1;
21610a920b5bSAndy Whitcroft			if (defined $2) {
21620a920b5bSAndy Whitcroft				$realcnt=$3+1;
21630a920b5bSAndy Whitcroft			} else {
21640a920b5bSAndy Whitcroft				$realcnt=1+1;
21650a920b5bSAndy Whitcroft			}
2166c2fdda0dSAndy Whitcroft			annotate_reset();
216713214adfSAndy Whitcroft			$prev_values = 'E';
216813214adfSAndy Whitcroft
2169773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2170170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
21712b474a1aSAndy Whitcroft			%suppress_export = ();
21723e469cdcSAndy Whitcroft			$suppress_statement = 0;
21730a920b5bSAndy Whitcroft			next;
21740a920b5bSAndy Whitcroft
21754a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
21764a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
21774a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2178773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
21790a920b5bSAndy Whitcroft			$realline++;
2180d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
21810a920b5bSAndy Whitcroft
21824a0df2efSAndy Whitcroft			# Measure the line length and indent.
2183c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
21840a920b5bSAndy Whitcroft
21850a920b5bSAndy Whitcroft			# Track the previous line.
21860a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
21870a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2188c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2189c2fdda0dSAndy Whitcroft
2190773647a0SAndy Whitcroft			#warn "line<$line>\n";
21916c72ffaaSAndy Whitcroft
2192d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2193d8aaf121SAndy Whitcroft			$realcnt--;
21940a920b5bSAndy Whitcroft		}
21950a920b5bSAndy Whitcroft
2196cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2197cc77cdcaSAndy Whitcroft
21986c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
21996c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2200773647a0SAndy Whitcroft
22012ac73b4fSJoe Perches		my $found_file = 0;
2202773647a0SAndy Whitcroft		# extract the filename as it passes
22033bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
22043bf9a009SRabin Vincent			$realfile = $1;
22052b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2206270c49a0SJoe Perches			$in_commit_log = 0;
22072ac73b4fSJoe Perches			$found_file = 1;
22083bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2209773647a0SAndy Whitcroft			$realfile = $1;
22102b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2211270c49a0SJoe Perches			$in_commit_log = 0;
22121e855726SWolfram Sang
22131e855726SWolfram Sang			$p1_prefix = $1;
2214e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2215e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2216000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2217000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
22181e855726SWolfram Sang			}
2219773647a0SAndy Whitcroft
2220c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2221000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2222000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2223773647a0SAndy Whitcroft			}
22242ac73b4fSJoe Perches			$found_file = 1;
22252ac73b4fSJoe Perches		}
22262ac73b4fSJoe Perches
222734d8815fSJoe Perches#make up the handle for any error we report on this line
222834d8815fSJoe Perches		if ($showfile) {
222934d8815fSJoe Perches			$prefix = "$realfile:$realline: "
223034d8815fSJoe Perches		} elsif ($emacs) {
22317d3a9f67SJoe Perches			if ($file) {
22327d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
22337d3a9f67SJoe Perches			} else {
223434d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
223534d8815fSJoe Perches			}
22367d3a9f67SJoe Perches		}
223734d8815fSJoe Perches
22382ac73b4fSJoe Perches		if ($found_file) {
22397bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
22402ac73b4fSJoe Perches				$check = 1;
22412ac73b4fSJoe Perches			} else {
22422ac73b4fSJoe Perches				$check = $check_orig;
22432ac73b4fSJoe Perches			}
2244773647a0SAndy Whitcroft			next;
2245773647a0SAndy Whitcroft		}
2246773647a0SAndy Whitcroft
2247389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
22480a920b5bSAndy Whitcroft
2249c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2250c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2251c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
22520a920b5bSAndy Whitcroft
22536c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
22546c72ffaaSAndy Whitcroft
2255e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2256e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
2257e518e9a5SJoe Perches		    (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2258e518e9a5SJoe Perches		      $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2259e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2260e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2261e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2262e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2263e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2264e518e9a5SJoe Perches		}
2265e518e9a5SJoe Perches
22663bf9a009SRabin Vincent# Check for incorrect file permissions
22673bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
22683bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
226904db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
227004db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2271000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2272000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
22733bf9a009SRabin Vincent			}
22743bf9a009SRabin Vincent		}
22753bf9a009SRabin Vincent
227620112475SJoe Perches# Check the patch for a signoff:
2277d8aaf121SAndy Whitcroft		if ($line =~ /^\s*signed-off-by:/i) {
22784a0df2efSAndy Whitcroft			$signoff++;
227915662b3eSJoe Perches			$in_commit_log = 0;
22800a920b5bSAndy Whitcroft		}
228120112475SJoe Perches
2282e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2283e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2284e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2285e0d975b1SJoe Perches			$reported_maintainer_file = 1;
2286e0d975b1SJoe Perches		}
2287e0d975b1SJoe Perches
228820112475SJoe Perches# Check signature styles
2289270c49a0SJoe Perches		if (!$in_header_lines &&
2290ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
229120112475SJoe Perches			my $space_before = $1;
229220112475SJoe Perches			my $sign_off = $2;
229320112475SJoe Perches			my $space_after = $3;
229420112475SJoe Perches			my $email = $4;
229520112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
229620112475SJoe Perches
2297ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
2298ce0338dfSJoe Perches				WARN("BAD_SIGN_OFF",
2299ce0338dfSJoe Perches				     "Non-standard signature: $sign_off\n" . $herecurr);
2300ce0338dfSJoe Perches			}
230120112475SJoe Perches			if (defined $space_before && $space_before ne "") {
23023705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
23033705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
23043705ce5bSJoe Perches				    $fix) {
2305194f66fcSJoe Perches					$fixed[$fixlinenr] =
23063705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
23073705ce5bSJoe Perches				}
230820112475SJoe Perches			}
230920112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
23103705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
23113705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
23123705ce5bSJoe Perches				    $fix) {
2313194f66fcSJoe Perches					$fixed[$fixlinenr] =
23143705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
23153705ce5bSJoe Perches				}
23163705ce5bSJoe Perches
231720112475SJoe Perches			}
231820112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
23193705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
23203705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
23213705ce5bSJoe Perches				    $fix) {
2322194f66fcSJoe Perches					$fixed[$fixlinenr] =
23233705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
23243705ce5bSJoe Perches				}
232520112475SJoe Perches			}
232620112475SJoe Perches
232720112475SJoe Perches			my ($email_name, $email_address, $comment) = parse_email($email);
232820112475SJoe Perches			my $suggested_email = format_email(($email_name, $email_address));
232920112475SJoe Perches			if ($suggested_email eq "") {
2330000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
2331000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
233220112475SJoe Perches			} else {
233320112475SJoe Perches				my $dequoted = $suggested_email;
233420112475SJoe Perches				$dequoted =~ s/^"//;
233520112475SJoe Perches				$dequoted =~ s/" </ </;
233620112475SJoe Perches				# Don't force email to have quotes
233720112475SJoe Perches				# Allow just an angle bracketed address
233820112475SJoe Perches				if ("$dequoted$comment" ne $email &&
233920112475SJoe Perches				    "<$email_address>$comment" ne $email &&
234020112475SJoe Perches				    "$suggested_email$comment" ne $email) {
2341000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
2342000d1cc1SJoe Perches					     "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
234320112475SJoe Perches				}
23440a920b5bSAndy Whitcroft			}
23457e51f197SJoe Perches
23467e51f197SJoe Perches# Check for duplicate signatures
23477e51f197SJoe Perches			my $sig_nospace = $line;
23487e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
23497e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
23507e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
23517e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
23527e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
23537e51f197SJoe Perches			} else {
23547e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
23557e51f197SJoe Perches			}
23560a920b5bSAndy Whitcroft		}
23570a920b5bSAndy Whitcroft
2358a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
2359a2fe16b9SJoe Perches		if ($in_header_lines &&
2360a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2361a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
2362a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2363a2fe16b9SJoe Perches		}
2364a2fe16b9SJoe Perches
23659b3189ebSJoe Perches# Check for old stable address
23669b3189ebSJoe Perches		if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
23679b3189ebSJoe Perches			ERROR("STABLE_ADDRESS",
23689b3189ebSJoe Perches			      "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
23699b3189ebSJoe Perches		}
23709b3189ebSJoe Perches
23717ebd05efSChristopher Covington# Check for unwanted Gerrit info
23727ebd05efSChristopher Covington		if ($in_commit_log && $line =~ /^\s*change-id:/i) {
23737ebd05efSChristopher Covington			ERROR("GERRIT_CHANGE_ID",
23747ebd05efSChristopher Covington			      "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
23757ebd05efSChristopher Covington		}
23767ebd05efSChristopher Covington
2377369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
2378369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2379369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2380369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2381369c8dd3SJoe Perches					# timestamp
2382369c8dd3SJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2383369c8dd3SJoe Perches					# stack dump address
2384369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
2385369c8dd3SJoe Perches		}
2386369c8dd3SJoe Perches
23872a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
23882a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
2389bf4daf12SJoe Perches		    length($line) > 75 &&
2390bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2391bf4daf12SJoe Perches					# file delta changes
2392bf4daf12SJoe Perches		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2393bf4daf12SJoe Perches					# filename then :
2394bf4daf12SJoe Perches		      $line =~ /^\s*(?:Fixes:|Link:)/i ||
2395bf4daf12SJoe Perches					# A Fixes: or Link: line
2396bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
23972a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
23982a076f40SJoe Perches			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
23992a076f40SJoe Perches			$commit_log_long_line = 1;
24002a076f40SJoe Perches		}
24012a076f40SJoe Perches
2402bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
2403bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
2404bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
2405bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
2406bf4daf12SJoe Perches		}
2407bf4daf12SJoe Perches
24080d7835fcSJoe Perches# Check for git id commit length and improperly formed commit descriptions
2409369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
2410fe043ea1SJoe Perches		    ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
2411bf4daf12SJoe Perches		     ($line =~ /\b[0-9a-f]{12,40}\b/i &&
2412369c8dd3SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2413bf4daf12SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
2414fe043ea1SJoe Perches			my $init_char = "c";
2415fe043ea1SJoe Perches			my $orig_commit = "";
24160d7835fcSJoe Perches			my $short = 1;
24170d7835fcSJoe Perches			my $long = 0;
24180d7835fcSJoe Perches			my $case = 1;
24190d7835fcSJoe Perches			my $space = 1;
24200d7835fcSJoe Perches			my $hasdesc = 0;
242119c146a6SJoe Perches			my $hasparens = 0;
24220d7835fcSJoe Perches			my $id = '0123456789ab';
24230d7835fcSJoe Perches			my $orig_desc = "commit description";
24240d7835fcSJoe Perches			my $description = "";
24250d7835fcSJoe Perches
2426fe043ea1SJoe Perches			if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2427fe043ea1SJoe Perches				$init_char = $1;
2428fe043ea1SJoe Perches				$orig_commit = lc($2);
2429fe043ea1SJoe Perches			} elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2430fe043ea1SJoe Perches				$orig_commit = lc($1);
2431fe043ea1SJoe Perches			}
2432fe043ea1SJoe Perches
24330d7835fcSJoe Perches			$short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
24340d7835fcSJoe Perches			$long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
24350d7835fcSJoe Perches			$space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
24360d7835fcSJoe Perches			$case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
24370d7835fcSJoe Perches			if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
24380d7835fcSJoe Perches				$orig_desc = $1;
243919c146a6SJoe Perches				$hasparens = 1;
24400d7835fcSJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
24410d7835fcSJoe Perches				 defined $rawlines[$linenr] &&
24420d7835fcSJoe Perches				 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
24430d7835fcSJoe Perches				$orig_desc = $1;
244419c146a6SJoe Perches				$hasparens = 1;
2445b671fde0SJoe Perches			} elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2446b671fde0SJoe Perches				 defined $rawlines[$linenr] &&
2447b671fde0SJoe Perches				 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2448b671fde0SJoe Perches				$line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2449b671fde0SJoe Perches				$orig_desc = $1;
2450b671fde0SJoe Perches				$rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2451b671fde0SJoe Perches				$orig_desc .= " " . $1;
245219c146a6SJoe Perches				$hasparens = 1;
24530d7835fcSJoe Perches			}
24540d7835fcSJoe Perches
24550d7835fcSJoe Perches			($id, $description) = git_commit_info($orig_commit,
24560d7835fcSJoe Perches							      $id, $orig_desc);
24570d7835fcSJoe Perches
245819c146a6SJoe Perches			if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
2459d311cd44SJoe Perches				ERROR("GIT_COMMIT_ID",
24600d7835fcSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
24610d7835fcSJoe Perches			}
2462d311cd44SJoe Perches		}
2463d311cd44SJoe Perches
246413f1937eSJoe Perches# Check for added, moved or deleted files
246513f1937eSJoe Perches		if (!$reported_maintainer_file && !$in_commit_log &&
246613f1937eSJoe Perches		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
246713f1937eSJoe Perches		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
246813f1937eSJoe Perches		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
246913f1937eSJoe Perches		      (defined($1) || defined($2))))) {
247013f1937eSJoe Perches			$reported_maintainer_file = 1;
247113f1937eSJoe Perches			WARN("FILE_PATH_CHANGES",
247213f1937eSJoe Perches			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
247313f1937eSJoe Perches		}
247413f1937eSJoe Perches
247500df344fSAndy Whitcroft# Check for wrappage within a valid hunk of the file
24768905a67cSAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
2477000d1cc1SJoe Perches			ERROR("CORRUPTED_PATCH",
2478000d1cc1SJoe Perches			      "patch seems to be corrupt (line wrapped?)\n" .
24796c72ffaaSAndy Whitcroft				$herecurr) if (!$emitted_corrupt++);
2480de7d4f0eSAndy Whitcroft		}
2481de7d4f0eSAndy Whitcroft
24826ecd9674SAndy Whitcroft# Check for absolute kernel paths.
24836ecd9674SAndy Whitcroft		if ($tree) {
24846ecd9674SAndy Whitcroft			while ($line =~ m{(?:^|\s)(/\S*)}g) {
24856ecd9674SAndy Whitcroft				my $file = $1;
24866ecd9674SAndy Whitcroft
24876ecd9674SAndy Whitcroft				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
24886ecd9674SAndy Whitcroft				    check_absolute_file($1, $herecurr)) {
24896ecd9674SAndy Whitcroft					#
24906ecd9674SAndy Whitcroft				} else {
24916ecd9674SAndy Whitcroft					check_absolute_file($file, $herecurr);
24926ecd9674SAndy Whitcroft				}
24936ecd9674SAndy Whitcroft			}
24946ecd9674SAndy Whitcroft		}
24956ecd9674SAndy Whitcroft
2496de7d4f0eSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2497de7d4f0eSAndy Whitcroft		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
2498171ae1a4SAndy Whitcroft		    $rawline !~ m/^$UTF8*$/) {
2499171ae1a4SAndy Whitcroft			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2500171ae1a4SAndy Whitcroft
2501171ae1a4SAndy Whitcroft			my $blank = copy_spacing($rawline);
2502171ae1a4SAndy Whitcroft			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2503171ae1a4SAndy Whitcroft			my $hereptr = "$hereline$ptr\n";
2504171ae1a4SAndy Whitcroft
250534d99219SJoe Perches			CHK("INVALID_UTF8",
2506000d1cc1SJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
250700df344fSAndy Whitcroft		}
25080a920b5bSAndy Whitcroft
250915662b3eSJoe Perches# Check if it's the start of a commit log
251015662b3eSJoe Perches# (not a header line and we haven't seen the patch filename)
251115662b3eSJoe Perches		if ($in_header_lines && $realfile =~ /^$/ &&
251229ee1b0cSJoe Perches		    !($rawline =~ /^\s+\S/ ||
251329ee1b0cSJoe Perches		      $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
251415662b3eSJoe Perches			$in_header_lines = 0;
251515662b3eSJoe Perches			$in_commit_log = 1;
251615662b3eSJoe Perches		}
251715662b3eSJoe Perches
2518fa64205dSPasi Savanainen# Check if there is UTF-8 in a commit log when a mail header has explicitly
2519fa64205dSPasi Savanainen# declined it, i.e defined some charset where it is missing.
2520fa64205dSPasi Savanainen		if ($in_header_lines &&
2521fa64205dSPasi Savanainen		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2522fa64205dSPasi Savanainen		    $1 !~ /utf-8/i) {
2523fa64205dSPasi Savanainen			$non_utf8_charset = 1;
2524fa64205dSPasi Savanainen		}
2525fa64205dSPasi Savanainen
2526fa64205dSPasi Savanainen		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
252715662b3eSJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
2528fa64205dSPasi Savanainen			WARN("UTF8_BEFORE_PATCH",
252915662b3eSJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
253015662b3eSJoe Perches		}
253115662b3eSJoe Perches
253266b47b4aSKees Cook# Check for various typo / spelling mistakes
253366d7a382SJoe Perches		if (defined($misspellings) &&
253466d7a382SJoe Perches		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
2535ebfd7d62SJoe Perches			while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
253666b47b4aSKees Cook				my $typo = $1;
253766b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
253866b47b4aSKees Cook				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
253966b47b4aSKees Cook				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
254066b47b4aSKees Cook				my $msg_type = \&WARN;
254166b47b4aSKees Cook				$msg_type = \&CHK if ($file);
254266b47b4aSKees Cook				if (&{$msg_type}("TYPO_SPELLING",
254366b47b4aSKees Cook						 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
254466b47b4aSKees Cook				    $fix) {
254566b47b4aSKees Cook					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
254666b47b4aSKees Cook				}
254766b47b4aSKees Cook			}
254866b47b4aSKees Cook		}
254966b47b4aSKees Cook
255030670854SAndy Whitcroft# ignore non-hunk lines and lines being removed
255130670854SAndy Whitcroft		next if (!$hunk_line || $line =~ /^-/);
255200df344fSAndy Whitcroft
25530a920b5bSAndy Whitcroft#trailing whitespace
25549c0ca6f9SAndy Whitcroft		if ($line =~ /^\+.*\015/) {
2555c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2556d5e616fcSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
2557d5e616fcSJoe Perches				  "DOS line endings\n" . $herevet) &&
2558d5e616fcSJoe Perches			    $fix) {
2559194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
2560d5e616fcSJoe Perches			}
2561c2fdda0dSAndy Whitcroft		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2562c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25633705ce5bSJoe Perches			if (ERROR("TRAILING_WHITESPACE",
25643705ce5bSJoe Perches				  "trailing whitespace\n" . $herevet) &&
25653705ce5bSJoe Perches			    $fix) {
2566194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
25673705ce5bSJoe Perches			}
25683705ce5bSJoe Perches
2569d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
25700a920b5bSAndy Whitcroft		}
25715368df20SAndy Whitcroft
25724783f894SJosh Triplett# Check for FSF mailing addresses.
2573109d8cb2SAlexander Duyck		if ($rawline =~ /\bwrite to the Free/i ||
25743e2232f2SJoe Perches		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
25753e2232f2SJoe Perches		    $rawline =~ /\b51\s+Franklin\s+St/i) {
25764783f894SJosh Triplett			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
25774783f894SJosh Triplett			my $msg_type = \&ERROR;
25784783f894SJosh Triplett			$msg_type = \&CHK if ($file);
25794783f894SJosh Triplett			&{$msg_type}("FSF_MAILING_ADDRESS",
25804783f894SJosh 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)
25814783f894SJosh Triplett		}
25824783f894SJosh Triplett
25833354957aSAndi Kleen# check for Kconfig help text having a real description
25849fe287d7SAndy Whitcroft# Only applies when adding the entry originally, after that we do not have
25859fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
25863354957aSAndi Kleen		if ($realfile =~ /Kconfig/ &&
25878d73e0e7SJoe Perches		    $line =~ /^\+\s*config\s+/) {
25883354957aSAndi Kleen			my $length = 0;
25899fe287d7SAndy Whitcroft			my $cnt = $realcnt;
25909fe287d7SAndy Whitcroft			my $ln = $linenr + 1;
25919fe287d7SAndy Whitcroft			my $f;
2592a1385803SAndy Whitcroft			my $is_start = 0;
25939fe287d7SAndy Whitcroft			my $is_end = 0;
2594a1385803SAndy Whitcroft			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
25959fe287d7SAndy Whitcroft				$f = $lines[$ln - 1];
25969fe287d7SAndy Whitcroft				$cnt-- if ($lines[$ln - 1] !~ /^-/);
25979fe287d7SAndy Whitcroft				$is_end = $lines[$ln - 1] =~ /^\+/;
25989fe287d7SAndy Whitcroft
25999fe287d7SAndy Whitcroft				next if ($f =~ /^-/);
26008d73e0e7SJoe Perches				last if (!$file && $f =~ /^\@\@/);
2601a1385803SAndy Whitcroft
26028d73e0e7SJoe Perches				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
2603a1385803SAndy Whitcroft					$is_start = 1;
26048d73e0e7SJoe Perches				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
2605a1385803SAndy Whitcroft					$length = -1;
2606a1385803SAndy Whitcroft				}
2607a1385803SAndy Whitcroft
26089fe287d7SAndy Whitcroft				$f =~ s/^.//;
26093354957aSAndi Kleen				$f =~ s/#.*//;
26103354957aSAndi Kleen				$f =~ s/^\s+//;
26113354957aSAndi Kleen				next if ($f =~ /^$/);
26129fe287d7SAndy Whitcroft				if ($f =~ /^\s*config\s/) {
26139fe287d7SAndy Whitcroft					$is_end = 1;
26149fe287d7SAndy Whitcroft					last;
26159fe287d7SAndy Whitcroft				}
26163354957aSAndi Kleen				$length++;
26173354957aSAndi Kleen			}
261856193274SVadim Bendebury			if ($is_start && $is_end && $length < $min_conf_desc_length) {
2619000d1cc1SJoe Perches				WARN("CONFIG_DESCRIPTION",
262056193274SVadim Bendebury				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
262156193274SVadim Bendebury			}
2622a1385803SAndy Whitcroft			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
26233354957aSAndi Kleen		}
26243354957aSAndi Kleen
26251ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
26261ba8dfd1SKees Cook		if ($realfile =~ /Kconfig/ &&
26271ba8dfd1SKees Cook		    $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
26281ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
26291ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
26301ba8dfd1SKees Cook		}
26311ba8dfd1SKees Cook
2632327953e9SChristoph Jaeger# discourage the use of boolean for type definition attributes of Kconfig options
2633327953e9SChristoph Jaeger		if ($realfile =~ /Kconfig/ &&
2634327953e9SChristoph Jaeger		    $line =~ /^\+\s*\bboolean\b/) {
2635327953e9SChristoph Jaeger			WARN("CONFIG_TYPE_BOOLEAN",
2636327953e9SChristoph Jaeger			     "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2637327953e9SChristoph Jaeger		}
2638327953e9SChristoph Jaeger
2639c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2640c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2641c68e5878SArnaud Lacombe			my $flag = $1;
2642c68e5878SArnaud Lacombe			my $replacement = {
2643c68e5878SArnaud Lacombe				'EXTRA_AFLAGS' =>   'asflags-y',
2644c68e5878SArnaud Lacombe				'EXTRA_CFLAGS' =>   'ccflags-y',
2645c68e5878SArnaud Lacombe				'EXTRA_CPPFLAGS' => 'cppflags-y',
2646c68e5878SArnaud Lacombe				'EXTRA_LDFLAGS' =>  'ldflags-y',
2647c68e5878SArnaud Lacombe			};
2648c68e5878SArnaud Lacombe
2649c68e5878SArnaud Lacombe			WARN("DEPRECATED_VARIABLE",
2650c68e5878SArnaud Lacombe			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2651c68e5878SArnaud Lacombe		}
2652c68e5878SArnaud Lacombe
2653bff5da43SRob Herring# check for DT compatible documentation
26547dd05b38SFlorian Vaussard		if (defined $root &&
26557dd05b38SFlorian Vaussard			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
26567dd05b38SFlorian Vaussard			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
26577dd05b38SFlorian Vaussard
2658bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2659bff5da43SRob Herring
2660cc93319bSFlorian Vaussard			my $dt_path = $root . "/Documentation/devicetree/bindings/";
2661cc93319bSFlorian Vaussard			my $vp_file = $dt_path . "vendor-prefixes.txt";
2662cc93319bSFlorian Vaussard
2663bff5da43SRob Herring			foreach my $compat (@compats) {
2664bff5da43SRob Herring				my $compat2 = $compat;
2665185d566bSRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2666185d566bSRob Herring				my $compat3 = $compat;
2667185d566bSRob Herring				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2668185d566bSRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
2669bff5da43SRob Herring				if ( $? >> 8 ) {
2670bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2671bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2672bff5da43SRob Herring				}
2673bff5da43SRob Herring
26744fbf32a6SFlorian Vaussard				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
26754fbf32a6SFlorian Vaussard				my $vendor = $1;
2676cc93319bSFlorian Vaussard				`grep -Eq "^$vendor\\b" $vp_file`;
2677bff5da43SRob Herring				if ( $? >> 8 ) {
2678bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
2679cc93319bSFlorian Vaussard					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
2680bff5da43SRob Herring				}
2681bff5da43SRob Herring			}
2682bff5da43SRob Herring		}
2683bff5da43SRob Herring
26845368df20SAndy Whitcroft# check we are in a valid source file if not then ignore this hunk
2685de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
26865368df20SAndy Whitcroft
268747e0c88bSJoe Perches# line length limit (with some exclusions)
268847e0c88bSJoe Perches#
268947e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
269047e0c88bSJoe Perches#	logging functions like pr_info that end in a string
269147e0c88bSJoe Perches#	lines with a single string
269247e0c88bSJoe Perches#	#defines that are a single string
269347e0c88bSJoe Perches#
269447e0c88bSJoe Perches# There are 3 different line length message types:
269547e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_linelength
269647e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
269747e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
269847e0c88bSJoe Perches#
269947e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
270047e0c88bSJoe Perches#
270147e0c88bSJoe Perches
2702b4749e96SJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
270347e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
270447e0c88bSJoe Perches
270547e0c88bSJoe Perches			# Check the allowed long line types first
270647e0c88bSJoe Perches
270747e0c88bSJoe Perches			# logging functions that end in a string that starts
270847e0c88bSJoe Perches			# before $max_line_length
270947e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
271047e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
271147e0c88bSJoe Perches				$msg_type = "";
271247e0c88bSJoe Perches
271347e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
271447e0c88bSJoe Perches			# #defines with only strings
271547e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
271647e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
271747e0c88bSJoe Perches				$msg_type = "";
271847e0c88bSJoe Perches
271947e0c88bSJoe Perches			# Otherwise set the alternate message types
272047e0c88bSJoe Perches
272147e0c88bSJoe Perches			# a comment starts before $max_line_length
272247e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
272347e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
272447e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
272547e0c88bSJoe Perches
272647e0c88bSJoe Perches			# a quoted string starts before $max_line_length
272747e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
272847e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
272947e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
273047e0c88bSJoe Perches			}
273147e0c88bSJoe Perches
273247e0c88bSJoe Perches			if ($msg_type ne "" &&
273347e0c88bSJoe Perches			    (show_type("LONG_LINE") || show_type($msg_type))) {
273447e0c88bSJoe Perches				WARN($msg_type,
27356cd7f386SJoe Perches				     "line over $max_line_length characters\n" . $herecurr);
27360a920b5bSAndy Whitcroft			}
273747e0c88bSJoe Perches		}
27380a920b5bSAndy Whitcroft
27398905a67cSAndy Whitcroft# check for adding lines without a newline.
27408905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
2741000d1cc1SJoe Perches			WARN("MISSING_EOF_NEWLINE",
2742000d1cc1SJoe Perches			     "adding a line without newline at end of file\n" . $herecurr);
27438905a67cSAndy Whitcroft		}
27448905a67cSAndy Whitcroft
274542e41c54SMike Frysinger# Blackfin: use hi/lo macros
274642e41c54SMike Frysinger		if ($realfile =~ m@arch/blackfin/.*\.S$@) {
274742e41c54SMike Frysinger			if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
274842e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2749000d1cc1SJoe Perches				ERROR("LO_MACRO",
2750000d1cc1SJoe Perches				      "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
275142e41c54SMike Frysinger			}
275242e41c54SMike Frysinger			if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
275342e41c54SMike Frysinger				my $herevet = "$here\n" . cat_vet($line) . "\n";
2754000d1cc1SJoe Perches				ERROR("HI_MACRO",
2755000d1cc1SJoe Perches				      "use the HI() macro, not (... >> 16)\n" . $herevet);
275642e41c54SMike Frysinger			}
275742e41c54SMike Frysinger		}
275842e41c54SMike Frysinger
2759b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
2760de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
27610a920b5bSAndy Whitcroft
27620a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
27630a920b5bSAndy Whitcroft# more than 8 must use tabs.
2764c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
2765c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
2766c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2767d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
27683705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
27693705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
27703705ce5bSJoe Perches			    $fix) {
2771194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
27723705ce5bSJoe Perches			}
27730a920b5bSAndy Whitcroft		}
27740a920b5bSAndy Whitcroft
277508e44365SAlberto Panizzo# check for space before tabs.
277608e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
277708e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
27783705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
27793705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
27803705ce5bSJoe Perches			    $fix) {
2781194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2782d2207ccbSJoe Perches					   s/(^\+.*) {8,8}\t/$1\t\t/) {}
2783194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
2784c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
27853705ce5bSJoe Perches			}
278608e44365SAlberto Panizzo		}
278708e44365SAlberto Panizzo
2788d1fe9c09SJoe Perches# check for && or || at the start of a line
2789d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2790d1fe9c09SJoe Perches			CHK("LOGICAL_CONTINUATIONS",
2791d1fe9c09SJoe Perches			    "Logical continuations should be on the previous line\n" . $hereprev);
2792d1fe9c09SJoe Perches		}
2793d1fe9c09SJoe Perches
2794a91e8994SJoe Perches# check indentation starts on a tab stop
2795a91e8994SJoe Perches		if ($^V && $^V ge 5.10.0 &&
2796a91e8994SJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2797a91e8994SJoe Perches			my $indent = length($1);
2798a91e8994SJoe Perches			if ($indent % 8) {
2799a91e8994SJoe Perches				if (WARN("TABSTOP",
2800a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
2801a91e8994SJoe Perches				    $fix) {
2802a91e8994SJoe Perches					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2803a91e8994SJoe Perches				}
2804a91e8994SJoe Perches			}
2805a91e8994SJoe Perches		}
2806a91e8994SJoe Perches
2807d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
2808d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
280991cb5195SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
2810d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
2811d1fe9c09SJoe Perches			my $oldindent = $1;
2812d1fe9c09SJoe Perches			my $rest = $2;
2813d1fe9c09SJoe Perches
2814d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
2815d1fe9c09SJoe Perches			if ($pos >= 0) {
2816b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
2817b34a26f3SJoe Perches				my $newindent = $2;
2818d1fe9c09SJoe Perches
2819d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
2820d1fe9c09SJoe Perches					"\t" x ($pos / 8) .
2821d1fe9c09SJoe Perches					" "  x ($pos % 8);
2822d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
2823d1fe9c09SJoe Perches
2824d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
2825d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
28263705ce5bSJoe Perches
28273705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
28283705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
28293705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
2830194f66fcSJoe Perches						$fixed[$fixlinenr] =~
28313705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
28323705ce5bSJoe Perches					}
2833d1fe9c09SJoe Perches				}
2834d1fe9c09SJoe Perches			}
2835d1fe9c09SJoe Perches		}
2836d1fe9c09SJoe Perches
28376ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
28386ab3a970SJoe Perches# avoid checking a few false positives:
28396ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
28406ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
28416ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
28426ab3a970SJoe Perches#   multiline macros that define functions
28436ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
28446ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
28456ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
28463705ce5bSJoe Perches			if (CHK("SPACING",
2847f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
28483705ce5bSJoe Perches			    $fix) {
2849194f66fcSJoe Perches				$fixed[$fixlinenr] =~
2850f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
28513705ce5bSJoe Perches			}
2852aad4f614SJoe Perches		}
2853aad4f614SJoe Perches
285486406b1cSJoe Perches# Block comment styles
285586406b1cSJoe Perches# Networking with an initial /*
285605880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
2857fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
285885ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
285985ad978cSJoe Perches		    $realline > 2) {
286005880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
286105880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
286205880600SJoe Perches		}
286305880600SJoe Perches
286486406b1cSJoe Perches# Block comments use * on subsequent lines
286586406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
286686406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
2867a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
286861135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
2869a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
287086406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
287186406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
2872a605e32eSJoe Perches		}
2873a605e32eSJoe Perches
287486406b1cSJoe Perches# Block comments use */ on trailing lines
287586406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
2876c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
2877c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
2878c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
287986406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
288086406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
288105880600SJoe Perches		}
288205880600SJoe Perches
28837f619191SJoe Perches# check for missing blank lines after struct/union declarations
28847f619191SJoe Perches# with exceptions for various attributes and macros
28857f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
28867f619191SJoe Perches		    $line =~ /^\+/ &&
28877f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
28887f619191SJoe Perches		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
28897f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
28907f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
28917f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
28927f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
28937f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
28947f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
2895d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2896d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2897d752fcc8SJoe Perches			    $fix) {
2898f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2899d752fcc8SJoe Perches			}
29007f619191SJoe Perches		}
29017f619191SJoe Perches
2902365dd4eaSJoe Perches# check for multiple consecutive blank lines
2903365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
2904365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
2905365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
2906d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
2907d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
2908d752fcc8SJoe Perches			    $fix) {
2909f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
2910d752fcc8SJoe Perches			}
2911d752fcc8SJoe Perches
2912365dd4eaSJoe Perches			$last_blank_line = $linenr;
2913365dd4eaSJoe Perches		}
2914365dd4eaSJoe Perches
29153b617e3bSJoe Perches# check for missing blank lines after declarations
29163f7bac03SJoe Perches		if ($sline =~ /^\+\s+\S/ &&			#Not at char 1
29173f7bac03SJoe Perches			# actual declarations
29183f7bac03SJoe Perches		    ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
29195a4e1fd3SJoe Perches			# function pointer declarations
29205a4e1fd3SJoe Perches		     $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
29213f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
29223f7bac03SJoe Perches		     $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
29233f7bac03SJoe Perches			# known declaration macros
29243f7bac03SJoe Perches		     $prevline =~ /^\+\s+$declaration_macros/) &&
29253f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
29263f7bac03SJoe Perches		    !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
29273f7bac03SJoe Perches			# other possible extensions of declaration lines
29283f7bac03SJoe Perches		      $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
29293f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
29303f7bac03SJoe Perches		      $prevline =~ /(?:\{\s*|\\)$/) &&
29313f7bac03SJoe Perches			# looks like a declaration
29323f7bac03SJoe Perches		    !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
29335a4e1fd3SJoe Perches			# function pointer declarations
29345a4e1fd3SJoe Perches		      $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
29353f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
29363f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
29373f7bac03SJoe Perches			# known declaration macros
29383f7bac03SJoe Perches		      $sline =~ /^\+\s+$declaration_macros/ ||
29393f7bac03SJoe Perches			# start of struct or union or enum
29403b617e3bSJoe Perches		      $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
29413f7bac03SJoe Perches			# start or end of block or continuation of declaration
29423f7bac03SJoe Perches		      $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
29433f7bac03SJoe Perches			# bitfield continuation
29443f7bac03SJoe Perches		      $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
29453f7bac03SJoe Perches			# other possible extensions of declaration lines
29463f7bac03SJoe Perches		      $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
29473f7bac03SJoe Perches			# indentation of previous and current line are the same
29483f7bac03SJoe Perches		    (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
2949d752fcc8SJoe Perches			if (WARN("LINE_SPACING",
2950d752fcc8SJoe Perches				 "Missing a blank line after declarations\n" . $hereprev) &&
2951d752fcc8SJoe Perches			    $fix) {
2952f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
2953d752fcc8SJoe Perches			}
29543b617e3bSJoe Perches		}
29553b617e3bSJoe Perches
29565f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
29576b4c5bebSAndy Whitcroft# Exceptions:
29586b4c5bebSAndy Whitcroft#  1) within comments
29596b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
29606b4c5bebSAndy Whitcroft#  3) hanging labels
29613705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
29625f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
29633705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
29643705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
29653705ce5bSJoe Perches			    $fix) {
2966194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
29673705ce5bSJoe Perches			}
29685f7ddae6SRaffaele Recalcati		}
29695f7ddae6SRaffaele Recalcati
2970b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
2971b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
2972b9ea10d6SAndy Whitcroft
2973032a4c0fSJoe Perches# check indentation of any line with a bare else
2974840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
2975032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
2976032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
2977032a4c0fSJoe Perches			my $tabs = length($1) + 1;
2978840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
2979840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
2980840080a0SJoe Perches			     defined $lines[$linenr] &&
2981840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
2982032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
2983032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
2984032a4c0fSJoe Perches			}
2985032a4c0fSJoe Perches		}
2986032a4c0fSJoe Perches
2987c00df19aSJoe Perches# check indentation of a line with a break;
2988c00df19aSJoe Perches# if the previous line is a goto or return and is indented the same # of tabs
2989c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
2990c00df19aSJoe Perches			my $tabs = $1;
2991c00df19aSJoe Perches			if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
2992c00df19aSJoe Perches				WARN("UNNECESSARY_BREAK",
2993c00df19aSJoe Perches				     "break is not useful after a goto or return\n" . $hereprev);
2994c00df19aSJoe Perches			}
2995c00df19aSJoe Perches		}
2996c00df19aSJoe Perches
29971ba8dfd1SKees Cook# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
29981ba8dfd1SKees Cook		if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
29991ba8dfd1SKees Cook			WARN("CONFIG_EXPERIMENTAL",
30001ba8dfd1SKees Cook			     "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
30011ba8dfd1SKees Cook		}
30021ba8dfd1SKees Cook
3003c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
3004cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
3005000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
3006000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
3007c2fdda0dSAndy Whitcroft		}
300822f2a2efSAndy Whitcroft
300942e41c54SMike Frysinger# Blackfin: don't use __builtin_bfin_[cs]sync
301042e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_csync/) {
301142e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3012000d1cc1SJoe Perches			ERROR("CSYNC",
3013000d1cc1SJoe Perches			      "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
301442e41c54SMike Frysinger		}
301542e41c54SMike Frysinger		if ($line =~ /__builtin_bfin_ssync/) {
301642e41c54SMike Frysinger			my $herevet = "$here\n" . cat_vet($line) . "\n";
3017000d1cc1SJoe Perches			ERROR("SSYNC",
3018000d1cc1SJoe Perches			      "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
301942e41c54SMike Frysinger		}
302042e41c54SMike Frysinger
302156e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
302256e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
302356e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
302456e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
302556e77d70SJoe Perches		}
302656e77d70SJoe Perches
30279c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
30282b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
30292b474a1aSAndy Whitcroft		    $realline_next);
30303e469cdcSAndy Whitcroft#print "LINE<$line>\n";
30313e469cdcSAndy Whitcroft		if ($linenr >= $suppress_statement &&
30321b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
3033170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3034f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
3035171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
3036171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
3037171ae1a4SAndy Whitcroft
30383e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
30393e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
30403e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
30413e469cdcSAndy Whitcroft			# until we hit end of it.
30423e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
30433e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
30443e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
30453e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
30463e469cdcSAndy Whitcroft			}
3047f74bd194SAndy Whitcroft
30482b474a1aSAndy Whitcroft			# Find the real next line.
30492b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
30502b474a1aSAndy Whitcroft			if (defined $realline_next &&
30512b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
30522b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
30532b474a1aSAndy Whitcroft				$realline_next++;
30542b474a1aSAndy Whitcroft			}
30552b474a1aSAndy Whitcroft
3056171ae1a4SAndy Whitcroft			my $s = $stat;
3057171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
3058cf655043SAndy Whitcroft
3059c2fdda0dSAndy Whitcroft			# Ignore goto labels.
3060171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
3061c2fdda0dSAndy Whitcroft
3062c2fdda0dSAndy Whitcroft			# Ignore functions being called
3063171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
3064c2fdda0dSAndy Whitcroft
3065463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
3066463f2864SAndy Whitcroft
3067c45dcabdSAndy Whitcroft			# declarations always start with types
3068d2506586SAndy 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) {
3069c45dcabdSAndy Whitcroft				my $type = $1;
3070c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
3071c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
3072c45dcabdSAndy Whitcroft
30736c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
3074a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
3075c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
3076c2fdda0dSAndy Whitcroft			}
30778905a67cSAndy Whitcroft
30786c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
307965863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
3080c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
30819c0ca6f9SAndy Whitcroft			}
30828905a67cSAndy Whitcroft
30838905a67cSAndy Whitcroft			# Check for any sort of function declaration.
30848905a67cSAndy Whitcroft			# int foo(something bar, other baz);
30858905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
3086171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
30878905a67cSAndy Whitcroft				my ($name_len) = length($1);
30888905a67cSAndy Whitcroft
3089cf655043SAndy Whitcroft				my $ctx = $s;
3090773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
30918905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
3092cf655043SAndy Whitcroft
30938905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
3094c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
30958905a67cSAndy Whitcroft
3096c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
30978905a67cSAndy Whitcroft					}
30988905a67cSAndy Whitcroft				}
30998905a67cSAndy Whitcroft			}
31008905a67cSAndy Whitcroft
31019c0ca6f9SAndy Whitcroft		}
31029c0ca6f9SAndy Whitcroft
310300df344fSAndy Whitcroft#
310400df344fSAndy Whitcroft# Checks which may be anchored in the context.
310500df344fSAndy Whitcroft#
310600df344fSAndy Whitcroft
310700df344fSAndy Whitcroft# Check for switch () and associated case and default
310800df344fSAndy Whitcroft# statements should be at the same indent.
310900df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
311000df344fSAndy Whitcroft			my $err = '';
311100df344fSAndy Whitcroft			my $sep = '';
311200df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
311300df344fSAndy Whitcroft			shift(@ctx);
311400df344fSAndy Whitcroft			for my $ctx (@ctx) {
311500df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
311600df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
311700df344fSAndy Whitcroft							$indent != $cindent) {
311800df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
311900df344fSAndy Whitcroft					$sep = '';
312000df344fSAndy Whitcroft				} else {
312100df344fSAndy Whitcroft					$sep = "[...]\n";
312200df344fSAndy Whitcroft				}
312300df344fSAndy Whitcroft			}
312400df344fSAndy Whitcroft			if ($err ne '') {
3125000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
3126000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
3127de7d4f0eSAndy Whitcroft			}
3128de7d4f0eSAndy Whitcroft		}
3129de7d4f0eSAndy Whitcroft
3130de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
3131de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
31320fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
3133773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
3134773647a0SAndy Whitcroft
31359c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
31368eef05ddSJoe Perches
31378eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
31388eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
31398eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
31408eef05ddSJoe Perches			}
31418eef05ddSJoe Perches
3142de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
3143de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
3144de7d4f0eSAndy Whitcroft
3145548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
3146548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
3147de7d4f0eSAndy Whitcroft
3148548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3149548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
3150548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
3151548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3152548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
3153773647a0SAndy Whitcroft				$ctx_ln++;
3154773647a0SAndy Whitcroft			}
3155548596d5SAndy Whitcroft
315653210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
315753210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
3158773647a0SAndy Whitcroft
3159773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
3160000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
3161000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
316201464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
316300df344fSAndy Whitcroft			}
3164773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3165773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
3166773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
3167773647a0SAndy Whitcroft			{
31689c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
31699c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
3170000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
3171000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
317201464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
31739c0ca6f9SAndy Whitcroft				}
31749c0ca6f9SAndy Whitcroft			}
317500df344fSAndy Whitcroft		}
317600df344fSAndy Whitcroft
31774d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
31780fe3dc2bSJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
31793e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
31803e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
31813e469cdcSAndy Whitcroft					if (!defined $stat);
31824d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
31834d001e4dSAndy Whitcroft
31844d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
31854d001e4dSAndy Whitcroft
31869f5af480SJoe Perches			# remove inline comments
31879f5af480SJoe Perches			$s =~ s/$;/ /g;
31889f5af480SJoe Perches			$c =~ s/$;/ /g;
31894d001e4dSAndy Whitcroft
31904d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
31916f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
31926f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
31934d001e4dSAndy Whitcroft
31949f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
31959f5af480SJoe Perches			# none on the first line, and are going to readd them
31969f5af480SJoe Perches			# where necessary.
31979f5af480SJoe Perches			$s =~ s/\n./\n/gs;
31989f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
31999f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
32009f5af480SJoe Perches			}
32019f5af480SJoe Perches
32024d001e4dSAndy Whitcroft			# We want to check the first line inside the block
32034d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
32044d001e4dSAndy Whitcroft			#  1) any blank line termination
32054d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
32064d001e4dSAndy Whitcroft			#  3) any do (...) {
32074d001e4dSAndy Whitcroft			my $continuation = 0;
32084d001e4dSAndy Whitcroft			my $check = 0;
32094d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
32104d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
32114d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
32124d001e4dSAndy Whitcroft				$continuation = 1;
32134d001e4dSAndy Whitcroft			}
32149bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
32154d001e4dSAndy Whitcroft				$check = 1;
32164d001e4dSAndy Whitcroft				$cond_lines++;
32174d001e4dSAndy Whitcroft			}
32184d001e4dSAndy Whitcroft
32194d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
32204d001e4dSAndy Whitcroft			# preprocessor statement.
32214d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
32224d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
32234d001e4dSAndy Whitcroft				$check = 0;
32244d001e4dSAndy Whitcroft			}
32254d001e4dSAndy Whitcroft
32269bd49efeSAndy Whitcroft			my $cond_ptr = -1;
3227740504c6SAndy Whitcroft			$continuation = 0;
32289bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
32299bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
32304d001e4dSAndy Whitcroft
3231f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
3232f16fa28fSAndy Whitcroft				# is not linear.
3233f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3234f16fa28fSAndy Whitcroft					$check = 0;
3235f16fa28fSAndy Whitcroft				}
3236f16fa28fSAndy Whitcroft
32379bd49efeSAndy Whitcroft				# Ignore:
32389bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
32399bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
32409bd49efeSAndy Whitcroft				#  3) labels.
3241740504c6SAndy Whitcroft				if ($continuation ||
3242740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
32439bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
32449bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
3245740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
324630dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
32479bd49efeSAndy Whitcroft						$cond_lines++;
32489bd49efeSAndy Whitcroft					}
32494d001e4dSAndy Whitcroft				}
325030dad6ebSAndy Whitcroft			}
32514d001e4dSAndy Whitcroft
32524d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
32534d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
32544d001e4dSAndy Whitcroft
32554d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
32564d001e4dSAndy Whitcroft			# this is not this patch's fault.
32574d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
32584d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
32594d001e4dSAndy Whitcroft				$check = 0;
32604d001e4dSAndy Whitcroft			}
32614d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
32624d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
32634d001e4dSAndy Whitcroft			}
32644d001e4dSAndy Whitcroft
32659bd49efeSAndy 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";
32664d001e4dSAndy Whitcroft
32679f5af480SJoe Perches			if ($check && $s ne '' &&
32689f5af480SJoe Perches			    (($sindent % 8) != 0 ||
32699f5af480SJoe Perches			     ($sindent < $indent) ||
32709f5af480SJoe Perches			     ($sindent > $indent + 8))) {
3271000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
3272000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
32734d001e4dSAndy Whitcroft			}
32744d001e4dSAndy Whitcroft		}
32754d001e4dSAndy Whitcroft
32766c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
32776c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
32781f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
32791f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
32806c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
3281c2fdda0dSAndy Whitcroft		if ($dbg_values) {
3282c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
3283cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
3284cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
32851f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
3286c2fdda0dSAndy Whitcroft		}
32876c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
32886c72ffaaSAndy Whitcroft
328900df344fSAndy Whitcroft#ignore lines not being added
32903705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
329100df344fSAndy Whitcroft
3292a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
3293207a8e84SJoe Perches		while ($line =~ m{($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
3294a1ce18e4SJoe Perches			my $type = $1;
3295a1ce18e4SJoe Perches			my $var = $2;
3296207a8e84SJoe Perches			$var = "" if (!defined $var);
3297207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
3298a1ce18e4SJoe Perches				my $sign = $1;
3299a1ce18e4SJoe Perches				my $pointer = $2;
3300a1ce18e4SJoe Perches
3301a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
3302a1ce18e4SJoe Perches
3303a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
3304a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3305a1ce18e4SJoe Perches				    $fix) {
3306a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
3307207a8e84SJoe Perches					my $comp_pointer = $pointer;
3308207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
3309207a8e84SJoe Perches					$decl .= $comp_pointer;
3310207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
3311207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
3312a1ce18e4SJoe Perches				}
3313a1ce18e4SJoe Perches			}
3314a1ce18e4SJoe Perches		}
3315a1ce18e4SJoe Perches
3316653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
33177429c690SAndy Whitcroft		if ($dbg_type) {
33187429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
3319000d1cc1SJoe Perches				ERROR("TEST_TYPE",
3320000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
33217429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
3322000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
3323000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
33247429c690SAndy Whitcroft			}
3325653d4876SAndy Whitcroft			next;
3326653d4876SAndy Whitcroft		}
3327a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
3328a1ef277eSAndy Whitcroft		if ($dbg_attr) {
33299360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
3330000d1cc1SJoe Perches				ERROR("TEST_ATTR",
3331000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
33329360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
3333000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
3334000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
3335a1ef277eSAndy Whitcroft			}
3336a1ef277eSAndy Whitcroft			next;
3337a1ef277eSAndy Whitcroft		}
3338653d4876SAndy Whitcroft
3339f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
334099423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
334199423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
3342d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
3343d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
3344f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3345f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
3346f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
3347d752fcc8SJoe Perches				my $fixedline = $prevrawline;
3348d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
3349f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3350d752fcc8SJoe Perches				$fixedline = $line;
3351d752fcc8SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1/;
3352f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
3353d752fcc8SJoe Perches			}
3354f0a594c1SAndy Whitcroft		}
3355f0a594c1SAndy Whitcroft
335600df344fSAndy Whitcroft#
335700df344fSAndy Whitcroft# Checks which are anchored on the added line.
335800df344fSAndy Whitcroft#
335900df344fSAndy Whitcroft
3360653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
3361c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
3362653d4876SAndy Whitcroft			my $path = $1;
3363653d4876SAndy Whitcroft			if ($path =~ m{//}) {
3364000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
3365495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
3366495e9d84SJoe Perches			}
3367495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3368495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
3369495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
3370653d4876SAndy Whitcroft			}
3371653d4876SAndy Whitcroft		}
3372653d4876SAndy Whitcroft
337300df344fSAndy Whitcroft# no C99 // comments
337400df344fSAndy Whitcroft		if ($line =~ m{//}) {
33753705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
33763705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
33773705ce5bSJoe Perches			    $fix) {
3378194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
33793705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
33803705ce5bSJoe Perches					my $comment = trim($1);
3381194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
33823705ce5bSJoe Perches				}
33833705ce5bSJoe Perches			}
338400df344fSAndy Whitcroft		}
338500df344fSAndy Whitcroft		# Remove C99 comments.
33860a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
33876c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
33880a920b5bSAndy Whitcroft
33892b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
33902b474a1aSAndy Whitcroft# the whole statement.
33912b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
33922b474a1aSAndy Whitcroft		if (defined $realline_next &&
33932b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
33942b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
33952b474a1aSAndy Whitcroft		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
33962b474a1aSAndy Whitcroft		     $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
33973cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
33983cbf62dfSAndy Whitcroft			# a prefix:
33993cbf62dfSAndy Whitcroft			#   XXX(foo);
34003cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
3401653d4876SAndy Whitcroft			my $name = $1;
340287a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
34033cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
34043cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
34053cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
34063cbf62dfSAndy Whitcroft
34073cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
34082b474a1aSAndy Whitcroft				\n.}\s*$|
340948012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
341048012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
341148012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
34122b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
34132b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
341448012058SAndy Whitcroft			    )/x) {
34152b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
34162b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
34172b474a1aSAndy Whitcroft			} else {
34182b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
34190a920b5bSAndy Whitcroft			}
34200a920b5bSAndy Whitcroft		}
34212b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
34222b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
34232b474a1aSAndy Whitcroft		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
34242b474a1aSAndy Whitcroft		     $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
34252b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
34262b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
34272b474a1aSAndy Whitcroft		}
34282b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
34292b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
3430000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
3431000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
34322b474a1aSAndy Whitcroft		}
34330a920b5bSAndy Whitcroft
34345150bda4SJoe Eloff# check for global initialisers.
34356d32f7a3SJoe Perches		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
3436d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
34376d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
3438d5e616fcSJoe Perches			    $fix) {
34396d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
3440d5e616fcSJoe Perches			}
3441f0a594c1SAndy Whitcroft		}
34420a920b5bSAndy Whitcroft# check for static initialisers.
34436d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
3444d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
34456d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
3446d5e616fcSJoe Perches				      $herecurr) &&
3447d5e616fcSJoe Perches			    $fix) {
34486d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
3449d5e616fcSJoe Perches			}
34500a920b5bSAndy Whitcroft		}
34510a920b5bSAndy Whitcroft
34521813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
34531813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
34541813087dSJoe Perches			my $tmp = trim($1);
34551813087dSJoe Perches			WARN("MISORDERED_TYPE",
34561813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
34571813087dSJoe Perches		}
34581813087dSJoe Perches
3459cb710ecaSJoe Perches# check for static const char * arrays.
3460cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
3461000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3462000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
3463cb710ecaSJoe Perches				$herecurr);
3464cb710ecaSJoe Perches               }
3465cb710ecaSJoe Perches
3466cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
3467cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
3468000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
3469000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
3470cb710ecaSJoe Perches				$herecurr);
3471cb710ecaSJoe Perches               }
3472cb710ecaSJoe Perches
3473ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
3474ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3475ab7e23f3SJoe Perches			my $found = $1;
3476ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3477ab7e23f3SJoe Perches				WARN("CONST_CONST",
3478ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3479ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3480ab7e23f3SJoe Perches				WARN("CONST_CONST",
3481ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
3482ab7e23f3SJoe Perches			}
3483ab7e23f3SJoe Perches		}
3484ab7e23f3SJoe Perches
34859b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
34869b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
34879b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
34889b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
34899b0fa60dSJoe Perches				$herecurr);
34909b0fa60dSJoe Perches               }
34919b0fa60dSJoe Perches
3492b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3493b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3494b598b670SJoe Perches			my $array = $1;
3495b598b670SJoe 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*\))@) {
3496b598b670SJoe Perches				my $array_div = $1;
3497b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
3498b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3499b598b670SJoe Perches				    $fix) {
3500b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3501b598b670SJoe Perches				}
3502b598b670SJoe Perches			}
3503b598b670SJoe Perches		}
3504b598b670SJoe Perches
3505b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
3506b36190c5SJoe Perches		if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3507b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
3508b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3509b36190c5SJoe Perches			    $fix) {
3510194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
3511b36190c5SJoe Perches			}
3512b36190c5SJoe Perches		}
3513b36190c5SJoe Perches
351492e112fdSJoe Perches# check for uses of DEFINE_PCI_DEVICE_TABLE
351592e112fdSJoe Perches		if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
351692e112fdSJoe Perches			if (WARN("DEFINE_PCI_DEVICE_TABLE",
351792e112fdSJoe Perches				 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
351892e112fdSJoe Perches			    $fix) {
3519194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
352092e112fdSJoe Perches			}
352193ed0e2dSJoe Perches		}
352293ed0e2dSJoe Perches
3523653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
3524653d4876SAndy Whitcroft# make sense.
3525653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
35268054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
3527c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
35288ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
3529653d4876SAndy Whitcroft		    $line !~ /\b__bitwise(?:__|)\b/) {
3530000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
3531000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
35320a920b5bSAndy Whitcroft		}
35330a920b5bSAndy Whitcroft
35340a920b5bSAndy Whitcroft# * goes on variable not on type
353565863862SAndy Whitcroft		# (char*[ const])
3536bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3537bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
35383705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
3539d8aaf121SAndy Whitcroft
354065863862SAndy Whitcroft			# Should start with a space.
354165863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
354265863862SAndy Whitcroft			# Should not end with a space.
354365863862SAndy Whitcroft			$to =~ s/\s+$//;
354465863862SAndy Whitcroft			# '*'s should not have spaces between.
3545f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
354665863862SAndy Whitcroft			}
3547d8aaf121SAndy Whitcroft
35483705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
354965863862SAndy Whitcroft			if ($from ne $to) {
35503705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
35513705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
35523705ce5bSJoe Perches				    $fix) {
35533705ce5bSJoe Perches					my $sub_from = $ident;
35543705ce5bSJoe Perches					my $sub_to = $ident;
35553705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3556194f66fcSJoe Perches					$fixed[$fixlinenr] =~
35573705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
35583705ce5bSJoe Perches				}
355965863862SAndy Whitcroft			}
3560bfcb2cc7SAndy Whitcroft		}
3561bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3562bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
35633705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
3564d8aaf121SAndy Whitcroft
356565863862SAndy Whitcroft			# Should start with a space.
356665863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
356765863862SAndy Whitcroft			# Should not end with a space.
356865863862SAndy Whitcroft			$to =~ s/\s+$//;
356965863862SAndy Whitcroft			# '*'s should not have spaces between.
3570f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
357165863862SAndy Whitcroft			}
357265863862SAndy Whitcroft			# Modifiers should have spaces.
357365863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
357465863862SAndy Whitcroft
35753705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
3576667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
35773705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
35783705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
35793705ce5bSJoe Perches				    $fix) {
35803705ce5bSJoe Perches
35813705ce5bSJoe Perches					my $sub_from = $match;
35823705ce5bSJoe Perches					my $sub_to = $match;
35833705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
3584194f66fcSJoe Perches					$fixed[$fixlinenr] =~
35853705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
35863705ce5bSJoe Perches				}
358765863862SAndy Whitcroft			}
35880a920b5bSAndy Whitcroft		}
35890a920b5bSAndy Whitcroft
35909d3e3c70SJoe Perches# avoid BUG() or BUG_ON()
35919d3e3c70SJoe Perches		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
35929d3e3c70SJoe Perches			my $msg_type = \&WARN;
35939d3e3c70SJoe Perches			$msg_type = \&CHK if ($file);
35949d3e3c70SJoe Perches			&{$msg_type}("AVOID_BUG",
35959d3e3c70SJoe Perches				     "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
35969d3e3c70SJoe Perches		}
35970a920b5bSAndy Whitcroft
35989d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
35998905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
3600000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
3601000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
36028905a67cSAndy Whitcroft		}
36038905a67cSAndy Whitcroft
360417441227SJoe Perches# check for uses of printk_ratelimit
360517441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
3606000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
3607000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
360817441227SJoe Perches		}
360917441227SJoe Perches
361000df344fSAndy Whitcroft# printk should use KERN_* levels.  Note that follow on printk's on the
361100df344fSAndy Whitcroft# same line do not need a level, so we use the current block context
361200df344fSAndy Whitcroft# to try and find and validate the current printk.  In summary the current
361325985edcSLucas De Marchi# printk includes all preceding printk's which have no newline on the end.
361400df344fSAndy Whitcroft# we assume the first bad printk is the one to report.
3615f0a594c1SAndy Whitcroft		if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
361600df344fSAndy Whitcroft			my $ok = 0;
361700df344fSAndy Whitcroft			for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
361800df344fSAndy Whitcroft				#print "CHECK<$lines[$ln - 1]\n";
361925985edcSLucas De Marchi				# we have a preceding printk if it ends
362000df344fSAndy Whitcroft				# with "\n" ignore it, else it is to blame
362100df344fSAndy Whitcroft				if ($lines[$ln - 1] =~ m{\bprintk\(}) {
362200df344fSAndy Whitcroft					if ($rawlines[$ln - 1] !~ m{\\n"}) {
362300df344fSAndy Whitcroft						$ok = 1;
362400df344fSAndy Whitcroft					}
362500df344fSAndy Whitcroft					last;
362600df344fSAndy Whitcroft				}
362700df344fSAndy Whitcroft			}
362800df344fSAndy Whitcroft			if ($ok == 0) {
3629000d1cc1SJoe Perches				WARN("PRINTK_WITHOUT_KERN_LEVEL",
3630000d1cc1SJoe Perches				     "printk() should include KERN_ facility level\n" . $herecurr);
36310a920b5bSAndy Whitcroft			}
363200df344fSAndy Whitcroft		}
36330a920b5bSAndy Whitcroft
3634243f3803SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3635243f3803SJoe Perches			my $orig = $1;
3636243f3803SJoe Perches			my $level = lc($orig);
3637243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
36388f26b837SJoe Perches			my $level2 = $level;
36398f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
3640243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
3641daa8b059SYogesh Chaudhari			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to printk(KERN_$orig ...\n" . $herecurr);
3642243f3803SJoe Perches		}
3643243f3803SJoe Perches
3644243f3803SJoe Perches		if ($line =~ /\bpr_warning\s*\(/) {
3645d5e616fcSJoe Perches			if (WARN("PREFER_PR_LEVEL",
3646d5e616fcSJoe Perches				 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3647d5e616fcSJoe Perches			    $fix) {
3648194f66fcSJoe Perches				$fixed[$fixlinenr] =~
3649d5e616fcSJoe Perches				    s/\bpr_warning\b/pr_warn/;
3650d5e616fcSJoe Perches			}
3651243f3803SJoe Perches		}
3652243f3803SJoe Perches
3653dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3654dc139313SJoe Perches			my $orig = $1;
3655dc139313SJoe Perches			my $level = lc($orig);
3656dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
3657dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
3658dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
3659dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3660dc139313SJoe Perches		}
3661dc139313SJoe Perches
366291c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
366391c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
366491c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
366591c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
366691c9afafSAndy Lutomirski			WARN("ENOSYS",
366791c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
366891c9afafSAndy Lutomirski		}
366991c9afafSAndy Lutomirski
3670653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
3671653d4876SAndy Whitcroft# or if closed on same line
36728d182478SJoe Perches		if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
36734e5d56bdSEddie Kovsky		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
36748d182478SJoe Perches			if (ERROR("OPEN_BRACE",
36758d182478SJoe Perches				  "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
36768d182478SJoe Perches			    $fix) {
36778d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
36788d182478SJoe Perches				my $fixed_line = $rawline;
36798d182478SJoe Perches				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
36808d182478SJoe Perches				my $line1 = $1;
36818d182478SJoe Perches				my $line2 = $2;
36828d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
36838d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
36848d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
36858d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
36868d182478SJoe Perches				}
36878d182478SJoe Perches			}
36880a920b5bSAndy Whitcroft		}
3689653d4876SAndy Whitcroft
36908905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
36918905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
36928905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
36938d182478SJoe Perches			if (ERROR("OPEN_BRACE",
36948d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
36958d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
36968d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
36978d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
36988d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
36998d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
37008d182478SJoe Perches				$fixedline = $rawline;
37018d182478SJoe Perches				$fixedline =~ s/^(.\s*){\s*/$1\t/;
37028d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
37038d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
37048d182478SJoe Perches				}
37058d182478SJoe Perches			}
37068905a67cSAndy Whitcroft		}
37078905a67cSAndy Whitcroft
37080c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
37093705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
37103705ce5bSJoe Perches			if (WARN("SPACING",
37113705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
37123705ce5bSJoe Perches			    $fix) {
3713194f66fcSJoe Perches				$fixed[$fixlinenr] =~
37143705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
37153705ce5bSJoe Perches			}
37160c73b4ebSAndy Whitcroft		}
37170c73b4ebSAndy Whitcroft
371831070b5dSJoe Perches# Function pointer declarations
371931070b5dSJoe Perches# check spacing between type, funcptr, and args
372031070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
372191f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
372231070b5dSJoe Perches			my $declare = $1;
372331070b5dSJoe Perches			my $pre_pointer_space = $2;
372431070b5dSJoe Perches			my $post_pointer_space = $3;
372531070b5dSJoe Perches			my $funcname = $4;
372631070b5dSJoe Perches			my $post_funcname_space = $5;
372731070b5dSJoe Perches			my $pre_args_space = $6;
372831070b5dSJoe Perches
372991f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
373091f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
373191f72e9cSJoe Perches# don't need a space so don't warn for those.
373291f72e9cSJoe Perches			my $post_declare_space = "";
373391f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
373491f72e9cSJoe Perches				$post_declare_space = $1;
373591f72e9cSJoe Perches				$declare = rtrim($declare);
373691f72e9cSJoe Perches			}
373791f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
373831070b5dSJoe Perches				WARN("SPACING",
373931070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
374091f72e9cSJoe Perches				$post_declare_space = " ";
374131070b5dSJoe Perches			}
374231070b5dSJoe Perches
374331070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
374491f72e9cSJoe Perches# This test is not currently implemented because these declarations are
374591f72e9cSJoe Perches# equivalent to
374691f72e9cSJoe Perches#	int  foo(int bar, ...)
374791f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
374891f72e9cSJoe Perches#
374991f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
375091f72e9cSJoe Perches#				WARN("SPACING",
375191f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
375291f72e9cSJoe Perches#			}
375331070b5dSJoe Perches
375431070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
375531070b5dSJoe Perches			if (defined $pre_pointer_space &&
375631070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
375731070b5dSJoe Perches				WARN("SPACING",
375831070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
375931070b5dSJoe Perches			}
376031070b5dSJoe Perches
376131070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
376231070b5dSJoe Perches			if (defined $post_pointer_space &&
376331070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
376431070b5dSJoe Perches				WARN("SPACING",
376531070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
376631070b5dSJoe Perches			}
376731070b5dSJoe Perches
376831070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
376931070b5dSJoe Perches			if (defined $post_funcname_space &&
377031070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
377131070b5dSJoe Perches				WARN("SPACING",
377231070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
377331070b5dSJoe Perches			}
377431070b5dSJoe Perches
377531070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
377631070b5dSJoe Perches			if (defined $pre_args_space &&
377731070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
377831070b5dSJoe Perches				WARN("SPACING",
377931070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
378031070b5dSJoe Perches			}
378131070b5dSJoe Perches
378231070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
3783194f66fcSJoe Perches				$fixed[$fixlinenr] =~
378491f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
378531070b5dSJoe Perches			}
378631070b5dSJoe Perches		}
378731070b5dSJoe Perches
37888d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
37898d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
3790fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3791fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
37928d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
37938d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
37948d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
3795fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
3796daebc534SAndy Whitcroft			    $prefix !~ /[{,]\s+$/) {
37973705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
37983705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
37993705ce5bSJoe Perches				    $fix) {
3800194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
38013705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
38023705ce5bSJoe Perches				}
38038d31cfceSAndy Whitcroft			}
38048d31cfceSAndy Whitcroft		}
38058d31cfceSAndy Whitcroft
3806f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
38076c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
3808c2fdda0dSAndy Whitcroft			my $name = $1;
3809773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
3810773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
3811c2fdda0dSAndy Whitcroft
3812c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
3813773647a0SAndy Whitcroft			if ($name =~ /^(?:
3814773647a0SAndy Whitcroft				if|for|while|switch|return|case|
3815773647a0SAndy Whitcroft				volatile|__volatile__|
3816773647a0SAndy Whitcroft				__attribute__|format|__extension__|
3817773647a0SAndy Whitcroft				asm|__asm__)$/x)
3818773647a0SAndy Whitcroft			{
3819c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
3820c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
3821c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
3822c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
3823773647a0SAndy Whitcroft
3824773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
3825c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
3826c2fdda0dSAndy Whitcroft
3827c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
3828c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
3829773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
3830c2fdda0dSAndy Whitcroft
3831c2fdda0dSAndy Whitcroft			} else {
38323705ce5bSJoe Perches				if (WARN("SPACING",
38333705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
38343705ce5bSJoe Perches					     $fix) {
3835194f66fcSJoe Perches					$fixed[$fixlinenr] =~
38363705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
38373705ce5bSJoe Perches				}
3838f0a594c1SAndy Whitcroft			}
38396c72ffaaSAndy Whitcroft		}
38409a4cad4eSEric Nelson
3841653d4876SAndy Whitcroft# Check operator spacing.
38420a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
38433705ce5bSJoe Perches			my $fixed_line = "";
38443705ce5bSJoe Perches			my $line_fixed = 0;
38453705ce5bSJoe Perches
38469c0ca6f9SAndy Whitcroft			my $ops = qr{
38479c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
38489c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
38499c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
38501f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
385184731623SJoe Perches				\?:|\?|:
38529c0ca6f9SAndy Whitcroft			}x;
3853cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
38543705ce5bSJoe Perches
38553705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
38563705ce5bSJoe Perches##			foreach my $el (@elements) {
38573705ce5bSJoe Perches##				print("el: <$el>\n");
38583705ce5bSJoe Perches##			}
38593705ce5bSJoe Perches
38603705ce5bSJoe Perches			my @fix_elements = ();
386100df344fSAndy Whitcroft			my $off = 0;
38626c72ffaaSAndy Whitcroft
38633705ce5bSJoe Perches			foreach my $el (@elements) {
38643705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
38653705ce5bSJoe Perches				$off += length($el);
38663705ce5bSJoe Perches			}
38673705ce5bSJoe Perches
38683705ce5bSJoe Perches			$off = 0;
38693705ce5bSJoe Perches
38706c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
3871b34c648bSJoe Perches			my $last_after = -1;
38726c72ffaaSAndy Whitcroft
38730a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
38743705ce5bSJoe Perches
38753705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
38763705ce5bSJoe Perches
38773705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
38783705ce5bSJoe Perches
38794a0df2efSAndy Whitcroft				$off += length($elements[$n]);
38804a0df2efSAndy Whitcroft
388125985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
3882773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
3883773647a0SAndy Whitcroft				my $cc = '';
3884773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
3885773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
3886773647a0SAndy Whitcroft				}
3887773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
3888773647a0SAndy Whitcroft
38894a0df2efSAndy Whitcroft				my $a = '';
38904a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
38914a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
3892cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
38934a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
38944a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
3895773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
38964a0df2efSAndy Whitcroft
38970a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
38984a0df2efSAndy Whitcroft
38994a0df2efSAndy Whitcroft				my $c = '';
39000a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
39014a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
39024a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
3903cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
39044a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
39054a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
39068b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
39074a0df2efSAndy Whitcroft				} else {
39084a0df2efSAndy Whitcroft					$c = 'E';
39090a920b5bSAndy Whitcroft				}
39100a920b5bSAndy Whitcroft
39114a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
39124a0df2efSAndy Whitcroft
39134a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
39144a0df2efSAndy Whitcroft
39156c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
3916de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
39170a920b5bSAndy Whitcroft
391874048ed8SAndy Whitcroft				# Pull out the value of this operator.
39196c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
39200a920b5bSAndy Whitcroft
39211f65f947SAndy Whitcroft				# Get the full operator variant.
39221f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
39231f65f947SAndy Whitcroft
392413214adfSAndy Whitcroft				# Ignore operators passed as parameters.
392513214adfSAndy Whitcroft				if ($op_type ne 'V' &&
3926d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
392713214adfSAndy Whitcroft
3928cf655043SAndy Whitcroft#				# Ignore comments
3929cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
393013214adfSAndy Whitcroft
3931d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
393213214adfSAndy Whitcroft				} elsif ($op eq ';') {
3933cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
3934cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
39353705ce5bSJoe Perches						if (ERROR("SPACING",
39363705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
3937b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
39383705ce5bSJoe Perches							$line_fixed = 1;
39393705ce5bSJoe Perches						}
3940d8aaf121SAndy Whitcroft					}
3941d8aaf121SAndy Whitcroft
3942d8aaf121SAndy Whitcroft				# // is a comment
3943d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
39440a920b5bSAndy Whitcroft
3945b00e4814SJoe Perches				#   :   when part of a bitfield
3946b00e4814SJoe Perches				} elsif ($opv eq ':B') {
3947b00e4814SJoe Perches					# skip the bitfield test for now
3948b00e4814SJoe Perches
39491f65f947SAndy Whitcroft				# No spaces for:
39501f65f947SAndy Whitcroft				#   ->
3951b00e4814SJoe Perches				} elsif ($op eq '->') {
39524a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
39533705ce5bSJoe Perches						if (ERROR("SPACING",
39543705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
3955b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
39563705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
39573705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
39583705ce5bSJoe Perches							}
3959b34c648bSJoe Perches							$line_fixed = 1;
39603705ce5bSJoe Perches						}
39610a920b5bSAndy Whitcroft					}
39620a920b5bSAndy Whitcroft
39632381097bSJoe Perches				# , must not have a space before and must have a space on the right.
39640a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
39652381097bSJoe Perches					my $rtrim_before = 0;
39662381097bSJoe Perches					my $space_after = 0;
39672381097bSJoe Perches					if ($ctx =~ /Wx./) {
39682381097bSJoe Perches						if (ERROR("SPACING",
39692381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
39702381097bSJoe Perches							$line_fixed = 1;
39712381097bSJoe Perches							$rtrim_before = 1;
39722381097bSJoe Perches						}
39732381097bSJoe Perches					}
3974cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
39753705ce5bSJoe Perches						if (ERROR("SPACING",
39763705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
39773705ce5bSJoe Perches							$line_fixed = 1;
3978b34c648bSJoe Perches							$last_after = $n;
39792381097bSJoe Perches							$space_after = 1;
39802381097bSJoe Perches						}
39812381097bSJoe Perches					}
39822381097bSJoe Perches					if ($rtrim_before || $space_after) {
39832381097bSJoe Perches						if ($rtrim_before) {
39842381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
39852381097bSJoe Perches						} else {
39862381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
39872381097bSJoe Perches						}
39882381097bSJoe Perches						if ($space_after) {
39892381097bSJoe Perches							$good .= " ";
39903705ce5bSJoe Perches						}
39910a920b5bSAndy Whitcroft					}
39920a920b5bSAndy Whitcroft
39939c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
399474048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
39959c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
39969c0ca6f9SAndy Whitcroft
39979c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
39989c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
39999c0ca6f9SAndy Whitcroft				# unary operator, or a cast
40009c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
400174048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
40020d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
4003cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
40043705ce5bSJoe Perches						if (ERROR("SPACING",
40053705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
4006b34c648bSJoe Perches							if ($n != $last_after + 2) {
4007b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
40083705ce5bSJoe Perches								$line_fixed = 1;
40093705ce5bSJoe Perches							}
40100a920b5bSAndy Whitcroft						}
4011b34c648bSJoe Perches					}
4012a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
4013171ae1a4SAndy Whitcroft						# A unary '*' may be const
4014171ae1a4SAndy Whitcroft
4015171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
40163705ce5bSJoe Perches						if (ERROR("SPACING",
40173705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4018b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
40193705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
40203705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
40213705ce5bSJoe Perches							}
4022b34c648bSJoe Perches							$line_fixed = 1;
40233705ce5bSJoe Perches						}
40240a920b5bSAndy Whitcroft					}
40250a920b5bSAndy Whitcroft
40260a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
40270a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
4028773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
40293705ce5bSJoe Perches						if (ERROR("SPACING",
40303705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
4031b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
40323705ce5bSJoe Perches							$line_fixed = 1;
40333705ce5bSJoe Perches						}
40340a920b5bSAndy Whitcroft					}
4035773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
4036773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
40373705ce5bSJoe Perches						if (ERROR("SPACING",
40383705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4039b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
40403705ce5bSJoe Perches							$line_fixed = 1;
40413705ce5bSJoe Perches						}
4042653d4876SAndy Whitcroft					}
4043773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
40443705ce5bSJoe Perches						if (ERROR("SPACING",
40453705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
4046b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
40473705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
40483705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4049773647a0SAndy Whitcroft							}
4050b34c648bSJoe Perches							$line_fixed = 1;
40513705ce5bSJoe Perches						}
40523705ce5bSJoe Perches					}
40530a920b5bSAndy Whitcroft
40540a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
40559c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
40569c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
40579c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
4058c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
4059c2fdda0dSAndy Whitcroft					 $op eq '%')
40600a920b5bSAndy Whitcroft				{
4061d2e025f3SJoe Perches					if ($check) {
4062d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4063d2e025f3SJoe Perches							if (CHK("SPACING",
4064d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
4065d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4066d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4067d2e025f3SJoe Perches								$line_fixed = 1;
4068d2e025f3SJoe Perches							}
4069d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4070d2e025f3SJoe Perches							if (CHK("SPACING",
4071d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
4072d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4073d2e025f3SJoe Perches								$line_fixed = 1;
4074d2e025f3SJoe Perches							}
4075d2e025f3SJoe Perches						}
4076d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
40773705ce5bSJoe Perches						if (ERROR("SPACING",
40783705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
4079b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4080b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4081b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4082b34c648bSJoe Perches							}
40833705ce5bSJoe Perches							$line_fixed = 1;
40843705ce5bSJoe Perches						}
40850a920b5bSAndy Whitcroft					}
40860a920b5bSAndy Whitcroft
40871f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
40881f65f947SAndy Whitcroft				# terminating a case value or a label.
40891f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
40901f65f947SAndy Whitcroft					if ($ctx =~ /Wx./) {
40913705ce5bSJoe Perches						if (ERROR("SPACING",
40923705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
4093b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
40943705ce5bSJoe Perches							$line_fixed = 1;
40953705ce5bSJoe Perches						}
40961f65f947SAndy Whitcroft					}
40971f65f947SAndy Whitcroft
40980a920b5bSAndy Whitcroft				# All the others need spaces both sides.
4099cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
41001f65f947SAndy Whitcroft					my $ok = 0;
41011f65f947SAndy Whitcroft
410222f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
41031f65f947SAndy Whitcroft					if (($op eq '<' &&
41041f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
41051f65f947SAndy Whitcroft					    ($op eq '>' &&
41061f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
41071f65f947SAndy Whitcroft					{
41081f65f947SAndy Whitcroft					    	$ok = 1;
41091f65f947SAndy Whitcroft					}
41101f65f947SAndy Whitcroft
4111e0df7e1fSJoe Perches					# for asm volatile statements
4112e0df7e1fSJoe Perches					# ignore a colon with another
4113e0df7e1fSJoe Perches					# colon immediately before or after
4114e0df7e1fSJoe Perches					if (($op eq ':') &&
4115e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
4116e0df7e1fSJoe Perches						$ok = 1;
4117e0df7e1fSJoe Perches					}
4118e0df7e1fSJoe Perches
411984731623SJoe Perches					# messages are ERROR, but ?: are CHK
41201f65f947SAndy Whitcroft					if ($ok == 0) {
412184731623SJoe Perches						my $msg_type = \&ERROR;
412284731623SJoe Perches						$msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
412384731623SJoe Perches
412484731623SJoe Perches						if (&{$msg_type}("SPACING",
41253705ce5bSJoe Perches								 "spaces required around that '$op' $at\n" . $hereptr)) {
4126b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4127b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
4128b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
4129b34c648bSJoe Perches							}
41303705ce5bSJoe Perches							$line_fixed = 1;
41313705ce5bSJoe Perches						}
41320a920b5bSAndy Whitcroft					}
413322f2a2efSAndy Whitcroft				}
41344a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
41353705ce5bSJoe Perches
41363705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
41373705ce5bSJoe Perches
41383705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
41390a920b5bSAndy Whitcroft			}
41403705ce5bSJoe Perches
41413705ce5bSJoe Perches			if (($#elements % 2) == 0) {
41423705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
41433705ce5bSJoe Perches			}
41443705ce5bSJoe Perches
4145194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4146194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
41473705ce5bSJoe Perches			}
41483705ce5bSJoe Perches
41493705ce5bSJoe Perches
41500a920b5bSAndy Whitcroft		}
41510a920b5bSAndy Whitcroft
4152786b6326SJoe Perches# check for whitespace before a non-naked semicolon
4153d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
4154786b6326SJoe Perches			if (WARN("SPACING",
4155786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
4156786b6326SJoe Perches			    $fix) {
4157194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
4158786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
4159786b6326SJoe Perches			}
4160786b6326SJoe Perches		}
4161786b6326SJoe Perches
4162f0a594c1SAndy Whitcroft# check for multiple assignments
4163f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
4164000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
4165000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
4166f0a594c1SAndy Whitcroft		}
4167f0a594c1SAndy Whitcroft
416822f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
416922f2a2efSAndy Whitcroft## # continuation.
417022f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
417122f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
417222f2a2efSAndy Whitcroft##
417322f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
417422f2a2efSAndy Whitcroft## 			# falsly report the parameters of functions.
417522f2a2efSAndy Whitcroft## 			my $ln = $line;
417622f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
417722f2a2efSAndy Whitcroft## 			}
417822f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
4179000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
4180000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
418122f2a2efSAndy Whitcroft## 			}
418222f2a2efSAndy Whitcroft## 		}
4183f0a594c1SAndy Whitcroft
41840a920b5bSAndy Whitcroft#need space before brace following if, while, etc
41856b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
41864e5d56bdSEddie Kovsky		    $line =~ /do\{/) {
41873705ce5bSJoe Perches			if (ERROR("SPACING",
41883705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
41893705ce5bSJoe Perches			    $fix) {
4190194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
41913705ce5bSJoe Perches			}
4192de7d4f0eSAndy Whitcroft		}
4193de7d4f0eSAndy Whitcroft
4194c4a62ef9SJoe Perches## # check for blank lines before declarations
4195c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4196c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
4197c4a62ef9SJoe Perches##			WARN("SPACING",
4198c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
4199c4a62ef9SJoe Perches##		}
4200c4a62ef9SJoe Perches##
4201c4a62ef9SJoe Perches
4202de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
4203de7d4f0eSAndy Whitcroft# on the line
4204de7d4f0eSAndy Whitcroft		if ($line =~ /}(?!(?:,|;|\)))\S/) {
4205d5e616fcSJoe Perches			if (ERROR("SPACING",
4206d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
4207d5e616fcSJoe Perches			    $fix) {
4208194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4209d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
4210d5e616fcSJoe Perches			}
42110a920b5bSAndy Whitcroft		}
42120a920b5bSAndy Whitcroft
421322f2a2efSAndy Whitcroft# check spacing on square brackets
421422f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
42153705ce5bSJoe Perches			if (ERROR("SPACING",
42163705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
42173705ce5bSJoe Perches			    $fix) {
4218194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42193705ce5bSJoe Perches				    s/\[\s+/\[/;
42203705ce5bSJoe Perches			}
422122f2a2efSAndy Whitcroft		}
422222f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
42233705ce5bSJoe Perches			if (ERROR("SPACING",
42243705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
42253705ce5bSJoe Perches			    $fix) {
4226194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42273705ce5bSJoe Perches				    s/\s+\]/\]/;
42283705ce5bSJoe Perches			}
422922f2a2efSAndy Whitcroft		}
423022f2a2efSAndy Whitcroft
4231c45dcabdSAndy Whitcroft# check spacing on parentheses
42329c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
42339c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
42343705ce5bSJoe Perches			if (ERROR("SPACING",
42353705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
42363705ce5bSJoe Perches			    $fix) {
4237194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42383705ce5bSJoe Perches				    s/\(\s+/\(/;
42393705ce5bSJoe Perches			}
424022f2a2efSAndy Whitcroft		}
424113214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
4242c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
4243c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
42443705ce5bSJoe Perches			if (ERROR("SPACING",
42453705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
42463705ce5bSJoe Perches			    $fix) {
4247194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42483705ce5bSJoe Perches				    s/\s+\)/\)/;
42493705ce5bSJoe Perches			}
425022f2a2efSAndy Whitcroft		}
425122f2a2efSAndy Whitcroft
4252e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
4253e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4254e2826fd0SJoe Perches
4255e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
4256ea4acbb1SJoe Perches			my $var = $1;
4257ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4258ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
4259ea4acbb1SJoe Perches			    $fix) {
4260ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4261ea4acbb1SJoe Perches			}
4262ea4acbb1SJoe Perches		}
4263ea4acbb1SJoe Perches
4264ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
4265ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
4266ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
4267ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4268ea4acbb1SJoe Perches			my $var = $2;
4269ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
4270ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4271ea4acbb1SJoe Perches			    $fix) {
4272ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
4273ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
4274ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4275ea4acbb1SJoe Perches			}
4276e2826fd0SJoe Perches		}
4277e2826fd0SJoe Perches
42780a920b5bSAndy Whitcroft#goto labels aren't indented, allow a single space however
42794a0df2efSAndy Whitcroft		if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
42800a920b5bSAndy Whitcroft		   !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
42813705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
42823705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
42833705ce5bSJoe Perches			    $fix) {
4284194f66fcSJoe Perches				$fixed[$fixlinenr] =~
42853705ce5bSJoe Perches				    s/^(.)\s+/$1/;
42863705ce5bSJoe Perches			}
42870a920b5bSAndy Whitcroft		}
42880a920b5bSAndy Whitcroft
42895b9553abSJoe Perches# return is not a function
4290507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
4291c45dcabdSAndy Whitcroft			my $spacing = $1;
4292507e5141SJoe Perches			if ($^V && $^V ge 5.10.0 &&
42935b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
42945b9553abSJoe Perches				my $value = $1;
42955b9553abSJoe Perches				$value = deparenthesize($value);
42965b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4297000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
4298000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
42995b9553abSJoe Perches				}
4300c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
4301000d1cc1SJoe Perches				ERROR("SPACING",
4302000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
4303c45dcabdSAndy Whitcroft			}
4304c45dcabdSAndy Whitcroft		}
4305507e5141SJoe Perches
4306b43ae21bSJoe Perches# unnecessary return in a void function
4307b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
4308b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
4309b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
4310b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
4311b43ae21bSJoe Perches		    $linenr >= 3 &&
4312b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
4313b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
43149819cf25SJoe Perches			WARN("RETURN_VOID",
4315b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
43169819cf25SJoe Perches               }
43179819cf25SJoe Perches
4318189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
4319189248d8SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4320189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
4321189248d8SJoe Perches			my $openparens = $1;
4322189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
4323189248d8SJoe Perches			my $msg = "";
4324189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4325189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
4326189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
4327189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
4328189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
4329189248d8SJoe Perches			}
4330189248d8SJoe Perches		}
4331189248d8SJoe Perches
4332c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
4333c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
4334c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
4335c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4336c5595fa2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
4337c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4338c5595fa2SJoe Perches			my $lead = $1;
4339c5595fa2SJoe Perches			my $const = $2;
4340c5595fa2SJoe Perches			my $comp = $3;
4341c5595fa2SJoe Perches			my $to = $4;
4342c5595fa2SJoe Perches			my $newcomp = $comp;
4343f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
4344c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4345c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
4346c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4347c5595fa2SJoe Perches			    $fix) {
4348c5595fa2SJoe Perches				if ($comp eq "<") {
4349c5595fa2SJoe Perches					$newcomp = ">";
4350c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
4351c5595fa2SJoe Perches					$newcomp = ">=";
4352c5595fa2SJoe Perches				} elsif ($comp eq ">") {
4353c5595fa2SJoe Perches					$newcomp = "<";
4354c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
4355c5595fa2SJoe Perches					$newcomp = "<=";
4356c5595fa2SJoe Perches				}
4357c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4358c5595fa2SJoe Perches			}
4359c5595fa2SJoe Perches		}
4360c5595fa2SJoe Perches
4361f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
4362f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
436353a3c448SAndy Whitcroft			my $name = $1;
436453a3c448SAndy Whitcroft			if ($name ne 'EOF' && $name ne 'ERROR') {
4365000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
4366f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
436753a3c448SAndy Whitcroft			}
436853a3c448SAndy Whitcroft		}
4369c45dcabdSAndy Whitcroft
43700a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
43714a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
43723705ce5bSJoe Perches			if (ERROR("SPACING",
43733705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
43743705ce5bSJoe Perches			    $fix) {
4375194f66fcSJoe Perches				$fixed[$fixlinenr] =~
43763705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
43773705ce5bSJoe Perches			}
43780a920b5bSAndy Whitcroft		}
43790a920b5bSAndy Whitcroft
4380f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
4381f5fe35ddSAndy Whitcroft# statements after the conditional.
4382170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
43833e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
43843e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
43853e469cdcSAndy Whitcroft					if (!defined $stat);
4386170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
4387170d3a22SAndy Whitcroft						$remain_next, $off_next);
4388170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
4389170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
4390170d3a22SAndy Whitcroft
4391170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
4392170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
4393170d3a22SAndy Whitcroft				# then count those as offsets.
4394170d3a22SAndy Whitcroft				my ($whitespace) =
4395170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4396170d3a22SAndy Whitcroft				my $offset =
4397170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
4398170d3a22SAndy Whitcroft
4399170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
4400170d3a22SAndy Whitcroft								$offset} = 1;
4401170d3a22SAndy Whitcroft			}
4402170d3a22SAndy Whitcroft		}
4403170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
4404c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
4405170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
4406171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
44078905a67cSAndy Whitcroft
4408b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
4409000d1cc1SJoe Perches				ERROR("ASSIGN_IN_IF",
4410000d1cc1SJoe Perches				      "do not use assignment in if condition\n" . $herecurr);
44118905a67cSAndy Whitcroft			}
44128905a67cSAndy Whitcroft
44138905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
44148905a67cSAndy Whitcroft			# conditional.
4415773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
44168905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
441713214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
441853210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
441953210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
4420773647a0SAndy Whitcroft			{
4421bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
4422bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
4423bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
442442bdf74cSHidetoshi Seto				my $stat_real = '';
4425bb44ad39SAndy Whitcroft
442642bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
442742bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
4428bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
4429bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
4430bb44ad39SAndy Whitcroft				}
4431bb44ad39SAndy Whitcroft
4432000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4433000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
44348905a67cSAndy Whitcroft			}
44358905a67cSAndy Whitcroft		}
44368905a67cSAndy Whitcroft
443713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
443813214adfSAndy Whitcroft		if ($line =~ /
443913214adfSAndy Whitcroft			(?:
444013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
444113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
444213214adfSAndy Whitcroft				(?:\&\&|\|\|)
444313214adfSAndy Whitcroft			|
444413214adfSAndy Whitcroft				(?:\&\&|\|\|)
444513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
444613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
444713214adfSAndy Whitcroft			)/x)
444813214adfSAndy Whitcroft		{
4449000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
4450000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
445113214adfSAndy Whitcroft		}
445213214adfSAndy Whitcroft
44538905a67cSAndy Whitcroft# if and else should not have general statements after it
445413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
445513214adfSAndy Whitcroft			my $s = $1;
445613214adfSAndy Whitcroft			$s =~ s/$;//g; 	# Remove any comments
445713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
4458000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
4459000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
44600a920b5bSAndy Whitcroft			}
446113214adfSAndy Whitcroft		}
446239667782SAndy Whitcroft# if should not continue a brace
446339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
4464000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4465048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
446639667782SAndy Whitcroft				$herecurr);
446739667782SAndy Whitcroft		}
4468a1080bf8SAndy Whitcroft# case and default should not have general statements after them
4469a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4470a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
44713fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
4472a1080bf8SAndy Whitcroft			\s*return\s+
4473a1080bf8SAndy Whitcroft		    )/xg)
4474a1080bf8SAndy Whitcroft		{
4475000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
4476000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
4477a1080bf8SAndy Whitcroft		}
44780a920b5bSAndy Whitcroft
44790a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
44800a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
44818b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
44820a920b5bSAndy Whitcroft		    $previndent == $indent) {
44838b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
44848b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
44858b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
44868b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
44878b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
44888b8856f4SJoe Perches				my $fixedline = $prevrawline;
44898b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
44908b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
44918b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
44928b8856f4SJoe Perches				}
44938b8856f4SJoe Perches				$fixedline = $rawline;
44948b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
44958b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
44968b8856f4SJoe Perches			}
44970a920b5bSAndy Whitcroft		}
44980a920b5bSAndy Whitcroft
44998b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4500c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
4501c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4502c2fdda0dSAndy Whitcroft
4503c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
4504c2fdda0dSAndy Whitcroft			# conditional.
4505773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
4506c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
4507c2fdda0dSAndy Whitcroft
4508c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
45098b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
45108b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
45118b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
45128b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
45138b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
45148b8856f4SJoe Perches					my $fixedline = $prevrawline;
45158b8856f4SJoe Perches					my $trailing = $rawline;
45168b8856f4SJoe Perches					$trailing =~ s/^\+//;
45178b8856f4SJoe Perches					$trailing = trim($trailing);
45188b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
45198b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
45208b8856f4SJoe Perches				}
4521c2fdda0dSAndy Whitcroft			}
4522c2fdda0dSAndy Whitcroft		}
4523c2fdda0dSAndy Whitcroft
452495e2c602SJoe Perches#Specific variable tests
4525323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
4526323c1260SJoe Perches			my $var = $1;
452795e2c602SJoe Perches
452895e2c602SJoe Perches#gcc binary extension
452995e2c602SJoe Perches			if ($var =~ /^$Binary$/) {
4530d5e616fcSJoe Perches				if (WARN("GCC_BINARY_CONSTANT",
4531d5e616fcSJoe Perches					 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4532d5e616fcSJoe Perches				    $fix) {
4533d5e616fcSJoe Perches					my $hexval = sprintf("0x%x", oct($var));
4534194f66fcSJoe Perches					$fixed[$fixlinenr] =~
4535d5e616fcSJoe Perches					    s/\b$var\b/$hexval/;
4536d5e616fcSJoe Perches				}
453795e2c602SJoe Perches			}
453895e2c602SJoe Perches
453995e2c602SJoe Perches#CamelCase
4540807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
4541be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
454222735ce8SJoe Perches#Ignore Page<foo> variants
4543807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
454422735ce8SJoe Perches#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
4545f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4546f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
4547f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
45487e781f67SJoe Perches				while ($var =~ m{($Ident)}g) {
45497e781f67SJoe Perches					my $word = $1;
45507e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
4551d8b07710SJoe Perches					if ($check) {
4552d8b07710SJoe Perches						seed_camelcase_includes();
4553d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
4554d8b07710SJoe Perches							seed_camelcase_file($realfile);
4555d8b07710SJoe Perches							$camelcase_file_seeded = 1;
4556d8b07710SJoe Perches						}
4557d8b07710SJoe Perches					}
45587e781f67SJoe Perches					if (!defined $camelcase{$word}) {
45597e781f67SJoe Perches						$camelcase{$word} = 1;
4560be79794bSJoe Perches						CHK("CAMELCASE",
45617e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
45627e781f67SJoe Perches					}
4563323c1260SJoe Perches				}
4564323c1260SJoe Perches			}
45653445686aSJoe Perches		}
45660a920b5bSAndy Whitcroft
45670a920b5bSAndy Whitcroft#no spaces allowed after \ in define
4568d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
4569d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4570d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4571d5e616fcSJoe Perches			    $fix) {
4572194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
4573d5e616fcSJoe Perches			}
45740a920b5bSAndy Whitcroft		}
45750a920b5bSAndy Whitcroft
45760e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
45770e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
4578c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
4579e09dec48SAndy Whitcroft			my $file = "$1.h";
4580e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
4581e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
4582e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
45837840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
4584c45dcabdSAndy Whitcroft			{
45850e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
45860e212e0aSFabian Frederick				if ($asminclude > 0) {
4587e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
4588000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
4589000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4590e09dec48SAndy Whitcroft					} else {
4591000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
4592000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4593e09dec48SAndy Whitcroft					}
45940a920b5bSAndy Whitcroft				}
45950a920b5bSAndy Whitcroft			}
45960e212e0aSFabian Frederick		}
45970a920b5bSAndy Whitcroft
4598653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
4599653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
4600cf655043SAndy Whitcroft# in a known good container
4601b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
4602b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
4603d8aaf121SAndy Whitcroft			my $ln = $linenr;
4604d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
4605c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
4606c45dcabdSAndy Whitcroft			my $ctx = '';
460708a2843eSJoe Perches			my $has_flow_statement = 0;
460808a2843eSJoe Perches			my $has_arg_concat = 0;
4609c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
4610f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4611f74bd194SAndy Whitcroft			$ctx = $dstat;
4612c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
4613a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
4614c45dcabdSAndy Whitcroft
461508a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
461662e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
461708a2843eSJoe Perches
4618f74bd194SAndy Whitcroft			$dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
4619292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
4620c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
4621c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
4622c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
4623c45dcabdSAndy Whitcroft
4624c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
4625bf30d6edSAndy Whitcroft			while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4626bf30d6edSAndy Whitcroft			       $dstat =~ s/\{[^\{\}]*\}/1/ ||
46276b10df42SVladimir Zapolskiy			       $dstat =~ s/.\[[^\[\]]*\]/1/)
4628bf30d6edSAndy Whitcroft			{
4629c45dcabdSAndy Whitcroft			}
4630c45dcabdSAndy Whitcroft
4631e45bab8eSAndy Whitcroft			# Flatten any obvious string concatentation.
463233acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
463333acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
4634e45bab8eSAndy Whitcroft			{
4635e45bab8eSAndy Whitcroft			}
4636e45bab8eSAndy Whitcroft
463742e15293SJoe Perches			# Make asm volatile uses seem like a generic function
463842e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
463942e15293SJoe Perches
4640c45dcabdSAndy Whitcroft			my $exceptions = qr{
4641c45dcabdSAndy Whitcroft				$Declare|
4642c45dcabdSAndy Whitcroft				module_param_named|
4643a0a0a7a9SKees Cook				MODULE_PARM_DESC|
4644c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
4645c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
4646383099fdSAndy Whitcroft				__typeof__\(|
464722fd2d3eSStefani Seibold				union|
464822fd2d3eSStefani Seibold				struct|
4649ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
46506b10df42SVladimir Zapolskiy				^\"|\"$|
46516b10df42SVladimir Zapolskiy				^\[
4652c45dcabdSAndy Whitcroft			}x;
46535eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
4654f74bd194SAndy Whitcroft			if ($dstat ne '' &&
4655f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
4656f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
46573cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
4658356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
4659f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
4660f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
4661e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
466272f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
4663f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
4664f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
4665f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
46664e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
4667f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
4668c45dcabdSAndy Whitcroft			{
4669f74bd194SAndy Whitcroft				$ctx =~ s/\n*$//;
4670f74bd194SAndy Whitcroft				my $herectx = $here . "\n";
4671f74bd194SAndy Whitcroft				my $cnt = statement_rawlines($ctx);
4672f74bd194SAndy Whitcroft
4673f74bd194SAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
4674f74bd194SAndy Whitcroft					$herectx .= raw_line($linenr, $n) . "\n";
4675c45dcabdSAndy Whitcroft				}
4676c45dcabdSAndy Whitcroft
4677f74bd194SAndy Whitcroft				if ($dstat =~ /;/) {
4678f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4679f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4680f74bd194SAndy Whitcroft				} else {
4681000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
4682388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
4683d8aaf121SAndy Whitcroft				}
46840a920b5bSAndy Whitcroft			}
46855023d347SJoe Perches
468608a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
468708a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
468808a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
468908a2843eSJoe Perches				my $herectx = $here . "\n";
469008a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
469108a2843eSJoe Perches
469208a2843eSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
469308a2843eSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
469408a2843eSJoe Perches				}
469508a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
469608a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
469708a2843eSJoe Perches			}
469808a2843eSJoe Perches
4699481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
47005023d347SJoe Perches
47015023d347SJoe Perches		} else {
47025023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
4703481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
4704481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
47055023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
47065023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
47075023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
47085023d347SJoe Perches			}
4709653d4876SAndy Whitcroft		}
47100a920b5bSAndy Whitcroft
4711b13edf7fSJoe Perches# do {} while (0) macro tests:
4712b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
4713b13edf7fSJoe Perches# macro should not end with a semicolon
4714b13edf7fSJoe Perches		if ($^V && $^V ge 5.10.0 &&
4715b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
4716b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4717b13edf7fSJoe Perches			my $ln = $linenr;
4718b13edf7fSJoe Perches			my $cnt = $realcnt;
4719b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
4720b13edf7fSJoe Perches			my $ctx = '';
4721b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
4722b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
4723b13edf7fSJoe Perches			$ctx = $dstat;
4724b13edf7fSJoe Perches
4725b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
47261b36b201SJoe Perches			$dstat =~ s/$;/ /g;
4727b13edf7fSJoe Perches
4728b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4729b13edf7fSJoe Perches				my $stmts = $2;
4730b13edf7fSJoe Perches				my $semis = $3;
4731b13edf7fSJoe Perches
4732b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
4733b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
4734b13edf7fSJoe Perches				my $herectx = $here . "\n";
4735b13edf7fSJoe Perches
4736b13edf7fSJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4737b13edf7fSJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4738b13edf7fSJoe Perches				}
4739b13edf7fSJoe Perches
4740ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
4741ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
4742b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4743b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4744b13edf7fSJoe Perches				}
4745b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
4746b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4747b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4748b13edf7fSJoe Perches				}
4749f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4750f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
4751f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
4752f5ef95b1SJoe Perches				my $herectx = $here . "\n";
4753f5ef95b1SJoe Perches
4754f5ef95b1SJoe Perches				for (my $n = 0; $n < $cnt; $n++) {
4755f5ef95b1SJoe Perches					$herectx .= raw_line($linenr, $n) . "\n";
4756f5ef95b1SJoe Perches				}
4757f5ef95b1SJoe Perches
4758f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
4759f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
4760b13edf7fSJoe Perches			}
4761b13edf7fSJoe Perches		}
4762b13edf7fSJoe Perches
4763080ba929SMike Frysinger# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4764080ba929SMike Frysinger# all assignments may have only one of the following with an assignment:
4765080ba929SMike Frysinger#	.
4766080ba929SMike Frysinger#	ALIGN(...)
4767080ba929SMike Frysinger#	VMLINUX_SYMBOL(...)
4768080ba929SMike Frysinger		if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
4769000d1cc1SJoe Perches			WARN("MISSING_VMLINUX_SYMBOL",
4770000d1cc1SJoe Perches			     "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
4771080ba929SMike Frysinger		}
4772080ba929SMike Frysinger
4773f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
477413214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
477513214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
4776cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
477713214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
4778cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4779cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
4780aad4f614SJoe Perches				my @allowed = ();
4781aad4f614SJoe Perches				my $allow = 0;
478213214adfSAndy Whitcroft				my $seen = 0;
4783773647a0SAndy Whitcroft				my $herectx = $here . "\n";
4784cf655043SAndy Whitcroft				my $ln = $linenr - 1;
478513214adfSAndy Whitcroft				for my $chunk (@chunks) {
478613214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
478713214adfSAndy Whitcroft
4788773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
4789773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4790773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
4791773647a0SAndy Whitcroft
4792aad4f614SJoe Perches					$allowed[$allow] = 0;
4793773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4794773647a0SAndy Whitcroft
4795773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
4796773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
4797773647a0SAndy Whitcroft
4798773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
4799cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
4800cf655043SAndy Whitcroft
4801773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
480213214adfSAndy Whitcroft
480313214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
480413214adfSAndy Whitcroft
4805aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
4806cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
4807cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
4808aad4f614SJoe Perches						$allowed[$allow] = 1;
480913214adfSAndy Whitcroft					}
481013214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
4811cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
4812aad4f614SJoe Perches						$allowed[$allow] = 1;
481313214adfSAndy Whitcroft					}
4814cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
4815cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
4816aad4f614SJoe Perches						$allowed[$allow] = 1;
481713214adfSAndy Whitcroft					}
4818aad4f614SJoe Perches					$allow++;
481913214adfSAndy Whitcroft				}
4820aad4f614SJoe Perches				if ($seen) {
4821aad4f614SJoe Perches					my $sum_allowed = 0;
4822aad4f614SJoe Perches					foreach (@allowed) {
4823aad4f614SJoe Perches						$sum_allowed += $_;
4824aad4f614SJoe Perches					}
4825aad4f614SJoe Perches					if ($sum_allowed == 0) {
4826000d1cc1SJoe Perches						WARN("BRACES",
4827000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
4828aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
4829aad4f614SJoe Perches						 $seen != $allow) {
4830aad4f614SJoe Perches						CHK("BRACES",
4831aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
4832aad4f614SJoe Perches					}
483313214adfSAndy Whitcroft				}
483413214adfSAndy Whitcroft			}
483513214adfSAndy Whitcroft		}
4836773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
483713214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
4838cf655043SAndy Whitcroft			my $allowed = 0;
4839f0a594c1SAndy Whitcroft
4840cf655043SAndy Whitcroft			# Check the pre-context.
4841cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4842cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
4843cf655043SAndy Whitcroft				$allowed = 1;
4844f0a594c1SAndy Whitcroft			}
4845773647a0SAndy Whitcroft
4846773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
4847773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
4848773647a0SAndy Whitcroft
4849cf655043SAndy Whitcroft			# Check the condition.
4850cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
4851773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
4852cf655043SAndy Whitcroft			if (defined $cond) {
4853773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
4854cf655043SAndy Whitcroft			}
4855cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
4856cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
4857cf655043SAndy Whitcroft				$allowed = 1;
4858cf655043SAndy Whitcroft			}
4859cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
4860cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
4861cf655043SAndy Whitcroft				$allowed = 1;
4862cf655043SAndy Whitcroft			}
4863cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
4864cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
4865cf655043SAndy Whitcroft				$allowed = 1;
4866cf655043SAndy Whitcroft			}
4867cf655043SAndy Whitcroft			# Check the post-context.
4868cf655043SAndy Whitcroft			if (defined $chunks[1]) {
4869cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
4870cf655043SAndy Whitcroft				if (defined $cond) {
4871773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
4872cf655043SAndy Whitcroft				}
4873cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
4874cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
4875cf655043SAndy Whitcroft					$allowed = 1;
4876cf655043SAndy Whitcroft				}
4877cf655043SAndy Whitcroft			}
4878cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
487969932487SJustin P. Mattock				my $herectx = $here . "\n";
4880f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
4881cf655043SAndy Whitcroft
4882f055663cSAndy Whitcroft				for (my $n = 0; $n < $cnt; $n++) {
488369932487SJustin P. Mattock					$herectx .= raw_line($linenr, $n) . "\n";
4884cf655043SAndy Whitcroft				}
4885cf655043SAndy Whitcroft
4886000d1cc1SJoe Perches				WARN("BRACES",
4887000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
4888f0a594c1SAndy Whitcroft			}
4889f0a594c1SAndy Whitcroft		}
4890f0a594c1SAndy Whitcroft
48910979ae66SJoe Perches# check for unnecessary blank lines around braces
489277b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
4893f8e58219SJoe Perches			if (CHK("BRACES",
4894f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
4895f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
4896f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4897f8e58219SJoe Perches			}
48980979ae66SJoe Perches		}
489977b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
4900f8e58219SJoe Perches			if (CHK("BRACES",
4901f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
4902f8e58219SJoe Perches			    $fix) {
4903f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4904f8e58219SJoe Perches			}
49050979ae66SJoe Perches		}
49060979ae66SJoe Perches
49074a0df2efSAndy Whitcroft# no volatiles please
49086c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
49096c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
4910000d1cc1SJoe Perches			WARN("VOLATILE",
4911000d1cc1SJoe Perches			     "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
49124a0df2efSAndy Whitcroft		}
49134a0df2efSAndy Whitcroft
49145e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
49155e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
49165e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
49175e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
491833acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
49195e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
49205e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
49215e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
49225e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
49235e4f6ba5SJoe Perches				     $fix &&
49245e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
49255e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
49265e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
49275e4f6ba5SJoe Perches				my $comma_close = "";
49285e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
49295e4f6ba5SJoe Perches					$comma_close = $1;
49305e4f6ba5SJoe Perches				}
49315e4f6ba5SJoe Perches
49325e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
49335e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49345e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
49355e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
49365e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
49375e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
49385e4f6ba5SJoe Perches				$fixedline = $rawline;
49395e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
49405e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
49415e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49425e4f6ba5SJoe Perches				}
49435e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
49445e4f6ba5SJoe Perches			}
49455e4f6ba5SJoe Perches		}
49465e4f6ba5SJoe Perches
49475e4f6ba5SJoe Perches# check for missing a space in a string concatenation
49485e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
49495e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
49505e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
49515e4f6ba5SJoe Perches		}
49525e4f6ba5SJoe Perches
49535e4f6ba5SJoe Perches# check for spaces before a quoted newline
49545e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
49555e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
49565e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
49575e4f6ba5SJoe Perches			    $fix) {
49585e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
49595e4f6ba5SJoe Perches			}
49605e4f6ba5SJoe Perches
49615e4f6ba5SJoe Perches		}
49625e4f6ba5SJoe Perches
4963f17dba4fSJoe Perches# concatenated string without spaces between elements
496433acb54aSJoe Perches		if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
4965f17dba4fSJoe Perches			CHK("CONCATENATED_STRING",
4966f17dba4fSJoe Perches			    "Concatenated strings should use spaces between elements\n" . $herecurr);
4967f17dba4fSJoe Perches		}
4968f17dba4fSJoe Perches
496990ad30e5SJoe Perches# uncoalesced string fragments
497033acb54aSJoe Perches		if ($line =~ /$String\s*"/) {
497190ad30e5SJoe Perches			WARN("STRING_FRAGMENTS",
497290ad30e5SJoe Perches			     "Consecutive strings are generally better as a single string\n" . $herecurr);
497390ad30e5SJoe Perches		}
497490ad30e5SJoe Perches
49756e300757SJoe Perches# check for %L{u,d,i} and 0x%[udi] in strings
49765e4f6ba5SJoe Perches		my $string;
49775e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
49785e4f6ba5SJoe Perches			$string = substr($rawline, $-[1], $+[1] - $-[1]);
49795e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
49806e300757SJoe Perches			if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
49815e4f6ba5SJoe Perches				WARN("PRINTF_L",
49825e4f6ba5SJoe Perches				     "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
49835e4f6ba5SJoe Perches				last;
49845e4f6ba5SJoe Perches			}
49856e300757SJoe Perches			if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
49866e300757SJoe Perches				ERROR("PRINTF_0xDECIMAL",
49876e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
49886e300757SJoe Perches			}
49895e4f6ba5SJoe Perches		}
49905e4f6ba5SJoe Perches
49915e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
49925e4f6ba5SJoe Perches		if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
49935e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
49945e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
49955e4f6ba5SJoe Perches		}
49965e4f6ba5SJoe Perches
499700df344fSAndy Whitcroft# warn about #if 0
4998c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
4999000d1cc1SJoe Perches			CHK("REDUNDANT_CODE",
5000000d1cc1SJoe Perches			    "if this code is redundant consider removing it\n" .
5001de7d4f0eSAndy Whitcroft				$herecurr);
50024a0df2efSAndy Whitcroft		}
50034a0df2efSAndy Whitcroft
500403df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
500503df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
5006100425deSJoe Perches			my $tested = quotemeta($1);
5007100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5008100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5009100425deSJoe Perches				my $func = $1;
5010100425deSJoe Perches				if (WARN('NEEDLESS_IF',
5011100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5012100425deSJoe Perches				    $fix) {
5013100425deSJoe Perches					my $do_fix = 1;
5014100425deSJoe Perches					my $leading_tabs = "";
5015100425deSJoe Perches					my $new_leading_tabs = "";
5016100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5017100425deSJoe Perches						$leading_tabs = $1;
5018100425deSJoe Perches					} else {
5019100425deSJoe Perches						$do_fix = 0;
5020100425deSJoe Perches					}
5021100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5022100425deSJoe Perches						$new_leading_tabs = $1;
5023100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5024100425deSJoe Perches							$do_fix = 0;
5025100425deSJoe Perches						}
5026100425deSJoe Perches					} else {
5027100425deSJoe Perches						$do_fix = 0;
5028100425deSJoe Perches					}
5029100425deSJoe Perches					if ($do_fix) {
5030100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
5031100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5032100425deSJoe Perches					}
5033100425deSJoe Perches				}
50344c432a8fSGreg Kroah-Hartman			}
50354c432a8fSGreg Kroah-Hartman		}
5036f0a594c1SAndy Whitcroft
5037ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
5038ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5039ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5040ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
5041ebfdc409SJoe Perches		    $linenr > 3) {
5042ebfdc409SJoe Perches			my $testval = $2;
5043ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
5044ebfdc409SJoe Perches
5045ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5046ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5047ebfdc409SJoe Perches
5048ebfdc409SJoe 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)/) {
5049ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
5050ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
5051ebfdc409SJoe Perches			}
5052ebfdc409SJoe Perches		}
5053ebfdc409SJoe Perches
5054f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
5055dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
5056f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5057f78d98f6SJoe Perches			my $level = $1;
5058f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
5059f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
5060f78d98f6SJoe Perches			    $fix) {
5061f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
5062f78d98f6SJoe Perches			}
5063f78d98f6SJoe Perches		}
5064f78d98f6SJoe Perches
5065abb08a53SJoe Perches# check for mask then right shift without a parentheses
5066abb08a53SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5067abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5068abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5069abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
5070abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5071abb08a53SJoe Perches		}
5072abb08a53SJoe Perches
5073b75ac618SJoe Perches# check for pointer comparisons to NULL
5074b75ac618SJoe Perches		if ($^V && $^V ge 5.10.0) {
5075b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5076b75ac618SJoe Perches				my $val = $1;
5077b75ac618SJoe Perches				my $equal = "!";
5078b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
5079b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
5080b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5081b75ac618SJoe Perches					    $fix) {
5082b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5083b75ac618SJoe Perches				}
5084b75ac618SJoe Perches			}
5085b75ac618SJoe Perches		}
5086b75ac618SJoe Perches
50878716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
50888716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
50898716de38SJoe Perches			my $attr = $1;
50908716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
50918716de38SJoe Perches				my $ptr = $1;
50928716de38SJoe Perches				my $var = $2;
50938716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
50948716de38SJoe Perches				      ERROR("MISPLACED_INIT",
50958716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
50968716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
50978716de38SJoe Perches				      WARN("MISPLACED_INIT",
50988716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
50998716de38SJoe Perches				    $fix) {
5100194f66fcSJoe 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;
51018716de38SJoe Perches				}
51028716de38SJoe Perches			}
51038716de38SJoe Perches		}
51048716de38SJoe Perches
5105e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
5106e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5107e970b884SJoe Perches			my $attr = $1;
5108e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
5109e970b884SJoe Perches			my $attr_prefix = $1;
5110e970b884SJoe Perches			my $attr_type = $2;
5111e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5112e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5113e970b884SJoe Perches			    $fix) {
5114194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5115e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
5116e970b884SJoe Perches			}
5117e970b884SJoe Perches		}
5118e970b884SJoe Perches
5119e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
5120e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5121e970b884SJoe Perches			my $attr = $1;
5122e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
5123e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
5124e970b884SJoe Perches			    $fix) {
5125194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
5126e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
5127e970b884SJoe Perches				$lead = rtrim($1);
5128e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
5129e970b884SJoe Perches				$lead = "${lead}const ";
5130194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
5131e970b884SJoe Perches			}
5132e970b884SJoe Perches		}
5133e970b884SJoe Perches
5134c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
5135c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
5136c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5137c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
5138c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5139c17893c7SJoe Perches			    $fix) {
5140c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5141c17893c7SJoe Perches			}
5142c17893c7SJoe Perches		}
5143c17893c7SJoe Perches
5144fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
5145fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
5146fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5147fbdb8138SJoe Perches			my $constant_func = $1;
5148fbdb8138SJoe Perches			my $func = $constant_func;
5149fbdb8138SJoe Perches			$func =~ s/^__constant_//;
5150fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
5151fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
5152fbdb8138SJoe Perches			    $fix) {
5153194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
5154fbdb8138SJoe Perches			}
5155fbdb8138SJoe Perches		}
5156fbdb8138SJoe Perches
51571a15a250SPatrick Pannuto# prefer usleep_range over udelay
515837581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
515943c1d77cSJoe Perches			my $delay = $1;
51601a15a250SPatrick Pannuto			# ignore udelay's < 10, however
516143c1d77cSJoe Perches			if (! ($delay < 10) ) {
5162000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
516343c1d77cSJoe Perches				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
516443c1d77cSJoe Perches			}
516543c1d77cSJoe Perches			if ($delay > 2000) {
516643c1d77cSJoe Perches				WARN("LONG_UDELAY",
516743c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
51681a15a250SPatrick Pannuto			}
51691a15a250SPatrick Pannuto		}
51701a15a250SPatrick Pannuto
517109ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
517209ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
517309ef8725SPatrick Pannuto			if ($1 < 20) {
5174000d1cc1SJoe Perches				WARN("MSLEEP",
517543c1d77cSJoe Perches				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
517609ef8725SPatrick Pannuto			}
517709ef8725SPatrick Pannuto		}
517809ef8725SPatrick Pannuto
517936ec1939SJoe Perches# check for comparisons of jiffies
518036ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
518136ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
518236ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
518336ec1939SJoe Perches		}
518436ec1939SJoe Perches
51859d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
51869d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
51879d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
51889d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
51899d7a34a5SJoe Perches		}
51909d7a34a5SJoe Perches
519100df344fSAndy Whitcroft# warn about #ifdefs in C files
5192c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
519300df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
519400df344fSAndy Whitcroft#			print "$herecurr";
519500df344fSAndy Whitcroft#			$clean = 0;
519600df344fSAndy Whitcroft#		}
519700df344fSAndy Whitcroft
519822f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
5199c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
52003705ce5bSJoe Perches			if (ERROR("SPACING",
52013705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
52023705ce5bSJoe Perches			    $fix) {
5203194f66fcSJoe Perches				$fixed[$fixlinenr] =~
52043705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
52053705ce5bSJoe Perches			}
52063705ce5bSJoe Perches
520722f2a2efSAndy Whitcroft		}
520822f2a2efSAndy Whitcroft
52094a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
5210171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5211171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
52124a0df2efSAndy Whitcroft			my $which = $1;
52134a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5214000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
5215000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
52164a0df2efSAndy Whitcroft			}
52174a0df2efSAndy Whitcroft		}
52184a0df2efSAndy Whitcroft# check for memory barriers without a comment.
5219402c2553SMichael S. Tsirkin
5220402c2553SMichael S. Tsirkin		my $barriers = qr{
5221402c2553SMichael S. Tsirkin			mb|
5222402c2553SMichael S. Tsirkin			rmb|
5223402c2553SMichael S. Tsirkin			wmb|
5224402c2553SMichael S. Tsirkin			read_barrier_depends
5225402c2553SMichael S. Tsirkin		}x;
5226402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
5227402c2553SMichael S. Tsirkin			mb__before_atomic|
5228402c2553SMichael S. Tsirkin			mb__after_atomic|
5229402c2553SMichael S. Tsirkin			store_release|
5230402c2553SMichael S. Tsirkin			load_acquire|
5231402c2553SMichael S. Tsirkin			store_mb|
5232402c2553SMichael S. Tsirkin			(?:$barriers)
5233402c2553SMichael S. Tsirkin		}x;
5234402c2553SMichael S. Tsirkin		my $all_barriers = qr{
5235402c2553SMichael S. Tsirkin			(?:$barriers)|
523643e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
523743e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
5238402c2553SMichael S. Tsirkin		}x;
5239402c2553SMichael S. Tsirkin
5240402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
52414a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
5242c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
5243000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
52444a0df2efSAndy Whitcroft			}
52454a0df2efSAndy Whitcroft		}
52463ad81779SPaul E. McKenney
5247f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5248f4073b0fSMichael S. Tsirkin
5249f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
5250f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
5251f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5252f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5253f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
5254f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5255f4073b0fSMichael S. Tsirkin		}
5256f4073b0fSMichael S. Tsirkin
5257cb426e99SJoe Perches# check for waitqueue_active without a comment.
5258cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
5259cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
5260cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
5261cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
5262cb426e99SJoe Perches			}
5263cb426e99SJoe Perches		}
52643ad81779SPaul E. McKenney
52653ad81779SPaul E. McKenney# Check for expedited grace periods that interrupt non-idle non-nohz
52663ad81779SPaul E. McKenney# online CPUs.  These expedited can therefore degrade real-time response
52673ad81779SPaul E. McKenney# if used carelessly, and should be avoided where not absolutely
52683ad81779SPaul E. McKenney# needed.  It is always OK to use synchronize_rcu_expedited() and
52693ad81779SPaul E. McKenney# synchronize_sched_expedited() at boot time (before real-time applications
52703ad81779SPaul E. McKenney# start) and in error situations where real-time response is compromised in
52713ad81779SPaul E. McKenney# any case.  Note that synchronize_srcu_expedited() does -not- interrupt
52723ad81779SPaul E. McKenney# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
52733ad81779SPaul E. McKenney# Of course, nothing comes for free, and srcu_read_lock() and
52743ad81779SPaul E. McKenney# srcu_read_unlock() do contain full memory barriers in payment for
52753ad81779SPaul E. McKenney# synchronize_srcu_expedited() non-interruption properties.
52763ad81779SPaul E. McKenney		if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
52773ad81779SPaul E. McKenney			WARN("EXPEDITED_RCU_GRACE_PERIOD",
52783ad81779SPaul E. McKenney			     "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
52793ad81779SPaul E. McKenney
52803ad81779SPaul E. McKenney		}
52813ad81779SPaul E. McKenney
52824a0df2efSAndy Whitcroft# check of hardware specific defines
5283c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
5284000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
5285000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
52860a920b5bSAndy Whitcroft		}
5287653d4876SAndy Whitcroft
5288d4977c78STobias Klauser# Check that the storage class is at the beginning of a declaration
5289d4977c78STobias Klauser		if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
5290000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
5291000d1cc1SJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr)
5292d4977c78STobias Klauser		}
5293d4977c78STobias Klauser
5294de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
5295de7d4f0eSAndy Whitcroft# storage class and type.
52969c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
52979c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
5298000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
5299000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
5300de7d4f0eSAndy Whitcroft		}
5301de7d4f0eSAndy Whitcroft
53028905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
53032b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53042b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
5305d5e616fcSJoe Perches			if (WARN("INLINE",
5306d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
5307d5e616fcSJoe Perches			    $fix) {
5308194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
5309d5e616fcSJoe Perches
5310d5e616fcSJoe Perches			}
53118905a67cSAndy Whitcroft		}
53128905a67cSAndy Whitcroft
53133d130fd0SJoe Perches# Check for __attribute__ packed, prefer __packed
53142b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53152b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
5316000d1cc1SJoe Perches			WARN("PREFER_PACKED",
5317000d1cc1SJoe Perches			     "__packed is preferred over __attribute__((packed))\n" . $herecurr);
53183d130fd0SJoe Perches		}
53193d130fd0SJoe Perches
532039b7e287SJoe Perches# Check for __attribute__ aligned, prefer __aligned
53212b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53222b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
5323000d1cc1SJoe Perches			WARN("PREFER_ALIGNED",
5324000d1cc1SJoe Perches			     "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
532539b7e287SJoe Perches		}
532639b7e287SJoe Perches
53275f14d3bdSJoe Perches# Check for __attribute__ format(printf, prefer __printf
53282b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53292b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
5330d5e616fcSJoe Perches			if (WARN("PREFER_PRINTF",
5331d5e616fcSJoe Perches				 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5332d5e616fcSJoe Perches			    $fix) {
5333194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
5334d5e616fcSJoe Perches
5335d5e616fcSJoe Perches			}
53365f14d3bdSJoe Perches		}
53375f14d3bdSJoe Perches
53386061d949SJoe Perches# Check for __attribute__ format(scanf, prefer __scanf
53392b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
53402b7ab453SJoe Perches		    $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
5341d5e616fcSJoe Perches			if (WARN("PREFER_SCANF",
5342d5e616fcSJoe Perches				 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5343d5e616fcSJoe Perches			    $fix) {
5344194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
5345d5e616fcSJoe Perches			}
53466061d949SJoe Perches		}
53476061d949SJoe Perches
5348619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
5349619a908aSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5350619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5351619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5352619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
5353619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
5354619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
5355619a908aSJoe Perches		}
5356619a908aSJoe Perches
5357e6176fa4SJoe Perches# check for c99 types like uint8_t used outside of uapi/
5358e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
5359e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5360e6176fa4SJoe Perches			my $type = $1;
5361e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
5362e6176fa4SJoe Perches				$type = $1;
5363e6176fa4SJoe Perches				my $kernel_type = 'u';
5364e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
5365e6176fa4SJoe Perches				$type =~ /(\d+)/;
5366e6176fa4SJoe Perches				$kernel_type .= $1;
5367e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
5368e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5369e6176fa4SJoe Perches				    $fix) {
5370e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5371e6176fa4SJoe Perches				}
5372e6176fa4SJoe Perches			}
5373e6176fa4SJoe Perches		}
5374e6176fa4SJoe Perches
5375938224b5SJoe Perches# check for cast of C90 native int or longer types constants
5376938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5377938224b5SJoe Perches			my $cast = $1;
5378938224b5SJoe Perches			my $const = $2;
5379938224b5SJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
5380938224b5SJoe Perches				 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5381938224b5SJoe Perches			    $fix) {
5382938224b5SJoe Perches				my $suffix = "";
5383938224b5SJoe Perches				my $newconst = $const;
5384938224b5SJoe Perches				$newconst =~ s/${Int_type}$//;
5385938224b5SJoe Perches				$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5386938224b5SJoe Perches				if ($cast =~ /\blong\s+long\b/) {
5387938224b5SJoe Perches					$suffix .= 'LL';
5388938224b5SJoe Perches				} elsif ($cast =~ /\blong\b/) {
5389938224b5SJoe Perches					$suffix .= 'L';
5390938224b5SJoe Perches				}
5391938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5392938224b5SJoe Perches			}
5393938224b5SJoe Perches		}
5394938224b5SJoe Perches
53958f53a9b8SJoe Perches# check for sizeof(&)
53968f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
5397000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
5398000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
53998f53a9b8SJoe Perches		}
54008f53a9b8SJoe Perches
540166c80b60SJoe Perches# check for sizeof without parenthesis
540266c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
5403d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
5404d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5405d5e616fcSJoe Perches			    $fix) {
5406194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
5407d5e616fcSJoe Perches			}
540866c80b60SJoe Perches		}
540966c80b60SJoe Perches
541088982feaSJoe Perches# check for struct spinlock declarations
541188982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
541288982feaSJoe Perches			WARN("USE_SPINLOCK_T",
541388982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
541488982feaSJoe Perches		}
541588982feaSJoe Perches
5416a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
541706668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
5418a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
5419caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
5420caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
5421d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
5422d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5423d5e616fcSJoe Perches				    $fix) {
5424194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
5425d5e616fcSJoe Perches				}
5426a6962d72SJoe Perches			}
5427a6962d72SJoe Perches		}
5428a6962d72SJoe Perches
5429554e165cSAndy Whitcroft# Check for misused memsets
5430d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5431d1fe9c09SJoe Perches		    defined $stat &&
54329e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
5433554e165cSAndy Whitcroft
5434d7c76ba7SJoe Perches			my $ms_addr = $2;
5435d1fe9c09SJoe Perches			my $ms_val = $7;
5436d1fe9c09SJoe Perches			my $ms_size = $12;
5437d7c76ba7SJoe Perches
5438554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
5439554e165cSAndy Whitcroft				ERROR("MEMSET",
5440d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
5441554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
5442554e165cSAndy Whitcroft				WARN("MEMSET",
5443d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5444d7c76ba7SJoe Perches			}
5445d7c76ba7SJoe Perches		}
5446d7c76ba7SJoe Perches
544798a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
544898a9bba5SJoe Perches		if ($^V && $^V ge 5.10.0 &&
544910895d2cSMateusz Kulikowski		    defined $stat &&
545010895d2cSMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
545198a9bba5SJoe Perches			if (WARN("PREFER_ETHER_ADDR_COPY",
545210895d2cSMateusz Kulikowski				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
545398a9bba5SJoe Perches			    $fix) {
5454194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
545598a9bba5SJoe Perches			}
545698a9bba5SJoe Perches		}
545798a9bba5SJoe Perches
5458b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5459b6117d17SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
5460b6117d17SMateusz Kulikowski		    defined $stat &&
5461b6117d17SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5462b6117d17SMateusz Kulikowski			WARN("PREFER_ETHER_ADDR_EQUAL",
5463b6117d17SMateusz Kulikowski			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5464b6117d17SMateusz Kulikowski		}
5465b6117d17SMateusz Kulikowski
54668617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
54678617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
54688617cd09SMateusz Kulikowski		if ($^V && $^V ge 5.10.0 &&
54698617cd09SMateusz Kulikowski		    defined $stat &&
54708617cd09SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
54718617cd09SMateusz Kulikowski
54728617cd09SMateusz Kulikowski			my $ms_val = $7;
54738617cd09SMateusz Kulikowski
54748617cd09SMateusz Kulikowski			if ($ms_val =~ /^(?:0x|)0+$/i) {
54758617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_ZERO_ADDR",
54768617cd09SMateusz Kulikowski					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
54778617cd09SMateusz Kulikowski				    $fix) {
54788617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
54798617cd09SMateusz Kulikowski				}
54808617cd09SMateusz Kulikowski			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
54818617cd09SMateusz Kulikowski				if (WARN("PREFER_ETH_BROADCAST_ADDR",
54828617cd09SMateusz Kulikowski					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
54838617cd09SMateusz Kulikowski				    $fix) {
54848617cd09SMateusz Kulikowski					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
54858617cd09SMateusz Kulikowski				}
54868617cd09SMateusz Kulikowski			}
54878617cd09SMateusz Kulikowski		}
54888617cd09SMateusz Kulikowski
5489d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
5490d1fe9c09SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5491d1fe9c09SJoe Perches		    defined $stat &&
5492d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
5493d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
5494d7c76ba7SJoe Perches				my $call = $1;
5495d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
5496d7c76ba7SJoe Perches				my $arg1 = $3;
5497d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
5498d1fe9c09SJoe Perches				my $arg2 = $8;
5499d7c76ba7SJoe Perches				my $cast;
5500d7c76ba7SJoe Perches
5501d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
5502d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
5503d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
5504d7c76ba7SJoe Perches					$cast = $cast1;
5505d7c76ba7SJoe Perches				} else {
5506d7c76ba7SJoe Perches					$cast = $cast2;
5507d7c76ba7SJoe Perches				}
5508d7c76ba7SJoe Perches				WARN("MINMAX",
5509d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
5510554e165cSAndy Whitcroft			}
5511554e165cSAndy Whitcroft		}
5512554e165cSAndy Whitcroft
55134a273195SJoe Perches# check usleep_range arguments
55144a273195SJoe Perches		if ($^V && $^V ge 5.10.0 &&
55154a273195SJoe Perches		    defined $stat &&
55164a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
55174a273195SJoe Perches			my $min = $1;
55184a273195SJoe Perches			my $max = $7;
55194a273195SJoe Perches			if ($min eq $max) {
55204a273195SJoe Perches				WARN("USLEEP_RANGE",
55214a273195SJoe Perches				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
55224a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
55234a273195SJoe Perches				 $min > $max) {
55244a273195SJoe Perches				WARN("USLEEP_RANGE",
55254a273195SJoe Perches				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
55264a273195SJoe Perches			}
55274a273195SJoe Perches		}
55284a273195SJoe Perches
5529823b794cSJoe Perches# check for naked sscanf
5530823b794cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5531823b794cSJoe Perches		    defined $stat &&
55326c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
5533823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5534823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5535823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5536823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
5537823b794cSJoe Perches			$lc = $lc + $linenr;
5538823b794cSJoe Perches			my $stat_real = raw_line($linenr, 0);
5539823b794cSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5540823b794cSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5541823b794cSJoe Perches			}
5542823b794cSJoe Perches			WARN("NAKED_SSCANF",
5543823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5544823b794cSJoe Perches		}
5545823b794cSJoe Perches
5546afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
5547afc819abSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5548afc819abSJoe Perches		    defined $stat &&
5549afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
5550afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
5551afc819abSJoe Perches			$lc = $lc + $linenr;
5552afc819abSJoe Perches			my $stat_real = raw_line($linenr, 0);
5553afc819abSJoe Perches		        for (my $count = $linenr + 1; $count <= $lc; $count++) {
5554afc819abSJoe Perches				$stat_real = $stat_real . "\n" . raw_line($count, 0);
5555afc819abSJoe Perches			}
5556afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5557afc819abSJoe Perches				my $format = $6;
5558afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
5559afc819abSJoe Perches				if ($count == 1 &&
5560afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5561afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
5562afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5563afc819abSJoe Perches				}
5564afc819abSJoe Perches			}
5565afc819abSJoe Perches		}
5566afc819abSJoe Perches
556770dc8a48SJoe Perches# check for new externs in .h files.
556870dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
556970dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
5570d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
557170dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
557270dc8a48SJoe Perches			    $fix) {
5573194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
557470dc8a48SJoe Perches			}
557570dc8a48SJoe Perches		}
557670dc8a48SJoe Perches
5577de7d4f0eSAndy Whitcroft# check for new externs in .c files.
5578171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
5579c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
5580171ae1a4SAndy Whitcroft		{
5581c45dcabdSAndy Whitcroft			my $function_name = $1;
5582c45dcabdSAndy Whitcroft			my $paren_space = $2;
5583171ae1a4SAndy Whitcroft
5584171ae1a4SAndy Whitcroft			my $s = $stat;
5585171ae1a4SAndy Whitcroft			if (defined $cond) {
5586171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
5587171ae1a4SAndy Whitcroft			}
5588c45dcabdSAndy Whitcroft			if ($s =~ /^\s*;/ &&
5589c45dcabdSAndy Whitcroft			    $function_name ne 'uninitialized_var')
5590c45dcabdSAndy Whitcroft			{
5591000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
5592000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
5593de7d4f0eSAndy Whitcroft			}
5594de7d4f0eSAndy Whitcroft
5595171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
5596000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
5597000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
5598171ae1a4SAndy Whitcroft			}
55999c9ba34eSAndy Whitcroft
56009c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
56019c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
56029c9ba34eSAndy Whitcroft		{
5603000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
5604000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
5605171ae1a4SAndy Whitcroft		}
5606171ae1a4SAndy Whitcroft
5607de7d4f0eSAndy Whitcroft# checks for new __setup's
5608de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
5609de7d4f0eSAndy Whitcroft			my $name = $1;
5610de7d4f0eSAndy Whitcroft
5611de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
5612000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
5613000d1cc1SJoe Perches				    "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
5614de7d4f0eSAndy Whitcroft			}
5615653d4876SAndy Whitcroft		}
56169c0ca6f9SAndy Whitcroft
56179c0ca6f9SAndy Whitcroft# check for pointless casting of kmalloc return
5618caf2a54fSJoe Perches		if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
5619000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
5620000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
56219c0ca6f9SAndy Whitcroft		}
562213214adfSAndy Whitcroft
5623a640d25cSJoe Perches# alloc style
5624a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5625a640d25cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5626a640d25cSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5627a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
5628a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5629a640d25cSJoe Perches		}
5630a640d25cSJoe Perches
563160a55369SJoe Perches# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
563260a55369SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5633e367455aSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
563460a55369SJoe Perches			my $oldfunc = $3;
563560a55369SJoe Perches			my $a1 = $4;
563660a55369SJoe Perches			my $a2 = $10;
563760a55369SJoe Perches			my $newfunc = "kmalloc_array";
563860a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
563960a55369SJoe Perches			my $r1 = $a1;
564060a55369SJoe Perches			my $r2 = $a2;
564160a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
564260a55369SJoe Perches				$r1 = $a2;
564360a55369SJoe Perches				$r2 = $a1;
564460a55369SJoe Perches			}
5645e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5646e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
5647e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
5648e367455aSJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5649e367455aSJoe Perches				    $fix) {
5650194f66fcSJoe 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;
565160a55369SJoe Perches
565260a55369SJoe Perches				}
565360a55369SJoe Perches			}
565460a55369SJoe Perches		}
565560a55369SJoe Perches
5656972fdea2SJoe Perches# check for krealloc arg reuse
5657972fdea2SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5658972fdea2SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5659972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
5660972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5661972fdea2SJoe Perches		}
5662972fdea2SJoe Perches
56635ce59ae0SJoe Perches# check for alloc argument mismatch
56645ce59ae0SJoe Perches		if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
56655ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
56665ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
56675ce59ae0SJoe Perches		}
56685ce59ae0SJoe Perches
5669caf2a54fSJoe Perches# check for multiple semicolons
5670caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
5671d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
5672d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5673d5e616fcSJoe Perches			    $fix) {
5674194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
5675d5e616fcSJoe Perches			}
5676d1e2ad07SJoe Perches		}
5677d1e2ad07SJoe Perches
56780ab90191SJoe Perches# check for #defines like: 1 << <digit> that could be BIT(digit)
56790ab90191SJoe Perches		if ($line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
56800ab90191SJoe Perches			my $ull = "";
56810ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
56820ab90191SJoe Perches			if (CHK("BIT_MACRO",
56830ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
56840ab90191SJoe Perches			    $fix) {
56850ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
56860ab90191SJoe Perches			}
56870ab90191SJoe Perches		}
56880ab90191SJoe Perches
56892d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
56902d632745SJoe 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*$/) {
56912d632745SJoe Perches			my $config = $1;
56922d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
56932d632745SJoe Perches				 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
56942d632745SJoe Perches			    $fix) {
56952d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
56962d632745SJoe Perches			}
56972d632745SJoe Perches		}
56982d632745SJoe Perches
5699e81f239bSJoe Perches# check for case / default statements not preceded by break/fallthrough/switch
5700c34c09a8SJoe Perches		if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5701c34c09a8SJoe Perches			my $has_break = 0;
5702c34c09a8SJoe Perches			my $has_statement = 0;
5703c34c09a8SJoe Perches			my $count = 0;
5704c34c09a8SJoe Perches			my $prevline = $linenr;
5705e81f239bSJoe Perches			while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
5706c34c09a8SJoe Perches				$prevline--;
5707c34c09a8SJoe Perches				my $rline = $rawlines[$prevline - 1];
5708c34c09a8SJoe Perches				my $fline = $lines[$prevline - 1];
5709c34c09a8SJoe Perches				last if ($fline =~ /^\@\@/);
5710c34c09a8SJoe Perches				next if ($fline =~ /^\-/);
5711c34c09a8SJoe Perches				next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5712c34c09a8SJoe Perches				$has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5713c34c09a8SJoe Perches				next if ($fline =~ /^.[\s$;]*$/);
5714c34c09a8SJoe Perches				$has_statement = 1;
5715c34c09a8SJoe Perches				$count++;
5716c34c09a8SJoe Perches				$has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5717c34c09a8SJoe Perches			}
5718c34c09a8SJoe Perches			if (!$has_break && $has_statement) {
5719c34c09a8SJoe Perches				WARN("MISSING_BREAK",
5720c34c09a8SJoe Perches				     "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5721c34c09a8SJoe Perches			}
5722c34c09a8SJoe Perches		}
5723c34c09a8SJoe Perches
5724d1e2ad07SJoe Perches# check for switch/default statements without a break;
5725d1e2ad07SJoe Perches		if ($^V && $^V ge 5.10.0 &&
5726d1e2ad07SJoe Perches		    defined $stat &&
5727d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5728d1e2ad07SJoe Perches			my $ctx = '';
5729d1e2ad07SJoe Perches			my $herectx = $here . "\n";
5730d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
5731d1e2ad07SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
5732d1e2ad07SJoe Perches				$herectx .= raw_line($linenr, $n) . "\n";
5733d1e2ad07SJoe Perches			}
5734d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
5735d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
5736caf2a54fSJoe Perches		}
5737caf2a54fSJoe Perches
573813214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
5739d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
5740d5e616fcSJoe Perches			if (WARN("USE_FUNC",
5741d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
5742d5e616fcSJoe Perches			    $fix) {
5743194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
5744d5e616fcSJoe Perches			}
574513214adfSAndy Whitcroft		}
5746773647a0SAndy Whitcroft
574762ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
574862ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
574962ec818fSJoe Perches			ERROR("DATE_TIME",
575062ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
575162ec818fSJoe Perches		}
575262ec818fSJoe Perches
57532c92488aSJoe Perches# check for use of yield()
57542c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
57552c92488aSJoe Perches			WARN("YIELD",
57562c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
57572c92488aSJoe Perches		}
57582c92488aSJoe Perches
5759179f8f40SJoe Perches# check for comparisons against true and false
5760179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5761179f8f40SJoe Perches			my $lead = $1;
5762179f8f40SJoe Perches			my $arg = $2;
5763179f8f40SJoe Perches			my $test = $3;
5764179f8f40SJoe Perches			my $otype = $4;
5765179f8f40SJoe Perches			my $trail = $5;
5766179f8f40SJoe Perches			my $op = "!";
5767179f8f40SJoe Perches
5768179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5769179f8f40SJoe Perches
5770179f8f40SJoe Perches			my $type = lc($otype);
5771179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
5772179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
5773179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
5774179f8f40SJoe Perches					$op = "";
5775179f8f40SJoe Perches				}
5776179f8f40SJoe Perches
5777179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
5778179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
5779179f8f40SJoe Perches
5780179f8f40SJoe Perches## maybe suggesting a correct construct would better
5781179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5782179f8f40SJoe Perches
5783179f8f40SJoe Perches			}
5784179f8f40SJoe Perches		}
5785179f8f40SJoe Perches
57864882720bSThomas Gleixner# check for semaphores initialized locked
57874882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
5788000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
5789000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
5790773647a0SAndy Whitcroft		}
57916712d858SJoe Perches
579267d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
579367d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
5794000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
579567d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
5796773647a0SAndy Whitcroft		}
57976712d858SJoe Perches
5798ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
5799f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
5800000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
5801ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
5802f3db6639SMichael Ellerman		}
58036712d858SJoe Perches
58040f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
58050f3c5aabSJoe Perches		my $const_structs = qr{
58060f3c5aabSJoe Perches				acpi_dock_ops|
580779404849SEmese Revfy				address_space_operations|
580879404849SEmese Revfy				backlight_ops|
580979404849SEmese Revfy				block_device_operations|
581079404849SEmese Revfy				dentry_operations|
581179404849SEmese Revfy				dev_pm_ops|
581279404849SEmese Revfy				dma_map_ops|
581379404849SEmese Revfy				extent_io_ops|
581479404849SEmese Revfy				file_lock_operations|
581579404849SEmese Revfy				file_operations|
581679404849SEmese Revfy				hv_ops|
581779404849SEmese Revfy				ide_dma_ops|
581879404849SEmese Revfy				intel_dvo_dev_ops|
581979404849SEmese Revfy				item_operations|
582079404849SEmese Revfy				iwl_ops|
582179404849SEmese Revfy				kgdb_arch|
582279404849SEmese Revfy				kgdb_io|
582379404849SEmese Revfy				kset_uevent_ops|
582479404849SEmese Revfy				lock_manager_operations|
582579404849SEmese Revfy				microcode_ops|
582679404849SEmese Revfy				mtrr_ops|
582779404849SEmese Revfy				neigh_ops|
582879404849SEmese Revfy				nlmsvc_binding|
58290f3c5aabSJoe Perches				of_device_id|
583079404849SEmese Revfy				pci_raw_ops|
583179404849SEmese Revfy				pipe_buf_operations|
583279404849SEmese Revfy				platform_hibernation_ops|
583379404849SEmese Revfy				platform_suspend_ops|
583479404849SEmese Revfy				proto_ops|
583579404849SEmese Revfy				rpc_pipe_ops|
583679404849SEmese Revfy				seq_operations|
583779404849SEmese Revfy				snd_ac97_build_ops|
583879404849SEmese Revfy				soc_pcmcia_socket_ops|
583979404849SEmese Revfy				stacktrace_ops|
584079404849SEmese Revfy				sysfs_ops|
584179404849SEmese Revfy				tty_operations|
58426d07d01bSJoe Perches				uart_ops|
584379404849SEmese Revfy				usb_mon_operations|
584479404849SEmese Revfy				wd_ops}x;
58456903ffb2SAndy Whitcroft		if ($line !~ /\bconst\b/ &&
58460f3c5aabSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b/) {
5847000d1cc1SJoe Perches			WARN("CONST_STRUCT",
5848000d1cc1SJoe Perches			     "struct $1 should normally be const\n" .
58496903ffb2SAndy Whitcroft				$herecurr);
58502b6db5cbSAndy Whitcroft		}
5851773647a0SAndy Whitcroft
5852773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
5853773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
5854773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
5855c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
5856c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
5857171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
5858171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
5859171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
5860773647a0SAndy Whitcroft		{
5861000d1cc1SJoe Perches			WARN("NR_CPUS",
5862000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
5863773647a0SAndy Whitcroft		}
58649c9ba34eSAndy Whitcroft
586552ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
586652ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
586752ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
586852ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
586952ea8506SJoe Perches		}
587052ea8506SJoe Perches
5871acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
5872acd9362cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5873acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
5874acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
5875acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
5876acd9362cSJoe Perches		}
5877acd9362cSJoe Perches
5878691d77b6SAndy Whitcroft# whine mightly about in_atomic
5879691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
5880691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
5881000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
5882000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
5883f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
5884000d1cc1SJoe Perches				WARN("IN_ATOMIC",
5885000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
5886691d77b6SAndy Whitcroft			}
5887691d77b6SAndy Whitcroft		}
58881704f47bSPeter Zijlstra
5889481aea5cSJoe Perches# whine about ACCESS_ONCE
5890481aea5cSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5891481aea5cSJoe Perches		    $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
5892481aea5cSJoe Perches			my $par = $1;
5893481aea5cSJoe Perches			my $eq = $2;
5894481aea5cSJoe Perches			my $fun = $3;
5895481aea5cSJoe Perches			$par =~ s/^\(\s*(.*)\s*\)$/$1/;
5896481aea5cSJoe Perches			if (defined($eq)) {
5897481aea5cSJoe Perches				if (WARN("PREFER_WRITE_ONCE",
5898481aea5cSJoe Perches					 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
5899481aea5cSJoe Perches				    $fix) {
5900481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
5901481aea5cSJoe Perches				}
5902481aea5cSJoe Perches			} else {
5903481aea5cSJoe Perches				if (WARN("PREFER_READ_ONCE",
5904481aea5cSJoe Perches					 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
5905481aea5cSJoe Perches				    $fix) {
5906481aea5cSJoe Perches					$fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
5907481aea5cSJoe Perches				}
5908481aea5cSJoe Perches			}
5909481aea5cSJoe Perches		}
5910481aea5cSJoe Perches
59111704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
59121704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
59131704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
59141704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
59151704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
59161704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
5917000d1cc1SJoe Perches				ERROR("LOCKDEP",
5918000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
59191704f47bSPeter Zijlstra			}
59201704f47bSPeter Zijlstra		}
592188f8831cSDave Jones
5922b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
5923b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
5924000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
5925000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
592688f8831cSDave Jones		}
59272435880fSJoe Perches
5928515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
5929515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
5930515a235eSJoe Perches		if ($^V && $^V ge 5.10.0 &&
5931515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
59322435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
59332435880fSJoe Perches				my $func = $entry->[0];
59342435880fSJoe Perches				my $arg_pos = $entry->[1];
59352435880fSJoe Perches
59362435880fSJoe Perches				my $skip_args = "";
59372435880fSJoe Perches				if ($arg_pos > 1) {
59382435880fSJoe Perches					$arg_pos--;
59392435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
59402435880fSJoe Perches				}
59412435880fSJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
5942515a235eSJoe Perches				if ($line =~ /$test/) {
59432435880fSJoe Perches					my $val = $1;
59442435880fSJoe Perches					$val = $6 if ($skip_args ne "");
59452435880fSJoe Perches
59461727cc70SJoe Perches					if ($val !~ /^0$/ &&
59471727cc70SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
59481727cc70SJoe Perches					     length($val) ne 4)) {
59492435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
59501727cc70SJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
5951c0a5c898SJoe Perches					} elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
5952c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
5953c0a5c898SJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
59542435880fSJoe Perches					}
59552435880fSJoe Perches				}
59562435880fSJoe Perches			}
595713214adfSAndy Whitcroft		}
59585a6d20ceSBjorn Andersson
59595a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
59605a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
59615a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
59625a6d20ceSBjorn Andersson			my $valid_licenses = qr{
59635a6d20ceSBjorn Andersson						GPL|
59645a6d20ceSBjorn Andersson						GPL\ v2|
59655a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
59665a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
59675a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
59685a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
59695a6d20ceSBjorn Andersson						Proprietary
59705a6d20ceSBjorn Andersson					}x;
59715a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
59725a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
59735a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
59745a6d20ceSBjorn Andersson			}
59755a6d20ceSBjorn Andersson		}
5976515a235eSJoe Perches	}
597713214adfSAndy Whitcroft
597813214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
597913214adfSAndy Whitcroft	# so just keep quiet.
598013214adfSAndy Whitcroft	if ($#rawlines == -1) {
598113214adfSAndy Whitcroft		exit(0);
59820a920b5bSAndy Whitcroft	}
59830a920b5bSAndy Whitcroft
59848905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
59858905a67cSAndy Whitcroft	# things that appear to be patches.
59868905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
59878905a67cSAndy Whitcroft		exit(0);
59888905a67cSAndy Whitcroft	}
59898905a67cSAndy Whitcroft
59908905a67cSAndy Whitcroft	# This is not a patch, and we are are in 'no-patch' mode so
59918905a67cSAndy Whitcroft	# just keep quiet.
59928905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
59938905a67cSAndy Whitcroft		exit(0);
59948905a67cSAndy Whitcroft	}
59958905a67cSAndy Whitcroft
599606330fc4SJoe Perches	if (!$is_patch && $file !~ /cover-letter\.patch$/) {
5997000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
5998000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
59990a920b5bSAndy Whitcroft	}
600034d8815fSJoe Perches	if ($is_patch && $filename ne '-' && $chk_signoff && $signoff == 0) {
6001000d1cc1SJoe Perches		ERROR("MISSING_SIGN_OFF",
6002000d1cc1SJoe Perches		      "Missing Signed-off-by: line(s)\n");
60030a920b5bSAndy Whitcroft	}
60040a920b5bSAndy Whitcroft
6005f0a594c1SAndy Whitcroft	print report_dump();
600613214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
600713214adfSAndy Whitcroft		print "$filename " if ($summary_file);
60086c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
60096c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
60106c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
60116c72ffaaSAndy Whitcroft	}
60128905a67cSAndy Whitcroft
6013d2c0a235SAndy Whitcroft	if ($quiet == 0) {
6014ef212196SJoe Perches		# If there were any defects found and not already fixing them
6015ef212196SJoe Perches		if (!$clean and !$fix) {
6016ef212196SJoe Perches			print << "EOM"
6017ef212196SJoe Perches
6018ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
6019ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
6020ef212196SJoe PerchesEOM
6021ef212196SJoe Perches		}
6022d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
6023d2c0a235SAndy Whitcroft		# then suggest that.
6024d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
6025b0781216SMike Frysinger			$rpt_cleaners = 0;
6026d8469f16SJoe Perches			print << "EOM"
6027d8469f16SJoe Perches
6028d8469f16SJoe PerchesNOTE: Whitespace errors detected.
6029d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
6030d8469f16SJoe PerchesEOM
6031d2c0a235SAndy Whitcroft		}
6032d2c0a235SAndy Whitcroft	}
6033d2c0a235SAndy Whitcroft
6034d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
6035d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
6036d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
60379624b8d6SJoe Perches		my $newfile = $filename;
60389624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
60393705ce5bSJoe Perches		my $linecount = 0;
60403705ce5bSJoe Perches		my $f;
60413705ce5bSJoe Perches
6042d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6043d752fcc8SJoe Perches
60443705ce5bSJoe Perches		open($f, '>', $newfile)
60453705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
60463705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
60473705ce5bSJoe Perches			$linecount++;
60483705ce5bSJoe Perches			if ($file) {
60493705ce5bSJoe Perches				if ($linecount > 3) {
60503705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
60513705ce5bSJoe Perches					print $f $fixed_line . "\n";
60523705ce5bSJoe Perches				}
60533705ce5bSJoe Perches			} else {
60543705ce5bSJoe Perches				print $f $fixed_line . "\n";
60553705ce5bSJoe Perches			}
60563705ce5bSJoe Perches		}
60573705ce5bSJoe Perches		close($f);
60583705ce5bSJoe Perches
60593705ce5bSJoe Perches		if (!$quiet) {
60603705ce5bSJoe Perches			print << "EOM";
6061d8469f16SJoe Perches
60623705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
60633705ce5bSJoe Perches
60643705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
60653705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
60663705ce5bSJoe Perches
60673705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
60683705ce5bSJoe PerchesNo warranties, expressed or implied...
60693705ce5bSJoe PerchesEOM
60703705ce5bSJoe Perches		}
60713705ce5bSJoe Perches	}
60723705ce5bSJoe Perches
6073d8469f16SJoe Perches	if ($quiet == 0) {
6074d8469f16SJoe Perches		print "\n";
6075d8469f16SJoe Perches		if ($clean == 1) {
6076d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
6077d8469f16SJoe Perches		} else {
6078d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
60790a920b5bSAndy Whitcroft		}
60800a920b5bSAndy Whitcroft	}
60810a920b5bSAndy Whitcroft	return $clean;
60820a920b5bSAndy Whitcroft}
6083