xref: /aosp_15_r20/external/ltp/scripts/checkpatch.pl (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker#!/usr/bin/env perl
2*49cdfc7eSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0-only
3*49cdfc7eSAndroid Build Coastguard Worker#
4*49cdfc7eSAndroid Build Coastguard Worker# (c) 2001, Dave Jones. (the file handling bit)
5*49cdfc7eSAndroid Build Coastguard Worker# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
6*49cdfc7eSAndroid Build Coastguard Worker# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
7*49cdfc7eSAndroid Build Coastguard Worker# (c) 2008-2010 Andy Whitcroft <[email protected]>
8*49cdfc7eSAndroid Build Coastguard Worker# (c) 2010-2018 Joe Perches <[email protected]>
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Workeruse strict;
11*49cdfc7eSAndroid Build Coastguard Workeruse warnings;
12*49cdfc7eSAndroid Build Coastguard Workeruse POSIX;
13*49cdfc7eSAndroid Build Coastguard Workeruse File::Basename;
14*49cdfc7eSAndroid Build Coastguard Workeruse Cwd 'abs_path';
15*49cdfc7eSAndroid Build Coastguard Workeruse Term::ANSIColor qw(:constants);
16*49cdfc7eSAndroid Build Coastguard Workeruse Encode qw(decode encode);
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Workermy $P = $0;
19*49cdfc7eSAndroid Build Coastguard Workermy $D = dirname(abs_path($P));
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Workermy $V = '0.32';
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Workeruse Getopt::Long qw(:config no_auto_abbrev);
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Workermy $quiet = 0;
26*49cdfc7eSAndroid Build Coastguard Workermy $verbose = 0;
27*49cdfc7eSAndroid Build Coastguard Workermy %verbose_messages = ();
28*49cdfc7eSAndroid Build Coastguard Workermy %verbose_emitted = ();
29*49cdfc7eSAndroid Build Coastguard Workermy $tree = 1;
30*49cdfc7eSAndroid Build Coastguard Workermy $chk_signoff = 1;
31*49cdfc7eSAndroid Build Coastguard Workermy $chk_patch = 1;
32*49cdfc7eSAndroid Build Coastguard Workermy $tst_only;
33*49cdfc7eSAndroid Build Coastguard Workermy $emacs = 0;
34*49cdfc7eSAndroid Build Coastguard Workermy $terse = 0;
35*49cdfc7eSAndroid Build Coastguard Workermy $showfile = 0;
36*49cdfc7eSAndroid Build Coastguard Workermy $file = 0;
37*49cdfc7eSAndroid Build Coastguard Workermy $git = 0;
38*49cdfc7eSAndroid Build Coastguard Workermy %git_commits = ();
39*49cdfc7eSAndroid Build Coastguard Workermy $check = 0;
40*49cdfc7eSAndroid Build Coastguard Workermy $check_orig = 0;
41*49cdfc7eSAndroid Build Coastguard Workermy $summary = 1;
42*49cdfc7eSAndroid Build Coastguard Workermy $mailback = 0;
43*49cdfc7eSAndroid Build Coastguard Workermy $summary_file = 0;
44*49cdfc7eSAndroid Build Coastguard Workermy $show_types = 0;
45*49cdfc7eSAndroid Build Coastguard Workermy $list_types = 0;
46*49cdfc7eSAndroid Build Coastguard Workermy $fix = 0;
47*49cdfc7eSAndroid Build Coastguard Workermy $fix_inplace = 0;
48*49cdfc7eSAndroid Build Coastguard Workermy $root;
49*49cdfc7eSAndroid Build Coastguard Workermy $gitroot = $ENV{'GIT_DIR'};
50*49cdfc7eSAndroid Build Coastguard Worker$gitroot = ".git" if !defined($gitroot);
51*49cdfc7eSAndroid Build Coastguard Workermy %debug;
52*49cdfc7eSAndroid Build Coastguard Workermy %camelcase = ();
53*49cdfc7eSAndroid Build Coastguard Workermy %use_type = ();
54*49cdfc7eSAndroid Build Coastguard Workermy @use = ();
55*49cdfc7eSAndroid Build Coastguard Workermy %ignore_type = ();
56*49cdfc7eSAndroid Build Coastguard Workermy @ignore = ();
57*49cdfc7eSAndroid Build Coastguard Workermy $help = 0;
58*49cdfc7eSAndroid Build Coastguard Workermy $configuration_file = ".checkpatch.conf";
59*49cdfc7eSAndroid Build Coastguard Workermy $max_line_length = 100;
60*49cdfc7eSAndroid Build Coastguard Workermy $ignore_perl_version = 0;
61*49cdfc7eSAndroid Build Coastguard Workermy $minimum_perl_version = 5.10.0;
62*49cdfc7eSAndroid Build Coastguard Workermy $min_conf_desc_length = 4;
63*49cdfc7eSAndroid Build Coastguard Workermy $spelling_file = "$D/spelling.txt";
64*49cdfc7eSAndroid Build Coastguard Workermy $codespell = 0;
65*49cdfc7eSAndroid Build Coastguard Workermy $codespellfile = "/usr/share/codespell/dictionary.txt";
66*49cdfc7eSAndroid Build Coastguard Workermy $conststructsfile = "$D/const_structs.checkpatch";
67*49cdfc7eSAndroid Build Coastguard Workermy $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
68*49cdfc7eSAndroid Build Coastguard Workermy $typedefsfile;
69*49cdfc7eSAndroid Build Coastguard Workermy $color = "auto";
70*49cdfc7eSAndroid Build Coastguard Workermy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
71*49cdfc7eSAndroid Build Coastguard Worker# git output parsing needs US English output, so first set backtick child process LANGUAGE
72*49cdfc7eSAndroid Build Coastguard Workermy $git_command ='export LANGUAGE=en_US.UTF-8; git';
73*49cdfc7eSAndroid Build Coastguard Workermy $tabsize = 8;
74*49cdfc7eSAndroid Build Coastguard Workermy ${CONFIG_} = "CONFIG_";
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Workersub help {
77*49cdfc7eSAndroid Build Coastguard Worker	my ($exitcode) = @_;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker	print << "EOM";
80*49cdfc7eSAndroid Build Coastguard WorkerUsage: $P [OPTION]... [FILE]...
81*49cdfc7eSAndroid Build Coastguard WorkerVersion: $V
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard WorkerOptions:
84*49cdfc7eSAndroid Build Coastguard Worker  -q, --quiet                quiet
85*49cdfc7eSAndroid Build Coastguard Worker  -v, --verbose              verbose mode
86*49cdfc7eSAndroid Build Coastguard Worker  --no-tree                  run without a kernel tree
87*49cdfc7eSAndroid Build Coastguard Worker  --no-signoff               do not check for 'Signed-off-by' line
88*49cdfc7eSAndroid Build Coastguard Worker  --patch                    treat FILE as patchfile (default)
89*49cdfc7eSAndroid Build Coastguard Worker  --emacs                    emacs compile window format
90*49cdfc7eSAndroid Build Coastguard Worker  --terse                    one line per report
91*49cdfc7eSAndroid Build Coastguard Worker  --showfile                 emit diffed file position, not input file position
92*49cdfc7eSAndroid Build Coastguard Worker  -g, --git                  treat FILE as a single commit or git revision range
93*49cdfc7eSAndroid Build Coastguard Worker                             single git commit with:
94*49cdfc7eSAndroid Build Coastguard Worker                               <rev>
95*49cdfc7eSAndroid Build Coastguard Worker                               <rev>^
96*49cdfc7eSAndroid Build Coastguard Worker                               <rev>~n
97*49cdfc7eSAndroid Build Coastguard Worker                             multiple git commits with:
98*49cdfc7eSAndroid Build Coastguard Worker                               <rev1>..<rev2>
99*49cdfc7eSAndroid Build Coastguard Worker                               <rev1>...<rev2>
100*49cdfc7eSAndroid Build Coastguard Worker                               <rev>-<count>
101*49cdfc7eSAndroid Build Coastguard Worker                             git merges are ignored
102*49cdfc7eSAndroid Build Coastguard Worker  -f, --file                 treat FILE as regular source file
103*49cdfc7eSAndroid Build Coastguard Worker  --subjective, --strict     enable more subjective tests
104*49cdfc7eSAndroid Build Coastguard Worker  --list-types               list the possible message types
105*49cdfc7eSAndroid Build Coastguard Worker  --types TYPE(,TYPE2...)    show only these comma separated message types
106*49cdfc7eSAndroid Build Coastguard Worker  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
107*49cdfc7eSAndroid Build Coastguard Worker  --show-types               show the specific message type in the output
108*49cdfc7eSAndroid Build Coastguard Worker  --max-line-length=n        set the maximum line length, (default $max_line_length)
109*49cdfc7eSAndroid Build Coastguard Worker                             if exceeded, warn on patches
110*49cdfc7eSAndroid Build Coastguard Worker                             requires --strict for use with --file
111*49cdfc7eSAndroid Build Coastguard Worker  --min-conf-desc-length=n   set the min description length, if shorter, warn
112*49cdfc7eSAndroid Build Coastguard Worker  --tab-size=n               set the number of spaces for tab (default $tabsize)
113*49cdfc7eSAndroid Build Coastguard Worker  --root=PATH                PATH to the kernel tree root
114*49cdfc7eSAndroid Build Coastguard Worker  --no-summary               suppress the per-file summary
115*49cdfc7eSAndroid Build Coastguard Worker  --mailback                 only produce a report in case of warnings/errors
116*49cdfc7eSAndroid Build Coastguard Worker  --summary-file             include the filename in summary
117*49cdfc7eSAndroid Build Coastguard Worker  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
118*49cdfc7eSAndroid Build Coastguard Worker                             'values', 'possible', 'type', and 'attr' (default
119*49cdfc7eSAndroid Build Coastguard Worker                             is all off)
120*49cdfc7eSAndroid Build Coastguard Worker  --test-only=WORD           report only warnings/errors containing WORD
121*49cdfc7eSAndroid Build Coastguard Worker                             literally
122*49cdfc7eSAndroid Build Coastguard Worker  --fix                      EXPERIMENTAL - may create horrible results
123*49cdfc7eSAndroid Build Coastguard Worker                             If correctable single-line errors exist, create
124*49cdfc7eSAndroid Build Coastguard Worker                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
125*49cdfc7eSAndroid Build Coastguard Worker                             with potential errors corrected to the preferred
126*49cdfc7eSAndroid Build Coastguard Worker                             checkpatch style
127*49cdfc7eSAndroid Build Coastguard Worker  --fix-inplace              EXPERIMENTAL - may create horrible results
128*49cdfc7eSAndroid Build Coastguard Worker                             Is the same as --fix, but overwrites the input
129*49cdfc7eSAndroid Build Coastguard Worker                             file.  It's your fault if there's no backup or git
130*49cdfc7eSAndroid Build Coastguard Worker  --ignore-perl-version      override checking of perl version.  expect
131*49cdfc7eSAndroid Build Coastguard Worker                             runtime errors.
132*49cdfc7eSAndroid Build Coastguard Worker  --codespell                Use the codespell dictionary for spelling/typos
133*49cdfc7eSAndroid Build Coastguard Worker                             (default:/usr/share/codespell/dictionary.txt)
134*49cdfc7eSAndroid Build Coastguard Worker  --codespellfile            Use this codespell dictionary
135*49cdfc7eSAndroid Build Coastguard Worker  --typedefsfile             Read additional types from this file
136*49cdfc7eSAndroid Build Coastguard Worker  --color[=WHEN]             Use colors 'always', 'never', or only when output
137*49cdfc7eSAndroid Build Coastguard Worker                             is a terminal ('auto'). Default is 'auto'.
138*49cdfc7eSAndroid Build Coastguard Worker  --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
139*49cdfc7eSAndroid Build Coastguard Worker                             ${CONFIG_})
140*49cdfc7eSAndroid Build Coastguard Worker  -h, --help, --version      display this help and exit
141*49cdfc7eSAndroid Build Coastguard Worker
142*49cdfc7eSAndroid Build Coastguard WorkerWhen FILE is - read standard input.
143*49cdfc7eSAndroid Build Coastguard WorkerEOM
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Worker	exit($exitcode);
146*49cdfc7eSAndroid Build Coastguard Worker}
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Workersub uniq {
149*49cdfc7eSAndroid Build Coastguard Worker	my %seen;
150*49cdfc7eSAndroid Build Coastguard Worker	return grep { !$seen{$_}++ } @_;
151*49cdfc7eSAndroid Build Coastguard Worker}
152*49cdfc7eSAndroid Build Coastguard Worker
153*49cdfc7eSAndroid Build Coastguard Workersub list_types {
154*49cdfc7eSAndroid Build Coastguard Worker	my ($exitcode) = @_;
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker	my $count = 0;
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker	local $/ = undef;
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker	open(my $script, '<', abs_path($P)) or
161*49cdfc7eSAndroid Build Coastguard Worker	    die "$P: Can't read '$P' $!\n";
162*49cdfc7eSAndroid Build Coastguard Worker
163*49cdfc7eSAndroid Build Coastguard Worker	my $text = <$script>;
164*49cdfc7eSAndroid Build Coastguard Worker	close($script);
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker	my %types = ();
167*49cdfc7eSAndroid Build Coastguard Worker	# Also catch when type or level is passed through a variable
168*49cdfc7eSAndroid Build Coastguard Worker	while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
169*49cdfc7eSAndroid Build Coastguard Worker		if (defined($1)) {
170*49cdfc7eSAndroid Build Coastguard Worker			if (exists($types{$2})) {
171*49cdfc7eSAndroid Build Coastguard Worker				$types{$2} .= ",$1" if ($types{$2} ne $1);
172*49cdfc7eSAndroid Build Coastguard Worker			} else {
173*49cdfc7eSAndroid Build Coastguard Worker				$types{$2} = $1;
174*49cdfc7eSAndroid Build Coastguard Worker			}
175*49cdfc7eSAndroid Build Coastguard Worker		} else {
176*49cdfc7eSAndroid Build Coastguard Worker			$types{$2} = "UNDETERMINED";
177*49cdfc7eSAndroid Build Coastguard Worker		}
178*49cdfc7eSAndroid Build Coastguard Worker	}
179*49cdfc7eSAndroid Build Coastguard Worker
180*49cdfc7eSAndroid Build Coastguard Worker	print("#\tMessage type\n\n");
181*49cdfc7eSAndroid Build Coastguard Worker	if ($color) {
182*49cdfc7eSAndroid Build Coastguard Worker		print(" ( Color coding: ");
183*49cdfc7eSAndroid Build Coastguard Worker		print(RED . "ERROR" . RESET);
184*49cdfc7eSAndroid Build Coastguard Worker		print(" | ");
185*49cdfc7eSAndroid Build Coastguard Worker		print(YELLOW . "WARNING" . RESET);
186*49cdfc7eSAndroid Build Coastguard Worker		print(" | ");
187*49cdfc7eSAndroid Build Coastguard Worker		print(GREEN . "CHECK" . RESET);
188*49cdfc7eSAndroid Build Coastguard Worker		print(" | ");
189*49cdfc7eSAndroid Build Coastguard Worker		print("Multiple levels / Undetermined");
190*49cdfc7eSAndroid Build Coastguard Worker		print(" )\n\n");
191*49cdfc7eSAndroid Build Coastguard Worker	}
192*49cdfc7eSAndroid Build Coastguard Worker
193*49cdfc7eSAndroid Build Coastguard Worker	foreach my $type (sort keys %types) {
194*49cdfc7eSAndroid Build Coastguard Worker		my $orig_type = $type;
195*49cdfc7eSAndroid Build Coastguard Worker		if ($color) {
196*49cdfc7eSAndroid Build Coastguard Worker			my $level = $types{$type};
197*49cdfc7eSAndroid Build Coastguard Worker			if ($level eq "ERROR") {
198*49cdfc7eSAndroid Build Coastguard Worker				$type = RED . $type . RESET;
199*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($level eq "WARN") {
200*49cdfc7eSAndroid Build Coastguard Worker				$type = YELLOW . $type . RESET;
201*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($level eq "CHK") {
202*49cdfc7eSAndroid Build Coastguard Worker				$type = GREEN . $type . RESET;
203*49cdfc7eSAndroid Build Coastguard Worker			}
204*49cdfc7eSAndroid Build Coastguard Worker		}
205*49cdfc7eSAndroid Build Coastguard Worker		print(++$count . "\t" . $type . "\n");
206*49cdfc7eSAndroid Build Coastguard Worker		if ($verbose && exists($verbose_messages{$orig_type})) {
207*49cdfc7eSAndroid Build Coastguard Worker			my $message = $verbose_messages{$orig_type};
208*49cdfc7eSAndroid Build Coastguard Worker			$message =~ s/\n/\n\t/g;
209*49cdfc7eSAndroid Build Coastguard Worker			print("\t" . $message . "\n\n");
210*49cdfc7eSAndroid Build Coastguard Worker		}
211*49cdfc7eSAndroid Build Coastguard Worker	}
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker	exit($exitcode);
214*49cdfc7eSAndroid Build Coastguard Worker}
215*49cdfc7eSAndroid Build Coastguard Worker
216*49cdfc7eSAndroid Build Coastguard Workermy $conf = which_conf($configuration_file);
217*49cdfc7eSAndroid Build Coastguard Workerif (-f $conf) {
218*49cdfc7eSAndroid Build Coastguard Worker	my @conf_args;
219*49cdfc7eSAndroid Build Coastguard Worker	open(my $conffile, '<', "$conf")
220*49cdfc7eSAndroid Build Coastguard Worker	    or warn "$P: Can't find a readable $configuration_file file $!\n";
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker	while (<$conffile>) {
223*49cdfc7eSAndroid Build Coastguard Worker		my $line = $_;
224*49cdfc7eSAndroid Build Coastguard Worker
225*49cdfc7eSAndroid Build Coastguard Worker		$line =~ s/\s*\n?$//g;
226*49cdfc7eSAndroid Build Coastguard Worker		$line =~ s/^\s*//g;
227*49cdfc7eSAndroid Build Coastguard Worker		$line =~ s/\s+/ /g;
228*49cdfc7eSAndroid Build Coastguard Worker
229*49cdfc7eSAndroid Build Coastguard Worker		next if ($line =~ m/^\s*#/);
230*49cdfc7eSAndroid Build Coastguard Worker		next if ($line =~ m/^\s*$/);
231*49cdfc7eSAndroid Build Coastguard Worker
232*49cdfc7eSAndroid Build Coastguard Worker		my @words = split(" ", $line);
233*49cdfc7eSAndroid Build Coastguard Worker		foreach my $word (@words) {
234*49cdfc7eSAndroid Build Coastguard Worker			last if ($word =~ m/^#/);
235*49cdfc7eSAndroid Build Coastguard Worker			push (@conf_args, $word);
236*49cdfc7eSAndroid Build Coastguard Worker		}
237*49cdfc7eSAndroid Build Coastguard Worker	}
238*49cdfc7eSAndroid Build Coastguard Worker	close($conffile);
239*49cdfc7eSAndroid Build Coastguard Worker	unshift(@ARGV, @conf_args) if @conf_args;
240*49cdfc7eSAndroid Build Coastguard Worker}
241*49cdfc7eSAndroid Build Coastguard Worker
242*49cdfc7eSAndroid Build Coastguard Workersub load_docs {
243*49cdfc7eSAndroid Build Coastguard Worker	open(my $docs, '<', "$docsfile")
244*49cdfc7eSAndroid Build Coastguard Worker	    or warn "$P: Can't read the documentation file $docsfile $!\n";
245*49cdfc7eSAndroid Build Coastguard Worker
246*49cdfc7eSAndroid Build Coastguard Worker	my $type = '';
247*49cdfc7eSAndroid Build Coastguard Worker	my $desc = '';
248*49cdfc7eSAndroid Build Coastguard Worker	my $in_desc = 0;
249*49cdfc7eSAndroid Build Coastguard Worker
250*49cdfc7eSAndroid Build Coastguard Worker	while (<$docs>) {
251*49cdfc7eSAndroid Build Coastguard Worker		chomp;
252*49cdfc7eSAndroid Build Coastguard Worker		my $line = $_;
253*49cdfc7eSAndroid Build Coastguard Worker		$line =~ s/\s+$//;
254*49cdfc7eSAndroid Build Coastguard Worker
255*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\s*\*\*(.+)\*\*$/) {
256*49cdfc7eSAndroid Build Coastguard Worker			if ($desc ne '') {
257*49cdfc7eSAndroid Build Coastguard Worker				$verbose_messages{$type} = trim($desc);
258*49cdfc7eSAndroid Build Coastguard Worker			}
259*49cdfc7eSAndroid Build Coastguard Worker			$type = $1;
260*49cdfc7eSAndroid Build Coastguard Worker			$desc = '';
261*49cdfc7eSAndroid Build Coastguard Worker			$in_desc = 1;
262*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($in_desc) {
263*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /^(?:\s{4,}|$)/) {
264*49cdfc7eSAndroid Build Coastguard Worker				$line =~ s/^\s{4}//;
265*49cdfc7eSAndroid Build Coastguard Worker				$desc .= $line;
266*49cdfc7eSAndroid Build Coastguard Worker				$desc .= "\n";
267*49cdfc7eSAndroid Build Coastguard Worker			} else {
268*49cdfc7eSAndroid Build Coastguard Worker				$verbose_messages{$type} = trim($desc);
269*49cdfc7eSAndroid Build Coastguard Worker				$type = '';
270*49cdfc7eSAndroid Build Coastguard Worker				$desc = '';
271*49cdfc7eSAndroid Build Coastguard Worker				$in_desc = 0;
272*49cdfc7eSAndroid Build Coastguard Worker			}
273*49cdfc7eSAndroid Build Coastguard Worker		}
274*49cdfc7eSAndroid Build Coastguard Worker	}
275*49cdfc7eSAndroid Build Coastguard Worker
276*49cdfc7eSAndroid Build Coastguard Worker	if ($desc ne '') {
277*49cdfc7eSAndroid Build Coastguard Worker		$verbose_messages{$type} = trim($desc);
278*49cdfc7eSAndroid Build Coastguard Worker	}
279*49cdfc7eSAndroid Build Coastguard Worker	close($docs);
280*49cdfc7eSAndroid Build Coastguard Worker}
281*49cdfc7eSAndroid Build Coastguard Worker
282*49cdfc7eSAndroid Build Coastguard Worker# Perl's Getopt::Long allows options to take optional arguments after a space.
283*49cdfc7eSAndroid Build Coastguard Worker# Prevent --color by itself from consuming other arguments
284*49cdfc7eSAndroid Build Coastguard Workerforeach (@ARGV) {
285*49cdfc7eSAndroid Build Coastguard Worker	if ($_ eq "--color" || $_ eq "-color") {
286*49cdfc7eSAndroid Build Coastguard Worker		$_ = "--color=$color";
287*49cdfc7eSAndroid Build Coastguard Worker	}
288*49cdfc7eSAndroid Build Coastguard Worker}
289*49cdfc7eSAndroid Build Coastguard Worker
290*49cdfc7eSAndroid Build Coastguard WorkerGetOptions(
291*49cdfc7eSAndroid Build Coastguard Worker	'q|quiet+'	=> \$quiet,
292*49cdfc7eSAndroid Build Coastguard Worker	'v|verbose!'	=> \$verbose,
293*49cdfc7eSAndroid Build Coastguard Worker	'tree!'		=> \$tree,
294*49cdfc7eSAndroid Build Coastguard Worker	'signoff!'	=> \$chk_signoff,
295*49cdfc7eSAndroid Build Coastguard Worker	'patch!'	=> \$chk_patch,
296*49cdfc7eSAndroid Build Coastguard Worker	'emacs!'	=> \$emacs,
297*49cdfc7eSAndroid Build Coastguard Worker	'terse!'	=> \$terse,
298*49cdfc7eSAndroid Build Coastguard Worker	'showfile!'	=> \$showfile,
299*49cdfc7eSAndroid Build Coastguard Worker	'f|file!'	=> \$file,
300*49cdfc7eSAndroid Build Coastguard Worker	'g|git!'	=> \$git,
301*49cdfc7eSAndroid Build Coastguard Worker	'subjective!'	=> \$check,
302*49cdfc7eSAndroid Build Coastguard Worker	'strict!'	=> \$check,
303*49cdfc7eSAndroid Build Coastguard Worker	'ignore=s'	=> \@ignore,
304*49cdfc7eSAndroid Build Coastguard Worker	'types=s'	=> \@use,
305*49cdfc7eSAndroid Build Coastguard Worker	'show-types!'	=> \$show_types,
306*49cdfc7eSAndroid Build Coastguard Worker	'list-types!'	=> \$list_types,
307*49cdfc7eSAndroid Build Coastguard Worker	'max-line-length=i' => \$max_line_length,
308*49cdfc7eSAndroid Build Coastguard Worker	'min-conf-desc-length=i' => \$min_conf_desc_length,
309*49cdfc7eSAndroid Build Coastguard Worker	'tab-size=i'	=> \$tabsize,
310*49cdfc7eSAndroid Build Coastguard Worker	'root=s'	=> \$root,
311*49cdfc7eSAndroid Build Coastguard Worker	'summary!'	=> \$summary,
312*49cdfc7eSAndroid Build Coastguard Worker	'mailback!'	=> \$mailback,
313*49cdfc7eSAndroid Build Coastguard Worker	'summary-file!'	=> \$summary_file,
314*49cdfc7eSAndroid Build Coastguard Worker	'fix!'		=> \$fix,
315*49cdfc7eSAndroid Build Coastguard Worker	'fix-inplace!'	=> \$fix_inplace,
316*49cdfc7eSAndroid Build Coastguard Worker	'ignore-perl-version!' => \$ignore_perl_version,
317*49cdfc7eSAndroid Build Coastguard Worker	'debug=s'	=> \%debug,
318*49cdfc7eSAndroid Build Coastguard Worker	'test-only=s'	=> \$tst_only,
319*49cdfc7eSAndroid Build Coastguard Worker	'codespell!'	=> \$codespell,
320*49cdfc7eSAndroid Build Coastguard Worker	'codespellfile=s'	=> \$codespellfile,
321*49cdfc7eSAndroid Build Coastguard Worker	'typedefsfile=s'	=> \$typedefsfile,
322*49cdfc7eSAndroid Build Coastguard Worker	'color=s'	=> \$color,
323*49cdfc7eSAndroid Build Coastguard Worker	'no-color'	=> \$color,	#keep old behaviors of -nocolor
324*49cdfc7eSAndroid Build Coastguard Worker	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
325*49cdfc7eSAndroid Build Coastguard Worker	'kconfig-prefix=s'	=> \${CONFIG_},
326*49cdfc7eSAndroid Build Coastguard Worker	'h|help'	=> \$help,
327*49cdfc7eSAndroid Build Coastguard Worker	'version'	=> \$help
328*49cdfc7eSAndroid Build Coastguard Worker) or help(1);
329*49cdfc7eSAndroid Build Coastguard Worker
330*49cdfc7eSAndroid Build Coastguard Workerhelp(0) if ($help);
331*49cdfc7eSAndroid Build Coastguard Worker
332*49cdfc7eSAndroid Build Coastguard Workerdie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
333*49cdfc7eSAndroid Build Coastguard Workerdie "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
334*49cdfc7eSAndroid Build Coastguard Worker
335*49cdfc7eSAndroid Build Coastguard Workerif ($color =~ /^[01]$/) {
336*49cdfc7eSAndroid Build Coastguard Worker	$color = !$color;
337*49cdfc7eSAndroid Build Coastguard Worker} elsif ($color =~ /^always$/i) {
338*49cdfc7eSAndroid Build Coastguard Worker	$color = 1;
339*49cdfc7eSAndroid Build Coastguard Worker} elsif ($color =~ /^never$/i) {
340*49cdfc7eSAndroid Build Coastguard Worker	$color = 0;
341*49cdfc7eSAndroid Build Coastguard Worker} elsif ($color =~ /^auto$/i) {
342*49cdfc7eSAndroid Build Coastguard Worker	$color = (-t STDOUT);
343*49cdfc7eSAndroid Build Coastguard Worker} else {
344*49cdfc7eSAndroid Build Coastguard Worker	die "$P: Invalid color mode: $color\n";
345*49cdfc7eSAndroid Build Coastguard Worker}
346*49cdfc7eSAndroid Build Coastguard Worker
347*49cdfc7eSAndroid Build Coastguard Workerload_docs() if ($verbose);
348*49cdfc7eSAndroid Build Coastguard Workerlist_types(0) if ($list_types);
349*49cdfc7eSAndroid Build Coastguard Worker
350*49cdfc7eSAndroid Build Coastguard Worker$fix = 1 if ($fix_inplace);
351*49cdfc7eSAndroid Build Coastguard Worker$check_orig = $check;
352*49cdfc7eSAndroid Build Coastguard Worker
353*49cdfc7eSAndroid Build Coastguard Workermy $exit = 0;
354*49cdfc7eSAndroid Build Coastguard Worker
355*49cdfc7eSAndroid Build Coastguard Workermy $perl_version_ok = 1;
356*49cdfc7eSAndroid Build Coastguard Workerif ($^V && $^V lt $minimum_perl_version) {
357*49cdfc7eSAndroid Build Coastguard Worker	$perl_version_ok = 0;
358*49cdfc7eSAndroid Build Coastguard Worker	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
359*49cdfc7eSAndroid Build Coastguard Worker	exit(1) if (!$ignore_perl_version);
360*49cdfc7eSAndroid Build Coastguard Worker}
361*49cdfc7eSAndroid Build Coastguard Worker
362*49cdfc7eSAndroid Build Coastguard Worker#if no filenames are given, push '-' to read patch from stdin
363*49cdfc7eSAndroid Build Coastguard Workerif ($#ARGV < 0) {
364*49cdfc7eSAndroid Build Coastguard Worker	push(@ARGV, '-');
365*49cdfc7eSAndroid Build Coastguard Worker}
366*49cdfc7eSAndroid Build Coastguard Worker
367*49cdfc7eSAndroid Build Coastguard Worker# skip TAB size 1 to avoid additional checks on $tabsize - 1
368*49cdfc7eSAndroid Build Coastguard Workerdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
369*49cdfc7eSAndroid Build Coastguard Worker
370*49cdfc7eSAndroid Build Coastguard Workersub hash_save_array_words {
371*49cdfc7eSAndroid Build Coastguard Worker	my ($hashRef, $arrayRef) = @_;
372*49cdfc7eSAndroid Build Coastguard Worker
373*49cdfc7eSAndroid Build Coastguard Worker	my @array = split(/,/, join(',', @$arrayRef));
374*49cdfc7eSAndroid Build Coastguard Worker	foreach my $word (@array) {
375*49cdfc7eSAndroid Build Coastguard Worker		$word =~ s/\s*\n?$//g;
376*49cdfc7eSAndroid Build Coastguard Worker		$word =~ s/^\s*//g;
377*49cdfc7eSAndroid Build Coastguard Worker		$word =~ s/\s+/ /g;
378*49cdfc7eSAndroid Build Coastguard Worker		$word =~ tr/[a-z]/[A-Z]/;
379*49cdfc7eSAndroid Build Coastguard Worker
380*49cdfc7eSAndroid Build Coastguard Worker		next if ($word =~ m/^\s*#/);
381*49cdfc7eSAndroid Build Coastguard Worker		next if ($word =~ m/^\s*$/);
382*49cdfc7eSAndroid Build Coastguard Worker
383*49cdfc7eSAndroid Build Coastguard Worker		$hashRef->{$word}++;
384*49cdfc7eSAndroid Build Coastguard Worker	}
385*49cdfc7eSAndroid Build Coastguard Worker}
386*49cdfc7eSAndroid Build Coastguard Worker
387*49cdfc7eSAndroid Build Coastguard Workersub hash_show_words {
388*49cdfc7eSAndroid Build Coastguard Worker	my ($hashRef, $prefix) = @_;
389*49cdfc7eSAndroid Build Coastguard Worker
390*49cdfc7eSAndroid Build Coastguard Worker	if (keys %$hashRef) {
391*49cdfc7eSAndroid Build Coastguard Worker		print "\nNOTE: $prefix message types:";
392*49cdfc7eSAndroid Build Coastguard Worker		foreach my $word (sort keys %$hashRef) {
393*49cdfc7eSAndroid Build Coastguard Worker			print " $word";
394*49cdfc7eSAndroid Build Coastguard Worker		}
395*49cdfc7eSAndroid Build Coastguard Worker		print "\n";
396*49cdfc7eSAndroid Build Coastguard Worker	}
397*49cdfc7eSAndroid Build Coastguard Worker}
398*49cdfc7eSAndroid Build Coastguard Worker
399*49cdfc7eSAndroid Build Coastguard Workerhash_save_array_words(\%ignore_type, \@ignore);
400*49cdfc7eSAndroid Build Coastguard Workerhash_save_array_words(\%use_type, \@use);
401*49cdfc7eSAndroid Build Coastguard Worker
402*49cdfc7eSAndroid Build Coastguard Workermy $dbg_values = 0;
403*49cdfc7eSAndroid Build Coastguard Workermy $dbg_possible = 0;
404*49cdfc7eSAndroid Build Coastguard Workermy $dbg_type = 0;
405*49cdfc7eSAndroid Build Coastguard Workermy $dbg_attr = 0;
406*49cdfc7eSAndroid Build Coastguard Workerfor my $key (keys %debug) {
407*49cdfc7eSAndroid Build Coastguard Worker	## no critic
408*49cdfc7eSAndroid Build Coastguard Worker	eval "\${dbg_$key} = '$debug{$key}';";
409*49cdfc7eSAndroid Build Coastguard Worker	die "$@" if ($@);
410*49cdfc7eSAndroid Build Coastguard Worker}
411*49cdfc7eSAndroid Build Coastguard Worker
412*49cdfc7eSAndroid Build Coastguard Workermy $rpt_cleaners = 0;
413*49cdfc7eSAndroid Build Coastguard Worker
414*49cdfc7eSAndroid Build Coastguard Workerif ($terse) {
415*49cdfc7eSAndroid Build Coastguard Worker	$emacs = 1;
416*49cdfc7eSAndroid Build Coastguard Worker	$quiet++;
417*49cdfc7eSAndroid Build Coastguard Worker}
418*49cdfc7eSAndroid Build Coastguard Worker
419*49cdfc7eSAndroid Build Coastguard Workerif ($tree) {
420*49cdfc7eSAndroid Build Coastguard Worker	if (defined $root) {
421*49cdfc7eSAndroid Build Coastguard Worker		if (!top_of_kernel_tree($root)) {
422*49cdfc7eSAndroid Build Coastguard Worker			die "$P: $root: --root does not point at a valid tree\n";
423*49cdfc7eSAndroid Build Coastguard Worker		}
424*49cdfc7eSAndroid Build Coastguard Worker	} else {
425*49cdfc7eSAndroid Build Coastguard Worker		if (top_of_kernel_tree('.')) {
426*49cdfc7eSAndroid Build Coastguard Worker			$root = '.';
427*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
428*49cdfc7eSAndroid Build Coastguard Worker						top_of_kernel_tree($1)) {
429*49cdfc7eSAndroid Build Coastguard Worker			$root = $1;
430*49cdfc7eSAndroid Build Coastguard Worker		}
431*49cdfc7eSAndroid Build Coastguard Worker	}
432*49cdfc7eSAndroid Build Coastguard Worker
433*49cdfc7eSAndroid Build Coastguard Worker	if (!defined $root) {
434*49cdfc7eSAndroid Build Coastguard Worker		print "Must be run from the top-level dir. of a kernel tree\n";
435*49cdfc7eSAndroid Build Coastguard Worker		exit(2);
436*49cdfc7eSAndroid Build Coastguard Worker	}
437*49cdfc7eSAndroid Build Coastguard Worker}
438*49cdfc7eSAndroid Build Coastguard Worker
439*49cdfc7eSAndroid Build Coastguard Workermy $emitted_corrupt = 0;
440*49cdfc7eSAndroid Build Coastguard Worker
441*49cdfc7eSAndroid Build Coastguard Workerour $Ident	= qr{
442*49cdfc7eSAndroid Build Coastguard Worker			[A-Za-z_][A-Za-z\d_]*
443*49cdfc7eSAndroid Build Coastguard Worker			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
444*49cdfc7eSAndroid Build Coastguard Worker		}x;
445*49cdfc7eSAndroid Build Coastguard Workerour $Storage	= qr{extern|static|asmlinkage};
446*49cdfc7eSAndroid Build Coastguard Workerour $Sparse	= qr{
447*49cdfc7eSAndroid Build Coastguard Worker			__user|
448*49cdfc7eSAndroid Build Coastguard Worker			__kernel|
449*49cdfc7eSAndroid Build Coastguard Worker			__force|
450*49cdfc7eSAndroid Build Coastguard Worker			__iomem|
451*49cdfc7eSAndroid Build Coastguard Worker			__must_check|
452*49cdfc7eSAndroid Build Coastguard Worker			__kprobes|
453*49cdfc7eSAndroid Build Coastguard Worker			__ref|
454*49cdfc7eSAndroid Build Coastguard Worker			__refconst|
455*49cdfc7eSAndroid Build Coastguard Worker			__refdata|
456*49cdfc7eSAndroid Build Coastguard Worker			__rcu|
457*49cdfc7eSAndroid Build Coastguard Worker			__private
458*49cdfc7eSAndroid Build Coastguard Worker		}x;
459*49cdfc7eSAndroid Build Coastguard Workerour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
460*49cdfc7eSAndroid Build Coastguard Workerour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
461*49cdfc7eSAndroid Build Coastguard Workerour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
462*49cdfc7eSAndroid Build Coastguard Workerour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
463*49cdfc7eSAndroid Build Coastguard Workerour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
464*49cdfc7eSAndroid Build Coastguard Worker
465*49cdfc7eSAndroid Build Coastguard Worker# Notes to $Attribute:
466*49cdfc7eSAndroid Build Coastguard Worker# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
467*49cdfc7eSAndroid Build Coastguard Workerour $Attribute	= qr{
468*49cdfc7eSAndroid Build Coastguard Worker			const|
469*49cdfc7eSAndroid Build Coastguard Worker			volatile|
470*49cdfc7eSAndroid Build Coastguard Worker			__percpu|
471*49cdfc7eSAndroid Build Coastguard Worker			__nocast|
472*49cdfc7eSAndroid Build Coastguard Worker			__safe|
473*49cdfc7eSAndroid Build Coastguard Worker			__bitwise|
474*49cdfc7eSAndroid Build Coastguard Worker			__packed__|
475*49cdfc7eSAndroid Build Coastguard Worker			__packed2__|
476*49cdfc7eSAndroid Build Coastguard Worker			__naked|
477*49cdfc7eSAndroid Build Coastguard Worker			__maybe_unused|
478*49cdfc7eSAndroid Build Coastguard Worker			__always_unused|
479*49cdfc7eSAndroid Build Coastguard Worker			__noreturn|
480*49cdfc7eSAndroid Build Coastguard Worker			__used|
481*49cdfc7eSAndroid Build Coastguard Worker			__cold|
482*49cdfc7eSAndroid Build Coastguard Worker			__pure|
483*49cdfc7eSAndroid Build Coastguard Worker			__noclone|
484*49cdfc7eSAndroid Build Coastguard Worker			__deprecated|
485*49cdfc7eSAndroid Build Coastguard Worker			__read_mostly|
486*49cdfc7eSAndroid Build Coastguard Worker			__ro_after_init|
487*49cdfc7eSAndroid Build Coastguard Worker			__kprobes|
488*49cdfc7eSAndroid Build Coastguard Worker			$InitAttribute|
489*49cdfc7eSAndroid Build Coastguard Worker			____cacheline_aligned|
490*49cdfc7eSAndroid Build Coastguard Worker			____cacheline_aligned_in_smp|
491*49cdfc7eSAndroid Build Coastguard Worker			____cacheline_internodealigned_in_smp|
492*49cdfc7eSAndroid Build Coastguard Worker			__weak|
493*49cdfc7eSAndroid Build Coastguard Worker			__alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\)
494*49cdfc7eSAndroid Build Coastguard Worker		  }x;
495*49cdfc7eSAndroid Build Coastguard Workerour $Modifier;
496*49cdfc7eSAndroid Build Coastguard Workerour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
497*49cdfc7eSAndroid Build Coastguard Workerour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
498*49cdfc7eSAndroid Build Coastguard Workerour $Lval	= qr{$Ident(?:$Member)*};
499*49cdfc7eSAndroid Build Coastguard Worker
500*49cdfc7eSAndroid Build Coastguard Workerour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
501*49cdfc7eSAndroid Build Coastguard Workerour $Binary	= qr{(?i)0b[01]+$Int_type?};
502*49cdfc7eSAndroid Build Coastguard Workerour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
503*49cdfc7eSAndroid Build Coastguard Workerour $Int	= qr{[0-9]+$Int_type?};
504*49cdfc7eSAndroid Build Coastguard Workerour $Octal	= qr{0[0-7]+$Int_type?};
505*49cdfc7eSAndroid Build Coastguard Workerour $String	= qr{(?:\b[Lu])?"[X\t]*"};
506*49cdfc7eSAndroid Build Coastguard Workerour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
507*49cdfc7eSAndroid Build Coastguard Workerour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
508*49cdfc7eSAndroid Build Coastguard Workerour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
509*49cdfc7eSAndroid Build Coastguard Workerour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
510*49cdfc7eSAndroid Build Coastguard Workerour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
511*49cdfc7eSAndroid Build Coastguard Workerour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
512*49cdfc7eSAndroid Build Coastguard Workerour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
513*49cdfc7eSAndroid Build Coastguard Workerour $Arithmetic = qr{\+|-|\*|\/|%};
514*49cdfc7eSAndroid Build Coastguard Workerour $Operators	= qr{
515*49cdfc7eSAndroid Build Coastguard Worker			<=|>=|==|!=|
516*49cdfc7eSAndroid Build Coastguard Worker			=>|->|<<|>>|<|>|!|~|
517*49cdfc7eSAndroid Build Coastguard Worker			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
518*49cdfc7eSAndroid Build Coastguard Worker		  }x;
519*49cdfc7eSAndroid Build Coastguard Worker
520*49cdfc7eSAndroid Build Coastguard Workerour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
521*49cdfc7eSAndroid Build Coastguard Worker
522*49cdfc7eSAndroid Build Coastguard Workerour $BasicType;
523*49cdfc7eSAndroid Build Coastguard Workerour $NonptrType;
524*49cdfc7eSAndroid Build Coastguard Workerour $NonptrTypeMisordered;
525*49cdfc7eSAndroid Build Coastguard Workerour $NonptrTypeWithAttr;
526*49cdfc7eSAndroid Build Coastguard Workerour $Type;
527*49cdfc7eSAndroid Build Coastguard Workerour $TypeMisordered;
528*49cdfc7eSAndroid Build Coastguard Workerour $Declare;
529*49cdfc7eSAndroid Build Coastguard Workerour $DeclareMisordered;
530*49cdfc7eSAndroid Build Coastguard Worker
531*49cdfc7eSAndroid Build Coastguard Workerour $NON_ASCII_UTF8	= qr{
532*49cdfc7eSAndroid Build Coastguard Worker	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
533*49cdfc7eSAndroid Build Coastguard Worker	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
534*49cdfc7eSAndroid Build Coastguard Worker	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
535*49cdfc7eSAndroid Build Coastguard Worker	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
536*49cdfc7eSAndroid Build Coastguard Worker	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
537*49cdfc7eSAndroid Build Coastguard Worker	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
538*49cdfc7eSAndroid Build Coastguard Worker	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
539*49cdfc7eSAndroid Build Coastguard Worker}x;
540*49cdfc7eSAndroid Build Coastguard Worker
541*49cdfc7eSAndroid Build Coastguard Workerour $UTF8	= qr{
542*49cdfc7eSAndroid Build Coastguard Worker	[\x09\x0A\x0D\x20-\x7E]              # ASCII
543*49cdfc7eSAndroid Build Coastguard Worker	| $NON_ASCII_UTF8
544*49cdfc7eSAndroid Build Coastguard Worker}x;
545*49cdfc7eSAndroid Build Coastguard Worker
546*49cdfc7eSAndroid Build Coastguard Workerour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
547*49cdfc7eSAndroid Build Coastguard Workerour $typeOtherOSTypedefs = qr{(?x:
548*49cdfc7eSAndroid Build Coastguard Worker	u_(?:char|short|int|long) |          # bsd
549*49cdfc7eSAndroid Build Coastguard Worker	u(?:nchar|short|int|long)            # sysv
550*49cdfc7eSAndroid Build Coastguard Worker)};
551*49cdfc7eSAndroid Build Coastguard Workerour $typeKernelTypedefs = qr{(?x:
552*49cdfc7eSAndroid Build Coastguard Worker	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
553*49cdfc7eSAndroid Build Coastguard Worker	atomic_t
554*49cdfc7eSAndroid Build Coastguard Worker)};
555*49cdfc7eSAndroid Build Coastguard Workerour $typeTypedefs = qr{(?x:
556*49cdfc7eSAndroid Build Coastguard Worker	$typeC99Typedefs\b|
557*49cdfc7eSAndroid Build Coastguard Worker	$typeOtherOSTypedefs\b|
558*49cdfc7eSAndroid Build Coastguard Worker	$typeKernelTypedefs\b
559*49cdfc7eSAndroid Build Coastguard Worker)};
560*49cdfc7eSAndroid Build Coastguard Worker
561*49cdfc7eSAndroid Build Coastguard Workerour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
562*49cdfc7eSAndroid Build Coastguard Worker
563*49cdfc7eSAndroid Build Coastguard Workerour $logFunctions = qr{(?x:
564*49cdfc7eSAndroid Build Coastguard Worker	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
565*49cdfc7eSAndroid Build Coastguard Worker	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
566*49cdfc7eSAndroid Build Coastguard Worker	TP_printk|
567*49cdfc7eSAndroid Build Coastguard Worker	WARN(?:_RATELIMIT|_ONCE|)|
568*49cdfc7eSAndroid Build Coastguard Worker	panic|
569*49cdfc7eSAndroid Build Coastguard Worker	MODULE_[A-Z_]+|
570*49cdfc7eSAndroid Build Coastguard Worker	seq_vprintf|seq_printf|seq_puts
571*49cdfc7eSAndroid Build Coastguard Worker)};
572*49cdfc7eSAndroid Build Coastguard Worker
573*49cdfc7eSAndroid Build Coastguard Workerour $allocFunctions = qr{(?x:
574*49cdfc7eSAndroid Build Coastguard Worker	(?:(?:devm_)?
575*49cdfc7eSAndroid Build Coastguard Worker		(?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? |
576*49cdfc7eSAndroid Build Coastguard Worker		kstrdup(?:_const)? |
577*49cdfc7eSAndroid Build Coastguard Worker		kmemdup(?:_nul)?) |
578*49cdfc7eSAndroid Build Coastguard Worker	(?:\w+)?alloc_skb(?:_ip_align)? |
579*49cdfc7eSAndroid Build Coastguard Worker				# dev_alloc_skb/netdev_alloc_skb, et al
580*49cdfc7eSAndroid Build Coastguard Worker	dma_alloc_coherent
581*49cdfc7eSAndroid Build Coastguard Worker)};
582*49cdfc7eSAndroid Build Coastguard Worker
583*49cdfc7eSAndroid Build Coastguard Workerour $signature_tags = qr{(?xi:
584*49cdfc7eSAndroid Build Coastguard Worker	Signed-off-by:|
585*49cdfc7eSAndroid Build Coastguard Worker	Co-developed-by:|
586*49cdfc7eSAndroid Build Coastguard Worker	Acked-by:|
587*49cdfc7eSAndroid Build Coastguard Worker	Tested-by:|
588*49cdfc7eSAndroid Build Coastguard Worker	Reviewed-by:|
589*49cdfc7eSAndroid Build Coastguard Worker	Reported-by:|
590*49cdfc7eSAndroid Build Coastguard Worker	Suggested-by:|
591*49cdfc7eSAndroid Build Coastguard Worker	To:|
592*49cdfc7eSAndroid Build Coastguard Worker	Cc:
593*49cdfc7eSAndroid Build Coastguard Worker)};
594*49cdfc7eSAndroid Build Coastguard Worker
595*49cdfc7eSAndroid Build Coastguard Workerour $tracing_logging_tags = qr{(?xi:
596*49cdfc7eSAndroid Build Coastguard Worker	[=-]*> |
597*49cdfc7eSAndroid Build Coastguard Worker	<[=-]* |
598*49cdfc7eSAndroid Build Coastguard Worker	\[ |
599*49cdfc7eSAndroid Build Coastguard Worker	\] |
600*49cdfc7eSAndroid Build Coastguard Worker	start |
601*49cdfc7eSAndroid Build Coastguard Worker	called |
602*49cdfc7eSAndroid Build Coastguard Worker	entered |
603*49cdfc7eSAndroid Build Coastguard Worker	entry |
604*49cdfc7eSAndroid Build Coastguard Worker	enter |
605*49cdfc7eSAndroid Build Coastguard Worker	in |
606*49cdfc7eSAndroid Build Coastguard Worker	inside |
607*49cdfc7eSAndroid Build Coastguard Worker	here |
608*49cdfc7eSAndroid Build Coastguard Worker	begin |
609*49cdfc7eSAndroid Build Coastguard Worker	exit |
610*49cdfc7eSAndroid Build Coastguard Worker	end |
611*49cdfc7eSAndroid Build Coastguard Worker	done |
612*49cdfc7eSAndroid Build Coastguard Worker	leave |
613*49cdfc7eSAndroid Build Coastguard Worker	completed |
614*49cdfc7eSAndroid Build Coastguard Worker	out |
615*49cdfc7eSAndroid Build Coastguard Worker	return |
616*49cdfc7eSAndroid Build Coastguard Worker	[\.\!:\s]*
617*49cdfc7eSAndroid Build Coastguard Worker)};
618*49cdfc7eSAndroid Build Coastguard Worker
619*49cdfc7eSAndroid Build Coastguard Workersub edit_distance_min {
620*49cdfc7eSAndroid Build Coastguard Worker	my (@arr) = @_;
621*49cdfc7eSAndroid Build Coastguard Worker	my $len = scalar @arr;
622*49cdfc7eSAndroid Build Coastguard Worker	if ((scalar @arr) < 1) {
623*49cdfc7eSAndroid Build Coastguard Worker		# if underflow, return
624*49cdfc7eSAndroid Build Coastguard Worker		return;
625*49cdfc7eSAndroid Build Coastguard Worker	}
626*49cdfc7eSAndroid Build Coastguard Worker	my $min = $arr[0];
627*49cdfc7eSAndroid Build Coastguard Worker	for my $i (0 .. ($len-1)) {
628*49cdfc7eSAndroid Build Coastguard Worker		if ($arr[$i] < $min) {
629*49cdfc7eSAndroid Build Coastguard Worker			$min = $arr[$i];
630*49cdfc7eSAndroid Build Coastguard Worker		}
631*49cdfc7eSAndroid Build Coastguard Worker	}
632*49cdfc7eSAndroid Build Coastguard Worker	return $min;
633*49cdfc7eSAndroid Build Coastguard Worker}
634*49cdfc7eSAndroid Build Coastguard Worker
635*49cdfc7eSAndroid Build Coastguard Workersub get_edit_distance {
636*49cdfc7eSAndroid Build Coastguard Worker	my ($str1, $str2) = @_;
637*49cdfc7eSAndroid Build Coastguard Worker	$str1 = lc($str1);
638*49cdfc7eSAndroid Build Coastguard Worker	$str2 = lc($str2);
639*49cdfc7eSAndroid Build Coastguard Worker	$str1 =~ s/-//g;
640*49cdfc7eSAndroid Build Coastguard Worker	$str2 =~ s/-//g;
641*49cdfc7eSAndroid Build Coastguard Worker	my $len1 = length($str1);
642*49cdfc7eSAndroid Build Coastguard Worker	my $len2 = length($str2);
643*49cdfc7eSAndroid Build Coastguard Worker	# two dimensional array storing minimum edit distance
644*49cdfc7eSAndroid Build Coastguard Worker	my @distance;
645*49cdfc7eSAndroid Build Coastguard Worker	for my $i (0 .. $len1) {
646*49cdfc7eSAndroid Build Coastguard Worker		for my $j (0 .. $len2) {
647*49cdfc7eSAndroid Build Coastguard Worker			if ($i == 0) {
648*49cdfc7eSAndroid Build Coastguard Worker				$distance[$i][$j] = $j;
649*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($j == 0) {
650*49cdfc7eSAndroid Build Coastguard Worker				$distance[$i][$j] = $i;
651*49cdfc7eSAndroid Build Coastguard Worker			} elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
652*49cdfc7eSAndroid Build Coastguard Worker				$distance[$i][$j] = $distance[$i - 1][$j - 1];
653*49cdfc7eSAndroid Build Coastguard Worker			} else {
654*49cdfc7eSAndroid Build Coastguard Worker				my $dist1 = $distance[$i][$j - 1]; #insert distance
655*49cdfc7eSAndroid Build Coastguard Worker				my $dist2 = $distance[$i - 1][$j]; # remove
656*49cdfc7eSAndroid Build Coastguard Worker				my $dist3 = $distance[$i - 1][$j - 1]; #replace
657*49cdfc7eSAndroid Build Coastguard Worker				$distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
658*49cdfc7eSAndroid Build Coastguard Worker			}
659*49cdfc7eSAndroid Build Coastguard Worker		}
660*49cdfc7eSAndroid Build Coastguard Worker	}
661*49cdfc7eSAndroid Build Coastguard Worker	return $distance[$len1][$len2];
662*49cdfc7eSAndroid Build Coastguard Worker}
663*49cdfc7eSAndroid Build Coastguard Worker
664*49cdfc7eSAndroid Build Coastguard Workersub find_standard_signature {
665*49cdfc7eSAndroid Build Coastguard Worker	my ($sign_off) = @_;
666*49cdfc7eSAndroid Build Coastguard Worker	my @standard_signature_tags = (
667*49cdfc7eSAndroid Build Coastguard Worker		'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
668*49cdfc7eSAndroid Build Coastguard Worker		'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
669*49cdfc7eSAndroid Build Coastguard Worker	);
670*49cdfc7eSAndroid Build Coastguard Worker	foreach my $signature (@standard_signature_tags) {
671*49cdfc7eSAndroid Build Coastguard Worker		return $signature if (get_edit_distance($sign_off, $signature) <= 2);
672*49cdfc7eSAndroid Build Coastguard Worker	}
673*49cdfc7eSAndroid Build Coastguard Worker
674*49cdfc7eSAndroid Build Coastguard Worker	return "";
675*49cdfc7eSAndroid Build Coastguard Worker}
676*49cdfc7eSAndroid Build Coastguard Worker
677*49cdfc7eSAndroid Build Coastguard Workerour @typeListMisordered = (
678*49cdfc7eSAndroid Build Coastguard Worker	qr{char\s+(?:un)?signed},
679*49cdfc7eSAndroid Build Coastguard Worker	qr{int\s+(?:(?:un)?signed\s+)?short\s},
680*49cdfc7eSAndroid Build Coastguard Worker	qr{int\s+short(?:\s+(?:un)?signed)},
681*49cdfc7eSAndroid Build Coastguard Worker	qr{short\s+int(?:\s+(?:un)?signed)},
682*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:un)?signed\s+int\s+short},
683*49cdfc7eSAndroid Build Coastguard Worker	qr{short\s+(?:un)?signed},
684*49cdfc7eSAndroid Build Coastguard Worker	qr{long\s+int\s+(?:un)?signed},
685*49cdfc7eSAndroid Build Coastguard Worker	qr{int\s+long\s+(?:un)?signed},
686*49cdfc7eSAndroid Build Coastguard Worker	qr{long\s+(?:un)?signed\s+int},
687*49cdfc7eSAndroid Build Coastguard Worker	qr{int\s+(?:un)?signed\s+long},
688*49cdfc7eSAndroid Build Coastguard Worker	qr{int\s+(?:un)?signed},
689*49cdfc7eSAndroid Build Coastguard Worker	qr{int\s+long\s+long\s+(?:un)?signed},
690*49cdfc7eSAndroid Build Coastguard Worker	qr{long\s+long\s+int\s+(?:un)?signed},
691*49cdfc7eSAndroid Build Coastguard Worker	qr{long\s+long\s+(?:un)?signed\s+int},
692*49cdfc7eSAndroid Build Coastguard Worker	qr{long\s+long\s+(?:un)?signed},
693*49cdfc7eSAndroid Build Coastguard Worker	qr{long\s+(?:un)?signed},
694*49cdfc7eSAndroid Build Coastguard Worker);
695*49cdfc7eSAndroid Build Coastguard Worker
696*49cdfc7eSAndroid Build Coastguard Workerour @typeList = (
697*49cdfc7eSAndroid Build Coastguard Worker	qr{void},
698*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:(?:un)?signed\s+)?char},
699*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:(?:un)?signed\s+)?short\s+int},
700*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:(?:un)?signed\s+)?short},
701*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:(?:un)?signed\s+)?int},
702*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:(?:un)?signed\s+)?long\s+int},
703*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
704*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:(?:un)?signed\s+)?long\s+long},
705*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:(?:un)?signed\s+)?long},
706*49cdfc7eSAndroid Build Coastguard Worker	qr{(?:un)?signed},
707*49cdfc7eSAndroid Build Coastguard Worker	qr{float},
708*49cdfc7eSAndroid Build Coastguard Worker	qr{double},
709*49cdfc7eSAndroid Build Coastguard Worker	qr{bool},
710*49cdfc7eSAndroid Build Coastguard Worker	qr{struct\s+$Ident},
711*49cdfc7eSAndroid Build Coastguard Worker	qr{union\s+$Ident},
712*49cdfc7eSAndroid Build Coastguard Worker	qr{enum\s+$Ident},
713*49cdfc7eSAndroid Build Coastguard Worker	qr{${Ident}_t},
714*49cdfc7eSAndroid Build Coastguard Worker	qr{${Ident}_handler},
715*49cdfc7eSAndroid Build Coastguard Worker	qr{${Ident}_handler_fn},
716*49cdfc7eSAndroid Build Coastguard Worker	@typeListMisordered,
717*49cdfc7eSAndroid Build Coastguard Worker);
718*49cdfc7eSAndroid Build Coastguard Worker
719*49cdfc7eSAndroid Build Coastguard Workerour $C90_int_types = qr{(?x:
720*49cdfc7eSAndroid Build Coastguard Worker	long\s+long\s+int\s+(?:un)?signed|
721*49cdfc7eSAndroid Build Coastguard Worker	long\s+long\s+(?:un)?signed\s+int|
722*49cdfc7eSAndroid Build Coastguard Worker	long\s+long\s+(?:un)?signed|
723*49cdfc7eSAndroid Build Coastguard Worker	(?:(?:un)?signed\s+)?long\s+long\s+int|
724*49cdfc7eSAndroid Build Coastguard Worker	(?:(?:un)?signed\s+)?long\s+long|
725*49cdfc7eSAndroid Build Coastguard Worker	int\s+long\s+long\s+(?:un)?signed|
726*49cdfc7eSAndroid Build Coastguard Worker	int\s+(?:(?:un)?signed\s+)?long\s+long|
727*49cdfc7eSAndroid Build Coastguard Worker
728*49cdfc7eSAndroid Build Coastguard Worker	long\s+int\s+(?:un)?signed|
729*49cdfc7eSAndroid Build Coastguard Worker	long\s+(?:un)?signed\s+int|
730*49cdfc7eSAndroid Build Coastguard Worker	long\s+(?:un)?signed|
731*49cdfc7eSAndroid Build Coastguard Worker	(?:(?:un)?signed\s+)?long\s+int|
732*49cdfc7eSAndroid Build Coastguard Worker	(?:(?:un)?signed\s+)?long|
733*49cdfc7eSAndroid Build Coastguard Worker	int\s+long\s+(?:un)?signed|
734*49cdfc7eSAndroid Build Coastguard Worker	int\s+(?:(?:un)?signed\s+)?long|
735*49cdfc7eSAndroid Build Coastguard Worker
736*49cdfc7eSAndroid Build Coastguard Worker	int\s+(?:un)?signed|
737*49cdfc7eSAndroid Build Coastguard Worker	(?:(?:un)?signed\s+)?int
738*49cdfc7eSAndroid Build Coastguard Worker)};
739*49cdfc7eSAndroid Build Coastguard Worker
740*49cdfc7eSAndroid Build Coastguard Workerour @typeListFile = ();
741*49cdfc7eSAndroid Build Coastguard Workerour @typeListWithAttr = (
742*49cdfc7eSAndroid Build Coastguard Worker	@typeList,
743*49cdfc7eSAndroid Build Coastguard Worker	qr{struct\s+$InitAttribute\s+$Ident},
744*49cdfc7eSAndroid Build Coastguard Worker	qr{union\s+$InitAttribute\s+$Ident},
745*49cdfc7eSAndroid Build Coastguard Worker);
746*49cdfc7eSAndroid Build Coastguard Worker
747*49cdfc7eSAndroid Build Coastguard Workerour @modifierList = (
748*49cdfc7eSAndroid Build Coastguard Worker	qr{fastcall},
749*49cdfc7eSAndroid Build Coastguard Worker);
750*49cdfc7eSAndroid Build Coastguard Workerour @modifierListFile = ();
751*49cdfc7eSAndroid Build Coastguard Worker
752*49cdfc7eSAndroid Build Coastguard Workerour @mode_permission_funcs = (
753*49cdfc7eSAndroid Build Coastguard Worker	["module_param", 3],
754*49cdfc7eSAndroid Build Coastguard Worker	["module_param_(?:array|named|string)", 4],
755*49cdfc7eSAndroid Build Coastguard Worker	["module_param_array_named", 5],
756*49cdfc7eSAndroid Build Coastguard Worker	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
757*49cdfc7eSAndroid Build Coastguard Worker	["proc_create(?:_data|)", 2],
758*49cdfc7eSAndroid Build Coastguard Worker	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
759*49cdfc7eSAndroid Build Coastguard Worker	["IIO_DEV_ATTR_[A-Z_]+", 1],
760*49cdfc7eSAndroid Build Coastguard Worker	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
761*49cdfc7eSAndroid Build Coastguard Worker	["SENSOR_TEMPLATE(?:_2|)", 3],
762*49cdfc7eSAndroid Build Coastguard Worker	["__ATTR", 2],
763*49cdfc7eSAndroid Build Coastguard Worker);
764*49cdfc7eSAndroid Build Coastguard Worker
765*49cdfc7eSAndroid Build Coastguard Workermy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
766*49cdfc7eSAndroid Build Coastguard Worker
767*49cdfc7eSAndroid Build Coastguard Worker#Create a search pattern for all these functions to speed up a loop below
768*49cdfc7eSAndroid Build Coastguard Workerour $mode_perms_search = "";
769*49cdfc7eSAndroid Build Coastguard Workerforeach my $entry (@mode_permission_funcs) {
770*49cdfc7eSAndroid Build Coastguard Worker	$mode_perms_search .= '|' if ($mode_perms_search ne "");
771*49cdfc7eSAndroid Build Coastguard Worker	$mode_perms_search .= $entry->[0];
772*49cdfc7eSAndroid Build Coastguard Worker}
773*49cdfc7eSAndroid Build Coastguard Worker$mode_perms_search = "(?:${mode_perms_search})";
774*49cdfc7eSAndroid Build Coastguard Worker
775*49cdfc7eSAndroid Build Coastguard Workerour %deprecated_apis = (
776*49cdfc7eSAndroid Build Coastguard Worker	"synchronize_rcu_bh"			=> "synchronize_rcu",
777*49cdfc7eSAndroid Build Coastguard Worker	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
778*49cdfc7eSAndroid Build Coastguard Worker	"call_rcu_bh"				=> "call_rcu",
779*49cdfc7eSAndroid Build Coastguard Worker	"rcu_barrier_bh"			=> "rcu_barrier",
780*49cdfc7eSAndroid Build Coastguard Worker	"synchronize_sched"			=> "synchronize_rcu",
781*49cdfc7eSAndroid Build Coastguard Worker	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
782*49cdfc7eSAndroid Build Coastguard Worker	"call_rcu_sched"			=> "call_rcu",
783*49cdfc7eSAndroid Build Coastguard Worker	"rcu_barrier_sched"			=> "rcu_barrier",
784*49cdfc7eSAndroid Build Coastguard Worker	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
785*49cdfc7eSAndroid Build Coastguard Worker	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
786*49cdfc7eSAndroid Build Coastguard Worker);
787*49cdfc7eSAndroid Build Coastguard Worker
788*49cdfc7eSAndroid Build Coastguard Worker#Create a search pattern for all these strings to speed up a loop below
789*49cdfc7eSAndroid Build Coastguard Workerour $deprecated_apis_search = "";
790*49cdfc7eSAndroid Build Coastguard Workerforeach my $entry (keys %deprecated_apis) {
791*49cdfc7eSAndroid Build Coastguard Worker	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
792*49cdfc7eSAndroid Build Coastguard Worker	$deprecated_apis_search .= $entry;
793*49cdfc7eSAndroid Build Coastguard Worker}
794*49cdfc7eSAndroid Build Coastguard Worker$deprecated_apis_search = "(?:${deprecated_apis_search})";
795*49cdfc7eSAndroid Build Coastguard Worker
796*49cdfc7eSAndroid Build Coastguard Workerour $mode_perms_world_writable = qr{
797*49cdfc7eSAndroid Build Coastguard Worker	S_IWUGO		|
798*49cdfc7eSAndroid Build Coastguard Worker	S_IWOTH		|
799*49cdfc7eSAndroid Build Coastguard Worker	S_IRWXUGO	|
800*49cdfc7eSAndroid Build Coastguard Worker	S_IALLUGO	|
801*49cdfc7eSAndroid Build Coastguard Worker	0[0-7][0-7][2367]
802*49cdfc7eSAndroid Build Coastguard Worker}x;
803*49cdfc7eSAndroid Build Coastguard Worker
804*49cdfc7eSAndroid Build Coastguard Workerour %mode_permission_string_types = (
805*49cdfc7eSAndroid Build Coastguard Worker	"S_IRWXU" => 0700,
806*49cdfc7eSAndroid Build Coastguard Worker	"S_IRUSR" => 0400,
807*49cdfc7eSAndroid Build Coastguard Worker	"S_IWUSR" => 0200,
808*49cdfc7eSAndroid Build Coastguard Worker	"S_IXUSR" => 0100,
809*49cdfc7eSAndroid Build Coastguard Worker	"S_IRWXG" => 0070,
810*49cdfc7eSAndroid Build Coastguard Worker	"S_IRGRP" => 0040,
811*49cdfc7eSAndroid Build Coastguard Worker	"S_IWGRP" => 0020,
812*49cdfc7eSAndroid Build Coastguard Worker	"S_IXGRP" => 0010,
813*49cdfc7eSAndroid Build Coastguard Worker	"S_IRWXO" => 0007,
814*49cdfc7eSAndroid Build Coastguard Worker	"S_IROTH" => 0004,
815*49cdfc7eSAndroid Build Coastguard Worker	"S_IWOTH" => 0002,
816*49cdfc7eSAndroid Build Coastguard Worker	"S_IXOTH" => 0001,
817*49cdfc7eSAndroid Build Coastguard Worker	"S_IRWXUGO" => 0777,
818*49cdfc7eSAndroid Build Coastguard Worker	"S_IRUGO" => 0444,
819*49cdfc7eSAndroid Build Coastguard Worker	"S_IWUGO" => 0222,
820*49cdfc7eSAndroid Build Coastguard Worker	"S_IXUGO" => 0111,
821*49cdfc7eSAndroid Build Coastguard Worker);
822*49cdfc7eSAndroid Build Coastguard Worker
823*49cdfc7eSAndroid Build Coastguard Worker#Create a search pattern for all these strings to speed up a loop below
824*49cdfc7eSAndroid Build Coastguard Workerour $mode_perms_string_search = "";
825*49cdfc7eSAndroid Build Coastguard Workerforeach my $entry (keys %mode_permission_string_types) {
826*49cdfc7eSAndroid Build Coastguard Worker	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
827*49cdfc7eSAndroid Build Coastguard Worker	$mode_perms_string_search .= $entry;
828*49cdfc7eSAndroid Build Coastguard Worker}
829*49cdfc7eSAndroid Build Coastguard Workerour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
830*49cdfc7eSAndroid Build Coastguard Workerour $multi_mode_perms_string_search = qr{
831*49cdfc7eSAndroid Build Coastguard Worker	${single_mode_perms_string_search}
832*49cdfc7eSAndroid Build Coastguard Worker	(?:\s*\|\s*${single_mode_perms_string_search})*
833*49cdfc7eSAndroid Build Coastguard Worker}x;
834*49cdfc7eSAndroid Build Coastguard Worker
835*49cdfc7eSAndroid Build Coastguard Workersub perms_to_octal {
836*49cdfc7eSAndroid Build Coastguard Worker	my ($string) = @_;
837*49cdfc7eSAndroid Build Coastguard Worker
838*49cdfc7eSAndroid Build Coastguard Worker	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
839*49cdfc7eSAndroid Build Coastguard Worker
840*49cdfc7eSAndroid Build Coastguard Worker	my $val = "";
841*49cdfc7eSAndroid Build Coastguard Worker	my $oval = "";
842*49cdfc7eSAndroid Build Coastguard Worker	my $to = 0;
843*49cdfc7eSAndroid Build Coastguard Worker	my $curpos = 0;
844*49cdfc7eSAndroid Build Coastguard Worker	my $lastpos = 0;
845*49cdfc7eSAndroid Build Coastguard Worker	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
846*49cdfc7eSAndroid Build Coastguard Worker		$curpos = pos($string);
847*49cdfc7eSAndroid Build Coastguard Worker		my $match = $2;
848*49cdfc7eSAndroid Build Coastguard Worker		my $omatch = $1;
849*49cdfc7eSAndroid Build Coastguard Worker		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
850*49cdfc7eSAndroid Build Coastguard Worker		$lastpos = $curpos;
851*49cdfc7eSAndroid Build Coastguard Worker		$to |= $mode_permission_string_types{$match};
852*49cdfc7eSAndroid Build Coastguard Worker		$val .= '\s*\|\s*' if ($val ne "");
853*49cdfc7eSAndroid Build Coastguard Worker		$val .= $match;
854*49cdfc7eSAndroid Build Coastguard Worker		$oval .= $omatch;
855*49cdfc7eSAndroid Build Coastguard Worker	}
856*49cdfc7eSAndroid Build Coastguard Worker	$oval =~ s/^\s*\|\s*//;
857*49cdfc7eSAndroid Build Coastguard Worker	$oval =~ s/\s*\|\s*$//;
858*49cdfc7eSAndroid Build Coastguard Worker	return sprintf("%04o", $to);
859*49cdfc7eSAndroid Build Coastguard Worker}
860*49cdfc7eSAndroid Build Coastguard Worker
861*49cdfc7eSAndroid Build Coastguard Workerour $allowed_asm_includes = qr{(?x:
862*49cdfc7eSAndroid Build Coastguard Worker	irq|
863*49cdfc7eSAndroid Build Coastguard Worker	memory|
864*49cdfc7eSAndroid Build Coastguard Worker	time|
865*49cdfc7eSAndroid Build Coastguard Worker	reboot
866*49cdfc7eSAndroid Build Coastguard Worker)};
867*49cdfc7eSAndroid Build Coastguard Worker# memory.h: ARM has a custom one
868*49cdfc7eSAndroid Build Coastguard Worker
869*49cdfc7eSAndroid Build Coastguard Worker# Load common spelling mistakes and build regular expression list.
870*49cdfc7eSAndroid Build Coastguard Workermy $misspellings;
871*49cdfc7eSAndroid Build Coastguard Workermy %spelling_fix;
872*49cdfc7eSAndroid Build Coastguard Worker
873*49cdfc7eSAndroid Build Coastguard Workerif (open(my $spelling, '<', $spelling_file)) {
874*49cdfc7eSAndroid Build Coastguard Worker	while (<$spelling>) {
875*49cdfc7eSAndroid Build Coastguard Worker		my $line = $_;
876*49cdfc7eSAndroid Build Coastguard Worker
877*49cdfc7eSAndroid Build Coastguard Worker		$line =~ s/\s*\n?$//g;
878*49cdfc7eSAndroid Build Coastguard Worker		$line =~ s/^\s*//g;
879*49cdfc7eSAndroid Build Coastguard Worker
880*49cdfc7eSAndroid Build Coastguard Worker		next if ($line =~ m/^\s*#/);
881*49cdfc7eSAndroid Build Coastguard Worker		next if ($line =~ m/^\s*$/);
882*49cdfc7eSAndroid Build Coastguard Worker
883*49cdfc7eSAndroid Build Coastguard Worker		my ($suspect, $fix) = split(/\|\|/, $line);
884*49cdfc7eSAndroid Build Coastguard Worker
885*49cdfc7eSAndroid Build Coastguard Worker		$spelling_fix{$suspect} = $fix;
886*49cdfc7eSAndroid Build Coastguard Worker	}
887*49cdfc7eSAndroid Build Coastguard Worker	close($spelling);
888*49cdfc7eSAndroid Build Coastguard Worker} else {
889*49cdfc7eSAndroid Build Coastguard Worker	warn "No typos will be found - file '$spelling_file': $!\n";
890*49cdfc7eSAndroid Build Coastguard Worker}
891*49cdfc7eSAndroid Build Coastguard Worker
892*49cdfc7eSAndroid Build Coastguard Workerif ($codespell) {
893*49cdfc7eSAndroid Build Coastguard Worker	if (open(my $spelling, '<', $codespellfile)) {
894*49cdfc7eSAndroid Build Coastguard Worker		while (<$spelling>) {
895*49cdfc7eSAndroid Build Coastguard Worker			my $line = $_;
896*49cdfc7eSAndroid Build Coastguard Worker
897*49cdfc7eSAndroid Build Coastguard Worker			$line =~ s/\s*\n?$//g;
898*49cdfc7eSAndroid Build Coastguard Worker			$line =~ s/^\s*//g;
899*49cdfc7eSAndroid Build Coastguard Worker
900*49cdfc7eSAndroid Build Coastguard Worker			next if ($line =~ m/^\s*#/);
901*49cdfc7eSAndroid Build Coastguard Worker			next if ($line =~ m/^\s*$/);
902*49cdfc7eSAndroid Build Coastguard Worker			next if ($line =~ m/, disabled/i);
903*49cdfc7eSAndroid Build Coastguard Worker
904*49cdfc7eSAndroid Build Coastguard Worker			$line =~ s/,.*$//;
905*49cdfc7eSAndroid Build Coastguard Worker
906*49cdfc7eSAndroid Build Coastguard Worker			my ($suspect, $fix) = split(/->/, $line);
907*49cdfc7eSAndroid Build Coastguard Worker
908*49cdfc7eSAndroid Build Coastguard Worker			$spelling_fix{$suspect} = $fix;
909*49cdfc7eSAndroid Build Coastguard Worker		}
910*49cdfc7eSAndroid Build Coastguard Worker		close($spelling);
911*49cdfc7eSAndroid Build Coastguard Worker	} else {
912*49cdfc7eSAndroid Build Coastguard Worker		warn "No codespell typos will be found - file '$codespellfile': $!\n";
913*49cdfc7eSAndroid Build Coastguard Worker	}
914*49cdfc7eSAndroid Build Coastguard Worker}
915*49cdfc7eSAndroid Build Coastguard Worker
916*49cdfc7eSAndroid Build Coastguard Worker$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
917*49cdfc7eSAndroid Build Coastguard Worker
918*49cdfc7eSAndroid Build Coastguard Workersub read_words {
919*49cdfc7eSAndroid Build Coastguard Worker	my ($wordsRef, $file) = @_;
920*49cdfc7eSAndroid Build Coastguard Worker
921*49cdfc7eSAndroid Build Coastguard Worker	if (open(my $words, '<', $file)) {
922*49cdfc7eSAndroid Build Coastguard Worker		while (<$words>) {
923*49cdfc7eSAndroid Build Coastguard Worker			my $line = $_;
924*49cdfc7eSAndroid Build Coastguard Worker
925*49cdfc7eSAndroid Build Coastguard Worker			$line =~ s/\s*\n?$//g;
926*49cdfc7eSAndroid Build Coastguard Worker			$line =~ s/^\s*//g;
927*49cdfc7eSAndroid Build Coastguard Worker
928*49cdfc7eSAndroid Build Coastguard Worker			next if ($line =~ m/^\s*#/);
929*49cdfc7eSAndroid Build Coastguard Worker			next if ($line =~ m/^\s*$/);
930*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /\s/) {
931*49cdfc7eSAndroid Build Coastguard Worker				print("$file: '$line' invalid - ignored\n");
932*49cdfc7eSAndroid Build Coastguard Worker				next;
933*49cdfc7eSAndroid Build Coastguard Worker			}
934*49cdfc7eSAndroid Build Coastguard Worker
935*49cdfc7eSAndroid Build Coastguard Worker			$$wordsRef .= '|' if (defined $$wordsRef);
936*49cdfc7eSAndroid Build Coastguard Worker			$$wordsRef .= $line;
937*49cdfc7eSAndroid Build Coastguard Worker		}
938*49cdfc7eSAndroid Build Coastguard Worker		close($file);
939*49cdfc7eSAndroid Build Coastguard Worker		return 1;
940*49cdfc7eSAndroid Build Coastguard Worker	}
941*49cdfc7eSAndroid Build Coastguard Worker
942*49cdfc7eSAndroid Build Coastguard Worker	return 0;
943*49cdfc7eSAndroid Build Coastguard Worker}
944*49cdfc7eSAndroid Build Coastguard Worker
945*49cdfc7eSAndroid Build Coastguard Workermy $const_structs;
946*49cdfc7eSAndroid Build Coastguard Workerif (show_type("CONST_STRUCT")) {
947*49cdfc7eSAndroid Build Coastguard Worker	read_words(\$const_structs, $conststructsfile)
948*49cdfc7eSAndroid Build Coastguard Worker	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
949*49cdfc7eSAndroid Build Coastguard Worker}
950*49cdfc7eSAndroid Build Coastguard Worker
951*49cdfc7eSAndroid Build Coastguard Workerif (defined($typedefsfile)) {
952*49cdfc7eSAndroid Build Coastguard Worker	my $typeOtherTypedefs;
953*49cdfc7eSAndroid Build Coastguard Worker	read_words(\$typeOtherTypedefs, $typedefsfile)
954*49cdfc7eSAndroid Build Coastguard Worker	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
955*49cdfc7eSAndroid Build Coastguard Worker	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
956*49cdfc7eSAndroid Build Coastguard Worker}
957*49cdfc7eSAndroid Build Coastguard Worker
958*49cdfc7eSAndroid Build Coastguard Workersub build_types {
959*49cdfc7eSAndroid Build Coastguard Worker	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
960*49cdfc7eSAndroid Build Coastguard Worker	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
961*49cdfc7eSAndroid Build Coastguard Worker	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
962*49cdfc7eSAndroid Build Coastguard Worker	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
963*49cdfc7eSAndroid Build Coastguard Worker	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
964*49cdfc7eSAndroid Build Coastguard Worker	$BasicType	= qr{
965*49cdfc7eSAndroid Build Coastguard Worker				(?:$typeTypedefs\b)|
966*49cdfc7eSAndroid Build Coastguard Worker				(?:${all}\b)
967*49cdfc7eSAndroid Build Coastguard Worker		}x;
968*49cdfc7eSAndroid Build Coastguard Worker	$NonptrType	= qr{
969*49cdfc7eSAndroid Build Coastguard Worker			(?:$Modifier\s+|const\s+)*
970*49cdfc7eSAndroid Build Coastguard Worker			(?:
971*49cdfc7eSAndroid Build Coastguard Worker				(?:typeof|__typeof__)\s*\([^\)]*\)|
972*49cdfc7eSAndroid Build Coastguard Worker				(?:$typeTypedefs\b)|
973*49cdfc7eSAndroid Build Coastguard Worker				(?:${all}\b)
974*49cdfc7eSAndroid Build Coastguard Worker			)
975*49cdfc7eSAndroid Build Coastguard Worker			(?:\s+$Modifier|\s+const)*
976*49cdfc7eSAndroid Build Coastguard Worker		  }x;
977*49cdfc7eSAndroid Build Coastguard Worker	$NonptrTypeMisordered	= qr{
978*49cdfc7eSAndroid Build Coastguard Worker			(?:$Modifier\s+|const\s+)*
979*49cdfc7eSAndroid Build Coastguard Worker			(?:
980*49cdfc7eSAndroid Build Coastguard Worker				(?:${Misordered}\b)
981*49cdfc7eSAndroid Build Coastguard Worker			)
982*49cdfc7eSAndroid Build Coastguard Worker			(?:\s+$Modifier|\s+const)*
983*49cdfc7eSAndroid Build Coastguard Worker		  }x;
984*49cdfc7eSAndroid Build Coastguard Worker	$NonptrTypeWithAttr	= qr{
985*49cdfc7eSAndroid Build Coastguard Worker			(?:$Modifier\s+|const\s+)*
986*49cdfc7eSAndroid Build Coastguard Worker			(?:
987*49cdfc7eSAndroid Build Coastguard Worker				(?:typeof|__typeof__)\s*\([^\)]*\)|
988*49cdfc7eSAndroid Build Coastguard Worker				(?:$typeTypedefs\b)|
989*49cdfc7eSAndroid Build Coastguard Worker				(?:${allWithAttr}\b)
990*49cdfc7eSAndroid Build Coastguard Worker			)
991*49cdfc7eSAndroid Build Coastguard Worker			(?:\s+$Modifier|\s+const)*
992*49cdfc7eSAndroid Build Coastguard Worker		  }x;
993*49cdfc7eSAndroid Build Coastguard Worker	$Type	= qr{
994*49cdfc7eSAndroid Build Coastguard Worker			$NonptrType
995*49cdfc7eSAndroid Build Coastguard Worker			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
996*49cdfc7eSAndroid Build Coastguard Worker			(?:\s+$Inline|\s+$Modifier)*
997*49cdfc7eSAndroid Build Coastguard Worker		  }x;
998*49cdfc7eSAndroid Build Coastguard Worker	$TypeMisordered	= qr{
999*49cdfc7eSAndroid Build Coastguard Worker			$NonptrTypeMisordered
1000*49cdfc7eSAndroid Build Coastguard Worker			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
1001*49cdfc7eSAndroid Build Coastguard Worker			(?:\s+$Inline|\s+$Modifier)*
1002*49cdfc7eSAndroid Build Coastguard Worker		  }x;
1003*49cdfc7eSAndroid Build Coastguard Worker	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
1004*49cdfc7eSAndroid Build Coastguard Worker	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
1005*49cdfc7eSAndroid Build Coastguard Worker}
1006*49cdfc7eSAndroid Build Coastguard Workerbuild_types();
1007*49cdfc7eSAndroid Build Coastguard Worker
1008*49cdfc7eSAndroid Build Coastguard Workerour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
1009*49cdfc7eSAndroid Build Coastguard Worker
1010*49cdfc7eSAndroid Build Coastguard Worker# Using $balanced_parens, $LvalOrFunc, or $FuncArg
1011*49cdfc7eSAndroid Build Coastguard Worker# requires at least perl version v5.10.0
1012*49cdfc7eSAndroid Build Coastguard Worker# Any use must be runtime checked with $^V
1013*49cdfc7eSAndroid Build Coastguard Worker
1014*49cdfc7eSAndroid Build Coastguard Workerour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
1015*49cdfc7eSAndroid Build Coastguard Workerour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
1016*49cdfc7eSAndroid Build Coastguard Workerour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
1017*49cdfc7eSAndroid Build Coastguard Worker
1018*49cdfc7eSAndroid Build Coastguard Workerour $declaration_macros = qr{(?x:
1019*49cdfc7eSAndroid Build Coastguard Worker	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
1020*49cdfc7eSAndroid Build Coastguard Worker	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
1021*49cdfc7eSAndroid Build Coastguard Worker	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(
1022*49cdfc7eSAndroid Build Coastguard Worker)};
1023*49cdfc7eSAndroid Build Coastguard Worker
1024*49cdfc7eSAndroid Build Coastguard Workerour %allow_repeated_words = (
1025*49cdfc7eSAndroid Build Coastguard Worker	add => '',
1026*49cdfc7eSAndroid Build Coastguard Worker	added => '',
1027*49cdfc7eSAndroid Build Coastguard Worker	bad => '',
1028*49cdfc7eSAndroid Build Coastguard Worker	be => '',
1029*49cdfc7eSAndroid Build Coastguard Worker);
1030*49cdfc7eSAndroid Build Coastguard Worker
1031*49cdfc7eSAndroid Build Coastguard Workersub deparenthesize {
1032*49cdfc7eSAndroid Build Coastguard Worker	my ($string) = @_;
1033*49cdfc7eSAndroid Build Coastguard Worker	return "" if (!defined($string));
1034*49cdfc7eSAndroid Build Coastguard Worker
1035*49cdfc7eSAndroid Build Coastguard Worker	while ($string =~ /^\s*\(.*\)\s*$/) {
1036*49cdfc7eSAndroid Build Coastguard Worker		$string =~ s@^\s*\(\s*@@;
1037*49cdfc7eSAndroid Build Coastguard Worker		$string =~ s@\s*\)\s*$@@;
1038*49cdfc7eSAndroid Build Coastguard Worker	}
1039*49cdfc7eSAndroid Build Coastguard Worker
1040*49cdfc7eSAndroid Build Coastguard Worker	$string =~ s@\s+@ @g;
1041*49cdfc7eSAndroid Build Coastguard Worker
1042*49cdfc7eSAndroid Build Coastguard Worker	return $string;
1043*49cdfc7eSAndroid Build Coastguard Worker}
1044*49cdfc7eSAndroid Build Coastguard Worker
1045*49cdfc7eSAndroid Build Coastguard Workersub seed_camelcase_file {
1046*49cdfc7eSAndroid Build Coastguard Worker	my ($file) = @_;
1047*49cdfc7eSAndroid Build Coastguard Worker
1048*49cdfc7eSAndroid Build Coastguard Worker	return if (!(-f $file));
1049*49cdfc7eSAndroid Build Coastguard Worker
1050*49cdfc7eSAndroid Build Coastguard Worker	local $/;
1051*49cdfc7eSAndroid Build Coastguard Worker
1052*49cdfc7eSAndroid Build Coastguard Worker	open(my $include_file, '<', "$file")
1053*49cdfc7eSAndroid Build Coastguard Worker	    or warn "$P: Can't read '$file' $!\n";
1054*49cdfc7eSAndroid Build Coastguard Worker	my $text = <$include_file>;
1055*49cdfc7eSAndroid Build Coastguard Worker	close($include_file);
1056*49cdfc7eSAndroid Build Coastguard Worker
1057*49cdfc7eSAndroid Build Coastguard Worker	my @lines = split('\n', $text);
1058*49cdfc7eSAndroid Build Coastguard Worker
1059*49cdfc7eSAndroid Build Coastguard Worker	foreach my $line (@lines) {
1060*49cdfc7eSAndroid Build Coastguard Worker		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
1061*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
1062*49cdfc7eSAndroid Build Coastguard Worker			$camelcase{$1} = 1;
1063*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
1064*49cdfc7eSAndroid Build Coastguard Worker			$camelcase{$1} = 1;
1065*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
1066*49cdfc7eSAndroid Build Coastguard Worker			$camelcase{$1} = 1;
1067*49cdfc7eSAndroid Build Coastguard Worker		}
1068*49cdfc7eSAndroid Build Coastguard Worker	}
1069*49cdfc7eSAndroid Build Coastguard Worker}
1070*49cdfc7eSAndroid Build Coastguard Worker
1071*49cdfc7eSAndroid Build Coastguard Workerour %maintained_status = ();
1072*49cdfc7eSAndroid Build Coastguard Worker
1073*49cdfc7eSAndroid Build Coastguard Workersub is_maintained_obsolete {
1074*49cdfc7eSAndroid Build Coastguard Worker	my ($filename) = @_;
1075*49cdfc7eSAndroid Build Coastguard Worker
1076*49cdfc7eSAndroid Build Coastguard Worker	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
1077*49cdfc7eSAndroid Build Coastguard Worker
1078*49cdfc7eSAndroid Build Coastguard Worker	if (!exists($maintained_status{$filename})) {
1079*49cdfc7eSAndroid Build Coastguard Worker		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1080*49cdfc7eSAndroid Build Coastguard Worker	}
1081*49cdfc7eSAndroid Build Coastguard Worker
1082*49cdfc7eSAndroid Build Coastguard Worker	return $maintained_status{$filename} =~ /obsolete/i;
1083*49cdfc7eSAndroid Build Coastguard Worker}
1084*49cdfc7eSAndroid Build Coastguard Worker
1085*49cdfc7eSAndroid Build Coastguard Workersub is_SPDX_License_valid {
1086*49cdfc7eSAndroid Build Coastguard Worker	my ($license) = @_;
1087*49cdfc7eSAndroid Build Coastguard Worker
1088*49cdfc7eSAndroid Build Coastguard Worker	return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
1089*49cdfc7eSAndroid Build Coastguard Worker
1090*49cdfc7eSAndroid Build Coastguard Worker	my $root_path = abs_path($root);
1091*49cdfc7eSAndroid Build Coastguard Worker	my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
1092*49cdfc7eSAndroid Build Coastguard Worker	return 0 if ($status ne "");
1093*49cdfc7eSAndroid Build Coastguard Worker	return 1;
1094*49cdfc7eSAndroid Build Coastguard Worker}
1095*49cdfc7eSAndroid Build Coastguard Worker
1096*49cdfc7eSAndroid Build Coastguard Workermy $camelcase_seeded = 0;
1097*49cdfc7eSAndroid Build Coastguard Workersub seed_camelcase_includes {
1098*49cdfc7eSAndroid Build Coastguard Worker	return if ($camelcase_seeded);
1099*49cdfc7eSAndroid Build Coastguard Worker
1100*49cdfc7eSAndroid Build Coastguard Worker	my $files;
1101*49cdfc7eSAndroid Build Coastguard Worker	my $camelcase_cache = "";
1102*49cdfc7eSAndroid Build Coastguard Worker	my @include_files = ();
1103*49cdfc7eSAndroid Build Coastguard Worker
1104*49cdfc7eSAndroid Build Coastguard Worker	$camelcase_seeded = 1;
1105*49cdfc7eSAndroid Build Coastguard Worker
1106*49cdfc7eSAndroid Build Coastguard Worker	if (-e "$gitroot") {
1107*49cdfc7eSAndroid Build Coastguard Worker		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
1108*49cdfc7eSAndroid Build Coastguard Worker		chomp $git_last_include_commit;
1109*49cdfc7eSAndroid Build Coastguard Worker		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1110*49cdfc7eSAndroid Build Coastguard Worker	} else {
1111*49cdfc7eSAndroid Build Coastguard Worker		my $last_mod_date = 0;
1112*49cdfc7eSAndroid Build Coastguard Worker		$files = `find $root/include -name "*.h"`;
1113*49cdfc7eSAndroid Build Coastguard Worker		@include_files = split('\n', $files);
1114*49cdfc7eSAndroid Build Coastguard Worker		foreach my $file (@include_files) {
1115*49cdfc7eSAndroid Build Coastguard Worker			my $date = POSIX::strftime("%Y%m%d%H%M",
1116*49cdfc7eSAndroid Build Coastguard Worker						   localtime((stat $file)[9]));
1117*49cdfc7eSAndroid Build Coastguard Worker			$last_mod_date = $date if ($last_mod_date < $date);
1118*49cdfc7eSAndroid Build Coastguard Worker		}
1119*49cdfc7eSAndroid Build Coastguard Worker		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1120*49cdfc7eSAndroid Build Coastguard Worker	}
1121*49cdfc7eSAndroid Build Coastguard Worker
1122*49cdfc7eSAndroid Build Coastguard Worker	if ($camelcase_cache ne "" && -f $camelcase_cache) {
1123*49cdfc7eSAndroid Build Coastguard Worker		open(my $camelcase_file, '<', "$camelcase_cache")
1124*49cdfc7eSAndroid Build Coastguard Worker		    or warn "$P: Can't read '$camelcase_cache' $!\n";
1125*49cdfc7eSAndroid Build Coastguard Worker		while (<$camelcase_file>) {
1126*49cdfc7eSAndroid Build Coastguard Worker			chomp;
1127*49cdfc7eSAndroid Build Coastguard Worker			$camelcase{$_} = 1;
1128*49cdfc7eSAndroid Build Coastguard Worker		}
1129*49cdfc7eSAndroid Build Coastguard Worker		close($camelcase_file);
1130*49cdfc7eSAndroid Build Coastguard Worker
1131*49cdfc7eSAndroid Build Coastguard Worker		return;
1132*49cdfc7eSAndroid Build Coastguard Worker	}
1133*49cdfc7eSAndroid Build Coastguard Worker
1134*49cdfc7eSAndroid Build Coastguard Worker	if (-e "$gitroot") {
1135*49cdfc7eSAndroid Build Coastguard Worker		$files = `${git_command} ls-files "include/*.h"`;
1136*49cdfc7eSAndroid Build Coastguard Worker		@include_files = split('\n', $files);
1137*49cdfc7eSAndroid Build Coastguard Worker	}
1138*49cdfc7eSAndroid Build Coastguard Worker
1139*49cdfc7eSAndroid Build Coastguard Worker	foreach my $file (@include_files) {
1140*49cdfc7eSAndroid Build Coastguard Worker		seed_camelcase_file($file);
1141*49cdfc7eSAndroid Build Coastguard Worker	}
1142*49cdfc7eSAndroid Build Coastguard Worker
1143*49cdfc7eSAndroid Build Coastguard Worker	if ($camelcase_cache ne "") {
1144*49cdfc7eSAndroid Build Coastguard Worker		unlink glob ".checkpatch-camelcase.*";
1145*49cdfc7eSAndroid Build Coastguard Worker		open(my $camelcase_file, '>', "$camelcase_cache")
1146*49cdfc7eSAndroid Build Coastguard Worker		    or warn "$P: Can't write '$camelcase_cache' $!\n";
1147*49cdfc7eSAndroid Build Coastguard Worker		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1148*49cdfc7eSAndroid Build Coastguard Worker			print $camelcase_file ("$_\n");
1149*49cdfc7eSAndroid Build Coastguard Worker		}
1150*49cdfc7eSAndroid Build Coastguard Worker		close($camelcase_file);
1151*49cdfc7eSAndroid Build Coastguard Worker	}
1152*49cdfc7eSAndroid Build Coastguard Worker}
1153*49cdfc7eSAndroid Build Coastguard Worker
1154*49cdfc7eSAndroid Build Coastguard Workersub git_is_single_file {
1155*49cdfc7eSAndroid Build Coastguard Worker	my ($filename) = @_;
1156*49cdfc7eSAndroid Build Coastguard Worker
1157*49cdfc7eSAndroid Build Coastguard Worker	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1158*49cdfc7eSAndroid Build Coastguard Worker
1159*49cdfc7eSAndroid Build Coastguard Worker	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1160*49cdfc7eSAndroid Build Coastguard Worker	my $count = $output =~ tr/\n//;
1161*49cdfc7eSAndroid Build Coastguard Worker	return $count eq 1 && $output =~ m{^${filename}$};
1162*49cdfc7eSAndroid Build Coastguard Worker}
1163*49cdfc7eSAndroid Build Coastguard Worker
1164*49cdfc7eSAndroid Build Coastguard Workersub git_commit_info {
1165*49cdfc7eSAndroid Build Coastguard Worker	my ($commit, $id, $desc) = @_;
1166*49cdfc7eSAndroid Build Coastguard Worker
1167*49cdfc7eSAndroid Build Coastguard Worker	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
1168*49cdfc7eSAndroid Build Coastguard Worker
1169*49cdfc7eSAndroid Build Coastguard Worker	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1170*49cdfc7eSAndroid Build Coastguard Worker	$output =~ s/^\s*//gm;
1171*49cdfc7eSAndroid Build Coastguard Worker	my @lines = split("\n", $output);
1172*49cdfc7eSAndroid Build Coastguard Worker
1173*49cdfc7eSAndroid Build Coastguard Worker	return ($id, $desc) if ($#lines < 0);
1174*49cdfc7eSAndroid Build Coastguard Worker
1175*49cdfc7eSAndroid Build Coastguard Worker	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1176*49cdfc7eSAndroid Build Coastguard Worker# Maybe one day convert this block of bash into something that returns
1177*49cdfc7eSAndroid Build Coastguard Worker# all matching commit ids, but it's very slow...
1178*49cdfc7eSAndroid Build Coastguard Worker#
1179*49cdfc7eSAndroid Build Coastguard Worker#		echo "checking commits $1..."
1180*49cdfc7eSAndroid Build Coastguard Worker#		git rev-list --remotes | grep -i "^$1" |
1181*49cdfc7eSAndroid Build Coastguard Worker#		while read line ; do
1182*49cdfc7eSAndroid Build Coastguard Worker#		    git log --format='%H %s' -1 $line |
1183*49cdfc7eSAndroid Build Coastguard Worker#		    echo "commit $(cut -c 1-12,41-)"
1184*49cdfc7eSAndroid Build Coastguard Worker#		done
1185*49cdfc7eSAndroid Build Coastguard Worker	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
1186*49cdfc7eSAndroid Build Coastguard Worker		 $lines[0] =~ /^fatal: bad object $commit/) {
1187*49cdfc7eSAndroid Build Coastguard Worker		$id = undef;
1188*49cdfc7eSAndroid Build Coastguard Worker	} else {
1189*49cdfc7eSAndroid Build Coastguard Worker		$id = substr($lines[0], 0, 12);
1190*49cdfc7eSAndroid Build Coastguard Worker		$desc = substr($lines[0], 41);
1191*49cdfc7eSAndroid Build Coastguard Worker	}
1192*49cdfc7eSAndroid Build Coastguard Worker
1193*49cdfc7eSAndroid Build Coastguard Worker	return ($id, $desc);
1194*49cdfc7eSAndroid Build Coastguard Worker}
1195*49cdfc7eSAndroid Build Coastguard Worker
1196*49cdfc7eSAndroid Build Coastguard Worker$chk_signoff = 0 if ($file);
1197*49cdfc7eSAndroid Build Coastguard Worker
1198*49cdfc7eSAndroid Build Coastguard Workermy @rawlines = ();
1199*49cdfc7eSAndroid Build Coastguard Workermy @lines = ();
1200*49cdfc7eSAndroid Build Coastguard Workermy @fixed = ();
1201*49cdfc7eSAndroid Build Coastguard Workermy @fixed_inserted = ();
1202*49cdfc7eSAndroid Build Coastguard Workermy @fixed_deleted = ();
1203*49cdfc7eSAndroid Build Coastguard Workermy $fixlinenr = -1;
1204*49cdfc7eSAndroid Build Coastguard Worker
1205*49cdfc7eSAndroid Build Coastguard Worker# If input is git commits, extract all commits from the commit expressions.
1206*49cdfc7eSAndroid Build Coastguard Worker# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
1207*49cdfc7eSAndroid Build Coastguard Workerdie "$P: No git repository found\n" if ($git && !-e "$gitroot");
1208*49cdfc7eSAndroid Build Coastguard Worker
1209*49cdfc7eSAndroid Build Coastguard Workerif ($git) {
1210*49cdfc7eSAndroid Build Coastguard Worker	my @commits = ();
1211*49cdfc7eSAndroid Build Coastguard Worker	foreach my $commit_expr (@ARGV) {
1212*49cdfc7eSAndroid Build Coastguard Worker		my $git_range;
1213*49cdfc7eSAndroid Build Coastguard Worker		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
1214*49cdfc7eSAndroid Build Coastguard Worker			$git_range = "-$2 $1";
1215*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($commit_expr =~ m/\.\./) {
1216*49cdfc7eSAndroid Build Coastguard Worker			$git_range = "$commit_expr";
1217*49cdfc7eSAndroid Build Coastguard Worker		} else {
1218*49cdfc7eSAndroid Build Coastguard Worker			$git_range = "-1 $commit_expr";
1219*49cdfc7eSAndroid Build Coastguard Worker		}
1220*49cdfc7eSAndroid Build Coastguard Worker		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
1221*49cdfc7eSAndroid Build Coastguard Worker		foreach my $line (split(/\n/, $lines)) {
1222*49cdfc7eSAndroid Build Coastguard Worker			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
1223*49cdfc7eSAndroid Build Coastguard Worker			next if (!defined($1) || !defined($2));
1224*49cdfc7eSAndroid Build Coastguard Worker			my $sha1 = $1;
1225*49cdfc7eSAndroid Build Coastguard Worker			my $subject = $2;
1226*49cdfc7eSAndroid Build Coastguard Worker			unshift(@commits, $sha1);
1227*49cdfc7eSAndroid Build Coastguard Worker			$git_commits{$sha1} = $subject;
1228*49cdfc7eSAndroid Build Coastguard Worker		}
1229*49cdfc7eSAndroid Build Coastguard Worker	}
1230*49cdfc7eSAndroid Build Coastguard Worker	die "$P: no git commits after extraction!\n" if (@commits == 0);
1231*49cdfc7eSAndroid Build Coastguard Worker	@ARGV = @commits;
1232*49cdfc7eSAndroid Build Coastguard Worker}
1233*49cdfc7eSAndroid Build Coastguard Worker
1234*49cdfc7eSAndroid Build Coastguard Workermy $vname;
1235*49cdfc7eSAndroid Build Coastguard Worker$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
1236*49cdfc7eSAndroid Build Coastguard Workerfor my $filename (@ARGV) {
1237*49cdfc7eSAndroid Build Coastguard Worker	my $FILE;
1238*49cdfc7eSAndroid Build Coastguard Worker	my $is_git_file = git_is_single_file($filename);
1239*49cdfc7eSAndroid Build Coastguard Worker	my $oldfile = $file;
1240*49cdfc7eSAndroid Build Coastguard Worker	$file = 1 if ($is_git_file);
1241*49cdfc7eSAndroid Build Coastguard Worker	if ($git) {
1242*49cdfc7eSAndroid Build Coastguard Worker		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
1243*49cdfc7eSAndroid Build Coastguard Worker			die "$P: $filename: git format-patch failed - $!\n";
1244*49cdfc7eSAndroid Build Coastguard Worker	} elsif ($file) {
1245*49cdfc7eSAndroid Build Coastguard Worker		open($FILE, '-|', "diff -u /dev/null $filename") ||
1246*49cdfc7eSAndroid Build Coastguard Worker			die "$P: $filename: diff failed - $!\n";
1247*49cdfc7eSAndroid Build Coastguard Worker	} elsif ($filename eq '-') {
1248*49cdfc7eSAndroid Build Coastguard Worker		open($FILE, '<&STDIN');
1249*49cdfc7eSAndroid Build Coastguard Worker	} else {
1250*49cdfc7eSAndroid Build Coastguard Worker		open($FILE, '<', "$filename") ||
1251*49cdfc7eSAndroid Build Coastguard Worker			die "$P: $filename: open failed - $!\n";
1252*49cdfc7eSAndroid Build Coastguard Worker	}
1253*49cdfc7eSAndroid Build Coastguard Worker	if ($filename eq '-') {
1254*49cdfc7eSAndroid Build Coastguard Worker		$vname = 'Your patch';
1255*49cdfc7eSAndroid Build Coastguard Worker	} elsif ($git) {
1256*49cdfc7eSAndroid Build Coastguard Worker		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1257*49cdfc7eSAndroid Build Coastguard Worker	} else {
1258*49cdfc7eSAndroid Build Coastguard Worker		$vname = $filename;
1259*49cdfc7eSAndroid Build Coastguard Worker	}
1260*49cdfc7eSAndroid Build Coastguard Worker	while (<$FILE>) {
1261*49cdfc7eSAndroid Build Coastguard Worker		chomp;
1262*49cdfc7eSAndroid Build Coastguard Worker		push(@rawlines, $_);
1263*49cdfc7eSAndroid Build Coastguard Worker		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
1264*49cdfc7eSAndroid Build Coastguard Worker	}
1265*49cdfc7eSAndroid Build Coastguard Worker	close($FILE);
1266*49cdfc7eSAndroid Build Coastguard Worker
1267*49cdfc7eSAndroid Build Coastguard Worker	if ($#ARGV > 0 && $quiet == 0) {
1268*49cdfc7eSAndroid Build Coastguard Worker		print '-' x length($vname) . "\n";
1269*49cdfc7eSAndroid Build Coastguard Worker		print "$vname\n";
1270*49cdfc7eSAndroid Build Coastguard Worker		print '-' x length($vname) . "\n";
1271*49cdfc7eSAndroid Build Coastguard Worker	}
1272*49cdfc7eSAndroid Build Coastguard Worker
1273*49cdfc7eSAndroid Build Coastguard Worker	if (!process($filename)) {
1274*49cdfc7eSAndroid Build Coastguard Worker		$exit = 1;
1275*49cdfc7eSAndroid Build Coastguard Worker	}
1276*49cdfc7eSAndroid Build Coastguard Worker	@rawlines = ();
1277*49cdfc7eSAndroid Build Coastguard Worker	@lines = ();
1278*49cdfc7eSAndroid Build Coastguard Worker	@fixed = ();
1279*49cdfc7eSAndroid Build Coastguard Worker	@fixed_inserted = ();
1280*49cdfc7eSAndroid Build Coastguard Worker	@fixed_deleted = ();
1281*49cdfc7eSAndroid Build Coastguard Worker	$fixlinenr = -1;
1282*49cdfc7eSAndroid Build Coastguard Worker	@modifierListFile = ();
1283*49cdfc7eSAndroid Build Coastguard Worker	@typeListFile = ();
1284*49cdfc7eSAndroid Build Coastguard Worker	build_types();
1285*49cdfc7eSAndroid Build Coastguard Worker	$file = $oldfile if ($is_git_file);
1286*49cdfc7eSAndroid Build Coastguard Worker}
1287*49cdfc7eSAndroid Build Coastguard Worker
1288*49cdfc7eSAndroid Build Coastguard Workerif (!$quiet) {
1289*49cdfc7eSAndroid Build Coastguard Worker	hash_show_words(\%use_type, "Used");
1290*49cdfc7eSAndroid Build Coastguard Worker	hash_show_words(\%ignore_type, "Ignored");
1291*49cdfc7eSAndroid Build Coastguard Worker
1292*49cdfc7eSAndroid Build Coastguard Worker	if (!$perl_version_ok) {
1293*49cdfc7eSAndroid Build Coastguard Worker		print << "EOM"
1294*49cdfc7eSAndroid Build Coastguard Worker
1295*49cdfc7eSAndroid Build Coastguard WorkerNOTE: perl $^V is not modern enough to detect all possible issues.
1296*49cdfc7eSAndroid Build Coastguard Worker      An upgrade to at least perl $minimum_perl_version is suggested.
1297*49cdfc7eSAndroid Build Coastguard WorkerEOM
1298*49cdfc7eSAndroid Build Coastguard Worker	}
1299*49cdfc7eSAndroid Build Coastguard Worker	if ($exit) {
1300*49cdfc7eSAndroid Build Coastguard Worker		print << "EOM"
1301*49cdfc7eSAndroid Build Coastguard Worker
1302*49cdfc7eSAndroid Build Coastguard WorkerNOTE: If any of the errors are false positives, please report
1303*49cdfc7eSAndroid Build Coastguard Worker      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1304*49cdfc7eSAndroid Build Coastguard WorkerEOM
1305*49cdfc7eSAndroid Build Coastguard Worker	}
1306*49cdfc7eSAndroid Build Coastguard Worker}
1307*49cdfc7eSAndroid Build Coastguard Worker
1308*49cdfc7eSAndroid Build Coastguard Workerexit($exit);
1309*49cdfc7eSAndroid Build Coastguard Worker
1310*49cdfc7eSAndroid Build Coastguard Workersub top_of_kernel_tree {
1311*49cdfc7eSAndroid Build Coastguard Worker	my ($root) = @_;
1312*49cdfc7eSAndroid Build Coastguard Worker
1313*49cdfc7eSAndroid Build Coastguard Worker	my @tree_check = (
1314*49cdfc7eSAndroid Build Coastguard Worker		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1315*49cdfc7eSAndroid Build Coastguard Worker		"README", "Documentation", "arch", "include", "drivers",
1316*49cdfc7eSAndroid Build Coastguard Worker		"fs", "init", "ipc", "kernel", "lib", "scripts",
1317*49cdfc7eSAndroid Build Coastguard Worker	);
1318*49cdfc7eSAndroid Build Coastguard Worker
1319*49cdfc7eSAndroid Build Coastguard Worker	foreach my $check (@tree_check) {
1320*49cdfc7eSAndroid Build Coastguard Worker		if (! -e $root . '/' . $check) {
1321*49cdfc7eSAndroid Build Coastguard Worker			return 0;
1322*49cdfc7eSAndroid Build Coastguard Worker		}
1323*49cdfc7eSAndroid Build Coastguard Worker	}
1324*49cdfc7eSAndroid Build Coastguard Worker	return 1;
1325*49cdfc7eSAndroid Build Coastguard Worker}
1326*49cdfc7eSAndroid Build Coastguard Worker
1327*49cdfc7eSAndroid Build Coastguard Workersub parse_email {
1328*49cdfc7eSAndroid Build Coastguard Worker	my ($formatted_email) = @_;
1329*49cdfc7eSAndroid Build Coastguard Worker
1330*49cdfc7eSAndroid Build Coastguard Worker	my $name = "";
1331*49cdfc7eSAndroid Build Coastguard Worker	my $quoted = "";
1332*49cdfc7eSAndroid Build Coastguard Worker	my $name_comment = "";
1333*49cdfc7eSAndroid Build Coastguard Worker	my $address = "";
1334*49cdfc7eSAndroid Build Coastguard Worker	my $comment = "";
1335*49cdfc7eSAndroid Build Coastguard Worker
1336*49cdfc7eSAndroid Build Coastguard Worker	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1337*49cdfc7eSAndroid Build Coastguard Worker		$name = $1;
1338*49cdfc7eSAndroid Build Coastguard Worker		$address = $2;
1339*49cdfc7eSAndroid Build Coastguard Worker		$comment = $3 if defined $3;
1340*49cdfc7eSAndroid Build Coastguard Worker	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1341*49cdfc7eSAndroid Build Coastguard Worker		$address = $1;
1342*49cdfc7eSAndroid Build Coastguard Worker		$comment = $2 if defined $2;
1343*49cdfc7eSAndroid Build Coastguard Worker	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1344*49cdfc7eSAndroid Build Coastguard Worker		$address = $1;
1345*49cdfc7eSAndroid Build Coastguard Worker		$comment = $2 if defined $2;
1346*49cdfc7eSAndroid Build Coastguard Worker		$formatted_email =~ s/\Q$address\E.*$//;
1347*49cdfc7eSAndroid Build Coastguard Worker		$name = $formatted_email;
1348*49cdfc7eSAndroid Build Coastguard Worker		$name = trim($name);
1349*49cdfc7eSAndroid Build Coastguard Worker		$name =~ s/^\"|\"$//g;
1350*49cdfc7eSAndroid Build Coastguard Worker		# If there's a name left after stripping spaces and
1351*49cdfc7eSAndroid Build Coastguard Worker		# leading quotes, and the address doesn't have both
1352*49cdfc7eSAndroid Build Coastguard Worker		# leading and trailing angle brackets, the address
1353*49cdfc7eSAndroid Build Coastguard Worker		# is invalid. ie:
1354*49cdfc7eSAndroid Build Coastguard Worker		#   "joe smith [email protected]" bad
1355*49cdfc7eSAndroid Build Coastguard Worker		#   "joe smith <[email protected]" bad
1356*49cdfc7eSAndroid Build Coastguard Worker		if ($name ne "" && $address !~ /^<[^>]+>$/) {
1357*49cdfc7eSAndroid Build Coastguard Worker			$name = "";
1358*49cdfc7eSAndroid Build Coastguard Worker			$address = "";
1359*49cdfc7eSAndroid Build Coastguard Worker			$comment = "";
1360*49cdfc7eSAndroid Build Coastguard Worker		}
1361*49cdfc7eSAndroid Build Coastguard Worker	}
1362*49cdfc7eSAndroid Build Coastguard Worker
1363*49cdfc7eSAndroid Build Coastguard Worker	# Extract comments from names excluding quoted parts
1364*49cdfc7eSAndroid Build Coastguard Worker	# "John D. (Doe)" - Do not extract
1365*49cdfc7eSAndroid Build Coastguard Worker	if ($name =~ s/\"(.+)\"//) {
1366*49cdfc7eSAndroid Build Coastguard Worker		$quoted = $1;
1367*49cdfc7eSAndroid Build Coastguard Worker	}
1368*49cdfc7eSAndroid Build Coastguard Worker	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1369*49cdfc7eSAndroid Build Coastguard Worker		$name_comment .= trim($1);
1370*49cdfc7eSAndroid Build Coastguard Worker	}
1371*49cdfc7eSAndroid Build Coastguard Worker	$name =~ s/^[ \"]+|[ \"]+$//g;
1372*49cdfc7eSAndroid Build Coastguard Worker	$name = trim("$quoted $name");
1373*49cdfc7eSAndroid Build Coastguard Worker
1374*49cdfc7eSAndroid Build Coastguard Worker	$address = trim($address);
1375*49cdfc7eSAndroid Build Coastguard Worker	$address =~ s/^\<|\>$//g;
1376*49cdfc7eSAndroid Build Coastguard Worker	$comment = trim($comment);
1377*49cdfc7eSAndroid Build Coastguard Worker
1378*49cdfc7eSAndroid Build Coastguard Worker	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1379*49cdfc7eSAndroid Build Coastguard Worker		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1380*49cdfc7eSAndroid Build Coastguard Worker		$name = "\"$name\"";
1381*49cdfc7eSAndroid Build Coastguard Worker	}
1382*49cdfc7eSAndroid Build Coastguard Worker
1383*49cdfc7eSAndroid Build Coastguard Worker	return ($name, $name_comment, $address, $comment);
1384*49cdfc7eSAndroid Build Coastguard Worker}
1385*49cdfc7eSAndroid Build Coastguard Worker
1386*49cdfc7eSAndroid Build Coastguard Workersub format_email {
1387*49cdfc7eSAndroid Build Coastguard Worker	my ($name, $name_comment, $address, $comment) = @_;
1388*49cdfc7eSAndroid Build Coastguard Worker
1389*49cdfc7eSAndroid Build Coastguard Worker	my $formatted_email;
1390*49cdfc7eSAndroid Build Coastguard Worker
1391*49cdfc7eSAndroid Build Coastguard Worker	$name =~ s/^[ \"]+|[ \"]+$//g;
1392*49cdfc7eSAndroid Build Coastguard Worker	$address = trim($address);
1393*49cdfc7eSAndroid Build Coastguard Worker	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
1394*49cdfc7eSAndroid Build Coastguard Worker
1395*49cdfc7eSAndroid Build Coastguard Worker	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1396*49cdfc7eSAndroid Build Coastguard Worker		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1397*49cdfc7eSAndroid Build Coastguard Worker		$name = "\"$name\"";
1398*49cdfc7eSAndroid Build Coastguard Worker	}
1399*49cdfc7eSAndroid Build Coastguard Worker
1400*49cdfc7eSAndroid Build Coastguard Worker	$name_comment = trim($name_comment);
1401*49cdfc7eSAndroid Build Coastguard Worker	$name_comment = " $name_comment" if ($name_comment ne "");
1402*49cdfc7eSAndroid Build Coastguard Worker	$comment = trim($comment);
1403*49cdfc7eSAndroid Build Coastguard Worker	$comment = " $comment" if ($comment ne "");
1404*49cdfc7eSAndroid Build Coastguard Worker
1405*49cdfc7eSAndroid Build Coastguard Worker	if ("$name" eq "") {
1406*49cdfc7eSAndroid Build Coastguard Worker		$formatted_email = "$address";
1407*49cdfc7eSAndroid Build Coastguard Worker	} else {
1408*49cdfc7eSAndroid Build Coastguard Worker		$formatted_email = "$name$name_comment <$address>";
1409*49cdfc7eSAndroid Build Coastguard Worker	}
1410*49cdfc7eSAndroid Build Coastguard Worker	$formatted_email .= "$comment";
1411*49cdfc7eSAndroid Build Coastguard Worker	return $formatted_email;
1412*49cdfc7eSAndroid Build Coastguard Worker}
1413*49cdfc7eSAndroid Build Coastguard Worker
1414*49cdfc7eSAndroid Build Coastguard Workersub reformat_email {
1415*49cdfc7eSAndroid Build Coastguard Worker	my ($email) = @_;
1416*49cdfc7eSAndroid Build Coastguard Worker
1417*49cdfc7eSAndroid Build Coastguard Worker	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
1418*49cdfc7eSAndroid Build Coastguard Worker	return format_email($email_name, $name_comment, $email_address, $comment);
1419*49cdfc7eSAndroid Build Coastguard Worker}
1420*49cdfc7eSAndroid Build Coastguard Worker
1421*49cdfc7eSAndroid Build Coastguard Workersub same_email_addresses {
1422*49cdfc7eSAndroid Build Coastguard Worker	my ($email1, $email2) = @_;
1423*49cdfc7eSAndroid Build Coastguard Worker
1424*49cdfc7eSAndroid Build Coastguard Worker	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1425*49cdfc7eSAndroid Build Coastguard Worker	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1426*49cdfc7eSAndroid Build Coastguard Worker
1427*49cdfc7eSAndroid Build Coastguard Worker	return $email1_name eq $email2_name &&
1428*49cdfc7eSAndroid Build Coastguard Worker	       $email1_address eq $email2_address &&
1429*49cdfc7eSAndroid Build Coastguard Worker	       $name1_comment eq $name2_comment &&
1430*49cdfc7eSAndroid Build Coastguard Worker	       $comment1 eq $comment2;
1431*49cdfc7eSAndroid Build Coastguard Worker}
1432*49cdfc7eSAndroid Build Coastguard Worker
1433*49cdfc7eSAndroid Build Coastguard Workersub which {
1434*49cdfc7eSAndroid Build Coastguard Worker	my ($bin) = @_;
1435*49cdfc7eSAndroid Build Coastguard Worker
1436*49cdfc7eSAndroid Build Coastguard Worker	foreach my $path (split(/:/, $ENV{PATH})) {
1437*49cdfc7eSAndroid Build Coastguard Worker		if (-e "$path/$bin") {
1438*49cdfc7eSAndroid Build Coastguard Worker			return "$path/$bin";
1439*49cdfc7eSAndroid Build Coastguard Worker		}
1440*49cdfc7eSAndroid Build Coastguard Worker	}
1441*49cdfc7eSAndroid Build Coastguard Worker
1442*49cdfc7eSAndroid Build Coastguard Worker	return "";
1443*49cdfc7eSAndroid Build Coastguard Worker}
1444*49cdfc7eSAndroid Build Coastguard Worker
1445*49cdfc7eSAndroid Build Coastguard Workersub which_conf {
1446*49cdfc7eSAndroid Build Coastguard Worker	my ($conf) = @_;
1447*49cdfc7eSAndroid Build Coastguard Worker
1448*49cdfc7eSAndroid Build Coastguard Worker	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1449*49cdfc7eSAndroid Build Coastguard Worker		if (-e "$path/$conf") {
1450*49cdfc7eSAndroid Build Coastguard Worker			return "$path/$conf";
1451*49cdfc7eSAndroid Build Coastguard Worker		}
1452*49cdfc7eSAndroid Build Coastguard Worker	}
1453*49cdfc7eSAndroid Build Coastguard Worker
1454*49cdfc7eSAndroid Build Coastguard Worker	return "";
1455*49cdfc7eSAndroid Build Coastguard Worker}
1456*49cdfc7eSAndroid Build Coastguard Worker
1457*49cdfc7eSAndroid Build Coastguard Workersub expand_tabs {
1458*49cdfc7eSAndroid Build Coastguard Worker	my ($str) = @_;
1459*49cdfc7eSAndroid Build Coastguard Worker
1460*49cdfc7eSAndroid Build Coastguard Worker	my $res = '';
1461*49cdfc7eSAndroid Build Coastguard Worker	my $n = 0;
1462*49cdfc7eSAndroid Build Coastguard Worker	for my $c (split(//, $str)) {
1463*49cdfc7eSAndroid Build Coastguard Worker		if ($c eq "\t") {
1464*49cdfc7eSAndroid Build Coastguard Worker			$res .= ' ';
1465*49cdfc7eSAndroid Build Coastguard Worker			$n++;
1466*49cdfc7eSAndroid Build Coastguard Worker			for (; ($n % $tabsize) != 0; $n++) {
1467*49cdfc7eSAndroid Build Coastguard Worker				$res .= ' ';
1468*49cdfc7eSAndroid Build Coastguard Worker			}
1469*49cdfc7eSAndroid Build Coastguard Worker			next;
1470*49cdfc7eSAndroid Build Coastguard Worker		}
1471*49cdfc7eSAndroid Build Coastguard Worker		$res .= $c;
1472*49cdfc7eSAndroid Build Coastguard Worker		$n++;
1473*49cdfc7eSAndroid Build Coastguard Worker	}
1474*49cdfc7eSAndroid Build Coastguard Worker
1475*49cdfc7eSAndroid Build Coastguard Worker	return $res;
1476*49cdfc7eSAndroid Build Coastguard Worker}
1477*49cdfc7eSAndroid Build Coastguard Workersub copy_spacing {
1478*49cdfc7eSAndroid Build Coastguard Worker	(my $res = shift) =~ tr/\t/ /c;
1479*49cdfc7eSAndroid Build Coastguard Worker	return $res;
1480*49cdfc7eSAndroid Build Coastguard Worker}
1481*49cdfc7eSAndroid Build Coastguard Worker
1482*49cdfc7eSAndroid Build Coastguard Workersub line_stats {
1483*49cdfc7eSAndroid Build Coastguard Worker	my ($line) = @_;
1484*49cdfc7eSAndroid Build Coastguard Worker
1485*49cdfc7eSAndroid Build Coastguard Worker	# Drop the diff line leader and expand tabs
1486*49cdfc7eSAndroid Build Coastguard Worker	$line =~ s/^.//;
1487*49cdfc7eSAndroid Build Coastguard Worker	$line = expand_tabs($line);
1488*49cdfc7eSAndroid Build Coastguard Worker
1489*49cdfc7eSAndroid Build Coastguard Worker	# Pick the indent from the front of the line.
1490*49cdfc7eSAndroid Build Coastguard Worker	my ($white) = ($line =~ /^(\s*)/);
1491*49cdfc7eSAndroid Build Coastguard Worker
1492*49cdfc7eSAndroid Build Coastguard Worker	return (length($line), length($white));
1493*49cdfc7eSAndroid Build Coastguard Worker}
1494*49cdfc7eSAndroid Build Coastguard Worker
1495*49cdfc7eSAndroid Build Coastguard Workermy $sanitise_quote = '';
1496*49cdfc7eSAndroid Build Coastguard Worker
1497*49cdfc7eSAndroid Build Coastguard Workersub sanitise_line_reset {
1498*49cdfc7eSAndroid Build Coastguard Worker	my ($in_comment) = @_;
1499*49cdfc7eSAndroid Build Coastguard Worker
1500*49cdfc7eSAndroid Build Coastguard Worker	if ($in_comment) {
1501*49cdfc7eSAndroid Build Coastguard Worker		$sanitise_quote = '*/';
1502*49cdfc7eSAndroid Build Coastguard Worker	} else {
1503*49cdfc7eSAndroid Build Coastguard Worker		$sanitise_quote = '';
1504*49cdfc7eSAndroid Build Coastguard Worker	}
1505*49cdfc7eSAndroid Build Coastguard Worker}
1506*49cdfc7eSAndroid Build Coastguard Workersub sanitise_line {
1507*49cdfc7eSAndroid Build Coastguard Worker	my ($line) = @_;
1508*49cdfc7eSAndroid Build Coastguard Worker
1509*49cdfc7eSAndroid Build Coastguard Worker	my $res = '';
1510*49cdfc7eSAndroid Build Coastguard Worker	my $l = '';
1511*49cdfc7eSAndroid Build Coastguard Worker
1512*49cdfc7eSAndroid Build Coastguard Worker	my $qlen = 0;
1513*49cdfc7eSAndroid Build Coastguard Worker	my $off = 0;
1514*49cdfc7eSAndroid Build Coastguard Worker	my $c;
1515*49cdfc7eSAndroid Build Coastguard Worker
1516*49cdfc7eSAndroid Build Coastguard Worker	# Always copy over the diff marker.
1517*49cdfc7eSAndroid Build Coastguard Worker	$res = substr($line, 0, 1);
1518*49cdfc7eSAndroid Build Coastguard Worker
1519*49cdfc7eSAndroid Build Coastguard Worker	for ($off = 1; $off < length($line); $off++) {
1520*49cdfc7eSAndroid Build Coastguard Worker		$c = substr($line, $off, 1);
1521*49cdfc7eSAndroid Build Coastguard Worker
1522*49cdfc7eSAndroid Build Coastguard Worker		# Comments we are whacking completely including the begin
1523*49cdfc7eSAndroid Build Coastguard Worker		# and end, all to $;.
1524*49cdfc7eSAndroid Build Coastguard Worker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1525*49cdfc7eSAndroid Build Coastguard Worker			$sanitise_quote = '*/';
1526*49cdfc7eSAndroid Build Coastguard Worker
1527*49cdfc7eSAndroid Build Coastguard Worker			substr($res, $off, 2, "$;$;");
1528*49cdfc7eSAndroid Build Coastguard Worker			$off++;
1529*49cdfc7eSAndroid Build Coastguard Worker			next;
1530*49cdfc7eSAndroid Build Coastguard Worker		}
1531*49cdfc7eSAndroid Build Coastguard Worker		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1532*49cdfc7eSAndroid Build Coastguard Worker			$sanitise_quote = '';
1533*49cdfc7eSAndroid Build Coastguard Worker			substr($res, $off, 2, "$;$;");
1534*49cdfc7eSAndroid Build Coastguard Worker			$off++;
1535*49cdfc7eSAndroid Build Coastguard Worker			next;
1536*49cdfc7eSAndroid Build Coastguard Worker		}
1537*49cdfc7eSAndroid Build Coastguard Worker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1538*49cdfc7eSAndroid Build Coastguard Worker			$sanitise_quote = '//';
1539*49cdfc7eSAndroid Build Coastguard Worker
1540*49cdfc7eSAndroid Build Coastguard Worker			substr($res, $off, 2, $sanitise_quote);
1541*49cdfc7eSAndroid Build Coastguard Worker			$off++;
1542*49cdfc7eSAndroid Build Coastguard Worker			next;
1543*49cdfc7eSAndroid Build Coastguard Worker		}
1544*49cdfc7eSAndroid Build Coastguard Worker
1545*49cdfc7eSAndroid Build Coastguard Worker		# A \ in a string means ignore the next character.
1546*49cdfc7eSAndroid Build Coastguard Worker		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1547*49cdfc7eSAndroid Build Coastguard Worker		    $c eq "\\") {
1548*49cdfc7eSAndroid Build Coastguard Worker			substr($res, $off, 2, 'XX');
1549*49cdfc7eSAndroid Build Coastguard Worker			$off++;
1550*49cdfc7eSAndroid Build Coastguard Worker			next;
1551*49cdfc7eSAndroid Build Coastguard Worker		}
1552*49cdfc7eSAndroid Build Coastguard Worker		# Regular quotes.
1553*49cdfc7eSAndroid Build Coastguard Worker		if ($c eq "'" || $c eq '"') {
1554*49cdfc7eSAndroid Build Coastguard Worker			if ($sanitise_quote eq '') {
1555*49cdfc7eSAndroid Build Coastguard Worker				$sanitise_quote = $c;
1556*49cdfc7eSAndroid Build Coastguard Worker
1557*49cdfc7eSAndroid Build Coastguard Worker				substr($res, $off, 1, $c);
1558*49cdfc7eSAndroid Build Coastguard Worker				next;
1559*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($sanitise_quote eq $c) {
1560*49cdfc7eSAndroid Build Coastguard Worker				$sanitise_quote = '';
1561*49cdfc7eSAndroid Build Coastguard Worker			}
1562*49cdfc7eSAndroid Build Coastguard Worker		}
1563*49cdfc7eSAndroid Build Coastguard Worker
1564*49cdfc7eSAndroid Build Coastguard Worker		#print "c<$c> SQ<$sanitise_quote>\n";
1565*49cdfc7eSAndroid Build Coastguard Worker		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1566*49cdfc7eSAndroid Build Coastguard Worker			substr($res, $off, 1, $;);
1567*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1568*49cdfc7eSAndroid Build Coastguard Worker			substr($res, $off, 1, $;);
1569*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1570*49cdfc7eSAndroid Build Coastguard Worker			substr($res, $off, 1, 'X');
1571*49cdfc7eSAndroid Build Coastguard Worker		} else {
1572*49cdfc7eSAndroid Build Coastguard Worker			substr($res, $off, 1, $c);
1573*49cdfc7eSAndroid Build Coastguard Worker		}
1574*49cdfc7eSAndroid Build Coastguard Worker	}
1575*49cdfc7eSAndroid Build Coastguard Worker
1576*49cdfc7eSAndroid Build Coastguard Worker	if ($sanitise_quote eq '//') {
1577*49cdfc7eSAndroid Build Coastguard Worker		$sanitise_quote = '';
1578*49cdfc7eSAndroid Build Coastguard Worker	}
1579*49cdfc7eSAndroid Build Coastguard Worker
1580*49cdfc7eSAndroid Build Coastguard Worker	# The pathname on a #include may be surrounded by '<' and '>'.
1581*49cdfc7eSAndroid Build Coastguard Worker	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1582*49cdfc7eSAndroid Build Coastguard Worker		my $clean = 'X' x length($1);
1583*49cdfc7eSAndroid Build Coastguard Worker		$res =~ s@\<.*\>@<$clean>@;
1584*49cdfc7eSAndroid Build Coastguard Worker
1585*49cdfc7eSAndroid Build Coastguard Worker	# The whole of a #error is a string.
1586*49cdfc7eSAndroid Build Coastguard Worker	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1587*49cdfc7eSAndroid Build Coastguard Worker		my $clean = 'X' x length($1);
1588*49cdfc7eSAndroid Build Coastguard Worker		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1589*49cdfc7eSAndroid Build Coastguard Worker	}
1590*49cdfc7eSAndroid Build Coastguard Worker
1591*49cdfc7eSAndroid Build Coastguard Worker	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1592*49cdfc7eSAndroid Build Coastguard Worker		my $match = $1;
1593*49cdfc7eSAndroid Build Coastguard Worker		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1594*49cdfc7eSAndroid Build Coastguard Worker	}
1595*49cdfc7eSAndroid Build Coastguard Worker
1596*49cdfc7eSAndroid Build Coastguard Worker	return $res;
1597*49cdfc7eSAndroid Build Coastguard Worker}
1598*49cdfc7eSAndroid Build Coastguard Worker
1599*49cdfc7eSAndroid Build Coastguard Workersub get_quoted_string {
1600*49cdfc7eSAndroid Build Coastguard Worker	my ($line, $rawline) = @_;
1601*49cdfc7eSAndroid Build Coastguard Worker
1602*49cdfc7eSAndroid Build Coastguard Worker	return "" if (!defined($line) || !defined($rawline));
1603*49cdfc7eSAndroid Build Coastguard Worker	return "" if ($line !~ m/($String)/g);
1604*49cdfc7eSAndroid Build Coastguard Worker	return substr($rawline, $-[0], $+[0] - $-[0]);
1605*49cdfc7eSAndroid Build Coastguard Worker}
1606*49cdfc7eSAndroid Build Coastguard Worker
1607*49cdfc7eSAndroid Build Coastguard Workersub ctx_statement_block {
1608*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $remain, $off) = @_;
1609*49cdfc7eSAndroid Build Coastguard Worker	my $line = $linenr - 1;
1610*49cdfc7eSAndroid Build Coastguard Worker	my $blk = '';
1611*49cdfc7eSAndroid Build Coastguard Worker	my $soff = $off;
1612*49cdfc7eSAndroid Build Coastguard Worker	my $coff = $off - 1;
1613*49cdfc7eSAndroid Build Coastguard Worker	my $coff_set = 0;
1614*49cdfc7eSAndroid Build Coastguard Worker
1615*49cdfc7eSAndroid Build Coastguard Worker	my $loff = 0;
1616*49cdfc7eSAndroid Build Coastguard Worker
1617*49cdfc7eSAndroid Build Coastguard Worker	my $type = '';
1618*49cdfc7eSAndroid Build Coastguard Worker	my $level = 0;
1619*49cdfc7eSAndroid Build Coastguard Worker	my @stack = ();
1620*49cdfc7eSAndroid Build Coastguard Worker	my $p;
1621*49cdfc7eSAndroid Build Coastguard Worker	my $c;
1622*49cdfc7eSAndroid Build Coastguard Worker	my $len = 0;
1623*49cdfc7eSAndroid Build Coastguard Worker
1624*49cdfc7eSAndroid Build Coastguard Worker	my $remainder;
1625*49cdfc7eSAndroid Build Coastguard Worker	while (1) {
1626*49cdfc7eSAndroid Build Coastguard Worker		@stack = (['', 0]) if ($#stack == -1);
1627*49cdfc7eSAndroid Build Coastguard Worker
1628*49cdfc7eSAndroid Build Coastguard Worker		#warn "CSB: blk<$blk> remain<$remain>\n";
1629*49cdfc7eSAndroid Build Coastguard Worker		# If we are about to drop off the end, pull in more
1630*49cdfc7eSAndroid Build Coastguard Worker		# context.
1631*49cdfc7eSAndroid Build Coastguard Worker		if ($off >= $len) {
1632*49cdfc7eSAndroid Build Coastguard Worker			for (; $remain > 0; $line++) {
1633*49cdfc7eSAndroid Build Coastguard Worker				last if (!defined $lines[$line]);
1634*49cdfc7eSAndroid Build Coastguard Worker				next if ($lines[$line] =~ /^-/);
1635*49cdfc7eSAndroid Build Coastguard Worker				$remain--;
1636*49cdfc7eSAndroid Build Coastguard Worker				$loff = $len;
1637*49cdfc7eSAndroid Build Coastguard Worker				$blk .= $lines[$line] . "\n";
1638*49cdfc7eSAndroid Build Coastguard Worker				$len = length($blk);
1639*49cdfc7eSAndroid Build Coastguard Worker				$line++;
1640*49cdfc7eSAndroid Build Coastguard Worker				last;
1641*49cdfc7eSAndroid Build Coastguard Worker			}
1642*49cdfc7eSAndroid Build Coastguard Worker			# Bail if there is no further context.
1643*49cdfc7eSAndroid Build Coastguard Worker			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
1644*49cdfc7eSAndroid Build Coastguard Worker			if ($off >= $len) {
1645*49cdfc7eSAndroid Build Coastguard Worker				last;
1646*49cdfc7eSAndroid Build Coastguard Worker			}
1647*49cdfc7eSAndroid Build Coastguard Worker			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1648*49cdfc7eSAndroid Build Coastguard Worker				$level++;
1649*49cdfc7eSAndroid Build Coastguard Worker				$type = '#';
1650*49cdfc7eSAndroid Build Coastguard Worker			}
1651*49cdfc7eSAndroid Build Coastguard Worker		}
1652*49cdfc7eSAndroid Build Coastguard Worker		$p = $c;
1653*49cdfc7eSAndroid Build Coastguard Worker		$c = substr($blk, $off, 1);
1654*49cdfc7eSAndroid Build Coastguard Worker		$remainder = substr($blk, $off);
1655*49cdfc7eSAndroid Build Coastguard Worker
1656*49cdfc7eSAndroid Build Coastguard Worker		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
1657*49cdfc7eSAndroid Build Coastguard Worker
1658*49cdfc7eSAndroid Build Coastguard Worker		# Handle nested #if/#else.
1659*49cdfc7eSAndroid Build Coastguard Worker		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1660*49cdfc7eSAndroid Build Coastguard Worker			push(@stack, [ $type, $level ]);
1661*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1662*49cdfc7eSAndroid Build Coastguard Worker			($type, $level) = @{$stack[$#stack - 1]};
1663*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($remainder =~ /^#\s*endif\b/) {
1664*49cdfc7eSAndroid Build Coastguard Worker			($type, $level) = @{pop(@stack)};
1665*49cdfc7eSAndroid Build Coastguard Worker		}
1666*49cdfc7eSAndroid Build Coastguard Worker
1667*49cdfc7eSAndroid Build Coastguard Worker		# Statement ends at the ';' or a close '}' at the
1668*49cdfc7eSAndroid Build Coastguard Worker		# outermost level.
1669*49cdfc7eSAndroid Build Coastguard Worker		if ($level == 0 && $c eq ';') {
1670*49cdfc7eSAndroid Build Coastguard Worker			last;
1671*49cdfc7eSAndroid Build Coastguard Worker		}
1672*49cdfc7eSAndroid Build Coastguard Worker
1673*49cdfc7eSAndroid Build Coastguard Worker		# An else is really a conditional as long as its not else if
1674*49cdfc7eSAndroid Build Coastguard Worker		if ($level == 0 && $coff_set == 0 &&
1675*49cdfc7eSAndroid Build Coastguard Worker				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1676*49cdfc7eSAndroid Build Coastguard Worker				$remainder =~ /^(else)(?:\s|{)/ &&
1677*49cdfc7eSAndroid Build Coastguard Worker				$remainder !~ /^else\s+if\b/) {
1678*49cdfc7eSAndroid Build Coastguard Worker			$coff = $off + length($1) - 1;
1679*49cdfc7eSAndroid Build Coastguard Worker			$coff_set = 1;
1680*49cdfc7eSAndroid Build Coastguard Worker			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1681*49cdfc7eSAndroid Build Coastguard Worker			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
1682*49cdfc7eSAndroid Build Coastguard Worker		}
1683*49cdfc7eSAndroid Build Coastguard Worker
1684*49cdfc7eSAndroid Build Coastguard Worker		if (($type eq '' || $type eq '(') && $c eq '(') {
1685*49cdfc7eSAndroid Build Coastguard Worker			$level++;
1686*49cdfc7eSAndroid Build Coastguard Worker			$type = '(';
1687*49cdfc7eSAndroid Build Coastguard Worker		}
1688*49cdfc7eSAndroid Build Coastguard Worker		if ($type eq '(' && $c eq ')') {
1689*49cdfc7eSAndroid Build Coastguard Worker			$level--;
1690*49cdfc7eSAndroid Build Coastguard Worker			$type = ($level != 0)? '(' : '';
1691*49cdfc7eSAndroid Build Coastguard Worker
1692*49cdfc7eSAndroid Build Coastguard Worker			if ($level == 0 && $coff < $soff) {
1693*49cdfc7eSAndroid Build Coastguard Worker				$coff = $off;
1694*49cdfc7eSAndroid Build Coastguard Worker				$coff_set = 1;
1695*49cdfc7eSAndroid Build Coastguard Worker				#warn "CSB: mark coff<$coff>\n";
1696*49cdfc7eSAndroid Build Coastguard Worker			}
1697*49cdfc7eSAndroid Build Coastguard Worker		}
1698*49cdfc7eSAndroid Build Coastguard Worker		if (($type eq '' || $type eq '{') && $c eq '{') {
1699*49cdfc7eSAndroid Build Coastguard Worker			$level++;
1700*49cdfc7eSAndroid Build Coastguard Worker			$type = '{';
1701*49cdfc7eSAndroid Build Coastguard Worker		}
1702*49cdfc7eSAndroid Build Coastguard Worker		if ($type eq '{' && $c eq '}') {
1703*49cdfc7eSAndroid Build Coastguard Worker			$level--;
1704*49cdfc7eSAndroid Build Coastguard Worker			$type = ($level != 0)? '{' : '';
1705*49cdfc7eSAndroid Build Coastguard Worker
1706*49cdfc7eSAndroid Build Coastguard Worker			if ($level == 0) {
1707*49cdfc7eSAndroid Build Coastguard Worker				if (substr($blk, $off + 1, 1) eq ';') {
1708*49cdfc7eSAndroid Build Coastguard Worker					$off++;
1709*49cdfc7eSAndroid Build Coastguard Worker				}
1710*49cdfc7eSAndroid Build Coastguard Worker				last;
1711*49cdfc7eSAndroid Build Coastguard Worker			}
1712*49cdfc7eSAndroid Build Coastguard Worker		}
1713*49cdfc7eSAndroid Build Coastguard Worker		# Preprocessor commands end at the newline unless escaped.
1714*49cdfc7eSAndroid Build Coastguard Worker		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1715*49cdfc7eSAndroid Build Coastguard Worker			$level--;
1716*49cdfc7eSAndroid Build Coastguard Worker			$type = '';
1717*49cdfc7eSAndroid Build Coastguard Worker			$off++;
1718*49cdfc7eSAndroid Build Coastguard Worker			last;
1719*49cdfc7eSAndroid Build Coastguard Worker		}
1720*49cdfc7eSAndroid Build Coastguard Worker		$off++;
1721*49cdfc7eSAndroid Build Coastguard Worker	}
1722*49cdfc7eSAndroid Build Coastguard Worker	# We are truly at the end, so shuffle to the next line.
1723*49cdfc7eSAndroid Build Coastguard Worker	if ($off == $len) {
1724*49cdfc7eSAndroid Build Coastguard Worker		$loff = $len + 1;
1725*49cdfc7eSAndroid Build Coastguard Worker		$line++;
1726*49cdfc7eSAndroid Build Coastguard Worker		$remain--;
1727*49cdfc7eSAndroid Build Coastguard Worker	}
1728*49cdfc7eSAndroid Build Coastguard Worker
1729*49cdfc7eSAndroid Build Coastguard Worker	my $statement = substr($blk, $soff, $off - $soff + 1);
1730*49cdfc7eSAndroid Build Coastguard Worker	my $condition = substr($blk, $soff, $coff - $soff + 1);
1731*49cdfc7eSAndroid Build Coastguard Worker
1732*49cdfc7eSAndroid Build Coastguard Worker	#warn "STATEMENT<$statement>\n";
1733*49cdfc7eSAndroid Build Coastguard Worker	#warn "CONDITION<$condition>\n";
1734*49cdfc7eSAndroid Build Coastguard Worker
1735*49cdfc7eSAndroid Build Coastguard Worker	#print "coff<$coff> soff<$off> loff<$loff>\n";
1736*49cdfc7eSAndroid Build Coastguard Worker
1737*49cdfc7eSAndroid Build Coastguard Worker	return ($statement, $condition,
1738*49cdfc7eSAndroid Build Coastguard Worker			$line, $remain + 1, $off - $loff + 1, $level);
1739*49cdfc7eSAndroid Build Coastguard Worker}
1740*49cdfc7eSAndroid Build Coastguard Worker
1741*49cdfc7eSAndroid Build Coastguard Workersub statement_lines {
1742*49cdfc7eSAndroid Build Coastguard Worker	my ($stmt) = @_;
1743*49cdfc7eSAndroid Build Coastguard Worker
1744*49cdfc7eSAndroid Build Coastguard Worker	# Strip the diff line prefixes and rip blank lines at start and end.
1745*49cdfc7eSAndroid Build Coastguard Worker	$stmt =~ s/(^|\n)./$1/g;
1746*49cdfc7eSAndroid Build Coastguard Worker	$stmt =~ s/^\s*//;
1747*49cdfc7eSAndroid Build Coastguard Worker	$stmt =~ s/\s*$//;
1748*49cdfc7eSAndroid Build Coastguard Worker
1749*49cdfc7eSAndroid Build Coastguard Worker	my @stmt_lines = ($stmt =~ /\n/g);
1750*49cdfc7eSAndroid Build Coastguard Worker
1751*49cdfc7eSAndroid Build Coastguard Worker	return $#stmt_lines + 2;
1752*49cdfc7eSAndroid Build Coastguard Worker}
1753*49cdfc7eSAndroid Build Coastguard Worker
1754*49cdfc7eSAndroid Build Coastguard Workersub statement_rawlines {
1755*49cdfc7eSAndroid Build Coastguard Worker	my ($stmt) = @_;
1756*49cdfc7eSAndroid Build Coastguard Worker
1757*49cdfc7eSAndroid Build Coastguard Worker	my @stmt_lines = ($stmt =~ /\n/g);
1758*49cdfc7eSAndroid Build Coastguard Worker
1759*49cdfc7eSAndroid Build Coastguard Worker	return $#stmt_lines + 2;
1760*49cdfc7eSAndroid Build Coastguard Worker}
1761*49cdfc7eSAndroid Build Coastguard Worker
1762*49cdfc7eSAndroid Build Coastguard Workersub statement_block_size {
1763*49cdfc7eSAndroid Build Coastguard Worker	my ($stmt) = @_;
1764*49cdfc7eSAndroid Build Coastguard Worker
1765*49cdfc7eSAndroid Build Coastguard Worker	$stmt =~ s/(^|\n)./$1/g;
1766*49cdfc7eSAndroid Build Coastguard Worker	$stmt =~ s/^\s*{//;
1767*49cdfc7eSAndroid Build Coastguard Worker	$stmt =~ s/}\s*$//;
1768*49cdfc7eSAndroid Build Coastguard Worker	$stmt =~ s/^\s*//;
1769*49cdfc7eSAndroid Build Coastguard Worker	$stmt =~ s/\s*$//;
1770*49cdfc7eSAndroid Build Coastguard Worker
1771*49cdfc7eSAndroid Build Coastguard Worker	my @stmt_lines = ($stmt =~ /\n/g);
1772*49cdfc7eSAndroid Build Coastguard Worker	my @stmt_statements = ($stmt =~ /;/g);
1773*49cdfc7eSAndroid Build Coastguard Worker
1774*49cdfc7eSAndroid Build Coastguard Worker	my $stmt_lines = $#stmt_lines + 2;
1775*49cdfc7eSAndroid Build Coastguard Worker	my $stmt_statements = $#stmt_statements + 1;
1776*49cdfc7eSAndroid Build Coastguard Worker
1777*49cdfc7eSAndroid Build Coastguard Worker	if ($stmt_lines > $stmt_statements) {
1778*49cdfc7eSAndroid Build Coastguard Worker		return $stmt_lines;
1779*49cdfc7eSAndroid Build Coastguard Worker	} else {
1780*49cdfc7eSAndroid Build Coastguard Worker		return $stmt_statements;
1781*49cdfc7eSAndroid Build Coastguard Worker	}
1782*49cdfc7eSAndroid Build Coastguard Worker}
1783*49cdfc7eSAndroid Build Coastguard Worker
1784*49cdfc7eSAndroid Build Coastguard Workersub ctx_statement_full {
1785*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $remain, $off) = @_;
1786*49cdfc7eSAndroid Build Coastguard Worker	my ($statement, $condition, $level);
1787*49cdfc7eSAndroid Build Coastguard Worker
1788*49cdfc7eSAndroid Build Coastguard Worker	my (@chunks);
1789*49cdfc7eSAndroid Build Coastguard Worker
1790*49cdfc7eSAndroid Build Coastguard Worker	# Grab the first conditional/block pair.
1791*49cdfc7eSAndroid Build Coastguard Worker	($statement, $condition, $linenr, $remain, $off, $level) =
1792*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_block($linenr, $remain, $off);
1793*49cdfc7eSAndroid Build Coastguard Worker	#print "F: c<$condition> s<$statement> remain<$remain>\n";
1794*49cdfc7eSAndroid Build Coastguard Worker	push(@chunks, [ $condition, $statement ]);
1795*49cdfc7eSAndroid Build Coastguard Worker	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1796*49cdfc7eSAndroid Build Coastguard Worker		return ($level, $linenr, @chunks);
1797*49cdfc7eSAndroid Build Coastguard Worker	}
1798*49cdfc7eSAndroid Build Coastguard Worker
1799*49cdfc7eSAndroid Build Coastguard Worker	# Pull in the following conditional/block pairs and see if they
1800*49cdfc7eSAndroid Build Coastguard Worker	# could continue the statement.
1801*49cdfc7eSAndroid Build Coastguard Worker	for (;;) {
1802*49cdfc7eSAndroid Build Coastguard Worker		($statement, $condition, $linenr, $remain, $off, $level) =
1803*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_block($linenr, $remain, $off);
1804*49cdfc7eSAndroid Build Coastguard Worker		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1805*49cdfc7eSAndroid Build Coastguard Worker		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1806*49cdfc7eSAndroid Build Coastguard Worker		#print "C: push\n";
1807*49cdfc7eSAndroid Build Coastguard Worker		push(@chunks, [ $condition, $statement ]);
1808*49cdfc7eSAndroid Build Coastguard Worker	}
1809*49cdfc7eSAndroid Build Coastguard Worker
1810*49cdfc7eSAndroid Build Coastguard Worker	return ($level, $linenr, @chunks);
1811*49cdfc7eSAndroid Build Coastguard Worker}
1812*49cdfc7eSAndroid Build Coastguard Worker
1813*49cdfc7eSAndroid Build Coastguard Workersub ctx_block_get {
1814*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
1815*49cdfc7eSAndroid Build Coastguard Worker	my $line;
1816*49cdfc7eSAndroid Build Coastguard Worker	my $start = $linenr - 1;
1817*49cdfc7eSAndroid Build Coastguard Worker	my $blk = '';
1818*49cdfc7eSAndroid Build Coastguard Worker	my @o;
1819*49cdfc7eSAndroid Build Coastguard Worker	my @c;
1820*49cdfc7eSAndroid Build Coastguard Worker	my @res = ();
1821*49cdfc7eSAndroid Build Coastguard Worker
1822*49cdfc7eSAndroid Build Coastguard Worker	my $level = 0;
1823*49cdfc7eSAndroid Build Coastguard Worker	my @stack = ($level);
1824*49cdfc7eSAndroid Build Coastguard Worker	for ($line = $start; $remain > 0; $line++) {
1825*49cdfc7eSAndroid Build Coastguard Worker		next if ($rawlines[$line] =~ /^-/);
1826*49cdfc7eSAndroid Build Coastguard Worker		$remain--;
1827*49cdfc7eSAndroid Build Coastguard Worker
1828*49cdfc7eSAndroid Build Coastguard Worker		$blk .= $rawlines[$line];
1829*49cdfc7eSAndroid Build Coastguard Worker
1830*49cdfc7eSAndroid Build Coastguard Worker		# Handle nested #if/#else.
1831*49cdfc7eSAndroid Build Coastguard Worker		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
1832*49cdfc7eSAndroid Build Coastguard Worker			push(@stack, $level);
1833*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
1834*49cdfc7eSAndroid Build Coastguard Worker			$level = $stack[$#stack - 1];
1835*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
1836*49cdfc7eSAndroid Build Coastguard Worker			$level = pop(@stack);
1837*49cdfc7eSAndroid Build Coastguard Worker		}
1838*49cdfc7eSAndroid Build Coastguard Worker
1839*49cdfc7eSAndroid Build Coastguard Worker		foreach my $c (split(//, $lines[$line])) {
1840*49cdfc7eSAndroid Build Coastguard Worker			##print "C<$c>L<$level><$open$close>O<$off>\n";
1841*49cdfc7eSAndroid Build Coastguard Worker			if ($off > 0) {
1842*49cdfc7eSAndroid Build Coastguard Worker				$off--;
1843*49cdfc7eSAndroid Build Coastguard Worker				next;
1844*49cdfc7eSAndroid Build Coastguard Worker			}
1845*49cdfc7eSAndroid Build Coastguard Worker
1846*49cdfc7eSAndroid Build Coastguard Worker			if ($c eq $close && $level > 0) {
1847*49cdfc7eSAndroid Build Coastguard Worker				$level--;
1848*49cdfc7eSAndroid Build Coastguard Worker				last if ($level == 0);
1849*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($c eq $open) {
1850*49cdfc7eSAndroid Build Coastguard Worker				$level++;
1851*49cdfc7eSAndroid Build Coastguard Worker			}
1852*49cdfc7eSAndroid Build Coastguard Worker		}
1853*49cdfc7eSAndroid Build Coastguard Worker
1854*49cdfc7eSAndroid Build Coastguard Worker		if (!$outer || $level <= 1) {
1855*49cdfc7eSAndroid Build Coastguard Worker			push(@res, $rawlines[$line]);
1856*49cdfc7eSAndroid Build Coastguard Worker		}
1857*49cdfc7eSAndroid Build Coastguard Worker
1858*49cdfc7eSAndroid Build Coastguard Worker		last if ($level == 0);
1859*49cdfc7eSAndroid Build Coastguard Worker	}
1860*49cdfc7eSAndroid Build Coastguard Worker
1861*49cdfc7eSAndroid Build Coastguard Worker	return ($level, @res);
1862*49cdfc7eSAndroid Build Coastguard Worker}
1863*49cdfc7eSAndroid Build Coastguard Workersub ctx_block_outer {
1864*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $remain) = @_;
1865*49cdfc7eSAndroid Build Coastguard Worker
1866*49cdfc7eSAndroid Build Coastguard Worker	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1867*49cdfc7eSAndroid Build Coastguard Worker	return @r;
1868*49cdfc7eSAndroid Build Coastguard Worker}
1869*49cdfc7eSAndroid Build Coastguard Workersub ctx_block {
1870*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $remain) = @_;
1871*49cdfc7eSAndroid Build Coastguard Worker
1872*49cdfc7eSAndroid Build Coastguard Worker	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1873*49cdfc7eSAndroid Build Coastguard Worker	return @r;
1874*49cdfc7eSAndroid Build Coastguard Worker}
1875*49cdfc7eSAndroid Build Coastguard Workersub ctx_statement {
1876*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $remain, $off) = @_;
1877*49cdfc7eSAndroid Build Coastguard Worker
1878*49cdfc7eSAndroid Build Coastguard Worker	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1879*49cdfc7eSAndroid Build Coastguard Worker	return @r;
1880*49cdfc7eSAndroid Build Coastguard Worker}
1881*49cdfc7eSAndroid Build Coastguard Workersub ctx_block_level {
1882*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $remain) = @_;
1883*49cdfc7eSAndroid Build Coastguard Worker
1884*49cdfc7eSAndroid Build Coastguard Worker	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1885*49cdfc7eSAndroid Build Coastguard Worker}
1886*49cdfc7eSAndroid Build Coastguard Workersub ctx_statement_level {
1887*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $remain, $off) = @_;
1888*49cdfc7eSAndroid Build Coastguard Worker
1889*49cdfc7eSAndroid Build Coastguard Worker	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1890*49cdfc7eSAndroid Build Coastguard Worker}
1891*49cdfc7eSAndroid Build Coastguard Worker
1892*49cdfc7eSAndroid Build Coastguard Workersub ctx_locate_comment {
1893*49cdfc7eSAndroid Build Coastguard Worker	my ($first_line, $end_line) = @_;
1894*49cdfc7eSAndroid Build Coastguard Worker
1895*49cdfc7eSAndroid Build Coastguard Worker	# If c99 comment on the current line, or the line before or after
1896*49cdfc7eSAndroid Build Coastguard Worker	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1897*49cdfc7eSAndroid Build Coastguard Worker	return $current_comment if (defined $current_comment);
1898*49cdfc7eSAndroid Build Coastguard Worker	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1899*49cdfc7eSAndroid Build Coastguard Worker	return $current_comment if (defined $current_comment);
1900*49cdfc7eSAndroid Build Coastguard Worker	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1901*49cdfc7eSAndroid Build Coastguard Worker	return $current_comment if (defined $current_comment);
1902*49cdfc7eSAndroid Build Coastguard Worker
1903*49cdfc7eSAndroid Build Coastguard Worker	# Catch a comment on the end of the line itself.
1904*49cdfc7eSAndroid Build Coastguard Worker	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
1905*49cdfc7eSAndroid Build Coastguard Worker	return $current_comment if (defined $current_comment);
1906*49cdfc7eSAndroid Build Coastguard Worker
1907*49cdfc7eSAndroid Build Coastguard Worker	# Look through the context and try and figure out if there is a
1908*49cdfc7eSAndroid Build Coastguard Worker	# comment.
1909*49cdfc7eSAndroid Build Coastguard Worker	my $in_comment = 0;
1910*49cdfc7eSAndroid Build Coastguard Worker	$current_comment = '';
1911*49cdfc7eSAndroid Build Coastguard Worker	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
1912*49cdfc7eSAndroid Build Coastguard Worker		my $line = $rawlines[$linenr - 1];
1913*49cdfc7eSAndroid Build Coastguard Worker		#warn "           $line\n";
1914*49cdfc7eSAndroid Build Coastguard Worker		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1915*49cdfc7eSAndroid Build Coastguard Worker			$in_comment = 1;
1916*49cdfc7eSAndroid Build Coastguard Worker		}
1917*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ m@/\*@) {
1918*49cdfc7eSAndroid Build Coastguard Worker			$in_comment = 1;
1919*49cdfc7eSAndroid Build Coastguard Worker		}
1920*49cdfc7eSAndroid Build Coastguard Worker		if (!$in_comment && $current_comment ne '') {
1921*49cdfc7eSAndroid Build Coastguard Worker			$current_comment = '';
1922*49cdfc7eSAndroid Build Coastguard Worker		}
1923*49cdfc7eSAndroid Build Coastguard Worker		$current_comment .= $line . "\n" if ($in_comment);
1924*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ m@\*/@) {
1925*49cdfc7eSAndroid Build Coastguard Worker			$in_comment = 0;
1926*49cdfc7eSAndroid Build Coastguard Worker		}
1927*49cdfc7eSAndroid Build Coastguard Worker	}
1928*49cdfc7eSAndroid Build Coastguard Worker
1929*49cdfc7eSAndroid Build Coastguard Worker	chomp($current_comment);
1930*49cdfc7eSAndroid Build Coastguard Worker	return($current_comment);
1931*49cdfc7eSAndroid Build Coastguard Worker}
1932*49cdfc7eSAndroid Build Coastguard Workersub ctx_has_comment {
1933*49cdfc7eSAndroid Build Coastguard Worker	my ($first_line, $end_line) = @_;
1934*49cdfc7eSAndroid Build Coastguard Worker	my $cmt = ctx_locate_comment($first_line, $end_line);
1935*49cdfc7eSAndroid Build Coastguard Worker
1936*49cdfc7eSAndroid Build Coastguard Worker	##print "LINE: $rawlines[$end_line - 1 ]\n";
1937*49cdfc7eSAndroid Build Coastguard Worker	##print "CMMT: $cmt\n";
1938*49cdfc7eSAndroid Build Coastguard Worker
1939*49cdfc7eSAndroid Build Coastguard Worker	return ($cmt ne '');
1940*49cdfc7eSAndroid Build Coastguard Worker}
1941*49cdfc7eSAndroid Build Coastguard Worker
1942*49cdfc7eSAndroid Build Coastguard Workersub raw_line {
1943*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $cnt) = @_;
1944*49cdfc7eSAndroid Build Coastguard Worker
1945*49cdfc7eSAndroid Build Coastguard Worker	my $offset = $linenr - 1;
1946*49cdfc7eSAndroid Build Coastguard Worker	$cnt++;
1947*49cdfc7eSAndroid Build Coastguard Worker
1948*49cdfc7eSAndroid Build Coastguard Worker	my $line;
1949*49cdfc7eSAndroid Build Coastguard Worker	while ($cnt) {
1950*49cdfc7eSAndroid Build Coastguard Worker		$line = $rawlines[$offset++];
1951*49cdfc7eSAndroid Build Coastguard Worker		next if (defined($line) && $line =~ /^-/);
1952*49cdfc7eSAndroid Build Coastguard Worker		$cnt--;
1953*49cdfc7eSAndroid Build Coastguard Worker	}
1954*49cdfc7eSAndroid Build Coastguard Worker
1955*49cdfc7eSAndroid Build Coastguard Worker	return $line;
1956*49cdfc7eSAndroid Build Coastguard Worker}
1957*49cdfc7eSAndroid Build Coastguard Worker
1958*49cdfc7eSAndroid Build Coastguard Workersub get_stat_real {
1959*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $lc) = @_;
1960*49cdfc7eSAndroid Build Coastguard Worker
1961*49cdfc7eSAndroid Build Coastguard Worker	my $stat_real = raw_line($linenr, 0);
1962*49cdfc7eSAndroid Build Coastguard Worker	for (my $count = $linenr + 1; $count <= $lc; $count++) {
1963*49cdfc7eSAndroid Build Coastguard Worker		$stat_real = $stat_real . "\n" . raw_line($count, 0);
1964*49cdfc7eSAndroid Build Coastguard Worker	}
1965*49cdfc7eSAndroid Build Coastguard Worker
1966*49cdfc7eSAndroid Build Coastguard Worker	return $stat_real;
1967*49cdfc7eSAndroid Build Coastguard Worker}
1968*49cdfc7eSAndroid Build Coastguard Worker
1969*49cdfc7eSAndroid Build Coastguard Workersub get_stat_here {
1970*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $cnt, $here) = @_;
1971*49cdfc7eSAndroid Build Coastguard Worker
1972*49cdfc7eSAndroid Build Coastguard Worker	my $herectx = $here . "\n";
1973*49cdfc7eSAndroid Build Coastguard Worker	for (my $n = 0; $n < $cnt; $n++) {
1974*49cdfc7eSAndroid Build Coastguard Worker		$herectx .= raw_line($linenr, $n) . "\n";
1975*49cdfc7eSAndroid Build Coastguard Worker	}
1976*49cdfc7eSAndroid Build Coastguard Worker
1977*49cdfc7eSAndroid Build Coastguard Worker	return $herectx;
1978*49cdfc7eSAndroid Build Coastguard Worker}
1979*49cdfc7eSAndroid Build Coastguard Worker
1980*49cdfc7eSAndroid Build Coastguard Workersub cat_vet {
1981*49cdfc7eSAndroid Build Coastguard Worker	my ($vet) = @_;
1982*49cdfc7eSAndroid Build Coastguard Worker	my ($res, $coded);
1983*49cdfc7eSAndroid Build Coastguard Worker
1984*49cdfc7eSAndroid Build Coastguard Worker	$res = '';
1985*49cdfc7eSAndroid Build Coastguard Worker	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1986*49cdfc7eSAndroid Build Coastguard Worker		$res .= $1;
1987*49cdfc7eSAndroid Build Coastguard Worker		if ($2 ne '') {
1988*49cdfc7eSAndroid Build Coastguard Worker			$coded = sprintf("^%c", unpack('C', $2) + 64);
1989*49cdfc7eSAndroid Build Coastguard Worker			$res .= $coded;
1990*49cdfc7eSAndroid Build Coastguard Worker		}
1991*49cdfc7eSAndroid Build Coastguard Worker	}
1992*49cdfc7eSAndroid Build Coastguard Worker	$res =~ s/$/\$/;
1993*49cdfc7eSAndroid Build Coastguard Worker
1994*49cdfc7eSAndroid Build Coastguard Worker	return $res;
1995*49cdfc7eSAndroid Build Coastguard Worker}
1996*49cdfc7eSAndroid Build Coastguard Worker
1997*49cdfc7eSAndroid Build Coastguard Workermy $av_preprocessor = 0;
1998*49cdfc7eSAndroid Build Coastguard Workermy $av_pending;
1999*49cdfc7eSAndroid Build Coastguard Workermy @av_paren_type;
2000*49cdfc7eSAndroid Build Coastguard Workermy $av_pend_colon;
2001*49cdfc7eSAndroid Build Coastguard Worker
2002*49cdfc7eSAndroid Build Coastguard Workersub annotate_reset {
2003*49cdfc7eSAndroid Build Coastguard Worker	$av_preprocessor = 0;
2004*49cdfc7eSAndroid Build Coastguard Worker	$av_pending = '_';
2005*49cdfc7eSAndroid Build Coastguard Worker	@av_paren_type = ('E');
2006*49cdfc7eSAndroid Build Coastguard Worker	$av_pend_colon = 'O';
2007*49cdfc7eSAndroid Build Coastguard Worker}
2008*49cdfc7eSAndroid Build Coastguard Worker
2009*49cdfc7eSAndroid Build Coastguard Workersub annotate_values {
2010*49cdfc7eSAndroid Build Coastguard Worker	my ($stream, $type) = @_;
2011*49cdfc7eSAndroid Build Coastguard Worker
2012*49cdfc7eSAndroid Build Coastguard Worker	my $res;
2013*49cdfc7eSAndroid Build Coastguard Worker	my $var = '_' x length($stream);
2014*49cdfc7eSAndroid Build Coastguard Worker	my $cur = $stream;
2015*49cdfc7eSAndroid Build Coastguard Worker
2016*49cdfc7eSAndroid Build Coastguard Worker	print "$stream\n" if ($dbg_values > 1);
2017*49cdfc7eSAndroid Build Coastguard Worker
2018*49cdfc7eSAndroid Build Coastguard Worker	while (length($cur)) {
2019*49cdfc7eSAndroid Build Coastguard Worker		@av_paren_type = ('E') if ($#av_paren_type < 0);
2020*49cdfc7eSAndroid Build Coastguard Worker		print " <" . join('', @av_paren_type) .
2021*49cdfc7eSAndroid Build Coastguard Worker				"> <$type> <$av_pending>" if ($dbg_values > 1);
2022*49cdfc7eSAndroid Build Coastguard Worker		if ($cur =~ /^(\s+)/o) {
2023*49cdfc7eSAndroid Build Coastguard Worker			print "WS($1)\n" if ($dbg_values > 1);
2024*49cdfc7eSAndroid Build Coastguard Worker			if ($1 =~ /\n/ && $av_preprocessor) {
2025*49cdfc7eSAndroid Build Coastguard Worker				$type = pop(@av_paren_type);
2026*49cdfc7eSAndroid Build Coastguard Worker				$av_preprocessor = 0;
2027*49cdfc7eSAndroid Build Coastguard Worker			}
2028*49cdfc7eSAndroid Build Coastguard Worker
2029*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
2030*49cdfc7eSAndroid Build Coastguard Worker			print "CAST($1)\n" if ($dbg_values > 1);
2031*49cdfc7eSAndroid Build Coastguard Worker			push(@av_paren_type, $type);
2032*49cdfc7eSAndroid Build Coastguard Worker			$type = 'c';
2033*49cdfc7eSAndroid Build Coastguard Worker
2034*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
2035*49cdfc7eSAndroid Build Coastguard Worker			print "DECLARE($1)\n" if ($dbg_values > 1);
2036*49cdfc7eSAndroid Build Coastguard Worker			$type = 'T';
2037*49cdfc7eSAndroid Build Coastguard Worker
2038*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^($Modifier)\s*/) {
2039*49cdfc7eSAndroid Build Coastguard Worker			print "MODIFIER($1)\n" if ($dbg_values > 1);
2040*49cdfc7eSAndroid Build Coastguard Worker			$type = 'T';
2041*49cdfc7eSAndroid Build Coastguard Worker
2042*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
2043*49cdfc7eSAndroid Build Coastguard Worker			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
2044*49cdfc7eSAndroid Build Coastguard Worker			$av_preprocessor = 1;
2045*49cdfc7eSAndroid Build Coastguard Worker			push(@av_paren_type, $type);
2046*49cdfc7eSAndroid Build Coastguard Worker			if ($2 ne '') {
2047*49cdfc7eSAndroid Build Coastguard Worker				$av_pending = 'N';
2048*49cdfc7eSAndroid Build Coastguard Worker			}
2049*49cdfc7eSAndroid Build Coastguard Worker			$type = 'E';
2050*49cdfc7eSAndroid Build Coastguard Worker
2051*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
2052*49cdfc7eSAndroid Build Coastguard Worker			print "UNDEF($1)\n" if ($dbg_values > 1);
2053*49cdfc7eSAndroid Build Coastguard Worker			$av_preprocessor = 1;
2054*49cdfc7eSAndroid Build Coastguard Worker			push(@av_paren_type, $type);
2055*49cdfc7eSAndroid Build Coastguard Worker
2056*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
2057*49cdfc7eSAndroid Build Coastguard Worker			print "PRE_START($1)\n" if ($dbg_values > 1);
2058*49cdfc7eSAndroid Build Coastguard Worker			$av_preprocessor = 1;
2059*49cdfc7eSAndroid Build Coastguard Worker
2060*49cdfc7eSAndroid Build Coastguard Worker			push(@av_paren_type, $type);
2061*49cdfc7eSAndroid Build Coastguard Worker			push(@av_paren_type, $type);
2062*49cdfc7eSAndroid Build Coastguard Worker			$type = 'E';
2063*49cdfc7eSAndroid Build Coastguard Worker
2064*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
2065*49cdfc7eSAndroid Build Coastguard Worker			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2066*49cdfc7eSAndroid Build Coastguard Worker			$av_preprocessor = 1;
2067*49cdfc7eSAndroid Build Coastguard Worker
2068*49cdfc7eSAndroid Build Coastguard Worker			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2069*49cdfc7eSAndroid Build Coastguard Worker
2070*49cdfc7eSAndroid Build Coastguard Worker			$type = 'E';
2071*49cdfc7eSAndroid Build Coastguard Worker
2072*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
2073*49cdfc7eSAndroid Build Coastguard Worker			print "PRE_END($1)\n" if ($dbg_values > 1);
2074*49cdfc7eSAndroid Build Coastguard Worker
2075*49cdfc7eSAndroid Build Coastguard Worker			$av_preprocessor = 1;
2076*49cdfc7eSAndroid Build Coastguard Worker
2077*49cdfc7eSAndroid Build Coastguard Worker			# Assume all arms of the conditional end as this
2078*49cdfc7eSAndroid Build Coastguard Worker			# one does, and continue as if the #endif was not here.
2079*49cdfc7eSAndroid Build Coastguard Worker			pop(@av_paren_type);
2080*49cdfc7eSAndroid Build Coastguard Worker			push(@av_paren_type, $type);
2081*49cdfc7eSAndroid Build Coastguard Worker			$type = 'E';
2082*49cdfc7eSAndroid Build Coastguard Worker
2083*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\\\n)/o) {
2084*49cdfc7eSAndroid Build Coastguard Worker			print "PRECONT($1)\n" if ($dbg_values > 1);
2085*49cdfc7eSAndroid Build Coastguard Worker
2086*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2087*49cdfc7eSAndroid Build Coastguard Worker			print "ATTR($1)\n" if ($dbg_values > 1);
2088*49cdfc7eSAndroid Build Coastguard Worker			$av_pending = $type;
2089*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2090*49cdfc7eSAndroid Build Coastguard Worker
2091*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
2092*49cdfc7eSAndroid Build Coastguard Worker			print "SIZEOF($1)\n" if ($dbg_values > 1);
2093*49cdfc7eSAndroid Build Coastguard Worker			if (defined $2) {
2094*49cdfc7eSAndroid Build Coastguard Worker				$av_pending = 'V';
2095*49cdfc7eSAndroid Build Coastguard Worker			}
2096*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2097*49cdfc7eSAndroid Build Coastguard Worker
2098*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(if|while|for)\b/o) {
2099*49cdfc7eSAndroid Build Coastguard Worker			print "COND($1)\n" if ($dbg_values > 1);
2100*49cdfc7eSAndroid Build Coastguard Worker			$av_pending = 'E';
2101*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2102*49cdfc7eSAndroid Build Coastguard Worker
2103*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~/^(case)/o) {
2104*49cdfc7eSAndroid Build Coastguard Worker			print "CASE($1)\n" if ($dbg_values > 1);
2105*49cdfc7eSAndroid Build Coastguard Worker			$av_pend_colon = 'C';
2106*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2107*49cdfc7eSAndroid Build Coastguard Worker
2108*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
2109*49cdfc7eSAndroid Build Coastguard Worker			print "KEYWORD($1)\n" if ($dbg_values > 1);
2110*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2111*49cdfc7eSAndroid Build Coastguard Worker
2112*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\()/o) {
2113*49cdfc7eSAndroid Build Coastguard Worker			print "PAREN('$1')\n" if ($dbg_values > 1);
2114*49cdfc7eSAndroid Build Coastguard Worker			push(@av_paren_type, $av_pending);
2115*49cdfc7eSAndroid Build Coastguard Worker			$av_pending = '_';
2116*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2117*49cdfc7eSAndroid Build Coastguard Worker
2118*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\))/o) {
2119*49cdfc7eSAndroid Build Coastguard Worker			my $new_type = pop(@av_paren_type);
2120*49cdfc7eSAndroid Build Coastguard Worker			if ($new_type ne '_') {
2121*49cdfc7eSAndroid Build Coastguard Worker				$type = $new_type;
2122*49cdfc7eSAndroid Build Coastguard Worker				print "PAREN('$1') -> $type\n"
2123*49cdfc7eSAndroid Build Coastguard Worker							if ($dbg_values > 1);
2124*49cdfc7eSAndroid Build Coastguard Worker			} else {
2125*49cdfc7eSAndroid Build Coastguard Worker				print "PAREN('$1')\n" if ($dbg_values > 1);
2126*49cdfc7eSAndroid Build Coastguard Worker			}
2127*49cdfc7eSAndroid Build Coastguard Worker
2128*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^($Ident)\s*\(/o) {
2129*49cdfc7eSAndroid Build Coastguard Worker			print "FUNC($1)\n" if ($dbg_values > 1);
2130*49cdfc7eSAndroid Build Coastguard Worker			$type = 'V';
2131*49cdfc7eSAndroid Build Coastguard Worker			$av_pending = 'V';
2132*49cdfc7eSAndroid Build Coastguard Worker
2133*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
2134*49cdfc7eSAndroid Build Coastguard Worker			if (defined $2 && $type eq 'C' || $type eq 'T') {
2135*49cdfc7eSAndroid Build Coastguard Worker				$av_pend_colon = 'B';
2136*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($type eq 'E') {
2137*49cdfc7eSAndroid Build Coastguard Worker				$av_pend_colon = 'L';
2138*49cdfc7eSAndroid Build Coastguard Worker			}
2139*49cdfc7eSAndroid Build Coastguard Worker			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
2140*49cdfc7eSAndroid Build Coastguard Worker			$type = 'V';
2141*49cdfc7eSAndroid Build Coastguard Worker
2142*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^($Ident|$Constant)/o) {
2143*49cdfc7eSAndroid Build Coastguard Worker			print "IDENT($1)\n" if ($dbg_values > 1);
2144*49cdfc7eSAndroid Build Coastguard Worker			$type = 'V';
2145*49cdfc7eSAndroid Build Coastguard Worker
2146*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^($Assignment)/o) {
2147*49cdfc7eSAndroid Build Coastguard Worker			print "ASSIGN($1)\n" if ($dbg_values > 1);
2148*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2149*49cdfc7eSAndroid Build Coastguard Worker
2150*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~/^(;|{|})/) {
2151*49cdfc7eSAndroid Build Coastguard Worker			print "END($1)\n" if ($dbg_values > 1);
2152*49cdfc7eSAndroid Build Coastguard Worker			$type = 'E';
2153*49cdfc7eSAndroid Build Coastguard Worker			$av_pend_colon = 'O';
2154*49cdfc7eSAndroid Build Coastguard Worker
2155*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~/^(,)/) {
2156*49cdfc7eSAndroid Build Coastguard Worker			print "COMMA($1)\n" if ($dbg_values > 1);
2157*49cdfc7eSAndroid Build Coastguard Worker			$type = 'C';
2158*49cdfc7eSAndroid Build Coastguard Worker
2159*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\?)/o) {
2160*49cdfc7eSAndroid Build Coastguard Worker			print "QUESTION($1)\n" if ($dbg_values > 1);
2161*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2162*49cdfc7eSAndroid Build Coastguard Worker
2163*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(:)/o) {
2164*49cdfc7eSAndroid Build Coastguard Worker			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
2165*49cdfc7eSAndroid Build Coastguard Worker
2166*49cdfc7eSAndroid Build Coastguard Worker			substr($var, length($res), 1, $av_pend_colon);
2167*49cdfc7eSAndroid Build Coastguard Worker			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
2168*49cdfc7eSAndroid Build Coastguard Worker				$type = 'E';
2169*49cdfc7eSAndroid Build Coastguard Worker			} else {
2170*49cdfc7eSAndroid Build Coastguard Worker				$type = 'N';
2171*49cdfc7eSAndroid Build Coastguard Worker			}
2172*49cdfc7eSAndroid Build Coastguard Worker			$av_pend_colon = 'O';
2173*49cdfc7eSAndroid Build Coastguard Worker
2174*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(\[)/o) {
2175*49cdfc7eSAndroid Build Coastguard Worker			print "CLOSE($1)\n" if ($dbg_values > 1);
2176*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2177*49cdfc7eSAndroid Build Coastguard Worker
2178*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
2179*49cdfc7eSAndroid Build Coastguard Worker			my $variant;
2180*49cdfc7eSAndroid Build Coastguard Worker
2181*49cdfc7eSAndroid Build Coastguard Worker			print "OPV($1)\n" if ($dbg_values > 1);
2182*49cdfc7eSAndroid Build Coastguard Worker			if ($type eq 'V') {
2183*49cdfc7eSAndroid Build Coastguard Worker				$variant = 'B';
2184*49cdfc7eSAndroid Build Coastguard Worker			} else {
2185*49cdfc7eSAndroid Build Coastguard Worker				$variant = 'U';
2186*49cdfc7eSAndroid Build Coastguard Worker			}
2187*49cdfc7eSAndroid Build Coastguard Worker
2188*49cdfc7eSAndroid Build Coastguard Worker			substr($var, length($res), 1, $variant);
2189*49cdfc7eSAndroid Build Coastguard Worker			$type = 'N';
2190*49cdfc7eSAndroid Build Coastguard Worker
2191*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /^($Operators)/o) {
2192*49cdfc7eSAndroid Build Coastguard Worker			print "OP($1)\n" if ($dbg_values > 1);
2193*49cdfc7eSAndroid Build Coastguard Worker			if ($1 ne '++' && $1 ne '--') {
2194*49cdfc7eSAndroid Build Coastguard Worker				$type = 'N';
2195*49cdfc7eSAndroid Build Coastguard Worker			}
2196*49cdfc7eSAndroid Build Coastguard Worker
2197*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($cur =~ /(^.)/o) {
2198*49cdfc7eSAndroid Build Coastguard Worker			print "C($1)\n" if ($dbg_values > 1);
2199*49cdfc7eSAndroid Build Coastguard Worker		}
2200*49cdfc7eSAndroid Build Coastguard Worker		if (defined $1) {
2201*49cdfc7eSAndroid Build Coastguard Worker			$cur = substr($cur, length($1));
2202*49cdfc7eSAndroid Build Coastguard Worker			$res .= $type x length($1);
2203*49cdfc7eSAndroid Build Coastguard Worker		}
2204*49cdfc7eSAndroid Build Coastguard Worker	}
2205*49cdfc7eSAndroid Build Coastguard Worker
2206*49cdfc7eSAndroid Build Coastguard Worker	return ($res, $var);
2207*49cdfc7eSAndroid Build Coastguard Worker}
2208*49cdfc7eSAndroid Build Coastguard Worker
2209*49cdfc7eSAndroid Build Coastguard Workersub possible {
2210*49cdfc7eSAndroid Build Coastguard Worker	my ($possible, $line) = @_;
2211*49cdfc7eSAndroid Build Coastguard Worker	my $notPermitted = qr{(?:
2212*49cdfc7eSAndroid Build Coastguard Worker		^(?:
2213*49cdfc7eSAndroid Build Coastguard Worker			$Modifier|
2214*49cdfc7eSAndroid Build Coastguard Worker			$Storage|
2215*49cdfc7eSAndroid Build Coastguard Worker			$Type|
2216*49cdfc7eSAndroid Build Coastguard Worker			DEFINE_\S+
2217*49cdfc7eSAndroid Build Coastguard Worker		)$|
2218*49cdfc7eSAndroid Build Coastguard Worker		^(?:
2219*49cdfc7eSAndroid Build Coastguard Worker			goto|
2220*49cdfc7eSAndroid Build Coastguard Worker			return|
2221*49cdfc7eSAndroid Build Coastguard Worker			case|
2222*49cdfc7eSAndroid Build Coastguard Worker			else|
2223*49cdfc7eSAndroid Build Coastguard Worker			asm|__asm__|
2224*49cdfc7eSAndroid Build Coastguard Worker			do|
2225*49cdfc7eSAndroid Build Coastguard Worker			\#|
2226*49cdfc7eSAndroid Build Coastguard Worker			\#\#|
2227*49cdfc7eSAndroid Build Coastguard Worker		)(?:\s|$)|
2228*49cdfc7eSAndroid Build Coastguard Worker		^(?:typedef|struct|enum)\b
2229*49cdfc7eSAndroid Build Coastguard Worker	    )}x;
2230*49cdfc7eSAndroid Build Coastguard Worker	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
2231*49cdfc7eSAndroid Build Coastguard Worker	if ($possible !~ $notPermitted) {
2232*49cdfc7eSAndroid Build Coastguard Worker		# Check for modifiers.
2233*49cdfc7eSAndroid Build Coastguard Worker		$possible =~ s/\s*$Storage\s*//g;
2234*49cdfc7eSAndroid Build Coastguard Worker		$possible =~ s/\s*$Sparse\s*//g;
2235*49cdfc7eSAndroid Build Coastguard Worker		if ($possible =~ /^\s*$/) {
2236*49cdfc7eSAndroid Build Coastguard Worker
2237*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($possible =~ /\s/) {
2238*49cdfc7eSAndroid Build Coastguard Worker			$possible =~ s/\s*$Type\s*//g;
2239*49cdfc7eSAndroid Build Coastguard Worker			for my $modifier (split(' ', $possible)) {
2240*49cdfc7eSAndroid Build Coastguard Worker				if ($modifier !~ $notPermitted) {
2241*49cdfc7eSAndroid Build Coastguard Worker					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2242*49cdfc7eSAndroid Build Coastguard Worker					push(@modifierListFile, $modifier);
2243*49cdfc7eSAndroid Build Coastguard Worker				}
2244*49cdfc7eSAndroid Build Coastguard Worker			}
2245*49cdfc7eSAndroid Build Coastguard Worker
2246*49cdfc7eSAndroid Build Coastguard Worker		} else {
2247*49cdfc7eSAndroid Build Coastguard Worker			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2248*49cdfc7eSAndroid Build Coastguard Worker			push(@typeListFile, $possible);
2249*49cdfc7eSAndroid Build Coastguard Worker		}
2250*49cdfc7eSAndroid Build Coastguard Worker		build_types();
2251*49cdfc7eSAndroid Build Coastguard Worker	} else {
2252*49cdfc7eSAndroid Build Coastguard Worker		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
2253*49cdfc7eSAndroid Build Coastguard Worker	}
2254*49cdfc7eSAndroid Build Coastguard Worker}
2255*49cdfc7eSAndroid Build Coastguard Worker
2256*49cdfc7eSAndroid Build Coastguard Workermy $prefix = '';
2257*49cdfc7eSAndroid Build Coastguard Worker
2258*49cdfc7eSAndroid Build Coastguard Workersub show_type {
2259*49cdfc7eSAndroid Build Coastguard Worker	my ($type) = @_;
2260*49cdfc7eSAndroid Build Coastguard Worker
2261*49cdfc7eSAndroid Build Coastguard Worker	$type =~ tr/[a-z]/[A-Z]/;
2262*49cdfc7eSAndroid Build Coastguard Worker
2263*49cdfc7eSAndroid Build Coastguard Worker	return defined $use_type{$type} if (scalar keys %use_type > 0);
2264*49cdfc7eSAndroid Build Coastguard Worker
2265*49cdfc7eSAndroid Build Coastguard Worker	return !defined $ignore_type{$type};
2266*49cdfc7eSAndroid Build Coastguard Worker}
2267*49cdfc7eSAndroid Build Coastguard Worker
2268*49cdfc7eSAndroid Build Coastguard Workersub report {
2269*49cdfc7eSAndroid Build Coastguard Worker	my ($level, $type, $msg) = @_;
2270*49cdfc7eSAndroid Build Coastguard Worker
2271*49cdfc7eSAndroid Build Coastguard Worker	if (!show_type($type) ||
2272*49cdfc7eSAndroid Build Coastguard Worker	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2273*49cdfc7eSAndroid Build Coastguard Worker		return 0;
2274*49cdfc7eSAndroid Build Coastguard Worker	}
2275*49cdfc7eSAndroid Build Coastguard Worker	my $output = '';
2276*49cdfc7eSAndroid Build Coastguard Worker	if ($color) {
2277*49cdfc7eSAndroid Build Coastguard Worker		if ($level eq 'ERROR') {
2278*49cdfc7eSAndroid Build Coastguard Worker			$output .= RED;
2279*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($level eq 'WARNING') {
2280*49cdfc7eSAndroid Build Coastguard Worker			$output .= YELLOW;
2281*49cdfc7eSAndroid Build Coastguard Worker		} else {
2282*49cdfc7eSAndroid Build Coastguard Worker			$output .= GREEN;
2283*49cdfc7eSAndroid Build Coastguard Worker		}
2284*49cdfc7eSAndroid Build Coastguard Worker	}
2285*49cdfc7eSAndroid Build Coastguard Worker	$output .= $prefix . $level . ':';
2286*49cdfc7eSAndroid Build Coastguard Worker	if ($show_types) {
2287*49cdfc7eSAndroid Build Coastguard Worker		$output .= BLUE if ($color);
2288*49cdfc7eSAndroid Build Coastguard Worker		$output .= "$type:";
2289*49cdfc7eSAndroid Build Coastguard Worker	}
2290*49cdfc7eSAndroid Build Coastguard Worker	$output .= RESET if ($color);
2291*49cdfc7eSAndroid Build Coastguard Worker	$output .= ' ' . $msg . "\n";
2292*49cdfc7eSAndroid Build Coastguard Worker
2293*49cdfc7eSAndroid Build Coastguard Worker	if ($showfile) {
2294*49cdfc7eSAndroid Build Coastguard Worker		my @lines = split("\n", $output, -1);
2295*49cdfc7eSAndroid Build Coastguard Worker		splice(@lines, 1, 1);
2296*49cdfc7eSAndroid Build Coastguard Worker		$output = join("\n", @lines);
2297*49cdfc7eSAndroid Build Coastguard Worker	}
2298*49cdfc7eSAndroid Build Coastguard Worker
2299*49cdfc7eSAndroid Build Coastguard Worker	if ($terse) {
2300*49cdfc7eSAndroid Build Coastguard Worker		$output = (split('\n', $output))[0] . "\n";
2301*49cdfc7eSAndroid Build Coastguard Worker	}
2302*49cdfc7eSAndroid Build Coastguard Worker
2303*49cdfc7eSAndroid Build Coastguard Worker	if ($verbose && exists($verbose_messages{$type}) &&
2304*49cdfc7eSAndroid Build Coastguard Worker	    !exists($verbose_emitted{$type})) {
2305*49cdfc7eSAndroid Build Coastguard Worker		$output .= $verbose_messages{$type} . "\n\n";
2306*49cdfc7eSAndroid Build Coastguard Worker		$verbose_emitted{$type} = 1;
2307*49cdfc7eSAndroid Build Coastguard Worker	}
2308*49cdfc7eSAndroid Build Coastguard Worker
2309*49cdfc7eSAndroid Build Coastguard Worker	push(our @report, $output);
2310*49cdfc7eSAndroid Build Coastguard Worker
2311*49cdfc7eSAndroid Build Coastguard Worker	return 1;
2312*49cdfc7eSAndroid Build Coastguard Worker}
2313*49cdfc7eSAndroid Build Coastguard Worker
2314*49cdfc7eSAndroid Build Coastguard Workersub report_dump {
2315*49cdfc7eSAndroid Build Coastguard Worker	our @report;
2316*49cdfc7eSAndroid Build Coastguard Worker}
2317*49cdfc7eSAndroid Build Coastguard Worker
2318*49cdfc7eSAndroid Build Coastguard Workersub fixup_current_range {
2319*49cdfc7eSAndroid Build Coastguard Worker	my ($lineRef, $offset, $length) = @_;
2320*49cdfc7eSAndroid Build Coastguard Worker
2321*49cdfc7eSAndroid Build Coastguard Worker	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2322*49cdfc7eSAndroid Build Coastguard Worker		my $o = $1;
2323*49cdfc7eSAndroid Build Coastguard Worker		my $l = $2;
2324*49cdfc7eSAndroid Build Coastguard Worker		my $no = $o + $offset;
2325*49cdfc7eSAndroid Build Coastguard Worker		my $nl = $l + $length;
2326*49cdfc7eSAndroid Build Coastguard Worker		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2327*49cdfc7eSAndroid Build Coastguard Worker	}
2328*49cdfc7eSAndroid Build Coastguard Worker}
2329*49cdfc7eSAndroid Build Coastguard Worker
2330*49cdfc7eSAndroid Build Coastguard Workersub fix_inserted_deleted_lines {
2331*49cdfc7eSAndroid Build Coastguard Worker	my ($linesRef, $insertedRef, $deletedRef) = @_;
2332*49cdfc7eSAndroid Build Coastguard Worker
2333*49cdfc7eSAndroid Build Coastguard Worker	my $range_last_linenr = 0;
2334*49cdfc7eSAndroid Build Coastguard Worker	my $delta_offset = 0;
2335*49cdfc7eSAndroid Build Coastguard Worker
2336*49cdfc7eSAndroid Build Coastguard Worker	my $old_linenr = 0;
2337*49cdfc7eSAndroid Build Coastguard Worker	my $new_linenr = 0;
2338*49cdfc7eSAndroid Build Coastguard Worker
2339*49cdfc7eSAndroid Build Coastguard Worker	my $next_insert = 0;
2340*49cdfc7eSAndroid Build Coastguard Worker	my $next_delete = 0;
2341*49cdfc7eSAndroid Build Coastguard Worker
2342*49cdfc7eSAndroid Build Coastguard Worker	my @lines = ();
2343*49cdfc7eSAndroid Build Coastguard Worker
2344*49cdfc7eSAndroid Build Coastguard Worker	my $inserted = @{$insertedRef}[$next_insert++];
2345*49cdfc7eSAndroid Build Coastguard Worker	my $deleted = @{$deletedRef}[$next_delete++];
2346*49cdfc7eSAndroid Build Coastguard Worker
2347*49cdfc7eSAndroid Build Coastguard Worker	foreach my $old_line (@{$linesRef}) {
2348*49cdfc7eSAndroid Build Coastguard Worker		my $save_line = 1;
2349*49cdfc7eSAndroid Build Coastguard Worker		my $line = $old_line;	#don't modify the array
2350*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2351*49cdfc7eSAndroid Build Coastguard Worker			$delta_offset = 0;
2352*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2353*49cdfc7eSAndroid Build Coastguard Worker			$range_last_linenr = $new_linenr;
2354*49cdfc7eSAndroid Build Coastguard Worker			fixup_current_range(\$line, $delta_offset, 0);
2355*49cdfc7eSAndroid Build Coastguard Worker		}
2356*49cdfc7eSAndroid Build Coastguard Worker
2357*49cdfc7eSAndroid Build Coastguard Worker		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2358*49cdfc7eSAndroid Build Coastguard Worker			$deleted = @{$deletedRef}[$next_delete++];
2359*49cdfc7eSAndroid Build Coastguard Worker			$save_line = 0;
2360*49cdfc7eSAndroid Build Coastguard Worker			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2361*49cdfc7eSAndroid Build Coastguard Worker		}
2362*49cdfc7eSAndroid Build Coastguard Worker
2363*49cdfc7eSAndroid Build Coastguard Worker		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2364*49cdfc7eSAndroid Build Coastguard Worker			push(@lines, ${$inserted}{'LINE'});
2365*49cdfc7eSAndroid Build Coastguard Worker			$inserted = @{$insertedRef}[$next_insert++];
2366*49cdfc7eSAndroid Build Coastguard Worker			$new_linenr++;
2367*49cdfc7eSAndroid Build Coastguard Worker			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2368*49cdfc7eSAndroid Build Coastguard Worker		}
2369*49cdfc7eSAndroid Build Coastguard Worker
2370*49cdfc7eSAndroid Build Coastguard Worker		if ($save_line) {
2371*49cdfc7eSAndroid Build Coastguard Worker			push(@lines, $line);
2372*49cdfc7eSAndroid Build Coastguard Worker			$new_linenr++;
2373*49cdfc7eSAndroid Build Coastguard Worker		}
2374*49cdfc7eSAndroid Build Coastguard Worker
2375*49cdfc7eSAndroid Build Coastguard Worker		$old_linenr++;
2376*49cdfc7eSAndroid Build Coastguard Worker	}
2377*49cdfc7eSAndroid Build Coastguard Worker
2378*49cdfc7eSAndroid Build Coastguard Worker	return @lines;
2379*49cdfc7eSAndroid Build Coastguard Worker}
2380*49cdfc7eSAndroid Build Coastguard Worker
2381*49cdfc7eSAndroid Build Coastguard Workersub fix_insert_line {
2382*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $line) = @_;
2383*49cdfc7eSAndroid Build Coastguard Worker
2384*49cdfc7eSAndroid Build Coastguard Worker	my $inserted = {
2385*49cdfc7eSAndroid Build Coastguard Worker		LINENR => $linenr,
2386*49cdfc7eSAndroid Build Coastguard Worker		LINE => $line,
2387*49cdfc7eSAndroid Build Coastguard Worker	};
2388*49cdfc7eSAndroid Build Coastguard Worker	push(@fixed_inserted, $inserted);
2389*49cdfc7eSAndroid Build Coastguard Worker}
2390*49cdfc7eSAndroid Build Coastguard Worker
2391*49cdfc7eSAndroid Build Coastguard Workersub fix_delete_line {
2392*49cdfc7eSAndroid Build Coastguard Worker	my ($linenr, $line) = @_;
2393*49cdfc7eSAndroid Build Coastguard Worker
2394*49cdfc7eSAndroid Build Coastguard Worker	my $deleted = {
2395*49cdfc7eSAndroid Build Coastguard Worker		LINENR => $linenr,
2396*49cdfc7eSAndroid Build Coastguard Worker		LINE => $line,
2397*49cdfc7eSAndroid Build Coastguard Worker	};
2398*49cdfc7eSAndroid Build Coastguard Worker
2399*49cdfc7eSAndroid Build Coastguard Worker	push(@fixed_deleted, $deleted);
2400*49cdfc7eSAndroid Build Coastguard Worker}
2401*49cdfc7eSAndroid Build Coastguard Worker
2402*49cdfc7eSAndroid Build Coastguard Workersub ERROR {
2403*49cdfc7eSAndroid Build Coastguard Worker	my ($type, $msg) = @_;
2404*49cdfc7eSAndroid Build Coastguard Worker
2405*49cdfc7eSAndroid Build Coastguard Worker	if (report("ERROR", $type, $msg)) {
2406*49cdfc7eSAndroid Build Coastguard Worker		our $clean = 0;
2407*49cdfc7eSAndroid Build Coastguard Worker		our $cnt_error++;
2408*49cdfc7eSAndroid Build Coastguard Worker		return 1;
2409*49cdfc7eSAndroid Build Coastguard Worker	}
2410*49cdfc7eSAndroid Build Coastguard Worker	return 0;
2411*49cdfc7eSAndroid Build Coastguard Worker}
2412*49cdfc7eSAndroid Build Coastguard Workersub WARN {
2413*49cdfc7eSAndroid Build Coastguard Worker	my ($type, $msg) = @_;
2414*49cdfc7eSAndroid Build Coastguard Worker
2415*49cdfc7eSAndroid Build Coastguard Worker	if (report("WARNING", $type, $msg)) {
2416*49cdfc7eSAndroid Build Coastguard Worker		our $clean = 0;
2417*49cdfc7eSAndroid Build Coastguard Worker		our $cnt_warn++;
2418*49cdfc7eSAndroid Build Coastguard Worker		return 1;
2419*49cdfc7eSAndroid Build Coastguard Worker	}
2420*49cdfc7eSAndroid Build Coastguard Worker	return 0;
2421*49cdfc7eSAndroid Build Coastguard Worker}
2422*49cdfc7eSAndroid Build Coastguard Workersub CHK {
2423*49cdfc7eSAndroid Build Coastguard Worker	my ($type, $msg) = @_;
2424*49cdfc7eSAndroid Build Coastguard Worker
2425*49cdfc7eSAndroid Build Coastguard Worker	if ($check && report("CHECK", $type, $msg)) {
2426*49cdfc7eSAndroid Build Coastguard Worker		our $clean = 0;
2427*49cdfc7eSAndroid Build Coastguard Worker		our $cnt_chk++;
2428*49cdfc7eSAndroid Build Coastguard Worker		return 1;
2429*49cdfc7eSAndroid Build Coastguard Worker	}
2430*49cdfc7eSAndroid Build Coastguard Worker	return 0;
2431*49cdfc7eSAndroid Build Coastguard Worker}
2432*49cdfc7eSAndroid Build Coastguard Worker
2433*49cdfc7eSAndroid Build Coastguard Workersub check_absolute_file {
2434*49cdfc7eSAndroid Build Coastguard Worker	my ($absolute, $herecurr) = @_;
2435*49cdfc7eSAndroid Build Coastguard Worker	my $file = $absolute;
2436*49cdfc7eSAndroid Build Coastguard Worker
2437*49cdfc7eSAndroid Build Coastguard Worker	##print "absolute<$absolute>\n";
2438*49cdfc7eSAndroid Build Coastguard Worker
2439*49cdfc7eSAndroid Build Coastguard Worker	# See if any suffix of this path is a path within the tree.
2440*49cdfc7eSAndroid Build Coastguard Worker	while ($file =~ s@^[^/]*/@@) {
2441*49cdfc7eSAndroid Build Coastguard Worker		if (-f "$root/$file") {
2442*49cdfc7eSAndroid Build Coastguard Worker			##print "file<$file>\n";
2443*49cdfc7eSAndroid Build Coastguard Worker			last;
2444*49cdfc7eSAndroid Build Coastguard Worker		}
2445*49cdfc7eSAndroid Build Coastguard Worker	}
2446*49cdfc7eSAndroid Build Coastguard Worker	if (! -f _)  {
2447*49cdfc7eSAndroid Build Coastguard Worker		return 0;
2448*49cdfc7eSAndroid Build Coastguard Worker	}
2449*49cdfc7eSAndroid Build Coastguard Worker
2450*49cdfc7eSAndroid Build Coastguard Worker	# It is, so see if the prefix is acceptable.
2451*49cdfc7eSAndroid Build Coastguard Worker	my $prefix = $absolute;
2452*49cdfc7eSAndroid Build Coastguard Worker	substr($prefix, -length($file)) = '';
2453*49cdfc7eSAndroid Build Coastguard Worker
2454*49cdfc7eSAndroid Build Coastguard Worker	##print "prefix<$prefix>\n";
2455*49cdfc7eSAndroid Build Coastguard Worker	if ($prefix ne ".../") {
2456*49cdfc7eSAndroid Build Coastguard Worker		WARN("USE_RELATIVE_PATH",
2457*49cdfc7eSAndroid Build Coastguard Worker		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
2458*49cdfc7eSAndroid Build Coastguard Worker	}
2459*49cdfc7eSAndroid Build Coastguard Worker}
2460*49cdfc7eSAndroid Build Coastguard Worker
2461*49cdfc7eSAndroid Build Coastguard Workersub trim {
2462*49cdfc7eSAndroid Build Coastguard Worker	my ($string) = @_;
2463*49cdfc7eSAndroid Build Coastguard Worker
2464*49cdfc7eSAndroid Build Coastguard Worker	$string =~ s/^\s+|\s+$//g;
2465*49cdfc7eSAndroid Build Coastguard Worker
2466*49cdfc7eSAndroid Build Coastguard Worker	return $string;
2467*49cdfc7eSAndroid Build Coastguard Worker}
2468*49cdfc7eSAndroid Build Coastguard Worker
2469*49cdfc7eSAndroid Build Coastguard Workersub ltrim {
2470*49cdfc7eSAndroid Build Coastguard Worker	my ($string) = @_;
2471*49cdfc7eSAndroid Build Coastguard Worker
2472*49cdfc7eSAndroid Build Coastguard Worker	$string =~ s/^\s+//;
2473*49cdfc7eSAndroid Build Coastguard Worker
2474*49cdfc7eSAndroid Build Coastguard Worker	return $string;
2475*49cdfc7eSAndroid Build Coastguard Worker}
2476*49cdfc7eSAndroid Build Coastguard Worker
2477*49cdfc7eSAndroid Build Coastguard Workersub rtrim {
2478*49cdfc7eSAndroid Build Coastguard Worker	my ($string) = @_;
2479*49cdfc7eSAndroid Build Coastguard Worker
2480*49cdfc7eSAndroid Build Coastguard Worker	$string =~ s/\s+$//;
2481*49cdfc7eSAndroid Build Coastguard Worker
2482*49cdfc7eSAndroid Build Coastguard Worker	return $string;
2483*49cdfc7eSAndroid Build Coastguard Worker}
2484*49cdfc7eSAndroid Build Coastguard Worker
2485*49cdfc7eSAndroid Build Coastguard Workersub string_find_replace {
2486*49cdfc7eSAndroid Build Coastguard Worker	my ($string, $find, $replace) = @_;
2487*49cdfc7eSAndroid Build Coastguard Worker
2488*49cdfc7eSAndroid Build Coastguard Worker	$string =~ s/$find/$replace/g;
2489*49cdfc7eSAndroid Build Coastguard Worker
2490*49cdfc7eSAndroid Build Coastguard Worker	return $string;
2491*49cdfc7eSAndroid Build Coastguard Worker}
2492*49cdfc7eSAndroid Build Coastguard Worker
2493*49cdfc7eSAndroid Build Coastguard Workersub tabify {
2494*49cdfc7eSAndroid Build Coastguard Worker	my ($leading) = @_;
2495*49cdfc7eSAndroid Build Coastguard Worker
2496*49cdfc7eSAndroid Build Coastguard Worker	my $source_indent = $tabsize;
2497*49cdfc7eSAndroid Build Coastguard Worker	my $max_spaces_before_tab = $source_indent - 1;
2498*49cdfc7eSAndroid Build Coastguard Worker	my $spaces_to_tab = " " x $source_indent;
2499*49cdfc7eSAndroid Build Coastguard Worker
2500*49cdfc7eSAndroid Build Coastguard Worker	#convert leading spaces to tabs
2501*49cdfc7eSAndroid Build Coastguard Worker	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2502*49cdfc7eSAndroid Build Coastguard Worker	#Remove spaces before a tab
2503*49cdfc7eSAndroid Build Coastguard Worker	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2504*49cdfc7eSAndroid Build Coastguard Worker
2505*49cdfc7eSAndroid Build Coastguard Worker	return "$leading";
2506*49cdfc7eSAndroid Build Coastguard Worker}
2507*49cdfc7eSAndroid Build Coastguard Worker
2508*49cdfc7eSAndroid Build Coastguard Workersub pos_last_openparen {
2509*49cdfc7eSAndroid Build Coastguard Worker	my ($line) = @_;
2510*49cdfc7eSAndroid Build Coastguard Worker
2511*49cdfc7eSAndroid Build Coastguard Worker	my $pos = 0;
2512*49cdfc7eSAndroid Build Coastguard Worker
2513*49cdfc7eSAndroid Build Coastguard Worker	my $opens = $line =~ tr/\(/\(/;
2514*49cdfc7eSAndroid Build Coastguard Worker	my $closes = $line =~ tr/\)/\)/;
2515*49cdfc7eSAndroid Build Coastguard Worker
2516*49cdfc7eSAndroid Build Coastguard Worker	my $last_openparen = 0;
2517*49cdfc7eSAndroid Build Coastguard Worker
2518*49cdfc7eSAndroid Build Coastguard Worker	if (($opens == 0) || ($closes >= $opens)) {
2519*49cdfc7eSAndroid Build Coastguard Worker		return -1;
2520*49cdfc7eSAndroid Build Coastguard Worker	}
2521*49cdfc7eSAndroid Build Coastguard Worker
2522*49cdfc7eSAndroid Build Coastguard Worker	my $len = length($line);
2523*49cdfc7eSAndroid Build Coastguard Worker
2524*49cdfc7eSAndroid Build Coastguard Worker	for ($pos = 0; $pos < $len; $pos++) {
2525*49cdfc7eSAndroid Build Coastguard Worker		my $string = substr($line, $pos);
2526*49cdfc7eSAndroid Build Coastguard Worker		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2527*49cdfc7eSAndroid Build Coastguard Worker			$pos += length($1) - 1;
2528*49cdfc7eSAndroid Build Coastguard Worker		} elsif (substr($line, $pos, 1) eq '(') {
2529*49cdfc7eSAndroid Build Coastguard Worker			$last_openparen = $pos;
2530*49cdfc7eSAndroid Build Coastguard Worker		} elsif (index($string, '(') == -1) {
2531*49cdfc7eSAndroid Build Coastguard Worker			last;
2532*49cdfc7eSAndroid Build Coastguard Worker		}
2533*49cdfc7eSAndroid Build Coastguard Worker	}
2534*49cdfc7eSAndroid Build Coastguard Worker
2535*49cdfc7eSAndroid Build Coastguard Worker	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2536*49cdfc7eSAndroid Build Coastguard Worker}
2537*49cdfc7eSAndroid Build Coastguard Worker
2538*49cdfc7eSAndroid Build Coastguard Workersub get_raw_comment {
2539*49cdfc7eSAndroid Build Coastguard Worker	my ($line, $rawline) = @_;
2540*49cdfc7eSAndroid Build Coastguard Worker	my $comment = '';
2541*49cdfc7eSAndroid Build Coastguard Worker
2542*49cdfc7eSAndroid Build Coastguard Worker	for my $i (0 .. (length($line) - 1)) {
2543*49cdfc7eSAndroid Build Coastguard Worker		if (substr($line, $i, 1) eq "$;") {
2544*49cdfc7eSAndroid Build Coastguard Worker			$comment .= substr($rawline, $i, 1);
2545*49cdfc7eSAndroid Build Coastguard Worker		}
2546*49cdfc7eSAndroid Build Coastguard Worker	}
2547*49cdfc7eSAndroid Build Coastguard Worker
2548*49cdfc7eSAndroid Build Coastguard Worker	return $comment;
2549*49cdfc7eSAndroid Build Coastguard Worker}
2550*49cdfc7eSAndroid Build Coastguard Worker
2551*49cdfc7eSAndroid Build Coastguard Workersub exclude_global_initialisers {
2552*49cdfc7eSAndroid Build Coastguard Worker	my ($realfile) = @_;
2553*49cdfc7eSAndroid Build Coastguard Worker
2554*49cdfc7eSAndroid Build Coastguard Worker	# Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
2555*49cdfc7eSAndroid Build Coastguard Worker	return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
2556*49cdfc7eSAndroid Build Coastguard Worker		$realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
2557*49cdfc7eSAndroid Build Coastguard Worker		$realfile =~ m@/bpf/.*\.bpf\.c$@;
2558*49cdfc7eSAndroid Build Coastguard Worker}
2559*49cdfc7eSAndroid Build Coastguard Worker
2560*49cdfc7eSAndroid Build Coastguard Workersub process {
2561*49cdfc7eSAndroid Build Coastguard Worker	my $filename = shift;
2562*49cdfc7eSAndroid Build Coastguard Worker
2563*49cdfc7eSAndroid Build Coastguard Worker	my $linenr=0;
2564*49cdfc7eSAndroid Build Coastguard Worker	my $prevline="";
2565*49cdfc7eSAndroid Build Coastguard Worker	my $prevrawline="";
2566*49cdfc7eSAndroid Build Coastguard Worker	my $stashline="";
2567*49cdfc7eSAndroid Build Coastguard Worker	my $stashrawline="";
2568*49cdfc7eSAndroid Build Coastguard Worker
2569*49cdfc7eSAndroid Build Coastguard Worker	my $length;
2570*49cdfc7eSAndroid Build Coastguard Worker	my $indent;
2571*49cdfc7eSAndroid Build Coastguard Worker	my $previndent=0;
2572*49cdfc7eSAndroid Build Coastguard Worker	my $stashindent=0;
2573*49cdfc7eSAndroid Build Coastguard Worker
2574*49cdfc7eSAndroid Build Coastguard Worker	our $clean = 1;
2575*49cdfc7eSAndroid Build Coastguard Worker	my $signoff = 0;
2576*49cdfc7eSAndroid Build Coastguard Worker	my $author = '';
2577*49cdfc7eSAndroid Build Coastguard Worker	my $authorsignoff = 0;
2578*49cdfc7eSAndroid Build Coastguard Worker	my $author_sob = '';
2579*49cdfc7eSAndroid Build Coastguard Worker	my $is_patch = 0;
2580*49cdfc7eSAndroid Build Coastguard Worker	my $is_binding_patch = -1;
2581*49cdfc7eSAndroid Build Coastguard Worker	my $in_header_lines = $file ? 0 : 1;
2582*49cdfc7eSAndroid Build Coastguard Worker	my $in_commit_log = 0;		#Scanning lines before patch
2583*49cdfc7eSAndroid Build Coastguard Worker	my $has_patch_separator = 0;	#Found a --- line
2584*49cdfc7eSAndroid Build Coastguard Worker	my $has_commit_log = 0;		#Encountered lines before patch
2585*49cdfc7eSAndroid Build Coastguard Worker	my $commit_log_lines = 0;	#Number of commit log lines
2586*49cdfc7eSAndroid Build Coastguard Worker	my $commit_log_possible_stack_dump = 0;
2587*49cdfc7eSAndroid Build Coastguard Worker	my $commit_log_long_line = 0;
2588*49cdfc7eSAndroid Build Coastguard Worker	my $commit_log_has_diff = 0;
2589*49cdfc7eSAndroid Build Coastguard Worker	my $reported_maintainer_file = 0;
2590*49cdfc7eSAndroid Build Coastguard Worker	my $non_utf8_charset = 0;
2591*49cdfc7eSAndroid Build Coastguard Worker
2592*49cdfc7eSAndroid Build Coastguard Worker	my $last_git_commit_id_linenr = -1;
2593*49cdfc7eSAndroid Build Coastguard Worker
2594*49cdfc7eSAndroid Build Coastguard Worker	my $last_blank_line = 0;
2595*49cdfc7eSAndroid Build Coastguard Worker	my $last_coalesced_string_linenr = -1;
2596*49cdfc7eSAndroid Build Coastguard Worker
2597*49cdfc7eSAndroid Build Coastguard Worker	our @report = ();
2598*49cdfc7eSAndroid Build Coastguard Worker	our $cnt_lines = 0;
2599*49cdfc7eSAndroid Build Coastguard Worker	our $cnt_error = 0;
2600*49cdfc7eSAndroid Build Coastguard Worker	our $cnt_warn = 0;
2601*49cdfc7eSAndroid Build Coastguard Worker	our $cnt_chk = 0;
2602*49cdfc7eSAndroid Build Coastguard Worker
2603*49cdfc7eSAndroid Build Coastguard Worker	# Trace the real file/line as we go.
2604*49cdfc7eSAndroid Build Coastguard Worker	my $realfile = '';
2605*49cdfc7eSAndroid Build Coastguard Worker	my $realline = 0;
2606*49cdfc7eSAndroid Build Coastguard Worker	my $realcnt = 0;
2607*49cdfc7eSAndroid Build Coastguard Worker	my $here = '';
2608*49cdfc7eSAndroid Build Coastguard Worker	my $context_function;		#undef'd unless there's a known function
2609*49cdfc7eSAndroid Build Coastguard Worker	my $in_comment = 0;
2610*49cdfc7eSAndroid Build Coastguard Worker	my $comment_edge = 0;
2611*49cdfc7eSAndroid Build Coastguard Worker	my $first_line = 0;
2612*49cdfc7eSAndroid Build Coastguard Worker	my $p1_prefix = '';
2613*49cdfc7eSAndroid Build Coastguard Worker
2614*49cdfc7eSAndroid Build Coastguard Worker	my $prev_values = 'E';
2615*49cdfc7eSAndroid Build Coastguard Worker
2616*49cdfc7eSAndroid Build Coastguard Worker	# suppression flags
2617*49cdfc7eSAndroid Build Coastguard Worker	my %suppress_ifbraces;
2618*49cdfc7eSAndroid Build Coastguard Worker	my %suppress_whiletrailers;
2619*49cdfc7eSAndroid Build Coastguard Worker	my %suppress_export;
2620*49cdfc7eSAndroid Build Coastguard Worker	my $suppress_statement = 0;
2621*49cdfc7eSAndroid Build Coastguard Worker
2622*49cdfc7eSAndroid Build Coastguard Worker	my %signatures = ();
2623*49cdfc7eSAndroid Build Coastguard Worker
2624*49cdfc7eSAndroid Build Coastguard Worker	# Pre-scan the patch sanitizing the lines.
2625*49cdfc7eSAndroid Build Coastguard Worker	# Pre-scan the patch looking for any __setup documentation.
2626*49cdfc7eSAndroid Build Coastguard Worker	#
2627*49cdfc7eSAndroid Build Coastguard Worker	my @setup_docs = ();
2628*49cdfc7eSAndroid Build Coastguard Worker	my $setup_docs = 0;
2629*49cdfc7eSAndroid Build Coastguard Worker
2630*49cdfc7eSAndroid Build Coastguard Worker	my $camelcase_file_seeded = 0;
2631*49cdfc7eSAndroid Build Coastguard Worker
2632*49cdfc7eSAndroid Build Coastguard Worker	my $checklicenseline = 1;
2633*49cdfc7eSAndroid Build Coastguard Worker
2634*49cdfc7eSAndroid Build Coastguard Worker	sanitise_line_reset();
2635*49cdfc7eSAndroid Build Coastguard Worker	my $line;
2636*49cdfc7eSAndroid Build Coastguard Worker	foreach my $rawline (@rawlines) {
2637*49cdfc7eSAndroid Build Coastguard Worker		$linenr++;
2638*49cdfc7eSAndroid Build Coastguard Worker		$line = $rawline;
2639*49cdfc7eSAndroid Build Coastguard Worker
2640*49cdfc7eSAndroid Build Coastguard Worker		push(@fixed, $rawline) if ($fix);
2641*49cdfc7eSAndroid Build Coastguard Worker
2642*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2643*49cdfc7eSAndroid Build Coastguard Worker			$setup_docs = 0;
2644*49cdfc7eSAndroid Build Coastguard Worker			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2645*49cdfc7eSAndroid Build Coastguard Worker				$setup_docs = 1;
2646*49cdfc7eSAndroid Build Coastguard Worker			}
2647*49cdfc7eSAndroid Build Coastguard Worker			#next;
2648*49cdfc7eSAndroid Build Coastguard Worker		}
2649*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2650*49cdfc7eSAndroid Build Coastguard Worker			$realline=$1-1;
2651*49cdfc7eSAndroid Build Coastguard Worker			if (defined $2) {
2652*49cdfc7eSAndroid Build Coastguard Worker				$realcnt=$3+1;
2653*49cdfc7eSAndroid Build Coastguard Worker			} else {
2654*49cdfc7eSAndroid Build Coastguard Worker				$realcnt=1+1;
2655*49cdfc7eSAndroid Build Coastguard Worker			}
2656*49cdfc7eSAndroid Build Coastguard Worker			$in_comment = 0;
2657*49cdfc7eSAndroid Build Coastguard Worker
2658*49cdfc7eSAndroid Build Coastguard Worker			# Guestimate if this is a continuing comment.  Run
2659*49cdfc7eSAndroid Build Coastguard Worker			# the context looking for a comment "edge".  If this
2660*49cdfc7eSAndroid Build Coastguard Worker			# edge is a close comment then we must be in a comment
2661*49cdfc7eSAndroid Build Coastguard Worker			# at context start.
2662*49cdfc7eSAndroid Build Coastguard Worker			my $edge;
2663*49cdfc7eSAndroid Build Coastguard Worker			my $cnt = $realcnt;
2664*49cdfc7eSAndroid Build Coastguard Worker			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2665*49cdfc7eSAndroid Build Coastguard Worker				next if (defined $rawlines[$ln - 1] &&
2666*49cdfc7eSAndroid Build Coastguard Worker					 $rawlines[$ln - 1] =~ /^-/);
2667*49cdfc7eSAndroid Build Coastguard Worker				$cnt--;
2668*49cdfc7eSAndroid Build Coastguard Worker				#print "RAW<$rawlines[$ln - 1]>\n";
2669*49cdfc7eSAndroid Build Coastguard Worker				last if (!defined $rawlines[$ln - 1]);
2670*49cdfc7eSAndroid Build Coastguard Worker				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2671*49cdfc7eSAndroid Build Coastguard Worker				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2672*49cdfc7eSAndroid Build Coastguard Worker					($edge) = $1;
2673*49cdfc7eSAndroid Build Coastguard Worker					last;
2674*49cdfc7eSAndroid Build Coastguard Worker				}
2675*49cdfc7eSAndroid Build Coastguard Worker			}
2676*49cdfc7eSAndroid Build Coastguard Worker			if (defined $edge && $edge eq '*/') {
2677*49cdfc7eSAndroid Build Coastguard Worker				$in_comment = 1;
2678*49cdfc7eSAndroid Build Coastguard Worker			}
2679*49cdfc7eSAndroid Build Coastguard Worker
2680*49cdfc7eSAndroid Build Coastguard Worker			# Guestimate if this is a continuing comment.  If this
2681*49cdfc7eSAndroid Build Coastguard Worker			# is the start of a diff block and this line starts
2682*49cdfc7eSAndroid Build Coastguard Worker			# ' *' then it is very likely a comment.
2683*49cdfc7eSAndroid Build Coastguard Worker			if (!defined $edge &&
2684*49cdfc7eSAndroid Build Coastguard Worker			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2685*49cdfc7eSAndroid Build Coastguard Worker			{
2686*49cdfc7eSAndroid Build Coastguard Worker				$in_comment = 1;
2687*49cdfc7eSAndroid Build Coastguard Worker			}
2688*49cdfc7eSAndroid Build Coastguard Worker
2689*49cdfc7eSAndroid Build Coastguard Worker			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2690*49cdfc7eSAndroid Build Coastguard Worker			sanitise_line_reset($in_comment);
2691*49cdfc7eSAndroid Build Coastguard Worker
2692*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2693*49cdfc7eSAndroid Build Coastguard Worker			# Standardise the strings and chars within the input to
2694*49cdfc7eSAndroid Build Coastguard Worker			# simplify matching -- only bother with positive lines.
2695*49cdfc7eSAndroid Build Coastguard Worker			$line = sanitise_line($rawline);
2696*49cdfc7eSAndroid Build Coastguard Worker		}
2697*49cdfc7eSAndroid Build Coastguard Worker		push(@lines, $line);
2698*49cdfc7eSAndroid Build Coastguard Worker
2699*49cdfc7eSAndroid Build Coastguard Worker		if ($realcnt > 1) {
2700*49cdfc7eSAndroid Build Coastguard Worker			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2701*49cdfc7eSAndroid Build Coastguard Worker		} else {
2702*49cdfc7eSAndroid Build Coastguard Worker			$realcnt = 0;
2703*49cdfc7eSAndroid Build Coastguard Worker		}
2704*49cdfc7eSAndroid Build Coastguard Worker
2705*49cdfc7eSAndroid Build Coastguard Worker		#print "==>$rawline\n";
2706*49cdfc7eSAndroid Build Coastguard Worker		#print "-->$line\n";
2707*49cdfc7eSAndroid Build Coastguard Worker
2708*49cdfc7eSAndroid Build Coastguard Worker		if ($setup_docs && $line =~ /^\+/) {
2709*49cdfc7eSAndroid Build Coastguard Worker			push(@setup_docs, $line);
2710*49cdfc7eSAndroid Build Coastguard Worker		}
2711*49cdfc7eSAndroid Build Coastguard Worker	}
2712*49cdfc7eSAndroid Build Coastguard Worker
2713*49cdfc7eSAndroid Build Coastguard Worker	$prefix = '';
2714*49cdfc7eSAndroid Build Coastguard Worker
2715*49cdfc7eSAndroid Build Coastguard Worker	$realcnt = 0;
2716*49cdfc7eSAndroid Build Coastguard Worker	$linenr = 0;
2717*49cdfc7eSAndroid Build Coastguard Worker	$fixlinenr = -1;
2718*49cdfc7eSAndroid Build Coastguard Worker	foreach my $line (@lines) {
2719*49cdfc7eSAndroid Build Coastguard Worker		$linenr++;
2720*49cdfc7eSAndroid Build Coastguard Worker		$fixlinenr++;
2721*49cdfc7eSAndroid Build Coastguard Worker		my $sline = $line;	#copy of $line
2722*49cdfc7eSAndroid Build Coastguard Worker		$sline =~ s/$;/ /g;	#with comments as spaces
2723*49cdfc7eSAndroid Build Coastguard Worker
2724*49cdfc7eSAndroid Build Coastguard Worker		my $rawline = $rawlines[$linenr - 1];
2725*49cdfc7eSAndroid Build Coastguard Worker		my $raw_comment = get_raw_comment($line, $rawline);
2726*49cdfc7eSAndroid Build Coastguard Worker
2727*49cdfc7eSAndroid Build Coastguard Worker# check if it's a mode change, rename or start of a patch
2728*49cdfc7eSAndroid Build Coastguard Worker		if (!$in_commit_log &&
2729*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
2730*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
2731*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
2732*49cdfc7eSAndroid Build Coastguard Worker			$is_patch = 1;
2733*49cdfc7eSAndroid Build Coastguard Worker		}
2734*49cdfc7eSAndroid Build Coastguard Worker
2735*49cdfc7eSAndroid Build Coastguard Worker#extract the line range in the file after the patch is applied
2736*49cdfc7eSAndroid Build Coastguard Worker		if (!$in_commit_log &&
2737*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2738*49cdfc7eSAndroid Build Coastguard Worker			my $context = $4;
2739*49cdfc7eSAndroid Build Coastguard Worker			$is_patch = 1;
2740*49cdfc7eSAndroid Build Coastguard Worker			$first_line = $linenr + 1;
2741*49cdfc7eSAndroid Build Coastguard Worker			$realline=$1-1;
2742*49cdfc7eSAndroid Build Coastguard Worker			if (defined $2) {
2743*49cdfc7eSAndroid Build Coastguard Worker				$realcnt=$3+1;
2744*49cdfc7eSAndroid Build Coastguard Worker			} else {
2745*49cdfc7eSAndroid Build Coastguard Worker				$realcnt=1+1;
2746*49cdfc7eSAndroid Build Coastguard Worker			}
2747*49cdfc7eSAndroid Build Coastguard Worker			annotate_reset();
2748*49cdfc7eSAndroid Build Coastguard Worker			$prev_values = 'E';
2749*49cdfc7eSAndroid Build Coastguard Worker
2750*49cdfc7eSAndroid Build Coastguard Worker			%suppress_ifbraces = ();
2751*49cdfc7eSAndroid Build Coastguard Worker			%suppress_whiletrailers = ();
2752*49cdfc7eSAndroid Build Coastguard Worker			%suppress_export = ();
2753*49cdfc7eSAndroid Build Coastguard Worker			$suppress_statement = 0;
2754*49cdfc7eSAndroid Build Coastguard Worker			if ($context =~ /\b(\w+)\s*\(/) {
2755*49cdfc7eSAndroid Build Coastguard Worker				$context_function = $1;
2756*49cdfc7eSAndroid Build Coastguard Worker			} else {
2757*49cdfc7eSAndroid Build Coastguard Worker				undef $context_function;
2758*49cdfc7eSAndroid Build Coastguard Worker			}
2759*49cdfc7eSAndroid Build Coastguard Worker			next;
2760*49cdfc7eSAndroid Build Coastguard Worker
2761*49cdfc7eSAndroid Build Coastguard Worker# track the line number as we move through the hunk, note that
2762*49cdfc7eSAndroid Build Coastguard Worker# new versions of GNU diff omit the leading space on completely
2763*49cdfc7eSAndroid Build Coastguard Worker# blank context lines so we need to count that too.
2764*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($line =~ /^( |\+|$)/) {
2765*49cdfc7eSAndroid Build Coastguard Worker			$realline++;
2766*49cdfc7eSAndroid Build Coastguard Worker			$realcnt-- if ($realcnt != 0);
2767*49cdfc7eSAndroid Build Coastguard Worker
2768*49cdfc7eSAndroid Build Coastguard Worker			# Measure the line length and indent.
2769*49cdfc7eSAndroid Build Coastguard Worker			($length, $indent) = line_stats($rawline);
2770*49cdfc7eSAndroid Build Coastguard Worker
2771*49cdfc7eSAndroid Build Coastguard Worker			# Track the previous line.
2772*49cdfc7eSAndroid Build Coastguard Worker			($prevline, $stashline) = ($stashline, $line);
2773*49cdfc7eSAndroid Build Coastguard Worker			($previndent, $stashindent) = ($stashindent, $indent);
2774*49cdfc7eSAndroid Build Coastguard Worker			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2775*49cdfc7eSAndroid Build Coastguard Worker
2776*49cdfc7eSAndroid Build Coastguard Worker			#warn "line<$line>\n";
2777*49cdfc7eSAndroid Build Coastguard Worker
2778*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($realcnt == 1) {
2779*49cdfc7eSAndroid Build Coastguard Worker			$realcnt--;
2780*49cdfc7eSAndroid Build Coastguard Worker		}
2781*49cdfc7eSAndroid Build Coastguard Worker
2782*49cdfc7eSAndroid Build Coastguard Worker		my $hunk_line = ($realcnt != 0);
2783*49cdfc7eSAndroid Build Coastguard Worker
2784*49cdfc7eSAndroid Build Coastguard Worker		$here = "#$linenr: " if (!$file);
2785*49cdfc7eSAndroid Build Coastguard Worker		$here = "#$realline: " if ($file);
2786*49cdfc7eSAndroid Build Coastguard Worker
2787*49cdfc7eSAndroid Build Coastguard Worker		my $found_file = 0;
2788*49cdfc7eSAndroid Build Coastguard Worker		# extract the filename as it passes
2789*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^diff --git.*?(\S+)$/) {
2790*49cdfc7eSAndroid Build Coastguard Worker			$realfile = $1;
2791*49cdfc7eSAndroid Build Coastguard Worker			$realfile =~ s@^([^/]*)/@@ if (!$file);
2792*49cdfc7eSAndroid Build Coastguard Worker			$in_commit_log = 0;
2793*49cdfc7eSAndroid Build Coastguard Worker			$found_file = 1;
2794*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2795*49cdfc7eSAndroid Build Coastguard Worker			$realfile = $1;
2796*49cdfc7eSAndroid Build Coastguard Worker			$realfile =~ s@^([^/]*)/@@ if (!$file);
2797*49cdfc7eSAndroid Build Coastguard Worker			$in_commit_log = 0;
2798*49cdfc7eSAndroid Build Coastguard Worker
2799*49cdfc7eSAndroid Build Coastguard Worker			$p1_prefix = $1;
2800*49cdfc7eSAndroid Build Coastguard Worker			if (!$file && $tree && $p1_prefix ne '' &&
2801*49cdfc7eSAndroid Build Coastguard Worker			    -e "$root/$p1_prefix") {
2802*49cdfc7eSAndroid Build Coastguard Worker				WARN("PATCH_PREFIX",
2803*49cdfc7eSAndroid Build Coastguard Worker				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
2804*49cdfc7eSAndroid Build Coastguard Worker			}
2805*49cdfc7eSAndroid Build Coastguard Worker
2806*49cdfc7eSAndroid Build Coastguard Worker			if ($realfile =~ m@^include/asm/@) {
2807*49cdfc7eSAndroid Build Coastguard Worker				ERROR("MODIFIED_INCLUDE_ASM",
2808*49cdfc7eSAndroid Build Coastguard Worker				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2809*49cdfc7eSAndroid Build Coastguard Worker			}
2810*49cdfc7eSAndroid Build Coastguard Worker			$found_file = 1;
2811*49cdfc7eSAndroid Build Coastguard Worker		}
2812*49cdfc7eSAndroid Build Coastguard Worker
2813*49cdfc7eSAndroid Build Coastguard Worker#make up the handle for any error we report on this line
2814*49cdfc7eSAndroid Build Coastguard Worker		if ($showfile) {
2815*49cdfc7eSAndroid Build Coastguard Worker			$prefix = "$realfile:$realline: "
2816*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($emacs) {
2817*49cdfc7eSAndroid Build Coastguard Worker			if ($file) {
2818*49cdfc7eSAndroid Build Coastguard Worker				$prefix = "$filename:$realline: ";
2819*49cdfc7eSAndroid Build Coastguard Worker			} else {
2820*49cdfc7eSAndroid Build Coastguard Worker				$prefix = "$filename:$linenr: ";
2821*49cdfc7eSAndroid Build Coastguard Worker			}
2822*49cdfc7eSAndroid Build Coastguard Worker		}
2823*49cdfc7eSAndroid Build Coastguard Worker
2824*49cdfc7eSAndroid Build Coastguard Worker		if ($found_file) {
2825*49cdfc7eSAndroid Build Coastguard Worker			if (is_maintained_obsolete($realfile)) {
2826*49cdfc7eSAndroid Build Coastguard Worker				WARN("OBSOLETE",
2827*49cdfc7eSAndroid Build Coastguard Worker				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
2828*49cdfc7eSAndroid Build Coastguard Worker			}
2829*49cdfc7eSAndroid Build Coastguard Worker			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
2830*49cdfc7eSAndroid Build Coastguard Worker				$check = 1;
2831*49cdfc7eSAndroid Build Coastguard Worker			} else {
2832*49cdfc7eSAndroid Build Coastguard Worker				$check = $check_orig;
2833*49cdfc7eSAndroid Build Coastguard Worker			}
2834*49cdfc7eSAndroid Build Coastguard Worker			$checklicenseline = 1;
2835*49cdfc7eSAndroid Build Coastguard Worker
2836*49cdfc7eSAndroid Build Coastguard Worker			if ($realfile !~ /^MAINTAINERS/) {
2837*49cdfc7eSAndroid Build Coastguard Worker				my $last_binding_patch = $is_binding_patch;
2838*49cdfc7eSAndroid Build Coastguard Worker
2839*49cdfc7eSAndroid Build Coastguard Worker				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2840*49cdfc7eSAndroid Build Coastguard Worker
2841*49cdfc7eSAndroid Build Coastguard Worker				if (($last_binding_patch != -1) &&
2842*49cdfc7eSAndroid Build Coastguard Worker				    ($last_binding_patch ^ $is_binding_patch)) {
2843*49cdfc7eSAndroid Build Coastguard Worker					WARN("DT_SPLIT_BINDING_PATCH",
2844*49cdfc7eSAndroid Build Coastguard Worker					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2845*49cdfc7eSAndroid Build Coastguard Worker				}
2846*49cdfc7eSAndroid Build Coastguard Worker			}
2847*49cdfc7eSAndroid Build Coastguard Worker
2848*49cdfc7eSAndroid Build Coastguard Worker			next;
2849*49cdfc7eSAndroid Build Coastguard Worker		}
2850*49cdfc7eSAndroid Build Coastguard Worker
2851*49cdfc7eSAndroid Build Coastguard Worker		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
2852*49cdfc7eSAndroid Build Coastguard Worker
2853*49cdfc7eSAndroid Build Coastguard Worker		my $hereline = "$here\n$rawline\n";
2854*49cdfc7eSAndroid Build Coastguard Worker		my $herecurr = "$here\n$rawline\n";
2855*49cdfc7eSAndroid Build Coastguard Worker		my $hereprev = "$here\n$prevrawline\n$rawline\n";
2856*49cdfc7eSAndroid Build Coastguard Worker
2857*49cdfc7eSAndroid Build Coastguard Worker		$cnt_lines++ if ($realcnt != 0);
2858*49cdfc7eSAndroid Build Coastguard Worker
2859*49cdfc7eSAndroid Build Coastguard Worker# Verify the existence of a commit log if appropriate
2860*49cdfc7eSAndroid Build Coastguard Worker# 2 is used because a $signature is counted in $commit_log_lines
2861*49cdfc7eSAndroid Build Coastguard Worker		if ($in_commit_log) {
2862*49cdfc7eSAndroid Build Coastguard Worker			if ($line !~ /^\s*$/) {
2863*49cdfc7eSAndroid Build Coastguard Worker				$commit_log_lines++;	#could be a $signature
2864*49cdfc7eSAndroid Build Coastguard Worker			}
2865*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($has_commit_log && $commit_log_lines < 2) {
2866*49cdfc7eSAndroid Build Coastguard Worker			WARN("COMMIT_MESSAGE",
2867*49cdfc7eSAndroid Build Coastguard Worker			     "Missing commit description - Add an appropriate one\n");
2868*49cdfc7eSAndroid Build Coastguard Worker			$commit_log_lines = 2;	#warn only once
2869*49cdfc7eSAndroid Build Coastguard Worker		}
2870*49cdfc7eSAndroid Build Coastguard Worker
2871*49cdfc7eSAndroid Build Coastguard Worker# Check if the commit log has what seems like a diff which can confuse patch
2872*49cdfc7eSAndroid Build Coastguard Worker		if ($in_commit_log && !$commit_log_has_diff &&
2873*49cdfc7eSAndroid Build Coastguard Worker		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
2874*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2875*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2876*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2877*49cdfc7eSAndroid Build Coastguard Worker			ERROR("DIFF_IN_COMMIT_MSG",
2878*49cdfc7eSAndroid Build Coastguard Worker			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2879*49cdfc7eSAndroid Build Coastguard Worker			$commit_log_has_diff = 1;
2880*49cdfc7eSAndroid Build Coastguard Worker		}
2881*49cdfc7eSAndroid Build Coastguard Worker
2882*49cdfc7eSAndroid Build Coastguard Worker# Check for incorrect file permissions
2883*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2884*49cdfc7eSAndroid Build Coastguard Worker			my $permhere = $here . "FILE: $realfile\n";
2885*49cdfc7eSAndroid Build Coastguard Worker			if ($realfile !~ m@scripts/@ &&
2886*49cdfc7eSAndroid Build Coastguard Worker			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2887*49cdfc7eSAndroid Build Coastguard Worker				ERROR("EXECUTE_PERMISSIONS",
2888*49cdfc7eSAndroid Build Coastguard Worker				      "do not set execute permissions for source files\n" . $permhere);
2889*49cdfc7eSAndroid Build Coastguard Worker			}
2890*49cdfc7eSAndroid Build Coastguard Worker		}
2891*49cdfc7eSAndroid Build Coastguard Worker
2892*49cdfc7eSAndroid Build Coastguard Worker# Check the patch for a From:
2893*49cdfc7eSAndroid Build Coastguard Worker		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2894*49cdfc7eSAndroid Build Coastguard Worker			$author = $1;
2895*49cdfc7eSAndroid Build Coastguard Worker			my $curline = $linenr;
2896*49cdfc7eSAndroid Build Coastguard Worker			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2897*49cdfc7eSAndroid Build Coastguard Worker				$author .= $1;
2898*49cdfc7eSAndroid Build Coastguard Worker			}
2899*49cdfc7eSAndroid Build Coastguard Worker			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2900*49cdfc7eSAndroid Build Coastguard Worker			$author =~ s/"//g;
2901*49cdfc7eSAndroid Build Coastguard Worker			$author = reformat_email($author);
2902*49cdfc7eSAndroid Build Coastguard Worker		}
2903*49cdfc7eSAndroid Build Coastguard Worker
2904*49cdfc7eSAndroid Build Coastguard Worker# Check the patch for a signoff:
2905*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
2906*49cdfc7eSAndroid Build Coastguard Worker			$signoff++;
2907*49cdfc7eSAndroid Build Coastguard Worker			$in_commit_log = 0;
2908*49cdfc7eSAndroid Build Coastguard Worker			if ($author ne ''  && $authorsignoff != 1) {
2909*49cdfc7eSAndroid Build Coastguard Worker				if (same_email_addresses($1, $author)) {
2910*49cdfc7eSAndroid Build Coastguard Worker					$authorsignoff = 1;
2911*49cdfc7eSAndroid Build Coastguard Worker				} else {
2912*49cdfc7eSAndroid Build Coastguard Worker					my $ctx = $1;
2913*49cdfc7eSAndroid Build Coastguard Worker					my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
2914*49cdfc7eSAndroid Build Coastguard Worker					my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
2915*49cdfc7eSAndroid Build Coastguard Worker
2916*49cdfc7eSAndroid Build Coastguard Worker					if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
2917*49cdfc7eSAndroid Build Coastguard Worker						$author_sob = $ctx;
2918*49cdfc7eSAndroid Build Coastguard Worker						$authorsignoff = 2;
2919*49cdfc7eSAndroid Build Coastguard Worker					} elsif (lc $email_address eq lc $author_address) {
2920*49cdfc7eSAndroid Build Coastguard Worker						$author_sob = $ctx;
2921*49cdfc7eSAndroid Build Coastguard Worker						$authorsignoff = 3;
2922*49cdfc7eSAndroid Build Coastguard Worker					} elsif ($email_name eq $author_name) {
2923*49cdfc7eSAndroid Build Coastguard Worker						$author_sob = $ctx;
2924*49cdfc7eSAndroid Build Coastguard Worker						$authorsignoff = 4;
2925*49cdfc7eSAndroid Build Coastguard Worker
2926*49cdfc7eSAndroid Build Coastguard Worker						my $address1 = $email_address;
2927*49cdfc7eSAndroid Build Coastguard Worker						my $address2 = $author_address;
2928*49cdfc7eSAndroid Build Coastguard Worker
2929*49cdfc7eSAndroid Build Coastguard Worker						if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
2930*49cdfc7eSAndroid Build Coastguard Worker							$address1 = "$1$2";
2931*49cdfc7eSAndroid Build Coastguard Worker						}
2932*49cdfc7eSAndroid Build Coastguard Worker						if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
2933*49cdfc7eSAndroid Build Coastguard Worker							$address2 = "$1$2";
2934*49cdfc7eSAndroid Build Coastguard Worker						}
2935*49cdfc7eSAndroid Build Coastguard Worker						if ($address1 eq $address2) {
2936*49cdfc7eSAndroid Build Coastguard Worker							$authorsignoff = 5;
2937*49cdfc7eSAndroid Build Coastguard Worker						}
2938*49cdfc7eSAndroid Build Coastguard Worker					}
2939*49cdfc7eSAndroid Build Coastguard Worker				}
2940*49cdfc7eSAndroid Build Coastguard Worker			}
2941*49cdfc7eSAndroid Build Coastguard Worker		}
2942*49cdfc7eSAndroid Build Coastguard Worker
2943*49cdfc7eSAndroid Build Coastguard Worker# Check for patch separator
2944*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^---$/) {
2945*49cdfc7eSAndroid Build Coastguard Worker			$has_patch_separator = 1;
2946*49cdfc7eSAndroid Build Coastguard Worker			$in_commit_log = 0;
2947*49cdfc7eSAndroid Build Coastguard Worker		}
2948*49cdfc7eSAndroid Build Coastguard Worker
2949*49cdfc7eSAndroid Build Coastguard Worker# Check if MAINTAINERS is being updated.  If so, there's probably no need to
2950*49cdfc7eSAndroid Build Coastguard Worker# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2951*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2952*49cdfc7eSAndroid Build Coastguard Worker			$reported_maintainer_file = 1;
2953*49cdfc7eSAndroid Build Coastguard Worker		}
2954*49cdfc7eSAndroid Build Coastguard Worker
2955*49cdfc7eSAndroid Build Coastguard Worker# Check signature styles
2956*49cdfc7eSAndroid Build Coastguard Worker		if (!$in_header_lines &&
2957*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
2958*49cdfc7eSAndroid Build Coastguard Worker			my $space_before = $1;
2959*49cdfc7eSAndroid Build Coastguard Worker			my $sign_off = $2;
2960*49cdfc7eSAndroid Build Coastguard Worker			my $space_after = $3;
2961*49cdfc7eSAndroid Build Coastguard Worker			my $email = $4;
2962*49cdfc7eSAndroid Build Coastguard Worker			my $ucfirst_sign_off = ucfirst(lc($sign_off));
2963*49cdfc7eSAndroid Build Coastguard Worker
2964*49cdfc7eSAndroid Build Coastguard Worker			if ($sign_off !~ /$signature_tags/) {
2965*49cdfc7eSAndroid Build Coastguard Worker				my $suggested_signature = find_standard_signature($sign_off);
2966*49cdfc7eSAndroid Build Coastguard Worker				if ($suggested_signature eq "") {
2967*49cdfc7eSAndroid Build Coastguard Worker					WARN("BAD_SIGN_OFF",
2968*49cdfc7eSAndroid Build Coastguard Worker					     "Non-standard signature: $sign_off\n" . $herecurr);
2969*49cdfc7eSAndroid Build Coastguard Worker				} else {
2970*49cdfc7eSAndroid Build Coastguard Worker					if (WARN("BAD_SIGN_OFF",
2971*49cdfc7eSAndroid Build Coastguard Worker						 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
2972*49cdfc7eSAndroid Build Coastguard Worker					    $fix) {
2973*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
2974*49cdfc7eSAndroid Build Coastguard Worker					}
2975*49cdfc7eSAndroid Build Coastguard Worker				}
2976*49cdfc7eSAndroid Build Coastguard Worker			}
2977*49cdfc7eSAndroid Build Coastguard Worker			if (defined $space_before && $space_before ne "") {
2978*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("BAD_SIGN_OFF",
2979*49cdfc7eSAndroid Build Coastguard Worker					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2980*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
2981*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =
2982*49cdfc7eSAndroid Build Coastguard Worker					    "$ucfirst_sign_off $email";
2983*49cdfc7eSAndroid Build Coastguard Worker				}
2984*49cdfc7eSAndroid Build Coastguard Worker			}
2985*49cdfc7eSAndroid Build Coastguard Worker			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
2986*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("BAD_SIGN_OFF",
2987*49cdfc7eSAndroid Build Coastguard Worker					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2988*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
2989*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =
2990*49cdfc7eSAndroid Build Coastguard Worker					    "$ucfirst_sign_off $email";
2991*49cdfc7eSAndroid Build Coastguard Worker				}
2992*49cdfc7eSAndroid Build Coastguard Worker
2993*49cdfc7eSAndroid Build Coastguard Worker			}
2994*49cdfc7eSAndroid Build Coastguard Worker			if (!defined $space_after || $space_after ne " ") {
2995*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("BAD_SIGN_OFF",
2996*49cdfc7eSAndroid Build Coastguard Worker					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2997*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
2998*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =
2999*49cdfc7eSAndroid Build Coastguard Worker					    "$ucfirst_sign_off $email";
3000*49cdfc7eSAndroid Build Coastguard Worker				}
3001*49cdfc7eSAndroid Build Coastguard Worker			}
3002*49cdfc7eSAndroid Build Coastguard Worker
3003*49cdfc7eSAndroid Build Coastguard Worker			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
3004*49cdfc7eSAndroid Build Coastguard Worker			my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
3005*49cdfc7eSAndroid Build Coastguard Worker			if ($suggested_email eq "") {
3006*49cdfc7eSAndroid Build Coastguard Worker				ERROR("BAD_SIGN_OFF",
3007*49cdfc7eSAndroid Build Coastguard Worker				      "Unrecognized email address: '$email'\n" . $herecurr);
3008*49cdfc7eSAndroid Build Coastguard Worker			} else {
3009*49cdfc7eSAndroid Build Coastguard Worker				my $dequoted = $suggested_email;
3010*49cdfc7eSAndroid Build Coastguard Worker				$dequoted =~ s/^"//;
3011*49cdfc7eSAndroid Build Coastguard Worker				$dequoted =~ s/" </ </;
3012*49cdfc7eSAndroid Build Coastguard Worker				# Don't force email to have quotes
3013*49cdfc7eSAndroid Build Coastguard Worker				# Allow just an angle bracketed address
3014*49cdfc7eSAndroid Build Coastguard Worker				if (!same_email_addresses($email, $suggested_email)) {
3015*49cdfc7eSAndroid Build Coastguard Worker					if (WARN("BAD_SIGN_OFF",
3016*49cdfc7eSAndroid Build Coastguard Worker						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3017*49cdfc7eSAndroid Build Coastguard Worker					    $fix) {
3018*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3019*49cdfc7eSAndroid Build Coastguard Worker					}
3020*49cdfc7eSAndroid Build Coastguard Worker				}
3021*49cdfc7eSAndroid Build Coastguard Worker
3022*49cdfc7eSAndroid Build Coastguard Worker				# Address part shouldn't have comments
3023*49cdfc7eSAndroid Build Coastguard Worker				my $stripped_address = $email_address;
3024*49cdfc7eSAndroid Build Coastguard Worker				$stripped_address =~ s/\([^\(\)]*\)//g;
3025*49cdfc7eSAndroid Build Coastguard Worker				if ($email_address ne $stripped_address) {
3026*49cdfc7eSAndroid Build Coastguard Worker					if (WARN("BAD_SIGN_OFF",
3027*49cdfc7eSAndroid Build Coastguard Worker						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3028*49cdfc7eSAndroid Build Coastguard Worker					    $fix) {
3029*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3030*49cdfc7eSAndroid Build Coastguard Worker					}
3031*49cdfc7eSAndroid Build Coastguard Worker				}
3032*49cdfc7eSAndroid Build Coastguard Worker
3033*49cdfc7eSAndroid Build Coastguard Worker				# Only one name comment should be allowed
3034*49cdfc7eSAndroid Build Coastguard Worker				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3035*49cdfc7eSAndroid Build Coastguard Worker				if ($comment_count > 1) {
3036*49cdfc7eSAndroid Build Coastguard Worker					WARN("BAD_SIGN_OFF",
3037*49cdfc7eSAndroid Build Coastguard Worker					     "Use a single name comment in email: '$email'\n" . $herecurr);
3038*49cdfc7eSAndroid Build Coastguard Worker				}
3039*49cdfc7eSAndroid Build Coastguard Worker
3040*49cdfc7eSAndroid Build Coastguard Worker
3041*49cdfc7eSAndroid Build Coastguard Worker				# [email protected] or [email protected] shouldn't
3042*49cdfc7eSAndroid Build Coastguard Worker				# have an email name. In addition comments should strictly
3043*49cdfc7eSAndroid Build Coastguard Worker				# begin with a #
3044*49cdfc7eSAndroid Build Coastguard Worker				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3045*49cdfc7eSAndroid Build Coastguard Worker					if (($comment ne "" && $comment !~ /^#.+/) ||
3046*49cdfc7eSAndroid Build Coastguard Worker					    ($email_name ne "")) {
3047*49cdfc7eSAndroid Build Coastguard Worker						my $cur_name = $email_name;
3048*49cdfc7eSAndroid Build Coastguard Worker						my $new_comment = $comment;
3049*49cdfc7eSAndroid Build Coastguard Worker						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3050*49cdfc7eSAndroid Build Coastguard Worker
3051*49cdfc7eSAndroid Build Coastguard Worker						# Remove brackets enclosing comment text
3052*49cdfc7eSAndroid Build Coastguard Worker						# and # from start of comments to get comment text
3053*49cdfc7eSAndroid Build Coastguard Worker						$new_comment =~ s/^\((.*)\)$/$1/;
3054*49cdfc7eSAndroid Build Coastguard Worker						$new_comment =~ s/^\[(.*)\]$/$1/;
3055*49cdfc7eSAndroid Build Coastguard Worker						$new_comment =~ s/^[\s\#]+|\s+$//g;
3056*49cdfc7eSAndroid Build Coastguard Worker
3057*49cdfc7eSAndroid Build Coastguard Worker						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
3058*49cdfc7eSAndroid Build Coastguard Worker						$new_comment = " # $new_comment" if ($new_comment ne "");
3059*49cdfc7eSAndroid Build Coastguard Worker						my $new_email = "$email_address$new_comment";
3060*49cdfc7eSAndroid Build Coastguard Worker
3061*49cdfc7eSAndroid Build Coastguard Worker						if (WARN("BAD_STABLE_ADDRESS_STYLE",
3062*49cdfc7eSAndroid Build Coastguard Worker							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3063*49cdfc7eSAndroid Build Coastguard Worker						    $fix) {
3064*49cdfc7eSAndroid Build Coastguard Worker							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3065*49cdfc7eSAndroid Build Coastguard Worker						}
3066*49cdfc7eSAndroid Build Coastguard Worker					}
3067*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3068*49cdfc7eSAndroid Build Coastguard Worker					my $new_comment = $comment;
3069*49cdfc7eSAndroid Build Coastguard Worker
3070*49cdfc7eSAndroid Build Coastguard Worker					# Extract comment text from within brackets or
3071*49cdfc7eSAndroid Build Coastguard Worker					# c89 style /*...*/ comments
3072*49cdfc7eSAndroid Build Coastguard Worker					$new_comment =~ s/^\[(.*)\]$/$1/;
3073*49cdfc7eSAndroid Build Coastguard Worker					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3074*49cdfc7eSAndroid Build Coastguard Worker
3075*49cdfc7eSAndroid Build Coastguard Worker					$new_comment = trim($new_comment);
3076*49cdfc7eSAndroid Build Coastguard Worker					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3077*49cdfc7eSAndroid Build Coastguard Worker					$new_comment = "($new_comment)" if ($new_comment ne "");
3078*49cdfc7eSAndroid Build Coastguard Worker					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
3079*49cdfc7eSAndroid Build Coastguard Worker
3080*49cdfc7eSAndroid Build Coastguard Worker					if (WARN("BAD_SIGN_OFF",
3081*49cdfc7eSAndroid Build Coastguard Worker						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3082*49cdfc7eSAndroid Build Coastguard Worker					    $fix) {
3083*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3084*49cdfc7eSAndroid Build Coastguard Worker					}
3085*49cdfc7eSAndroid Build Coastguard Worker				}
3086*49cdfc7eSAndroid Build Coastguard Worker			}
3087*49cdfc7eSAndroid Build Coastguard Worker
3088*49cdfc7eSAndroid Build Coastguard Worker# Check for duplicate signatures
3089*49cdfc7eSAndroid Build Coastguard Worker			my $sig_nospace = $line;
3090*49cdfc7eSAndroid Build Coastguard Worker			$sig_nospace =~ s/\s//g;
3091*49cdfc7eSAndroid Build Coastguard Worker			$sig_nospace = lc($sig_nospace);
3092*49cdfc7eSAndroid Build Coastguard Worker			if (defined $signatures{$sig_nospace}) {
3093*49cdfc7eSAndroid Build Coastguard Worker				WARN("BAD_SIGN_OFF",
3094*49cdfc7eSAndroid Build Coastguard Worker				     "Duplicate signature\n" . $herecurr);
3095*49cdfc7eSAndroid Build Coastguard Worker			} else {
3096*49cdfc7eSAndroid Build Coastguard Worker				$signatures{$sig_nospace} = 1;
3097*49cdfc7eSAndroid Build Coastguard Worker			}
3098*49cdfc7eSAndroid Build Coastguard Worker
3099*49cdfc7eSAndroid Build Coastguard Worker# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
3100*49cdfc7eSAndroid Build Coastguard Worker			if ($sign_off =~ /^co-developed-by:$/i) {
3101*49cdfc7eSAndroid Build Coastguard Worker				if ($email eq $author) {
3102*49cdfc7eSAndroid Build Coastguard Worker					WARN("BAD_SIGN_OFF",
3103*49cdfc7eSAndroid Build Coastguard Worker					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . "$here\n" . $rawline);
3104*49cdfc7eSAndroid Build Coastguard Worker				}
3105*49cdfc7eSAndroid Build Coastguard Worker				if (!defined $lines[$linenr]) {
3106*49cdfc7eSAndroid Build Coastguard Worker					WARN("BAD_SIGN_OFF",
3107*49cdfc7eSAndroid Build Coastguard Worker					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline);
3108*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($rawlines[$linenr] !~ /^\s*signed-off-by:\s*(.*)/i) {
3109*49cdfc7eSAndroid Build Coastguard Worker					WARN("BAD_SIGN_OFF",
3110*49cdfc7eSAndroid Build Coastguard Worker					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3111*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($1 ne $email) {
3112*49cdfc7eSAndroid Build Coastguard Worker					WARN("BAD_SIGN_OFF",
3113*49cdfc7eSAndroid Build Coastguard Worker					     "Co-developed-by and Signed-off-by: name/email do not match \n" . "$here\n" . $rawline . "\n" .$rawlines[$linenr]);
3114*49cdfc7eSAndroid Build Coastguard Worker				}
3115*49cdfc7eSAndroid Build Coastguard Worker			}
3116*49cdfc7eSAndroid Build Coastguard Worker		}
3117*49cdfc7eSAndroid Build Coastguard Worker
3118*49cdfc7eSAndroid Build Coastguard Worker# Check email subject for common tools that don't need to be mentioned
3119*49cdfc7eSAndroid Build Coastguard Worker		if ($in_header_lines &&
3120*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3121*49cdfc7eSAndroid Build Coastguard Worker			WARN("EMAIL_SUBJECT",
3122*49cdfc7eSAndroid Build Coastguard Worker			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3123*49cdfc7eSAndroid Build Coastguard Worker		}
3124*49cdfc7eSAndroid Build Coastguard Worker
3125*49cdfc7eSAndroid Build Coastguard Worker# Check for Gerrit Change-Ids not in any patch context
3126*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
3127*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("GERRIT_CHANGE_ID",
3128*49cdfc7eSAndroid Build Coastguard Worker			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
3129*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3130*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr, $rawline);
3131*49cdfc7eSAndroid Build Coastguard Worker			}
3132*49cdfc7eSAndroid Build Coastguard Worker		}
3133*49cdfc7eSAndroid Build Coastguard Worker
3134*49cdfc7eSAndroid Build Coastguard Worker# Check if the commit log is in a possible stack dump
3135*49cdfc7eSAndroid Build Coastguard Worker		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3136*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3137*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3138*49cdfc7eSAndroid Build Coastguard Worker					# timestamp
3139*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3140*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3141*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3142*49cdfc7eSAndroid Build Coastguard Worker					# stack dump address styles
3143*49cdfc7eSAndroid Build Coastguard Worker			$commit_log_possible_stack_dump = 1;
3144*49cdfc7eSAndroid Build Coastguard Worker		}
3145*49cdfc7eSAndroid Build Coastguard Worker
3146*49cdfc7eSAndroid Build Coastguard Worker# Check for line lengths > 75 in commit log, warn once
3147*49cdfc7eSAndroid Build Coastguard Worker		if ($in_commit_log && !$commit_log_long_line &&
3148*49cdfc7eSAndroid Build Coastguard Worker		    length($line) > 75 &&
3149*49cdfc7eSAndroid Build Coastguard Worker		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3150*49cdfc7eSAndroid Build Coastguard Worker					# file delta changes
3151*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
3152*49cdfc7eSAndroid Build Coastguard Worker					# filename then :
3153*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\s*(?:Fixes:|Link:|$signature_tags)/i ||
3154*49cdfc7eSAndroid Build Coastguard Worker					# A Fixes: or Link: line or signature tag line
3155*49cdfc7eSAndroid Build Coastguard Worker		      $commit_log_possible_stack_dump)) {
3156*49cdfc7eSAndroid Build Coastguard Worker			WARN("COMMIT_LOG_LONG_LINE",
3157*49cdfc7eSAndroid Build Coastguard Worker			     "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
3158*49cdfc7eSAndroid Build Coastguard Worker			$commit_log_long_line = 1;
3159*49cdfc7eSAndroid Build Coastguard Worker		}
3160*49cdfc7eSAndroid Build Coastguard Worker
3161*49cdfc7eSAndroid Build Coastguard Worker# Reset possible stack dump if a blank line is found
3162*49cdfc7eSAndroid Build Coastguard Worker		if ($in_commit_log && $commit_log_possible_stack_dump &&
3163*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\s*$/) {
3164*49cdfc7eSAndroid Build Coastguard Worker			$commit_log_possible_stack_dump = 0;
3165*49cdfc7eSAndroid Build Coastguard Worker		}
3166*49cdfc7eSAndroid Build Coastguard Worker
3167*49cdfc7eSAndroid Build Coastguard Worker# Check for lines starting with a #
3168*49cdfc7eSAndroid Build Coastguard Worker		if ($in_commit_log && $line =~ /^#/) {
3169*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("COMMIT_COMMENT_SYMBOL",
3170*49cdfc7eSAndroid Build Coastguard Worker				 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
3171*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3172*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/^/ /;
3173*49cdfc7eSAndroid Build Coastguard Worker			}
3174*49cdfc7eSAndroid Build Coastguard Worker		}
3175*49cdfc7eSAndroid Build Coastguard Worker
3176*49cdfc7eSAndroid Build Coastguard Worker# Check for git id commit length and improperly formed commit descriptions
3177*49cdfc7eSAndroid Build Coastguard Worker# A correctly formed commit description is:
3178*49cdfc7eSAndroid Build Coastguard Worker#    commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3179*49cdfc7eSAndroid Build Coastguard Worker# with the commit subject '("' prefix and '")' suffix
3180*49cdfc7eSAndroid Build Coastguard Worker# This is a fairly compilicated block as it tests for what appears to be
3181*49cdfc7eSAndroid Build Coastguard Worker# bare SHA-1 hash with  minimum length of 5.  It also avoids several types of
3182*49cdfc7eSAndroid Build Coastguard Worker# possible SHA-1 matches.
3183*49cdfc7eSAndroid Build Coastguard Worker# A commit match can span multiple lines so this block attempts to find a
3184*49cdfc7eSAndroid Build Coastguard Worker# complete typical commit on a maximum of 3 lines
3185*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
3186*49cdfc7eSAndroid Build Coastguard Worker		    $in_commit_log && !$commit_log_possible_stack_dump &&
3187*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
3188*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
3189*49cdfc7eSAndroid Build Coastguard Worker		    (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
3190*49cdfc7eSAndroid Build Coastguard Worker		      ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
3191*49cdfc7eSAndroid Build Coastguard Worker		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
3192*49cdfc7eSAndroid Build Coastguard Worker		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
3193*49cdfc7eSAndroid Build Coastguard Worker		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
3194*49cdfc7eSAndroid Build Coastguard Worker			my $init_char = "c";
3195*49cdfc7eSAndroid Build Coastguard Worker			my $orig_commit = "";
3196*49cdfc7eSAndroid Build Coastguard Worker			my $short = 1;
3197*49cdfc7eSAndroid Build Coastguard Worker			my $long = 0;
3198*49cdfc7eSAndroid Build Coastguard Worker			my $case = 1;
3199*49cdfc7eSAndroid Build Coastguard Worker			my $space = 1;
3200*49cdfc7eSAndroid Build Coastguard Worker			my $id = '0123456789ab';
3201*49cdfc7eSAndroid Build Coastguard Worker			my $orig_desc = "commit description";
3202*49cdfc7eSAndroid Build Coastguard Worker			my $description = "";
3203*49cdfc7eSAndroid Build Coastguard Worker			my $herectx = $herecurr;
3204*49cdfc7eSAndroid Build Coastguard Worker			my $has_parens = 0;
3205*49cdfc7eSAndroid Build Coastguard Worker			my $has_quotes = 0;
3206*49cdfc7eSAndroid Build Coastguard Worker
3207*49cdfc7eSAndroid Build Coastguard Worker			my $input = $line;
3208*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
3209*49cdfc7eSAndroid Build Coastguard Worker				for (my $n = 0; $n < 2; $n++) {
3210*49cdfc7eSAndroid Build Coastguard Worker					if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
3211*49cdfc7eSAndroid Build Coastguard Worker						$orig_desc = $1;
3212*49cdfc7eSAndroid Build Coastguard Worker						$has_parens = 1;
3213*49cdfc7eSAndroid Build Coastguard Worker						# Always strip leading/trailing parens then double quotes if existing
3214*49cdfc7eSAndroid Build Coastguard Worker						$orig_desc = substr($orig_desc, 1, -1);
3215*49cdfc7eSAndroid Build Coastguard Worker						if ($orig_desc =~ /^".*"$/) {
3216*49cdfc7eSAndroid Build Coastguard Worker							$orig_desc = substr($orig_desc, 1, -1);
3217*49cdfc7eSAndroid Build Coastguard Worker							$has_quotes = 1;
3218*49cdfc7eSAndroid Build Coastguard Worker						}
3219*49cdfc7eSAndroid Build Coastguard Worker						last;
3220*49cdfc7eSAndroid Build Coastguard Worker					}
3221*49cdfc7eSAndroid Build Coastguard Worker					last if ($#lines < $linenr + $n);
3222*49cdfc7eSAndroid Build Coastguard Worker					$input .= " " . trim($rawlines[$linenr + $n]);
3223*49cdfc7eSAndroid Build Coastguard Worker					$herectx .= "$rawlines[$linenr + $n]\n";
3224*49cdfc7eSAndroid Build Coastguard Worker				}
3225*49cdfc7eSAndroid Build Coastguard Worker				$herectx = $herecurr if (!$has_parens);
3226*49cdfc7eSAndroid Build Coastguard Worker			}
3227*49cdfc7eSAndroid Build Coastguard Worker
3228*49cdfc7eSAndroid Build Coastguard Worker			if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
3229*49cdfc7eSAndroid Build Coastguard Worker				$init_char = $1;
3230*49cdfc7eSAndroid Build Coastguard Worker				$orig_commit = lc($2);
3231*49cdfc7eSAndroid Build Coastguard Worker				$short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
3232*49cdfc7eSAndroid Build Coastguard Worker				$long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3233*49cdfc7eSAndroid Build Coastguard Worker				$space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
3234*49cdfc7eSAndroid Build Coastguard Worker				$case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
3235*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
3236*49cdfc7eSAndroid Build Coastguard Worker				$orig_commit = lc($1);
3237*49cdfc7eSAndroid Build Coastguard Worker			}
3238*49cdfc7eSAndroid Build Coastguard Worker
3239*49cdfc7eSAndroid Build Coastguard Worker			($id, $description) = git_commit_info($orig_commit,
3240*49cdfc7eSAndroid Build Coastguard Worker							      $id, $orig_desc);
3241*49cdfc7eSAndroid Build Coastguard Worker
3242*49cdfc7eSAndroid Build Coastguard Worker			if (defined($id) &&
3243*49cdfc7eSAndroid Build Coastguard Worker			    ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
3244*49cdfc7eSAndroid Build Coastguard Worker			    $last_git_commit_id_linenr != $linenr - 1) {
3245*49cdfc7eSAndroid Build Coastguard Worker				ERROR("GIT_COMMIT_ID",
3246*49cdfc7eSAndroid Build Coastguard Worker				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
3247*49cdfc7eSAndroid Build Coastguard Worker			}
3248*49cdfc7eSAndroid Build Coastguard Worker			#don't report the next line if this line ends in commit and the sha1 hash is the next line
3249*49cdfc7eSAndroid Build Coastguard Worker			$last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
3250*49cdfc7eSAndroid Build Coastguard Worker		}
3251*49cdfc7eSAndroid Build Coastguard Worker
3252*49cdfc7eSAndroid Build Coastguard Worker# Check for added, moved or deleted files
3253*49cdfc7eSAndroid Build Coastguard Worker		if (!$reported_maintainer_file && !$in_commit_log &&
3254*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3255*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3256*49cdfc7eSAndroid Build Coastguard Worker		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
3257*49cdfc7eSAndroid Build Coastguard Worker		      (defined($1) || defined($2))))) {
3258*49cdfc7eSAndroid Build Coastguard Worker			$is_patch = 1;
3259*49cdfc7eSAndroid Build Coastguard Worker			$reported_maintainer_file = 1;
3260*49cdfc7eSAndroid Build Coastguard Worker			WARN("FILE_PATH_CHANGES",
3261*49cdfc7eSAndroid Build Coastguard Worker			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3262*49cdfc7eSAndroid Build Coastguard Worker		}
3263*49cdfc7eSAndroid Build Coastguard Worker
3264*49cdfc7eSAndroid Build Coastguard Worker# Check for adding new DT bindings not in schema format
3265*49cdfc7eSAndroid Build Coastguard Worker		if (!$in_commit_log &&
3266*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /^new file mode\s*\d+\s*$/) &&
3267*49cdfc7eSAndroid Build Coastguard Worker		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3268*49cdfc7eSAndroid Build Coastguard Worker			WARN("DT_SCHEMA_BINDING_PATCH",
3269*49cdfc7eSAndroid Build Coastguard Worker			     "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
3270*49cdfc7eSAndroid Build Coastguard Worker		}
3271*49cdfc7eSAndroid Build Coastguard Worker
3272*49cdfc7eSAndroid Build Coastguard Worker# Check for wrappage within a valid hunk of the file
3273*49cdfc7eSAndroid Build Coastguard Worker		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3274*49cdfc7eSAndroid Build Coastguard Worker			ERROR("CORRUPTED_PATCH",
3275*49cdfc7eSAndroid Build Coastguard Worker			      "patch seems to be corrupt (line wrapped?)\n" .
3276*49cdfc7eSAndroid Build Coastguard Worker				$herecurr) if (!$emitted_corrupt++);
3277*49cdfc7eSAndroid Build Coastguard Worker		}
3278*49cdfc7eSAndroid Build Coastguard Worker
3279*49cdfc7eSAndroid Build Coastguard Worker# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
3280*49cdfc7eSAndroid Build Coastguard Worker		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
3281*49cdfc7eSAndroid Build Coastguard Worker		    $rawline !~ m/^$UTF8*$/) {
3282*49cdfc7eSAndroid Build Coastguard Worker			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3283*49cdfc7eSAndroid Build Coastguard Worker
3284*49cdfc7eSAndroid Build Coastguard Worker			my $blank = copy_spacing($rawline);
3285*49cdfc7eSAndroid Build Coastguard Worker			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
3286*49cdfc7eSAndroid Build Coastguard Worker			my $hereptr = "$hereline$ptr\n";
3287*49cdfc7eSAndroid Build Coastguard Worker
3288*49cdfc7eSAndroid Build Coastguard Worker			CHK("INVALID_UTF8",
3289*49cdfc7eSAndroid Build Coastguard Worker			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
3290*49cdfc7eSAndroid Build Coastguard Worker		}
3291*49cdfc7eSAndroid Build Coastguard Worker
3292*49cdfc7eSAndroid Build Coastguard Worker# Check if it's the start of a commit log
3293*49cdfc7eSAndroid Build Coastguard Worker# (not a header line and we haven't seen the patch filename)
3294*49cdfc7eSAndroid Build Coastguard Worker		if ($in_header_lines && $realfile =~ /^$/ &&
3295*49cdfc7eSAndroid Build Coastguard Worker		    !($rawline =~ /^\s+(?:\S|$)/ ||
3296*49cdfc7eSAndroid Build Coastguard Worker		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
3297*49cdfc7eSAndroid Build Coastguard Worker			$in_header_lines = 0;
3298*49cdfc7eSAndroid Build Coastguard Worker			$in_commit_log = 1;
3299*49cdfc7eSAndroid Build Coastguard Worker			$has_commit_log = 1;
3300*49cdfc7eSAndroid Build Coastguard Worker		}
3301*49cdfc7eSAndroid Build Coastguard Worker
3302*49cdfc7eSAndroid Build Coastguard Worker# Check if there is UTF-8 in a commit log when a mail header has explicitly
3303*49cdfc7eSAndroid Build Coastguard Worker# declined it, i.e defined some charset where it is missing.
3304*49cdfc7eSAndroid Build Coastguard Worker		if ($in_header_lines &&
3305*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3306*49cdfc7eSAndroid Build Coastguard Worker		    $1 !~ /utf-8/i) {
3307*49cdfc7eSAndroid Build Coastguard Worker			$non_utf8_charset = 1;
3308*49cdfc7eSAndroid Build Coastguard Worker		}
3309*49cdfc7eSAndroid Build Coastguard Worker
3310*49cdfc7eSAndroid Build Coastguard Worker		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
3311*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /$NON_ASCII_UTF8/) {
3312*49cdfc7eSAndroid Build Coastguard Worker			WARN("UTF8_BEFORE_PATCH",
3313*49cdfc7eSAndroid Build Coastguard Worker			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3314*49cdfc7eSAndroid Build Coastguard Worker		}
3315*49cdfc7eSAndroid Build Coastguard Worker
3316*49cdfc7eSAndroid Build Coastguard Worker# Check for absolute kernel paths in commit message
3317*49cdfc7eSAndroid Build Coastguard Worker		if ($tree && $in_commit_log) {
3318*49cdfc7eSAndroid Build Coastguard Worker			while ($line =~ m{(?:^|\s)(/\S*)}g) {
3319*49cdfc7eSAndroid Build Coastguard Worker				my $file = $1;
3320*49cdfc7eSAndroid Build Coastguard Worker
3321*49cdfc7eSAndroid Build Coastguard Worker				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
3322*49cdfc7eSAndroid Build Coastguard Worker				    check_absolute_file($1, $herecurr)) {
3323*49cdfc7eSAndroid Build Coastguard Worker					#
3324*49cdfc7eSAndroid Build Coastguard Worker				} else {
3325*49cdfc7eSAndroid Build Coastguard Worker					check_absolute_file($file, $herecurr);
3326*49cdfc7eSAndroid Build Coastguard Worker				}
3327*49cdfc7eSAndroid Build Coastguard Worker			}
3328*49cdfc7eSAndroid Build Coastguard Worker		}
3329*49cdfc7eSAndroid Build Coastguard Worker
3330*49cdfc7eSAndroid Build Coastguard Worker# Check for various typo / spelling mistakes
3331*49cdfc7eSAndroid Build Coastguard Worker		if (defined($misspellings) &&
3332*49cdfc7eSAndroid Build Coastguard Worker		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
3333*49cdfc7eSAndroid Build Coastguard Worker			while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
3334*49cdfc7eSAndroid Build Coastguard Worker				my $typo = $1;
3335*49cdfc7eSAndroid Build Coastguard Worker				my $blank = copy_spacing($rawline);
3336*49cdfc7eSAndroid Build Coastguard Worker				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
3337*49cdfc7eSAndroid Build Coastguard Worker				my $hereptr = "$hereline$ptr\n";
3338*49cdfc7eSAndroid Build Coastguard Worker				my $typo_fix = $spelling_fix{lc($typo)};
3339*49cdfc7eSAndroid Build Coastguard Worker				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3340*49cdfc7eSAndroid Build Coastguard Worker				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
3341*49cdfc7eSAndroid Build Coastguard Worker				my $msg_level = \&WARN;
3342*49cdfc7eSAndroid Build Coastguard Worker				$msg_level = \&CHK if ($file);
3343*49cdfc7eSAndroid Build Coastguard Worker				if (&{$msg_level}("TYPO_SPELLING",
3344*49cdfc7eSAndroid Build Coastguard Worker						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
3345*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
3346*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3347*49cdfc7eSAndroid Build Coastguard Worker				}
3348*49cdfc7eSAndroid Build Coastguard Worker			}
3349*49cdfc7eSAndroid Build Coastguard Worker		}
3350*49cdfc7eSAndroid Build Coastguard Worker
3351*49cdfc7eSAndroid Build Coastguard Worker# check for invalid commit id
3352*49cdfc7eSAndroid Build Coastguard Worker		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
3353*49cdfc7eSAndroid Build Coastguard Worker			my $id;
3354*49cdfc7eSAndroid Build Coastguard Worker			my $description;
3355*49cdfc7eSAndroid Build Coastguard Worker			($id, $description) = git_commit_info($2, undef, undef);
3356*49cdfc7eSAndroid Build Coastguard Worker			if (!defined($id)) {
3357*49cdfc7eSAndroid Build Coastguard Worker				WARN("UNKNOWN_COMMIT_ID",
3358*49cdfc7eSAndroid Build Coastguard Worker				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
3359*49cdfc7eSAndroid Build Coastguard Worker			}
3360*49cdfc7eSAndroid Build Coastguard Worker		}
3361*49cdfc7eSAndroid Build Coastguard Worker
3362*49cdfc7eSAndroid Build Coastguard Worker# check for repeated words separated by a single space
3363*49cdfc7eSAndroid Build Coastguard Worker# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3364*49cdfc7eSAndroid Build Coastguard Worker		if (($rawline =~ /^\+/ || $in_commit_log) &&
3365*49cdfc7eSAndroid Build Coastguard Worker		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
3366*49cdfc7eSAndroid Build Coastguard Worker			pos($rawline) = 1 if (!$in_commit_log);
3367*49cdfc7eSAndroid Build Coastguard Worker			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3368*49cdfc7eSAndroid Build Coastguard Worker
3369*49cdfc7eSAndroid Build Coastguard Worker				my $first = $1;
3370*49cdfc7eSAndroid Build Coastguard Worker				my $second = $2;
3371*49cdfc7eSAndroid Build Coastguard Worker				my $start_pos = $-[1];
3372*49cdfc7eSAndroid Build Coastguard Worker				my $end_pos = $+[2];
3373*49cdfc7eSAndroid Build Coastguard Worker				if ($first =~ /(?:struct|union|enum)/) {
3374*49cdfc7eSAndroid Build Coastguard Worker					pos($rawline) += length($first) + length($second) + 1;
3375*49cdfc7eSAndroid Build Coastguard Worker					next;
3376*49cdfc7eSAndroid Build Coastguard Worker				}
3377*49cdfc7eSAndroid Build Coastguard Worker
3378*49cdfc7eSAndroid Build Coastguard Worker				next if (lc($first) ne lc($second));
3379*49cdfc7eSAndroid Build Coastguard Worker				next if ($first eq 'long');
3380*49cdfc7eSAndroid Build Coastguard Worker
3381*49cdfc7eSAndroid Build Coastguard Worker				# check for character before and after the word matches
3382*49cdfc7eSAndroid Build Coastguard Worker				my $start_char = '';
3383*49cdfc7eSAndroid Build Coastguard Worker				my $end_char = '';
3384*49cdfc7eSAndroid Build Coastguard Worker				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3385*49cdfc7eSAndroid Build Coastguard Worker				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3386*49cdfc7eSAndroid Build Coastguard Worker
3387*49cdfc7eSAndroid Build Coastguard Worker				next if ($start_char =~ /^\S$/);
3388*49cdfc7eSAndroid Build Coastguard Worker				next if (index(" \t.,;?!", $end_char) == -1);
3389*49cdfc7eSAndroid Build Coastguard Worker
3390*49cdfc7eSAndroid Build Coastguard Worker				# avoid repeating hex occurrences like 'ff ff fe 09 ...'
3391*49cdfc7eSAndroid Build Coastguard Worker				if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3392*49cdfc7eSAndroid Build Coastguard Worker					next if (!exists($allow_repeated_words{lc($first)}));
3393*49cdfc7eSAndroid Build Coastguard Worker				}
3394*49cdfc7eSAndroid Build Coastguard Worker
3395*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("REPEATED_WORD",
3396*49cdfc7eSAndroid Build Coastguard Worker					 "Possible repeated word: '$first'\n" . $herecurr) &&
3397*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
3398*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3399*49cdfc7eSAndroid Build Coastguard Worker				}
3400*49cdfc7eSAndroid Build Coastguard Worker			}
3401*49cdfc7eSAndroid Build Coastguard Worker
3402*49cdfc7eSAndroid Build Coastguard Worker			# if it's a repeated word on consecutive lines in a comment block
3403*49cdfc7eSAndroid Build Coastguard Worker			if ($prevline =~ /$;+\s*$/ &&
3404*49cdfc7eSAndroid Build Coastguard Worker			    $prevrawline =~ /($word_pattern)\s*$/) {
3405*49cdfc7eSAndroid Build Coastguard Worker				my $last_word = $1;
3406*49cdfc7eSAndroid Build Coastguard Worker				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
3407*49cdfc7eSAndroid Build Coastguard Worker					if (WARN("REPEATED_WORD",
3408*49cdfc7eSAndroid Build Coastguard Worker						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3409*49cdfc7eSAndroid Build Coastguard Worker					    $fix) {
3410*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3411*49cdfc7eSAndroid Build Coastguard Worker					}
3412*49cdfc7eSAndroid Build Coastguard Worker				}
3413*49cdfc7eSAndroid Build Coastguard Worker			}
3414*49cdfc7eSAndroid Build Coastguard Worker		}
3415*49cdfc7eSAndroid Build Coastguard Worker
3416*49cdfc7eSAndroid Build Coastguard Worker# ignore non-hunk lines and lines being removed
3417*49cdfc7eSAndroid Build Coastguard Worker		next if (!$hunk_line || $line =~ /^-/);
3418*49cdfc7eSAndroid Build Coastguard Worker
3419*49cdfc7eSAndroid Build Coastguard Worker#trailing whitespace
3420*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+.*\015/) {
3421*49cdfc7eSAndroid Build Coastguard Worker			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3422*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("DOS_LINE_ENDINGS",
3423*49cdfc7eSAndroid Build Coastguard Worker				  "DOS line endings\n" . $herevet) &&
3424*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3425*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
3426*49cdfc7eSAndroid Build Coastguard Worker			}
3427*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
3428*49cdfc7eSAndroid Build Coastguard Worker			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3429*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("TRAILING_WHITESPACE",
3430*49cdfc7eSAndroid Build Coastguard Worker				  "trailing whitespace\n" . $herevet) &&
3431*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3432*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\s+$//;
3433*49cdfc7eSAndroid Build Coastguard Worker			}
3434*49cdfc7eSAndroid Build Coastguard Worker
3435*49cdfc7eSAndroid Build Coastguard Worker			$rpt_cleaners = 1;
3436*49cdfc7eSAndroid Build Coastguard Worker		}
3437*49cdfc7eSAndroid Build Coastguard Worker
3438*49cdfc7eSAndroid Build Coastguard Worker# Check for FSF mailing addresses.
3439*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /\bwrite to the Free/i ||
3440*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
3441*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
3442*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /\b51\s+Franklin\s+St/i) {
3443*49cdfc7eSAndroid Build Coastguard Worker			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3444*49cdfc7eSAndroid Build Coastguard Worker			my $msg_level = \&ERROR;
3445*49cdfc7eSAndroid Build Coastguard Worker			$msg_level = \&CHK if ($file);
3446*49cdfc7eSAndroid Build Coastguard Worker			&{$msg_level}("FSF_MAILING_ADDRESS",
3447*49cdfc7eSAndroid Build Coastguard Worker				      "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)
3448*49cdfc7eSAndroid Build Coastguard Worker		}
3449*49cdfc7eSAndroid Build Coastguard Worker
3450*49cdfc7eSAndroid Build Coastguard Worker# check for Kconfig help text having a real description
3451*49cdfc7eSAndroid Build Coastguard Worker# Only applies when adding the entry originally, after that we do not have
3452*49cdfc7eSAndroid Build Coastguard Worker# sufficient context to determine whether it is indeed long enough.
3453*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile =~ /Kconfig/ &&
3454*49cdfc7eSAndroid Build Coastguard Worker		    # 'choice' is usually the last thing on the line (though
3455*49cdfc7eSAndroid Build Coastguard Worker		    # Kconfig supports named choices), so use a word boundary
3456*49cdfc7eSAndroid Build Coastguard Worker		    # (\b) rather than a whitespace character (\s)
3457*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
3458*49cdfc7eSAndroid Build Coastguard Worker			my $length = 0;
3459*49cdfc7eSAndroid Build Coastguard Worker			my $cnt = $realcnt;
3460*49cdfc7eSAndroid Build Coastguard Worker			my $ln = $linenr + 1;
3461*49cdfc7eSAndroid Build Coastguard Worker			my $f;
3462*49cdfc7eSAndroid Build Coastguard Worker			my $is_start = 0;
3463*49cdfc7eSAndroid Build Coastguard Worker			my $is_end = 0;
3464*49cdfc7eSAndroid Build Coastguard Worker			for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
3465*49cdfc7eSAndroid Build Coastguard Worker				$f = $lines[$ln - 1];
3466*49cdfc7eSAndroid Build Coastguard Worker				$cnt-- if ($lines[$ln - 1] !~ /^-/);
3467*49cdfc7eSAndroid Build Coastguard Worker				$is_end = $lines[$ln - 1] =~ /^\+/;
3468*49cdfc7eSAndroid Build Coastguard Worker
3469*49cdfc7eSAndroid Build Coastguard Worker				next if ($f =~ /^-/);
3470*49cdfc7eSAndroid Build Coastguard Worker				last if (!$file && $f =~ /^\@\@/);
3471*49cdfc7eSAndroid Build Coastguard Worker
3472*49cdfc7eSAndroid Build Coastguard Worker				if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3473*49cdfc7eSAndroid Build Coastguard Worker					$is_start = 1;
3474*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
3475*49cdfc7eSAndroid Build Coastguard Worker					$length = -1;
3476*49cdfc7eSAndroid Build Coastguard Worker				}
3477*49cdfc7eSAndroid Build Coastguard Worker
3478*49cdfc7eSAndroid Build Coastguard Worker				$f =~ s/^.//;
3479*49cdfc7eSAndroid Build Coastguard Worker				$f =~ s/#.*//;
3480*49cdfc7eSAndroid Build Coastguard Worker				$f =~ s/^\s+//;
3481*49cdfc7eSAndroid Build Coastguard Worker				next if ($f =~ /^$/);
3482*49cdfc7eSAndroid Build Coastguard Worker
3483*49cdfc7eSAndroid Build Coastguard Worker				# This only checks context lines in the patch
3484*49cdfc7eSAndroid Build Coastguard Worker				# and so hopefully shouldn't trigger false
3485*49cdfc7eSAndroid Build Coastguard Worker				# positives, even though some of these are
3486*49cdfc7eSAndroid Build Coastguard Worker				# common words in help texts
3487*49cdfc7eSAndroid Build Coastguard Worker				if ($f =~ /^\s*(?:config|menuconfig|choice|endchoice|
3488*49cdfc7eSAndroid Build Coastguard Worker						  if|endif|menu|endmenu|source)\b/x) {
3489*49cdfc7eSAndroid Build Coastguard Worker					$is_end = 1;
3490*49cdfc7eSAndroid Build Coastguard Worker					last;
3491*49cdfc7eSAndroid Build Coastguard Worker				}
3492*49cdfc7eSAndroid Build Coastguard Worker				$length++;
3493*49cdfc7eSAndroid Build Coastguard Worker			}
3494*49cdfc7eSAndroid Build Coastguard Worker			if ($is_start && $is_end && $length < $min_conf_desc_length) {
3495*49cdfc7eSAndroid Build Coastguard Worker				WARN("CONFIG_DESCRIPTION",
3496*49cdfc7eSAndroid Build Coastguard Worker				     "please write a paragraph that describes the config symbol fully\n" . $herecurr);
3497*49cdfc7eSAndroid Build Coastguard Worker			}
3498*49cdfc7eSAndroid Build Coastguard Worker			#print "is_start<$is_start> is_end<$is_end> length<$length>\n";
3499*49cdfc7eSAndroid Build Coastguard Worker		}
3500*49cdfc7eSAndroid Build Coastguard Worker
3501*49cdfc7eSAndroid Build Coastguard Worker# check MAINTAINERS entries
3502*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile =~ /^MAINTAINERS$/) {
3503*49cdfc7eSAndroid Build Coastguard Worker# check MAINTAINERS entries for the right form
3504*49cdfc7eSAndroid Build Coastguard Worker			if ($rawline =~ /^\+[A-Z]:/ &&
3505*49cdfc7eSAndroid Build Coastguard Worker			    $rawline !~ /^\+[A-Z]:\t\S/) {
3506*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("MAINTAINERS_STYLE",
3507*49cdfc7eSAndroid Build Coastguard Worker					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
3508*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
3509*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
3510*49cdfc7eSAndroid Build Coastguard Worker				}
3511*49cdfc7eSAndroid Build Coastguard Worker			}
3512*49cdfc7eSAndroid Build Coastguard Worker# check MAINTAINERS entries for the right ordering too
3513*49cdfc7eSAndroid Build Coastguard Worker			my $preferred_order = 'MRLSWQBCPTFXNK';
3514*49cdfc7eSAndroid Build Coastguard Worker			if ($rawline =~ /^\+[A-Z]:/ &&
3515*49cdfc7eSAndroid Build Coastguard Worker			    $prevrawline =~ /^[\+ ][A-Z]:/) {
3516*49cdfc7eSAndroid Build Coastguard Worker				$rawline =~ /^\+([A-Z]):\s*(.*)/;
3517*49cdfc7eSAndroid Build Coastguard Worker				my $cur = $1;
3518*49cdfc7eSAndroid Build Coastguard Worker				my $curval = $2;
3519*49cdfc7eSAndroid Build Coastguard Worker				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
3520*49cdfc7eSAndroid Build Coastguard Worker				my $prev = $1;
3521*49cdfc7eSAndroid Build Coastguard Worker				my $prevval = $2;
3522*49cdfc7eSAndroid Build Coastguard Worker				my $curindex = index($preferred_order, $cur);
3523*49cdfc7eSAndroid Build Coastguard Worker				my $previndex = index($preferred_order, $prev);
3524*49cdfc7eSAndroid Build Coastguard Worker				if ($curindex < 0) {
3525*49cdfc7eSAndroid Build Coastguard Worker					WARN("MAINTAINERS_STYLE",
3526*49cdfc7eSAndroid Build Coastguard Worker					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
3527*49cdfc7eSAndroid Build Coastguard Worker				} else {
3528*49cdfc7eSAndroid Build Coastguard Worker					if ($previndex >= 0 && $curindex < $previndex) {
3529*49cdfc7eSAndroid Build Coastguard Worker						WARN("MAINTAINERS_STYLE",
3530*49cdfc7eSAndroid Build Coastguard Worker						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3531*49cdfc7eSAndroid Build Coastguard Worker					} elsif ((($prev eq 'F' && $cur eq 'F') ||
3532*49cdfc7eSAndroid Build Coastguard Worker						  ($prev eq 'X' && $cur eq 'X')) &&
3533*49cdfc7eSAndroid Build Coastguard Worker						 ($prevval cmp $curval) > 0) {
3534*49cdfc7eSAndroid Build Coastguard Worker						WARN("MAINTAINERS_STYLE",
3535*49cdfc7eSAndroid Build Coastguard Worker						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3536*49cdfc7eSAndroid Build Coastguard Worker					}
3537*49cdfc7eSAndroid Build Coastguard Worker				}
3538*49cdfc7eSAndroid Build Coastguard Worker			}
3539*49cdfc7eSAndroid Build Coastguard Worker		}
3540*49cdfc7eSAndroid Build Coastguard Worker
3541*49cdfc7eSAndroid Build Coastguard Worker		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3542*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3543*49cdfc7eSAndroid Build Coastguard Worker			my $flag = $1;
3544*49cdfc7eSAndroid Build Coastguard Worker			my $replacement = {
3545*49cdfc7eSAndroid Build Coastguard Worker				'EXTRA_AFLAGS' =>   'asflags-y',
3546*49cdfc7eSAndroid Build Coastguard Worker				'EXTRA_CFLAGS' =>   'ccflags-y',
3547*49cdfc7eSAndroid Build Coastguard Worker				'EXTRA_CPPFLAGS' => 'cppflags-y',
3548*49cdfc7eSAndroid Build Coastguard Worker				'EXTRA_LDFLAGS' =>  'ldflags-y',
3549*49cdfc7eSAndroid Build Coastguard Worker			};
3550*49cdfc7eSAndroid Build Coastguard Worker
3551*49cdfc7eSAndroid Build Coastguard Worker			WARN("DEPRECATED_VARIABLE",
3552*49cdfc7eSAndroid Build Coastguard Worker			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3553*49cdfc7eSAndroid Build Coastguard Worker		}
3554*49cdfc7eSAndroid Build Coastguard Worker
3555*49cdfc7eSAndroid Build Coastguard Worker# check for DT compatible documentation
3556*49cdfc7eSAndroid Build Coastguard Worker		if (defined $root &&
3557*49cdfc7eSAndroid Build Coastguard Worker			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3558*49cdfc7eSAndroid Build Coastguard Worker			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3559*49cdfc7eSAndroid Build Coastguard Worker
3560*49cdfc7eSAndroid Build Coastguard Worker			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3561*49cdfc7eSAndroid Build Coastguard Worker
3562*49cdfc7eSAndroid Build Coastguard Worker			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3563*49cdfc7eSAndroid Build Coastguard Worker			my $vp_file = $dt_path . "vendor-prefixes.yaml";
3564*49cdfc7eSAndroid Build Coastguard Worker
3565*49cdfc7eSAndroid Build Coastguard Worker			foreach my $compat (@compats) {
3566*49cdfc7eSAndroid Build Coastguard Worker				my $compat2 = $compat;
3567*49cdfc7eSAndroid Build Coastguard Worker				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3568*49cdfc7eSAndroid Build Coastguard Worker				my $compat3 = $compat;
3569*49cdfc7eSAndroid Build Coastguard Worker				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3570*49cdfc7eSAndroid Build Coastguard Worker				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3571*49cdfc7eSAndroid Build Coastguard Worker				if ( $? >> 8 ) {
3572*49cdfc7eSAndroid Build Coastguard Worker					WARN("UNDOCUMENTED_DT_STRING",
3573*49cdfc7eSAndroid Build Coastguard Worker					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
3574*49cdfc7eSAndroid Build Coastguard Worker				}
3575*49cdfc7eSAndroid Build Coastguard Worker
3576*49cdfc7eSAndroid Build Coastguard Worker				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
3577*49cdfc7eSAndroid Build Coastguard Worker				my $vendor = $1;
3578*49cdfc7eSAndroid Build Coastguard Worker				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
3579*49cdfc7eSAndroid Build Coastguard Worker				if ( $? >> 8 ) {
3580*49cdfc7eSAndroid Build Coastguard Worker					WARN("UNDOCUMENTED_DT_STRING",
3581*49cdfc7eSAndroid Build Coastguard Worker					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3582*49cdfc7eSAndroid Build Coastguard Worker				}
3583*49cdfc7eSAndroid Build Coastguard Worker			}
3584*49cdfc7eSAndroid Build Coastguard Worker		}
3585*49cdfc7eSAndroid Build Coastguard Worker
3586*49cdfc7eSAndroid Build Coastguard Worker# check for using SPDX license tag at beginning of files
3587*49cdfc7eSAndroid Build Coastguard Worker		if ($realline == $checklicenseline) {
3588*49cdfc7eSAndroid Build Coastguard Worker			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
3589*49cdfc7eSAndroid Build Coastguard Worker				$checklicenseline = 2;
3590*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($rawline =~ /^\+/) {
3591*49cdfc7eSAndroid Build Coastguard Worker				my $comment = "";
3592*49cdfc7eSAndroid Build Coastguard Worker				if ($realfile =~ /\.(h|s|S)$/) {
3593*49cdfc7eSAndroid Build Coastguard Worker					$comment = '/*';
3594*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($realfile =~ /\.(c|dts|dtsi)$/) {
3595*49cdfc7eSAndroid Build Coastguard Worker					$comment = '//';
3596*49cdfc7eSAndroid Build Coastguard Worker				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
3597*49cdfc7eSAndroid Build Coastguard Worker					$comment = '#';
3598*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($realfile =~ /\.rst$/) {
3599*49cdfc7eSAndroid Build Coastguard Worker					$comment = '..';
3600*49cdfc7eSAndroid Build Coastguard Worker				}
3601*49cdfc7eSAndroid Build Coastguard Worker
3602*49cdfc7eSAndroid Build Coastguard Worker# check SPDX comment style for .[chsS] files
3603*49cdfc7eSAndroid Build Coastguard Worker				if ($realfile =~ /\.[chsS]$/ &&
3604*49cdfc7eSAndroid Build Coastguard Worker				    $rawline =~ /SPDX-License-Identifier:/ &&
3605*49cdfc7eSAndroid Build Coastguard Worker				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
3606*49cdfc7eSAndroid Build Coastguard Worker					WARN("SPDX_LICENSE_TAG",
3607*49cdfc7eSAndroid Build Coastguard Worker					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
3608*49cdfc7eSAndroid Build Coastguard Worker				}
3609*49cdfc7eSAndroid Build Coastguard Worker
3610*49cdfc7eSAndroid Build Coastguard Worker				if ($comment !~ /^$/ &&
3611*49cdfc7eSAndroid Build Coastguard Worker				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
3612*49cdfc7eSAndroid Build Coastguard Worker					WARN("SPDX_LICENSE_TAG",
3613*49cdfc7eSAndroid Build Coastguard Worker					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
3614*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
3615*49cdfc7eSAndroid Build Coastguard Worker					my $spdx_license = $1;
3616*49cdfc7eSAndroid Build Coastguard Worker					if (!is_SPDX_License_valid($spdx_license)) {
3617*49cdfc7eSAndroid Build Coastguard Worker						WARN("SPDX_LICENSE_TAG",
3618*49cdfc7eSAndroid Build Coastguard Worker						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
3619*49cdfc7eSAndroid Build Coastguard Worker					}
3620*49cdfc7eSAndroid Build Coastguard Worker					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3621*49cdfc7eSAndroid Build Coastguard Worker					    not $spdx_license =~ /GPL-2\.0.*BSD-2-Clause/) {
3622*49cdfc7eSAndroid Build Coastguard Worker						my $msg_level = \&WARN;
3623*49cdfc7eSAndroid Build Coastguard Worker						$msg_level = \&CHK if ($file);
3624*49cdfc7eSAndroid Build Coastguard Worker						if (&{$msg_level}("SPDX_LICENSE_TAG",
3625*49cdfc7eSAndroid Build Coastguard Worker
3626*49cdfc7eSAndroid Build Coastguard Worker								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3627*49cdfc7eSAndroid Build Coastguard Worker						    $fix) {
3628*49cdfc7eSAndroid Build Coastguard Worker							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3629*49cdfc7eSAndroid Build Coastguard Worker						}
3630*49cdfc7eSAndroid Build Coastguard Worker					}
3631*49cdfc7eSAndroid Build Coastguard Worker				}
3632*49cdfc7eSAndroid Build Coastguard Worker			}
3633*49cdfc7eSAndroid Build Coastguard Worker		}
3634*49cdfc7eSAndroid Build Coastguard Worker
3635*49cdfc7eSAndroid Build Coastguard Worker# check for embedded filenames
3636*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^\+.*\Q$realfile\E/) {
3637*49cdfc7eSAndroid Build Coastguard Worker			WARN("EMBEDDED_FILENAME",
3638*49cdfc7eSAndroid Build Coastguard Worker			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3639*49cdfc7eSAndroid Build Coastguard Worker		}
3640*49cdfc7eSAndroid Build Coastguard Worker
3641*49cdfc7eSAndroid Build Coastguard Worker# check we are in a valid source file if not then ignore this hunk
3642*49cdfc7eSAndroid Build Coastguard Worker		next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
3643*49cdfc7eSAndroid Build Coastguard Worker
3644*49cdfc7eSAndroid Build Coastguard Worker# check for using SPDX-License-Identifier on the wrong line number
3645*49cdfc7eSAndroid Build Coastguard Worker		if ($realline != $checklicenseline &&
3646*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3647*49cdfc7eSAndroid Build Coastguard Worker		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3648*49cdfc7eSAndroid Build Coastguard Worker			WARN("SPDX_LICENSE_TAG",
3649*49cdfc7eSAndroid Build Coastguard Worker			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3650*49cdfc7eSAndroid Build Coastguard Worker		}
3651*49cdfc7eSAndroid Build Coastguard Worker
3652*49cdfc7eSAndroid Build Coastguard Worker# line length limit (with some exclusions)
3653*49cdfc7eSAndroid Build Coastguard Worker#
3654*49cdfc7eSAndroid Build Coastguard Worker# There are a few types of lines that may extend beyond $max_line_length:
3655*49cdfc7eSAndroid Build Coastguard Worker#	logging functions like pr_info that end in a string
3656*49cdfc7eSAndroid Build Coastguard Worker#	lines with a single string
3657*49cdfc7eSAndroid Build Coastguard Worker#	#defines that are a single string
3658*49cdfc7eSAndroid Build Coastguard Worker#	lines with an RFC3986 like URL
3659*49cdfc7eSAndroid Build Coastguard Worker#
3660*49cdfc7eSAndroid Build Coastguard Worker# There are 3 different line length message types:
3661*49cdfc7eSAndroid Build Coastguard Worker# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
3662*49cdfc7eSAndroid Build Coastguard Worker# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
3663*49cdfc7eSAndroid Build Coastguard Worker# LONG_LINE		all other lines longer than $max_line_length
3664*49cdfc7eSAndroid Build Coastguard Worker#
3665*49cdfc7eSAndroid Build Coastguard Worker# if LONG_LINE is ignored, the other 2 types are also ignored
3666*49cdfc7eSAndroid Build Coastguard Worker#
3667*49cdfc7eSAndroid Build Coastguard Worker
3668*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+/ && $length > $max_line_length) {
3669*49cdfc7eSAndroid Build Coastguard Worker			my $msg_type = "LONG_LINE";
3670*49cdfc7eSAndroid Build Coastguard Worker
3671*49cdfc7eSAndroid Build Coastguard Worker			# Check the allowed long line types first
3672*49cdfc7eSAndroid Build Coastguard Worker
3673*49cdfc7eSAndroid Build Coastguard Worker			# logging functions that end in a string that starts
3674*49cdfc7eSAndroid Build Coastguard Worker			# before $max_line_length
3675*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
3676*49cdfc7eSAndroid Build Coastguard Worker			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3677*49cdfc7eSAndroid Build Coastguard Worker				$msg_type = "";
3678*49cdfc7eSAndroid Build Coastguard Worker
3679*49cdfc7eSAndroid Build Coastguard Worker			# lines with only strings (w/ possible termination)
3680*49cdfc7eSAndroid Build Coastguard Worker			# #defines with only strings
3681*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
3682*49cdfc7eSAndroid Build Coastguard Worker				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
3683*49cdfc7eSAndroid Build Coastguard Worker				$msg_type = "";
3684*49cdfc7eSAndroid Build Coastguard Worker
3685*49cdfc7eSAndroid Build Coastguard Worker			# More special cases
3686*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3687*49cdfc7eSAndroid Build Coastguard Worker				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3688*49cdfc7eSAndroid Build Coastguard Worker				$msg_type = "";
3689*49cdfc7eSAndroid Build Coastguard Worker
3690*49cdfc7eSAndroid Build Coastguard Worker			# URL ($rawline is used in case the URL is in a comment)
3691*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
3692*49cdfc7eSAndroid Build Coastguard Worker				$msg_type = "";
3693*49cdfc7eSAndroid Build Coastguard Worker
3694*49cdfc7eSAndroid Build Coastguard Worker			# Otherwise set the alternate message types
3695*49cdfc7eSAndroid Build Coastguard Worker
3696*49cdfc7eSAndroid Build Coastguard Worker			# a comment starts before $max_line_length
3697*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($line =~ /($;[\s$;]*)$/ &&
3698*49cdfc7eSAndroid Build Coastguard Worker				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3699*49cdfc7eSAndroid Build Coastguard Worker				$msg_type = "LONG_LINE_COMMENT"
3700*49cdfc7eSAndroid Build Coastguard Worker
3701*49cdfc7eSAndroid Build Coastguard Worker			# a quoted string starts before $max_line_length
3702*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
3703*49cdfc7eSAndroid Build Coastguard Worker				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
3704*49cdfc7eSAndroid Build Coastguard Worker				$msg_type = "LONG_LINE_STRING"
3705*49cdfc7eSAndroid Build Coastguard Worker			}
3706*49cdfc7eSAndroid Build Coastguard Worker
3707*49cdfc7eSAndroid Build Coastguard Worker			if ($msg_type ne "" &&
3708*49cdfc7eSAndroid Build Coastguard Worker			    (show_type("LONG_LINE") || show_type($msg_type))) {
3709*49cdfc7eSAndroid Build Coastguard Worker				my $msg_level = \&WARN;
3710*49cdfc7eSAndroid Build Coastguard Worker				$msg_level = \&CHK if ($file);
3711*49cdfc7eSAndroid Build Coastguard Worker				&{$msg_level}($msg_type,
3712*49cdfc7eSAndroid Build Coastguard Worker					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
3713*49cdfc7eSAndroid Build Coastguard Worker			}
3714*49cdfc7eSAndroid Build Coastguard Worker		}
3715*49cdfc7eSAndroid Build Coastguard Worker
3716*49cdfc7eSAndroid Build Coastguard Worker# check for adding lines without a newline.
3717*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
3718*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("MISSING_EOF_NEWLINE",
3719*49cdfc7eSAndroid Build Coastguard Worker			         "adding a line without newline at end of file\n" . $herecurr) &&
3720*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3721*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr+1, "No newline at end of file");
3722*49cdfc7eSAndroid Build Coastguard Worker			}
3723*49cdfc7eSAndroid Build Coastguard Worker		}
3724*49cdfc7eSAndroid Build Coastguard Worker
3725*49cdfc7eSAndroid Build Coastguard Worker# check for .L prefix local symbols in .S files
3726*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile =~ /\.S$/ &&
3727*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3728*49cdfc7eSAndroid Build Coastguard Worker			WARN("AVOID_L_PREFIX",
3729*49cdfc7eSAndroid Build Coastguard Worker			     "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/asm-annotations.rst\n" . $herecurr);
3730*49cdfc7eSAndroid Build Coastguard Worker		}
3731*49cdfc7eSAndroid Build Coastguard Worker
3732*49cdfc7eSAndroid Build Coastguard Worker# check we are in a valid source file C or perl if not then ignore this hunk
3733*49cdfc7eSAndroid Build Coastguard Worker		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
3734*49cdfc7eSAndroid Build Coastguard Worker
3735*49cdfc7eSAndroid Build Coastguard Worker# at the beginning of a line any tabs must come first and anything
3736*49cdfc7eSAndroid Build Coastguard Worker# more than $tabsize must use tabs.
3737*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3738*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /^\+\s*        \s*/) {
3739*49cdfc7eSAndroid Build Coastguard Worker			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3740*49cdfc7eSAndroid Build Coastguard Worker			$rpt_cleaners = 1;
3741*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("CODE_INDENT",
3742*49cdfc7eSAndroid Build Coastguard Worker				  "code indent should use tabs where possible\n" . $herevet) &&
3743*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3744*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3745*49cdfc7eSAndroid Build Coastguard Worker			}
3746*49cdfc7eSAndroid Build Coastguard Worker		}
3747*49cdfc7eSAndroid Build Coastguard Worker
3748*49cdfc7eSAndroid Build Coastguard Worker# check for space before tabs.
3749*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
3750*49cdfc7eSAndroid Build Coastguard Worker			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3751*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("SPACE_BEFORE_TAB",
3752*49cdfc7eSAndroid Build Coastguard Worker				"please, no space before tabs\n" . $herevet) &&
3753*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3754*49cdfc7eSAndroid Build Coastguard Worker				while ($fixed[$fixlinenr] =~
3755*49cdfc7eSAndroid Build Coastguard Worker					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3756*49cdfc7eSAndroid Build Coastguard Worker				while ($fixed[$fixlinenr] =~
3757*49cdfc7eSAndroid Build Coastguard Worker					   s/(^\+.*) +\t/$1\t/) {}
3758*49cdfc7eSAndroid Build Coastguard Worker			}
3759*49cdfc7eSAndroid Build Coastguard Worker		}
3760*49cdfc7eSAndroid Build Coastguard Worker
3761*49cdfc7eSAndroid Build Coastguard Worker# check for assignments on the start of a line
3762*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3763*49cdfc7eSAndroid Build Coastguard Worker			my $operator = $1;
3764*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("ASSIGNMENT_CONTINUATIONS",
3765*49cdfc7eSAndroid Build Coastguard Worker				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3766*49cdfc7eSAndroid Build Coastguard Worker			    $fix && $prevrawline =~ /^\+/) {
3767*49cdfc7eSAndroid Build Coastguard Worker				# add assignment operator to the previous line, remove from current line
3768*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr - 1] .= " $operator";
3769*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3770*49cdfc7eSAndroid Build Coastguard Worker			}
3771*49cdfc7eSAndroid Build Coastguard Worker		}
3772*49cdfc7eSAndroid Build Coastguard Worker
3773*49cdfc7eSAndroid Build Coastguard Worker# check for && or || at the start of a line
3774*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
3775*49cdfc7eSAndroid Build Coastguard Worker			my $operator = $1;
3776*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("LOGICAL_CONTINUATIONS",
3777*49cdfc7eSAndroid Build Coastguard Worker				"Logical continuations should be on the previous line\n" . $hereprev) &&
3778*49cdfc7eSAndroid Build Coastguard Worker			    $fix && $prevrawline =~ /^\+/) {
3779*49cdfc7eSAndroid Build Coastguard Worker				# insert logical operator at last non-comment, non-whitepsace char on previous line
3780*49cdfc7eSAndroid Build Coastguard Worker				$prevline =~ /[\s$;]*$/;
3781*49cdfc7eSAndroid Build Coastguard Worker				my $line_end = substr($prevrawline, $-[0]);
3782*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
3783*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3784*49cdfc7eSAndroid Build Coastguard Worker			}
3785*49cdfc7eSAndroid Build Coastguard Worker		}
3786*49cdfc7eSAndroid Build Coastguard Worker
3787*49cdfc7eSAndroid Build Coastguard Worker# check indentation starts on a tab stop
3788*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
3789*49cdfc7eSAndroid Build Coastguard Worker		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3790*49cdfc7eSAndroid Build Coastguard Worker			my $indent = length($1);
3791*49cdfc7eSAndroid Build Coastguard Worker			if ($indent % $tabsize) {
3792*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("TABSTOP",
3793*49cdfc7eSAndroid Build Coastguard Worker					 "Statements should start on a tabstop\n" . $herecurr) &&
3794*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
3795*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3796*49cdfc7eSAndroid Build Coastguard Worker				}
3797*49cdfc7eSAndroid Build Coastguard Worker			}
3798*49cdfc7eSAndroid Build Coastguard Worker		}
3799*49cdfc7eSAndroid Build Coastguard Worker
3800*49cdfc7eSAndroid Build Coastguard Worker# check multi-line statement indentation matches previous line
3801*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
3802*49cdfc7eSAndroid Build Coastguard Worker		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3803*49cdfc7eSAndroid Build Coastguard Worker			$prevline =~ /^\+(\t*)(.*)$/;
3804*49cdfc7eSAndroid Build Coastguard Worker			my $oldindent = $1;
3805*49cdfc7eSAndroid Build Coastguard Worker			my $rest = $2;
3806*49cdfc7eSAndroid Build Coastguard Worker
3807*49cdfc7eSAndroid Build Coastguard Worker			my $pos = pos_last_openparen($rest);
3808*49cdfc7eSAndroid Build Coastguard Worker			if ($pos >= 0) {
3809*49cdfc7eSAndroid Build Coastguard Worker				$line =~ /^(\+| )([ \t]*)/;
3810*49cdfc7eSAndroid Build Coastguard Worker				my $newindent = $2;
3811*49cdfc7eSAndroid Build Coastguard Worker
3812*49cdfc7eSAndroid Build Coastguard Worker				my $goodtabindent = $oldindent .
3813*49cdfc7eSAndroid Build Coastguard Worker					"\t" x ($pos / $tabsize) .
3814*49cdfc7eSAndroid Build Coastguard Worker					" "  x ($pos % $tabsize);
3815*49cdfc7eSAndroid Build Coastguard Worker				my $goodspaceindent = $oldindent . " "  x $pos;
3816*49cdfc7eSAndroid Build Coastguard Worker
3817*49cdfc7eSAndroid Build Coastguard Worker				if ($newindent ne $goodtabindent &&
3818*49cdfc7eSAndroid Build Coastguard Worker				    $newindent ne $goodspaceindent) {
3819*49cdfc7eSAndroid Build Coastguard Worker
3820*49cdfc7eSAndroid Build Coastguard Worker					if (CHK("PARENTHESIS_ALIGNMENT",
3821*49cdfc7eSAndroid Build Coastguard Worker						"Alignment should match open parenthesis\n" . $hereprev) &&
3822*49cdfc7eSAndroid Build Coastguard Worker					    $fix && $line =~ /^\+/) {
3823*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~
3824*49cdfc7eSAndroid Build Coastguard Worker						    s/^\+[ \t]*/\+$goodtabindent/;
3825*49cdfc7eSAndroid Build Coastguard Worker					}
3826*49cdfc7eSAndroid Build Coastguard Worker				}
3827*49cdfc7eSAndroid Build Coastguard Worker			}
3828*49cdfc7eSAndroid Build Coastguard Worker		}
3829*49cdfc7eSAndroid Build Coastguard Worker
3830*49cdfc7eSAndroid Build Coastguard Worker# check for space after cast like "(int) foo" or "(struct foo) bar"
3831*49cdfc7eSAndroid Build Coastguard Worker# avoid checking a few false positives:
3832*49cdfc7eSAndroid Build Coastguard Worker#   "sizeof(<type>)" or "__alignof__(<type>)"
3833*49cdfc7eSAndroid Build Coastguard Worker#   function pointer declarations like "(*foo)(int) = bar;"
3834*49cdfc7eSAndroid Build Coastguard Worker#   structure definitions like "(struct foo) { 0 };"
3835*49cdfc7eSAndroid Build Coastguard Worker#   multiline macros that define functions
3836*49cdfc7eSAndroid Build Coastguard Worker#   known attributes or the __attribute__ keyword
3837*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3838*49cdfc7eSAndroid Build Coastguard Worker		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
3839*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("SPACING",
3840*49cdfc7eSAndroid Build Coastguard Worker				"No space is necessary after a cast\n" . $herecurr) &&
3841*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3842*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
3843*49cdfc7eSAndroid Build Coastguard Worker				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
3844*49cdfc7eSAndroid Build Coastguard Worker			}
3845*49cdfc7eSAndroid Build Coastguard Worker		}
3846*49cdfc7eSAndroid Build Coastguard Worker
3847*49cdfc7eSAndroid Build Coastguard Worker# Block comment styles
3848*49cdfc7eSAndroid Build Coastguard Worker# Networking with an initial /*
3849*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile =~ m@^(drivers/net/|net/)@ &&
3850*49cdfc7eSAndroid Build Coastguard Worker		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
3851*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /^\+[ \t]*\*/ &&
3852*49cdfc7eSAndroid Build Coastguard Worker		    $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
3853*49cdfc7eSAndroid Build Coastguard Worker			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3854*49cdfc7eSAndroid Build Coastguard Worker			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3855*49cdfc7eSAndroid Build Coastguard Worker		}
3856*49cdfc7eSAndroid Build Coastguard Worker
3857*49cdfc7eSAndroid Build Coastguard Worker# Block comments use * on subsequent lines
3858*49cdfc7eSAndroid Build Coastguard Worker		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3859*49cdfc7eSAndroid Build Coastguard Worker		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
3860*49cdfc7eSAndroid Build Coastguard Worker		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
3861*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /^\+/ &&			#line is new
3862*49cdfc7eSAndroid Build Coastguard Worker		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
3863*49cdfc7eSAndroid Build Coastguard Worker			WARN("BLOCK_COMMENT_STYLE",
3864*49cdfc7eSAndroid Build Coastguard Worker			     "Block comments use * on subsequent lines\n" . $hereprev);
3865*49cdfc7eSAndroid Build Coastguard Worker		}
3866*49cdfc7eSAndroid Build Coastguard Worker
3867*49cdfc7eSAndroid Build Coastguard Worker# Block comments use */ on trailing lines
3868*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
3869*49cdfc7eSAndroid Build Coastguard Worker		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
3870*49cdfc7eSAndroid Build Coastguard Worker		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
3871*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
3872*49cdfc7eSAndroid Build Coastguard Worker			WARN("BLOCK_COMMENT_STYLE",
3873*49cdfc7eSAndroid Build Coastguard Worker			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
3874*49cdfc7eSAndroid Build Coastguard Worker		}
3875*49cdfc7eSAndroid Build Coastguard Worker
3876*49cdfc7eSAndroid Build Coastguard Worker# Block comment * alignment
3877*49cdfc7eSAndroid Build Coastguard Worker		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
3878*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
3879*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
3880*49cdfc7eSAndroid Build Coastguard Worker		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
3881*49cdfc7eSAndroid Build Coastguard Worker		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
3882*49cdfc7eSAndroid Build Coastguard Worker		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
3883*49cdfc7eSAndroid Build Coastguard Worker			my $oldindent;
3884*49cdfc7eSAndroid Build Coastguard Worker			$prevrawline =~ m@^\+([ \t]*/?)\*@;
3885*49cdfc7eSAndroid Build Coastguard Worker			if (defined($1)) {
3886*49cdfc7eSAndroid Build Coastguard Worker				$oldindent = expand_tabs($1);
3887*49cdfc7eSAndroid Build Coastguard Worker			} else {
3888*49cdfc7eSAndroid Build Coastguard Worker				$prevrawline =~ m@^\+(.*/?)\*@;
3889*49cdfc7eSAndroid Build Coastguard Worker				$oldindent = expand_tabs($1);
3890*49cdfc7eSAndroid Build Coastguard Worker			}
3891*49cdfc7eSAndroid Build Coastguard Worker			$rawline =~ m@^\+([ \t]*)\*@;
3892*49cdfc7eSAndroid Build Coastguard Worker			my $newindent = $1;
3893*49cdfc7eSAndroid Build Coastguard Worker			$newindent = expand_tabs($newindent);
3894*49cdfc7eSAndroid Build Coastguard Worker			if (length($oldindent) ne length($newindent)) {
3895*49cdfc7eSAndroid Build Coastguard Worker				WARN("BLOCK_COMMENT_STYLE",
3896*49cdfc7eSAndroid Build Coastguard Worker				     "Block comments should align the * on each line\n" . $hereprev);
3897*49cdfc7eSAndroid Build Coastguard Worker			}
3898*49cdfc7eSAndroid Build Coastguard Worker		}
3899*49cdfc7eSAndroid Build Coastguard Worker
3900*49cdfc7eSAndroid Build Coastguard Worker# check for missing blank lines after struct/union declarations
3901*49cdfc7eSAndroid Build Coastguard Worker# with exceptions for various attributes and macros
3902*49cdfc7eSAndroid Build Coastguard Worker		if ($prevline =~ /^[\+ ]};?\s*$/ &&
3903*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\+/ &&
3904*49cdfc7eSAndroid Build Coastguard Worker		    !($line =~ /^\+\s*$/ ||
3905*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3906*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\+\s*MODULE_/i ||
3907*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3908*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\+[a-z_]*init/ ||
3909*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3910*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\+\s*DECLARE/ ||
3911*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
3912*49cdfc7eSAndroid Build Coastguard Worker		      $line =~ /^\+\s*__setup/)) {
3913*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("LINE_SPACING",
3914*49cdfc7eSAndroid Build Coastguard Worker				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3915*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3916*49cdfc7eSAndroid Build Coastguard Worker				fix_insert_line($fixlinenr, "\+");
3917*49cdfc7eSAndroid Build Coastguard Worker			}
3918*49cdfc7eSAndroid Build Coastguard Worker		}
3919*49cdfc7eSAndroid Build Coastguard Worker
3920*49cdfc7eSAndroid Build Coastguard Worker# check for multiple consecutive blank lines
3921*49cdfc7eSAndroid Build Coastguard Worker		if ($prevline =~ /^[\+ ]\s*$/ &&
3922*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\+\s*$/ &&
3923*49cdfc7eSAndroid Build Coastguard Worker		    $last_blank_line != ($linenr - 1)) {
3924*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("LINE_SPACING",
3925*49cdfc7eSAndroid Build Coastguard Worker				"Please don't use multiple blank lines\n" . $hereprev) &&
3926*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3927*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr, $rawline);
3928*49cdfc7eSAndroid Build Coastguard Worker			}
3929*49cdfc7eSAndroid Build Coastguard Worker
3930*49cdfc7eSAndroid Build Coastguard Worker			$last_blank_line = $linenr;
3931*49cdfc7eSAndroid Build Coastguard Worker		}
3932*49cdfc7eSAndroid Build Coastguard Worker
3933*49cdfc7eSAndroid Build Coastguard Worker# check for missing blank lines after declarations
3934*49cdfc7eSAndroid Build Coastguard Worker# (declarations must have the same indentation and not be at the start of line)
3935*49cdfc7eSAndroid Build Coastguard Worker		if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
3936*49cdfc7eSAndroid Build Coastguard Worker			# use temporaries
3937*49cdfc7eSAndroid Build Coastguard Worker			my $sl = $sline;
3938*49cdfc7eSAndroid Build Coastguard Worker			my $pl = $prevline;
3939*49cdfc7eSAndroid Build Coastguard Worker			# remove $Attribute/$Sparse uses to simplify comparisons
3940*49cdfc7eSAndroid Build Coastguard Worker			$sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3941*49cdfc7eSAndroid Build Coastguard Worker			$pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
3942*49cdfc7eSAndroid Build Coastguard Worker			if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3943*49cdfc7eSAndroid Build Coastguard Worker			# function pointer declarations
3944*49cdfc7eSAndroid Build Coastguard Worker			     $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3945*49cdfc7eSAndroid Build Coastguard Worker			# foo bar; where foo is some local typedef or #define
3946*49cdfc7eSAndroid Build Coastguard Worker			     $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3947*49cdfc7eSAndroid Build Coastguard Worker			# known declaration macros
3948*49cdfc7eSAndroid Build Coastguard Worker			     $pl =~ /^\+\s+$declaration_macros/) &&
3949*49cdfc7eSAndroid Build Coastguard Worker			# for "else if" which can look like "$Ident $Ident"
3950*49cdfc7eSAndroid Build Coastguard Worker			    !($pl =~ /^\+\s+$c90_Keywords\b/ ||
3951*49cdfc7eSAndroid Build Coastguard Worker			# other possible extensions of declaration lines
3952*49cdfc7eSAndroid Build Coastguard Worker			      $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3953*49cdfc7eSAndroid Build Coastguard Worker			# not starting a section or a macro "\" extended line
3954*49cdfc7eSAndroid Build Coastguard Worker			      $pl =~ /(?:\{\s*|\\)$/) &&
3955*49cdfc7eSAndroid Build Coastguard Worker			# looks like a declaration
3956*49cdfc7eSAndroid Build Coastguard Worker			    !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
3957*49cdfc7eSAndroid Build Coastguard Worker			# function pointer declarations
3958*49cdfc7eSAndroid Build Coastguard Worker			      $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
3959*49cdfc7eSAndroid Build Coastguard Worker			# foo bar; where foo is some local typedef or #define
3960*49cdfc7eSAndroid Build Coastguard Worker			      $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3961*49cdfc7eSAndroid Build Coastguard Worker			# known declaration macros
3962*49cdfc7eSAndroid Build Coastguard Worker			      $sl =~ /^\+\s+$declaration_macros/ ||
3963*49cdfc7eSAndroid Build Coastguard Worker			# start of struct or union or enum
3964*49cdfc7eSAndroid Build Coastguard Worker			      $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
3965*49cdfc7eSAndroid Build Coastguard Worker			# start or end of block or continuation of declaration
3966*49cdfc7eSAndroid Build Coastguard Worker			      $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3967*49cdfc7eSAndroid Build Coastguard Worker			# bitfield continuation
3968*49cdfc7eSAndroid Build Coastguard Worker			      $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3969*49cdfc7eSAndroid Build Coastguard Worker			# other possible extensions of declaration lines
3970*49cdfc7eSAndroid Build Coastguard Worker			      $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
3971*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("LINE_SPACING",
3972*49cdfc7eSAndroid Build Coastguard Worker					 "Missing a blank line after declarations\n" . $hereprev) &&
3973*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
3974*49cdfc7eSAndroid Build Coastguard Worker					fix_insert_line($fixlinenr, "\+");
3975*49cdfc7eSAndroid Build Coastguard Worker				}
3976*49cdfc7eSAndroid Build Coastguard Worker			}
3977*49cdfc7eSAndroid Build Coastguard Worker		}
3978*49cdfc7eSAndroid Build Coastguard Worker
3979*49cdfc7eSAndroid Build Coastguard Worker# check for spaces at the beginning of a line.
3980*49cdfc7eSAndroid Build Coastguard Worker# Exceptions:
3981*49cdfc7eSAndroid Build Coastguard Worker#  1) within comments
3982*49cdfc7eSAndroid Build Coastguard Worker#  2) indented preprocessor commands
3983*49cdfc7eSAndroid Build Coastguard Worker#  3) hanging labels
3984*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
3985*49cdfc7eSAndroid Build Coastguard Worker			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3986*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("LEADING_SPACE",
3987*49cdfc7eSAndroid Build Coastguard Worker				 "please, no spaces at the start of a line\n" . $herevet) &&
3988*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
3989*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
3990*49cdfc7eSAndroid Build Coastguard Worker			}
3991*49cdfc7eSAndroid Build Coastguard Worker		}
3992*49cdfc7eSAndroid Build Coastguard Worker
3993*49cdfc7eSAndroid Build Coastguard Worker# check we are in a valid C source file if not then ignore this hunk
3994*49cdfc7eSAndroid Build Coastguard Worker		next if ($realfile !~ /\.(h|c)$/);
3995*49cdfc7eSAndroid Build Coastguard Worker
3996*49cdfc7eSAndroid Build Coastguard Worker# check for unusual line ending [ or (
3997*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+.*([\[\(])\s*$/) {
3998*49cdfc7eSAndroid Build Coastguard Worker			CHK("OPEN_ENDED_LINE",
3999*49cdfc7eSAndroid Build Coastguard Worker			    "Lines should not end with a '$1'\n" . $herecurr);
4000*49cdfc7eSAndroid Build Coastguard Worker		}
4001*49cdfc7eSAndroid Build Coastguard Worker
4002*49cdfc7eSAndroid Build Coastguard Worker# check if this appears to be the start function declaration, save the name
4003*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^\+\{\s*$/ &&
4004*49cdfc7eSAndroid Build Coastguard Worker		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
4005*49cdfc7eSAndroid Build Coastguard Worker			$context_function = $1;
4006*49cdfc7eSAndroid Build Coastguard Worker		}
4007*49cdfc7eSAndroid Build Coastguard Worker
4008*49cdfc7eSAndroid Build Coastguard Worker# check if this appears to be the end of function declaration
4009*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^\+\}\s*$/) {
4010*49cdfc7eSAndroid Build Coastguard Worker			undef $context_function;
4011*49cdfc7eSAndroid Build Coastguard Worker		}
4012*49cdfc7eSAndroid Build Coastguard Worker
4013*49cdfc7eSAndroid Build Coastguard Worker# check indentation of any line with a bare else
4014*49cdfc7eSAndroid Build Coastguard Worker# (but not if it is a multiple line "if (foo) return bar; else return baz;")
4015*49cdfc7eSAndroid Build Coastguard Worker# if the previous line is a break or return and is indented 1 tab more...
4016*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4017*49cdfc7eSAndroid Build Coastguard Worker			my $tabs = length($1) + 1;
4018*49cdfc7eSAndroid Build Coastguard Worker			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4019*49cdfc7eSAndroid Build Coastguard Worker			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4020*49cdfc7eSAndroid Build Coastguard Worker			     defined $lines[$linenr] &&
4021*49cdfc7eSAndroid Build Coastguard Worker			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
4022*49cdfc7eSAndroid Build Coastguard Worker				WARN("UNNECESSARY_ELSE",
4023*49cdfc7eSAndroid Build Coastguard Worker				     "else is not generally useful after a break or return\n" . $hereprev);
4024*49cdfc7eSAndroid Build Coastguard Worker			}
4025*49cdfc7eSAndroid Build Coastguard Worker		}
4026*49cdfc7eSAndroid Build Coastguard Worker
4027*49cdfc7eSAndroid Build Coastguard Worker# check indentation of a line with a break;
4028*49cdfc7eSAndroid Build Coastguard Worker# if the previous line is a goto, return or break
4029*49cdfc7eSAndroid Build Coastguard Worker# and is indented the same # of tabs
4030*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4031*49cdfc7eSAndroid Build Coastguard Worker			my $tabs = $1;
4032*49cdfc7eSAndroid Build Coastguard Worker			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4033*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("UNNECESSARY_BREAK",
4034*49cdfc7eSAndroid Build Coastguard Worker					 "break is not useful after a $1\n" . $hereprev) &&
4035*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
4036*49cdfc7eSAndroid Build Coastguard Worker					fix_delete_line($fixlinenr, $rawline);
4037*49cdfc7eSAndroid Build Coastguard Worker				}
4038*49cdfc7eSAndroid Build Coastguard Worker			}
4039*49cdfc7eSAndroid Build Coastguard Worker		}
4040*49cdfc7eSAndroid Build Coastguard Worker
4041*49cdfc7eSAndroid Build Coastguard Worker# check for RCS/CVS revision markers
4042*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
4043*49cdfc7eSAndroid Build Coastguard Worker			WARN("CVS_KEYWORD",
4044*49cdfc7eSAndroid Build Coastguard Worker			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
4045*49cdfc7eSAndroid Build Coastguard Worker		}
4046*49cdfc7eSAndroid Build Coastguard Worker
4047*49cdfc7eSAndroid Build Coastguard Worker# check for old HOTPLUG __dev<foo> section markings
4048*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
4049*49cdfc7eSAndroid Build Coastguard Worker			WARN("HOTPLUG_SECTION",
4050*49cdfc7eSAndroid Build Coastguard Worker			     "Using $1 is unnecessary\n" . $herecurr);
4051*49cdfc7eSAndroid Build Coastguard Worker		}
4052*49cdfc7eSAndroid Build Coastguard Worker
4053*49cdfc7eSAndroid Build Coastguard Worker# Check for potential 'bare' types
4054*49cdfc7eSAndroid Build Coastguard Worker		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
4055*49cdfc7eSAndroid Build Coastguard Worker		    $realline_next);
4056*49cdfc7eSAndroid Build Coastguard Worker#print "LINE<$line>\n";
4057*49cdfc7eSAndroid Build Coastguard Worker		if ($linenr > $suppress_statement &&
4058*49cdfc7eSAndroid Build Coastguard Worker		    $realcnt && $sline =~ /.\s*\S/) {
4059*49cdfc7eSAndroid Build Coastguard Worker			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4060*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_block($linenr, $realcnt, 0);
4061*49cdfc7eSAndroid Build Coastguard Worker			$stat =~ s/\n./\n /g;
4062*49cdfc7eSAndroid Build Coastguard Worker			$cond =~ s/\n./\n /g;
4063*49cdfc7eSAndroid Build Coastguard Worker
4064*49cdfc7eSAndroid Build Coastguard Worker#print "linenr<$linenr> <$stat>\n";
4065*49cdfc7eSAndroid Build Coastguard Worker			# If this statement has no statement boundaries within
4066*49cdfc7eSAndroid Build Coastguard Worker			# it there is no point in retrying a statement scan
4067*49cdfc7eSAndroid Build Coastguard Worker			# until we hit end of it.
4068*49cdfc7eSAndroid Build Coastguard Worker			my $frag = $stat; $frag =~ s/;+\s*$//;
4069*49cdfc7eSAndroid Build Coastguard Worker			if ($frag !~ /(?:{|;)/) {
4070*49cdfc7eSAndroid Build Coastguard Worker#print "skip<$line_nr_next>\n";
4071*49cdfc7eSAndroid Build Coastguard Worker				$suppress_statement = $line_nr_next;
4072*49cdfc7eSAndroid Build Coastguard Worker			}
4073*49cdfc7eSAndroid Build Coastguard Worker
4074*49cdfc7eSAndroid Build Coastguard Worker			# Find the real next line.
4075*49cdfc7eSAndroid Build Coastguard Worker			$realline_next = $line_nr_next;
4076*49cdfc7eSAndroid Build Coastguard Worker			if (defined $realline_next &&
4077*49cdfc7eSAndroid Build Coastguard Worker			    (!defined $lines[$realline_next - 1] ||
4078*49cdfc7eSAndroid Build Coastguard Worker			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
4079*49cdfc7eSAndroid Build Coastguard Worker				$realline_next++;
4080*49cdfc7eSAndroid Build Coastguard Worker			}
4081*49cdfc7eSAndroid Build Coastguard Worker
4082*49cdfc7eSAndroid Build Coastguard Worker			my $s = $stat;
4083*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/{.*$//s;
4084*49cdfc7eSAndroid Build Coastguard Worker
4085*49cdfc7eSAndroid Build Coastguard Worker			# Ignore goto labels.
4086*49cdfc7eSAndroid Build Coastguard Worker			if ($s =~ /$Ident:\*$/s) {
4087*49cdfc7eSAndroid Build Coastguard Worker
4088*49cdfc7eSAndroid Build Coastguard Worker			# Ignore functions being called
4089*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
4090*49cdfc7eSAndroid Build Coastguard Worker
4091*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($s =~ /^.\s*else\b/s) {
4092*49cdfc7eSAndroid Build Coastguard Worker
4093*49cdfc7eSAndroid Build Coastguard Worker			# declarations always start with types
4094*49cdfc7eSAndroid Build Coastguard Worker			} 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) {
4095*49cdfc7eSAndroid Build Coastguard Worker				my $type = $1;
4096*49cdfc7eSAndroid Build Coastguard Worker				$type =~ s/\s+/ /g;
4097*49cdfc7eSAndroid Build Coastguard Worker				possible($type, "A:" . $s);
4098*49cdfc7eSAndroid Build Coastguard Worker
4099*49cdfc7eSAndroid Build Coastguard Worker			# definitions in global scope can only start with types
4100*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
4101*49cdfc7eSAndroid Build Coastguard Worker				possible($1, "B:" . $s);
4102*49cdfc7eSAndroid Build Coastguard Worker			}
4103*49cdfc7eSAndroid Build Coastguard Worker
4104*49cdfc7eSAndroid Build Coastguard Worker			# any (foo ... *) is a pointer cast, and foo is a type
4105*49cdfc7eSAndroid Build Coastguard Worker			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
4106*49cdfc7eSAndroid Build Coastguard Worker				possible($1, "C:" . $s);
4107*49cdfc7eSAndroid Build Coastguard Worker			}
4108*49cdfc7eSAndroid Build Coastguard Worker
4109*49cdfc7eSAndroid Build Coastguard Worker			# Check for any sort of function declaration.
4110*49cdfc7eSAndroid Build Coastguard Worker			# int foo(something bar, other baz);
4111*49cdfc7eSAndroid Build Coastguard Worker			# void (*store_gdt)(x86_descr_ptr *);
4112*49cdfc7eSAndroid Build Coastguard Worker			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
4113*49cdfc7eSAndroid Build Coastguard Worker				my ($name_len) = length($1);
4114*49cdfc7eSAndroid Build Coastguard Worker
4115*49cdfc7eSAndroid Build Coastguard Worker				my $ctx = $s;
4116*49cdfc7eSAndroid Build Coastguard Worker				substr($ctx, 0, $name_len + 1, '');
4117*49cdfc7eSAndroid Build Coastguard Worker				$ctx =~ s/\)[^\)]*$//;
4118*49cdfc7eSAndroid Build Coastguard Worker
4119*49cdfc7eSAndroid Build Coastguard Worker				for my $arg (split(/\s*,\s*/, $ctx)) {
4120*49cdfc7eSAndroid Build Coastguard Worker					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
4121*49cdfc7eSAndroid Build Coastguard Worker
4122*49cdfc7eSAndroid Build Coastguard Worker						possible($1, "D:" . $s);
4123*49cdfc7eSAndroid Build Coastguard Worker					}
4124*49cdfc7eSAndroid Build Coastguard Worker				}
4125*49cdfc7eSAndroid Build Coastguard Worker			}
4126*49cdfc7eSAndroid Build Coastguard Worker
4127*49cdfc7eSAndroid Build Coastguard Worker		}
4128*49cdfc7eSAndroid Build Coastguard Worker
4129*49cdfc7eSAndroid Build Coastguard Worker#
4130*49cdfc7eSAndroid Build Coastguard Worker# Checks which may be anchored in the context.
4131*49cdfc7eSAndroid Build Coastguard Worker#
4132*49cdfc7eSAndroid Build Coastguard Worker
4133*49cdfc7eSAndroid Build Coastguard Worker# Check for switch () and associated case and default
4134*49cdfc7eSAndroid Build Coastguard Worker# statements should be at the same indent.
4135*49cdfc7eSAndroid Build Coastguard Worker		if ($line=~/\bswitch\s*\(.*\)/) {
4136*49cdfc7eSAndroid Build Coastguard Worker			my $err = '';
4137*49cdfc7eSAndroid Build Coastguard Worker			my $sep = '';
4138*49cdfc7eSAndroid Build Coastguard Worker			my @ctx = ctx_block_outer($linenr, $realcnt);
4139*49cdfc7eSAndroid Build Coastguard Worker			shift(@ctx);
4140*49cdfc7eSAndroid Build Coastguard Worker			for my $ctx (@ctx) {
4141*49cdfc7eSAndroid Build Coastguard Worker				my ($clen, $cindent) = line_stats($ctx);
4142*49cdfc7eSAndroid Build Coastguard Worker				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
4143*49cdfc7eSAndroid Build Coastguard Worker							$indent != $cindent) {
4144*49cdfc7eSAndroid Build Coastguard Worker					$err .= "$sep$ctx\n";
4145*49cdfc7eSAndroid Build Coastguard Worker					$sep = '';
4146*49cdfc7eSAndroid Build Coastguard Worker				} else {
4147*49cdfc7eSAndroid Build Coastguard Worker					$sep = "[...]\n";
4148*49cdfc7eSAndroid Build Coastguard Worker				}
4149*49cdfc7eSAndroid Build Coastguard Worker			}
4150*49cdfc7eSAndroid Build Coastguard Worker			if ($err ne '') {
4151*49cdfc7eSAndroid Build Coastguard Worker				ERROR("SWITCH_CASE_INDENT_LEVEL",
4152*49cdfc7eSAndroid Build Coastguard Worker				      "switch and case should be at the same indent\n$hereline$err");
4153*49cdfc7eSAndroid Build Coastguard Worker			}
4154*49cdfc7eSAndroid Build Coastguard Worker		}
4155*49cdfc7eSAndroid Build Coastguard Worker
4156*49cdfc7eSAndroid Build Coastguard Worker# if/while/etc brace do not go on next line, unless defining a do while loop,
4157*49cdfc7eSAndroid Build Coastguard Worker# or if that brace on the next line is for something else
4158*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
4159*49cdfc7eSAndroid Build Coastguard Worker			my $pre_ctx = "$1$2";
4160*49cdfc7eSAndroid Build Coastguard Worker
4161*49cdfc7eSAndroid Build Coastguard Worker			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
4162*49cdfc7eSAndroid Build Coastguard Worker
4163*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /^\+\t{6,}/) {
4164*49cdfc7eSAndroid Build Coastguard Worker				WARN("DEEP_INDENTATION",
4165*49cdfc7eSAndroid Build Coastguard Worker				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
4166*49cdfc7eSAndroid Build Coastguard Worker			}
4167*49cdfc7eSAndroid Build Coastguard Worker
4168*49cdfc7eSAndroid Build Coastguard Worker			my $ctx_cnt = $realcnt - $#ctx - 1;
4169*49cdfc7eSAndroid Build Coastguard Worker			my $ctx = join("\n", @ctx);
4170*49cdfc7eSAndroid Build Coastguard Worker
4171*49cdfc7eSAndroid Build Coastguard Worker			my $ctx_ln = $linenr;
4172*49cdfc7eSAndroid Build Coastguard Worker			my $ctx_skip = $realcnt;
4173*49cdfc7eSAndroid Build Coastguard Worker
4174*49cdfc7eSAndroid Build Coastguard Worker			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4175*49cdfc7eSAndroid Build Coastguard Worker					defined $lines[$ctx_ln - 1] &&
4176*49cdfc7eSAndroid Build Coastguard Worker					$lines[$ctx_ln - 1] =~ /^-/)) {
4177*49cdfc7eSAndroid Build Coastguard Worker				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4178*49cdfc7eSAndroid Build Coastguard Worker				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4179*49cdfc7eSAndroid Build Coastguard Worker				$ctx_ln++;
4180*49cdfc7eSAndroid Build Coastguard Worker			}
4181*49cdfc7eSAndroid Build Coastguard Worker
4182*49cdfc7eSAndroid Build Coastguard Worker			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
4183*49cdfc7eSAndroid Build Coastguard Worker			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4184*49cdfc7eSAndroid Build Coastguard Worker
4185*49cdfc7eSAndroid Build Coastguard Worker			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4186*49cdfc7eSAndroid Build Coastguard Worker				ERROR("OPEN_BRACE",
4187*49cdfc7eSAndroid Build Coastguard Worker				      "that open brace { should be on the previous line\n" .
4188*49cdfc7eSAndroid Build Coastguard Worker					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
4189*49cdfc7eSAndroid Build Coastguard Worker			}
4190*49cdfc7eSAndroid Build Coastguard Worker			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4191*49cdfc7eSAndroid Build Coastguard Worker			    $ctx =~ /\)\s*\;\s*$/ &&
4192*49cdfc7eSAndroid Build Coastguard Worker			    defined $lines[$ctx_ln - 1])
4193*49cdfc7eSAndroid Build Coastguard Worker			{
4194*49cdfc7eSAndroid Build Coastguard Worker				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
4195*49cdfc7eSAndroid Build Coastguard Worker				if ($nindent > $indent) {
4196*49cdfc7eSAndroid Build Coastguard Worker					WARN("TRAILING_SEMICOLON",
4197*49cdfc7eSAndroid Build Coastguard Worker					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
4198*49cdfc7eSAndroid Build Coastguard Worker						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
4199*49cdfc7eSAndroid Build Coastguard Worker				}
4200*49cdfc7eSAndroid Build Coastguard Worker			}
4201*49cdfc7eSAndroid Build Coastguard Worker		}
4202*49cdfc7eSAndroid Build Coastguard Worker
4203*49cdfc7eSAndroid Build Coastguard Worker# Check relative indent for conditionals and blocks.
4204*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
4205*49cdfc7eSAndroid Build Coastguard Worker			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4206*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_block($linenr, $realcnt, 0)
4207*49cdfc7eSAndroid Build Coastguard Worker					if (!defined $stat);
4208*49cdfc7eSAndroid Build Coastguard Worker			my ($s, $c) = ($stat, $cond);
4209*49cdfc7eSAndroid Build Coastguard Worker
4210*49cdfc7eSAndroid Build Coastguard Worker			substr($s, 0, length($c), '');
4211*49cdfc7eSAndroid Build Coastguard Worker
4212*49cdfc7eSAndroid Build Coastguard Worker			# remove inline comments
4213*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/$;/ /g;
4214*49cdfc7eSAndroid Build Coastguard Worker			$c =~ s/$;/ /g;
4215*49cdfc7eSAndroid Build Coastguard Worker
4216*49cdfc7eSAndroid Build Coastguard Worker			# Find out how long the conditional actually is.
4217*49cdfc7eSAndroid Build Coastguard Worker			my @newlines = ($c =~ /\n/gs);
4218*49cdfc7eSAndroid Build Coastguard Worker			my $cond_lines = 1 + $#newlines;
4219*49cdfc7eSAndroid Build Coastguard Worker
4220*49cdfc7eSAndroid Build Coastguard Worker			# Make sure we remove the line prefixes as we have
4221*49cdfc7eSAndroid Build Coastguard Worker			# none on the first line, and are going to readd them
4222*49cdfc7eSAndroid Build Coastguard Worker			# where necessary.
4223*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/\n./\n/gs;
4224*49cdfc7eSAndroid Build Coastguard Worker			while ($s =~ /\n\s+\\\n/) {
4225*49cdfc7eSAndroid Build Coastguard Worker				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
4226*49cdfc7eSAndroid Build Coastguard Worker			}
4227*49cdfc7eSAndroid Build Coastguard Worker
4228*49cdfc7eSAndroid Build Coastguard Worker			# We want to check the first line inside the block
4229*49cdfc7eSAndroid Build Coastguard Worker			# starting at the end of the conditional, so remove:
4230*49cdfc7eSAndroid Build Coastguard Worker			#  1) any blank line termination
4231*49cdfc7eSAndroid Build Coastguard Worker			#  2) any opening brace { on end of the line
4232*49cdfc7eSAndroid Build Coastguard Worker			#  3) any do (...) {
4233*49cdfc7eSAndroid Build Coastguard Worker			my $continuation = 0;
4234*49cdfc7eSAndroid Build Coastguard Worker			my $check = 0;
4235*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/^.*\bdo\b//;
4236*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/^\s*{//;
4237*49cdfc7eSAndroid Build Coastguard Worker			if ($s =~ s/^\s*\\//) {
4238*49cdfc7eSAndroid Build Coastguard Worker				$continuation = 1;
4239*49cdfc7eSAndroid Build Coastguard Worker			}
4240*49cdfc7eSAndroid Build Coastguard Worker			if ($s =~ s/^\s*?\n//) {
4241*49cdfc7eSAndroid Build Coastguard Worker				$check = 1;
4242*49cdfc7eSAndroid Build Coastguard Worker				$cond_lines++;
4243*49cdfc7eSAndroid Build Coastguard Worker			}
4244*49cdfc7eSAndroid Build Coastguard Worker
4245*49cdfc7eSAndroid Build Coastguard Worker			# Also ignore a loop construct at the end of a
4246*49cdfc7eSAndroid Build Coastguard Worker			# preprocessor statement.
4247*49cdfc7eSAndroid Build Coastguard Worker			if (($prevline =~ /^.\s*#\s*define\s/ ||
4248*49cdfc7eSAndroid Build Coastguard Worker			    $prevline =~ /\\\s*$/) && $continuation == 0) {
4249*49cdfc7eSAndroid Build Coastguard Worker				$check = 0;
4250*49cdfc7eSAndroid Build Coastguard Worker			}
4251*49cdfc7eSAndroid Build Coastguard Worker
4252*49cdfc7eSAndroid Build Coastguard Worker			my $cond_ptr = -1;
4253*49cdfc7eSAndroid Build Coastguard Worker			$continuation = 0;
4254*49cdfc7eSAndroid Build Coastguard Worker			while ($cond_ptr != $cond_lines) {
4255*49cdfc7eSAndroid Build Coastguard Worker				$cond_ptr = $cond_lines;
4256*49cdfc7eSAndroid Build Coastguard Worker
4257*49cdfc7eSAndroid Build Coastguard Worker				# If we see an #else/#elif then the code
4258*49cdfc7eSAndroid Build Coastguard Worker				# is not linear.
4259*49cdfc7eSAndroid Build Coastguard Worker				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4260*49cdfc7eSAndroid Build Coastguard Worker					$check = 0;
4261*49cdfc7eSAndroid Build Coastguard Worker				}
4262*49cdfc7eSAndroid Build Coastguard Worker
4263*49cdfc7eSAndroid Build Coastguard Worker				# Ignore:
4264*49cdfc7eSAndroid Build Coastguard Worker				#  1) blank lines, they should be at 0,
4265*49cdfc7eSAndroid Build Coastguard Worker				#  2) preprocessor lines, and
4266*49cdfc7eSAndroid Build Coastguard Worker				#  3) labels.
4267*49cdfc7eSAndroid Build Coastguard Worker				if ($continuation ||
4268*49cdfc7eSAndroid Build Coastguard Worker				    $s =~ /^\s*?\n/ ||
4269*49cdfc7eSAndroid Build Coastguard Worker				    $s =~ /^\s*#\s*?/ ||
4270*49cdfc7eSAndroid Build Coastguard Worker				    $s =~ /^\s*$Ident\s*:/) {
4271*49cdfc7eSAndroid Build Coastguard Worker					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
4272*49cdfc7eSAndroid Build Coastguard Worker					if ($s =~ s/^.*?\n//) {
4273*49cdfc7eSAndroid Build Coastguard Worker						$cond_lines++;
4274*49cdfc7eSAndroid Build Coastguard Worker					}
4275*49cdfc7eSAndroid Build Coastguard Worker				}
4276*49cdfc7eSAndroid Build Coastguard Worker			}
4277*49cdfc7eSAndroid Build Coastguard Worker
4278*49cdfc7eSAndroid Build Coastguard Worker			my (undef, $sindent) = line_stats("+" . $s);
4279*49cdfc7eSAndroid Build Coastguard Worker			my $stat_real = raw_line($linenr, $cond_lines);
4280*49cdfc7eSAndroid Build Coastguard Worker
4281*49cdfc7eSAndroid Build Coastguard Worker			# Check if either of these lines are modified, else
4282*49cdfc7eSAndroid Build Coastguard Worker			# this is not this patch's fault.
4283*49cdfc7eSAndroid Build Coastguard Worker			if (!defined($stat_real) ||
4284*49cdfc7eSAndroid Build Coastguard Worker			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
4285*49cdfc7eSAndroid Build Coastguard Worker				$check = 0;
4286*49cdfc7eSAndroid Build Coastguard Worker			}
4287*49cdfc7eSAndroid Build Coastguard Worker			if (defined($stat_real) && $cond_lines > 1) {
4288*49cdfc7eSAndroid Build Coastguard Worker				$stat_real = "[...]\n$stat_real";
4289*49cdfc7eSAndroid Build Coastguard Worker			}
4290*49cdfc7eSAndroid Build Coastguard Worker
4291*49cdfc7eSAndroid Build Coastguard Worker			#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";
4292*49cdfc7eSAndroid Build Coastguard Worker
4293*49cdfc7eSAndroid Build Coastguard Worker			if ($check && $s ne '' &&
4294*49cdfc7eSAndroid Build Coastguard Worker			    (($sindent % $tabsize) != 0 ||
4295*49cdfc7eSAndroid Build Coastguard Worker			     ($sindent < $indent) ||
4296*49cdfc7eSAndroid Build Coastguard Worker			     ($sindent == $indent &&
4297*49cdfc7eSAndroid Build Coastguard Worker			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4298*49cdfc7eSAndroid Build Coastguard Worker			     ($sindent > $indent + $tabsize))) {
4299*49cdfc7eSAndroid Build Coastguard Worker				WARN("SUSPECT_CODE_INDENT",
4300*49cdfc7eSAndroid Build Coastguard Worker				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
4301*49cdfc7eSAndroid Build Coastguard Worker			}
4302*49cdfc7eSAndroid Build Coastguard Worker		}
4303*49cdfc7eSAndroid Build Coastguard Worker
4304*49cdfc7eSAndroid Build Coastguard Worker		# Track the 'values' across context and added lines.
4305*49cdfc7eSAndroid Build Coastguard Worker		my $opline = $line; $opline =~ s/^./ /;
4306*49cdfc7eSAndroid Build Coastguard Worker		my ($curr_values, $curr_vars) =
4307*49cdfc7eSAndroid Build Coastguard Worker				annotate_values($opline . "\n", $prev_values);
4308*49cdfc7eSAndroid Build Coastguard Worker		$curr_values = $prev_values . $curr_values;
4309*49cdfc7eSAndroid Build Coastguard Worker		if ($dbg_values) {
4310*49cdfc7eSAndroid Build Coastguard Worker			my $outline = $opline; $outline =~ s/\t/ /g;
4311*49cdfc7eSAndroid Build Coastguard Worker			print "$linenr > .$outline\n";
4312*49cdfc7eSAndroid Build Coastguard Worker			print "$linenr > $curr_values\n";
4313*49cdfc7eSAndroid Build Coastguard Worker			print "$linenr >  $curr_vars\n";
4314*49cdfc7eSAndroid Build Coastguard Worker		}
4315*49cdfc7eSAndroid Build Coastguard Worker		$prev_values = substr($curr_values, -1);
4316*49cdfc7eSAndroid Build Coastguard Worker
4317*49cdfc7eSAndroid Build Coastguard Worker#ignore lines not being added
4318*49cdfc7eSAndroid Build Coastguard Worker		next if ($line =~ /^[^\+]/);
4319*49cdfc7eSAndroid Build Coastguard Worker
4320*49cdfc7eSAndroid Build Coastguard Worker# check for self assignments used to avoid compiler warnings
4321*49cdfc7eSAndroid Build Coastguard Worker# e.g.:	int foo = foo, *bar = NULL;
4322*49cdfc7eSAndroid Build Coastguard Worker#	struct foo bar = *(&(bar));
4323*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
4324*49cdfc7eSAndroid Build Coastguard Worker			my $var = $1;
4325*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
4326*49cdfc7eSAndroid Build Coastguard Worker				WARN("SELF_ASSIGNMENT",
4327*49cdfc7eSAndroid Build Coastguard Worker				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
4328*49cdfc7eSAndroid Build Coastguard Worker			}
4329*49cdfc7eSAndroid Build Coastguard Worker		}
4330*49cdfc7eSAndroid Build Coastguard Worker
4331*49cdfc7eSAndroid Build Coastguard Worker# check for dereferences that span multiple lines
4332*49cdfc7eSAndroid Build Coastguard Worker		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
4333*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
4334*49cdfc7eSAndroid Build Coastguard Worker			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
4335*49cdfc7eSAndroid Build Coastguard Worker			my $ref = $1;
4336*49cdfc7eSAndroid Build Coastguard Worker			$line =~ /^.\s*($Lval)/;
4337*49cdfc7eSAndroid Build Coastguard Worker			$ref .= $1;
4338*49cdfc7eSAndroid Build Coastguard Worker			$ref =~ s/\s//g;
4339*49cdfc7eSAndroid Build Coastguard Worker			WARN("MULTILINE_DEREFERENCE",
4340*49cdfc7eSAndroid Build Coastguard Worker			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
4341*49cdfc7eSAndroid Build Coastguard Worker		}
4342*49cdfc7eSAndroid Build Coastguard Worker
4343*49cdfc7eSAndroid Build Coastguard Worker# check for declarations of signed or unsigned without int
4344*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4345*49cdfc7eSAndroid Build Coastguard Worker			my $type = $1;
4346*49cdfc7eSAndroid Build Coastguard Worker			my $var = $2;
4347*49cdfc7eSAndroid Build Coastguard Worker			$var = "" if (!defined $var);
4348*49cdfc7eSAndroid Build Coastguard Worker			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4349*49cdfc7eSAndroid Build Coastguard Worker				my $sign = $1;
4350*49cdfc7eSAndroid Build Coastguard Worker				my $pointer = $2;
4351*49cdfc7eSAndroid Build Coastguard Worker
4352*49cdfc7eSAndroid Build Coastguard Worker				$pointer = "" if (!defined $pointer);
4353*49cdfc7eSAndroid Build Coastguard Worker
4354*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("UNSPECIFIED_INT",
4355*49cdfc7eSAndroid Build Coastguard Worker					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4356*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
4357*49cdfc7eSAndroid Build Coastguard Worker					my $decl = trim($sign) . " int ";
4358*49cdfc7eSAndroid Build Coastguard Worker					my $comp_pointer = $pointer;
4359*49cdfc7eSAndroid Build Coastguard Worker					$comp_pointer =~ s/\s//g;
4360*49cdfc7eSAndroid Build Coastguard Worker					$decl .= $comp_pointer;
4361*49cdfc7eSAndroid Build Coastguard Worker					$decl = rtrim($decl) if ($var eq "");
4362*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4363*49cdfc7eSAndroid Build Coastguard Worker				}
4364*49cdfc7eSAndroid Build Coastguard Worker			}
4365*49cdfc7eSAndroid Build Coastguard Worker		}
4366*49cdfc7eSAndroid Build Coastguard Worker
4367*49cdfc7eSAndroid Build Coastguard Worker# TEST: allow direct testing of the type matcher.
4368*49cdfc7eSAndroid Build Coastguard Worker		if ($dbg_type) {
4369*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /^.\s*$Declare\s*$/) {
4370*49cdfc7eSAndroid Build Coastguard Worker				ERROR("TEST_TYPE",
4371*49cdfc7eSAndroid Build Coastguard Worker				      "TEST: is type\n" . $herecurr);
4372*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4373*49cdfc7eSAndroid Build Coastguard Worker				ERROR("TEST_NOT_TYPE",
4374*49cdfc7eSAndroid Build Coastguard Worker				      "TEST: is not type ($1 is)\n". $herecurr);
4375*49cdfc7eSAndroid Build Coastguard Worker			}
4376*49cdfc7eSAndroid Build Coastguard Worker			next;
4377*49cdfc7eSAndroid Build Coastguard Worker		}
4378*49cdfc7eSAndroid Build Coastguard Worker# TEST: allow direct testing of the attribute matcher.
4379*49cdfc7eSAndroid Build Coastguard Worker		if ($dbg_attr) {
4380*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /^.\s*$Modifier\s*$/) {
4381*49cdfc7eSAndroid Build Coastguard Worker				ERROR("TEST_ATTR",
4382*49cdfc7eSAndroid Build Coastguard Worker				      "TEST: is attr\n" . $herecurr);
4383*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4384*49cdfc7eSAndroid Build Coastguard Worker				ERROR("TEST_NOT_ATTR",
4385*49cdfc7eSAndroid Build Coastguard Worker				      "TEST: is not attr ($1 is)\n". $herecurr);
4386*49cdfc7eSAndroid Build Coastguard Worker			}
4387*49cdfc7eSAndroid Build Coastguard Worker			next;
4388*49cdfc7eSAndroid Build Coastguard Worker		}
4389*49cdfc7eSAndroid Build Coastguard Worker
4390*49cdfc7eSAndroid Build Coastguard Worker# check for initialisation to aggregates open brace on the next line
4391*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*{/ &&
4392*49cdfc7eSAndroid Build Coastguard Worker		    $prevline =~ /(?:^|[^=])=\s*$/) {
4393*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("OPEN_BRACE",
4394*49cdfc7eSAndroid Build Coastguard Worker				  "that open brace { should be on the previous line\n" . $hereprev) &&
4395*49cdfc7eSAndroid Build Coastguard Worker			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4396*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr - 1, $prevrawline);
4397*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr, $rawline);
4398*49cdfc7eSAndroid Build Coastguard Worker				my $fixedline = $prevrawline;
4399*49cdfc7eSAndroid Build Coastguard Worker				$fixedline =~ s/\s*=\s*$/ = {/;
4400*49cdfc7eSAndroid Build Coastguard Worker				fix_insert_line($fixlinenr, $fixedline);
4401*49cdfc7eSAndroid Build Coastguard Worker				$fixedline = $line;
4402*49cdfc7eSAndroid Build Coastguard Worker				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4403*49cdfc7eSAndroid Build Coastguard Worker				fix_insert_line($fixlinenr, $fixedline);
4404*49cdfc7eSAndroid Build Coastguard Worker			}
4405*49cdfc7eSAndroid Build Coastguard Worker		}
4406*49cdfc7eSAndroid Build Coastguard Worker
4407*49cdfc7eSAndroid Build Coastguard Worker#
4408*49cdfc7eSAndroid Build Coastguard Worker# Checks which are anchored on the added line.
4409*49cdfc7eSAndroid Build Coastguard Worker#
4410*49cdfc7eSAndroid Build Coastguard Worker
4411*49cdfc7eSAndroid Build Coastguard Worker# check for malformed paths in #include statements (uses RAW line)
4412*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4413*49cdfc7eSAndroid Build Coastguard Worker			my $path = $1;
4414*49cdfc7eSAndroid Build Coastguard Worker			if ($path =~ m{//}) {
4415*49cdfc7eSAndroid Build Coastguard Worker				ERROR("MALFORMED_INCLUDE",
4416*49cdfc7eSAndroid Build Coastguard Worker				      "malformed #include filename\n" . $herecurr);
4417*49cdfc7eSAndroid Build Coastguard Worker			}
4418*49cdfc7eSAndroid Build Coastguard Worker			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4419*49cdfc7eSAndroid Build Coastguard Worker				ERROR("UAPI_INCLUDE",
4420*49cdfc7eSAndroid Build Coastguard Worker				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4421*49cdfc7eSAndroid Build Coastguard Worker			}
4422*49cdfc7eSAndroid Build Coastguard Worker		}
4423*49cdfc7eSAndroid Build Coastguard Worker
4424*49cdfc7eSAndroid Build Coastguard Worker# no C99 // comments
4425*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ m{//}) {
4426*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("C99_COMMENTS",
4427*49cdfc7eSAndroid Build Coastguard Worker				  "do not use C99 // comments\n" . $herecurr) &&
4428*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4429*49cdfc7eSAndroid Build Coastguard Worker				my $line = $fixed[$fixlinenr];
4430*49cdfc7eSAndroid Build Coastguard Worker				if ($line =~ /\/\/(.*)$/) {
4431*49cdfc7eSAndroid Build Coastguard Worker					my $comment = trim($1);
4432*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
4433*49cdfc7eSAndroid Build Coastguard Worker				}
4434*49cdfc7eSAndroid Build Coastguard Worker			}
4435*49cdfc7eSAndroid Build Coastguard Worker		}
4436*49cdfc7eSAndroid Build Coastguard Worker		# Remove C99 comments.
4437*49cdfc7eSAndroid Build Coastguard Worker		$line =~ s@//.*@@;
4438*49cdfc7eSAndroid Build Coastguard Worker		$opline =~ s@//.*@@;
4439*49cdfc7eSAndroid Build Coastguard Worker
4440*49cdfc7eSAndroid Build Coastguard Worker# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
4441*49cdfc7eSAndroid Build Coastguard Worker# the whole statement.
4442*49cdfc7eSAndroid Build Coastguard Worker#print "APW <$lines[$realline_next - 1]>\n";
4443*49cdfc7eSAndroid Build Coastguard Worker		if (defined $realline_next &&
4444*49cdfc7eSAndroid Build Coastguard Worker		    exists $lines[$realline_next - 1] &&
4445*49cdfc7eSAndroid Build Coastguard Worker		    !defined $suppress_export{$realline_next} &&
4446*49cdfc7eSAndroid Build Coastguard Worker		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
4447*49cdfc7eSAndroid Build Coastguard Worker			# Handle definitions which produce identifiers with
4448*49cdfc7eSAndroid Build Coastguard Worker			# a prefix:
4449*49cdfc7eSAndroid Build Coastguard Worker			#   XXX(foo);
4450*49cdfc7eSAndroid Build Coastguard Worker			#   EXPORT_SYMBOL(something_foo);
4451*49cdfc7eSAndroid Build Coastguard Worker			my $name = $1;
4452*49cdfc7eSAndroid Build Coastguard Worker			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
4453*49cdfc7eSAndroid Build Coastguard Worker			    $name =~ /^${Ident}_$2/) {
4454*49cdfc7eSAndroid Build Coastguard Worker#print "FOO C name<$name>\n";
4455*49cdfc7eSAndroid Build Coastguard Worker				$suppress_export{$realline_next} = 1;
4456*49cdfc7eSAndroid Build Coastguard Worker
4457*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($stat !~ /(?:
4458*49cdfc7eSAndroid Build Coastguard Worker				\n.}\s*$|
4459*49cdfc7eSAndroid Build Coastguard Worker				^.DEFINE_$Ident\(\Q$name\E\)|
4460*49cdfc7eSAndroid Build Coastguard Worker				^.DECLARE_$Ident\(\Q$name\E\)|
4461*49cdfc7eSAndroid Build Coastguard Worker				^.LIST_HEAD\(\Q$name\E\)|
4462*49cdfc7eSAndroid Build Coastguard Worker				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
4463*49cdfc7eSAndroid Build Coastguard Worker				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
4464*49cdfc7eSAndroid Build Coastguard Worker			    )/x) {
4465*49cdfc7eSAndroid Build Coastguard Worker#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
4466*49cdfc7eSAndroid Build Coastguard Worker				$suppress_export{$realline_next} = 2;
4467*49cdfc7eSAndroid Build Coastguard Worker			} else {
4468*49cdfc7eSAndroid Build Coastguard Worker				$suppress_export{$realline_next} = 1;
4469*49cdfc7eSAndroid Build Coastguard Worker			}
4470*49cdfc7eSAndroid Build Coastguard Worker		}
4471*49cdfc7eSAndroid Build Coastguard Worker		if (!defined $suppress_export{$linenr} &&
4472*49cdfc7eSAndroid Build Coastguard Worker		    $prevline =~ /^.\s*$/ &&
4473*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
4474*49cdfc7eSAndroid Build Coastguard Worker#print "FOO B <$lines[$linenr - 1]>\n";
4475*49cdfc7eSAndroid Build Coastguard Worker			$suppress_export{$linenr} = 2;
4476*49cdfc7eSAndroid Build Coastguard Worker		}
4477*49cdfc7eSAndroid Build Coastguard Worker		if (defined $suppress_export{$linenr} &&
4478*49cdfc7eSAndroid Build Coastguard Worker		    $suppress_export{$linenr} == 2) {
4479*49cdfc7eSAndroid Build Coastguard Worker			WARN("EXPORT_SYMBOL",
4480*49cdfc7eSAndroid Build Coastguard Worker			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
4481*49cdfc7eSAndroid Build Coastguard Worker		}
4482*49cdfc7eSAndroid Build Coastguard Worker
4483*49cdfc7eSAndroid Build Coastguard Worker# check for global initialisers.
4484*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
4485*49cdfc7eSAndroid Build Coastguard Worker		    !exclude_global_initialisers($realfile)) {
4486*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("GLOBAL_INITIALISERS",
4487*49cdfc7eSAndroid Build Coastguard Worker				  "do not initialise globals to $1\n" . $herecurr) &&
4488*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4489*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4490*49cdfc7eSAndroid Build Coastguard Worker			}
4491*49cdfc7eSAndroid Build Coastguard Worker		}
4492*49cdfc7eSAndroid Build Coastguard Worker# check for static initialisers.
4493*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4494*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("INITIALISED_STATIC",
4495*49cdfc7eSAndroid Build Coastguard Worker				  "do not initialise statics to $1\n" .
4496*49cdfc7eSAndroid Build Coastguard Worker				      $herecurr) &&
4497*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4498*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4499*49cdfc7eSAndroid Build Coastguard Worker			}
4500*49cdfc7eSAndroid Build Coastguard Worker		}
4501*49cdfc7eSAndroid Build Coastguard Worker
4502*49cdfc7eSAndroid Build Coastguard Worker# check for misordered declarations of char/short/int/long with signed/unsigned
4503*49cdfc7eSAndroid Build Coastguard Worker		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
4504*49cdfc7eSAndroid Build Coastguard Worker			my $tmp = trim($1);
4505*49cdfc7eSAndroid Build Coastguard Worker			WARN("MISORDERED_TYPE",
4506*49cdfc7eSAndroid Build Coastguard Worker			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
4507*49cdfc7eSAndroid Build Coastguard Worker		}
4508*49cdfc7eSAndroid Build Coastguard Worker
4509*49cdfc7eSAndroid Build Coastguard Worker# check for unnecessary <signed> int declarations of short/long/long long
4510*49cdfc7eSAndroid Build Coastguard Worker		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4511*49cdfc7eSAndroid Build Coastguard Worker			my $type = trim($1);
4512*49cdfc7eSAndroid Build Coastguard Worker			next if ($type !~ /\bint\b/);
4513*49cdfc7eSAndroid Build Coastguard Worker			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4514*49cdfc7eSAndroid Build Coastguard Worker			my $new_type = $type;
4515*49cdfc7eSAndroid Build Coastguard Worker			$new_type =~ s/\b\s*int\s*\b/ /;
4516*49cdfc7eSAndroid Build Coastguard Worker			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4517*49cdfc7eSAndroid Build Coastguard Worker			$new_type =~ s/^const\s+//;
4518*49cdfc7eSAndroid Build Coastguard Worker			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4519*49cdfc7eSAndroid Build Coastguard Worker			$new_type = "const $new_type" if ($type =~ /^const\b/);
4520*49cdfc7eSAndroid Build Coastguard Worker			$new_type =~ s/\s+/ /g;
4521*49cdfc7eSAndroid Build Coastguard Worker			$new_type = trim($new_type);
4522*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("UNNECESSARY_INT",
4523*49cdfc7eSAndroid Build Coastguard Worker				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4524*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4525*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4526*49cdfc7eSAndroid Build Coastguard Worker			}
4527*49cdfc7eSAndroid Build Coastguard Worker		}
4528*49cdfc7eSAndroid Build Coastguard Worker
4529*49cdfc7eSAndroid Build Coastguard Worker# check for static const char * arrays.
4530*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4531*49cdfc7eSAndroid Build Coastguard Worker			WARN("STATIC_CONST_CHAR_ARRAY",
4532*49cdfc7eSAndroid Build Coastguard Worker			     "static const char * array should probably be static const char * const\n" .
4533*49cdfc7eSAndroid Build Coastguard Worker				$herecurr);
4534*49cdfc7eSAndroid Build Coastguard Worker		}
4535*49cdfc7eSAndroid Build Coastguard Worker
4536*49cdfc7eSAndroid Build Coastguard Worker# check for initialized const char arrays that should be static const
4537*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
4538*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("STATIC_CONST_CHAR_ARRAY",
4539*49cdfc7eSAndroid Build Coastguard Worker				 "const array should probably be static const\n" . $herecurr) &&
4540*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4541*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
4542*49cdfc7eSAndroid Build Coastguard Worker			}
4543*49cdfc7eSAndroid Build Coastguard Worker		}
4544*49cdfc7eSAndroid Build Coastguard Worker
4545*49cdfc7eSAndroid Build Coastguard Worker# check for static char foo[] = "bar" declarations.
4546*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4547*49cdfc7eSAndroid Build Coastguard Worker			WARN("STATIC_CONST_CHAR_ARRAY",
4548*49cdfc7eSAndroid Build Coastguard Worker			     "static char array declaration should probably be static const char\n" .
4549*49cdfc7eSAndroid Build Coastguard Worker				$herecurr);
4550*49cdfc7eSAndroid Build Coastguard Worker		}
4551*49cdfc7eSAndroid Build Coastguard Worker
4552*49cdfc7eSAndroid Build Coastguard Worker# check for const <foo> const where <foo> is not a pointer or array type
4553*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4554*49cdfc7eSAndroid Build Coastguard Worker			my $found = $1;
4555*49cdfc7eSAndroid Build Coastguard Worker			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4556*49cdfc7eSAndroid Build Coastguard Worker				WARN("CONST_CONST",
4557*49cdfc7eSAndroid Build Coastguard Worker				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4558*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4559*49cdfc7eSAndroid Build Coastguard Worker				WARN("CONST_CONST",
4560*49cdfc7eSAndroid Build Coastguard Worker				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4561*49cdfc7eSAndroid Build Coastguard Worker			}
4562*49cdfc7eSAndroid Build Coastguard Worker		}
4563*49cdfc7eSAndroid Build Coastguard Worker
4564*49cdfc7eSAndroid Build Coastguard Worker# check for const static or static <non ptr type> const declarations
4565*49cdfc7eSAndroid Build Coastguard Worker# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
4566*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
4567*49cdfc7eSAndroid Build Coastguard Worker		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
4568*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("STATIC_CONST",
4569*49cdfc7eSAndroid Build Coastguard Worker				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
4570*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4571*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
4572*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
4573*49cdfc7eSAndroid Build Coastguard Worker			}
4574*49cdfc7eSAndroid Build Coastguard Worker		}
4575*49cdfc7eSAndroid Build Coastguard Worker
4576*49cdfc7eSAndroid Build Coastguard Worker# check for non-global char *foo[] = {"bar", ...} declarations.
4577*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
4578*49cdfc7eSAndroid Build Coastguard Worker			WARN("STATIC_CONST_CHAR_ARRAY",
4579*49cdfc7eSAndroid Build Coastguard Worker			     "char * array declaration might be better as static const\n" .
4580*49cdfc7eSAndroid Build Coastguard Worker				$herecurr);
4581*49cdfc7eSAndroid Build Coastguard Worker		}
4582*49cdfc7eSAndroid Build Coastguard Worker
4583*49cdfc7eSAndroid Build Coastguard Worker# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4584*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4585*49cdfc7eSAndroid Build Coastguard Worker			my $array = $1;
4586*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4587*49cdfc7eSAndroid Build Coastguard Worker				my $array_div = $1;
4588*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("ARRAY_SIZE",
4589*49cdfc7eSAndroid Build Coastguard Worker					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4590*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
4591*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4592*49cdfc7eSAndroid Build Coastguard Worker				}
4593*49cdfc7eSAndroid Build Coastguard Worker			}
4594*49cdfc7eSAndroid Build Coastguard Worker		}
4595*49cdfc7eSAndroid Build Coastguard Worker
4596*49cdfc7eSAndroid Build Coastguard Worker# check for function declarations without arguments like "int foo()"
4597*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4598*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("FUNCTION_WITHOUT_ARGS",
4599*49cdfc7eSAndroid Build Coastguard Worker				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4600*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4601*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4602*49cdfc7eSAndroid Build Coastguard Worker			}
4603*49cdfc7eSAndroid Build Coastguard Worker		}
4604*49cdfc7eSAndroid Build Coastguard Worker
4605*49cdfc7eSAndroid Build Coastguard Worker# check for new typedefs, only function parameters and sparse annotations
4606*49cdfc7eSAndroid Build Coastguard Worker# make sense.
4607*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\btypedef\s/ &&
4608*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4609*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
4610*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /\b$typeTypedefs\b/ &&
4611*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /\b__bitwise\b/) {
4612*49cdfc7eSAndroid Build Coastguard Worker			WARN("NEW_TYPEDEFS",
4613*49cdfc7eSAndroid Build Coastguard Worker			     "do not add new typedefs\n" . $herecurr);
4614*49cdfc7eSAndroid Build Coastguard Worker		}
4615*49cdfc7eSAndroid Build Coastguard Worker
4616*49cdfc7eSAndroid Build Coastguard Worker# * goes on variable not on type
4617*49cdfc7eSAndroid Build Coastguard Worker		# (char*[ const])
4618*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4619*49cdfc7eSAndroid Build Coastguard Worker			#print "AA<$1>\n";
4620*49cdfc7eSAndroid Build Coastguard Worker			my ($ident, $from, $to) = ($1, $2, $2);
4621*49cdfc7eSAndroid Build Coastguard Worker
4622*49cdfc7eSAndroid Build Coastguard Worker			# Should start with a space.
4623*49cdfc7eSAndroid Build Coastguard Worker			$to =~ s/^(\S)/ $1/;
4624*49cdfc7eSAndroid Build Coastguard Worker			# Should not end with a space.
4625*49cdfc7eSAndroid Build Coastguard Worker			$to =~ s/\s+$//;
4626*49cdfc7eSAndroid Build Coastguard Worker			# '*'s should not have spaces between.
4627*49cdfc7eSAndroid Build Coastguard Worker			while ($to =~ s/\*\s+\*/\*\*/) {
4628*49cdfc7eSAndroid Build Coastguard Worker			}
4629*49cdfc7eSAndroid Build Coastguard Worker
4630*49cdfc7eSAndroid Build Coastguard Worker##			print "1: from<$from> to<$to> ident<$ident>\n";
4631*49cdfc7eSAndroid Build Coastguard Worker			if ($from ne $to) {
4632*49cdfc7eSAndroid Build Coastguard Worker				if (ERROR("POINTER_LOCATION",
4633*49cdfc7eSAndroid Build Coastguard Worker					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
4634*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
4635*49cdfc7eSAndroid Build Coastguard Worker					my $sub_from = $ident;
4636*49cdfc7eSAndroid Build Coastguard Worker					my $sub_to = $ident;
4637*49cdfc7eSAndroid Build Coastguard Worker					$sub_to =~ s/\Q$from\E/$to/;
4638*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~
4639*49cdfc7eSAndroid Build Coastguard Worker					    s@\Q$sub_from\E@$sub_to@;
4640*49cdfc7eSAndroid Build Coastguard Worker				}
4641*49cdfc7eSAndroid Build Coastguard Worker			}
4642*49cdfc7eSAndroid Build Coastguard Worker		}
4643*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4644*49cdfc7eSAndroid Build Coastguard Worker			#print "BB<$1>\n";
4645*49cdfc7eSAndroid Build Coastguard Worker			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4646*49cdfc7eSAndroid Build Coastguard Worker
4647*49cdfc7eSAndroid Build Coastguard Worker			# Should start with a space.
4648*49cdfc7eSAndroid Build Coastguard Worker			$to =~ s/^(\S)/ $1/;
4649*49cdfc7eSAndroid Build Coastguard Worker			# Should not end with a space.
4650*49cdfc7eSAndroid Build Coastguard Worker			$to =~ s/\s+$//;
4651*49cdfc7eSAndroid Build Coastguard Worker			# '*'s should not have spaces between.
4652*49cdfc7eSAndroid Build Coastguard Worker			while ($to =~ s/\*\s+\*/\*\*/) {
4653*49cdfc7eSAndroid Build Coastguard Worker			}
4654*49cdfc7eSAndroid Build Coastguard Worker			# Modifiers should have spaces.
4655*49cdfc7eSAndroid Build Coastguard Worker			$to =~ s/(\b$Modifier$)/$1 /;
4656*49cdfc7eSAndroid Build Coastguard Worker
4657*49cdfc7eSAndroid Build Coastguard Worker##			print "2: from<$from> to<$to> ident<$ident>\n";
4658*49cdfc7eSAndroid Build Coastguard Worker			if ($from ne $to && $ident !~ /^$Modifier$/) {
4659*49cdfc7eSAndroid Build Coastguard Worker				if (ERROR("POINTER_LOCATION",
4660*49cdfc7eSAndroid Build Coastguard Worker					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
4661*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
4662*49cdfc7eSAndroid Build Coastguard Worker
4663*49cdfc7eSAndroid Build Coastguard Worker					my $sub_from = $match;
4664*49cdfc7eSAndroid Build Coastguard Worker					my $sub_to = $match;
4665*49cdfc7eSAndroid Build Coastguard Worker					$sub_to =~ s/\Q$from\E/$to/;
4666*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~
4667*49cdfc7eSAndroid Build Coastguard Worker					    s@\Q$sub_from\E@$sub_to@;
4668*49cdfc7eSAndroid Build Coastguard Worker				}
4669*49cdfc7eSAndroid Build Coastguard Worker			}
4670*49cdfc7eSAndroid Build Coastguard Worker		}
4671*49cdfc7eSAndroid Build Coastguard Worker
4672*49cdfc7eSAndroid Build Coastguard Worker# avoid BUG() or BUG_ON()
4673*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
4674*49cdfc7eSAndroid Build Coastguard Worker			my $msg_level = \&WARN;
4675*49cdfc7eSAndroid Build Coastguard Worker			$msg_level = \&CHK if ($file);
4676*49cdfc7eSAndroid Build Coastguard Worker			&{$msg_level}("AVOID_BUG",
4677*49cdfc7eSAndroid Build Coastguard Worker				      "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
4678*49cdfc7eSAndroid Build Coastguard Worker		}
4679*49cdfc7eSAndroid Build Coastguard Worker
4680*49cdfc7eSAndroid Build Coastguard Worker# avoid LINUX_VERSION_CODE
4681*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4682*49cdfc7eSAndroid Build Coastguard Worker			WARN("LINUX_VERSION_CODE",
4683*49cdfc7eSAndroid Build Coastguard Worker			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
4684*49cdfc7eSAndroid Build Coastguard Worker		}
4685*49cdfc7eSAndroid Build Coastguard Worker
4686*49cdfc7eSAndroid Build Coastguard Worker# check for uses of printk_ratelimit
4687*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4688*49cdfc7eSAndroid Build Coastguard Worker			WARN("PRINTK_RATELIMITED",
4689*49cdfc7eSAndroid Build Coastguard Worker			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
4690*49cdfc7eSAndroid Build Coastguard Worker		}
4691*49cdfc7eSAndroid Build Coastguard Worker
4692*49cdfc7eSAndroid Build Coastguard Worker# printk should use KERN_* levels
4693*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4694*49cdfc7eSAndroid Build Coastguard Worker			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4695*49cdfc7eSAndroid Build Coastguard Worker			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
4696*49cdfc7eSAndroid Build Coastguard Worker		}
4697*49cdfc7eSAndroid Build Coastguard Worker
4698*49cdfc7eSAndroid Build Coastguard Worker# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4699*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4700*49cdfc7eSAndroid Build Coastguard Worker			my $printk = $1;
4701*49cdfc7eSAndroid Build Coastguard Worker			my $modifier = $2;
4702*49cdfc7eSAndroid Build Coastguard Worker			my $orig = $3;
4703*49cdfc7eSAndroid Build Coastguard Worker			$modifier = "" if (!defined($modifier));
4704*49cdfc7eSAndroid Build Coastguard Worker			my $level = lc($orig);
4705*49cdfc7eSAndroid Build Coastguard Worker			$level = "warn" if ($level eq "warning");
4706*49cdfc7eSAndroid Build Coastguard Worker			my $level2 = $level;
4707*49cdfc7eSAndroid Build Coastguard Worker			$level2 = "dbg" if ($level eq "debug");
4708*49cdfc7eSAndroid Build Coastguard Worker			$level .= $modifier;
4709*49cdfc7eSAndroid Build Coastguard Worker			$level2 .= $modifier;
4710*49cdfc7eSAndroid Build Coastguard Worker			WARN("PREFER_PR_LEVEL",
4711*49cdfc7eSAndroid Build Coastguard Worker			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to $printk(KERN_$orig ...\n" . $herecurr);
4712*49cdfc7eSAndroid Build Coastguard Worker		}
4713*49cdfc7eSAndroid Build Coastguard Worker
4714*49cdfc7eSAndroid Build Coastguard Worker# prefer dev_<level> to dev_printk(KERN_<LEVEL>
4715*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4716*49cdfc7eSAndroid Build Coastguard Worker			my $orig = $1;
4717*49cdfc7eSAndroid Build Coastguard Worker			my $level = lc($orig);
4718*49cdfc7eSAndroid Build Coastguard Worker			$level = "warn" if ($level eq "warning");
4719*49cdfc7eSAndroid Build Coastguard Worker			$level = "dbg" if ($level eq "debug");
4720*49cdfc7eSAndroid Build Coastguard Worker			WARN("PREFER_DEV_LEVEL",
4721*49cdfc7eSAndroid Build Coastguard Worker			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4722*49cdfc7eSAndroid Build Coastguard Worker		}
4723*49cdfc7eSAndroid Build Coastguard Worker
4724*49cdfc7eSAndroid Build Coastguard Worker# trace_printk should not be used in production code.
4725*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
4726*49cdfc7eSAndroid Build Coastguard Worker			WARN("TRACE_PRINTK",
4727*49cdfc7eSAndroid Build Coastguard Worker			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
4728*49cdfc7eSAndroid Build Coastguard Worker		}
4729*49cdfc7eSAndroid Build Coastguard Worker
4730*49cdfc7eSAndroid Build Coastguard Worker# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
4731*49cdfc7eSAndroid Build Coastguard Worker# number of false positives, but assembly files are not checked, so at
4732*49cdfc7eSAndroid Build Coastguard Worker# least the arch entry code will not trigger this warning.
4733*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bENOSYS\b/) {
4734*49cdfc7eSAndroid Build Coastguard Worker			WARN("ENOSYS",
4735*49cdfc7eSAndroid Build Coastguard Worker			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
4736*49cdfc7eSAndroid Build Coastguard Worker		}
4737*49cdfc7eSAndroid Build Coastguard Worker
4738*49cdfc7eSAndroid Build Coastguard Worker# ENOTSUPP is not a standard error code and should be avoided in new patches.
4739*49cdfc7eSAndroid Build Coastguard Worker# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
4740*49cdfc7eSAndroid Build Coastguard Worker# Similarly to ENOSYS warning a small number of false positives is expected.
4741*49cdfc7eSAndroid Build Coastguard Worker		if (!$file && $line =~ /\bENOTSUPP\b/) {
4742*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("ENOTSUPP",
4743*49cdfc7eSAndroid Build Coastguard Worker				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
4744*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4745*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
4746*49cdfc7eSAndroid Build Coastguard Worker			}
4747*49cdfc7eSAndroid Build Coastguard Worker		}
4748*49cdfc7eSAndroid Build Coastguard Worker
4749*49cdfc7eSAndroid Build Coastguard Worker# function brace can't be on same line, except for #defines of do while,
4750*49cdfc7eSAndroid Build Coastguard Worker# or if closed on same line
4751*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
4752*49cdfc7eSAndroid Build Coastguard Worker		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
4753*49cdfc7eSAndroid Build Coastguard Worker		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
4754*49cdfc7eSAndroid Build Coastguard Worker		    $sline !~ /}/) {
4755*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("OPEN_BRACE",
4756*49cdfc7eSAndroid Build Coastguard Worker				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
4757*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4758*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr, $rawline);
4759*49cdfc7eSAndroid Build Coastguard Worker				my $fixed_line = $rawline;
4760*49cdfc7eSAndroid Build Coastguard Worker				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
4761*49cdfc7eSAndroid Build Coastguard Worker				my $line1 = $1;
4762*49cdfc7eSAndroid Build Coastguard Worker				my $line2 = $2;
4763*49cdfc7eSAndroid Build Coastguard Worker				fix_insert_line($fixlinenr, ltrim($line1));
4764*49cdfc7eSAndroid Build Coastguard Worker				fix_insert_line($fixlinenr, "\+{");
4765*49cdfc7eSAndroid Build Coastguard Worker				if ($line2 !~ /^\s*$/) {
4766*49cdfc7eSAndroid Build Coastguard Worker					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
4767*49cdfc7eSAndroid Build Coastguard Worker				}
4768*49cdfc7eSAndroid Build Coastguard Worker			}
4769*49cdfc7eSAndroid Build Coastguard Worker		}
4770*49cdfc7eSAndroid Build Coastguard Worker
4771*49cdfc7eSAndroid Build Coastguard Worker# open braces for enum, union and struct go on the same line.
4772*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*{/ &&
4773*49cdfc7eSAndroid Build Coastguard Worker		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
4774*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("OPEN_BRACE",
4775*49cdfc7eSAndroid Build Coastguard Worker				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
4776*49cdfc7eSAndroid Build Coastguard Worker			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4777*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr - 1, $prevrawline);
4778*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr, $rawline);
4779*49cdfc7eSAndroid Build Coastguard Worker				my $fixedline = rtrim($prevrawline) . " {";
4780*49cdfc7eSAndroid Build Coastguard Worker				fix_insert_line($fixlinenr, $fixedline);
4781*49cdfc7eSAndroid Build Coastguard Worker				$fixedline = $rawline;
4782*49cdfc7eSAndroid Build Coastguard Worker				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
4783*49cdfc7eSAndroid Build Coastguard Worker				if ($fixedline !~ /^\+\s*$/) {
4784*49cdfc7eSAndroid Build Coastguard Worker					fix_insert_line($fixlinenr, $fixedline);
4785*49cdfc7eSAndroid Build Coastguard Worker				}
4786*49cdfc7eSAndroid Build Coastguard Worker			}
4787*49cdfc7eSAndroid Build Coastguard Worker		}
4788*49cdfc7eSAndroid Build Coastguard Worker
4789*49cdfc7eSAndroid Build Coastguard Worker# missing space after union, struct or enum definition
4790*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
4791*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("SPACING",
4792*49cdfc7eSAndroid Build Coastguard Worker				 "missing space after $1 definition\n" . $herecurr) &&
4793*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
4794*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
4795*49cdfc7eSAndroid Build Coastguard Worker				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
4796*49cdfc7eSAndroid Build Coastguard Worker			}
4797*49cdfc7eSAndroid Build Coastguard Worker		}
4798*49cdfc7eSAndroid Build Coastguard Worker
4799*49cdfc7eSAndroid Build Coastguard Worker# Function pointer declarations
4800*49cdfc7eSAndroid Build Coastguard Worker# check spacing between type, funcptr, and args
4801*49cdfc7eSAndroid Build Coastguard Worker# canonical declaration is "type (*funcptr)(args...)"
4802*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
4803*49cdfc7eSAndroid Build Coastguard Worker			my $declare = $1;
4804*49cdfc7eSAndroid Build Coastguard Worker			my $pre_pointer_space = $2;
4805*49cdfc7eSAndroid Build Coastguard Worker			my $post_pointer_space = $3;
4806*49cdfc7eSAndroid Build Coastguard Worker			my $funcname = $4;
4807*49cdfc7eSAndroid Build Coastguard Worker			my $post_funcname_space = $5;
4808*49cdfc7eSAndroid Build Coastguard Worker			my $pre_args_space = $6;
4809*49cdfc7eSAndroid Build Coastguard Worker
4810*49cdfc7eSAndroid Build Coastguard Worker# the $Declare variable will capture all spaces after the type
4811*49cdfc7eSAndroid Build Coastguard Worker# so check it for a missing trailing missing space but pointer return types
4812*49cdfc7eSAndroid Build Coastguard Worker# don't need a space so don't warn for those.
4813*49cdfc7eSAndroid Build Coastguard Worker			my $post_declare_space = "";
4814*49cdfc7eSAndroid Build Coastguard Worker			if ($declare =~ /(\s+)$/) {
4815*49cdfc7eSAndroid Build Coastguard Worker				$post_declare_space = $1;
4816*49cdfc7eSAndroid Build Coastguard Worker				$declare = rtrim($declare);
4817*49cdfc7eSAndroid Build Coastguard Worker			}
4818*49cdfc7eSAndroid Build Coastguard Worker			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
4819*49cdfc7eSAndroid Build Coastguard Worker				WARN("SPACING",
4820*49cdfc7eSAndroid Build Coastguard Worker				     "missing space after return type\n" . $herecurr);
4821*49cdfc7eSAndroid Build Coastguard Worker				$post_declare_space = " ";
4822*49cdfc7eSAndroid Build Coastguard Worker			}
4823*49cdfc7eSAndroid Build Coastguard Worker
4824*49cdfc7eSAndroid Build Coastguard Worker# unnecessary space "type  (*funcptr)(args...)"
4825*49cdfc7eSAndroid Build Coastguard Worker# This test is not currently implemented because these declarations are
4826*49cdfc7eSAndroid Build Coastguard Worker# equivalent to
4827*49cdfc7eSAndroid Build Coastguard Worker#	int  foo(int bar, ...)
4828*49cdfc7eSAndroid Build Coastguard Worker# and this is form shouldn't/doesn't generate a checkpatch warning.
4829*49cdfc7eSAndroid Build Coastguard Worker#
4830*49cdfc7eSAndroid Build Coastguard Worker#			elsif ($declare =~ /\s{2,}$/) {
4831*49cdfc7eSAndroid Build Coastguard Worker#				WARN("SPACING",
4832*49cdfc7eSAndroid Build Coastguard Worker#				     "Multiple spaces after return type\n" . $herecurr);
4833*49cdfc7eSAndroid Build Coastguard Worker#			}
4834*49cdfc7eSAndroid Build Coastguard Worker
4835*49cdfc7eSAndroid Build Coastguard Worker# unnecessary space "type ( *funcptr)(args...)"
4836*49cdfc7eSAndroid Build Coastguard Worker			if (defined $pre_pointer_space &&
4837*49cdfc7eSAndroid Build Coastguard Worker			    $pre_pointer_space =~ /^\s/) {
4838*49cdfc7eSAndroid Build Coastguard Worker				WARN("SPACING",
4839*49cdfc7eSAndroid Build Coastguard Worker				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
4840*49cdfc7eSAndroid Build Coastguard Worker			}
4841*49cdfc7eSAndroid Build Coastguard Worker
4842*49cdfc7eSAndroid Build Coastguard Worker# unnecessary space "type (* funcptr)(args...)"
4843*49cdfc7eSAndroid Build Coastguard Worker			if (defined $post_pointer_space &&
4844*49cdfc7eSAndroid Build Coastguard Worker			    $post_pointer_space =~ /^\s/) {
4845*49cdfc7eSAndroid Build Coastguard Worker				WARN("SPACING",
4846*49cdfc7eSAndroid Build Coastguard Worker				     "Unnecessary space before function pointer name\n" . $herecurr);
4847*49cdfc7eSAndroid Build Coastguard Worker			}
4848*49cdfc7eSAndroid Build Coastguard Worker
4849*49cdfc7eSAndroid Build Coastguard Worker# unnecessary space "type (*funcptr )(args...)"
4850*49cdfc7eSAndroid Build Coastguard Worker			if (defined $post_funcname_space &&
4851*49cdfc7eSAndroid Build Coastguard Worker			    $post_funcname_space =~ /^\s/) {
4852*49cdfc7eSAndroid Build Coastguard Worker				WARN("SPACING",
4853*49cdfc7eSAndroid Build Coastguard Worker				     "Unnecessary space after function pointer name\n" . $herecurr);
4854*49cdfc7eSAndroid Build Coastguard Worker			}
4855*49cdfc7eSAndroid Build Coastguard Worker
4856*49cdfc7eSAndroid Build Coastguard Worker# unnecessary space "type (*funcptr) (args...)"
4857*49cdfc7eSAndroid Build Coastguard Worker			if (defined $pre_args_space &&
4858*49cdfc7eSAndroid Build Coastguard Worker			    $pre_args_space =~ /^\s/) {
4859*49cdfc7eSAndroid Build Coastguard Worker				WARN("SPACING",
4860*49cdfc7eSAndroid Build Coastguard Worker				     "Unnecessary space before function pointer arguments\n" . $herecurr);
4861*49cdfc7eSAndroid Build Coastguard Worker			}
4862*49cdfc7eSAndroid Build Coastguard Worker
4863*49cdfc7eSAndroid Build Coastguard Worker			if (show_type("SPACING") && $fix) {
4864*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
4865*49cdfc7eSAndroid Build Coastguard Worker				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
4866*49cdfc7eSAndroid Build Coastguard Worker			}
4867*49cdfc7eSAndroid Build Coastguard Worker		}
4868*49cdfc7eSAndroid Build Coastguard Worker
4869*49cdfc7eSAndroid Build Coastguard Worker# check for spacing round square brackets; allowed:
4870*49cdfc7eSAndroid Build Coastguard Worker#  1. with a type on the left -- int [] a;
4871*49cdfc7eSAndroid Build Coastguard Worker#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4872*49cdfc7eSAndroid Build Coastguard Worker#  3. inside a curly brace -- = { [0...10] = 5 }
4873*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ /(.*?\s)\[/g) {
4874*49cdfc7eSAndroid Build Coastguard Worker			my ($where, $prefix) = ($-[1], $1);
4875*49cdfc7eSAndroid Build Coastguard Worker			if ($prefix !~ /$Type\s+$/ &&
4876*49cdfc7eSAndroid Build Coastguard Worker			    ($where != 0 || $prefix !~ /^.\s+$/) &&
4877*49cdfc7eSAndroid Build Coastguard Worker			    $prefix !~ /[{,:]\s+$/) {
4878*49cdfc7eSAndroid Build Coastguard Worker				if (ERROR("BRACKET_SPACE",
4879*49cdfc7eSAndroid Build Coastguard Worker					  "space prohibited before open square bracket '['\n" . $herecurr) &&
4880*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
4881*49cdfc7eSAndroid Build Coastguard Worker				    $fixed[$fixlinenr] =~
4882*49cdfc7eSAndroid Build Coastguard Worker					s/^(\+.*?)\s+\[/$1\[/;
4883*49cdfc7eSAndroid Build Coastguard Worker				}
4884*49cdfc7eSAndroid Build Coastguard Worker			}
4885*49cdfc7eSAndroid Build Coastguard Worker		}
4886*49cdfc7eSAndroid Build Coastguard Worker
4887*49cdfc7eSAndroid Build Coastguard Worker# check for spaces between functions and their parentheses.
4888*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ /($Ident)\s+\(/g) {
4889*49cdfc7eSAndroid Build Coastguard Worker			my $name = $1;
4890*49cdfc7eSAndroid Build Coastguard Worker			my $ctx_before = substr($line, 0, $-[1]);
4891*49cdfc7eSAndroid Build Coastguard Worker			my $ctx = "$ctx_before$name";
4892*49cdfc7eSAndroid Build Coastguard Worker
4893*49cdfc7eSAndroid Build Coastguard Worker			# Ignore those directives where spaces _are_ permitted.
4894*49cdfc7eSAndroid Build Coastguard Worker			if ($name =~ /^(?:
4895*49cdfc7eSAndroid Build Coastguard Worker				if|for|while|switch|return|case|
4896*49cdfc7eSAndroid Build Coastguard Worker				volatile|__volatile__|
4897*49cdfc7eSAndroid Build Coastguard Worker				__attribute__|format|__extension__|
4898*49cdfc7eSAndroid Build Coastguard Worker				asm|__asm__)$/x)
4899*49cdfc7eSAndroid Build Coastguard Worker			{
4900*49cdfc7eSAndroid Build Coastguard Worker			# cpp #define statements have non-optional spaces, ie
4901*49cdfc7eSAndroid Build Coastguard Worker			# if there is a space between the name and the open
4902*49cdfc7eSAndroid Build Coastguard Worker			# parenthesis it is simply not a parameter group.
4903*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
4904*49cdfc7eSAndroid Build Coastguard Worker
4905*49cdfc7eSAndroid Build Coastguard Worker			# cpp #elif statement condition may start with a (
4906*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
4907*49cdfc7eSAndroid Build Coastguard Worker
4908*49cdfc7eSAndroid Build Coastguard Worker			# If this whole things ends with a type its most
4909*49cdfc7eSAndroid Build Coastguard Worker			# likely a typedef for a function.
4910*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($ctx =~ /$Type$/) {
4911*49cdfc7eSAndroid Build Coastguard Worker
4912*49cdfc7eSAndroid Build Coastguard Worker			} else {
4913*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("SPACING",
4914*49cdfc7eSAndroid Build Coastguard Worker					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4915*49cdfc7eSAndroid Build Coastguard Worker					     $fix) {
4916*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~
4917*49cdfc7eSAndroid Build Coastguard Worker					    s/\b$name\s+\(/$name\(/;
4918*49cdfc7eSAndroid Build Coastguard Worker				}
4919*49cdfc7eSAndroid Build Coastguard Worker			}
4920*49cdfc7eSAndroid Build Coastguard Worker		}
4921*49cdfc7eSAndroid Build Coastguard Worker
4922*49cdfc7eSAndroid Build Coastguard Worker# Check operator spacing.
4923*49cdfc7eSAndroid Build Coastguard Worker		if (!($line=~/\#\s*include/)) {
4924*49cdfc7eSAndroid Build Coastguard Worker			my $fixed_line = "";
4925*49cdfc7eSAndroid Build Coastguard Worker			my $line_fixed = 0;
4926*49cdfc7eSAndroid Build Coastguard Worker
4927*49cdfc7eSAndroid Build Coastguard Worker			my $ops = qr{
4928*49cdfc7eSAndroid Build Coastguard Worker				<<=|>>=|<=|>=|==|!=|
4929*49cdfc7eSAndroid Build Coastguard Worker				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4930*49cdfc7eSAndroid Build Coastguard Worker				=>|->|<<|>>|<|>|=|!|~|
4931*49cdfc7eSAndroid Build Coastguard Worker				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
4932*49cdfc7eSAndroid Build Coastguard Worker				\?:|\?|:
4933*49cdfc7eSAndroid Build Coastguard Worker			}x;
4934*49cdfc7eSAndroid Build Coastguard Worker			my @elements = split(/($ops|;)/, $opline);
4935*49cdfc7eSAndroid Build Coastguard Worker
4936*49cdfc7eSAndroid Build Coastguard Worker##			print("element count: <" . $#elements . ">\n");
4937*49cdfc7eSAndroid Build Coastguard Worker##			foreach my $el (@elements) {
4938*49cdfc7eSAndroid Build Coastguard Worker##				print("el: <$el>\n");
4939*49cdfc7eSAndroid Build Coastguard Worker##			}
4940*49cdfc7eSAndroid Build Coastguard Worker
4941*49cdfc7eSAndroid Build Coastguard Worker			my @fix_elements = ();
4942*49cdfc7eSAndroid Build Coastguard Worker			my $off = 0;
4943*49cdfc7eSAndroid Build Coastguard Worker
4944*49cdfc7eSAndroid Build Coastguard Worker			foreach my $el (@elements) {
4945*49cdfc7eSAndroid Build Coastguard Worker				push(@fix_elements, substr($rawline, $off, length($el)));
4946*49cdfc7eSAndroid Build Coastguard Worker				$off += length($el);
4947*49cdfc7eSAndroid Build Coastguard Worker			}
4948*49cdfc7eSAndroid Build Coastguard Worker
4949*49cdfc7eSAndroid Build Coastguard Worker			$off = 0;
4950*49cdfc7eSAndroid Build Coastguard Worker
4951*49cdfc7eSAndroid Build Coastguard Worker			my $blank = copy_spacing($opline);
4952*49cdfc7eSAndroid Build Coastguard Worker			my $last_after = -1;
4953*49cdfc7eSAndroid Build Coastguard Worker
4954*49cdfc7eSAndroid Build Coastguard Worker			for (my $n = 0; $n < $#elements; $n += 2) {
4955*49cdfc7eSAndroid Build Coastguard Worker
4956*49cdfc7eSAndroid Build Coastguard Worker				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4957*49cdfc7eSAndroid Build Coastguard Worker
4958*49cdfc7eSAndroid Build Coastguard Worker##				print("n: <$n> good: <$good>\n");
4959*49cdfc7eSAndroid Build Coastguard Worker
4960*49cdfc7eSAndroid Build Coastguard Worker				$off += length($elements[$n]);
4961*49cdfc7eSAndroid Build Coastguard Worker
4962*49cdfc7eSAndroid Build Coastguard Worker				# Pick up the preceding and succeeding characters.
4963*49cdfc7eSAndroid Build Coastguard Worker				my $ca = substr($opline, 0, $off);
4964*49cdfc7eSAndroid Build Coastguard Worker				my $cc = '';
4965*49cdfc7eSAndroid Build Coastguard Worker				if (length($opline) >= ($off + length($elements[$n + 1]))) {
4966*49cdfc7eSAndroid Build Coastguard Worker					$cc = substr($opline, $off + length($elements[$n + 1]));
4967*49cdfc7eSAndroid Build Coastguard Worker				}
4968*49cdfc7eSAndroid Build Coastguard Worker				my $cb = "$ca$;$cc";
4969*49cdfc7eSAndroid Build Coastguard Worker
4970*49cdfc7eSAndroid Build Coastguard Worker				my $a = '';
4971*49cdfc7eSAndroid Build Coastguard Worker				$a = 'V' if ($elements[$n] ne '');
4972*49cdfc7eSAndroid Build Coastguard Worker				$a = 'W' if ($elements[$n] =~ /\s$/);
4973*49cdfc7eSAndroid Build Coastguard Worker				$a = 'C' if ($elements[$n] =~ /$;$/);
4974*49cdfc7eSAndroid Build Coastguard Worker				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4975*49cdfc7eSAndroid Build Coastguard Worker				$a = 'O' if ($elements[$n] eq '');
4976*49cdfc7eSAndroid Build Coastguard Worker				$a = 'E' if ($ca =~ /^\s*$/);
4977*49cdfc7eSAndroid Build Coastguard Worker
4978*49cdfc7eSAndroid Build Coastguard Worker				my $op = $elements[$n + 1];
4979*49cdfc7eSAndroid Build Coastguard Worker
4980*49cdfc7eSAndroid Build Coastguard Worker				my $c = '';
4981*49cdfc7eSAndroid Build Coastguard Worker				if (defined $elements[$n + 2]) {
4982*49cdfc7eSAndroid Build Coastguard Worker					$c = 'V' if ($elements[$n + 2] ne '');
4983*49cdfc7eSAndroid Build Coastguard Worker					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
4984*49cdfc7eSAndroid Build Coastguard Worker					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
4985*49cdfc7eSAndroid Build Coastguard Worker					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4986*49cdfc7eSAndroid Build Coastguard Worker					$c = 'O' if ($elements[$n + 2] eq '');
4987*49cdfc7eSAndroid Build Coastguard Worker					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
4988*49cdfc7eSAndroid Build Coastguard Worker				} else {
4989*49cdfc7eSAndroid Build Coastguard Worker					$c = 'E';
4990*49cdfc7eSAndroid Build Coastguard Worker				}
4991*49cdfc7eSAndroid Build Coastguard Worker
4992*49cdfc7eSAndroid Build Coastguard Worker				my $ctx = "${a}x${c}";
4993*49cdfc7eSAndroid Build Coastguard Worker
4994*49cdfc7eSAndroid Build Coastguard Worker				my $at = "(ctx:$ctx)";
4995*49cdfc7eSAndroid Build Coastguard Worker
4996*49cdfc7eSAndroid Build Coastguard Worker				my $ptr = substr($blank, 0, $off) . "^";
4997*49cdfc7eSAndroid Build Coastguard Worker				my $hereptr = "$hereline$ptr\n";
4998*49cdfc7eSAndroid Build Coastguard Worker
4999*49cdfc7eSAndroid Build Coastguard Worker				# Pull out the value of this operator.
5000*49cdfc7eSAndroid Build Coastguard Worker				my $op_type = substr($curr_values, $off + 1, 1);
5001*49cdfc7eSAndroid Build Coastguard Worker
5002*49cdfc7eSAndroid Build Coastguard Worker				# Get the full operator variant.
5003*49cdfc7eSAndroid Build Coastguard Worker				my $opv = $op . substr($curr_vars, $off, 1);
5004*49cdfc7eSAndroid Build Coastguard Worker
5005*49cdfc7eSAndroid Build Coastguard Worker				# Ignore operators passed as parameters.
5006*49cdfc7eSAndroid Build Coastguard Worker				if ($op_type ne 'V' &&
5007*49cdfc7eSAndroid Build Coastguard Worker				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
5008*49cdfc7eSAndroid Build Coastguard Worker
5009*49cdfc7eSAndroid Build Coastguard Worker#				# Ignore comments
5010*49cdfc7eSAndroid Build Coastguard Worker#				} elsif ($op =~ /^$;+$/) {
5011*49cdfc7eSAndroid Build Coastguard Worker
5012*49cdfc7eSAndroid Build Coastguard Worker				# ; should have either the end of line or a space or \ after it
5013*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($op eq ';') {
5014*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx !~ /.x[WEBC]/ &&
5015*49cdfc7eSAndroid Build Coastguard Worker					    $cc !~ /^\\/ && $cc !~ /^;/) {
5016*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5017*49cdfc7eSAndroid Build Coastguard Worker							  "space required after that '$op' $at\n" . $hereptr)) {
5018*49cdfc7eSAndroid Build Coastguard Worker							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
5019*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5020*49cdfc7eSAndroid Build Coastguard Worker						}
5021*49cdfc7eSAndroid Build Coastguard Worker					}
5022*49cdfc7eSAndroid Build Coastguard Worker
5023*49cdfc7eSAndroid Build Coastguard Worker				# // is a comment
5024*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($op eq '//') {
5025*49cdfc7eSAndroid Build Coastguard Worker
5026*49cdfc7eSAndroid Build Coastguard Worker				#   :   when part of a bitfield
5027*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($opv eq ':B') {
5028*49cdfc7eSAndroid Build Coastguard Worker					# skip the bitfield test for now
5029*49cdfc7eSAndroid Build Coastguard Worker
5030*49cdfc7eSAndroid Build Coastguard Worker				# No spaces for:
5031*49cdfc7eSAndroid Build Coastguard Worker				#   ->
5032*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($op eq '->') {
5033*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx =~ /Wx.|.xW/) {
5034*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5035*49cdfc7eSAndroid Build Coastguard Worker							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
5036*49cdfc7eSAndroid Build Coastguard Worker							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5037*49cdfc7eSAndroid Build Coastguard Worker							if (defined $fix_elements[$n + 2]) {
5038*49cdfc7eSAndroid Build Coastguard Worker								$fix_elements[$n + 2] =~ s/^\s+//;
5039*49cdfc7eSAndroid Build Coastguard Worker							}
5040*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5041*49cdfc7eSAndroid Build Coastguard Worker						}
5042*49cdfc7eSAndroid Build Coastguard Worker					}
5043*49cdfc7eSAndroid Build Coastguard Worker
5044*49cdfc7eSAndroid Build Coastguard Worker				# , must not have a space before and must have a space on the right.
5045*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($op eq ',') {
5046*49cdfc7eSAndroid Build Coastguard Worker					my $rtrim_before = 0;
5047*49cdfc7eSAndroid Build Coastguard Worker					my $space_after = 0;
5048*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx =~ /Wx./) {
5049*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5050*49cdfc7eSAndroid Build Coastguard Worker							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5051*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5052*49cdfc7eSAndroid Build Coastguard Worker							$rtrim_before = 1;
5053*49cdfc7eSAndroid Build Coastguard Worker						}
5054*49cdfc7eSAndroid Build Coastguard Worker					}
5055*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
5056*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5057*49cdfc7eSAndroid Build Coastguard Worker							  "space required after that '$op' $at\n" . $hereptr)) {
5058*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5059*49cdfc7eSAndroid Build Coastguard Worker							$last_after = $n;
5060*49cdfc7eSAndroid Build Coastguard Worker							$space_after = 1;
5061*49cdfc7eSAndroid Build Coastguard Worker						}
5062*49cdfc7eSAndroid Build Coastguard Worker					}
5063*49cdfc7eSAndroid Build Coastguard Worker					if ($rtrim_before || $space_after) {
5064*49cdfc7eSAndroid Build Coastguard Worker						if ($rtrim_before) {
5065*49cdfc7eSAndroid Build Coastguard Worker							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5066*49cdfc7eSAndroid Build Coastguard Worker						} else {
5067*49cdfc7eSAndroid Build Coastguard Worker							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
5068*49cdfc7eSAndroid Build Coastguard Worker						}
5069*49cdfc7eSAndroid Build Coastguard Worker						if ($space_after) {
5070*49cdfc7eSAndroid Build Coastguard Worker							$good .= " ";
5071*49cdfc7eSAndroid Build Coastguard Worker						}
5072*49cdfc7eSAndroid Build Coastguard Worker					}
5073*49cdfc7eSAndroid Build Coastguard Worker
5074*49cdfc7eSAndroid Build Coastguard Worker				# '*' as part of a type definition -- reported already.
5075*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($opv eq '*_') {
5076*49cdfc7eSAndroid Build Coastguard Worker					#warn "'*' is part of type\n";
5077*49cdfc7eSAndroid Build Coastguard Worker
5078*49cdfc7eSAndroid Build Coastguard Worker				# unary operators should have a space before and
5079*49cdfc7eSAndroid Build Coastguard Worker				# none after.  May be left adjacent to another
5080*49cdfc7eSAndroid Build Coastguard Worker				# unary operator, or a cast
5081*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($op eq '!' || $op eq '~' ||
5082*49cdfc7eSAndroid Build Coastguard Worker					 $opv eq '*U' || $opv eq '-U' ||
5083*49cdfc7eSAndroid Build Coastguard Worker					 $opv eq '&U' || $opv eq '&&U') {
5084*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
5085*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5086*49cdfc7eSAndroid Build Coastguard Worker							  "space required before that '$op' $at\n" . $hereptr)) {
5087*49cdfc7eSAndroid Build Coastguard Worker							if ($n != $last_after + 2) {
5088*49cdfc7eSAndroid Build Coastguard Worker								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
5089*49cdfc7eSAndroid Build Coastguard Worker								$line_fixed = 1;
5090*49cdfc7eSAndroid Build Coastguard Worker							}
5091*49cdfc7eSAndroid Build Coastguard Worker						}
5092*49cdfc7eSAndroid Build Coastguard Worker					}
5093*49cdfc7eSAndroid Build Coastguard Worker					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
5094*49cdfc7eSAndroid Build Coastguard Worker						# A unary '*' may be const
5095*49cdfc7eSAndroid Build Coastguard Worker
5096*49cdfc7eSAndroid Build Coastguard Worker					} elsif ($ctx =~ /.xW/) {
5097*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5098*49cdfc7eSAndroid Build Coastguard Worker							  "space prohibited after that '$op' $at\n" . $hereptr)) {
5099*49cdfc7eSAndroid Build Coastguard Worker							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
5100*49cdfc7eSAndroid Build Coastguard Worker							if (defined $fix_elements[$n + 2]) {
5101*49cdfc7eSAndroid Build Coastguard Worker								$fix_elements[$n + 2] =~ s/^\s+//;
5102*49cdfc7eSAndroid Build Coastguard Worker							}
5103*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5104*49cdfc7eSAndroid Build Coastguard Worker						}
5105*49cdfc7eSAndroid Build Coastguard Worker					}
5106*49cdfc7eSAndroid Build Coastguard Worker
5107*49cdfc7eSAndroid Build Coastguard Worker				# unary ++ and unary -- are allowed no space on one side.
5108*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($op eq '++' or $op eq '--') {
5109*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
5110*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5111*49cdfc7eSAndroid Build Coastguard Worker							  "space required one side of that '$op' $at\n" . $hereptr)) {
5112*49cdfc7eSAndroid Build Coastguard Worker							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
5113*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5114*49cdfc7eSAndroid Build Coastguard Worker						}
5115*49cdfc7eSAndroid Build Coastguard Worker					}
5116*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx =~ /Wx[BE]/ ||
5117*49cdfc7eSAndroid Build Coastguard Worker					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
5118*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5119*49cdfc7eSAndroid Build Coastguard Worker							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5120*49cdfc7eSAndroid Build Coastguard Worker							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5121*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5122*49cdfc7eSAndroid Build Coastguard Worker						}
5123*49cdfc7eSAndroid Build Coastguard Worker					}
5124*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx =~ /ExW/) {
5125*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5126*49cdfc7eSAndroid Build Coastguard Worker							  "space prohibited after that '$op' $at\n" . $hereptr)) {
5127*49cdfc7eSAndroid Build Coastguard Worker							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
5128*49cdfc7eSAndroid Build Coastguard Worker							if (defined $fix_elements[$n + 2]) {
5129*49cdfc7eSAndroid Build Coastguard Worker								$fix_elements[$n + 2] =~ s/^\s+//;
5130*49cdfc7eSAndroid Build Coastguard Worker							}
5131*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5132*49cdfc7eSAndroid Build Coastguard Worker						}
5133*49cdfc7eSAndroid Build Coastguard Worker					}
5134*49cdfc7eSAndroid Build Coastguard Worker
5135*49cdfc7eSAndroid Build Coastguard Worker				# << and >> may either have or not have spaces both sides
5136*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($op eq '<<' or $op eq '>>' or
5137*49cdfc7eSAndroid Build Coastguard Worker					 $op eq '&' or $op eq '^' or $op eq '|' or
5138*49cdfc7eSAndroid Build Coastguard Worker					 $op eq '+' or $op eq '-' or
5139*49cdfc7eSAndroid Build Coastguard Worker					 $op eq '*' or $op eq '/' or
5140*49cdfc7eSAndroid Build Coastguard Worker					 $op eq '%')
5141*49cdfc7eSAndroid Build Coastguard Worker				{
5142*49cdfc7eSAndroid Build Coastguard Worker					if ($check) {
5143*49cdfc7eSAndroid Build Coastguard Worker						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5144*49cdfc7eSAndroid Build Coastguard Worker							if (CHK("SPACING",
5145*49cdfc7eSAndroid Build Coastguard Worker								"spaces preferred around that '$op' $at\n" . $hereptr)) {
5146*49cdfc7eSAndroid Build Coastguard Worker								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5147*49cdfc7eSAndroid Build Coastguard Worker								$fix_elements[$n + 2] =~ s/^\s+//;
5148*49cdfc7eSAndroid Build Coastguard Worker								$line_fixed = 1;
5149*49cdfc7eSAndroid Build Coastguard Worker							}
5150*49cdfc7eSAndroid Build Coastguard Worker						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5151*49cdfc7eSAndroid Build Coastguard Worker							if (CHK("SPACING",
5152*49cdfc7eSAndroid Build Coastguard Worker								"space preferred before that '$op' $at\n" . $hereptr)) {
5153*49cdfc7eSAndroid Build Coastguard Worker								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5154*49cdfc7eSAndroid Build Coastguard Worker								$line_fixed = 1;
5155*49cdfc7eSAndroid Build Coastguard Worker							}
5156*49cdfc7eSAndroid Build Coastguard Worker						}
5157*49cdfc7eSAndroid Build Coastguard Worker					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
5158*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5159*49cdfc7eSAndroid Build Coastguard Worker							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
5160*49cdfc7eSAndroid Build Coastguard Worker							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5161*49cdfc7eSAndroid Build Coastguard Worker							if (defined $fix_elements[$n + 2]) {
5162*49cdfc7eSAndroid Build Coastguard Worker								$fix_elements[$n + 2] =~ s/^\s+//;
5163*49cdfc7eSAndroid Build Coastguard Worker							}
5164*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5165*49cdfc7eSAndroid Build Coastguard Worker						}
5166*49cdfc7eSAndroid Build Coastguard Worker					}
5167*49cdfc7eSAndroid Build Coastguard Worker
5168*49cdfc7eSAndroid Build Coastguard Worker				# A colon needs no spaces before when it is
5169*49cdfc7eSAndroid Build Coastguard Worker				# terminating a case value or a label.
5170*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($opv eq ':C' || $opv eq ':L') {
5171*49cdfc7eSAndroid Build Coastguard Worker					if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
5172*49cdfc7eSAndroid Build Coastguard Worker						if (ERROR("SPACING",
5173*49cdfc7eSAndroid Build Coastguard Worker							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5174*49cdfc7eSAndroid Build Coastguard Worker							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
5175*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5176*49cdfc7eSAndroid Build Coastguard Worker						}
5177*49cdfc7eSAndroid Build Coastguard Worker					}
5178*49cdfc7eSAndroid Build Coastguard Worker
5179*49cdfc7eSAndroid Build Coastguard Worker				# All the others need spaces both sides.
5180*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($ctx !~ /[EWC]x[CWE]/) {
5181*49cdfc7eSAndroid Build Coastguard Worker					my $ok = 0;
5182*49cdfc7eSAndroid Build Coastguard Worker
5183*49cdfc7eSAndroid Build Coastguard Worker					# Ignore email addresses <foo@bar>
5184*49cdfc7eSAndroid Build Coastguard Worker					if (($op eq '<' &&
5185*49cdfc7eSAndroid Build Coastguard Worker					     $cc =~ /^\S+\@\S+>/) ||
5186*49cdfc7eSAndroid Build Coastguard Worker					    ($op eq '>' &&
5187*49cdfc7eSAndroid Build Coastguard Worker					     $ca =~ /<\S+\@\S+$/))
5188*49cdfc7eSAndroid Build Coastguard Worker					{
5189*49cdfc7eSAndroid Build Coastguard Worker						$ok = 1;
5190*49cdfc7eSAndroid Build Coastguard Worker					}
5191*49cdfc7eSAndroid Build Coastguard Worker
5192*49cdfc7eSAndroid Build Coastguard Worker					# for asm volatile statements
5193*49cdfc7eSAndroid Build Coastguard Worker					# ignore a colon with another
5194*49cdfc7eSAndroid Build Coastguard Worker					# colon immediately before or after
5195*49cdfc7eSAndroid Build Coastguard Worker					if (($op eq ':') &&
5196*49cdfc7eSAndroid Build Coastguard Worker					    ($ca =~ /:$/ || $cc =~ /^:/)) {
5197*49cdfc7eSAndroid Build Coastguard Worker						$ok = 1;
5198*49cdfc7eSAndroid Build Coastguard Worker					}
5199*49cdfc7eSAndroid Build Coastguard Worker
5200*49cdfc7eSAndroid Build Coastguard Worker					# messages are ERROR, but ?: are CHK
5201*49cdfc7eSAndroid Build Coastguard Worker					if ($ok == 0) {
5202*49cdfc7eSAndroid Build Coastguard Worker						my $msg_level = \&ERROR;
5203*49cdfc7eSAndroid Build Coastguard Worker						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
5204*49cdfc7eSAndroid Build Coastguard Worker
5205*49cdfc7eSAndroid Build Coastguard Worker						if (&{$msg_level}("SPACING",
5206*49cdfc7eSAndroid Build Coastguard Worker								  "spaces required around that '$op' $at\n" . $hereptr)) {
5207*49cdfc7eSAndroid Build Coastguard Worker							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5208*49cdfc7eSAndroid Build Coastguard Worker							if (defined $fix_elements[$n + 2]) {
5209*49cdfc7eSAndroid Build Coastguard Worker								$fix_elements[$n + 2] =~ s/^\s+//;
5210*49cdfc7eSAndroid Build Coastguard Worker							}
5211*49cdfc7eSAndroid Build Coastguard Worker							$line_fixed = 1;
5212*49cdfc7eSAndroid Build Coastguard Worker						}
5213*49cdfc7eSAndroid Build Coastguard Worker					}
5214*49cdfc7eSAndroid Build Coastguard Worker				}
5215*49cdfc7eSAndroid Build Coastguard Worker				$off += length($elements[$n + 1]);
5216*49cdfc7eSAndroid Build Coastguard Worker
5217*49cdfc7eSAndroid Build Coastguard Worker##				print("n: <$n> GOOD: <$good>\n");
5218*49cdfc7eSAndroid Build Coastguard Worker
5219*49cdfc7eSAndroid Build Coastguard Worker				$fixed_line = $fixed_line . $good;
5220*49cdfc7eSAndroid Build Coastguard Worker			}
5221*49cdfc7eSAndroid Build Coastguard Worker
5222*49cdfc7eSAndroid Build Coastguard Worker			if (($#elements % 2) == 0) {
5223*49cdfc7eSAndroid Build Coastguard Worker				$fixed_line = $fixed_line . $fix_elements[$#elements];
5224*49cdfc7eSAndroid Build Coastguard Worker			}
5225*49cdfc7eSAndroid Build Coastguard Worker
5226*49cdfc7eSAndroid Build Coastguard Worker			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5227*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] = $fixed_line;
5228*49cdfc7eSAndroid Build Coastguard Worker			}
5229*49cdfc7eSAndroid Build Coastguard Worker
5230*49cdfc7eSAndroid Build Coastguard Worker
5231*49cdfc7eSAndroid Build Coastguard Worker		}
5232*49cdfc7eSAndroid Build Coastguard Worker
5233*49cdfc7eSAndroid Build Coastguard Worker# check for whitespace before a non-naked semicolon
5234*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+.*\S\s+;\s*$/) {
5235*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("SPACING",
5236*49cdfc7eSAndroid Build Coastguard Worker				 "space prohibited before semicolon\n" . $herecurr) &&
5237*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5238*49cdfc7eSAndroid Build Coastguard Worker				1 while $fixed[$fixlinenr] =~
5239*49cdfc7eSAndroid Build Coastguard Worker				    s/^(\+.*\S)\s+;/$1;/;
5240*49cdfc7eSAndroid Build Coastguard Worker			}
5241*49cdfc7eSAndroid Build Coastguard Worker		}
5242*49cdfc7eSAndroid Build Coastguard Worker
5243*49cdfc7eSAndroid Build Coastguard Worker# check for multiple assignments
5244*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5245*49cdfc7eSAndroid Build Coastguard Worker			CHK("MULTIPLE_ASSIGNMENTS",
5246*49cdfc7eSAndroid Build Coastguard Worker			    "multiple assignments should be avoided\n" . $herecurr);
5247*49cdfc7eSAndroid Build Coastguard Worker		}
5248*49cdfc7eSAndroid Build Coastguard Worker
5249*49cdfc7eSAndroid Build Coastguard Worker## # check for multiple declarations, allowing for a function declaration
5250*49cdfc7eSAndroid Build Coastguard Worker## # continuation.
5251*49cdfc7eSAndroid Build Coastguard Worker## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
5252*49cdfc7eSAndroid Build Coastguard Worker## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
5253*49cdfc7eSAndroid Build Coastguard Worker##
5254*49cdfc7eSAndroid Build Coastguard Worker## 			# Remove any bracketed sections to ensure we do not
5255*49cdfc7eSAndroid Build Coastguard Worker## 			# falsely report the parameters of functions.
5256*49cdfc7eSAndroid Build Coastguard Worker## 			my $ln = $line;
5257*49cdfc7eSAndroid Build Coastguard Worker## 			while ($ln =~ s/\([^\(\)]*\)//g) {
5258*49cdfc7eSAndroid Build Coastguard Worker## 			}
5259*49cdfc7eSAndroid Build Coastguard Worker## 			if ($ln =~ /,/) {
5260*49cdfc7eSAndroid Build Coastguard Worker## 				WARN("MULTIPLE_DECLARATION",
5261*49cdfc7eSAndroid Build Coastguard Worker##				     "declaring multiple variables together should be avoided\n" . $herecurr);
5262*49cdfc7eSAndroid Build Coastguard Worker## 			}
5263*49cdfc7eSAndroid Build Coastguard Worker## 		}
5264*49cdfc7eSAndroid Build Coastguard Worker
5265*49cdfc7eSAndroid Build Coastguard Worker#need space before brace following if, while, etc
5266*49cdfc7eSAndroid Build Coastguard Worker		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
5267*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b(?:else|do)\{/) {
5268*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("SPACING",
5269*49cdfc7eSAndroid Build Coastguard Worker				  "space required before the open brace '{'\n" . $herecurr) &&
5270*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5271*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
5272*49cdfc7eSAndroid Build Coastguard Worker			}
5273*49cdfc7eSAndroid Build Coastguard Worker		}
5274*49cdfc7eSAndroid Build Coastguard Worker
5275*49cdfc7eSAndroid Build Coastguard Worker## # check for blank lines before declarations
5276*49cdfc7eSAndroid Build Coastguard Worker##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5277*49cdfc7eSAndroid Build Coastguard Worker##		    $prevrawline =~ /^.\s*$/) {
5278*49cdfc7eSAndroid Build Coastguard Worker##			WARN("SPACING",
5279*49cdfc7eSAndroid Build Coastguard Worker##			     "No blank lines before declarations\n" . $hereprev);
5280*49cdfc7eSAndroid Build Coastguard Worker##		}
5281*49cdfc7eSAndroid Build Coastguard Worker##
5282*49cdfc7eSAndroid Build Coastguard Worker
5283*49cdfc7eSAndroid Build Coastguard Worker# closing brace should have a space following it when it has anything
5284*49cdfc7eSAndroid Build Coastguard Worker# on the line
5285*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5286*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("SPACING",
5287*49cdfc7eSAndroid Build Coastguard Worker				  "space required after that close brace '}'\n" . $herecurr) &&
5288*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5289*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
5290*49cdfc7eSAndroid Build Coastguard Worker				    s/}((?!(?:,|;|\)))\S)/} $1/;
5291*49cdfc7eSAndroid Build Coastguard Worker			}
5292*49cdfc7eSAndroid Build Coastguard Worker		}
5293*49cdfc7eSAndroid Build Coastguard Worker
5294*49cdfc7eSAndroid Build Coastguard Worker# check spacing on square brackets
5295*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
5296*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("SPACING",
5297*49cdfc7eSAndroid Build Coastguard Worker				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
5298*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5299*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
5300*49cdfc7eSAndroid Build Coastguard Worker				    s/\[\s+/\[/;
5301*49cdfc7eSAndroid Build Coastguard Worker			}
5302*49cdfc7eSAndroid Build Coastguard Worker		}
5303*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\s\]/) {
5304*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("SPACING",
5305*49cdfc7eSAndroid Build Coastguard Worker				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
5306*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5307*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
5308*49cdfc7eSAndroid Build Coastguard Worker				    s/\s+\]/\]/;
5309*49cdfc7eSAndroid Build Coastguard Worker			}
5310*49cdfc7eSAndroid Build Coastguard Worker		}
5311*49cdfc7eSAndroid Build Coastguard Worker
5312*49cdfc7eSAndroid Build Coastguard Worker# check spacing on parentheses
5313*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
5314*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /for\s*\(\s+;/) {
5315*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("SPACING",
5316*49cdfc7eSAndroid Build Coastguard Worker				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
5317*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5318*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
5319*49cdfc7eSAndroid Build Coastguard Worker				    s/\(\s+/\(/;
5320*49cdfc7eSAndroid Build Coastguard Worker			}
5321*49cdfc7eSAndroid Build Coastguard Worker		}
5322*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5323*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /for\s*\(.*;\s+\)/ &&
5324*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /:\s+\)/) {
5325*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("SPACING",
5326*49cdfc7eSAndroid Build Coastguard Worker				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
5327*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5328*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
5329*49cdfc7eSAndroid Build Coastguard Worker				    s/\s+\)/\)/;
5330*49cdfc7eSAndroid Build Coastguard Worker			}
5331*49cdfc7eSAndroid Build Coastguard Worker		}
5332*49cdfc7eSAndroid Build Coastguard Worker
5333*49cdfc7eSAndroid Build Coastguard Worker# check unnecessary parentheses around addressof/dereference single $Lvals
5334*49cdfc7eSAndroid Build Coastguard Worker# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5335*49cdfc7eSAndroid Build Coastguard Worker
5336*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5337*49cdfc7eSAndroid Build Coastguard Worker			my $var = $1;
5338*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("UNNECESSARY_PARENTHESES",
5339*49cdfc7eSAndroid Build Coastguard Worker				"Unnecessary parentheses around $var\n" . $herecurr) &&
5340*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5341*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5342*49cdfc7eSAndroid Build Coastguard Worker			}
5343*49cdfc7eSAndroid Build Coastguard Worker		}
5344*49cdfc7eSAndroid Build Coastguard Worker
5345*49cdfc7eSAndroid Build Coastguard Worker# check for unnecessary parentheses around function pointer uses
5346*49cdfc7eSAndroid Build Coastguard Worker# ie: (foo->bar)(); should be foo->bar();
5347*49cdfc7eSAndroid Build Coastguard Worker# but not "if (foo->bar) (" to avoid some false positives
5348*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5349*49cdfc7eSAndroid Build Coastguard Worker			my $var = $2;
5350*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("UNNECESSARY_PARENTHESES",
5351*49cdfc7eSAndroid Build Coastguard Worker				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5352*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5353*49cdfc7eSAndroid Build Coastguard Worker				my $var2 = deparenthesize($var);
5354*49cdfc7eSAndroid Build Coastguard Worker				$var2 =~ s/\s//g;
5355*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5356*49cdfc7eSAndroid Build Coastguard Worker			}
5357*49cdfc7eSAndroid Build Coastguard Worker		}
5358*49cdfc7eSAndroid Build Coastguard Worker
5359*49cdfc7eSAndroid Build Coastguard Worker# check for unnecessary parentheses around comparisons in if uses
5360*49cdfc7eSAndroid Build Coastguard Worker# when !drivers/staging or command-line uses --strict
5361*49cdfc7eSAndroid Build Coastguard Worker		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
5362*49cdfc7eSAndroid Build Coastguard Worker		    $perl_version_ok && defined($stat) &&
5363*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
5364*49cdfc7eSAndroid Build Coastguard Worker			my $if_stat = $1;
5365*49cdfc7eSAndroid Build Coastguard Worker			my $test = substr($2, 1, -1);
5366*49cdfc7eSAndroid Build Coastguard Worker			my $herectx;
5367*49cdfc7eSAndroid Build Coastguard Worker			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
5368*49cdfc7eSAndroid Build Coastguard Worker				my $match = $1;
5369*49cdfc7eSAndroid Build Coastguard Worker				# avoid parentheses around potential macro args
5370*49cdfc7eSAndroid Build Coastguard Worker				next if ($match =~ /^\s*\w+\s*$/);
5371*49cdfc7eSAndroid Build Coastguard Worker				if (!defined($herectx)) {
5372*49cdfc7eSAndroid Build Coastguard Worker					$herectx = $here . "\n";
5373*49cdfc7eSAndroid Build Coastguard Worker					my $cnt = statement_rawlines($if_stat);
5374*49cdfc7eSAndroid Build Coastguard Worker					for (my $n = 0; $n < $cnt; $n++) {
5375*49cdfc7eSAndroid Build Coastguard Worker						my $rl = raw_line($linenr, $n);
5376*49cdfc7eSAndroid Build Coastguard Worker						$herectx .=  $rl . "\n";
5377*49cdfc7eSAndroid Build Coastguard Worker						last if $rl =~ /^[ \+].*\{/;
5378*49cdfc7eSAndroid Build Coastguard Worker					}
5379*49cdfc7eSAndroid Build Coastguard Worker				}
5380*49cdfc7eSAndroid Build Coastguard Worker				CHK("UNNECESSARY_PARENTHESES",
5381*49cdfc7eSAndroid Build Coastguard Worker				    "Unnecessary parentheses around '$match'\n" . $herectx);
5382*49cdfc7eSAndroid Build Coastguard Worker			}
5383*49cdfc7eSAndroid Build Coastguard Worker		}
5384*49cdfc7eSAndroid Build Coastguard Worker
5385*49cdfc7eSAndroid Build Coastguard Worker# check that goto labels aren't indented (allow a single space indentation)
5386*49cdfc7eSAndroid Build Coastguard Worker# and ignore bitfield definitions like foo:1
5387*49cdfc7eSAndroid Build Coastguard Worker# Strictly, labels can have whitespace after the identifier and before the :
5388*49cdfc7eSAndroid Build Coastguard Worker# but this is not allowed here as many ?: uses would appear to be labels
5389*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
5390*49cdfc7eSAndroid Build Coastguard Worker		    $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
5391*49cdfc7eSAndroid Build Coastguard Worker		    $sline !~ /^.\s+default:/) {
5392*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("INDENTED_LABEL",
5393*49cdfc7eSAndroid Build Coastguard Worker				 "labels should not be indented\n" . $herecurr) &&
5394*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5395*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
5396*49cdfc7eSAndroid Build Coastguard Worker				    s/^(.)\s+/$1/;
5397*49cdfc7eSAndroid Build Coastguard Worker			}
5398*49cdfc7eSAndroid Build Coastguard Worker		}
5399*49cdfc7eSAndroid Build Coastguard Worker
5400*49cdfc7eSAndroid Build Coastguard Worker# check if a statement with a comma should be two statements like:
5401*49cdfc7eSAndroid Build Coastguard Worker#	foo = bar(),	/* comma should be semicolon */
5402*49cdfc7eSAndroid Build Coastguard Worker#	bar = baz();
5403*49cdfc7eSAndroid Build Coastguard Worker		if (defined($stat) &&
5404*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
5405*49cdfc7eSAndroid Build Coastguard Worker			my $cnt = statement_rawlines($stat);
5406*49cdfc7eSAndroid Build Coastguard Worker			my $herectx = get_stat_here($linenr, $cnt, $here);
5407*49cdfc7eSAndroid Build Coastguard Worker			WARN("SUSPECT_COMMA_SEMICOLON",
5408*49cdfc7eSAndroid Build Coastguard Worker			     "Possible comma where semicolon could be used\n" . $herectx);
5409*49cdfc7eSAndroid Build Coastguard Worker		}
5410*49cdfc7eSAndroid Build Coastguard Worker
5411*49cdfc7eSAndroid Build Coastguard Worker# return is not a function
5412*49cdfc7eSAndroid Build Coastguard Worker		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5413*49cdfc7eSAndroid Build Coastguard Worker			my $spacing = $1;
5414*49cdfc7eSAndroid Build Coastguard Worker			if ($perl_version_ok &&
5415*49cdfc7eSAndroid Build Coastguard Worker			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
5416*49cdfc7eSAndroid Build Coastguard Worker				my $value = $1;
5417*49cdfc7eSAndroid Build Coastguard Worker				$value = deparenthesize($value);
5418*49cdfc7eSAndroid Build Coastguard Worker				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5419*49cdfc7eSAndroid Build Coastguard Worker					ERROR("RETURN_PARENTHESES",
5420*49cdfc7eSAndroid Build Coastguard Worker					      "return is not a function, parentheses are not required\n" . $herecurr);
5421*49cdfc7eSAndroid Build Coastguard Worker				}
5422*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($spacing !~ /\s+/) {
5423*49cdfc7eSAndroid Build Coastguard Worker				ERROR("SPACING",
5424*49cdfc7eSAndroid Build Coastguard Worker				      "space required before the open parenthesis '('\n" . $herecurr);
5425*49cdfc7eSAndroid Build Coastguard Worker			}
5426*49cdfc7eSAndroid Build Coastguard Worker		}
5427*49cdfc7eSAndroid Build Coastguard Worker
5428*49cdfc7eSAndroid Build Coastguard Worker# unnecessary return in a void function
5429*49cdfc7eSAndroid Build Coastguard Worker# at end-of-function, with the previous line a single leading tab, then return;
5430*49cdfc7eSAndroid Build Coastguard Worker# and the line before that not a goto label target like "out:"
5431*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^[ \+]}\s*$/ &&
5432*49cdfc7eSAndroid Build Coastguard Worker		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5433*49cdfc7eSAndroid Build Coastguard Worker		    $linenr >= 3 &&
5434*49cdfc7eSAndroid Build Coastguard Worker		    $lines[$linenr - 3] =~ /^[ +]/ &&
5435*49cdfc7eSAndroid Build Coastguard Worker		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
5436*49cdfc7eSAndroid Build Coastguard Worker			WARN("RETURN_VOID",
5437*49cdfc7eSAndroid Build Coastguard Worker			     "void function return statements are not generally useful\n" . $hereprev);
5438*49cdfc7eSAndroid Build Coastguard Worker		}
5439*49cdfc7eSAndroid Build Coastguard Worker
5440*49cdfc7eSAndroid Build Coastguard Worker# if statements using unnecessary parentheses - ie: if ((foo == bar))
5441*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
5442*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5443*49cdfc7eSAndroid Build Coastguard Worker			my $openparens = $1;
5444*49cdfc7eSAndroid Build Coastguard Worker			my $count = $openparens =~ tr@\(@\(@;
5445*49cdfc7eSAndroid Build Coastguard Worker			my $msg = "";
5446*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5447*49cdfc7eSAndroid Build Coastguard Worker				my $comp = $4;	#Not $1 because of $LvalOrFunc
5448*49cdfc7eSAndroid Build Coastguard Worker				$msg = " - maybe == should be = ?" if ($comp eq "==");
5449*49cdfc7eSAndroid Build Coastguard Worker				WARN("UNNECESSARY_PARENTHESES",
5450*49cdfc7eSAndroid Build Coastguard Worker				     "Unnecessary parentheses$msg\n" . $herecurr);
5451*49cdfc7eSAndroid Build Coastguard Worker			}
5452*49cdfc7eSAndroid Build Coastguard Worker		}
5453*49cdfc7eSAndroid Build Coastguard Worker
5454*49cdfc7eSAndroid Build Coastguard Worker# comparisons with a constant or upper case identifier on the left
5455*49cdfc7eSAndroid Build Coastguard Worker#	avoid cases like "foo + BAR < baz"
5456*49cdfc7eSAndroid Build Coastguard Worker#	only fix matches surrounded by parentheses to avoid incorrect
5457*49cdfc7eSAndroid Build Coastguard Worker#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
5458*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
5459*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5460*49cdfc7eSAndroid Build Coastguard Worker			my $lead = $1;
5461*49cdfc7eSAndroid Build Coastguard Worker			my $const = $2;
5462*49cdfc7eSAndroid Build Coastguard Worker			my $comp = $3;
5463*49cdfc7eSAndroid Build Coastguard Worker			my $to = $4;
5464*49cdfc7eSAndroid Build Coastguard Worker			my $newcomp = $comp;
5465*49cdfc7eSAndroid Build Coastguard Worker
5466*49cdfc7eSAndroid Build Coastguard Worker			if ($const !~ /^\QTST_/ &&
5467*49cdfc7eSAndroid Build Coastguard Worker			    $lead !~ /(?:$Operators|\.)\s*$/ &&
5468*49cdfc7eSAndroid Build Coastguard Worker			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5469*49cdfc7eSAndroid Build Coastguard Worker			    WARN("CONSTANT_COMPARISON",
5470*49cdfc7eSAndroid Build Coastguard Worker				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5471*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5472*49cdfc7eSAndroid Build Coastguard Worker				if ($comp eq "<") {
5473*49cdfc7eSAndroid Build Coastguard Worker					$newcomp = ">";
5474*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($comp eq "<=") {
5475*49cdfc7eSAndroid Build Coastguard Worker					$newcomp = ">=";
5476*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($comp eq ">") {
5477*49cdfc7eSAndroid Build Coastguard Worker					$newcomp = "<";
5478*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($comp eq ">=") {
5479*49cdfc7eSAndroid Build Coastguard Worker					$newcomp = "<=";
5480*49cdfc7eSAndroid Build Coastguard Worker				}
5481*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5482*49cdfc7eSAndroid Build Coastguard Worker			}
5483*49cdfc7eSAndroid Build Coastguard Worker		}
5484*49cdfc7eSAndroid Build Coastguard Worker
5485*49cdfc7eSAndroid Build Coastguard Worker# Return of what appears to be an errno should normally be negative
5486*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
5487*49cdfc7eSAndroid Build Coastguard Worker			my $name = $1;
5488*49cdfc7eSAndroid Build Coastguard Worker			if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
5489*49cdfc7eSAndroid Build Coastguard Worker				WARN("USE_NEGATIVE_ERRNO",
5490*49cdfc7eSAndroid Build Coastguard Worker				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
5491*49cdfc7eSAndroid Build Coastguard Worker			}
5492*49cdfc7eSAndroid Build Coastguard Worker		}
5493*49cdfc7eSAndroid Build Coastguard Worker
5494*49cdfc7eSAndroid Build Coastguard Worker# Need a space before open parenthesis after if, while etc
5495*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b(if|while|for|switch)\(/) {
5496*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("SPACING",
5497*49cdfc7eSAndroid Build Coastguard Worker				  "space required before the open parenthesis '('\n" . $herecurr) &&
5498*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5499*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
5500*49cdfc7eSAndroid Build Coastguard Worker				    s/\b(if|while|for|switch)\(/$1 \(/;
5501*49cdfc7eSAndroid Build Coastguard Worker			}
5502*49cdfc7eSAndroid Build Coastguard Worker		}
5503*49cdfc7eSAndroid Build Coastguard Worker
5504*49cdfc7eSAndroid Build Coastguard Worker# Check for illegal assignment in if conditional -- and check for trailing
5505*49cdfc7eSAndroid Build Coastguard Worker# statements after the conditional.
5506*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /do\s*(?!{)/) {
5507*49cdfc7eSAndroid Build Coastguard Worker			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
5508*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_block($linenr, $realcnt, 0)
5509*49cdfc7eSAndroid Build Coastguard Worker					if (!defined $stat);
5510*49cdfc7eSAndroid Build Coastguard Worker			my ($stat_next) = ctx_statement_block($line_nr_next,
5511*49cdfc7eSAndroid Build Coastguard Worker						$remain_next, $off_next);
5512*49cdfc7eSAndroid Build Coastguard Worker			$stat_next =~ s/\n./\n /g;
5513*49cdfc7eSAndroid Build Coastguard Worker			##print "stat<$stat> stat_next<$stat_next>\n";
5514*49cdfc7eSAndroid Build Coastguard Worker
5515*49cdfc7eSAndroid Build Coastguard Worker			if ($stat_next =~ /^\s*while\b/) {
5516*49cdfc7eSAndroid Build Coastguard Worker				# If the statement carries leading newlines,
5517*49cdfc7eSAndroid Build Coastguard Worker				# then count those as offsets.
5518*49cdfc7eSAndroid Build Coastguard Worker				my ($whitespace) =
5519*49cdfc7eSAndroid Build Coastguard Worker					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5520*49cdfc7eSAndroid Build Coastguard Worker				my $offset =
5521*49cdfc7eSAndroid Build Coastguard Worker					statement_rawlines($whitespace) - 1;
5522*49cdfc7eSAndroid Build Coastguard Worker
5523*49cdfc7eSAndroid Build Coastguard Worker				$suppress_whiletrailers{$line_nr_next +
5524*49cdfc7eSAndroid Build Coastguard Worker								$offset} = 1;
5525*49cdfc7eSAndroid Build Coastguard Worker			}
5526*49cdfc7eSAndroid Build Coastguard Worker		}
5527*49cdfc7eSAndroid Build Coastguard Worker		if (!defined $suppress_whiletrailers{$linenr} &&
5528*49cdfc7eSAndroid Build Coastguard Worker		    defined($stat) && defined($cond) &&
5529*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5530*49cdfc7eSAndroid Build Coastguard Worker			my ($s, $c) = ($stat, $cond);
5531*49cdfc7eSAndroid Build Coastguard Worker
5532*49cdfc7eSAndroid Build Coastguard Worker			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
5533*49cdfc7eSAndroid Build Coastguard Worker				if (ERROR("ASSIGN_IN_IF",
5534*49cdfc7eSAndroid Build Coastguard Worker					  "do not use assignment in if condition\n" . $herecurr) &&
5535*49cdfc7eSAndroid Build Coastguard Worker				    $fix && $perl_version_ok) {
5536*49cdfc7eSAndroid Build Coastguard Worker					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
5537*49cdfc7eSAndroid Build Coastguard Worker						my $space = $1;
5538*49cdfc7eSAndroid Build Coastguard Worker						my $not = $2;
5539*49cdfc7eSAndroid Build Coastguard Worker						my $statement = $3;
5540*49cdfc7eSAndroid Build Coastguard Worker						my $assigned = $4;
5541*49cdfc7eSAndroid Build Coastguard Worker						my $test = $8;
5542*49cdfc7eSAndroid Build Coastguard Worker						my $against = $9;
5543*49cdfc7eSAndroid Build Coastguard Worker						my $brace = $15;
5544*49cdfc7eSAndroid Build Coastguard Worker						fix_delete_line($fixlinenr, $rawline);
5545*49cdfc7eSAndroid Build Coastguard Worker						fix_insert_line($fixlinenr, "$space$statement;");
5546*49cdfc7eSAndroid Build Coastguard Worker						my $newline = "${space}if (";
5547*49cdfc7eSAndroid Build Coastguard Worker						$newline .= '!' if defined($not);
5548*49cdfc7eSAndroid Build Coastguard Worker						$newline .= '(' if (defined $not && defined($test) && defined($against));
5549*49cdfc7eSAndroid Build Coastguard Worker						$newline .= "$assigned";
5550*49cdfc7eSAndroid Build Coastguard Worker						$newline .= " $test $against" if (defined($test) && defined($against));
5551*49cdfc7eSAndroid Build Coastguard Worker						$newline .= ')' if (defined $not && defined($test) && defined($against));
5552*49cdfc7eSAndroid Build Coastguard Worker						$newline .= ')';
5553*49cdfc7eSAndroid Build Coastguard Worker						$newline .= " {" if (defined($brace));
5554*49cdfc7eSAndroid Build Coastguard Worker						fix_insert_line($fixlinenr + 1, $newline);
5555*49cdfc7eSAndroid Build Coastguard Worker					}
5556*49cdfc7eSAndroid Build Coastguard Worker				}
5557*49cdfc7eSAndroid Build Coastguard Worker			}
5558*49cdfc7eSAndroid Build Coastguard Worker
5559*49cdfc7eSAndroid Build Coastguard Worker			# Find out what is on the end of the line after the
5560*49cdfc7eSAndroid Build Coastguard Worker			# conditional.
5561*49cdfc7eSAndroid Build Coastguard Worker			substr($s, 0, length($c), '');
5562*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/\n.*//g;
5563*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/$;//g;	# Remove any comments
5564*49cdfc7eSAndroid Build Coastguard Worker			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
5565*49cdfc7eSAndroid Build Coastguard Worker			    $c !~ /}\s*while\s*/)
5566*49cdfc7eSAndroid Build Coastguard Worker			{
5567*49cdfc7eSAndroid Build Coastguard Worker				# Find out how long the conditional actually is.
5568*49cdfc7eSAndroid Build Coastguard Worker				my @newlines = ($c =~ /\n/gs);
5569*49cdfc7eSAndroid Build Coastguard Worker				my $cond_lines = 1 + $#newlines;
5570*49cdfc7eSAndroid Build Coastguard Worker				my $stat_real = '';
5571*49cdfc7eSAndroid Build Coastguard Worker
5572*49cdfc7eSAndroid Build Coastguard Worker				$stat_real = raw_line($linenr, $cond_lines)
5573*49cdfc7eSAndroid Build Coastguard Worker							. "\n" if ($cond_lines);
5574*49cdfc7eSAndroid Build Coastguard Worker				if (defined($stat_real) && $cond_lines > 1) {
5575*49cdfc7eSAndroid Build Coastguard Worker					$stat_real = "[...]\n$stat_real";
5576*49cdfc7eSAndroid Build Coastguard Worker				}
5577*49cdfc7eSAndroid Build Coastguard Worker
5578*49cdfc7eSAndroid Build Coastguard Worker				ERROR("TRAILING_STATEMENTS",
5579*49cdfc7eSAndroid Build Coastguard Worker				      "trailing statements should be on next line\n" . $herecurr . $stat_real);
5580*49cdfc7eSAndroid Build Coastguard Worker			}
5581*49cdfc7eSAndroid Build Coastguard Worker		}
5582*49cdfc7eSAndroid Build Coastguard Worker
5583*49cdfc7eSAndroid Build Coastguard Worker# Check for bitwise tests written as boolean
5584*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /
5585*49cdfc7eSAndroid Build Coastguard Worker			(?:
5586*49cdfc7eSAndroid Build Coastguard Worker				(?:\[|\(|\&\&|\|\|)
5587*49cdfc7eSAndroid Build Coastguard Worker				\s*0[xX][0-9]+\s*
5588*49cdfc7eSAndroid Build Coastguard Worker				(?:\&\&|\|\|)
5589*49cdfc7eSAndroid Build Coastguard Worker			|
5590*49cdfc7eSAndroid Build Coastguard Worker				(?:\&\&|\|\|)
5591*49cdfc7eSAndroid Build Coastguard Worker				\s*0[xX][0-9]+\s*
5592*49cdfc7eSAndroid Build Coastguard Worker				(?:\&\&|\|\||\)|\])
5593*49cdfc7eSAndroid Build Coastguard Worker			)/x)
5594*49cdfc7eSAndroid Build Coastguard Worker		{
5595*49cdfc7eSAndroid Build Coastguard Worker			WARN("HEXADECIMAL_BOOLEAN_TEST",
5596*49cdfc7eSAndroid Build Coastguard Worker			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
5597*49cdfc7eSAndroid Build Coastguard Worker		}
5598*49cdfc7eSAndroid Build Coastguard Worker
5599*49cdfc7eSAndroid Build Coastguard Worker# if and else should not have general statements after it
5600*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
5601*49cdfc7eSAndroid Build Coastguard Worker			my $s = $1;
5602*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/$;//g;	# Remove any comments
5603*49cdfc7eSAndroid Build Coastguard Worker			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5604*49cdfc7eSAndroid Build Coastguard Worker				ERROR("TRAILING_STATEMENTS",
5605*49cdfc7eSAndroid Build Coastguard Worker				      "trailing statements should be on next line\n" . $herecurr);
5606*49cdfc7eSAndroid Build Coastguard Worker			}
5607*49cdfc7eSAndroid Build Coastguard Worker		}
5608*49cdfc7eSAndroid Build Coastguard Worker# if should not continue a brace
5609*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /}\s*if\b/) {
5610*49cdfc7eSAndroid Build Coastguard Worker			ERROR("TRAILING_STATEMENTS",
5611*49cdfc7eSAndroid Build Coastguard Worker			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
5612*49cdfc7eSAndroid Build Coastguard Worker				$herecurr);
5613*49cdfc7eSAndroid Build Coastguard Worker		}
5614*49cdfc7eSAndroid Build Coastguard Worker# case and default should not have general statements after them
5615*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5616*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /\G(?:
5617*49cdfc7eSAndroid Build Coastguard Worker			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5618*49cdfc7eSAndroid Build Coastguard Worker			\s*return\s+
5619*49cdfc7eSAndroid Build Coastguard Worker		    )/xg)
5620*49cdfc7eSAndroid Build Coastguard Worker		{
5621*49cdfc7eSAndroid Build Coastguard Worker			ERROR("TRAILING_STATEMENTS",
5622*49cdfc7eSAndroid Build Coastguard Worker			      "trailing statements should be on next line\n" . $herecurr);
5623*49cdfc7eSAndroid Build Coastguard Worker		}
5624*49cdfc7eSAndroid Build Coastguard Worker
5625*49cdfc7eSAndroid Build Coastguard Worker		# Check for }<nl>else {, these must be at the same
5626*49cdfc7eSAndroid Build Coastguard Worker		# indent level to be relevant to each other.
5627*49cdfc7eSAndroid Build Coastguard Worker		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
5628*49cdfc7eSAndroid Build Coastguard Worker		    $previndent == $indent) {
5629*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("ELSE_AFTER_BRACE",
5630*49cdfc7eSAndroid Build Coastguard Worker				  "else should follow close brace '}'\n" . $hereprev) &&
5631*49cdfc7eSAndroid Build Coastguard Worker			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5632*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr - 1, $prevrawline);
5633*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr, $rawline);
5634*49cdfc7eSAndroid Build Coastguard Worker				my $fixedline = $prevrawline;
5635*49cdfc7eSAndroid Build Coastguard Worker				$fixedline =~ s/}\s*$//;
5636*49cdfc7eSAndroid Build Coastguard Worker				if ($fixedline !~ /^\+\s*$/) {
5637*49cdfc7eSAndroid Build Coastguard Worker					fix_insert_line($fixlinenr, $fixedline);
5638*49cdfc7eSAndroid Build Coastguard Worker				}
5639*49cdfc7eSAndroid Build Coastguard Worker				$fixedline = $rawline;
5640*49cdfc7eSAndroid Build Coastguard Worker				$fixedline =~ s/^(.\s*)else/$1} else/;
5641*49cdfc7eSAndroid Build Coastguard Worker				fix_insert_line($fixlinenr, $fixedline);
5642*49cdfc7eSAndroid Build Coastguard Worker			}
5643*49cdfc7eSAndroid Build Coastguard Worker		}
5644*49cdfc7eSAndroid Build Coastguard Worker
5645*49cdfc7eSAndroid Build Coastguard Worker		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5646*49cdfc7eSAndroid Build Coastguard Worker		    $previndent == $indent) {
5647*49cdfc7eSAndroid Build Coastguard Worker			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5648*49cdfc7eSAndroid Build Coastguard Worker
5649*49cdfc7eSAndroid Build Coastguard Worker			# Find out what is on the end of the line after the
5650*49cdfc7eSAndroid Build Coastguard Worker			# conditional.
5651*49cdfc7eSAndroid Build Coastguard Worker			substr($s, 0, length($c), '');
5652*49cdfc7eSAndroid Build Coastguard Worker			$s =~ s/\n.*//g;
5653*49cdfc7eSAndroid Build Coastguard Worker
5654*49cdfc7eSAndroid Build Coastguard Worker			if ($s =~ /^\s*;/) {
5655*49cdfc7eSAndroid Build Coastguard Worker				if (ERROR("WHILE_AFTER_BRACE",
5656*49cdfc7eSAndroid Build Coastguard Worker					  "while should follow close brace '}'\n" . $hereprev) &&
5657*49cdfc7eSAndroid Build Coastguard Worker				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
5658*49cdfc7eSAndroid Build Coastguard Worker					fix_delete_line($fixlinenr - 1, $prevrawline);
5659*49cdfc7eSAndroid Build Coastguard Worker					fix_delete_line($fixlinenr, $rawline);
5660*49cdfc7eSAndroid Build Coastguard Worker					my $fixedline = $prevrawline;
5661*49cdfc7eSAndroid Build Coastguard Worker					my $trailing = $rawline;
5662*49cdfc7eSAndroid Build Coastguard Worker					$trailing =~ s/^\+//;
5663*49cdfc7eSAndroid Build Coastguard Worker					$trailing = trim($trailing);
5664*49cdfc7eSAndroid Build Coastguard Worker					$fixedline =~ s/}\s*$/} $trailing/;
5665*49cdfc7eSAndroid Build Coastguard Worker					fix_insert_line($fixlinenr, $fixedline);
5666*49cdfc7eSAndroid Build Coastguard Worker				}
5667*49cdfc7eSAndroid Build Coastguard Worker			}
5668*49cdfc7eSAndroid Build Coastguard Worker		}
5669*49cdfc7eSAndroid Build Coastguard Worker
5670*49cdfc7eSAndroid Build Coastguard Worker#Specific variable tests
5671*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ m{($Constant|$Lval)}g) {
5672*49cdfc7eSAndroid Build Coastguard Worker			my $var = $1;
5673*49cdfc7eSAndroid Build Coastguard Worker
5674*49cdfc7eSAndroid Build Coastguard Worker#CamelCase
5675*49cdfc7eSAndroid Build Coastguard Worker			if ($var !~ /^$Constant$/ &&
5676*49cdfc7eSAndroid Build Coastguard Worker			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
5677*49cdfc7eSAndroid Build Coastguard Worker#Ignore some autogenerated defines and enum values
5678*49cdfc7eSAndroid Build Coastguard Worker			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
5679*49cdfc7eSAndroid Build Coastguard Worker#Ignore Page<foo> variants
5680*49cdfc7eSAndroid Build Coastguard Worker			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5681*49cdfc7eSAndroid Build Coastguard Worker#Ignore SI style variants like nS, mV and dB
5682*49cdfc7eSAndroid Build Coastguard Worker#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5683*49cdfc7eSAndroid Build Coastguard Worker			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5684*49cdfc7eSAndroid Build Coastguard Worker#Ignore some three character SI units explicitly, like MiB and KHz
5685*49cdfc7eSAndroid Build Coastguard Worker			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
5686*49cdfc7eSAndroid Build Coastguard Worker				while ($var =~ m{($Ident)}g) {
5687*49cdfc7eSAndroid Build Coastguard Worker					my $word = $1;
5688*49cdfc7eSAndroid Build Coastguard Worker					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5689*49cdfc7eSAndroid Build Coastguard Worker					if ($check) {
5690*49cdfc7eSAndroid Build Coastguard Worker						seed_camelcase_includes();
5691*49cdfc7eSAndroid Build Coastguard Worker						if (!$file && !$camelcase_file_seeded) {
5692*49cdfc7eSAndroid Build Coastguard Worker							seed_camelcase_file($realfile);
5693*49cdfc7eSAndroid Build Coastguard Worker							$camelcase_file_seeded = 1;
5694*49cdfc7eSAndroid Build Coastguard Worker						}
5695*49cdfc7eSAndroid Build Coastguard Worker					}
5696*49cdfc7eSAndroid Build Coastguard Worker					if (!defined $camelcase{$word}) {
5697*49cdfc7eSAndroid Build Coastguard Worker						$camelcase{$word} = 1;
5698*49cdfc7eSAndroid Build Coastguard Worker						CHK("CAMELCASE",
5699*49cdfc7eSAndroid Build Coastguard Worker						    "Avoid CamelCase: <$word>\n" . $herecurr);
5700*49cdfc7eSAndroid Build Coastguard Worker					}
5701*49cdfc7eSAndroid Build Coastguard Worker				}
5702*49cdfc7eSAndroid Build Coastguard Worker			}
5703*49cdfc7eSAndroid Build Coastguard Worker		}
5704*49cdfc7eSAndroid Build Coastguard Worker
5705*49cdfc7eSAndroid Build Coastguard Worker#no spaces allowed after \ in define
5706*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\#\s*define.*\\\s+$/) {
5707*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5708*49cdfc7eSAndroid Build Coastguard Worker				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5709*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
5710*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\s+$//;
5711*49cdfc7eSAndroid Build Coastguard Worker			}
5712*49cdfc7eSAndroid Build Coastguard Worker		}
5713*49cdfc7eSAndroid Build Coastguard Worker
5714*49cdfc7eSAndroid Build Coastguard Worker# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
5715*49cdfc7eSAndroid Build Coastguard Worker# itself <asm/foo.h> (uses RAW line)
5716*49cdfc7eSAndroid Build Coastguard Worker		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5717*49cdfc7eSAndroid Build Coastguard Worker			my $file = "$1.h";
5718*49cdfc7eSAndroid Build Coastguard Worker			my $checkfile = "include/linux/$file";
5719*49cdfc7eSAndroid Build Coastguard Worker			if (-f "$root/$checkfile" &&
5720*49cdfc7eSAndroid Build Coastguard Worker			    $realfile ne $checkfile &&
5721*49cdfc7eSAndroid Build Coastguard Worker			    $1 !~ /$allowed_asm_includes/)
5722*49cdfc7eSAndroid Build Coastguard Worker			{
5723*49cdfc7eSAndroid Build Coastguard Worker				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
5724*49cdfc7eSAndroid Build Coastguard Worker				if ($asminclude > 0) {
5725*49cdfc7eSAndroid Build Coastguard Worker					if ($realfile =~ m{^arch/}) {
5726*49cdfc7eSAndroid Build Coastguard Worker						CHK("ARCH_INCLUDE_LINUX",
5727*49cdfc7eSAndroid Build Coastguard Worker						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5728*49cdfc7eSAndroid Build Coastguard Worker					} else {
5729*49cdfc7eSAndroid Build Coastguard Worker						WARN("INCLUDE_LINUX",
5730*49cdfc7eSAndroid Build Coastguard Worker						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5731*49cdfc7eSAndroid Build Coastguard Worker					}
5732*49cdfc7eSAndroid Build Coastguard Worker				}
5733*49cdfc7eSAndroid Build Coastguard Worker			}
5734*49cdfc7eSAndroid Build Coastguard Worker		}
5735*49cdfc7eSAndroid Build Coastguard Worker
5736*49cdfc7eSAndroid Build Coastguard Worker# multi-statement macros should be enclosed in a do while loop, grab the
5737*49cdfc7eSAndroid Build Coastguard Worker# first statement and ensure its the whole macro if its not enclosed
5738*49cdfc7eSAndroid Build Coastguard Worker# in a known good container
5739*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5740*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5741*49cdfc7eSAndroid Build Coastguard Worker			my $ln = $linenr;
5742*49cdfc7eSAndroid Build Coastguard Worker			my $cnt = $realcnt;
5743*49cdfc7eSAndroid Build Coastguard Worker			my ($off, $dstat, $dcond, $rest);
5744*49cdfc7eSAndroid Build Coastguard Worker			my $ctx = '';
5745*49cdfc7eSAndroid Build Coastguard Worker			my $has_flow_statement = 0;
5746*49cdfc7eSAndroid Build Coastguard Worker			my $has_arg_concat = 0;
5747*49cdfc7eSAndroid Build Coastguard Worker			($dstat, $dcond, $ln, $cnt, $off) =
5748*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_block($linenr, $realcnt, 0);
5749*49cdfc7eSAndroid Build Coastguard Worker			$ctx = $dstat;
5750*49cdfc7eSAndroid Build Coastguard Worker			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5751*49cdfc7eSAndroid Build Coastguard Worker			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5752*49cdfc7eSAndroid Build Coastguard Worker
5753*49cdfc7eSAndroid Build Coastguard Worker			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
5754*49cdfc7eSAndroid Build Coastguard Worker			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
5755*49cdfc7eSAndroid Build Coastguard Worker
5756*49cdfc7eSAndroid Build Coastguard Worker			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5757*49cdfc7eSAndroid Build Coastguard Worker			my $define_args = $1;
5758*49cdfc7eSAndroid Build Coastguard Worker			my $define_stmt = $dstat;
5759*49cdfc7eSAndroid Build Coastguard Worker			my @def_args = ();
5760*49cdfc7eSAndroid Build Coastguard Worker
5761*49cdfc7eSAndroid Build Coastguard Worker			if (defined $define_args && $define_args ne "") {
5762*49cdfc7eSAndroid Build Coastguard Worker				$define_args = substr($define_args, 1, length($define_args) - 2);
5763*49cdfc7eSAndroid Build Coastguard Worker				$define_args =~ s/\s*//g;
5764*49cdfc7eSAndroid Build Coastguard Worker				$define_args =~ s/\\\+?//g;
5765*49cdfc7eSAndroid Build Coastguard Worker				@def_args = split(",", $define_args);
5766*49cdfc7eSAndroid Build Coastguard Worker			}
5767*49cdfc7eSAndroid Build Coastguard Worker
5768*49cdfc7eSAndroid Build Coastguard Worker			$dstat =~ s/$;//g;
5769*49cdfc7eSAndroid Build Coastguard Worker			$dstat =~ s/\\\n.//g;
5770*49cdfc7eSAndroid Build Coastguard Worker			$dstat =~ s/^\s*//s;
5771*49cdfc7eSAndroid Build Coastguard Worker			$dstat =~ s/\s*$//s;
5772*49cdfc7eSAndroid Build Coastguard Worker
5773*49cdfc7eSAndroid Build Coastguard Worker			# Flatten any parentheses and braces
5774*49cdfc7eSAndroid Build Coastguard Worker			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
5775*49cdfc7eSAndroid Build Coastguard Worker			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
5776*49cdfc7eSAndroid Build Coastguard Worker			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5777*49cdfc7eSAndroid Build Coastguard Worker			{
5778*49cdfc7eSAndroid Build Coastguard Worker			}
5779*49cdfc7eSAndroid Build Coastguard Worker
5780*49cdfc7eSAndroid Build Coastguard Worker			# Flatten any obvious string concatenation.
5781*49cdfc7eSAndroid Build Coastguard Worker			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
5782*49cdfc7eSAndroid Build Coastguard Worker			       $dstat =~ s/$Ident\s*($String)/$1/)
5783*49cdfc7eSAndroid Build Coastguard Worker			{
5784*49cdfc7eSAndroid Build Coastguard Worker			}
5785*49cdfc7eSAndroid Build Coastguard Worker
5786*49cdfc7eSAndroid Build Coastguard Worker			# Make asm volatile uses seem like a generic function
5787*49cdfc7eSAndroid Build Coastguard Worker			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
5788*49cdfc7eSAndroid Build Coastguard Worker
5789*49cdfc7eSAndroid Build Coastguard Worker			my $exceptions = qr{
5790*49cdfc7eSAndroid Build Coastguard Worker				$Declare|
5791*49cdfc7eSAndroid Build Coastguard Worker				module_param_named|
5792*49cdfc7eSAndroid Build Coastguard Worker				MODULE_PARM_DESC|
5793*49cdfc7eSAndroid Build Coastguard Worker				DECLARE_PER_CPU|
5794*49cdfc7eSAndroid Build Coastguard Worker				DEFINE_PER_CPU|
5795*49cdfc7eSAndroid Build Coastguard Worker				__typeof__\(|
5796*49cdfc7eSAndroid Build Coastguard Worker				union|
5797*49cdfc7eSAndroid Build Coastguard Worker				struct|
5798*49cdfc7eSAndroid Build Coastguard Worker				\.$Ident\s*=\s*|
5799*49cdfc7eSAndroid Build Coastguard Worker				^\"|\"$|
5800*49cdfc7eSAndroid Build Coastguard Worker				^\[
5801*49cdfc7eSAndroid Build Coastguard Worker			}x;
5802*49cdfc7eSAndroid Build Coastguard Worker			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5803*49cdfc7eSAndroid Build Coastguard Worker
5804*49cdfc7eSAndroid Build Coastguard Worker			$ctx =~ s/\n*$//;
5805*49cdfc7eSAndroid Build Coastguard Worker			my $stmt_cnt = statement_rawlines($ctx);
5806*49cdfc7eSAndroid Build Coastguard Worker			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5807*49cdfc7eSAndroid Build Coastguard Worker
5808*49cdfc7eSAndroid Build Coastguard Worker			if ($dstat ne '' &&
5809*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5810*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
5811*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5812*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5813*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /$exceptions/ &&
5814*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
5815*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
5816*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
5817*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
5818*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
5819*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
5820*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^do\s*{/ &&					# do {...
5821*49cdfc7eSAndroid Build Coastguard Worker			    $dstat !~ /^\(\{/ &&						# ({...
5822*49cdfc7eSAndroid Build Coastguard Worker			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
5823*49cdfc7eSAndroid Build Coastguard Worker			{
5824*49cdfc7eSAndroid Build Coastguard Worker				if ($dstat =~ /^\s*if\b/) {
5825*49cdfc7eSAndroid Build Coastguard Worker					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5826*49cdfc7eSAndroid Build Coastguard Worker					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
5827*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($dstat =~ /;/) {
5828*49cdfc7eSAndroid Build Coastguard Worker					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
5829*49cdfc7eSAndroid Build Coastguard Worker					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
5830*49cdfc7eSAndroid Build Coastguard Worker				} else {
5831*49cdfc7eSAndroid Build Coastguard Worker					ERROR("COMPLEX_MACRO",
5832*49cdfc7eSAndroid Build Coastguard Worker					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
5833*49cdfc7eSAndroid Build Coastguard Worker				}
5834*49cdfc7eSAndroid Build Coastguard Worker
5835*49cdfc7eSAndroid Build Coastguard Worker			}
5836*49cdfc7eSAndroid Build Coastguard Worker
5837*49cdfc7eSAndroid Build Coastguard Worker			# Make $define_stmt single line, comment-free, etc
5838*49cdfc7eSAndroid Build Coastguard Worker			my @stmt_array = split('\n', $define_stmt);
5839*49cdfc7eSAndroid Build Coastguard Worker			my $first = 1;
5840*49cdfc7eSAndroid Build Coastguard Worker			$define_stmt = "";
5841*49cdfc7eSAndroid Build Coastguard Worker			foreach my $l (@stmt_array) {
5842*49cdfc7eSAndroid Build Coastguard Worker				$l =~ s/\\$//;
5843*49cdfc7eSAndroid Build Coastguard Worker				if ($first) {
5844*49cdfc7eSAndroid Build Coastguard Worker					$define_stmt = $l;
5845*49cdfc7eSAndroid Build Coastguard Worker					$first = 0;
5846*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($l =~ /^[\+ ]/) {
5847*49cdfc7eSAndroid Build Coastguard Worker					$define_stmt .= substr($l, 1);
5848*49cdfc7eSAndroid Build Coastguard Worker				}
5849*49cdfc7eSAndroid Build Coastguard Worker			}
5850*49cdfc7eSAndroid Build Coastguard Worker			$define_stmt =~ s/$;//g;
5851*49cdfc7eSAndroid Build Coastguard Worker			$define_stmt =~ s/\s+/ /g;
5852*49cdfc7eSAndroid Build Coastguard Worker			$define_stmt = trim($define_stmt);
5853*49cdfc7eSAndroid Build Coastguard Worker
5854*49cdfc7eSAndroid Build Coastguard Worker# check if any macro arguments are reused (ignore '...' and 'type')
5855*49cdfc7eSAndroid Build Coastguard Worker			foreach my $arg (@def_args) {
5856*49cdfc7eSAndroid Build Coastguard Worker			        next if ($arg =~ /\.\.\./);
5857*49cdfc7eSAndroid Build Coastguard Worker			        next if ($arg =~ /^type$/i);
5858*49cdfc7eSAndroid Build Coastguard Worker				my $tmp_stmt = $define_stmt;
5859*49cdfc7eSAndroid Build Coastguard Worker				$tmp_stmt =~ s/\b(__must_be_array|offsetof|sizeof|sizeof_field|__stringify|typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
5860*49cdfc7eSAndroid Build Coastguard Worker				$tmp_stmt =~ s/\#+\s*$arg\b//g;
5861*49cdfc7eSAndroid Build Coastguard Worker				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
5862*49cdfc7eSAndroid Build Coastguard Worker				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
5863*49cdfc7eSAndroid Build Coastguard Worker				if ($use_cnt > 1) {
5864*49cdfc7eSAndroid Build Coastguard Worker					CHK("MACRO_ARG_REUSE",
5865*49cdfc7eSAndroid Build Coastguard Worker					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
5866*49cdfc7eSAndroid Build Coastguard Worker				    }
5867*49cdfc7eSAndroid Build Coastguard Worker# check if any macro arguments may have other precedence issues
5868*49cdfc7eSAndroid Build Coastguard Worker				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
5869*49cdfc7eSAndroid Build Coastguard Worker				    ((defined($1) && $1 ne ',') ||
5870*49cdfc7eSAndroid Build Coastguard Worker				     (defined($2) && $2 ne ','))) {
5871*49cdfc7eSAndroid Build Coastguard Worker					CHK("MACRO_ARG_PRECEDENCE",
5872*49cdfc7eSAndroid Build Coastguard Worker					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
5873*49cdfc7eSAndroid Build Coastguard Worker				}
5874*49cdfc7eSAndroid Build Coastguard Worker			}
5875*49cdfc7eSAndroid Build Coastguard Worker
5876*49cdfc7eSAndroid Build Coastguard Worker# check for macros with flow control, but without ## concatenation
5877*49cdfc7eSAndroid Build Coastguard Worker# ## concatenation is commonly a macro that defines a function so ignore those
5878*49cdfc7eSAndroid Build Coastguard Worker			if ($has_flow_statement && !$has_arg_concat) {
5879*49cdfc7eSAndroid Build Coastguard Worker				my $cnt = statement_rawlines($ctx);
5880*49cdfc7eSAndroid Build Coastguard Worker				my $herectx = get_stat_here($linenr, $cnt, $here);
5881*49cdfc7eSAndroid Build Coastguard Worker
5882*49cdfc7eSAndroid Build Coastguard Worker				WARN("MACRO_WITH_FLOW_CONTROL",
5883*49cdfc7eSAndroid Build Coastguard Worker				     "Macros with flow control statements should be avoided\n" . "$herectx");
5884*49cdfc7eSAndroid Build Coastguard Worker			}
5885*49cdfc7eSAndroid Build Coastguard Worker
5886*49cdfc7eSAndroid Build Coastguard Worker# check for line continuations outside of #defines, preprocessor #, and asm
5887*49cdfc7eSAndroid Build Coastguard Worker
5888*49cdfc7eSAndroid Build Coastguard Worker		} else {
5889*49cdfc7eSAndroid Build Coastguard Worker			if ($prevline !~ /^..*\\$/ &&
5890*49cdfc7eSAndroid Build Coastguard Worker			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
5891*49cdfc7eSAndroid Build Coastguard Worker			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
5892*49cdfc7eSAndroid Build Coastguard Worker			    $line =~ /^\+.*\\$/) {
5893*49cdfc7eSAndroid Build Coastguard Worker				WARN("LINE_CONTINUATIONS",
5894*49cdfc7eSAndroid Build Coastguard Worker				     "Avoid unnecessary line continuations\n" . $herecurr);
5895*49cdfc7eSAndroid Build Coastguard Worker			}
5896*49cdfc7eSAndroid Build Coastguard Worker		}
5897*49cdfc7eSAndroid Build Coastguard Worker
5898*49cdfc7eSAndroid Build Coastguard Worker# do {} while (0) macro tests:
5899*49cdfc7eSAndroid Build Coastguard Worker# single-statement macros do not need to be enclosed in do while (0) loop,
5900*49cdfc7eSAndroid Build Coastguard Worker# macro should not end with a semicolon
5901*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
5902*49cdfc7eSAndroid Build Coastguard Worker		    $realfile !~ m@/vmlinux.lds.h$@ &&
5903*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
5904*49cdfc7eSAndroid Build Coastguard Worker			my $ln = $linenr;
5905*49cdfc7eSAndroid Build Coastguard Worker			my $cnt = $realcnt;
5906*49cdfc7eSAndroid Build Coastguard Worker			my ($off, $dstat, $dcond, $rest);
5907*49cdfc7eSAndroid Build Coastguard Worker			my $ctx = '';
5908*49cdfc7eSAndroid Build Coastguard Worker			($dstat, $dcond, $ln, $cnt, $off) =
5909*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_block($linenr, $realcnt, 0);
5910*49cdfc7eSAndroid Build Coastguard Worker			$ctx = $dstat;
5911*49cdfc7eSAndroid Build Coastguard Worker
5912*49cdfc7eSAndroid Build Coastguard Worker			$dstat =~ s/\\\n.//g;
5913*49cdfc7eSAndroid Build Coastguard Worker			$dstat =~ s/$;/ /g;
5914*49cdfc7eSAndroid Build Coastguard Worker
5915*49cdfc7eSAndroid Build Coastguard Worker			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5916*49cdfc7eSAndroid Build Coastguard Worker				my $stmts = $2;
5917*49cdfc7eSAndroid Build Coastguard Worker				my $semis = $3;
5918*49cdfc7eSAndroid Build Coastguard Worker
5919*49cdfc7eSAndroid Build Coastguard Worker				$ctx =~ s/\n*$//;
5920*49cdfc7eSAndroid Build Coastguard Worker				my $cnt = statement_rawlines($ctx);
5921*49cdfc7eSAndroid Build Coastguard Worker				my $herectx = get_stat_here($linenr, $cnt, $here);
5922*49cdfc7eSAndroid Build Coastguard Worker
5923*49cdfc7eSAndroid Build Coastguard Worker				if (($stmts =~ tr/;/;/) == 1 &&
5924*49cdfc7eSAndroid Build Coastguard Worker				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
5925*49cdfc7eSAndroid Build Coastguard Worker					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5926*49cdfc7eSAndroid Build Coastguard Worker					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5927*49cdfc7eSAndroid Build Coastguard Worker				}
5928*49cdfc7eSAndroid Build Coastguard Worker				if (defined $semis && $semis ne "") {
5929*49cdfc7eSAndroid Build Coastguard Worker					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5930*49cdfc7eSAndroid Build Coastguard Worker					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5931*49cdfc7eSAndroid Build Coastguard Worker				}
5932*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5933*49cdfc7eSAndroid Build Coastguard Worker				$ctx =~ s/\n*$//;
5934*49cdfc7eSAndroid Build Coastguard Worker				my $cnt = statement_rawlines($ctx);
5935*49cdfc7eSAndroid Build Coastguard Worker				my $herectx = get_stat_here($linenr, $cnt, $here);
5936*49cdfc7eSAndroid Build Coastguard Worker
5937*49cdfc7eSAndroid Build Coastguard Worker				WARN("TRAILING_SEMICOLON",
5938*49cdfc7eSAndroid Build Coastguard Worker				     "macros should not use a trailing semicolon\n" . "$herectx");
5939*49cdfc7eSAndroid Build Coastguard Worker			}
5940*49cdfc7eSAndroid Build Coastguard Worker		}
5941*49cdfc7eSAndroid Build Coastguard Worker
5942*49cdfc7eSAndroid Build Coastguard Worker# check for redundant bracing round if etc
5943*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5944*49cdfc7eSAndroid Build Coastguard Worker			my ($level, $endln, @chunks) =
5945*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_full($linenr, $realcnt, 1);
5946*49cdfc7eSAndroid Build Coastguard Worker			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
5947*49cdfc7eSAndroid Build Coastguard Worker			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5948*49cdfc7eSAndroid Build Coastguard Worker			if ($#chunks > 0 && $level == 0) {
5949*49cdfc7eSAndroid Build Coastguard Worker				my @allowed = ();
5950*49cdfc7eSAndroid Build Coastguard Worker				my $allow = 0;
5951*49cdfc7eSAndroid Build Coastguard Worker				my $seen = 0;
5952*49cdfc7eSAndroid Build Coastguard Worker				my $herectx = $here . "\n";
5953*49cdfc7eSAndroid Build Coastguard Worker				my $ln = $linenr - 1;
5954*49cdfc7eSAndroid Build Coastguard Worker				for my $chunk (@chunks) {
5955*49cdfc7eSAndroid Build Coastguard Worker					my ($cond, $block) = @{$chunk};
5956*49cdfc7eSAndroid Build Coastguard Worker
5957*49cdfc7eSAndroid Build Coastguard Worker					# If the condition carries leading newlines, then count those as offsets.
5958*49cdfc7eSAndroid Build Coastguard Worker					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5959*49cdfc7eSAndroid Build Coastguard Worker					my $offset = statement_rawlines($whitespace) - 1;
5960*49cdfc7eSAndroid Build Coastguard Worker
5961*49cdfc7eSAndroid Build Coastguard Worker					$allowed[$allow] = 0;
5962*49cdfc7eSAndroid Build Coastguard Worker					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5963*49cdfc7eSAndroid Build Coastguard Worker
5964*49cdfc7eSAndroid Build Coastguard Worker					# We have looked at and allowed this specific line.
5965*49cdfc7eSAndroid Build Coastguard Worker					$suppress_ifbraces{$ln + $offset} = 1;
5966*49cdfc7eSAndroid Build Coastguard Worker
5967*49cdfc7eSAndroid Build Coastguard Worker					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
5968*49cdfc7eSAndroid Build Coastguard Worker					$ln += statement_rawlines($block) - 1;
5969*49cdfc7eSAndroid Build Coastguard Worker
5970*49cdfc7eSAndroid Build Coastguard Worker					substr($block, 0, length($cond), '');
5971*49cdfc7eSAndroid Build Coastguard Worker
5972*49cdfc7eSAndroid Build Coastguard Worker					$seen++ if ($block =~ /^\s*{/);
5973*49cdfc7eSAndroid Build Coastguard Worker
5974*49cdfc7eSAndroid Build Coastguard Worker					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
5975*49cdfc7eSAndroid Build Coastguard Worker					if (statement_lines($cond) > 1) {
5976*49cdfc7eSAndroid Build Coastguard Worker						#print "APW: ALLOWED: cond<$cond>\n";
5977*49cdfc7eSAndroid Build Coastguard Worker						$allowed[$allow] = 1;
5978*49cdfc7eSAndroid Build Coastguard Worker					}
5979*49cdfc7eSAndroid Build Coastguard Worker					if ($block =~/\b(?:if|for|while)\b/) {
5980*49cdfc7eSAndroid Build Coastguard Worker						#print "APW: ALLOWED: block<$block>\n";
5981*49cdfc7eSAndroid Build Coastguard Worker						$allowed[$allow] = 1;
5982*49cdfc7eSAndroid Build Coastguard Worker					}
5983*49cdfc7eSAndroid Build Coastguard Worker					if (statement_block_size($block) > 1) {
5984*49cdfc7eSAndroid Build Coastguard Worker						#print "APW: ALLOWED: lines block<$block>\n";
5985*49cdfc7eSAndroid Build Coastguard Worker						$allowed[$allow] = 1;
5986*49cdfc7eSAndroid Build Coastguard Worker					}
5987*49cdfc7eSAndroid Build Coastguard Worker					$allow++;
5988*49cdfc7eSAndroid Build Coastguard Worker				}
5989*49cdfc7eSAndroid Build Coastguard Worker				if ($seen) {
5990*49cdfc7eSAndroid Build Coastguard Worker					my $sum_allowed = 0;
5991*49cdfc7eSAndroid Build Coastguard Worker					foreach (@allowed) {
5992*49cdfc7eSAndroid Build Coastguard Worker						$sum_allowed += $_;
5993*49cdfc7eSAndroid Build Coastguard Worker					}
5994*49cdfc7eSAndroid Build Coastguard Worker					if ($sum_allowed == 0) {
5995*49cdfc7eSAndroid Build Coastguard Worker						WARN("BRACES",
5996*49cdfc7eSAndroid Build Coastguard Worker						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
5997*49cdfc7eSAndroid Build Coastguard Worker					} elsif ($sum_allowed != $allow &&
5998*49cdfc7eSAndroid Build Coastguard Worker						 $seen != $allow) {
5999*49cdfc7eSAndroid Build Coastguard Worker						CHK("BRACES",
6000*49cdfc7eSAndroid Build Coastguard Worker						    "braces {} should be used on all arms of this statement\n" . $herectx);
6001*49cdfc7eSAndroid Build Coastguard Worker					}
6002*49cdfc7eSAndroid Build Coastguard Worker				}
6003*49cdfc7eSAndroid Build Coastguard Worker			}
6004*49cdfc7eSAndroid Build Coastguard Worker		}
6005*49cdfc7eSAndroid Build Coastguard Worker		if (!defined $suppress_ifbraces{$linenr - 1} &&
6006*49cdfc7eSAndroid Build Coastguard Worker					$line =~ /\b(if|while|for|else)\b/) {
6007*49cdfc7eSAndroid Build Coastguard Worker			my $allowed = 0;
6008*49cdfc7eSAndroid Build Coastguard Worker
6009*49cdfc7eSAndroid Build Coastguard Worker			# Check the pre-context.
6010*49cdfc7eSAndroid Build Coastguard Worker			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6011*49cdfc7eSAndroid Build Coastguard Worker				#print "APW: ALLOWED: pre<$1>\n";
6012*49cdfc7eSAndroid Build Coastguard Worker				$allowed = 1;
6013*49cdfc7eSAndroid Build Coastguard Worker			}
6014*49cdfc7eSAndroid Build Coastguard Worker
6015*49cdfc7eSAndroid Build Coastguard Worker			my ($level, $endln, @chunks) =
6016*49cdfc7eSAndroid Build Coastguard Worker				ctx_statement_full($linenr, $realcnt, $-[0]);
6017*49cdfc7eSAndroid Build Coastguard Worker
6018*49cdfc7eSAndroid Build Coastguard Worker			# Check the condition.
6019*49cdfc7eSAndroid Build Coastguard Worker			my ($cond, $block) = @{$chunks[0]};
6020*49cdfc7eSAndroid Build Coastguard Worker			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
6021*49cdfc7eSAndroid Build Coastguard Worker			if (defined $cond) {
6022*49cdfc7eSAndroid Build Coastguard Worker				substr($block, 0, length($cond), '');
6023*49cdfc7eSAndroid Build Coastguard Worker			}
6024*49cdfc7eSAndroid Build Coastguard Worker			if (statement_lines($cond) > 1) {
6025*49cdfc7eSAndroid Build Coastguard Worker				#print "APW: ALLOWED: cond<$cond>\n";
6026*49cdfc7eSAndroid Build Coastguard Worker				$allowed = 1;
6027*49cdfc7eSAndroid Build Coastguard Worker			}
6028*49cdfc7eSAndroid Build Coastguard Worker			if ($block =~/\b(?:if|for|while)\b/) {
6029*49cdfc7eSAndroid Build Coastguard Worker				#print "APW: ALLOWED: block<$block>\n";
6030*49cdfc7eSAndroid Build Coastguard Worker				$allowed = 1;
6031*49cdfc7eSAndroid Build Coastguard Worker			}
6032*49cdfc7eSAndroid Build Coastguard Worker			if (statement_block_size($block) > 1) {
6033*49cdfc7eSAndroid Build Coastguard Worker				#print "APW: ALLOWED: lines block<$block>\n";
6034*49cdfc7eSAndroid Build Coastguard Worker				$allowed = 1;
6035*49cdfc7eSAndroid Build Coastguard Worker			}
6036*49cdfc7eSAndroid Build Coastguard Worker			# Check the post-context.
6037*49cdfc7eSAndroid Build Coastguard Worker			if (defined $chunks[1]) {
6038*49cdfc7eSAndroid Build Coastguard Worker				my ($cond, $block) = @{$chunks[1]};
6039*49cdfc7eSAndroid Build Coastguard Worker				if (defined $cond) {
6040*49cdfc7eSAndroid Build Coastguard Worker					substr($block, 0, length($cond), '');
6041*49cdfc7eSAndroid Build Coastguard Worker				}
6042*49cdfc7eSAndroid Build Coastguard Worker				if ($block =~ /^\s*\{/) {
6043*49cdfc7eSAndroid Build Coastguard Worker					#print "APW: ALLOWED: chunk-1 block<$block>\n";
6044*49cdfc7eSAndroid Build Coastguard Worker					$allowed = 1;
6045*49cdfc7eSAndroid Build Coastguard Worker				}
6046*49cdfc7eSAndroid Build Coastguard Worker			}
6047*49cdfc7eSAndroid Build Coastguard Worker			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
6048*49cdfc7eSAndroid Build Coastguard Worker				my $cnt = statement_rawlines($block);
6049*49cdfc7eSAndroid Build Coastguard Worker				my $herectx = get_stat_here($linenr, $cnt, $here);
6050*49cdfc7eSAndroid Build Coastguard Worker
6051*49cdfc7eSAndroid Build Coastguard Worker				WARN("BRACES",
6052*49cdfc7eSAndroid Build Coastguard Worker				     "braces {} are not necessary for single statement blocks\n" . $herectx);
6053*49cdfc7eSAndroid Build Coastguard Worker			}
6054*49cdfc7eSAndroid Build Coastguard Worker		}
6055*49cdfc7eSAndroid Build Coastguard Worker
6056*49cdfc7eSAndroid Build Coastguard Worker# check for single line unbalanced braces
6057*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
6058*49cdfc7eSAndroid Build Coastguard Worker		    $sline =~ /^.\s*else\s*\{\s*$/) {
6059*49cdfc7eSAndroid Build Coastguard Worker			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6060*49cdfc7eSAndroid Build Coastguard Worker		}
6061*49cdfc7eSAndroid Build Coastguard Worker
6062*49cdfc7eSAndroid Build Coastguard Worker# check for unnecessary blank lines around braces
6063*49cdfc7eSAndroid Build Coastguard Worker		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
6064*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("BRACES",
6065*49cdfc7eSAndroid Build Coastguard Worker				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6066*49cdfc7eSAndroid Build Coastguard Worker			    $fix && $prevrawline =~ /^\+/) {
6067*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr - 1, $prevrawline);
6068*49cdfc7eSAndroid Build Coastguard Worker			}
6069*49cdfc7eSAndroid Build Coastguard Worker		}
6070*49cdfc7eSAndroid Build Coastguard Worker		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
6071*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("BRACES",
6072*49cdfc7eSAndroid Build Coastguard Worker				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6073*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6074*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr, $rawline);
6075*49cdfc7eSAndroid Build Coastguard Worker			}
6076*49cdfc7eSAndroid Build Coastguard Worker		}
6077*49cdfc7eSAndroid Build Coastguard Worker
6078*49cdfc7eSAndroid Build Coastguard Worker# no volatiles please
6079*49cdfc7eSAndroid Build Coastguard Worker		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
6080*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
6081*49cdfc7eSAndroid Build Coastguard Worker			WARN("VOLATILE",
6082*49cdfc7eSAndroid Build Coastguard Worker			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
6083*49cdfc7eSAndroid Build Coastguard Worker		}
6084*49cdfc7eSAndroid Build Coastguard Worker
6085*49cdfc7eSAndroid Build Coastguard Worker# Check for user-visible strings broken across lines, which breaks the ability
6086*49cdfc7eSAndroid Build Coastguard Worker# to grep for the string.  Make exceptions when the previous string ends in a
6087*49cdfc7eSAndroid Build Coastguard Worker# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
6088*49cdfc7eSAndroid Build Coastguard Worker# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
6089*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+\s*$String/ &&
6090*49cdfc7eSAndroid Build Coastguard Worker		    $prevline =~ /"\s*$/ &&
6091*49cdfc7eSAndroid Build Coastguard Worker		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
6092*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("SPLIT_STRING",
6093*49cdfc7eSAndroid Build Coastguard Worker				 "quoted string split across lines\n" . $hereprev) &&
6094*49cdfc7eSAndroid Build Coastguard Worker				     $fix &&
6095*49cdfc7eSAndroid Build Coastguard Worker				     $prevrawline =~ /^\+.*"\s*$/ &&
6096*49cdfc7eSAndroid Build Coastguard Worker				     $last_coalesced_string_linenr != $linenr - 1) {
6097*49cdfc7eSAndroid Build Coastguard Worker				my $extracted_string = get_quoted_string($line, $rawline);
6098*49cdfc7eSAndroid Build Coastguard Worker				my $comma_close = "";
6099*49cdfc7eSAndroid Build Coastguard Worker				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
6100*49cdfc7eSAndroid Build Coastguard Worker					$comma_close = $1;
6101*49cdfc7eSAndroid Build Coastguard Worker				}
6102*49cdfc7eSAndroid Build Coastguard Worker
6103*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr - 1, $prevrawline);
6104*49cdfc7eSAndroid Build Coastguard Worker				fix_delete_line($fixlinenr, $rawline);
6105*49cdfc7eSAndroid Build Coastguard Worker				my $fixedline = $prevrawline;
6106*49cdfc7eSAndroid Build Coastguard Worker				$fixedline =~ s/"\s*$//;
6107*49cdfc7eSAndroid Build Coastguard Worker				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
6108*49cdfc7eSAndroid Build Coastguard Worker				fix_insert_line($fixlinenr - 1, $fixedline);
6109*49cdfc7eSAndroid Build Coastguard Worker				$fixedline = $rawline;
6110*49cdfc7eSAndroid Build Coastguard Worker				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
6111*49cdfc7eSAndroid Build Coastguard Worker				if ($fixedline !~ /\+\s*$/) {
6112*49cdfc7eSAndroid Build Coastguard Worker					fix_insert_line($fixlinenr, $fixedline);
6113*49cdfc7eSAndroid Build Coastguard Worker				}
6114*49cdfc7eSAndroid Build Coastguard Worker				$last_coalesced_string_linenr = $linenr;
6115*49cdfc7eSAndroid Build Coastguard Worker			}
6116*49cdfc7eSAndroid Build Coastguard Worker		}
6117*49cdfc7eSAndroid Build Coastguard Worker
6118*49cdfc7eSAndroid Build Coastguard Worker# check for missing a space in a string concatenation
6119*49cdfc7eSAndroid Build Coastguard Worker		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
6120*49cdfc7eSAndroid Build Coastguard Worker			WARN('MISSING_SPACE',
6121*49cdfc7eSAndroid Build Coastguard Worker			     "break quoted strings at a space character\n" . $hereprev);
6122*49cdfc7eSAndroid Build Coastguard Worker		}
6123*49cdfc7eSAndroid Build Coastguard Worker
6124*49cdfc7eSAndroid Build Coastguard Worker# check for an embedded function name in a string when the function is known
6125*49cdfc7eSAndroid Build Coastguard Worker# This does not work very well for -f --file checking as it depends on patch
6126*49cdfc7eSAndroid Build Coastguard Worker# context providing the function name or a single line form for in-file
6127*49cdfc7eSAndroid Build Coastguard Worker# function declarations
6128*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+.*$String/ &&
6129*49cdfc7eSAndroid Build Coastguard Worker		    defined($context_function) &&
6130*49cdfc7eSAndroid Build Coastguard Worker		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6131*49cdfc7eSAndroid Build Coastguard Worker		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
6132*49cdfc7eSAndroid Build Coastguard Worker			WARN("EMBEDDED_FUNCTION_NAME",
6133*49cdfc7eSAndroid Build Coastguard Worker			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
6134*49cdfc7eSAndroid Build Coastguard Worker		}
6135*49cdfc7eSAndroid Build Coastguard Worker
6136*49cdfc7eSAndroid Build Coastguard Worker# check for unnecessary function tracing like uses
6137*49cdfc7eSAndroid Build Coastguard Worker# This does not use $logFunctions because there are many instances like
6138*49cdfc7eSAndroid Build Coastguard Worker# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6139*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6140*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("TRACING_LOGGING",
6141*49cdfc7eSAndroid Build Coastguard Worker				 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6142*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6143*49cdfc7eSAndroid Build Coastguard Worker                                fix_delete_line($fixlinenr, $rawline);
6144*49cdfc7eSAndroid Build Coastguard Worker			}
6145*49cdfc7eSAndroid Build Coastguard Worker		}
6146*49cdfc7eSAndroid Build Coastguard Worker
6147*49cdfc7eSAndroid Build Coastguard Worker# check for spaces before a quoted newline
6148*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /^.*\".*\s\\n/) {
6149*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
6150*49cdfc7eSAndroid Build Coastguard Worker				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
6151*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6152*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
6153*49cdfc7eSAndroid Build Coastguard Worker			}
6154*49cdfc7eSAndroid Build Coastguard Worker
6155*49cdfc7eSAndroid Build Coastguard Worker		}
6156*49cdfc7eSAndroid Build Coastguard Worker
6157*49cdfc7eSAndroid Build Coastguard Worker# concatenated string without spaces between elements
6158*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /$String[A-Z_]/ ||
6159*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
6160*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("CONCATENATED_STRING",
6161*49cdfc7eSAndroid Build Coastguard Worker				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
6162*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6163*49cdfc7eSAndroid Build Coastguard Worker				while ($line =~ /($String)/g) {
6164*49cdfc7eSAndroid Build Coastguard Worker					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6165*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
6166*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
6167*49cdfc7eSAndroid Build Coastguard Worker				}
6168*49cdfc7eSAndroid Build Coastguard Worker			}
6169*49cdfc7eSAndroid Build Coastguard Worker		}
6170*49cdfc7eSAndroid Build Coastguard Worker
6171*49cdfc7eSAndroid Build Coastguard Worker# uncoalesced string fragments
6172*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /$String\s*[Lu]?"/) {
6173*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("STRING_FRAGMENTS",
6174*49cdfc7eSAndroid Build Coastguard Worker				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
6175*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6176*49cdfc7eSAndroid Build Coastguard Worker				while ($line =~ /($String)(?=\s*")/g) {
6177*49cdfc7eSAndroid Build Coastguard Worker					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
6178*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
6179*49cdfc7eSAndroid Build Coastguard Worker				}
6180*49cdfc7eSAndroid Build Coastguard Worker			}
6181*49cdfc7eSAndroid Build Coastguard Worker		}
6182*49cdfc7eSAndroid Build Coastguard Worker
6183*49cdfc7eSAndroid Build Coastguard Worker# check for non-standard and hex prefixed decimal printf formats
6184*49cdfc7eSAndroid Build Coastguard Worker		my $show_L = 1;	#don't show the same defect twice
6185*49cdfc7eSAndroid Build Coastguard Worker		my $show_Z = 1;
6186*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6187*49cdfc7eSAndroid Build Coastguard Worker			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
6188*49cdfc7eSAndroid Build Coastguard Worker			$string =~ s/%%/__/g;
6189*49cdfc7eSAndroid Build Coastguard Worker			# check for %L
6190*49cdfc7eSAndroid Build Coastguard Worker			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
6191*49cdfc7eSAndroid Build Coastguard Worker				WARN("PRINTF_L",
6192*49cdfc7eSAndroid Build Coastguard Worker				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6193*49cdfc7eSAndroid Build Coastguard Worker				$show_L = 0;
6194*49cdfc7eSAndroid Build Coastguard Worker			}
6195*49cdfc7eSAndroid Build Coastguard Worker			# check for %Z
6196*49cdfc7eSAndroid Build Coastguard Worker			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6197*49cdfc7eSAndroid Build Coastguard Worker				WARN("PRINTF_Z",
6198*49cdfc7eSAndroid Build Coastguard Worker				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6199*49cdfc7eSAndroid Build Coastguard Worker				$show_Z = 0;
6200*49cdfc7eSAndroid Build Coastguard Worker			}
6201*49cdfc7eSAndroid Build Coastguard Worker			# check for 0x<decimal>
6202*49cdfc7eSAndroid Build Coastguard Worker			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6203*49cdfc7eSAndroid Build Coastguard Worker				ERROR("PRINTF_0XDECIMAL",
6204*49cdfc7eSAndroid Build Coastguard Worker				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
6205*49cdfc7eSAndroid Build Coastguard Worker			}
6206*49cdfc7eSAndroid Build Coastguard Worker		}
6207*49cdfc7eSAndroid Build Coastguard Worker
6208*49cdfc7eSAndroid Build Coastguard Worker# check for line continuations in quoted strings with odd counts of "
6209*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
6210*49cdfc7eSAndroid Build Coastguard Worker			WARN("LINE_CONTINUATIONS",
6211*49cdfc7eSAndroid Build Coastguard Worker			     "Avoid line continuations in quoted strings\n" . $herecurr);
6212*49cdfc7eSAndroid Build Coastguard Worker		}
6213*49cdfc7eSAndroid Build Coastguard Worker
6214*49cdfc7eSAndroid Build Coastguard Worker# warn about #if 0
6215*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
6216*49cdfc7eSAndroid Build Coastguard Worker			WARN("IF_0",
6217*49cdfc7eSAndroid Build Coastguard Worker			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
6218*49cdfc7eSAndroid Build Coastguard Worker		}
6219*49cdfc7eSAndroid Build Coastguard Worker
6220*49cdfc7eSAndroid Build Coastguard Worker# warn about #if 1
6221*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
6222*49cdfc7eSAndroid Build Coastguard Worker			WARN("IF_1",
6223*49cdfc7eSAndroid Build Coastguard Worker			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
6224*49cdfc7eSAndroid Build Coastguard Worker		}
6225*49cdfc7eSAndroid Build Coastguard Worker
6226*49cdfc7eSAndroid Build Coastguard Worker# check for needless "if (<foo>) fn(<foo>)" uses
6227*49cdfc7eSAndroid Build Coastguard Worker		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6228*49cdfc7eSAndroid Build Coastguard Worker			my $tested = quotemeta($1);
6229*49cdfc7eSAndroid Build Coastguard Worker			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6230*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6231*49cdfc7eSAndroid Build Coastguard Worker				my $func = $1;
6232*49cdfc7eSAndroid Build Coastguard Worker				if (WARN('NEEDLESS_IF',
6233*49cdfc7eSAndroid Build Coastguard Worker					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6234*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
6235*49cdfc7eSAndroid Build Coastguard Worker					my $do_fix = 1;
6236*49cdfc7eSAndroid Build Coastguard Worker					my $leading_tabs = "";
6237*49cdfc7eSAndroid Build Coastguard Worker					my $new_leading_tabs = "";
6238*49cdfc7eSAndroid Build Coastguard Worker					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6239*49cdfc7eSAndroid Build Coastguard Worker						$leading_tabs = $1;
6240*49cdfc7eSAndroid Build Coastguard Worker					} else {
6241*49cdfc7eSAndroid Build Coastguard Worker						$do_fix = 0;
6242*49cdfc7eSAndroid Build Coastguard Worker					}
6243*49cdfc7eSAndroid Build Coastguard Worker					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6244*49cdfc7eSAndroid Build Coastguard Worker						$new_leading_tabs = $1;
6245*49cdfc7eSAndroid Build Coastguard Worker						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6246*49cdfc7eSAndroid Build Coastguard Worker							$do_fix = 0;
6247*49cdfc7eSAndroid Build Coastguard Worker						}
6248*49cdfc7eSAndroid Build Coastguard Worker					} else {
6249*49cdfc7eSAndroid Build Coastguard Worker						$do_fix = 0;
6250*49cdfc7eSAndroid Build Coastguard Worker					}
6251*49cdfc7eSAndroid Build Coastguard Worker					if ($do_fix) {
6252*49cdfc7eSAndroid Build Coastguard Worker						fix_delete_line($fixlinenr - 1, $prevrawline);
6253*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6254*49cdfc7eSAndroid Build Coastguard Worker					}
6255*49cdfc7eSAndroid Build Coastguard Worker				}
6256*49cdfc7eSAndroid Build Coastguard Worker			}
6257*49cdfc7eSAndroid Build Coastguard Worker		}
6258*49cdfc7eSAndroid Build Coastguard Worker
6259*49cdfc7eSAndroid Build Coastguard Worker# check for unnecessary "Out of Memory" messages
6260*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6261*49cdfc7eSAndroid Build Coastguard Worker		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6262*49cdfc7eSAndroid Build Coastguard Worker		    (defined $1 || defined $3) &&
6263*49cdfc7eSAndroid Build Coastguard Worker		    $linenr > 3) {
6264*49cdfc7eSAndroid Build Coastguard Worker			my $testval = $2;
6265*49cdfc7eSAndroid Build Coastguard Worker			my $testline = $lines[$linenr - 3];
6266*49cdfc7eSAndroid Build Coastguard Worker
6267*49cdfc7eSAndroid Build Coastguard Worker			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6268*49cdfc7eSAndroid Build Coastguard Worker#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6269*49cdfc7eSAndroid Build Coastguard Worker
6270*49cdfc7eSAndroid Build Coastguard Worker			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6271*49cdfc7eSAndroid Build Coastguard Worker			    $s !~ /\b__GFP_NOWARN\b/ ) {
6272*49cdfc7eSAndroid Build Coastguard Worker				WARN("OOM_MESSAGE",
6273*49cdfc7eSAndroid Build Coastguard Worker				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
6274*49cdfc7eSAndroid Build Coastguard Worker			}
6275*49cdfc7eSAndroid Build Coastguard Worker		}
6276*49cdfc7eSAndroid Build Coastguard Worker
6277*49cdfc7eSAndroid Build Coastguard Worker# check for logging functions with KERN_<LEVEL>
6278*49cdfc7eSAndroid Build Coastguard Worker		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6279*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6280*49cdfc7eSAndroid Build Coastguard Worker			my $level = $1;
6281*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("UNNECESSARY_KERN_LEVEL",
6282*49cdfc7eSAndroid Build Coastguard Worker				 "Possible unnecessary $level\n" . $herecurr) &&
6283*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6284*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
6285*49cdfc7eSAndroid Build Coastguard Worker			}
6286*49cdfc7eSAndroid Build Coastguard Worker		}
6287*49cdfc7eSAndroid Build Coastguard Worker
6288*49cdfc7eSAndroid Build Coastguard Worker# check for logging continuations
6289*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
6290*49cdfc7eSAndroid Build Coastguard Worker			WARN("LOGGING_CONTINUATION",
6291*49cdfc7eSAndroid Build Coastguard Worker			     "Avoid logging continuation uses where feasible\n" . $herecurr);
6292*49cdfc7eSAndroid Build Coastguard Worker		}
6293*49cdfc7eSAndroid Build Coastguard Worker
6294*49cdfc7eSAndroid Build Coastguard Worker# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
6295*49cdfc7eSAndroid Build Coastguard Worker		if (defined $stat &&
6296*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b$logFunctions\s*\(/ &&
6297*49cdfc7eSAndroid Build Coastguard Worker		    index($stat, '"') >= 0) {
6298*49cdfc7eSAndroid Build Coastguard Worker			my $lc = $stat =~ tr@\n@@;
6299*49cdfc7eSAndroid Build Coastguard Worker			$lc = $lc + $linenr;
6300*49cdfc7eSAndroid Build Coastguard Worker			my $stat_real = get_stat_real($linenr, $lc);
6301*49cdfc7eSAndroid Build Coastguard Worker			pos($stat_real) = index($stat_real, '"');
6302*49cdfc7eSAndroid Build Coastguard Worker			while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
6303*49cdfc7eSAndroid Build Coastguard Worker				my $pspec = $1;
6304*49cdfc7eSAndroid Build Coastguard Worker				my $h = $2;
6305*49cdfc7eSAndroid Build Coastguard Worker				my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
6306*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("UNNECESSARY_MODIFIER",
6307*49cdfc7eSAndroid Build Coastguard Worker					 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
6308*49cdfc7eSAndroid Build Coastguard Worker				    $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
6309*49cdfc7eSAndroid Build Coastguard Worker					my $nspec = $pspec;
6310*49cdfc7eSAndroid Build Coastguard Worker					$nspec =~ s/h//g;
6311*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
6312*49cdfc7eSAndroid Build Coastguard Worker				}
6313*49cdfc7eSAndroid Build Coastguard Worker			}
6314*49cdfc7eSAndroid Build Coastguard Worker		}
6315*49cdfc7eSAndroid Build Coastguard Worker
6316*49cdfc7eSAndroid Build Coastguard Worker# check for mask then right shift without a parentheses
6317*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6318*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6319*49cdfc7eSAndroid Build Coastguard Worker		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6320*49cdfc7eSAndroid Build Coastguard Worker			WARN("MASK_THEN_SHIFT",
6321*49cdfc7eSAndroid Build Coastguard Worker			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6322*49cdfc7eSAndroid Build Coastguard Worker		}
6323*49cdfc7eSAndroid Build Coastguard Worker
6324*49cdfc7eSAndroid Build Coastguard Worker# check for pointer comparisons to NULL
6325*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok) {
6326*49cdfc7eSAndroid Build Coastguard Worker			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6327*49cdfc7eSAndroid Build Coastguard Worker				my $val = $1;
6328*49cdfc7eSAndroid Build Coastguard Worker				my $equal = "!";
6329*49cdfc7eSAndroid Build Coastguard Worker				$equal = "" if ($4 eq "!=");
6330*49cdfc7eSAndroid Build Coastguard Worker				if (CHK("COMPARISON_TO_NULL",
6331*49cdfc7eSAndroid Build Coastguard Worker					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6332*49cdfc7eSAndroid Build Coastguard Worker					    $fix) {
6333*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6334*49cdfc7eSAndroid Build Coastguard Worker				}
6335*49cdfc7eSAndroid Build Coastguard Worker			}
6336*49cdfc7eSAndroid Build Coastguard Worker		}
6337*49cdfc7eSAndroid Build Coastguard Worker
6338*49cdfc7eSAndroid Build Coastguard Worker# check for bad placement of section $InitAttribute (e.g.: __initdata)
6339*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /(\b$InitAttribute\b)/) {
6340*49cdfc7eSAndroid Build Coastguard Worker			my $attr = $1;
6341*49cdfc7eSAndroid Build Coastguard Worker			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
6342*49cdfc7eSAndroid Build Coastguard Worker				my $ptr = $1;
6343*49cdfc7eSAndroid Build Coastguard Worker				my $var = $2;
6344*49cdfc7eSAndroid Build Coastguard Worker				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
6345*49cdfc7eSAndroid Build Coastguard Worker				      ERROR("MISPLACED_INIT",
6346*49cdfc7eSAndroid Build Coastguard Worker					    "$attr should be placed after $var\n" . $herecurr)) ||
6347*49cdfc7eSAndroid Build Coastguard Worker				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
6348*49cdfc7eSAndroid Build Coastguard Worker				      WARN("MISPLACED_INIT",
6349*49cdfc7eSAndroid Build Coastguard Worker					   "$attr should be placed after $var\n" . $herecurr))) &&
6350*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
6351*49cdfc7eSAndroid Build Coastguard Worker					$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;
6352*49cdfc7eSAndroid Build Coastguard Worker				}
6353*49cdfc7eSAndroid Build Coastguard Worker			}
6354*49cdfc7eSAndroid Build Coastguard Worker		}
6355*49cdfc7eSAndroid Build Coastguard Worker
6356*49cdfc7eSAndroid Build Coastguard Worker# check for $InitAttributeData (ie: __initdata) with const
6357*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6358*49cdfc7eSAndroid Build Coastguard Worker			my $attr = $1;
6359*49cdfc7eSAndroid Build Coastguard Worker			$attr =~ /($InitAttributePrefix)(.*)/;
6360*49cdfc7eSAndroid Build Coastguard Worker			my $attr_prefix = $1;
6361*49cdfc7eSAndroid Build Coastguard Worker			my $attr_type = $2;
6362*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("INIT_ATTRIBUTE",
6363*49cdfc7eSAndroid Build Coastguard Worker				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6364*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6365*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
6366*49cdfc7eSAndroid Build Coastguard Worker				    s/$InitAttributeData/${attr_prefix}initconst/;
6367*49cdfc7eSAndroid Build Coastguard Worker			}
6368*49cdfc7eSAndroid Build Coastguard Worker		}
6369*49cdfc7eSAndroid Build Coastguard Worker
6370*49cdfc7eSAndroid Build Coastguard Worker# check for $InitAttributeConst (ie: __initconst) without const
6371*49cdfc7eSAndroid Build Coastguard Worker		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6372*49cdfc7eSAndroid Build Coastguard Worker			my $attr = $1;
6373*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("INIT_ATTRIBUTE",
6374*49cdfc7eSAndroid Build Coastguard Worker				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
6375*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6376*49cdfc7eSAndroid Build Coastguard Worker				my $lead = $fixed[$fixlinenr] =~
6377*49cdfc7eSAndroid Build Coastguard Worker				    /(^\+\s*(?:static\s+))/;
6378*49cdfc7eSAndroid Build Coastguard Worker				$lead = rtrim($1);
6379*49cdfc7eSAndroid Build Coastguard Worker				$lead = "$lead " if ($lead !~ /^\+$/);
6380*49cdfc7eSAndroid Build Coastguard Worker				$lead = "${lead}const ";
6381*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6382*49cdfc7eSAndroid Build Coastguard Worker			}
6383*49cdfc7eSAndroid Build Coastguard Worker		}
6384*49cdfc7eSAndroid Build Coastguard Worker
6385*49cdfc7eSAndroid Build Coastguard Worker# check for __read_mostly with const non-pointer (should just be const)
6386*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b__read_mostly\b/ &&
6387*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6388*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("CONST_READ_MOSTLY",
6389*49cdfc7eSAndroid Build Coastguard Worker				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6390*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6391*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6392*49cdfc7eSAndroid Build Coastguard Worker			}
6393*49cdfc7eSAndroid Build Coastguard Worker		}
6394*49cdfc7eSAndroid Build Coastguard Worker
6395*49cdfc7eSAndroid Build Coastguard Worker# don't use __constant_<foo> functions outside of include/uapi/
6396*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile !~ m@^include/uapi/@ &&
6397*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6398*49cdfc7eSAndroid Build Coastguard Worker			my $constant_func = $1;
6399*49cdfc7eSAndroid Build Coastguard Worker			my $func = $constant_func;
6400*49cdfc7eSAndroid Build Coastguard Worker			$func =~ s/^__constant_//;
6401*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("CONSTANT_CONVERSION",
6402*49cdfc7eSAndroid Build Coastguard Worker				 "$constant_func should be $func\n" . $herecurr) &&
6403*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6404*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6405*49cdfc7eSAndroid Build Coastguard Worker			}
6406*49cdfc7eSAndroid Build Coastguard Worker		}
6407*49cdfc7eSAndroid Build Coastguard Worker
6408*49cdfc7eSAndroid Build Coastguard Worker# prefer usleep_range over udelay
6409*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
6410*49cdfc7eSAndroid Build Coastguard Worker			my $delay = $1;
6411*49cdfc7eSAndroid Build Coastguard Worker			# ignore udelay's < 10, however
6412*49cdfc7eSAndroid Build Coastguard Worker			if (! ($delay < 10) ) {
6413*49cdfc7eSAndroid Build Coastguard Worker				CHK("USLEEP_RANGE",
6414*49cdfc7eSAndroid Build Coastguard Worker				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
6415*49cdfc7eSAndroid Build Coastguard Worker			}
6416*49cdfc7eSAndroid Build Coastguard Worker			if ($delay > 2000) {
6417*49cdfc7eSAndroid Build Coastguard Worker				WARN("LONG_UDELAY",
6418*49cdfc7eSAndroid Build Coastguard Worker				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
6419*49cdfc7eSAndroid Build Coastguard Worker			}
6420*49cdfc7eSAndroid Build Coastguard Worker		}
6421*49cdfc7eSAndroid Build Coastguard Worker
6422*49cdfc7eSAndroid Build Coastguard Worker# warn about unexpectedly long msleep's
6423*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
6424*49cdfc7eSAndroid Build Coastguard Worker			if ($1 < 20) {
6425*49cdfc7eSAndroid Build Coastguard Worker				WARN("MSLEEP",
6426*49cdfc7eSAndroid Build Coastguard Worker				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
6427*49cdfc7eSAndroid Build Coastguard Worker			}
6428*49cdfc7eSAndroid Build Coastguard Worker		}
6429*49cdfc7eSAndroid Build Coastguard Worker
6430*49cdfc7eSAndroid Build Coastguard Worker# check for comparisons of jiffies
6431*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
6432*49cdfc7eSAndroid Build Coastguard Worker			WARN("JIFFIES_COMPARISON",
6433*49cdfc7eSAndroid Build Coastguard Worker			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
6434*49cdfc7eSAndroid Build Coastguard Worker		}
6435*49cdfc7eSAndroid Build Coastguard Worker
6436*49cdfc7eSAndroid Build Coastguard Worker# check for comparisons of get_jiffies_64()
6437*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
6438*49cdfc7eSAndroid Build Coastguard Worker			WARN("JIFFIES_COMPARISON",
6439*49cdfc7eSAndroid Build Coastguard Worker			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
6440*49cdfc7eSAndroid Build Coastguard Worker		}
6441*49cdfc7eSAndroid Build Coastguard Worker
6442*49cdfc7eSAndroid Build Coastguard Worker# warn about #ifdefs in C files
6443*49cdfc7eSAndroid Build Coastguard Worker#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
6444*49cdfc7eSAndroid Build Coastguard Worker#			print "#ifdef in C files should be avoided\n";
6445*49cdfc7eSAndroid Build Coastguard Worker#			print "$herecurr";
6446*49cdfc7eSAndroid Build Coastguard Worker#			$clean = 0;
6447*49cdfc7eSAndroid Build Coastguard Worker#		}
6448*49cdfc7eSAndroid Build Coastguard Worker
6449*49cdfc7eSAndroid Build Coastguard Worker# warn about spacing in #ifdefs
6450*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
6451*49cdfc7eSAndroid Build Coastguard Worker			if (ERROR("SPACING",
6452*49cdfc7eSAndroid Build Coastguard Worker				  "exactly one space required after that #$1\n" . $herecurr) &&
6453*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6454*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~
6455*49cdfc7eSAndroid Build Coastguard Worker				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
6456*49cdfc7eSAndroid Build Coastguard Worker			}
6457*49cdfc7eSAndroid Build Coastguard Worker
6458*49cdfc7eSAndroid Build Coastguard Worker		}
6459*49cdfc7eSAndroid Build Coastguard Worker
6460*49cdfc7eSAndroid Build Coastguard Worker# check for spinlock_t definitions without a comment.
6461*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6462*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
6463*49cdfc7eSAndroid Build Coastguard Worker			my $which = $1;
6464*49cdfc7eSAndroid Build Coastguard Worker			if (!ctx_has_comment($first_line, $linenr)) {
6465*49cdfc7eSAndroid Build Coastguard Worker				CHK("UNCOMMENTED_DEFINITION",
6466*49cdfc7eSAndroid Build Coastguard Worker				    "$1 definition without comment\n" . $herecurr);
6467*49cdfc7eSAndroid Build Coastguard Worker			}
6468*49cdfc7eSAndroid Build Coastguard Worker		}
6469*49cdfc7eSAndroid Build Coastguard Worker# check for memory barriers without a comment.
6470*49cdfc7eSAndroid Build Coastguard Worker
6471*49cdfc7eSAndroid Build Coastguard Worker		my $barriers = qr{
6472*49cdfc7eSAndroid Build Coastguard Worker			mb|
6473*49cdfc7eSAndroid Build Coastguard Worker			rmb|
6474*49cdfc7eSAndroid Build Coastguard Worker			wmb
6475*49cdfc7eSAndroid Build Coastguard Worker		}x;
6476*49cdfc7eSAndroid Build Coastguard Worker		my $barrier_stems = qr{
6477*49cdfc7eSAndroid Build Coastguard Worker			mb__before_atomic|
6478*49cdfc7eSAndroid Build Coastguard Worker			mb__after_atomic|
6479*49cdfc7eSAndroid Build Coastguard Worker			store_release|
6480*49cdfc7eSAndroid Build Coastguard Worker			load_acquire|
6481*49cdfc7eSAndroid Build Coastguard Worker			store_mb|
6482*49cdfc7eSAndroid Build Coastguard Worker			(?:$barriers)
6483*49cdfc7eSAndroid Build Coastguard Worker		}x;
6484*49cdfc7eSAndroid Build Coastguard Worker		my $all_barriers = qr{
6485*49cdfc7eSAndroid Build Coastguard Worker			(?:$barriers)|
6486*49cdfc7eSAndroid Build Coastguard Worker			smp_(?:$barrier_stems)|
6487*49cdfc7eSAndroid Build Coastguard Worker			virt_(?:$barrier_stems)
6488*49cdfc7eSAndroid Build Coastguard Worker		}x;
6489*49cdfc7eSAndroid Build Coastguard Worker
6490*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
6491*49cdfc7eSAndroid Build Coastguard Worker			if (!ctx_has_comment($first_line, $linenr)) {
6492*49cdfc7eSAndroid Build Coastguard Worker				WARN("MEMORY_BARRIER",
6493*49cdfc7eSAndroid Build Coastguard Worker				     "memory barrier without comment\n" . $herecurr);
6494*49cdfc7eSAndroid Build Coastguard Worker			}
6495*49cdfc7eSAndroid Build Coastguard Worker		}
6496*49cdfc7eSAndroid Build Coastguard Worker
6497*49cdfc7eSAndroid Build Coastguard Worker		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6498*49cdfc7eSAndroid Build Coastguard Worker
6499*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile !~ m@^include/asm-generic/@ &&
6500*49cdfc7eSAndroid Build Coastguard Worker		    $realfile !~ m@/barrier\.h$@ &&
6501*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6502*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6503*49cdfc7eSAndroid Build Coastguard Worker			WARN("MEMORY_BARRIER",
6504*49cdfc7eSAndroid Build Coastguard Worker			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6505*49cdfc7eSAndroid Build Coastguard Worker		}
6506*49cdfc7eSAndroid Build Coastguard Worker
6507*49cdfc7eSAndroid Build Coastguard Worker# check for waitqueue_active without a comment.
6508*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bwaitqueue_active\s*\(/) {
6509*49cdfc7eSAndroid Build Coastguard Worker			if (!ctx_has_comment($first_line, $linenr)) {
6510*49cdfc7eSAndroid Build Coastguard Worker				WARN("WAITQUEUE_ACTIVE",
6511*49cdfc7eSAndroid Build Coastguard Worker				     "waitqueue_active without comment\n" . $herecurr);
6512*49cdfc7eSAndroid Build Coastguard Worker			}
6513*49cdfc7eSAndroid Build Coastguard Worker		}
6514*49cdfc7eSAndroid Build Coastguard Worker
6515*49cdfc7eSAndroid Build Coastguard Worker# check for data_race without a comment.
6516*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bdata_race\s*\(/) {
6517*49cdfc7eSAndroid Build Coastguard Worker			if (!ctx_has_comment($first_line, $linenr)) {
6518*49cdfc7eSAndroid Build Coastguard Worker				WARN("DATA_RACE",
6519*49cdfc7eSAndroid Build Coastguard Worker				     "data_race without comment\n" . $herecurr);
6520*49cdfc7eSAndroid Build Coastguard Worker			}
6521*49cdfc7eSAndroid Build Coastguard Worker		}
6522*49cdfc7eSAndroid Build Coastguard Worker
6523*49cdfc7eSAndroid Build Coastguard Worker# check of hardware specific defines
6524*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6525*49cdfc7eSAndroid Build Coastguard Worker			CHK("ARCH_DEFINES",
6526*49cdfc7eSAndroid Build Coastguard Worker			    "architecture specific defines should be avoided\n" .  $herecurr);
6527*49cdfc7eSAndroid Build Coastguard Worker		}
6528*49cdfc7eSAndroid Build Coastguard Worker
6529*49cdfc7eSAndroid Build Coastguard Worker# check that the storage class is not after a type
6530*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6531*49cdfc7eSAndroid Build Coastguard Worker			WARN("STORAGE_CLASS",
6532*49cdfc7eSAndroid Build Coastguard Worker			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6533*49cdfc7eSAndroid Build Coastguard Worker		}
6534*49cdfc7eSAndroid Build Coastguard Worker# Check that the storage class is at the beginning of a declaration
6535*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b$Storage\b/ &&
6536*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /^.\s*$Storage/ &&
6537*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6538*49cdfc7eSAndroid Build Coastguard Worker		    $1 !~ /[\,\)]\s*$/) {
6539*49cdfc7eSAndroid Build Coastguard Worker			WARN("STORAGE_CLASS",
6540*49cdfc7eSAndroid Build Coastguard Worker			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6541*49cdfc7eSAndroid Build Coastguard Worker		}
6542*49cdfc7eSAndroid Build Coastguard Worker
6543*49cdfc7eSAndroid Build Coastguard Worker# check the location of the inline attribute, that it is between
6544*49cdfc7eSAndroid Build Coastguard Worker# storage class and type.
6545*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b$Type\s+$Inline\b/ ||
6546*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b$Inline\s+$Storage\b/) {
6547*49cdfc7eSAndroid Build Coastguard Worker			ERROR("INLINE_LOCATION",
6548*49cdfc7eSAndroid Build Coastguard Worker			      "inline keyword should sit between storage class and type\n" . $herecurr);
6549*49cdfc7eSAndroid Build Coastguard Worker		}
6550*49cdfc7eSAndroid Build Coastguard Worker
6551*49cdfc7eSAndroid Build Coastguard Worker# Check for __inline__ and __inline, prefer inline
6552*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile !~ m@\binclude/uapi/@ &&
6553*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b(__inline__|__inline)\b/) {
6554*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("INLINE",
6555*49cdfc7eSAndroid Build Coastguard Worker				 "plain inline is preferred over $1\n" . $herecurr) &&
6556*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6557*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6558*49cdfc7eSAndroid Build Coastguard Worker
6559*49cdfc7eSAndroid Build Coastguard Worker			}
6560*49cdfc7eSAndroid Build Coastguard Worker		}
6561*49cdfc7eSAndroid Build Coastguard Worker
6562*49cdfc7eSAndroid Build Coastguard Worker# Check for compiler attributes
6563*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile !~ m@\binclude/uapi/@ &&
6564*49cdfc7eSAndroid Build Coastguard Worker		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
6565*49cdfc7eSAndroid Build Coastguard Worker			my $attr = $1;
6566*49cdfc7eSAndroid Build Coastguard Worker			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
6567*49cdfc7eSAndroid Build Coastguard Worker
6568*49cdfc7eSAndroid Build Coastguard Worker			my %attr_list = (
6569*49cdfc7eSAndroid Build Coastguard Worker				"alias"				=> "__alias",
6570*49cdfc7eSAndroid Build Coastguard Worker				"aligned"			=> "__aligned",
6571*49cdfc7eSAndroid Build Coastguard Worker				"always_inline"			=> "__always_inline",
6572*49cdfc7eSAndroid Build Coastguard Worker				"assume_aligned"		=> "__assume_aligned",
6573*49cdfc7eSAndroid Build Coastguard Worker				"cold"				=> "__cold",
6574*49cdfc7eSAndroid Build Coastguard Worker				"const"				=> "__attribute_const__",
6575*49cdfc7eSAndroid Build Coastguard Worker				"copy"				=> "__copy",
6576*49cdfc7eSAndroid Build Coastguard Worker				"designated_init"		=> "__designated_init",
6577*49cdfc7eSAndroid Build Coastguard Worker				"externally_visible"		=> "__visible",
6578*49cdfc7eSAndroid Build Coastguard Worker				"format"			=> "printf|scanf",
6579*49cdfc7eSAndroid Build Coastguard Worker				"gnu_inline"			=> "__gnu_inline",
6580*49cdfc7eSAndroid Build Coastguard Worker				"malloc"			=> "__malloc",
6581*49cdfc7eSAndroid Build Coastguard Worker				"mode"				=> "__mode",
6582*49cdfc7eSAndroid Build Coastguard Worker				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
6583*49cdfc7eSAndroid Build Coastguard Worker				"noclone"			=> "__noclone",
6584*49cdfc7eSAndroid Build Coastguard Worker				"noinline"			=> "noinline",
6585*49cdfc7eSAndroid Build Coastguard Worker				"nonstring"			=> "__nonstring",
6586*49cdfc7eSAndroid Build Coastguard Worker				"noreturn"			=> "__noreturn",
6587*49cdfc7eSAndroid Build Coastguard Worker				"packed"			=> "__packed",
6588*49cdfc7eSAndroid Build Coastguard Worker				"pure"				=> "__pure",
6589*49cdfc7eSAndroid Build Coastguard Worker				"section"			=> "__section",
6590*49cdfc7eSAndroid Build Coastguard Worker				"used"				=> "__used",
6591*49cdfc7eSAndroid Build Coastguard Worker				"weak"				=> "__weak"
6592*49cdfc7eSAndroid Build Coastguard Worker			);
6593*49cdfc7eSAndroid Build Coastguard Worker
6594*49cdfc7eSAndroid Build Coastguard Worker			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6595*49cdfc7eSAndroid Build Coastguard Worker				my $orig_attr = $1;
6596*49cdfc7eSAndroid Build Coastguard Worker				my $params = '';
6597*49cdfc7eSAndroid Build Coastguard Worker				$params = $2 if defined($2);
6598*49cdfc7eSAndroid Build Coastguard Worker				my $curr_attr = $orig_attr;
6599*49cdfc7eSAndroid Build Coastguard Worker				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
6600*49cdfc7eSAndroid Build Coastguard Worker				if (exists($attr_list{$curr_attr})) {
6601*49cdfc7eSAndroid Build Coastguard Worker					my $new = $attr_list{$curr_attr};
6602*49cdfc7eSAndroid Build Coastguard Worker					if ($curr_attr eq "format" && $params) {
6603*49cdfc7eSAndroid Build Coastguard Worker						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6604*49cdfc7eSAndroid Build Coastguard Worker						$new = "__$1\($2";
6605*49cdfc7eSAndroid Build Coastguard Worker					} else {
6606*49cdfc7eSAndroid Build Coastguard Worker						$new = "$new$params";
6607*49cdfc7eSAndroid Build Coastguard Worker					}
6608*49cdfc7eSAndroid Build Coastguard Worker					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6609*49cdfc7eSAndroid Build Coastguard Worker						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
6610*49cdfc7eSAndroid Build Coastguard Worker					    $fix) {
6611*49cdfc7eSAndroid Build Coastguard Worker						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6612*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/$remove//;
6613*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6614*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6615*49cdfc7eSAndroid Build Coastguard Worker						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
6616*49cdfc7eSAndroid Build Coastguard Worker					}
6617*49cdfc7eSAndroid Build Coastguard Worker				}
6618*49cdfc7eSAndroid Build Coastguard Worker			}
6619*49cdfc7eSAndroid Build Coastguard Worker
6620*49cdfc7eSAndroid Build Coastguard Worker			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
6621*49cdfc7eSAndroid Build Coastguard Worker			if ($attr =~ /^_*unused/) {
6622*49cdfc7eSAndroid Build Coastguard Worker				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6623*49cdfc7eSAndroid Build Coastguard Worker				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6624*49cdfc7eSAndroid Build Coastguard Worker			}
6625*49cdfc7eSAndroid Build Coastguard Worker		}
6626*49cdfc7eSAndroid Build Coastguard Worker
6627*49cdfc7eSAndroid Build Coastguard Worker# Check for __attribute__ weak, or __weak declarations (may have link issues)
6628*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6629*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6630*49cdfc7eSAndroid Build Coastguard Worker		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6631*49cdfc7eSAndroid Build Coastguard Worker		     $line =~ /\b__weak\b/)) {
6632*49cdfc7eSAndroid Build Coastguard Worker			ERROR("WEAK_DECLARATION",
6633*49cdfc7eSAndroid Build Coastguard Worker			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6634*49cdfc7eSAndroid Build Coastguard Worker		}
6635*49cdfc7eSAndroid Build Coastguard Worker
6636*49cdfc7eSAndroid Build Coastguard Worker# check for c99 types like uint8_t used outside of uapi/ and tools/
6637*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile !~ m@\binclude/uapi/@ &&
6638*49cdfc7eSAndroid Build Coastguard Worker		    $realfile !~ m@\btools/@ &&
6639*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6640*49cdfc7eSAndroid Build Coastguard Worker			my $type = $1;
6641*49cdfc7eSAndroid Build Coastguard Worker			if ($type =~ /\b($typeC99Typedefs)\b/) {
6642*49cdfc7eSAndroid Build Coastguard Worker				$type = $1;
6643*49cdfc7eSAndroid Build Coastguard Worker				my $kernel_type = 'u';
6644*49cdfc7eSAndroid Build Coastguard Worker				$kernel_type = 's' if ($type =~ /^_*[si]/);
6645*49cdfc7eSAndroid Build Coastguard Worker				$type =~ /(\d+)/;
6646*49cdfc7eSAndroid Build Coastguard Worker				$kernel_type .= $1;
6647*49cdfc7eSAndroid Build Coastguard Worker				if (CHK("PREFER_KERNEL_TYPES",
6648*49cdfc7eSAndroid Build Coastguard Worker					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6649*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
6650*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6651*49cdfc7eSAndroid Build Coastguard Worker				}
6652*49cdfc7eSAndroid Build Coastguard Worker			}
6653*49cdfc7eSAndroid Build Coastguard Worker		}
6654*49cdfc7eSAndroid Build Coastguard Worker
6655*49cdfc7eSAndroid Build Coastguard Worker# check for cast of C90 native int or longer types constants
6656*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6657*49cdfc7eSAndroid Build Coastguard Worker			my $cast = $1;
6658*49cdfc7eSAndroid Build Coastguard Worker			my $const = $2;
6659*49cdfc7eSAndroid Build Coastguard Worker			my $suffix = "";
6660*49cdfc7eSAndroid Build Coastguard Worker			my $newconst = $const;
6661*49cdfc7eSAndroid Build Coastguard Worker			$newconst =~ s/${Int_type}$//;
6662*49cdfc7eSAndroid Build Coastguard Worker			$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6663*49cdfc7eSAndroid Build Coastguard Worker			if ($cast =~ /\blong\s+long\b/) {
6664*49cdfc7eSAndroid Build Coastguard Worker			    $suffix .= 'LL';
6665*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($cast =~ /\blong\b/) {
6666*49cdfc7eSAndroid Build Coastguard Worker			    $suffix .= 'L';
6667*49cdfc7eSAndroid Build Coastguard Worker			}
6668*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("TYPECAST_INT_CONSTANT",
6669*49cdfc7eSAndroid Build Coastguard Worker				 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
6670*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6671*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6672*49cdfc7eSAndroid Build Coastguard Worker			}
6673*49cdfc7eSAndroid Build Coastguard Worker		}
6674*49cdfc7eSAndroid Build Coastguard Worker
6675*49cdfc7eSAndroid Build Coastguard Worker# check for sizeof(&)
6676*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6677*49cdfc7eSAndroid Build Coastguard Worker			WARN("SIZEOF_ADDRESS",
6678*49cdfc7eSAndroid Build Coastguard Worker			     "sizeof(& should be avoided\n" . $herecurr);
6679*49cdfc7eSAndroid Build Coastguard Worker		}
6680*49cdfc7eSAndroid Build Coastguard Worker
6681*49cdfc7eSAndroid Build Coastguard Worker# check for sizeof without parenthesis
6682*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6683*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("SIZEOF_PARENTHESIS",
6684*49cdfc7eSAndroid Build Coastguard Worker				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6685*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6686*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6687*49cdfc7eSAndroid Build Coastguard Worker			}
6688*49cdfc7eSAndroid Build Coastguard Worker		}
6689*49cdfc7eSAndroid Build Coastguard Worker
6690*49cdfc7eSAndroid Build Coastguard Worker# check for struct spinlock declarations
6691*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
6692*49cdfc7eSAndroid Build Coastguard Worker			WARN("USE_SPINLOCK_T",
6693*49cdfc7eSAndroid Build Coastguard Worker			     "struct spinlock should be spinlock_t\n" . $herecurr);
6694*49cdfc7eSAndroid Build Coastguard Worker		}
6695*49cdfc7eSAndroid Build Coastguard Worker
6696*49cdfc7eSAndroid Build Coastguard Worker# check for seq_printf uses that could be seq_puts
6697*49cdfc7eSAndroid Build Coastguard Worker		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6698*49cdfc7eSAndroid Build Coastguard Worker			my $fmt = get_quoted_string($line, $rawline);
6699*49cdfc7eSAndroid Build Coastguard Worker			$fmt =~ s/%%//g;
6700*49cdfc7eSAndroid Build Coastguard Worker			if ($fmt !~ /%/) {
6701*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("PREFER_SEQ_PUTS",
6702*49cdfc7eSAndroid Build Coastguard Worker					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6703*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
6704*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6705*49cdfc7eSAndroid Build Coastguard Worker				}
6706*49cdfc7eSAndroid Build Coastguard Worker			}
6707*49cdfc7eSAndroid Build Coastguard Worker		}
6708*49cdfc7eSAndroid Build Coastguard Worker
6709*49cdfc7eSAndroid Build Coastguard Worker# check for vsprintf extension %p<foo> misuses
6710*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6711*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
6712*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
6713*49cdfc7eSAndroid Build Coastguard Worker		    $1 !~ /^_*volatile_*$/) {
6714*49cdfc7eSAndroid Build Coastguard Worker			my $stat_real;
6715*49cdfc7eSAndroid Build Coastguard Worker
6716*49cdfc7eSAndroid Build Coastguard Worker			my $lc = $stat =~ tr@\n@@;
6717*49cdfc7eSAndroid Build Coastguard Worker			$lc = $lc + $linenr;
6718*49cdfc7eSAndroid Build Coastguard Worker		        for (my $count = $linenr; $count <= $lc; $count++) {
6719*49cdfc7eSAndroid Build Coastguard Worker				my $specifier;
6720*49cdfc7eSAndroid Build Coastguard Worker				my $extension;
6721*49cdfc7eSAndroid Build Coastguard Worker				my $qualifier;
6722*49cdfc7eSAndroid Build Coastguard Worker				my $bad_specifier = "";
6723*49cdfc7eSAndroid Build Coastguard Worker				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
6724*49cdfc7eSAndroid Build Coastguard Worker				$fmt =~ s/%%//g;
6725*49cdfc7eSAndroid Build Coastguard Worker
6726*49cdfc7eSAndroid Build Coastguard Worker				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6727*49cdfc7eSAndroid Build Coastguard Worker					$specifier = $1;
6728*49cdfc7eSAndroid Build Coastguard Worker					$extension = $2;
6729*49cdfc7eSAndroid Build Coastguard Worker					$qualifier = $3;
6730*49cdfc7eSAndroid Build Coastguard Worker					if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
6731*49cdfc7eSAndroid Build Coastguard Worker					    ($extension eq "f" &&
6732*49cdfc7eSAndroid Build Coastguard Worker					     defined $qualifier && $qualifier !~ /^w/) ||
6733*49cdfc7eSAndroid Build Coastguard Worker					    ($extension eq "4" &&
6734*49cdfc7eSAndroid Build Coastguard Worker					     defined $qualifier && $qualifier !~ /^cc/)) {
6735*49cdfc7eSAndroid Build Coastguard Worker						$bad_specifier = $specifier;
6736*49cdfc7eSAndroid Build Coastguard Worker						last;
6737*49cdfc7eSAndroid Build Coastguard Worker					}
6738*49cdfc7eSAndroid Build Coastguard Worker					if ($extension eq "x" && !defined($stat_real)) {
6739*49cdfc7eSAndroid Build Coastguard Worker						if (!defined($stat_real)) {
6740*49cdfc7eSAndroid Build Coastguard Worker							$stat_real = get_stat_real($linenr, $lc);
6741*49cdfc7eSAndroid Build Coastguard Worker						}
6742*49cdfc7eSAndroid Build Coastguard Worker						WARN("VSPRINTF_SPECIFIER_PX",
6743*49cdfc7eSAndroid Build Coastguard Worker						     "Using vsprintf specifier '\%px' potentially exposes the kernel memory layout, if you don't really need the address please consider using '\%p'.\n" . "$here\n$stat_real\n");
6744*49cdfc7eSAndroid Build Coastguard Worker					}
6745*49cdfc7eSAndroid Build Coastguard Worker				}
6746*49cdfc7eSAndroid Build Coastguard Worker				if ($bad_specifier ne "") {
6747*49cdfc7eSAndroid Build Coastguard Worker					my $stat_real = get_stat_real($linenr, $lc);
6748*49cdfc7eSAndroid Build Coastguard Worker					my $ext_type = "Invalid";
6749*49cdfc7eSAndroid Build Coastguard Worker					my $use = "";
6750*49cdfc7eSAndroid Build Coastguard Worker					if ($bad_specifier =~ /p[Ff]/) {
6751*49cdfc7eSAndroid Build Coastguard Worker						$use = " - use %pS instead";
6752*49cdfc7eSAndroid Build Coastguard Worker						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6753*49cdfc7eSAndroid Build Coastguard Worker					}
6754*49cdfc7eSAndroid Build Coastguard Worker
6755*49cdfc7eSAndroid Build Coastguard Worker					WARN("VSPRINTF_POINTER_EXTENSION",
6756*49cdfc7eSAndroid Build Coastguard Worker					     "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6757*49cdfc7eSAndroid Build Coastguard Worker				}
6758*49cdfc7eSAndroid Build Coastguard Worker			}
6759*49cdfc7eSAndroid Build Coastguard Worker		}
6760*49cdfc7eSAndroid Build Coastguard Worker
6761*49cdfc7eSAndroid Build Coastguard Worker# Check for misused memsets
6762*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6763*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
6764*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6765*49cdfc7eSAndroid Build Coastguard Worker
6766*49cdfc7eSAndroid Build Coastguard Worker			my $ms_addr = $2;
6767*49cdfc7eSAndroid Build Coastguard Worker			my $ms_val = $7;
6768*49cdfc7eSAndroid Build Coastguard Worker			my $ms_size = $12;
6769*49cdfc7eSAndroid Build Coastguard Worker
6770*49cdfc7eSAndroid Build Coastguard Worker			if ($ms_size =~ /^(0x|)0$/i) {
6771*49cdfc7eSAndroid Build Coastguard Worker				ERROR("MEMSET",
6772*49cdfc7eSAndroid Build Coastguard Worker				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6773*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($ms_size =~ /^(0x|)1$/i) {
6774*49cdfc7eSAndroid Build Coastguard Worker				WARN("MEMSET",
6775*49cdfc7eSAndroid Build Coastguard Worker				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6776*49cdfc7eSAndroid Build Coastguard Worker			}
6777*49cdfc7eSAndroid Build Coastguard Worker		}
6778*49cdfc7eSAndroid Build Coastguard Worker
6779*49cdfc7eSAndroid Build Coastguard Worker# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
6780*49cdfc7eSAndroid Build Coastguard Worker#		if ($perl_version_ok &&
6781*49cdfc7eSAndroid Build Coastguard Worker#		    defined $stat &&
6782*49cdfc7eSAndroid Build Coastguard Worker#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6783*49cdfc7eSAndroid Build Coastguard Worker#			if (WARN("PREFER_ETHER_ADDR_COPY",
6784*49cdfc7eSAndroid Build Coastguard Worker#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6785*49cdfc7eSAndroid Build Coastguard Worker#			    $fix) {
6786*49cdfc7eSAndroid Build Coastguard Worker#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6787*49cdfc7eSAndroid Build Coastguard Worker#			}
6788*49cdfc7eSAndroid Build Coastguard Worker#		}
6789*49cdfc7eSAndroid Build Coastguard Worker
6790*49cdfc7eSAndroid Build Coastguard Worker# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
6791*49cdfc7eSAndroid Build Coastguard Worker#		if ($perl_version_ok &&
6792*49cdfc7eSAndroid Build Coastguard Worker#		    defined $stat &&
6793*49cdfc7eSAndroid Build Coastguard Worker#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6794*49cdfc7eSAndroid Build Coastguard Worker#			WARN("PREFER_ETHER_ADDR_EQUAL",
6795*49cdfc7eSAndroid Build Coastguard Worker#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6796*49cdfc7eSAndroid Build Coastguard Worker#		}
6797*49cdfc7eSAndroid Build Coastguard Worker
6798*49cdfc7eSAndroid Build Coastguard Worker# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
6799*49cdfc7eSAndroid Build Coastguard Worker# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
6800*49cdfc7eSAndroid Build Coastguard Worker#		if ($perl_version_ok &&
6801*49cdfc7eSAndroid Build Coastguard Worker#		    defined $stat &&
6802*49cdfc7eSAndroid Build Coastguard Worker#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6803*49cdfc7eSAndroid Build Coastguard Worker#
6804*49cdfc7eSAndroid Build Coastguard Worker#			my $ms_val = $7;
6805*49cdfc7eSAndroid Build Coastguard Worker#
6806*49cdfc7eSAndroid Build Coastguard Worker#			if ($ms_val =~ /^(?:0x|)0+$/i) {
6807*49cdfc7eSAndroid Build Coastguard Worker#				if (WARN("PREFER_ETH_ZERO_ADDR",
6808*49cdfc7eSAndroid Build Coastguard Worker#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
6809*49cdfc7eSAndroid Build Coastguard Worker#				    $fix) {
6810*49cdfc7eSAndroid Build Coastguard Worker#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
6811*49cdfc7eSAndroid Build Coastguard Worker#				}
6812*49cdfc7eSAndroid Build Coastguard Worker#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
6813*49cdfc7eSAndroid Build Coastguard Worker#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
6814*49cdfc7eSAndroid Build Coastguard Worker#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
6815*49cdfc7eSAndroid Build Coastguard Worker#				    $fix) {
6816*49cdfc7eSAndroid Build Coastguard Worker#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
6817*49cdfc7eSAndroid Build Coastguard Worker#				}
6818*49cdfc7eSAndroid Build Coastguard Worker#			}
6819*49cdfc7eSAndroid Build Coastguard Worker#		}
6820*49cdfc7eSAndroid Build Coastguard Worker
6821*49cdfc7eSAndroid Build Coastguard Worker# strlcpy uses that should likely be strscpy
6822*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bstrlcpy\s*\(/) {
6823*49cdfc7eSAndroid Build Coastguard Worker			WARN("STRLCPY",
6824*49cdfc7eSAndroid Build Coastguard Worker			     "Prefer strscpy over strlcpy - see: https://lore.kernel.org/r/CAHk-=wgfRnXz0W3D37d01q3JFkr_i_uTL=V6A6G1oUZcprmknw\@mail.gmail.com/\n" . $herecurr);
6825*49cdfc7eSAndroid Build Coastguard Worker		}
6826*49cdfc7eSAndroid Build Coastguard Worker
6827*49cdfc7eSAndroid Build Coastguard Worker# typecasts on min/max could be min_t/max_t
6828*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6829*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
6830*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
6831*49cdfc7eSAndroid Build Coastguard Worker			if (defined $2 || defined $7) {
6832*49cdfc7eSAndroid Build Coastguard Worker				my $call = $1;
6833*49cdfc7eSAndroid Build Coastguard Worker				my $cast1 = deparenthesize($2);
6834*49cdfc7eSAndroid Build Coastguard Worker				my $arg1 = $3;
6835*49cdfc7eSAndroid Build Coastguard Worker				my $cast2 = deparenthesize($7);
6836*49cdfc7eSAndroid Build Coastguard Worker				my $arg2 = $8;
6837*49cdfc7eSAndroid Build Coastguard Worker				my $cast;
6838*49cdfc7eSAndroid Build Coastguard Worker
6839*49cdfc7eSAndroid Build Coastguard Worker				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
6840*49cdfc7eSAndroid Build Coastguard Worker					$cast = "$cast1 or $cast2";
6841*49cdfc7eSAndroid Build Coastguard Worker				} elsif ($cast1 ne "") {
6842*49cdfc7eSAndroid Build Coastguard Worker					$cast = $cast1;
6843*49cdfc7eSAndroid Build Coastguard Worker				} else {
6844*49cdfc7eSAndroid Build Coastguard Worker					$cast = $cast2;
6845*49cdfc7eSAndroid Build Coastguard Worker				}
6846*49cdfc7eSAndroid Build Coastguard Worker				WARN("MINMAX",
6847*49cdfc7eSAndroid Build Coastguard Worker				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
6848*49cdfc7eSAndroid Build Coastguard Worker			}
6849*49cdfc7eSAndroid Build Coastguard Worker		}
6850*49cdfc7eSAndroid Build Coastguard Worker
6851*49cdfc7eSAndroid Build Coastguard Worker# check usleep_range arguments
6852*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6853*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
6854*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
6855*49cdfc7eSAndroid Build Coastguard Worker			my $min = $1;
6856*49cdfc7eSAndroid Build Coastguard Worker			my $max = $7;
6857*49cdfc7eSAndroid Build Coastguard Worker			if ($min eq $max) {
6858*49cdfc7eSAndroid Build Coastguard Worker				WARN("USLEEP_RANGE",
6859*49cdfc7eSAndroid Build Coastguard Worker				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
6860*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
6861*49cdfc7eSAndroid Build Coastguard Worker				 $min > $max) {
6862*49cdfc7eSAndroid Build Coastguard Worker				WARN("USLEEP_RANGE",
6863*49cdfc7eSAndroid Build Coastguard Worker				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
6864*49cdfc7eSAndroid Build Coastguard Worker			}
6865*49cdfc7eSAndroid Build Coastguard Worker		}
6866*49cdfc7eSAndroid Build Coastguard Worker
6867*49cdfc7eSAndroid Build Coastguard Worker# check for naked sscanf
6868*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6869*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
6870*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\bsscanf\b/ &&
6871*49cdfc7eSAndroid Build Coastguard Worker		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
6872*49cdfc7eSAndroid Build Coastguard Worker		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
6873*49cdfc7eSAndroid Build Coastguard Worker		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
6874*49cdfc7eSAndroid Build Coastguard Worker			my $lc = $stat =~ tr@\n@@;
6875*49cdfc7eSAndroid Build Coastguard Worker			$lc = $lc + $linenr;
6876*49cdfc7eSAndroid Build Coastguard Worker			my $stat_real = get_stat_real($linenr, $lc);
6877*49cdfc7eSAndroid Build Coastguard Worker			WARN("NAKED_SSCANF",
6878*49cdfc7eSAndroid Build Coastguard Worker			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
6879*49cdfc7eSAndroid Build Coastguard Worker		}
6880*49cdfc7eSAndroid Build Coastguard Worker
6881*49cdfc7eSAndroid Build Coastguard Worker# check for simple sscanf that should be kstrto<foo>
6882*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6883*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
6884*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\bsscanf\b/) {
6885*49cdfc7eSAndroid Build Coastguard Worker			my $lc = $stat =~ tr@\n@@;
6886*49cdfc7eSAndroid Build Coastguard Worker			$lc = $lc + $linenr;
6887*49cdfc7eSAndroid Build Coastguard Worker			my $stat_real = get_stat_real($linenr, $lc);
6888*49cdfc7eSAndroid Build Coastguard Worker			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
6889*49cdfc7eSAndroid Build Coastguard Worker				my $format = $6;
6890*49cdfc7eSAndroid Build Coastguard Worker				my $count = $format =~ tr@%@%@;
6891*49cdfc7eSAndroid Build Coastguard Worker				if ($count == 1 &&
6892*49cdfc7eSAndroid Build Coastguard Worker				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
6893*49cdfc7eSAndroid Build Coastguard Worker					WARN("SSCANF_TO_KSTRTO",
6894*49cdfc7eSAndroid Build Coastguard Worker					     "Prefer tst_parse_<type> to single variable sscanf\n" . "$here\n$stat_real\n");
6895*49cdfc7eSAndroid Build Coastguard Worker				}
6896*49cdfc7eSAndroid Build Coastguard Worker			}
6897*49cdfc7eSAndroid Build Coastguard Worker		}
6898*49cdfc7eSAndroid Build Coastguard Worker
6899*49cdfc7eSAndroid Build Coastguard Worker# check for new externs in .h files.
6900*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile =~ /\.h$/ &&
6901*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
6902*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("AVOID_EXTERNS",
6903*49cdfc7eSAndroid Build Coastguard Worker				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
6904*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
6905*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
6906*49cdfc7eSAndroid Build Coastguard Worker			}
6907*49cdfc7eSAndroid Build Coastguard Worker		}
6908*49cdfc7eSAndroid Build Coastguard Worker
6909*49cdfc7eSAndroid Build Coastguard Worker# check for new externs in .c files.
6910*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile =~ /\.c$/ && defined $stat &&
6911*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
6912*49cdfc7eSAndroid Build Coastguard Worker		{
6913*49cdfc7eSAndroid Build Coastguard Worker			my $function_name = $1;
6914*49cdfc7eSAndroid Build Coastguard Worker			my $paren_space = $2;
6915*49cdfc7eSAndroid Build Coastguard Worker
6916*49cdfc7eSAndroid Build Coastguard Worker			my $s = $stat;
6917*49cdfc7eSAndroid Build Coastguard Worker			if (defined $cond) {
6918*49cdfc7eSAndroid Build Coastguard Worker				substr($s, 0, length($cond), '');
6919*49cdfc7eSAndroid Build Coastguard Worker			}
6920*49cdfc7eSAndroid Build Coastguard Worker			if ($s =~ /^\s*;/)
6921*49cdfc7eSAndroid Build Coastguard Worker			{
6922*49cdfc7eSAndroid Build Coastguard Worker				WARN("AVOID_EXTERNS",
6923*49cdfc7eSAndroid Build Coastguard Worker				     "externs should be avoided in .c files\n" .  $herecurr);
6924*49cdfc7eSAndroid Build Coastguard Worker			}
6925*49cdfc7eSAndroid Build Coastguard Worker
6926*49cdfc7eSAndroid Build Coastguard Worker			if ($paren_space =~ /\n/) {
6927*49cdfc7eSAndroid Build Coastguard Worker				WARN("FUNCTION_ARGUMENTS",
6928*49cdfc7eSAndroid Build Coastguard Worker				     "arguments for function declarations should follow identifier\n" . $herecurr);
6929*49cdfc7eSAndroid Build Coastguard Worker			}
6930*49cdfc7eSAndroid Build Coastguard Worker
6931*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($realfile =~ /\.c$/ && defined $stat &&
6932*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^.\s*extern\s+/)
6933*49cdfc7eSAndroid Build Coastguard Worker		{
6934*49cdfc7eSAndroid Build Coastguard Worker			WARN("AVOID_EXTERNS",
6935*49cdfc7eSAndroid Build Coastguard Worker			     "externs should be avoided in .c files\n" .  $herecurr);
6936*49cdfc7eSAndroid Build Coastguard Worker		}
6937*49cdfc7eSAndroid Build Coastguard Worker
6938*49cdfc7eSAndroid Build Coastguard Worker# check for function declarations that have arguments without identifier names
6939*49cdfc7eSAndroid Build Coastguard Worker		if (defined $stat &&
6940*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
6941*49cdfc7eSAndroid Build Coastguard Worker		    $1 ne "void") {
6942*49cdfc7eSAndroid Build Coastguard Worker			my $args = trim($1);
6943*49cdfc7eSAndroid Build Coastguard Worker			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
6944*49cdfc7eSAndroid Build Coastguard Worker				my $arg = trim($1);
6945*49cdfc7eSAndroid Build Coastguard Worker				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
6946*49cdfc7eSAndroid Build Coastguard Worker					WARN("FUNCTION_ARGUMENTS",
6947*49cdfc7eSAndroid Build Coastguard Worker					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
6948*49cdfc7eSAndroid Build Coastguard Worker				}
6949*49cdfc7eSAndroid Build Coastguard Worker			}
6950*49cdfc7eSAndroid Build Coastguard Worker		}
6951*49cdfc7eSAndroid Build Coastguard Worker
6952*49cdfc7eSAndroid Build Coastguard Worker# check for function definitions
6953*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6954*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
6955*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
6956*49cdfc7eSAndroid Build Coastguard Worker			$context_function = $1;
6957*49cdfc7eSAndroid Build Coastguard Worker
6958*49cdfc7eSAndroid Build Coastguard Worker# check for multiline function definition with misplaced open brace
6959*49cdfc7eSAndroid Build Coastguard Worker			my $ok = 0;
6960*49cdfc7eSAndroid Build Coastguard Worker			my $cnt = statement_rawlines($stat);
6961*49cdfc7eSAndroid Build Coastguard Worker			my $herectx = $here . "\n";
6962*49cdfc7eSAndroid Build Coastguard Worker			for (my $n = 0; $n < $cnt; $n++) {
6963*49cdfc7eSAndroid Build Coastguard Worker				my $rl = raw_line($linenr, $n);
6964*49cdfc7eSAndroid Build Coastguard Worker				$herectx .=  $rl . "\n";
6965*49cdfc7eSAndroid Build Coastguard Worker				$ok = 1 if ($rl =~ /^[ \+]\{/);
6966*49cdfc7eSAndroid Build Coastguard Worker				$ok = 1 if ($rl =~ /\{/ && $n == 0);
6967*49cdfc7eSAndroid Build Coastguard Worker				last if $rl =~ /^[ \+].*\{/;
6968*49cdfc7eSAndroid Build Coastguard Worker			}
6969*49cdfc7eSAndroid Build Coastguard Worker			if (!$ok) {
6970*49cdfc7eSAndroid Build Coastguard Worker				ERROR("OPEN_BRACE",
6971*49cdfc7eSAndroid Build Coastguard Worker				      "open brace '{' following function definitions go on the next line\n" . $herectx);
6972*49cdfc7eSAndroid Build Coastguard Worker			}
6973*49cdfc7eSAndroid Build Coastguard Worker		}
6974*49cdfc7eSAndroid Build Coastguard Worker
6975*49cdfc7eSAndroid Build Coastguard Worker# checks for new __setup's
6976*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /\b__setup\("([^"]*)"/) {
6977*49cdfc7eSAndroid Build Coastguard Worker			my $name = $1;
6978*49cdfc7eSAndroid Build Coastguard Worker
6979*49cdfc7eSAndroid Build Coastguard Worker			if (!grep(/$name/, @setup_docs)) {
6980*49cdfc7eSAndroid Build Coastguard Worker				CHK("UNDOCUMENTED_SETUP",
6981*49cdfc7eSAndroid Build Coastguard Worker				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
6982*49cdfc7eSAndroid Build Coastguard Worker			}
6983*49cdfc7eSAndroid Build Coastguard Worker		}
6984*49cdfc7eSAndroid Build Coastguard Worker
6985*49cdfc7eSAndroid Build Coastguard Worker# check for pointless casting of alloc functions
6986*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
6987*49cdfc7eSAndroid Build Coastguard Worker			WARN("UNNECESSARY_CASTS",
6988*49cdfc7eSAndroid Build Coastguard Worker			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
6989*49cdfc7eSAndroid Build Coastguard Worker		}
6990*49cdfc7eSAndroid Build Coastguard Worker
6991*49cdfc7eSAndroid Build Coastguard Worker# alloc style
6992*49cdfc7eSAndroid Build Coastguard Worker# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
6993*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
6994*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
6995*49cdfc7eSAndroid Build Coastguard Worker			CHK("ALLOC_SIZEOF_STRUCT",
6996*49cdfc7eSAndroid Build Coastguard Worker			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
6997*49cdfc7eSAndroid Build Coastguard Worker		}
6998*49cdfc7eSAndroid Build Coastguard Worker
6999*49cdfc7eSAndroid Build Coastguard Worker# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
7000*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
7001*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
7002*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
7003*49cdfc7eSAndroid Build Coastguard Worker			my $oldfunc = $3;
7004*49cdfc7eSAndroid Build Coastguard Worker			my $a1 = $4;
7005*49cdfc7eSAndroid Build Coastguard Worker			my $a2 = $10;
7006*49cdfc7eSAndroid Build Coastguard Worker			my $newfunc = "kmalloc_array";
7007*49cdfc7eSAndroid Build Coastguard Worker			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
7008*49cdfc7eSAndroid Build Coastguard Worker			my $r1 = $a1;
7009*49cdfc7eSAndroid Build Coastguard Worker			my $r2 = $a2;
7010*49cdfc7eSAndroid Build Coastguard Worker			if ($a1 =~ /^sizeof\s*\S/) {
7011*49cdfc7eSAndroid Build Coastguard Worker				$r1 = $a2;
7012*49cdfc7eSAndroid Build Coastguard Worker				$r2 = $a1;
7013*49cdfc7eSAndroid Build Coastguard Worker			}
7014*49cdfc7eSAndroid Build Coastguard Worker			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7015*49cdfc7eSAndroid Build Coastguard Worker			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
7016*49cdfc7eSAndroid Build Coastguard Worker				my $cnt = statement_rawlines($stat);
7017*49cdfc7eSAndroid Build Coastguard Worker				my $herectx = get_stat_here($linenr, $cnt, $here);
7018*49cdfc7eSAndroid Build Coastguard Worker
7019*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("ALLOC_WITH_MULTIPLY",
7020*49cdfc7eSAndroid Build Coastguard Worker					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
7021*49cdfc7eSAndroid Build Coastguard Worker				    $cnt == 1 &&
7022*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
7023*49cdfc7eSAndroid Build Coastguard Worker					$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;
7024*49cdfc7eSAndroid Build Coastguard Worker				}
7025*49cdfc7eSAndroid Build Coastguard Worker			}
7026*49cdfc7eSAndroid Build Coastguard Worker		}
7027*49cdfc7eSAndroid Build Coastguard Worker
7028*49cdfc7eSAndroid Build Coastguard Worker# check for krealloc arg reuse
7029*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
7030*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
7031*49cdfc7eSAndroid Build Coastguard Worker		    $1 eq $3) {
7032*49cdfc7eSAndroid Build Coastguard Worker			WARN("KREALLOC_ARG_REUSE",
7033*49cdfc7eSAndroid Build Coastguard Worker			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7034*49cdfc7eSAndroid Build Coastguard Worker		}
7035*49cdfc7eSAndroid Build Coastguard Worker
7036*49cdfc7eSAndroid Build Coastguard Worker# check for alloc argument mismatch
7037*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b((?:devm_)?(?:kcalloc|kmalloc_array))\s*\(\s*sizeof\b/) {
7038*49cdfc7eSAndroid Build Coastguard Worker			WARN("ALLOC_ARRAY_ARGS",
7039*49cdfc7eSAndroid Build Coastguard Worker			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
7040*49cdfc7eSAndroid Build Coastguard Worker		}
7041*49cdfc7eSAndroid Build Coastguard Worker
7042*49cdfc7eSAndroid Build Coastguard Worker# check for multiple semicolons
7043*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /;\s*;\s*$/) {
7044*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("ONE_SEMICOLON",
7045*49cdfc7eSAndroid Build Coastguard Worker				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7046*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
7047*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
7048*49cdfc7eSAndroid Build Coastguard Worker			}
7049*49cdfc7eSAndroid Build Coastguard Worker		}
7050*49cdfc7eSAndroid Build Coastguard Worker
7051*49cdfc7eSAndroid Build Coastguard Worker# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7052*49cdfc7eSAndroid Build Coastguard Worker		if ($realfile !~ m@^include/uapi/@ &&
7053*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
7054*49cdfc7eSAndroid Build Coastguard Worker			my $ull = "";
7055*49cdfc7eSAndroid Build Coastguard Worker			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
7056*49cdfc7eSAndroid Build Coastguard Worker			if (CHK("BIT_MACRO",
7057*49cdfc7eSAndroid Build Coastguard Worker				"Prefer using the BIT$ull macro\n" . $herecurr) &&
7058*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
7059*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
7060*49cdfc7eSAndroid Build Coastguard Worker			}
7061*49cdfc7eSAndroid Build Coastguard Worker		}
7062*49cdfc7eSAndroid Build Coastguard Worker
7063*49cdfc7eSAndroid Build Coastguard Worker# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
7064*49cdfc7eSAndroid Build Coastguard Worker		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
7065*49cdfc7eSAndroid Build Coastguard Worker			WARN("IS_ENABLED_CONFIG",
7066*49cdfc7eSAndroid Build Coastguard Worker			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
7067*49cdfc7eSAndroid Build Coastguard Worker		}
7068*49cdfc7eSAndroid Build Coastguard Worker
7069*49cdfc7eSAndroid Build Coastguard Worker# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
7070*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
7071*49cdfc7eSAndroid Build Coastguard Worker			my $config = $1;
7072*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("PREFER_IS_ENABLED",
7073*49cdfc7eSAndroid Build Coastguard Worker				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
7074*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
7075*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
7076*49cdfc7eSAndroid Build Coastguard Worker			}
7077*49cdfc7eSAndroid Build Coastguard Worker		}
7078*49cdfc7eSAndroid Build Coastguard Worker
7079*49cdfc7eSAndroid Build Coastguard Worker# check for /* fallthrough */ like comment, prefer fallthrough;
7080*49cdfc7eSAndroid Build Coastguard Worker		my @fallthroughs = (
7081*49cdfc7eSAndroid Build Coastguard Worker			'fallthrough',
7082*49cdfc7eSAndroid Build Coastguard Worker			'@fallthrough@',
7083*49cdfc7eSAndroid Build Coastguard Worker			'lint -fallthrough[ \t]*',
7084*49cdfc7eSAndroid Build Coastguard Worker			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7085*49cdfc7eSAndroid Build Coastguard Worker			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7086*49cdfc7eSAndroid Build Coastguard Worker			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7087*49cdfc7eSAndroid Build Coastguard Worker			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7088*49cdfc7eSAndroid Build Coastguard Worker		    );
7089*49cdfc7eSAndroid Build Coastguard Worker		if ($raw_comment ne '') {
7090*49cdfc7eSAndroid Build Coastguard Worker			foreach my $ft (@fallthroughs) {
7091*49cdfc7eSAndroid Build Coastguard Worker				if ($raw_comment =~ /$ft/) {
7092*49cdfc7eSAndroid Build Coastguard Worker					my $msg_level = \&WARN;
7093*49cdfc7eSAndroid Build Coastguard Worker					$msg_level = \&CHK if ($file);
7094*49cdfc7eSAndroid Build Coastguard Worker					&{$msg_level}("PREFER_FALLTHROUGH",
7095*49cdfc7eSAndroid Build Coastguard Worker						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7096*49cdfc7eSAndroid Build Coastguard Worker					last;
7097*49cdfc7eSAndroid Build Coastguard Worker				}
7098*49cdfc7eSAndroid Build Coastguard Worker			}
7099*49cdfc7eSAndroid Build Coastguard Worker		}
7100*49cdfc7eSAndroid Build Coastguard Worker
7101*49cdfc7eSAndroid Build Coastguard Worker# check for switch/default statements without a break;
7102*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
7103*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
7104*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
7105*49cdfc7eSAndroid Build Coastguard Worker			my $cnt = statement_rawlines($stat);
7106*49cdfc7eSAndroid Build Coastguard Worker			my $herectx = get_stat_here($linenr, $cnt, $here);
7107*49cdfc7eSAndroid Build Coastguard Worker
7108*49cdfc7eSAndroid Build Coastguard Worker			WARN("DEFAULT_NO_BREAK",
7109*49cdfc7eSAndroid Build Coastguard Worker			     "switch default: should use break\n" . $herectx);
7110*49cdfc7eSAndroid Build Coastguard Worker		}
7111*49cdfc7eSAndroid Build Coastguard Worker
7112*49cdfc7eSAndroid Build Coastguard Worker# check for gcc specific __FUNCTION__
7113*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b__FUNCTION__\b/) {
7114*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("USE_FUNC",
7115*49cdfc7eSAndroid Build Coastguard Worker				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
7116*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
7117*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
7118*49cdfc7eSAndroid Build Coastguard Worker			}
7119*49cdfc7eSAndroid Build Coastguard Worker		}
7120*49cdfc7eSAndroid Build Coastguard Worker
7121*49cdfc7eSAndroid Build Coastguard Worker# check for uses of __DATE__, __TIME__, __TIMESTAMP__
7122*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
7123*49cdfc7eSAndroid Build Coastguard Worker			ERROR("DATE_TIME",
7124*49cdfc7eSAndroid Build Coastguard Worker			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
7125*49cdfc7eSAndroid Build Coastguard Worker		}
7126*49cdfc7eSAndroid Build Coastguard Worker
7127*49cdfc7eSAndroid Build Coastguard Worker# check for use of yield()
7128*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\byield\s*\(\s*\)/) {
7129*49cdfc7eSAndroid Build Coastguard Worker			WARN("YIELD",
7130*49cdfc7eSAndroid Build Coastguard Worker			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
7131*49cdfc7eSAndroid Build Coastguard Worker		}
7132*49cdfc7eSAndroid Build Coastguard Worker
7133*49cdfc7eSAndroid Build Coastguard Worker# check for comparisons against true and false
7134*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7135*49cdfc7eSAndroid Build Coastguard Worker			my $lead = $1;
7136*49cdfc7eSAndroid Build Coastguard Worker			my $arg = $2;
7137*49cdfc7eSAndroid Build Coastguard Worker			my $test = $3;
7138*49cdfc7eSAndroid Build Coastguard Worker			my $otype = $4;
7139*49cdfc7eSAndroid Build Coastguard Worker			my $trail = $5;
7140*49cdfc7eSAndroid Build Coastguard Worker			my $op = "!";
7141*49cdfc7eSAndroid Build Coastguard Worker
7142*49cdfc7eSAndroid Build Coastguard Worker			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7143*49cdfc7eSAndroid Build Coastguard Worker
7144*49cdfc7eSAndroid Build Coastguard Worker			my $type = lc($otype);
7145*49cdfc7eSAndroid Build Coastguard Worker			if ($type =~ /^(?:true|false)$/) {
7146*49cdfc7eSAndroid Build Coastguard Worker				if (("$test" eq "==" && "$type" eq "true") ||
7147*49cdfc7eSAndroid Build Coastguard Worker				    ("$test" eq "!=" && "$type" eq "false")) {
7148*49cdfc7eSAndroid Build Coastguard Worker					$op = "";
7149*49cdfc7eSAndroid Build Coastguard Worker				}
7150*49cdfc7eSAndroid Build Coastguard Worker
7151*49cdfc7eSAndroid Build Coastguard Worker				CHK("BOOL_COMPARISON",
7152*49cdfc7eSAndroid Build Coastguard Worker				    "Using comparison to $otype is error prone\n" . $herecurr);
7153*49cdfc7eSAndroid Build Coastguard Worker
7154*49cdfc7eSAndroid Build Coastguard Worker## maybe suggesting a correct construct would better
7155*49cdfc7eSAndroid Build Coastguard Worker##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7156*49cdfc7eSAndroid Build Coastguard Worker
7157*49cdfc7eSAndroid Build Coastguard Worker			}
7158*49cdfc7eSAndroid Build Coastguard Worker		}
7159*49cdfc7eSAndroid Build Coastguard Worker
7160*49cdfc7eSAndroid Build Coastguard Worker# check for semaphores initialized locked
7161*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
7162*49cdfc7eSAndroid Build Coastguard Worker			WARN("CONSIDER_COMPLETION",
7163*49cdfc7eSAndroid Build Coastguard Worker			     "consider using a completion\n" . $herecurr);
7164*49cdfc7eSAndroid Build Coastguard Worker		}
7165*49cdfc7eSAndroid Build Coastguard Worker
7166*49cdfc7eSAndroid Build Coastguard Worker# recommend kstrto* over simple_strto* and strict_strto*
7167*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
7168*49cdfc7eSAndroid Build Coastguard Worker			WARN("CONSIDER_KSTRTO",
7169*49cdfc7eSAndroid Build Coastguard Worker			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
7170*49cdfc7eSAndroid Build Coastguard Worker		}
7171*49cdfc7eSAndroid Build Coastguard Worker
7172*49cdfc7eSAndroid Build Coastguard Worker# check for __initcall(), use device_initcall() explicitly or more appropriate function please
7173*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*__initcall\s*\(/) {
7174*49cdfc7eSAndroid Build Coastguard Worker			WARN("USE_DEVICE_INITCALL",
7175*49cdfc7eSAndroid Build Coastguard Worker			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
7176*49cdfc7eSAndroid Build Coastguard Worker		}
7177*49cdfc7eSAndroid Build Coastguard Worker
7178*49cdfc7eSAndroid Build Coastguard Worker# check for spin_is_locked(), suggest lockdep instead
7179*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bspin_is_locked\(/) {
7180*49cdfc7eSAndroid Build Coastguard Worker			WARN("USE_LOCKDEP",
7181*49cdfc7eSAndroid Build Coastguard Worker			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
7182*49cdfc7eSAndroid Build Coastguard Worker		}
7183*49cdfc7eSAndroid Build Coastguard Worker
7184*49cdfc7eSAndroid Build Coastguard Worker# check for deprecated apis
7185*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
7186*49cdfc7eSAndroid Build Coastguard Worker			my $deprecated_api = $1;
7187*49cdfc7eSAndroid Build Coastguard Worker			my $new_api = $deprecated_apis{$deprecated_api};
7188*49cdfc7eSAndroid Build Coastguard Worker			WARN("DEPRECATED_API",
7189*49cdfc7eSAndroid Build Coastguard Worker			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
7190*49cdfc7eSAndroid Build Coastguard Worker		}
7191*49cdfc7eSAndroid Build Coastguard Worker
7192*49cdfc7eSAndroid Build Coastguard Worker# check for various structs that are normally const (ops, kgdb, device_tree)
7193*49cdfc7eSAndroid Build Coastguard Worker# and avoid what seem like struct definitions 'struct foo {'
7194*49cdfc7eSAndroid Build Coastguard Worker		if (defined($const_structs) &&
7195*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /\bconst\b/ &&
7196*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7197*49cdfc7eSAndroid Build Coastguard Worker			WARN("CONST_STRUCT",
7198*49cdfc7eSAndroid Build Coastguard Worker			     "struct $1 should normally be const\n" . $herecurr);
7199*49cdfc7eSAndroid Build Coastguard Worker		}
7200*49cdfc7eSAndroid Build Coastguard Worker
7201*49cdfc7eSAndroid Build Coastguard Worker# use of NR_CPUS is usually wrong
7202*49cdfc7eSAndroid Build Coastguard Worker# ignore definitions of NR_CPUS and usage to define arrays as likely right
7203*49cdfc7eSAndroid Build Coastguard Worker# ignore designated initializers using NR_CPUS
7204*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bNR_CPUS\b/ &&
7205*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7206*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7207*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7208*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
7209*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
7210*49cdfc7eSAndroid Build Coastguard Worker		    $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
7211*49cdfc7eSAndroid Build Coastguard Worker		{
7212*49cdfc7eSAndroid Build Coastguard Worker			WARN("NR_CPUS",
7213*49cdfc7eSAndroid Build Coastguard Worker			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7214*49cdfc7eSAndroid Build Coastguard Worker		}
7215*49cdfc7eSAndroid Build Coastguard Worker
7216*49cdfc7eSAndroid Build Coastguard Worker# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
7217*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
7218*49cdfc7eSAndroid Build Coastguard Worker			ERROR("DEFINE_ARCH_HAS",
7219*49cdfc7eSAndroid Build Coastguard Worker			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
7220*49cdfc7eSAndroid Build Coastguard Worker		}
7221*49cdfc7eSAndroid Build Coastguard Worker
7222*49cdfc7eSAndroid Build Coastguard Worker# likely/unlikely comparisons similar to "(likely(foo) > 0)"
7223*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
7224*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7225*49cdfc7eSAndroid Build Coastguard Worker			WARN("LIKELY_MISUSE",
7226*49cdfc7eSAndroid Build Coastguard Worker			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7227*49cdfc7eSAndroid Build Coastguard Worker		}
7228*49cdfc7eSAndroid Build Coastguard Worker
7229*49cdfc7eSAndroid Build Coastguard Worker# return sysfs_emit(foo, fmt, ...) fmt without newline
7230*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7231*49cdfc7eSAndroid Build Coastguard Worker		    substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7232*49cdfc7eSAndroid Build Coastguard Worker			my $offset = $+[6] - 1;
7233*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("SYSFS_EMIT",
7234*49cdfc7eSAndroid Build Coastguard Worker				 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7235*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
7236*49cdfc7eSAndroid Build Coastguard Worker				substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7237*49cdfc7eSAndroid Build Coastguard Worker			}
7238*49cdfc7eSAndroid Build Coastguard Worker		}
7239*49cdfc7eSAndroid Build Coastguard Worker
7240*49cdfc7eSAndroid Build Coastguard Worker# nested likely/unlikely calls
7241*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7242*49cdfc7eSAndroid Build Coastguard Worker			WARN("LIKELY_MISUSE",
7243*49cdfc7eSAndroid Build Coastguard Worker			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7244*49cdfc7eSAndroid Build Coastguard Worker		}
7245*49cdfc7eSAndroid Build Coastguard Worker
7246*49cdfc7eSAndroid Build Coastguard Worker# whine mightly about in_atomic
7247*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bin_atomic\s*\(/) {
7248*49cdfc7eSAndroid Build Coastguard Worker			if ($realfile =~ m@^drivers/@) {
7249*49cdfc7eSAndroid Build Coastguard Worker				ERROR("IN_ATOMIC",
7250*49cdfc7eSAndroid Build Coastguard Worker				      "do not use in_atomic in drivers\n" . $herecurr);
7251*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($realfile !~ m@^kernel/@) {
7252*49cdfc7eSAndroid Build Coastguard Worker				WARN("IN_ATOMIC",
7253*49cdfc7eSAndroid Build Coastguard Worker				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7254*49cdfc7eSAndroid Build Coastguard Worker			}
7255*49cdfc7eSAndroid Build Coastguard Worker		}
7256*49cdfc7eSAndroid Build Coastguard Worker
7257*49cdfc7eSAndroid Build Coastguard Worker# check for lockdep_set_novalidate_class
7258*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
7259*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
7260*49cdfc7eSAndroid Build Coastguard Worker			if ($realfile !~ m@^kernel/lockdep@ &&
7261*49cdfc7eSAndroid Build Coastguard Worker			    $realfile !~ m@^include/linux/lockdep@ &&
7262*49cdfc7eSAndroid Build Coastguard Worker			    $realfile !~ m@^drivers/base/core@) {
7263*49cdfc7eSAndroid Build Coastguard Worker				ERROR("LOCKDEP",
7264*49cdfc7eSAndroid Build Coastguard Worker				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
7265*49cdfc7eSAndroid Build Coastguard Worker			}
7266*49cdfc7eSAndroid Build Coastguard Worker		}
7267*49cdfc7eSAndroid Build Coastguard Worker
7268*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7269*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7270*49cdfc7eSAndroid Build Coastguard Worker			WARN("EXPORTED_WORLD_WRITABLE",
7271*49cdfc7eSAndroid Build Coastguard Worker			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
7272*49cdfc7eSAndroid Build Coastguard Worker		}
7273*49cdfc7eSAndroid Build Coastguard Worker
7274*49cdfc7eSAndroid Build Coastguard Worker# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
7275*49cdfc7eSAndroid Build Coastguard Worker# and whether or not function naming is typical and if
7276*49cdfc7eSAndroid Build Coastguard Worker# DEVICE_ATTR permissions uses are unusual too
7277*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
7278*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
7279*49cdfc7eSAndroid Build Coastguard Worker		    $stat =~ /\bDEVICE_ATTR\s*\(\s*(\w+)\s*,\s*\(?\s*(\s*(?:${multi_mode_perms_string_search}|0[0-7]{3,3})\s*)\s*\)?\s*,\s*(\w+)\s*,\s*(\w+)\s*\)/) {
7280*49cdfc7eSAndroid Build Coastguard Worker			my $var = $1;
7281*49cdfc7eSAndroid Build Coastguard Worker			my $perms = $2;
7282*49cdfc7eSAndroid Build Coastguard Worker			my $show = $3;
7283*49cdfc7eSAndroid Build Coastguard Worker			my $store = $4;
7284*49cdfc7eSAndroid Build Coastguard Worker			my $octal_perms = perms_to_octal($perms);
7285*49cdfc7eSAndroid Build Coastguard Worker			if ($show =~ /^${var}_show$/ &&
7286*49cdfc7eSAndroid Build Coastguard Worker			    $store =~ /^${var}_store$/ &&
7287*49cdfc7eSAndroid Build Coastguard Worker			    $octal_perms eq "0644") {
7288*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("DEVICE_ATTR_RW",
7289*49cdfc7eSAndroid Build Coastguard Worker					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
7290*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
7291*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
7292*49cdfc7eSAndroid Build Coastguard Worker				}
7293*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($show =~ /^${var}_show$/ &&
7294*49cdfc7eSAndroid Build Coastguard Worker				 $store =~ /^NULL$/ &&
7295*49cdfc7eSAndroid Build Coastguard Worker				 $octal_perms eq "0444") {
7296*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("DEVICE_ATTR_RO",
7297*49cdfc7eSAndroid Build Coastguard Worker					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
7298*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
7299*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
7300*49cdfc7eSAndroid Build Coastguard Worker				}
7301*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($show =~ /^NULL$/ &&
7302*49cdfc7eSAndroid Build Coastguard Worker				 $store =~ /^${var}_store$/ &&
7303*49cdfc7eSAndroid Build Coastguard Worker				 $octal_perms eq "0200") {
7304*49cdfc7eSAndroid Build Coastguard Worker				if (WARN("DEVICE_ATTR_WO",
7305*49cdfc7eSAndroid Build Coastguard Worker					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
7306*49cdfc7eSAndroid Build Coastguard Worker				    $fix) {
7307*49cdfc7eSAndroid Build Coastguard Worker					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
7308*49cdfc7eSAndroid Build Coastguard Worker				}
7309*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($octal_perms eq "0644" ||
7310*49cdfc7eSAndroid Build Coastguard Worker				 $octal_perms eq "0444" ||
7311*49cdfc7eSAndroid Build Coastguard Worker				 $octal_perms eq "0200") {
7312*49cdfc7eSAndroid Build Coastguard Worker				my $newshow = "$show";
7313*49cdfc7eSAndroid Build Coastguard Worker				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
7314*49cdfc7eSAndroid Build Coastguard Worker				my $newstore = $store;
7315*49cdfc7eSAndroid Build Coastguard Worker				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
7316*49cdfc7eSAndroid Build Coastguard Worker				my $rename = "";
7317*49cdfc7eSAndroid Build Coastguard Worker				if ($show ne $newshow) {
7318*49cdfc7eSAndroid Build Coastguard Worker					$rename .= " '$show' to '$newshow'";
7319*49cdfc7eSAndroid Build Coastguard Worker				}
7320*49cdfc7eSAndroid Build Coastguard Worker				if ($store ne $newstore) {
7321*49cdfc7eSAndroid Build Coastguard Worker					$rename .= " '$store' to '$newstore'";
7322*49cdfc7eSAndroid Build Coastguard Worker				}
7323*49cdfc7eSAndroid Build Coastguard Worker				WARN("DEVICE_ATTR_FUNCTIONS",
7324*49cdfc7eSAndroid Build Coastguard Worker				     "Consider renaming function(s)$rename\n" . $herecurr);
7325*49cdfc7eSAndroid Build Coastguard Worker			} else {
7326*49cdfc7eSAndroid Build Coastguard Worker				WARN("DEVICE_ATTR_PERMS",
7327*49cdfc7eSAndroid Build Coastguard Worker				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
7328*49cdfc7eSAndroid Build Coastguard Worker			}
7329*49cdfc7eSAndroid Build Coastguard Worker		}
7330*49cdfc7eSAndroid Build Coastguard Worker
7331*49cdfc7eSAndroid Build Coastguard Worker# Mode permission misuses where it seems decimal should be octal
7332*49cdfc7eSAndroid Build Coastguard Worker# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
7333*49cdfc7eSAndroid Build Coastguard Worker# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
7334*49cdfc7eSAndroid Build Coastguard Worker#   specific definition of not visible in sysfs.
7335*49cdfc7eSAndroid Build Coastguard Worker# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
7336*49cdfc7eSAndroid Build Coastguard Worker#   use the default permissions
7337*49cdfc7eSAndroid Build Coastguard Worker		if ($perl_version_ok &&
7338*49cdfc7eSAndroid Build Coastguard Worker		    defined $stat &&
7339*49cdfc7eSAndroid Build Coastguard Worker		    $line =~ /$mode_perms_search/) {
7340*49cdfc7eSAndroid Build Coastguard Worker			foreach my $entry (@mode_permission_funcs) {
7341*49cdfc7eSAndroid Build Coastguard Worker				my $func = $entry->[0];
7342*49cdfc7eSAndroid Build Coastguard Worker				my $arg_pos = $entry->[1];
7343*49cdfc7eSAndroid Build Coastguard Worker
7344*49cdfc7eSAndroid Build Coastguard Worker				my $lc = $stat =~ tr@\n@@;
7345*49cdfc7eSAndroid Build Coastguard Worker				$lc = $lc + $linenr;
7346*49cdfc7eSAndroid Build Coastguard Worker				my $stat_real = get_stat_real($linenr, $lc);
7347*49cdfc7eSAndroid Build Coastguard Worker
7348*49cdfc7eSAndroid Build Coastguard Worker				my $skip_args = "";
7349*49cdfc7eSAndroid Build Coastguard Worker				if ($arg_pos > 1) {
7350*49cdfc7eSAndroid Build Coastguard Worker					$arg_pos--;
7351*49cdfc7eSAndroid Build Coastguard Worker					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
7352*49cdfc7eSAndroid Build Coastguard Worker				}
7353*49cdfc7eSAndroid Build Coastguard Worker				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7354*49cdfc7eSAndroid Build Coastguard Worker				if ($stat =~ /$test/) {
7355*49cdfc7eSAndroid Build Coastguard Worker					my $val = $1;
7356*49cdfc7eSAndroid Build Coastguard Worker					$val = $6 if ($skip_args ne "");
7357*49cdfc7eSAndroid Build Coastguard Worker					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
7358*49cdfc7eSAndroid Build Coastguard Worker					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
7359*49cdfc7eSAndroid Build Coastguard Worker					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
7360*49cdfc7eSAndroid Build Coastguard Worker						ERROR("NON_OCTAL_PERMISSIONS",
7361*49cdfc7eSAndroid Build Coastguard Worker						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7362*49cdfc7eSAndroid Build Coastguard Worker					}
7363*49cdfc7eSAndroid Build Coastguard Worker					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7364*49cdfc7eSAndroid Build Coastguard Worker						ERROR("EXPORTED_WORLD_WRITABLE",
7365*49cdfc7eSAndroid Build Coastguard Worker						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
7366*49cdfc7eSAndroid Build Coastguard Worker					}
7367*49cdfc7eSAndroid Build Coastguard Worker				}
7368*49cdfc7eSAndroid Build Coastguard Worker			}
7369*49cdfc7eSAndroid Build Coastguard Worker		}
7370*49cdfc7eSAndroid Build Coastguard Worker
7371*49cdfc7eSAndroid Build Coastguard Worker# check for uses of S_<PERMS> that could be octal for readability
7372*49cdfc7eSAndroid Build Coastguard Worker		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
7373*49cdfc7eSAndroid Build Coastguard Worker			my $oval = $1;
7374*49cdfc7eSAndroid Build Coastguard Worker			my $octal = perms_to_octal($oval);
7375*49cdfc7eSAndroid Build Coastguard Worker			if (WARN("SYMBOLIC_PERMS",
7376*49cdfc7eSAndroid Build Coastguard Worker				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7377*49cdfc7eSAndroid Build Coastguard Worker			    $fix) {
7378*49cdfc7eSAndroid Build Coastguard Worker				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
7379*49cdfc7eSAndroid Build Coastguard Worker			}
7380*49cdfc7eSAndroid Build Coastguard Worker		}
7381*49cdfc7eSAndroid Build Coastguard Worker
7382*49cdfc7eSAndroid Build Coastguard Worker# validate content of MODULE_LICENSE against list from include/linux/module.h
7383*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
7384*49cdfc7eSAndroid Build Coastguard Worker			my $extracted_string = get_quoted_string($line, $rawline);
7385*49cdfc7eSAndroid Build Coastguard Worker			my $valid_licenses = qr{
7386*49cdfc7eSAndroid Build Coastguard Worker						GPL|
7387*49cdfc7eSAndroid Build Coastguard Worker						GPL\ v2|
7388*49cdfc7eSAndroid Build Coastguard Worker						GPL\ and\ additional\ rights|
7389*49cdfc7eSAndroid Build Coastguard Worker						Dual\ BSD/GPL|
7390*49cdfc7eSAndroid Build Coastguard Worker						Dual\ MIT/GPL|
7391*49cdfc7eSAndroid Build Coastguard Worker						Dual\ MPL/GPL|
7392*49cdfc7eSAndroid Build Coastguard Worker						Proprietary
7393*49cdfc7eSAndroid Build Coastguard Worker					}x;
7394*49cdfc7eSAndroid Build Coastguard Worker			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
7395*49cdfc7eSAndroid Build Coastguard Worker				WARN("MODULE_LICENSE",
7396*49cdfc7eSAndroid Build Coastguard Worker				     "unknown module license " . $extracted_string . "\n" . $herecurr);
7397*49cdfc7eSAndroid Build Coastguard Worker			}
7398*49cdfc7eSAndroid Build Coastguard Worker		}
7399*49cdfc7eSAndroid Build Coastguard Worker
7400*49cdfc7eSAndroid Build Coastguard Worker# check for sysctl duplicate constants
7401*49cdfc7eSAndroid Build Coastguard Worker		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
7402*49cdfc7eSAndroid Build Coastguard Worker			WARN("DUPLICATED_SYSCTL_CONST",
7403*49cdfc7eSAndroid Build Coastguard Worker				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
7404*49cdfc7eSAndroid Build Coastguard Worker		}
7405*49cdfc7eSAndroid Build Coastguard Worker	}
7406*49cdfc7eSAndroid Build Coastguard Worker
7407*49cdfc7eSAndroid Build Coastguard Worker	# If we have no input at all, then there is nothing to report on
7408*49cdfc7eSAndroid Build Coastguard Worker	# so just keep quiet.
7409*49cdfc7eSAndroid Build Coastguard Worker	if ($#rawlines == -1) {
7410*49cdfc7eSAndroid Build Coastguard Worker		exit(0);
7411*49cdfc7eSAndroid Build Coastguard Worker	}
7412*49cdfc7eSAndroid Build Coastguard Worker
7413*49cdfc7eSAndroid Build Coastguard Worker	# In mailback mode only produce a report in the negative, for
7414*49cdfc7eSAndroid Build Coastguard Worker	# things that appear to be patches.
7415*49cdfc7eSAndroid Build Coastguard Worker	if ($mailback && ($clean == 1 || !$is_patch)) {
7416*49cdfc7eSAndroid Build Coastguard Worker		exit(0);
7417*49cdfc7eSAndroid Build Coastguard Worker	}
7418*49cdfc7eSAndroid Build Coastguard Worker
7419*49cdfc7eSAndroid Build Coastguard Worker	# This is not a patch, and we are in 'no-patch' mode so
7420*49cdfc7eSAndroid Build Coastguard Worker	# just keep quiet.
7421*49cdfc7eSAndroid Build Coastguard Worker	if (!$chk_patch && !$is_patch) {
7422*49cdfc7eSAndroid Build Coastguard Worker		exit(0);
7423*49cdfc7eSAndroid Build Coastguard Worker	}
7424*49cdfc7eSAndroid Build Coastguard Worker
7425*49cdfc7eSAndroid Build Coastguard Worker	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7426*49cdfc7eSAndroid Build Coastguard Worker		ERROR("NOT_UNIFIED_DIFF",
7427*49cdfc7eSAndroid Build Coastguard Worker		      "Does not appear to be a unified-diff format patch\n");
7428*49cdfc7eSAndroid Build Coastguard Worker	}
7429*49cdfc7eSAndroid Build Coastguard Worker	if ($is_patch && $has_commit_log && $chk_signoff) {
7430*49cdfc7eSAndroid Build Coastguard Worker		if ($signoff == 0) {
7431*49cdfc7eSAndroid Build Coastguard Worker			ERROR("MISSING_SIGN_OFF",
7432*49cdfc7eSAndroid Build Coastguard Worker			      "Missing Signed-off-by: line(s)\n");
7433*49cdfc7eSAndroid Build Coastguard Worker		} elsif ($authorsignoff != 1) {
7434*49cdfc7eSAndroid Build Coastguard Worker			# authorsignoff values:
7435*49cdfc7eSAndroid Build Coastguard Worker			# 0 -> missing sign off
7436*49cdfc7eSAndroid Build Coastguard Worker			# 1 -> sign off identical
7437*49cdfc7eSAndroid Build Coastguard Worker			# 2 -> names and addresses match, comments mismatch
7438*49cdfc7eSAndroid Build Coastguard Worker			# 3 -> addresses match, names different
7439*49cdfc7eSAndroid Build Coastguard Worker			# 4 -> names match, addresses different
7440*49cdfc7eSAndroid Build Coastguard Worker			# 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
7441*49cdfc7eSAndroid Build Coastguard Worker
7442*49cdfc7eSAndroid Build Coastguard Worker			my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
7443*49cdfc7eSAndroid Build Coastguard Worker
7444*49cdfc7eSAndroid Build Coastguard Worker			if ($authorsignoff == 0) {
7445*49cdfc7eSAndroid Build Coastguard Worker				ERROR("NO_AUTHOR_SIGN_OFF",
7446*49cdfc7eSAndroid Build Coastguard Worker				      "Missing Signed-off-by: line by nominal patch author '$author'\n");
7447*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($authorsignoff == 2) {
7448*49cdfc7eSAndroid Build Coastguard Worker				CHK("FROM_SIGN_OFF_MISMATCH",
7449*49cdfc7eSAndroid Build Coastguard Worker				    "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
7450*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($authorsignoff == 3) {
7451*49cdfc7eSAndroid Build Coastguard Worker				WARN("FROM_SIGN_OFF_MISMATCH",
7452*49cdfc7eSAndroid Build Coastguard Worker				     "From:/Signed-off-by: email name mismatch: $sob_msg\n");
7453*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($authorsignoff == 4) {
7454*49cdfc7eSAndroid Build Coastguard Worker				WARN("FROM_SIGN_OFF_MISMATCH",
7455*49cdfc7eSAndroid Build Coastguard Worker				     "From:/Signed-off-by: email address mismatch: $sob_msg\n");
7456*49cdfc7eSAndroid Build Coastguard Worker			} elsif ($authorsignoff == 5) {
7457*49cdfc7eSAndroid Build Coastguard Worker				WARN("FROM_SIGN_OFF_MISMATCH",
7458*49cdfc7eSAndroid Build Coastguard Worker				     "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
7459*49cdfc7eSAndroid Build Coastguard Worker			}
7460*49cdfc7eSAndroid Build Coastguard Worker		}
7461*49cdfc7eSAndroid Build Coastguard Worker	}
7462*49cdfc7eSAndroid Build Coastguard Worker
7463*49cdfc7eSAndroid Build Coastguard Worker	print report_dump();
7464*49cdfc7eSAndroid Build Coastguard Worker	if ($summary && !($clean == 1 && $quiet == 1)) {
7465*49cdfc7eSAndroid Build Coastguard Worker		print "$filename " if ($summary_file);
7466*49cdfc7eSAndroid Build Coastguard Worker		print "total: $cnt_error errors, $cnt_warn warnings, " .
7467*49cdfc7eSAndroid Build Coastguard Worker			(($check)? "$cnt_chk checks, " : "") .
7468*49cdfc7eSAndroid Build Coastguard Worker			"$cnt_lines lines checked\n";
7469*49cdfc7eSAndroid Build Coastguard Worker	}
7470*49cdfc7eSAndroid Build Coastguard Worker
7471*49cdfc7eSAndroid Build Coastguard Worker	if ($quiet == 0) {
7472*49cdfc7eSAndroid Build Coastguard Worker		# If there were any defects found and not already fixing them
7473*49cdfc7eSAndroid Build Coastguard Worker		if (!$clean and !$fix) {
7474*49cdfc7eSAndroid Build Coastguard Worker			print << "EOM"
7475*49cdfc7eSAndroid Build Coastguard Worker
7476*49cdfc7eSAndroid Build Coastguard WorkerNOTE: For some of the reported defects, checkpatch may be able to
7477*49cdfc7eSAndroid Build Coastguard Worker      mechanically convert to the typical style using --fix or --fix-inplace.
7478*49cdfc7eSAndroid Build Coastguard WorkerEOM
7479*49cdfc7eSAndroid Build Coastguard Worker		}
7480*49cdfc7eSAndroid Build Coastguard Worker		# If there were whitespace errors which cleanpatch can fix
7481*49cdfc7eSAndroid Build Coastguard Worker		# then suggest that.
7482*49cdfc7eSAndroid Build Coastguard Worker		if ($rpt_cleaners) {
7483*49cdfc7eSAndroid Build Coastguard Worker			$rpt_cleaners = 0;
7484*49cdfc7eSAndroid Build Coastguard Worker			print << "EOM"
7485*49cdfc7eSAndroid Build Coastguard Worker
7486*49cdfc7eSAndroid Build Coastguard WorkerNOTE: Whitespace errors detected.
7487*49cdfc7eSAndroid Build Coastguard Worker      You may wish to use scripts/cleanpatch or scripts/cleanfile
7488*49cdfc7eSAndroid Build Coastguard WorkerEOM
7489*49cdfc7eSAndroid Build Coastguard Worker		}
7490*49cdfc7eSAndroid Build Coastguard Worker	}
7491*49cdfc7eSAndroid Build Coastguard Worker
7492*49cdfc7eSAndroid Build Coastguard Worker	if ($clean == 0 && $fix &&
7493*49cdfc7eSAndroid Build Coastguard Worker	    ("@rawlines" ne "@fixed" ||
7494*49cdfc7eSAndroid Build Coastguard Worker	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
7495*49cdfc7eSAndroid Build Coastguard Worker		my $newfile = $filename;
7496*49cdfc7eSAndroid Build Coastguard Worker		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
7497*49cdfc7eSAndroid Build Coastguard Worker		my $linecount = 0;
7498*49cdfc7eSAndroid Build Coastguard Worker		my $f;
7499*49cdfc7eSAndroid Build Coastguard Worker
7500*49cdfc7eSAndroid Build Coastguard Worker		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7501*49cdfc7eSAndroid Build Coastguard Worker
7502*49cdfc7eSAndroid Build Coastguard Worker		open($f, '>', $newfile)
7503*49cdfc7eSAndroid Build Coastguard Worker		    or die "$P: Can't open $newfile for write\n";
7504*49cdfc7eSAndroid Build Coastguard Worker		foreach my $fixed_line (@fixed) {
7505*49cdfc7eSAndroid Build Coastguard Worker			$linecount++;
7506*49cdfc7eSAndroid Build Coastguard Worker			if ($file) {
7507*49cdfc7eSAndroid Build Coastguard Worker				if ($linecount > 3) {
7508*49cdfc7eSAndroid Build Coastguard Worker					$fixed_line =~ s/^\+//;
7509*49cdfc7eSAndroid Build Coastguard Worker					print $f $fixed_line . "\n";
7510*49cdfc7eSAndroid Build Coastguard Worker				}
7511*49cdfc7eSAndroid Build Coastguard Worker			} else {
7512*49cdfc7eSAndroid Build Coastguard Worker				print $f $fixed_line . "\n";
7513*49cdfc7eSAndroid Build Coastguard Worker			}
7514*49cdfc7eSAndroid Build Coastguard Worker		}
7515*49cdfc7eSAndroid Build Coastguard Worker		close($f);
7516*49cdfc7eSAndroid Build Coastguard Worker
7517*49cdfc7eSAndroid Build Coastguard Worker		if (!$quiet) {
7518*49cdfc7eSAndroid Build Coastguard Worker			print << "EOM";
7519*49cdfc7eSAndroid Build Coastguard Worker
7520*49cdfc7eSAndroid Build Coastguard WorkerWrote EXPERIMENTAL --fix correction(s) to '$newfile'
7521*49cdfc7eSAndroid Build Coastguard Worker
7522*49cdfc7eSAndroid Build Coastguard WorkerDo _NOT_ trust the results written to this file.
7523*49cdfc7eSAndroid Build Coastguard WorkerDo _NOT_ submit these changes without inspecting them for correctness.
7524*49cdfc7eSAndroid Build Coastguard Worker
7525*49cdfc7eSAndroid Build Coastguard WorkerThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
7526*49cdfc7eSAndroid Build Coastguard WorkerNo warranties, expressed or implied...
7527*49cdfc7eSAndroid Build Coastguard WorkerEOM
7528*49cdfc7eSAndroid Build Coastguard Worker		}
7529*49cdfc7eSAndroid Build Coastguard Worker	}
7530*49cdfc7eSAndroid Build Coastguard Worker
7531*49cdfc7eSAndroid Build Coastguard Worker	if ($quiet == 0) {
7532*49cdfc7eSAndroid Build Coastguard Worker		print "\n";
7533*49cdfc7eSAndroid Build Coastguard Worker		if ($clean == 1) {
7534*49cdfc7eSAndroid Build Coastguard Worker			print "$vname has no obvious style problems and is ready for submission.\n";
7535*49cdfc7eSAndroid Build Coastguard Worker		} else {
7536*49cdfc7eSAndroid Build Coastguard Worker			print "$vname has style problems, please review.\n";
7537*49cdfc7eSAndroid Build Coastguard Worker		}
7538*49cdfc7eSAndroid Build Coastguard Worker	}
7539*49cdfc7eSAndroid Build Coastguard Worker	return $clean;
7540*49cdfc7eSAndroid Build Coastguard Worker}
7541