xref: /linux-6.15/scripts/checkpatch.pl (revision d5d6281a)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2882ea1d6SJoe Perches# SPDX-License-Identifier: GPL-2.0
3882ea1d6SJoe Perches#
4dbf004d7SDave Jones# (c) 2001, Dave Jones. (the file handling bit)
500df344fSAndy Whitcroft# (c) 2005, Joel Schopp <[email protected]> (the ugly bit)
62a5a2c25SAndy Whitcroft# (c) 2007,2008, Andy Whitcroft <[email protected]> (new conditions, test suite)
7015830beSAndy Whitcroft# (c) 2008-2010 Andy Whitcroft <[email protected]>
8882ea1d6SJoe Perches# (c) 2010-2018 Joe Perches <[email protected]>
90a920b5bSAndy Whitcroft
100a920b5bSAndy Whitcroftuse strict;
11cb77f0d6SKamil Rytarowskiuse warnings;
12c707a81dSJoe Perchesuse POSIX;
1336061e38SJoe Perchesuse File::Basename;
1436061e38SJoe Perchesuse Cwd 'abs_path';
1557230297SJoe Perchesuse Term::ANSIColor qw(:constants);
16cd261496SGeert Uytterhoevenuse Encode qw(decode encode);
170a920b5bSAndy Whitcroft
180a920b5bSAndy Whitcroftmy $P = $0;
1936061e38SJoe Perchesmy $D = dirname(abs_path($P));
200a920b5bSAndy Whitcroft
21000d1cc1SJoe Perchesmy $V = '0.32';
220a920b5bSAndy Whitcroft
230a920b5bSAndy Whitcroftuse Getopt::Long qw(:config no_auto_abbrev);
240a920b5bSAndy Whitcroft
250a920b5bSAndy Whitcroftmy $quiet = 0;
2652178ce0SDwaipayan Raymy $verbose = 0;
2752178ce0SDwaipayan Raymy %verbose_messages = ();
2852178ce0SDwaipayan Raymy %verbose_emitted = ();
290a920b5bSAndy Whitcroftmy $tree = 1;
300a920b5bSAndy Whitcroftmy $chk_signoff = 1;
31*d5d6281aSDan Carpentermy $chk_fixes_tag = 1;
320a920b5bSAndy Whitcroftmy $chk_patch = 1;
33773647a0SAndy Whitcroftmy $tst_only;
346c72ffaaSAndy Whitcroftmy $emacs = 0;
358905a67cSAndy Whitcroftmy $terse = 0;
3634d8815fSJoe Perchesmy $showfile = 0;
376c72ffaaSAndy Whitcroftmy $file = 0;
384a593c34SDu, Changbinmy $git = 0;
390dea9f1eSJoe Perchesmy %git_commits = ();
406c72ffaaSAndy Whitcroftmy $check = 0;
412ac73b4fSJoe Perchesmy $check_orig = 0;
428905a67cSAndy Whitcroftmy $summary = 1;
438905a67cSAndy Whitcroftmy $mailback = 0;
4413214adfSAndy Whitcroftmy $summary_file = 0;
45000d1cc1SJoe Perchesmy $show_types = 0;
463beb42ecSJoe Perchesmy $list_types = 0;
473705ce5bSJoe Perchesmy $fix = 0;
489624b8d6SJoe Perchesmy $fix_inplace = 0;
496c72ffaaSAndy Whitcroftmy $root;
500f7f635bSJoe Perchesmy $gitroot = $ENV{'GIT_DIR'};
510f7f635bSJoe Perches$gitroot = ".git" if !defined($gitroot);
52c2fdda0dSAndy Whitcroftmy %debug;
533445686aSJoe Perchesmy %camelcase = ();
5491bfe484SJoe Perchesmy %use_type = ();
5591bfe484SJoe Perchesmy @use = ();
5691bfe484SJoe Perchesmy %ignore_type = ();
57000d1cc1SJoe Perchesmy @ignore = ();
5877f5b10aSHannes Edermy $help = 0;
59000d1cc1SJoe Perchesmy $configuration_file = ".checkpatch.conf";
60bdc48fa1SJoe Perchesmy $max_line_length = 100;
61d62a201fSDave Hansenmy $ignore_perl_version = 0;
62d62a201fSDave Hansenmy $minimum_perl_version = 5.10.0;
6356193274SVadim Bendeburymy $min_conf_desc_length = 4;
6466b47b4aSKees Cookmy $spelling_file = "$D/spelling.txt";
65ebfd7d62SJoe Perchesmy $codespell = 0;
66f1a63678SMaxim Uvarovmy $codespellfile = "/usr/share/codespell/dictionary.txt";
670ee3e7b8SPeter Ujfalusimy $user_codespellfile = "";
68bf1fa1daSJoe Perchesmy $conststructsfile = "$D/const_structs.checkpatch";
6952178ce0SDwaipayan Raymy $docsfile = "$D/../Documentation/dev-tools/checkpatch.rst";
70ced69da1SQuentin Monnetmy $typedefsfile;
71737c0767SJohn Brooksmy $color = "auto";
7298005e8cSVadim Bendeburymy $allow_c99_comments = 1; # Can be overridden by --ignore C99_COMMENT_TOLERANCE
73dbbf869dSJoe Perches# git output parsing needs US English output, so first set backtick child process LANGUAGE
74dbbf869dSJoe Perchesmy $git_command ='export LANGUAGE=en_US.UTF-8; git';
75713a09deSAntonio Borneomy $tabsize = 8;
763e89ad85SJerome Forissiermy ${CONFIG_} = "CONFIG_";
7777f5b10aSHannes Eder
785b2c7334SJim Cromiemy %maybe_linker_symbol; # for externs in c exceptions, when seen in *vmlinux.lds.h
795b2c7334SJim Cromie
8077f5b10aSHannes Edersub help {
8177f5b10aSHannes Eder	my ($exitcode) = @_;
8277f5b10aSHannes Eder
8377f5b10aSHannes Eder	print << "EOM";
8477f5b10aSHannes EderUsage: $P [OPTION]... [FILE]...
8577f5b10aSHannes EderVersion: $V
8677f5b10aSHannes Eder
8777f5b10aSHannes EderOptions:
8877f5b10aSHannes Eder  -q, --quiet                quiet
8952178ce0SDwaipayan Ray  -v, --verbose              verbose mode
9077f5b10aSHannes Eder  --no-tree                  run without a kernel tree
9177f5b10aSHannes Eder  --no-signoff               do not check for 'Signed-off-by' line
92*d5d6281aSDan Carpenter  --no-fixes-tag             do not check for 'Fixes:' tag
9377f5b10aSHannes Eder  --patch                    treat FILE as patchfile (default)
9477f5b10aSHannes Eder  --emacs                    emacs compile window format
9577f5b10aSHannes Eder  --terse                    one line per report
9634d8815fSJoe Perches  --showfile                 emit diffed file position, not input file position
974a593c34SDu, Changbin  -g, --git                  treat FILE as a single commit or git revision range
984a593c34SDu, Changbin                             single git commit with:
994a593c34SDu, Changbin                               <rev>
1004a593c34SDu, Changbin                               <rev>^
1014a593c34SDu, Changbin                               <rev>~n
1024a593c34SDu, Changbin                             multiple git commits with:
1034a593c34SDu, Changbin                               <rev1>..<rev2>
1044a593c34SDu, Changbin                               <rev1>...<rev2>
1054a593c34SDu, Changbin                               <rev>-<count>
1064a593c34SDu, Changbin                             git merges are ignored
10777f5b10aSHannes Eder  -f, --file                 treat FILE as regular source file
10877f5b10aSHannes Eder  --subjective, --strict     enable more subjective tests
1093beb42ecSJoe Perches  --list-types               list the possible message types
11091bfe484SJoe Perches  --types TYPE(,TYPE2...)    show only these comma separated message types
111000d1cc1SJoe Perches  --ignore TYPE(,TYPE2...)   ignore various comma separated message types
1123beb42ecSJoe Perches  --show-types               show the specific message type in the output
113bdc48fa1SJoe Perches  --max-line-length=n        set the maximum line length, (default $max_line_length)
114bdc48fa1SJoe Perches                             if exceeded, warn on patches
115bdc48fa1SJoe Perches                             requires --strict for use with --file
11656193274SVadim Bendebury  --min-conf-desc-length=n   set the min description length, if shorter, warn
117bdc48fa1SJoe Perches  --tab-size=n               set the number of spaces for tab (default $tabsize)
11877f5b10aSHannes Eder  --root=PATH                PATH to the kernel tree root
11977f5b10aSHannes Eder  --no-summary               suppress the per-file summary
12077f5b10aSHannes Eder  --mailback                 only produce a report in case of warnings/errors
12177f5b10aSHannes Eder  --summary-file             include the filename in summary
12277f5b10aSHannes Eder  --debug KEY=[0|1]          turn on/off debugging of KEY, where KEY is one of
12377f5b10aSHannes Eder                             'values', 'possible', 'type', and 'attr' (default
12477f5b10aSHannes Eder                             is all off)
12577f5b10aSHannes Eder  --test-only=WORD           report only warnings/errors containing WORD
12677f5b10aSHannes Eder                             literally
1273705ce5bSJoe Perches  --fix                      EXPERIMENTAL - may create horrible results
1283705ce5bSJoe Perches                             If correctable single-line errors exist, create
1293705ce5bSJoe Perches                             "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
1303705ce5bSJoe Perches                             with potential errors corrected to the preferred
1313705ce5bSJoe Perches                             checkpatch style
1329624b8d6SJoe Perches  --fix-inplace              EXPERIMENTAL - may create horrible results
1339624b8d6SJoe Perches                             Is the same as --fix, but overwrites the input
1349624b8d6SJoe Perches                             file.  It's your fault if there's no backup or git
135d62a201fSDave Hansen  --ignore-perl-version      override checking of perl version.  expect
136d62a201fSDave Hansen                             runtime errors.
137ebfd7d62SJoe Perches  --codespell                Use the codespell dictionary for spelling/typos
1380ee3e7b8SPeter Ujfalusi                             (default:$codespellfile)
139ebfd7d62SJoe Perches  --codespellfile            Use this codespell dictionary
14075ad8c57SJerome Forissier  --typedefsfile             Read additional types from this file
141737c0767SJohn Brooks  --color[=WHEN]             Use colors 'always', 'never', or only when output
142737c0767SJohn Brooks                             is a terminal ('auto'). Default is 'auto'.
1433e89ad85SJerome Forissier  --kconfig-prefix=WORD      use WORD as a prefix for Kconfig symbols (default
1443e89ad85SJerome Forissier                             ${CONFIG_})
14577f5b10aSHannes Eder  -h, --help, --version      display this help and exit
14677f5b10aSHannes Eder
14777f5b10aSHannes EderWhen FILE is - read standard input.
14877f5b10aSHannes EderEOM
14977f5b10aSHannes Eder
15077f5b10aSHannes Eder	exit($exitcode);
15177f5b10aSHannes Eder}
15277f5b10aSHannes Eder
1533beb42ecSJoe Perchessub uniq {
1543beb42ecSJoe Perches	my %seen;
1553beb42ecSJoe Perches	return grep { !$seen{$_}++ } @_;
1563beb42ecSJoe Perches}
1573beb42ecSJoe Perches
1583beb42ecSJoe Perchessub list_types {
1593beb42ecSJoe Perches	my ($exitcode) = @_;
1603beb42ecSJoe Perches
1613beb42ecSJoe Perches	my $count = 0;
1623beb42ecSJoe Perches
1633beb42ecSJoe Perches	local $/ = undef;
1643beb42ecSJoe Perches
1653beb42ecSJoe Perches	open(my $script, '<', abs_path($P)) or
1663beb42ecSJoe Perches	    die "$P: Can't read '$P' $!\n";
1673beb42ecSJoe Perches
1683beb42ecSJoe Perches	my $text = <$script>;
1693beb42ecSJoe Perches	close($script);
1703beb42ecSJoe Perches
17152178ce0SDwaipayan Ray	my %types = ();
1720547fa58SJean Delvare	# Also catch when type or level is passed through a variable
17352178ce0SDwaipayan Ray	while ($text =~ /(?:(\bCHK|\bWARN|\bERROR|&\{\$msg_level})\s*\(|\$msg_type\s*=)\s*"([^"]+)"/g) {
17452178ce0SDwaipayan Ray		if (defined($1)) {
17552178ce0SDwaipayan Ray			if (exists($types{$2})) {
17652178ce0SDwaipayan Ray				$types{$2} .= ",$1" if ($types{$2} ne $1);
17752178ce0SDwaipayan Ray			} else {
17852178ce0SDwaipayan Ray				$types{$2} = $1;
1793beb42ecSJoe Perches			}
18052178ce0SDwaipayan Ray		} else {
18152178ce0SDwaipayan Ray			$types{$2} = "UNDETERMINED";
18252178ce0SDwaipayan Ray		}
18352178ce0SDwaipayan Ray	}
18452178ce0SDwaipayan Ray
1853beb42ecSJoe Perches	print("#\tMessage type\n\n");
18652178ce0SDwaipayan Ray	if ($color) {
18752178ce0SDwaipayan Ray		print(" ( Color coding: ");
18852178ce0SDwaipayan Ray		print(RED . "ERROR" . RESET);
18952178ce0SDwaipayan Ray		print(" | ");
19052178ce0SDwaipayan Ray		print(YELLOW . "WARNING" . RESET);
19152178ce0SDwaipayan Ray		print(" | ");
19252178ce0SDwaipayan Ray		print(GREEN . "CHECK" . RESET);
19352178ce0SDwaipayan Ray		print(" | ");
19452178ce0SDwaipayan Ray		print("Multiple levels / Undetermined");
19552178ce0SDwaipayan Ray		print(" )\n\n");
19652178ce0SDwaipayan Ray	}
19752178ce0SDwaipayan Ray
19852178ce0SDwaipayan Ray	foreach my $type (sort keys %types) {
19952178ce0SDwaipayan Ray		my $orig_type = $type;
20052178ce0SDwaipayan Ray		if ($color) {
20152178ce0SDwaipayan Ray			my $level = $types{$type};
20252178ce0SDwaipayan Ray			if ($level eq "ERROR") {
20352178ce0SDwaipayan Ray				$type = RED . $type . RESET;
20452178ce0SDwaipayan Ray			} elsif ($level eq "WARN") {
20552178ce0SDwaipayan Ray				$type = YELLOW . $type . RESET;
20652178ce0SDwaipayan Ray			} elsif ($level eq "CHK") {
20752178ce0SDwaipayan Ray				$type = GREEN . $type . RESET;
20852178ce0SDwaipayan Ray			}
20952178ce0SDwaipayan Ray		}
2103beb42ecSJoe Perches		print(++$count . "\t" . $type . "\n");
21152178ce0SDwaipayan Ray		if ($verbose && exists($verbose_messages{$orig_type})) {
21252178ce0SDwaipayan Ray			my $message = $verbose_messages{$orig_type};
21352178ce0SDwaipayan Ray			$message =~ s/\n/\n\t/g;
21452178ce0SDwaipayan Ray			print("\t" . $message . "\n\n");
21552178ce0SDwaipayan Ray		}
2163beb42ecSJoe Perches	}
2173beb42ecSJoe Perches
2183beb42ecSJoe Perches	exit($exitcode);
2193beb42ecSJoe Perches}
2203beb42ecSJoe Perches
221000d1cc1SJoe Perchesmy $conf = which_conf($configuration_file);
222000d1cc1SJoe Perchesif (-f $conf) {
223000d1cc1SJoe Perches	my @conf_args;
224000d1cc1SJoe Perches	open(my $conffile, '<', "$conf")
225000d1cc1SJoe Perches	    or warn "$P: Can't find a readable $configuration_file file $!\n";
226000d1cc1SJoe Perches
227000d1cc1SJoe Perches	while (<$conffile>) {
228000d1cc1SJoe Perches		my $line = $_;
229000d1cc1SJoe Perches
230000d1cc1SJoe Perches		$line =~ s/\s*\n?$//g;
231000d1cc1SJoe Perches		$line =~ s/^\s*//g;
232000d1cc1SJoe Perches		$line =~ s/\s+/ /g;
233000d1cc1SJoe Perches
234000d1cc1SJoe Perches		next if ($line =~ m/^\s*#/);
235000d1cc1SJoe Perches		next if ($line =~ m/^\s*$/);
236000d1cc1SJoe Perches
237000d1cc1SJoe Perches		my @words = split(" ", $line);
238000d1cc1SJoe Perches		foreach my $word (@words) {
239000d1cc1SJoe Perches			last if ($word =~ m/^#/);
240000d1cc1SJoe Perches			push (@conf_args, $word);
241000d1cc1SJoe Perches		}
242000d1cc1SJoe Perches	}
243000d1cc1SJoe Perches	close($conffile);
244000d1cc1SJoe Perches	unshift(@ARGV, @conf_args) if @conf_args;
245000d1cc1SJoe Perches}
246000d1cc1SJoe Perches
24752178ce0SDwaipayan Raysub load_docs {
24852178ce0SDwaipayan Ray	open(my $docs, '<', "$docsfile")
24952178ce0SDwaipayan Ray	    or warn "$P: Can't read the documentation file $docsfile $!\n";
25052178ce0SDwaipayan Ray
25152178ce0SDwaipayan Ray	my $type = '';
25252178ce0SDwaipayan Ray	my $desc = '';
25352178ce0SDwaipayan Ray	my $in_desc = 0;
25452178ce0SDwaipayan Ray
25552178ce0SDwaipayan Ray	while (<$docs>) {
25652178ce0SDwaipayan Ray		chomp;
25752178ce0SDwaipayan Ray		my $line = $_;
25852178ce0SDwaipayan Ray		$line =~ s/\s+$//;
25952178ce0SDwaipayan Ray
26052178ce0SDwaipayan Ray		if ($line =~ /^\s*\*\*(.+)\*\*$/) {
26152178ce0SDwaipayan Ray			if ($desc ne '') {
26252178ce0SDwaipayan Ray				$verbose_messages{$type} = trim($desc);
26352178ce0SDwaipayan Ray			}
26452178ce0SDwaipayan Ray			$type = $1;
26552178ce0SDwaipayan Ray			$desc = '';
26652178ce0SDwaipayan Ray			$in_desc = 1;
26752178ce0SDwaipayan Ray		} elsif ($in_desc) {
26852178ce0SDwaipayan Ray			if ($line =~ /^(?:\s{4,}|$)/) {
26952178ce0SDwaipayan Ray				$line =~ s/^\s{4}//;
27052178ce0SDwaipayan Ray				$desc .= $line;
27152178ce0SDwaipayan Ray				$desc .= "\n";
27252178ce0SDwaipayan Ray			} else {
27352178ce0SDwaipayan Ray				$verbose_messages{$type} = trim($desc);
27452178ce0SDwaipayan Ray				$type = '';
27552178ce0SDwaipayan Ray				$desc = '';
27652178ce0SDwaipayan Ray				$in_desc = 0;
27752178ce0SDwaipayan Ray			}
27852178ce0SDwaipayan Ray		}
27952178ce0SDwaipayan Ray	}
28052178ce0SDwaipayan Ray
28152178ce0SDwaipayan Ray	if ($desc ne '') {
28252178ce0SDwaipayan Ray		$verbose_messages{$type} = trim($desc);
28352178ce0SDwaipayan Ray	}
28452178ce0SDwaipayan Ray	close($docs);
28552178ce0SDwaipayan Ray}
28652178ce0SDwaipayan Ray
287737c0767SJohn Brooks# Perl's Getopt::Long allows options to take optional arguments after a space.
288737c0767SJohn Brooks# Prevent --color by itself from consuming other arguments
289737c0767SJohn Brooksforeach (@ARGV) {
290737c0767SJohn Brooks	if ($_ eq "--color" || $_ eq "-color") {
291737c0767SJohn Brooks		$_ = "--color=$color";
292737c0767SJohn Brooks	}
293737c0767SJohn Brooks}
294737c0767SJohn Brooks
2950a920b5bSAndy WhitcroftGetOptions(
2966c72ffaaSAndy Whitcroft	'q|quiet+'	=> \$quiet,
29752178ce0SDwaipayan Ray	'v|verbose!'	=> \$verbose,
2980a920b5bSAndy Whitcroft	'tree!'		=> \$tree,
2990a920b5bSAndy Whitcroft	'signoff!'	=> \$chk_signoff,
300*d5d6281aSDan Carpenter	'fixes-tag!'	=> \$chk_fixes_tag,
3010a920b5bSAndy Whitcroft	'patch!'	=> \$chk_patch,
3026c72ffaaSAndy Whitcroft	'emacs!'	=> \$emacs,
3038905a67cSAndy Whitcroft	'terse!'	=> \$terse,
30434d8815fSJoe Perches	'showfile!'	=> \$showfile,
30577f5b10aSHannes Eder	'f|file!'	=> \$file,
3064a593c34SDu, Changbin	'g|git!'	=> \$git,
3076c72ffaaSAndy Whitcroft	'subjective!'	=> \$check,
3086c72ffaaSAndy Whitcroft	'strict!'	=> \$check,
309000d1cc1SJoe Perches	'ignore=s'	=> \@ignore,
31091bfe484SJoe Perches	'types=s'	=> \@use,
311000d1cc1SJoe Perches	'show-types!'	=> \$show_types,
3123beb42ecSJoe Perches	'list-types!'	=> \$list_types,
3136cd7f386SJoe Perches	'max-line-length=i' => \$max_line_length,
31456193274SVadim Bendebury	'min-conf-desc-length=i' => \$min_conf_desc_length,
315713a09deSAntonio Borneo	'tab-size=i'	=> \$tabsize,
3166c72ffaaSAndy Whitcroft	'root=s'	=> \$root,
3178905a67cSAndy Whitcroft	'summary!'	=> \$summary,
3188905a67cSAndy Whitcroft	'mailback!'	=> \$mailback,
31913214adfSAndy Whitcroft	'summary-file!'	=> \$summary_file,
3203705ce5bSJoe Perches	'fix!'		=> \$fix,
3219624b8d6SJoe Perches	'fix-inplace!'	=> \$fix_inplace,
322d62a201fSDave Hansen	'ignore-perl-version!' => \$ignore_perl_version,
323c2fdda0dSAndy Whitcroft	'debug=s'	=> \%debug,
324773647a0SAndy Whitcroft	'test-only=s'	=> \$tst_only,
325ebfd7d62SJoe Perches	'codespell!'	=> \$codespell,
3260ee3e7b8SPeter Ujfalusi	'codespellfile=s'	=> \$user_codespellfile,
32775ad8c57SJerome Forissier	'typedefsfile=s'	=> \$typedefsfile,
328737c0767SJohn Brooks	'color=s'	=> \$color,
329737c0767SJohn Brooks	'no-color'	=> \$color,	#keep old behaviors of -nocolor
330737c0767SJohn Brooks	'nocolor'	=> \$color,	#keep old behaviors of -nocolor
3313e89ad85SJerome Forissier	'kconfig-prefix=s'	=> \${CONFIG_},
33277f5b10aSHannes Eder	'h|help'	=> \$help,
33377f5b10aSHannes Eder	'version'	=> \$help
3340ee3e7b8SPeter Ujfalusi) or $help = 2;
33577f5b10aSHannes Eder
3360ee3e7b8SPeter Ujfalusiif ($user_codespellfile) {
3370ee3e7b8SPeter Ujfalusi	# Use the user provided codespell file unconditionally
3380ee3e7b8SPeter Ujfalusi	$codespellfile = $user_codespellfile;
3390ee3e7b8SPeter Ujfalusi} elsif (!(-f $codespellfile)) {
3400ee3e7b8SPeter Ujfalusi	# If /usr/share/codespell/dictionary.txt is not present, try to find it
3410ee3e7b8SPeter Ujfalusi	# under codespell's install directory: <codespell_root>/data/dictionary.txt
342c882c6b1SSagar Patel	if (($codespell || $help) && which("python3") ne "") {
3430ee3e7b8SPeter Ujfalusi		my $python_codespell_dict = << "EOF";
3440ee3e7b8SPeter Ujfalusi
3450ee3e7b8SPeter Ujfalusiimport os.path as op
3460ee3e7b8SPeter Ujfalusiimport codespell_lib
3470ee3e7b8SPeter Ujfalusicodespell_dir = op.dirname(codespell_lib.__file__)
3480ee3e7b8SPeter Ujfalusicodespell_file = op.join(codespell_dir, 'data', 'dictionary.txt')
3490ee3e7b8SPeter Ujfalusiprint(codespell_file, end='')
3500ee3e7b8SPeter UjfalusiEOF
3510ee3e7b8SPeter Ujfalusi
352c882c6b1SSagar Patel		my $codespell_dict = `python3 -c "$python_codespell_dict" 2> /dev/null`;
3530ee3e7b8SPeter Ujfalusi		$codespellfile = $codespell_dict if (-f $codespell_dict);
3540ee3e7b8SPeter Ujfalusi	}
3550ee3e7b8SPeter Ujfalusi}
3560ee3e7b8SPeter Ujfalusi
3570ee3e7b8SPeter Ujfalusi# $help is 1 if either -h, --help or --version is passed as option - exitcode: 0
3580ee3e7b8SPeter Ujfalusi# $help is 2 if invalid option is passed - exitcode: 1
3590ee3e7b8SPeter Ujfalusihelp($help - 1) if ($help);
3600a920b5bSAndy Whitcroft
36152178ce0SDwaipayan Raydie "$P: --git cannot be used with --file or --fix\n" if ($git && ($file || $fix));
36252178ce0SDwaipayan Raydie "$P: --verbose cannot be used with --terse\n" if ($verbose && $terse);
36352178ce0SDwaipayan Ray
36452178ce0SDwaipayan Rayif ($color =~ /^[01]$/) {
36552178ce0SDwaipayan Ray	$color = !$color;
36652178ce0SDwaipayan Ray} elsif ($color =~ /^always$/i) {
36752178ce0SDwaipayan Ray	$color = 1;
36852178ce0SDwaipayan Ray} elsif ($color =~ /^never$/i) {
36952178ce0SDwaipayan Ray	$color = 0;
37052178ce0SDwaipayan Ray} elsif ($color =~ /^auto$/i) {
37152178ce0SDwaipayan Ray	$color = (-t STDOUT);
37252178ce0SDwaipayan Ray} else {
37352178ce0SDwaipayan Ray	die "$P: Invalid color mode: $color\n";
37452178ce0SDwaipayan Ray}
37552178ce0SDwaipayan Ray
37652178ce0SDwaipayan Rayload_docs() if ($verbose);
3773beb42ecSJoe Percheslist_types(0) if ($list_types);
3783beb42ecSJoe Perches
3799624b8d6SJoe Perches$fix = 1 if ($fix_inplace);
3802ac73b4fSJoe Perches$check_orig = $check;
3819624b8d6SJoe Perches
3820a920b5bSAndy Whitcroftmy $exit = 0;
3830a920b5bSAndy Whitcroft
3845b57980dSJoe Perchesmy $perl_version_ok = 1;
385d62a201fSDave Hansenif ($^V && $^V lt $minimum_perl_version) {
3865b57980dSJoe Perches	$perl_version_ok = 0;
387d62a201fSDave Hansen	printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
3885b57980dSJoe Perches	exit(1) if (!$ignore_perl_version);
389d62a201fSDave Hansen}
390d62a201fSDave Hansen
39145107ff6SAllen Hubbe#if no filenames are given, push '-' to read patch from stdin
3920a920b5bSAndy Whitcroftif ($#ARGV < 0) {
39345107ff6SAllen Hubbe	push(@ARGV, '-');
3940a920b5bSAndy Whitcroft}
3950a920b5bSAndy Whitcroft
396713a09deSAntonio Borneo# skip TAB size 1 to avoid additional checks on $tabsize - 1
39732f30ca9SJoe Perchesdie "$P: Invalid TAB size: $tabsize\n" if ($tabsize < 2);
398713a09deSAntonio Borneo
39991bfe484SJoe Perchessub hash_save_array_words {
40091bfe484SJoe Perches	my ($hashRef, $arrayRef) = @_;
40191bfe484SJoe Perches
40291bfe484SJoe Perches	my @array = split(/,/, join(',', @$arrayRef));
40391bfe484SJoe Perches	foreach my $word (@array) {
404000d1cc1SJoe Perches		$word =~ s/\s*\n?$//g;
405000d1cc1SJoe Perches		$word =~ s/^\s*//g;
406000d1cc1SJoe Perches		$word =~ s/\s+/ /g;
407000d1cc1SJoe Perches		$word =~ tr/[a-z]/[A-Z]/;
408000d1cc1SJoe Perches
409000d1cc1SJoe Perches		next if ($word =~ m/^\s*#/);
410000d1cc1SJoe Perches		next if ($word =~ m/^\s*$/);
411000d1cc1SJoe Perches
41291bfe484SJoe Perches		$hashRef->{$word}++;
413000d1cc1SJoe Perches	}
41491bfe484SJoe Perches}
41591bfe484SJoe Perches
41691bfe484SJoe Perchessub hash_show_words {
41791bfe484SJoe Perches	my ($hashRef, $prefix) = @_;
41891bfe484SJoe Perches
4193c816e49SJoe Perches	if (keys %$hashRef) {
420d8469f16SJoe Perches		print "\nNOTE: $prefix message types:";
42158cb3cf6SJoe Perches		foreach my $word (sort keys %$hashRef) {
42291bfe484SJoe Perches			print " $word";
42391bfe484SJoe Perches		}
424d8469f16SJoe Perches		print "\n";
42591bfe484SJoe Perches	}
42691bfe484SJoe Perches}
42791bfe484SJoe Perches
42891bfe484SJoe Percheshash_save_array_words(\%ignore_type, \@ignore);
42991bfe484SJoe Percheshash_save_array_words(\%use_type, \@use);
430000d1cc1SJoe Perches
431c2fdda0dSAndy Whitcroftmy $dbg_values = 0;
432c2fdda0dSAndy Whitcroftmy $dbg_possible = 0;
4337429c690SAndy Whitcroftmy $dbg_type = 0;
434a1ef277eSAndy Whitcroftmy $dbg_attr = 0;
435c2fdda0dSAndy Whitcroftfor my $key (keys %debug) {
43621caa13cSAndy Whitcroft	## no critic
43721caa13cSAndy Whitcroft	eval "\${dbg_$key} = '$debug{$key}';";
43821caa13cSAndy Whitcroft	die "$@" if ($@);
439c2fdda0dSAndy Whitcroft}
440c2fdda0dSAndy Whitcroft
441d2c0a235SAndy Whitcroftmy $rpt_cleaners = 0;
442d2c0a235SAndy Whitcroft
4438905a67cSAndy Whitcroftif ($terse) {
4448905a67cSAndy Whitcroft	$emacs = 1;
4458905a67cSAndy Whitcroft	$quiet++;
4468905a67cSAndy Whitcroft}
4478905a67cSAndy Whitcroft
4486c72ffaaSAndy Whitcroftif ($tree) {
4496c72ffaaSAndy Whitcroft	if (defined $root) {
4506c72ffaaSAndy Whitcroft		if (!top_of_kernel_tree($root)) {
4516c72ffaaSAndy Whitcroft			die "$P: $root: --root does not point at a valid tree\n";
4526c72ffaaSAndy Whitcroft		}
4536c72ffaaSAndy Whitcroft	} else {
4546c72ffaaSAndy Whitcroft		if (top_of_kernel_tree('.')) {
4556c72ffaaSAndy Whitcroft			$root = '.';
4566c72ffaaSAndy Whitcroft		} elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
4576c72ffaaSAndy Whitcroft						top_of_kernel_tree($1)) {
4586c72ffaaSAndy Whitcroft			$root = $1;
4596c72ffaaSAndy Whitcroft		}
4606c72ffaaSAndy Whitcroft	}
4616c72ffaaSAndy Whitcroft
4626c72ffaaSAndy Whitcroft	if (!defined $root) {
4630a920b5bSAndy Whitcroft		print "Must be run from the top-level dir. of a kernel tree\n";
4640a920b5bSAndy Whitcroft		exit(2);
4650a920b5bSAndy Whitcroft	}
4666c72ffaaSAndy Whitcroft}
4676c72ffaaSAndy Whitcroft
4686c72ffaaSAndy Whitcroftmy $emitted_corrupt = 0;
4696c72ffaaSAndy Whitcroft
4702ceb532bSAndy Whitcroftour $Ident	= qr{
4712ceb532bSAndy Whitcroft			[A-Za-z_][A-Za-z\d_]*
4722ceb532bSAndy Whitcroft			(?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
4732ceb532bSAndy Whitcroft		}x;
4746c72ffaaSAndy Whitcroftour $Storage	= qr{extern|static|asmlinkage};
4756c72ffaaSAndy Whitcroftour $Sparse	= qr{
4766c72ffaaSAndy Whitcroft			__user|
4776c72ffaaSAndy Whitcroft			__kernel|
4786c72ffaaSAndy Whitcroft			__force|
4796c72ffaaSAndy Whitcroft			__iomem|
4806c72ffaaSAndy Whitcroft			__must_check|
481417495edSAndy Whitcroft			__kprobes|
482165e72a6SSven Eckelmann			__ref|
48333aa4597SGeert Uytterhoeven			__refconst|
48433aa4597SGeert Uytterhoeven			__refdata|
485ad315455SBoqun Feng			__rcu|
486ad315455SBoqun Feng			__private
4876c72ffaaSAndy Whitcroft		}x;
488e970b884SJoe Perchesour $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
489e970b884SJoe Perchesour $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
490e970b884SJoe Perchesour $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
491e970b884SJoe Perchesour $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
492e970b884SJoe Perchesour $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
4938716de38SJoe Perches
49452131292SWolfram Sang# Notes to $Attribute:
49552131292SWolfram Sang# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
4966c72ffaaSAndy Whitcroftour $Attribute	= qr{
4976c72ffaaSAndy Whitcroft			const|
498b5e8736aSJoe Perches			volatile|
49903f1df7dSJoe Perches			__percpu|
50003f1df7dSJoe Perches			__nocast|
50103f1df7dSJoe Perches			__safe|
50246d832f5SMichael S. Tsirkin			__bitwise|
50303f1df7dSJoe Perches			__packed__|
50403f1df7dSJoe Perches			__packed2__|
50503f1df7dSJoe Perches			__naked|
50603f1df7dSJoe Perches			__maybe_unused|
50703f1df7dSJoe Perches			__always_unused|
50803f1df7dSJoe Perches			__noreturn|
50903f1df7dSJoe Perches			__used|
51003f1df7dSJoe Perches			__cold|
511e23ef1f3SJoe Perches			__pure|
51203f1df7dSJoe Perches			__noclone|
51303f1df7dSJoe Perches			__deprecated|
5146c72ffaaSAndy Whitcroft			__read_mostly|
515c5967e98SJoe Perches			__ro_after_init|
5166c72ffaaSAndy Whitcroft			__kprobes|
5178716de38SJoe Perches			$InitAttribute|
5182f9dadbaSMarcelo Schmitt			__aligned\s*\(.*\)|
51924e1d81aSAndy Whitcroft			____cacheline_aligned|
52024e1d81aSAndy Whitcroft			____cacheline_aligned_in_smp|
5215fe3af11SAndy Whitcroft			____cacheline_internodealigned_in_smp|
52286cffecdSKees Cook			__weak|
52386cffecdSKees Cook			__alloc_size\s*\(\s*\d+\s*(?:,\s*\d+\s*)?\)
5246c72ffaaSAndy Whitcroft		  }x;
525c45dcabdSAndy Whitcroftour $Modifier;
52691cb5195SJoe Perchesour $Inline	= qr{inline|__always_inline|noinline|__inline|__inline__};
5276c72ffaaSAndy Whitcroftour $Member	= qr{->$Ident|\.$Ident|\[[^]]*\]};
5286c72ffaaSAndy Whitcroftour $Lval	= qr{$Ident(?:$Member)*};
5296c72ffaaSAndy Whitcroft
53095e2c602SJoe Perchesour $Int_type	= qr{(?i)llu|ull|ll|lu|ul|l|u};
53195e2c602SJoe Perchesour $Binary	= qr{(?i)0b[01]+$Int_type?};
53295e2c602SJoe Perchesour $Hex	= qr{(?i)0x[0-9a-f]+$Int_type?};
53395e2c602SJoe Perchesour $Int	= qr{[0-9]+$Int_type?};
5342435880fSJoe Perchesour $Octal	= qr{0[0-7]+$Int_type?};
535d2af5aa6SJoe Perchesour $String	= qr{(?:\b[Lu])?"[X\t]*"};
536326b1ffcSJoe Perchesour $Float_hex	= qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
537326b1ffcSJoe Perchesour $Float_dec	= qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
538326b1ffcSJoe Perchesour $Float_int	= qr{(?i)[0-9]+e-?[0-9]+[fl]?};
53974349bccSJoe Perchesour $Float	= qr{$Float_hex|$Float_dec|$Float_int};
5402435880fSJoe Perchesour $Constant	= qr{$Float|$Binary|$Octal|$Hex|$Int};
541326b1ffcSJoe Perchesour $Assignment	= qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
542447432f3SJoe Perchesour $Compare    = qr{<=|>=|==|!=|<|(?<!-)>};
54323f780c9SJoe Perchesour $Arithmetic = qr{\+|-|\*|\/|%};
5446c72ffaaSAndy Whitcroftour $Operators	= qr{
5456c72ffaaSAndy Whitcroft			<=|>=|==|!=|
5466c72ffaaSAndy Whitcroft			=>|->|<<|>>|<|>|!|~|
54723f780c9SJoe Perches			&&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
5486c72ffaaSAndy Whitcroft		  }x;
5496c72ffaaSAndy Whitcroft
55091cb5195SJoe Perchesour $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
55191cb5195SJoe Perches
552ab7e23f3SJoe Perchesour $BasicType;
5538905a67cSAndy Whitcroftour $NonptrType;
5541813087dSJoe Perchesour $NonptrTypeMisordered;
5558716de38SJoe Perchesour $NonptrTypeWithAttr;
5568905a67cSAndy Whitcroftour $Type;
5571813087dSJoe Perchesour $TypeMisordered;
5588905a67cSAndy Whitcroftour $Declare;
5591813087dSJoe Perchesour $DeclareMisordered;
5608905a67cSAndy Whitcroft
56115662b3eSJoe Perchesour $NON_ASCII_UTF8	= qr{
56215662b3eSJoe Perches	[\xC2-\xDF][\x80-\xBF]               # non-overlong 2-byte
563171ae1a4SAndy Whitcroft	|  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
564171ae1a4SAndy Whitcroft	| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
565171ae1a4SAndy Whitcroft	|  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
566171ae1a4SAndy Whitcroft	|  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
567171ae1a4SAndy Whitcroft	| [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
568171ae1a4SAndy Whitcroft	|  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
569171ae1a4SAndy Whitcroft}x;
570171ae1a4SAndy Whitcroft
57115662b3eSJoe Perchesour $UTF8	= qr{
57215662b3eSJoe Perches	[\x09\x0A\x0D\x20-\x7E]              # ASCII
57315662b3eSJoe Perches	| $NON_ASCII_UTF8
57415662b3eSJoe Perches}x;
57515662b3eSJoe Perches
576e6176fa4SJoe Perchesour $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
577021158b4SJoe Perchesour $typeOtherOSTypedefs = qr{(?x:
578021158b4SJoe Perches	u_(?:char|short|int|long) |          # bsd
579021158b4SJoe Perches	u(?:nchar|short|int|long)            # sysv
580021158b4SJoe Perches)};
581e6176fa4SJoe Perchesour $typeKernelTypedefs = qr{(?x:
582fb9e9096SAndy Whitcroft	(?:__)?(?:u|s|be|le)(?:8|16|32|64)|
5838ed22cadSAndy Whitcroft	atomic_t
5848ed22cadSAndy Whitcroft)};
5858ea0114eSMickaël Salaünour $typeStdioTypedefs = qr{(?x:
5868ea0114eSMickaël Salaün	FILE
5878ea0114eSMickaël Salaün)};
588e6176fa4SJoe Perchesour $typeTypedefs = qr{(?x:
589e6176fa4SJoe Perches	$typeC99Typedefs\b|
590e6176fa4SJoe Perches	$typeOtherOSTypedefs\b|
5918ea0114eSMickaël Salaün	$typeKernelTypedefs\b|
5928ea0114eSMickaël Salaün	$typeStdioTypedefs\b
593e6176fa4SJoe Perches)};
5948ed22cadSAndy Whitcroft
5956d32f7a3SJoe Perchesour $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
5966d32f7a3SJoe Perches
597691e669bSJoe Perchesour $logFunctions = qr{(?x:
598758d7aadSMiles Chen	printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
5997d0b6594SJacob Keller	(?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
60087bd499aSJoe Perches	TP_printk|
6016e60c02eSJoe Perches	WARN(?:_RATELIMIT|_ONCE|)|
602b0531722SJoe Perches	panic|
60306668727SJoe Perches	MODULE_[A-Z_]+|
60406668727SJoe Perches	seq_vprintf|seq_printf|seq_puts
605691e669bSJoe Perches)};
606691e669bSJoe Perches
607e29a70f1SJoe Perchesour $allocFunctions = qr{(?x:
608e29a70f1SJoe Perches	(?:(?:devm_)?
60958f02267SJoe Perches		(?:kv|k|v)[czm]alloc(?:_array)?(?:_node)? |
610e29a70f1SJoe Perches		kstrdup(?:_const)? |
611e29a70f1SJoe Perches		kmemdup(?:_nul)?) |
612461e1565SChristophe JAILLET	(?:\w+)?alloc_skb(?:_ip_align)? |
613e29a70f1SJoe Perches				# dev_alloc_skb/netdev_alloc_skb, et al
614e29a70f1SJoe Perches	dma_alloc_coherent
615e29a70f1SJoe Perches)};
616e29a70f1SJoe Perches
61720112475SJoe Perchesour $signature_tags = qr{(?xi:
61820112475SJoe Perches	Signed-off-by:|
619d499480cSJorge Ramirez-Ortiz	Co-developed-by:|
62020112475SJoe Perches	Acked-by:|
62120112475SJoe Perches	Tested-by:|
62220112475SJoe Perches	Reviewed-by:|
62320112475SJoe Perches	Reported-by:|
6248543ae12SMugunthan V N	Suggested-by:|
62520112475SJoe Perches	To:|
62620112475SJoe Perches	Cc:
62720112475SJoe Perches)};
62820112475SJoe Perches
62944c31888SMatthieu Baertsour @link_tags = qw(Link Closes);
630f94e40eaSMatthieu Baerts
631f94e40eaSMatthieu Baerts#Create a search and print patterns for all these strings to be used directly below
632f94e40eaSMatthieu Baertsour $link_tags_search = "";
633f94e40eaSMatthieu Baertsour $link_tags_print = "";
634f94e40eaSMatthieu Baertsforeach my $entry (@link_tags) {
635f94e40eaSMatthieu Baerts	if ($link_tags_search ne "") {
636f94e40eaSMatthieu Baerts		$link_tags_search .= '|';
637f94e40eaSMatthieu Baerts		$link_tags_print .= ' or ';
638f94e40eaSMatthieu Baerts	}
639f94e40eaSMatthieu Baerts	$entry .= ':';
640f94e40eaSMatthieu Baerts	$link_tags_search .= $entry;
641f94e40eaSMatthieu Baerts	$link_tags_print .= "'$entry'";
642f94e40eaSMatthieu Baerts}
643f94e40eaSMatthieu Baerts$link_tags_search = "(?:${link_tags_search})";
644f94e40eaSMatthieu Baerts
645adb2da82SJoe Perchesour $tracing_logging_tags = qr{(?xi:
646adb2da82SJoe Perches	[=-]*> |
647adb2da82SJoe Perches	<[=-]* |
648adb2da82SJoe Perches	\[ |
649adb2da82SJoe Perches	\] |
650adb2da82SJoe Perches	start |
651adb2da82SJoe Perches	called |
652adb2da82SJoe Perches	entered |
653adb2da82SJoe Perches	entry |
654adb2da82SJoe Perches	enter |
655adb2da82SJoe Perches	in |
656adb2da82SJoe Perches	inside |
657adb2da82SJoe Perches	here |
658adb2da82SJoe Perches	begin |
659adb2da82SJoe Perches	exit |
660adb2da82SJoe Perches	end |
661adb2da82SJoe Perches	done |
662adb2da82SJoe Perches	leave |
663adb2da82SJoe Perches	completed |
664adb2da82SJoe Perches	out |
665adb2da82SJoe Perches	return |
666adb2da82SJoe Perches	[\.\!:\s]*
667adb2da82SJoe Perches)};
668adb2da82SJoe Perches
669831242abSAditya Srivastavasub edit_distance_min {
670831242abSAditya Srivastava	my (@arr) = @_;
671831242abSAditya Srivastava	my $len = scalar @arr;
672831242abSAditya Srivastava	if ((scalar @arr) < 1) {
673831242abSAditya Srivastava		# if underflow, return
674831242abSAditya Srivastava		return;
675831242abSAditya Srivastava	}
676831242abSAditya Srivastava	my $min = $arr[0];
677831242abSAditya Srivastava	for my $i (0 .. ($len-1)) {
678831242abSAditya Srivastava		if ($arr[$i] < $min) {
679831242abSAditya Srivastava			$min = $arr[$i];
680831242abSAditya Srivastava		}
681831242abSAditya Srivastava	}
682831242abSAditya Srivastava	return $min;
683831242abSAditya Srivastava}
684831242abSAditya Srivastava
685831242abSAditya Srivastavasub get_edit_distance {
686831242abSAditya Srivastava	my ($str1, $str2) = @_;
687831242abSAditya Srivastava	$str1 = lc($str1);
688831242abSAditya Srivastava	$str2 = lc($str2);
689831242abSAditya Srivastava	$str1 =~ s/-//g;
690831242abSAditya Srivastava	$str2 =~ s/-//g;
691831242abSAditya Srivastava	my $len1 = length($str1);
692831242abSAditya Srivastava	my $len2 = length($str2);
693831242abSAditya Srivastava	# two dimensional array storing minimum edit distance
694831242abSAditya Srivastava	my @distance;
695831242abSAditya Srivastava	for my $i (0 .. $len1) {
696831242abSAditya Srivastava		for my $j (0 .. $len2) {
697831242abSAditya Srivastava			if ($i == 0) {
698831242abSAditya Srivastava				$distance[$i][$j] = $j;
699831242abSAditya Srivastava			} elsif ($j == 0) {
700831242abSAditya Srivastava				$distance[$i][$j] = $i;
701831242abSAditya Srivastava			} elsif (substr($str1, $i-1, 1) eq substr($str2, $j-1, 1)) {
702831242abSAditya Srivastava				$distance[$i][$j] = $distance[$i - 1][$j - 1];
703831242abSAditya Srivastava			} else {
704831242abSAditya Srivastava				my $dist1 = $distance[$i][$j - 1]; #insert distance
705831242abSAditya Srivastava				my $dist2 = $distance[$i - 1][$j]; # remove
706831242abSAditya Srivastava				my $dist3 = $distance[$i - 1][$j - 1]; #replace
707831242abSAditya Srivastava				$distance[$i][$j] = 1 + edit_distance_min($dist1, $dist2, $dist3);
708831242abSAditya Srivastava			}
709831242abSAditya Srivastava		}
710831242abSAditya Srivastava	}
711831242abSAditya Srivastava	return $distance[$len1][$len2];
712831242abSAditya Srivastava}
713831242abSAditya Srivastava
714831242abSAditya Srivastavasub find_standard_signature {
715831242abSAditya Srivastava	my ($sign_off) = @_;
716831242abSAditya Srivastava	my @standard_signature_tags = (
717831242abSAditya Srivastava		'Signed-off-by:', 'Co-developed-by:', 'Acked-by:', 'Tested-by:',
718831242abSAditya Srivastava		'Reviewed-by:', 'Reported-by:', 'Suggested-by:'
719831242abSAditya Srivastava	);
720831242abSAditya Srivastava	foreach my $signature (@standard_signature_tags) {
721831242abSAditya Srivastava		return $signature if (get_edit_distance($sign_off, $signature) <= 2);
722831242abSAditya Srivastava	}
723831242abSAditya Srivastava
724831242abSAditya Srivastava	return "";
725831242abSAditya Srivastava}
726831242abSAditya Srivastava
7279b71f79fSBjorn Helgaasour $obsolete_archives = qr{(?xi:
7289b71f79fSBjorn Helgaas	\Qfreedesktop.org/archives/dri-devel\E |
7299b71f79fSBjorn Helgaas	\Qlists.infradead.org\E |
7309b71f79fSBjorn Helgaas	\Qlkml.org\E |
7319b71f79fSBjorn Helgaas	\Qmail-archive.com\E |
7329b71f79fSBjorn Helgaas	\Qmailman.alsa-project.org/pipermail\E |
7339b71f79fSBjorn Helgaas	\Qmarc.info\E |
7349b71f79fSBjorn Helgaas	\Qozlabs.org/pipermail\E |
7359b71f79fSBjorn Helgaas	\Qspinics.net\E
7369b71f79fSBjorn Helgaas)};
7379b71f79fSBjorn Helgaas
7381813087dSJoe Perchesour @typeListMisordered = (
7391813087dSJoe Perches	qr{char\s+(?:un)?signed},
7401813087dSJoe Perches	qr{int\s+(?:(?:un)?signed\s+)?short\s},
7411813087dSJoe Perches	qr{int\s+short(?:\s+(?:un)?signed)},
7421813087dSJoe Perches	qr{short\s+int(?:\s+(?:un)?signed)},
7431813087dSJoe Perches	qr{(?:un)?signed\s+int\s+short},
7441813087dSJoe Perches	qr{short\s+(?:un)?signed},
7451813087dSJoe Perches	qr{long\s+int\s+(?:un)?signed},
7461813087dSJoe Perches	qr{int\s+long\s+(?:un)?signed},
7471813087dSJoe Perches	qr{long\s+(?:un)?signed\s+int},
7481813087dSJoe Perches	qr{int\s+(?:un)?signed\s+long},
7491813087dSJoe Perches	qr{int\s+(?:un)?signed},
7501813087dSJoe Perches	qr{int\s+long\s+long\s+(?:un)?signed},
7511813087dSJoe Perches	qr{long\s+long\s+int\s+(?:un)?signed},
7521813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed\s+int},
7531813087dSJoe Perches	qr{long\s+long\s+(?:un)?signed},
7541813087dSJoe Perches	qr{long\s+(?:un)?signed},
7551813087dSJoe Perches);
7561813087dSJoe Perches
7578905a67cSAndy Whitcroftour @typeList = (
7588905a67cSAndy Whitcroft	qr{void},
7590c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?char},
7600c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short\s+int},
7610c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?short},
7620c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?int},
7630c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+int},
7640c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
7650c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long\s+long},
7660c773d9dSJoe Perches	qr{(?:(?:un)?signed\s+)?long},
7670c773d9dSJoe Perches	qr{(?:un)?signed},
7688905a67cSAndy Whitcroft	qr{float},
7698905a67cSAndy Whitcroft	qr{double},
7708905a67cSAndy Whitcroft	qr{bool},
7718905a67cSAndy Whitcroft	qr{struct\s+$Ident},
7728905a67cSAndy Whitcroft	qr{union\s+$Ident},
7738905a67cSAndy Whitcroft	qr{enum\s+$Ident},
7748905a67cSAndy Whitcroft	qr{${Ident}_t},
7758905a67cSAndy Whitcroft	qr{${Ident}_handler},
7768905a67cSAndy Whitcroft	qr{${Ident}_handler_fn},
7771813087dSJoe Perches	@typeListMisordered,
7788905a67cSAndy Whitcroft);
779938224b5SJoe Perches
780938224b5SJoe Perchesour $C90_int_types = qr{(?x:
781938224b5SJoe Perches	long\s+long\s+int\s+(?:un)?signed|
782938224b5SJoe Perches	long\s+long\s+(?:un)?signed\s+int|
783938224b5SJoe Perches	long\s+long\s+(?:un)?signed|
784938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long\s+int|
785938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+long|
786938224b5SJoe Perches	int\s+long\s+long\s+(?:un)?signed|
787938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long\s+long|
788938224b5SJoe Perches
789938224b5SJoe Perches	long\s+int\s+(?:un)?signed|
790938224b5SJoe Perches	long\s+(?:un)?signed\s+int|
791938224b5SJoe Perches	long\s+(?:un)?signed|
792938224b5SJoe Perches	(?:(?:un)?signed\s+)?long\s+int|
793938224b5SJoe Perches	(?:(?:un)?signed\s+)?long|
794938224b5SJoe Perches	int\s+long\s+(?:un)?signed|
795938224b5SJoe Perches	int\s+(?:(?:un)?signed\s+)?long|
796938224b5SJoe Perches
797938224b5SJoe Perches	int\s+(?:un)?signed|
798938224b5SJoe Perches	(?:(?:un)?signed\s+)?int
799938224b5SJoe Perches)};
800938224b5SJoe Perches
801485ff23eSAlex Dowadour @typeListFile = ();
8028716de38SJoe Perchesour @typeListWithAttr = (
8038716de38SJoe Perches	@typeList,
8048716de38SJoe Perches	qr{struct\s+$InitAttribute\s+$Ident},
8058716de38SJoe Perches	qr{union\s+$InitAttribute\s+$Ident},
8068716de38SJoe Perches);
8078716de38SJoe Perches
808c45dcabdSAndy Whitcroftour @modifierList = (
809c45dcabdSAndy Whitcroft	qr{fastcall},
810c45dcabdSAndy Whitcroft);
811485ff23eSAlex Dowadour @modifierListFile = ();
8128905a67cSAndy Whitcroft
8132435880fSJoe Perchesour @mode_permission_funcs = (
8142435880fSJoe Perches	["module_param", 3],
8152435880fSJoe Perches	["module_param_(?:array|named|string)", 4],
8162435880fSJoe Perches	["module_param_array_named", 5],
8172435880fSJoe Perches	["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
8182435880fSJoe Perches	["proc_create(?:_data|)", 2],
819459cf0aeSJoe Perches	["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
820459cf0aeSJoe Perches	["IIO_DEV_ATTR_[A-Z_]+", 1],
821459cf0aeSJoe Perches	["SENSOR_(?:DEVICE_|)ATTR_2", 2],
822459cf0aeSJoe Perches	["SENSOR_TEMPLATE(?:_2|)", 3],
823459cf0aeSJoe Perches	["__ATTR", 2],
8242435880fSJoe Perches);
8252435880fSJoe Perches
8261a3dcf2eSJoe Perchesmy $word_pattern = '\b[A-Z]?[a-z]{2,}\b';
8271a3dcf2eSJoe Perches
828515a235eSJoe Perches#Create a search pattern for all these functions to speed up a loop below
829515a235eSJoe Perchesour $mode_perms_search = "";
830515a235eSJoe Perchesforeach my $entry (@mode_permission_funcs) {
831515a235eSJoe Perches	$mode_perms_search .= '|' if ($mode_perms_search ne "");
832515a235eSJoe Perches	$mode_perms_search .= $entry->[0];
833515a235eSJoe Perches}
83400180468SJoe Perches$mode_perms_search = "(?:${mode_perms_search})";
835515a235eSJoe Perches
8369189c7e7SJoe Perchesour %deprecated_apis = (
8379189c7e7SJoe Perches	"synchronize_rcu_bh"			=> "synchronize_rcu",
8389189c7e7SJoe Perches	"synchronize_rcu_bh_expedited"		=> "synchronize_rcu_expedited",
8399189c7e7SJoe Perches	"call_rcu_bh"				=> "call_rcu",
8409189c7e7SJoe Perches	"rcu_barrier_bh"			=> "rcu_barrier",
8419189c7e7SJoe Perches	"synchronize_sched"			=> "synchronize_rcu",
8429189c7e7SJoe Perches	"synchronize_sched_expedited"		=> "synchronize_rcu_expedited",
8439189c7e7SJoe Perches	"call_rcu_sched"			=> "call_rcu",
8449189c7e7SJoe Perches	"rcu_barrier_sched"			=> "rcu_barrier",
8459189c7e7SJoe Perches	"get_state_synchronize_sched"		=> "get_state_synchronize_rcu",
8469189c7e7SJoe Perches	"cond_synchronize_sched"		=> "cond_synchronize_rcu",
847defdaff1SIra Weiny	"kmap"					=> "kmap_local_page",
848a3ea42ffSIra Weiny	"kunmap"				=> "kunmap_local",
849defdaff1SIra Weiny	"kmap_atomic"				=> "kmap_local_page",
850a3ea42ffSIra Weiny	"kunmap_atomic"				=> "kunmap_local",
8519189c7e7SJoe Perches);
8529189c7e7SJoe Perches
8539189c7e7SJoe Perches#Create a search pattern for all these strings to speed up a loop below
8549189c7e7SJoe Perchesour $deprecated_apis_search = "";
8559189c7e7SJoe Perchesforeach my $entry (keys %deprecated_apis) {
8569189c7e7SJoe Perches	$deprecated_apis_search .= '|' if ($deprecated_apis_search ne "");
8579189c7e7SJoe Perches	$deprecated_apis_search .= $entry;
8589189c7e7SJoe Perches}
8599189c7e7SJoe Perches$deprecated_apis_search = "(?:${deprecated_apis_search})";
8609189c7e7SJoe Perches
861b392c64fSJoe Perchesour $mode_perms_world_writable = qr{
862b392c64fSJoe Perches	S_IWUGO		|
863b392c64fSJoe Perches	S_IWOTH		|
864b392c64fSJoe Perches	S_IRWXUGO	|
865b392c64fSJoe Perches	S_IALLUGO	|
866b392c64fSJoe Perches	0[0-7][0-7][2367]
867b392c64fSJoe Perches}x;
868b392c64fSJoe Perches
869f90774e1SJoe Perchesour %mode_permission_string_types = (
870f90774e1SJoe Perches	"S_IRWXU" => 0700,
871f90774e1SJoe Perches	"S_IRUSR" => 0400,
872f90774e1SJoe Perches	"S_IWUSR" => 0200,
873f90774e1SJoe Perches	"S_IXUSR" => 0100,
874f90774e1SJoe Perches	"S_IRWXG" => 0070,
875f90774e1SJoe Perches	"S_IRGRP" => 0040,
876f90774e1SJoe Perches	"S_IWGRP" => 0020,
877f90774e1SJoe Perches	"S_IXGRP" => 0010,
878f90774e1SJoe Perches	"S_IRWXO" => 0007,
879f90774e1SJoe Perches	"S_IROTH" => 0004,
880f90774e1SJoe Perches	"S_IWOTH" => 0002,
881f90774e1SJoe Perches	"S_IXOTH" => 0001,
882f90774e1SJoe Perches	"S_IRWXUGO" => 0777,
883f90774e1SJoe Perches	"S_IRUGO" => 0444,
884f90774e1SJoe Perches	"S_IWUGO" => 0222,
885f90774e1SJoe Perches	"S_IXUGO" => 0111,
886f90774e1SJoe Perches);
887f90774e1SJoe Perches
888f90774e1SJoe Perches#Create a search pattern for all these strings to speed up a loop below
889f90774e1SJoe Perchesour $mode_perms_string_search = "";
890f90774e1SJoe Perchesforeach my $entry (keys %mode_permission_string_types) {
891f90774e1SJoe Perches	$mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
892f90774e1SJoe Perches	$mode_perms_string_search .= $entry;
893f90774e1SJoe Perches}
89400180468SJoe Perchesour $single_mode_perms_string_search = "(?:${mode_perms_string_search})";
89500180468SJoe Perchesour $multi_mode_perms_string_search = qr{
89600180468SJoe Perches	${single_mode_perms_string_search}
89700180468SJoe Perches	(?:\s*\|\s*${single_mode_perms_string_search})*
89800180468SJoe Perches}x;
89900180468SJoe Perches
90000180468SJoe Perchessub perms_to_octal {
90100180468SJoe Perches	my ($string) = @_;
90200180468SJoe Perches
90300180468SJoe Perches	return trim($string) if ($string =~ /^\s*0[0-7]{3,3}\s*$/);
90400180468SJoe Perches
90500180468SJoe Perches	my $val = "";
90600180468SJoe Perches	my $oval = "";
90700180468SJoe Perches	my $to = 0;
90800180468SJoe Perches	my $curpos = 0;
90900180468SJoe Perches	my $lastpos = 0;
91000180468SJoe Perches	while ($string =~ /\b(($single_mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
91100180468SJoe Perches		$curpos = pos($string);
91200180468SJoe Perches		my $match = $2;
91300180468SJoe Perches		my $omatch = $1;
91400180468SJoe Perches		last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
91500180468SJoe Perches		$lastpos = $curpos;
91600180468SJoe Perches		$to |= $mode_permission_string_types{$match};
91700180468SJoe Perches		$val .= '\s*\|\s*' if ($val ne "");
91800180468SJoe Perches		$val .= $match;
91900180468SJoe Perches		$oval .= $omatch;
92000180468SJoe Perches	}
92100180468SJoe Perches	$oval =~ s/^\s*\|\s*//;
92200180468SJoe Perches	$oval =~ s/\s*\|\s*$//;
92300180468SJoe Perches	return sprintf("%04o", $to);
92400180468SJoe Perches}
925f90774e1SJoe Perches
9267840a94cSWolfram Sangour $allowed_asm_includes = qr{(?x:
9277840a94cSWolfram Sang	irq|
928cdcee686SSergey Ryazanov	memory|
929cdcee686SSergey Ryazanov	time|
930cdcee686SSergey Ryazanov	reboot
9317840a94cSWolfram Sang)};
9327840a94cSWolfram Sang# memory.h: ARM has a custom one
9337840a94cSWolfram Sang
93466b47b4aSKees Cook# Load common spelling mistakes and build regular expression list.
93566b47b4aSKees Cookmy $misspellings;
93666b47b4aSKees Cookmy %spelling_fix;
93736061e38SJoe Perches
93836061e38SJoe Perchesif (open(my $spelling, '<', $spelling_file)) {
93966b47b4aSKees Cook	while (<$spelling>) {
94066b47b4aSKees Cook		my $line = $_;
94166b47b4aSKees Cook
94266b47b4aSKees Cook		$line =~ s/\s*\n?$//g;
94366b47b4aSKees Cook		$line =~ s/^\s*//g;
94466b47b4aSKees Cook
94566b47b4aSKees Cook		next if ($line =~ m/^\s*#/);
94666b47b4aSKees Cook		next if ($line =~ m/^\s*$/);
94766b47b4aSKees Cook
94866b47b4aSKees Cook		my ($suspect, $fix) = split(/\|\|/, $line);
94966b47b4aSKees Cook
95066b47b4aSKees Cook		$spelling_fix{$suspect} = $fix;
95166b47b4aSKees Cook	}
95266b47b4aSKees Cook	close($spelling);
95336061e38SJoe Perches} else {
95436061e38SJoe Perches	warn "No typos will be found - file '$spelling_file': $!\n";
95536061e38SJoe Perches}
95666b47b4aSKees Cook
957ebfd7d62SJoe Perchesif ($codespell) {
958ebfd7d62SJoe Perches	if (open(my $spelling, '<', $codespellfile)) {
959ebfd7d62SJoe Perches		while (<$spelling>) {
960ebfd7d62SJoe Perches			my $line = $_;
961ebfd7d62SJoe Perches
962ebfd7d62SJoe Perches			$line =~ s/\s*\n?$//g;
963ebfd7d62SJoe Perches			$line =~ s/^\s*//g;
964ebfd7d62SJoe Perches
965ebfd7d62SJoe Perches			next if ($line =~ m/^\s*#/);
966ebfd7d62SJoe Perches			next if ($line =~ m/^\s*$/);
967ebfd7d62SJoe Perches			next if ($line =~ m/, disabled/i);
968ebfd7d62SJoe Perches
969ebfd7d62SJoe Perches			$line =~ s/,.*$//;
970ebfd7d62SJoe Perches
971ebfd7d62SJoe Perches			my ($suspect, $fix) = split(/->/, $line);
972ebfd7d62SJoe Perches
973ebfd7d62SJoe Perches			$spelling_fix{$suspect} = $fix;
974ebfd7d62SJoe Perches		}
975ebfd7d62SJoe Perches		close($spelling);
976ebfd7d62SJoe Perches	} else {
977ebfd7d62SJoe Perches		warn "No codespell typos will be found - file '$codespellfile': $!\n";
978ebfd7d62SJoe Perches	}
979ebfd7d62SJoe Perches}
980ebfd7d62SJoe Perches
981ebfd7d62SJoe Perches$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
982ebfd7d62SJoe Perches
98375ad8c57SJerome Forissiersub read_words {
98475ad8c57SJerome Forissier	my ($wordsRef, $file) = @_;
98575ad8c57SJerome Forissier
98675ad8c57SJerome Forissier	if (open(my $words, '<', $file)) {
98775ad8c57SJerome Forissier		while (<$words>) {
988bf1fa1daSJoe Perches			my $line = $_;
989bf1fa1daSJoe Perches
990bf1fa1daSJoe Perches			$line =~ s/\s*\n?$//g;
991bf1fa1daSJoe Perches			$line =~ s/^\s*//g;
992bf1fa1daSJoe Perches
993bf1fa1daSJoe Perches			next if ($line =~ m/^\s*#/);
994bf1fa1daSJoe Perches			next if ($line =~ m/^\s*$/);
995bf1fa1daSJoe Perches			if ($line =~ /\s/) {
99675ad8c57SJerome Forissier				print("$file: '$line' invalid - ignored\n");
997bf1fa1daSJoe Perches				next;
998bf1fa1daSJoe Perches			}
999bf1fa1daSJoe Perches
1000ced69da1SQuentin Monnet			$$wordsRef .= '|' if (defined $$wordsRef);
100175ad8c57SJerome Forissier			$$wordsRef .= $line;
1002bf1fa1daSJoe Perches		}
100375ad8c57SJerome Forissier		close($file);
100475ad8c57SJerome Forissier		return 1;
1005bf1fa1daSJoe Perches	}
1006bf1fa1daSJoe Perches
100775ad8c57SJerome Forissier	return 0;
100875ad8c57SJerome Forissier}
100975ad8c57SJerome Forissier
1010ced69da1SQuentin Monnetmy $const_structs;
1011ced69da1SQuentin Monnetif (show_type("CONST_STRUCT")) {
101275ad8c57SJerome Forissier	read_words(\$const_structs, $conststructsfile)
101375ad8c57SJerome Forissier	    or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
1014ced69da1SQuentin Monnet}
101575ad8c57SJerome Forissier
1016ced69da1SQuentin Monnetif (defined($typedefsfile)) {
1017ced69da1SQuentin Monnet	my $typeOtherTypedefs;
101875ad8c57SJerome Forissier	read_words(\$typeOtherTypedefs, $typedefsfile)
101975ad8c57SJerome Forissier	    or warn "No additional types will be considered - file '$typedefsfile': $!\n";
1020ced69da1SQuentin Monnet	$typeTypedefs .= '|' . $typeOtherTypedefs if (defined $typeOtherTypedefs);
102175ad8c57SJerome Forissier}
102275ad8c57SJerome Forissier
10238905a67cSAndy Whitcroftsub build_types {
1024485ff23eSAlex Dowad	my $mods = "(?x:  \n" . join("|\n  ", (@modifierList, @modifierListFile)) . "\n)";
1025485ff23eSAlex Dowad	my $all = "(?x:  \n" . join("|\n  ", (@typeList, @typeListFile)) . "\n)";
10261813087dSJoe Perches	my $Misordered = "(?x:  \n" . join("|\n  ", @typeListMisordered) . "\n)";
10278716de38SJoe Perches	my $allWithAttr = "(?x:  \n" . join("|\n  ", @typeListWithAttr) . "\n)";
1028c8cb2ca3SAndy Whitcroft	$Modifier	= qr{(?:$Attribute|$Sparse|$mods)};
1029ab7e23f3SJoe Perches	$BasicType	= qr{
1030ab7e23f3SJoe Perches				(?:$typeTypedefs\b)|
1031ab7e23f3SJoe Perches				(?:${all}\b)
1032ab7e23f3SJoe Perches		}x;
10338905a67cSAndy Whitcroft	$NonptrType	= qr{
1034d2172eb5SAndy Whitcroft			(?:$Modifier\s+|const\s+)*
1035cf655043SAndy Whitcroft			(?:
10366b48db24SAndy Whitcroft				(?:typeof|__typeof__)\s*\([^\)]*\)|
10378ed22cadSAndy Whitcroft				(?:$typeTypedefs\b)|
1038c45dcabdSAndy Whitcroft				(?:${all}\b)
1039cf655043SAndy Whitcroft			)
1040c8cb2ca3SAndy Whitcroft			(?:\s+$Modifier|\s+const)*
10418905a67cSAndy Whitcroft		  }x;
10421813087dSJoe Perches	$NonptrTypeMisordered	= qr{
10431813087dSJoe Perches			(?:$Modifier\s+|const\s+)*
10441813087dSJoe Perches			(?:
10451813087dSJoe Perches				(?:${Misordered}\b)
10461813087dSJoe Perches			)
10471813087dSJoe Perches			(?:\s+$Modifier|\s+const)*
10481813087dSJoe Perches		  }x;
10498716de38SJoe Perches	$NonptrTypeWithAttr	= qr{
10508716de38SJoe Perches			(?:$Modifier\s+|const\s+)*
10518716de38SJoe Perches			(?:
10528716de38SJoe Perches				(?:typeof|__typeof__)\s*\([^\)]*\)|
10538716de38SJoe Perches				(?:$typeTypedefs\b)|
10548716de38SJoe Perches				(?:${allWithAttr}\b)
10558716de38SJoe Perches			)
10568716de38SJoe Perches			(?:\s+$Modifier|\s+const)*
10578716de38SJoe Perches		  }x;
10588905a67cSAndy Whitcroft	$Type	= qr{
1059c45dcabdSAndy Whitcroft			$NonptrType
10607b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
1061c8cb2ca3SAndy Whitcroft			(?:\s+$Inline|\s+$Modifier)*
10628905a67cSAndy Whitcroft		  }x;
10631813087dSJoe Perches	$TypeMisordered	= qr{
10641813087dSJoe Perches			$NonptrTypeMisordered
10657b18496cSAntonio Borneo			(?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+){0,4}
10661813087dSJoe Perches			(?:\s+$Inline|\s+$Modifier)*
10671813087dSJoe Perches		  }x;
106891cb5195SJoe Perches	$Declare	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
10691813087dSJoe Perches	$DeclareMisordered	= qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
10708905a67cSAndy Whitcroft}
10718905a67cSAndy Whitcroftbuild_types();
10726c72ffaaSAndy Whitcroft
10737d2367afSJoe Perchesour $Typecast	= qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
1074d1fe9c09SJoe Perches
1075d1fe9c09SJoe Perches# Using $balanced_parens, $LvalOrFunc, or $FuncArg
1076d1fe9c09SJoe Perches# requires at least perl version v5.10.0
1077d1fe9c09SJoe Perches# Any use must be runtime checked with $^V
1078d1fe9c09SJoe Perches
1079d1fe9c09SJoe Perchesour $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
10802435880fSJoe Perchesour $LvalOrFunc	= qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
1081c0a5c898SJoe Perchesour $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
10827d2367afSJoe Perches
1083f8422308SJoe Perchesour $declaration_macros = qr{(?x:
10843e838b6cSJoe Perches	(?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
1085fe658f94SSteffen Maier	(?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
1086dcea7964SJoe Perches	(?:SKCIPHER_REQUEST|SHASH_DESC|AHASH_REQUEST)_ON_STACK\s*\(|
1087dcea7964SJoe Perches	(?:$Storage\s+)?(?:XA_STATE|XA_STATE_ORDER)\s*\(
1088f8422308SJoe Perches)};
1089f8422308SJoe Perches
10908d0325ccSAditya Srivastavaour %allow_repeated_words = (
10918d0325ccSAditya Srivastava	add => '',
10928d0325ccSAditya Srivastava	added => '',
10938d0325ccSAditya Srivastava	bad => '',
10948d0325ccSAditya Srivastava	be => '',
10958d0325ccSAditya Srivastava);
10968d0325ccSAditya Srivastava
10977d2367afSJoe Perchessub deparenthesize {
10987d2367afSJoe Perches	my ($string) = @_;
10997d2367afSJoe Perches	return "" if (!defined($string));
11005b9553abSJoe Perches
11015b9553abSJoe Perches	while ($string =~ /^\s*\(.*\)\s*$/) {
11025b9553abSJoe Perches		$string =~ s@^\s*\(\s*@@;
11035b9553abSJoe Perches		$string =~ s@\s*\)\s*$@@;
11045b9553abSJoe Perches	}
11055b9553abSJoe Perches
11067d2367afSJoe Perches	$string =~ s@\s+@ @g;
11075b9553abSJoe Perches
11087d2367afSJoe Perches	return $string;
11097d2367afSJoe Perches}
11107d2367afSJoe Perches
11113445686aSJoe Perchessub seed_camelcase_file {
11123445686aSJoe Perches	my ($file) = @_;
11133445686aSJoe Perches
11143445686aSJoe Perches	return if (!(-f $file));
11153445686aSJoe Perches
11163445686aSJoe Perches	local $/;
11173445686aSJoe Perches
11183445686aSJoe Perches	open(my $include_file, '<', "$file")
11193445686aSJoe Perches	    or warn "$P: Can't read '$file' $!\n";
11203445686aSJoe Perches	my $text = <$include_file>;
11213445686aSJoe Perches	close($include_file);
11223445686aSJoe Perches
11233445686aSJoe Perches	my @lines = split('\n', $text);
11243445686aSJoe Perches
11253445686aSJoe Perches	foreach my $line (@lines) {
11263445686aSJoe Perches		next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
11273445686aSJoe Perches		if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
11283445686aSJoe Perches			$camelcase{$1} = 1;
112911ea516aSJoe Perches		} elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
113011ea516aSJoe Perches			$camelcase{$1} = 1;
113111ea516aSJoe Perches		} elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
11323445686aSJoe Perches			$camelcase{$1} = 1;
11333445686aSJoe Perches		}
11343445686aSJoe Perches	}
11353445686aSJoe Perches}
11363445686aSJoe Perches
1137cd28b119SJoe Perchesour %maintained_status = ();
1138cd28b119SJoe Perches
113985b0ee18SJoe Perchessub is_maintained_obsolete {
114085b0ee18SJoe Perches	my ($filename) = @_;
114185b0ee18SJoe Perches
1142f2c19c2fSJerome Forissier	return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
114385b0ee18SJoe Perches
1144cd28b119SJoe Perches	if (!exists($maintained_status{$filename})) {
1145cd28b119SJoe Perches		$maintained_status{$filename} = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
1146cd28b119SJoe Perches	}
114785b0ee18SJoe Perches
1148cd28b119SJoe Perches	return $maintained_status{$filename} =~ /obsolete/i;
114985b0ee18SJoe Perches}
115085b0ee18SJoe Perches
11513b6e8ac9SJoe Perchessub is_SPDX_License_valid {
11523b6e8ac9SJoe Perches	my ($license) = @_;
11533b6e8ac9SJoe Perches
1154f9363b31SGuenter Roeck	return 1 if (!$tree || which("python3") eq "" || !(-x "$root/scripts/spdxcheck.py") || !(-e "$gitroot"));
11553b6e8ac9SJoe Perches
115656294112SJoe Perches	my $root_path = abs_path($root);
1157f9363b31SGuenter Roeck	my $status = `cd "$root_path"; echo "$license" | scripts/spdxcheck.py -`;
11583b6e8ac9SJoe Perches	return 0 if ($status ne "");
11593b6e8ac9SJoe Perches	return 1;
11603b6e8ac9SJoe Perches}
11613b6e8ac9SJoe Perches
11623445686aSJoe Perchesmy $camelcase_seeded = 0;
11633445686aSJoe Perchessub seed_camelcase_includes {
11643445686aSJoe Perches	return if ($camelcase_seeded);
11653445686aSJoe Perches
11663445686aSJoe Perches	my $files;
1167c707a81dSJoe Perches	my $camelcase_cache = "";
1168c707a81dSJoe Perches	my @include_files = ();
1169c707a81dSJoe Perches
1170c707a81dSJoe Perches	$camelcase_seeded = 1;
1171351b2a1fSJoe Perches
11720f7f635bSJoe Perches	if (-e "$gitroot") {
1173dbbf869dSJoe Perches		my $git_last_include_commit = `${git_command} log --no-merges --pretty=format:"%h%n" -1 -- include`;
1174351b2a1fSJoe Perches		chomp $git_last_include_commit;
1175c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
1176c707a81dSJoe Perches	} else {
1177c707a81dSJoe Perches		my $last_mod_date = 0;
1178c707a81dSJoe Perches		$files = `find $root/include -name "*.h"`;
1179c707a81dSJoe Perches		@include_files = split('\n', $files);
1180c707a81dSJoe Perches		foreach my $file (@include_files) {
1181c707a81dSJoe Perches			my $date = POSIX::strftime("%Y%m%d%H%M",
1182c707a81dSJoe Perches						   localtime((stat $file)[9]));
1183c707a81dSJoe Perches			$last_mod_date = $date if ($last_mod_date < $date);
1184c707a81dSJoe Perches		}
1185c707a81dSJoe Perches		$camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
1186c707a81dSJoe Perches	}
1187c707a81dSJoe Perches
1188c707a81dSJoe Perches	if ($camelcase_cache ne "" && -f $camelcase_cache) {
1189c707a81dSJoe Perches		open(my $camelcase_file, '<', "$camelcase_cache")
1190c707a81dSJoe Perches		    or warn "$P: Can't read '$camelcase_cache' $!\n";
1191351b2a1fSJoe Perches		while (<$camelcase_file>) {
1192351b2a1fSJoe Perches			chomp;
1193351b2a1fSJoe Perches			$camelcase{$_} = 1;
1194351b2a1fSJoe Perches		}
1195351b2a1fSJoe Perches		close($camelcase_file);
1196351b2a1fSJoe Perches
1197351b2a1fSJoe Perches		return;
1198351b2a1fSJoe Perches	}
1199c707a81dSJoe Perches
12000f7f635bSJoe Perches	if (-e "$gitroot") {
1201dbbf869dSJoe Perches		$files = `${git_command} ls-files "include/*.h"`;
1202c707a81dSJoe Perches		@include_files = split('\n', $files);
12033445686aSJoe Perches	}
1204c707a81dSJoe Perches
12053445686aSJoe Perches	foreach my $file (@include_files) {
12063445686aSJoe Perches		seed_camelcase_file($file);
12073445686aSJoe Perches	}
1208351b2a1fSJoe Perches
1209c707a81dSJoe Perches	if ($camelcase_cache ne "") {
1210351b2a1fSJoe Perches		unlink glob ".checkpatch-camelcase.*";
1211c707a81dSJoe Perches		open(my $camelcase_file, '>', "$camelcase_cache")
1212c707a81dSJoe Perches		    or warn "$P: Can't write '$camelcase_cache' $!\n";
1213351b2a1fSJoe Perches		foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
1214351b2a1fSJoe Perches			print $camelcase_file ("$_\n");
1215351b2a1fSJoe Perches		}
1216351b2a1fSJoe Perches		close($camelcase_file);
1217351b2a1fSJoe Perches	}
12183445686aSJoe Perches}
12193445686aSJoe Perches
1220f5f61325SJoe Perchessub git_is_single_file {
1221f5f61325SJoe Perches	my ($filename) = @_;
1222f5f61325SJoe Perches
1223f5f61325SJoe Perches	return 0 if ((which("git") eq "") || !(-e "$gitroot"));
1224f5f61325SJoe Perches
1225f5f61325SJoe Perches	my $output = `${git_command} ls-files -- $filename 2>/dev/null`;
1226f5f61325SJoe Perches	my $count = $output =~ tr/\n//;
1227f5f61325SJoe Perches	return $count eq 1 && $output =~ m{^${filename}$};
1228f5f61325SJoe Perches}
1229f5f61325SJoe Perches
1230d311cd44SJoe Perchessub git_commit_info {
1231d311cd44SJoe Perches	my ($commit, $id, $desc) = @_;
1232d311cd44SJoe Perches
12330f7f635bSJoe Perches	return ($id, $desc) if ((which("git") eq "") || !(-e "$gitroot"));
1234d311cd44SJoe Perches
1235dbbf869dSJoe Perches	my $output = `${git_command} log --no-color --format='%H %s' -1 $commit 2>&1`;
1236d311cd44SJoe Perches	$output =~ s/^\s*//gm;
1237d311cd44SJoe Perches	my @lines = split("\n", $output);
1238d311cd44SJoe Perches
12390d7835fcSJoe Perches	return ($id, $desc) if ($#lines < 0);
12400d7835fcSJoe Perches
12415a7f4455SSean Christopherson	if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous/) {
1242d311cd44SJoe Perches# Maybe one day convert this block of bash into something that returns
1243d311cd44SJoe Perches# all matching commit ids, but it's very slow...
1244d311cd44SJoe Perches#
1245d311cd44SJoe Perches#		echo "checking commits $1..."
1246d311cd44SJoe Perches#		git rev-list --remotes | grep -i "^$1" |
1247d311cd44SJoe Perches#		while read line ; do
1248d311cd44SJoe Perches#		    git log --format='%H %s' -1 $line |
1249d311cd44SJoe Perches#		    echo "commit $(cut -c 1-12,41-)"
1250d311cd44SJoe Perches#		done
12514ce9f970SJoe Perches	} elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./ ||
12524ce9f970SJoe Perches		 $lines[0] =~ /^fatal: bad object $commit/) {
1253948b133aSHeinrich Schuchardt		$id = undef;
1254d311cd44SJoe Perches	} else {
1255d311cd44SJoe Perches		$id = substr($lines[0], 0, 12);
1256d311cd44SJoe Perches		$desc = substr($lines[0], 41);
1257d311cd44SJoe Perches	}
1258d311cd44SJoe Perches
1259d311cd44SJoe Perches	return ($id, $desc);
1260d311cd44SJoe Perches}
1261d311cd44SJoe Perches
12626c72ffaaSAndy Whitcroft$chk_signoff = 0 if ($file);
1263*d5d6281aSDan Carpenter$chk_fixes_tag = 0 if ($file);
12640a920b5bSAndy Whitcroft
126500df344fSAndy Whitcroftmy @rawlines = ();
1266c2fdda0dSAndy Whitcroftmy @lines = ();
12673705ce5bSJoe Perchesmy @fixed = ();
1268d752fcc8SJoe Perchesmy @fixed_inserted = ();
1269d752fcc8SJoe Perchesmy @fixed_deleted = ();
1270194f66fcSJoe Perchesmy $fixlinenr = -1;
1271194f66fcSJoe Perches
12724a593c34SDu, Changbin# If input is git commits, extract all commits from the commit expressions.
12734a593c34SDu, Changbin# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
12740f7f635bSJoe Perchesdie "$P: No git repository found\n" if ($git && !-e "$gitroot");
12754a593c34SDu, Changbin
12764a593c34SDu, Changbinif ($git) {
12774a593c34SDu, Changbin	my @commits = ();
12780dea9f1eSJoe Perches	foreach my $commit_expr (@ARGV) {
12794a593c34SDu, Changbin		my $git_range;
128028898fd1SJoe Perches		if ($commit_expr =~ m/^(.*)-(\d+)$/) {
128128898fd1SJoe Perches			$git_range = "-$2 $1";
12824a593c34SDu, Changbin		} elsif ($commit_expr =~ m/\.\./) {
12834a593c34SDu, Changbin			$git_range = "$commit_expr";
12844a593c34SDu, Changbin		} else {
12850dea9f1eSJoe Perches			$git_range = "-1 $commit_expr";
12860dea9f1eSJoe Perches		}
1287dbbf869dSJoe Perches		my $lines = `${git_command} log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
12880dea9f1eSJoe Perches		foreach my $line (split(/\n/, $lines)) {
128928898fd1SJoe Perches			$line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
129028898fd1SJoe Perches			next if (!defined($1) || !defined($2));
12910dea9f1eSJoe Perches			my $sha1 = $1;
12920dea9f1eSJoe Perches			my $subject = $2;
12930dea9f1eSJoe Perches			unshift(@commits, $sha1);
12940dea9f1eSJoe Perches			$git_commits{$sha1} = $subject;
12954a593c34SDu, Changbin		}
12964a593c34SDu, Changbin	}
12974a593c34SDu, Changbin	die "$P: no git commits after extraction!\n" if (@commits == 0);
12984a593c34SDu, Changbin	@ARGV = @commits;
12994a593c34SDu, Changbin}
13004a593c34SDu, Changbin
1301c2fdda0dSAndy Whitcroftmy $vname;
130298005e8cSVadim Bendebury$allow_c99_comments = !defined $ignore_type{"C99_COMMENT_TOLERANCE"};
13036c72ffaaSAndy Whitcroftfor my $filename (@ARGV) {
130421caa13cSAndy Whitcroft	my $FILE;
1305f5f61325SJoe Perches	my $is_git_file = git_is_single_file($filename);
1306f5f61325SJoe Perches	my $oldfile = $file;
1307f5f61325SJoe Perches	$file = 1 if ($is_git_file);
13084a593c34SDu, Changbin	if ($git) {
13094a593c34SDu, Changbin		open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
13104a593c34SDu, Changbin			die "$P: $filename: git format-patch failed - $!\n";
13114a593c34SDu, Changbin	} elsif ($file) {
131221caa13cSAndy Whitcroft		open($FILE, '-|', "diff -u /dev/null $filename") ||
13136c72ffaaSAndy Whitcroft			die "$P: $filename: diff failed - $!\n";
131421caa13cSAndy Whitcroft	} elsif ($filename eq '-') {
131521caa13cSAndy Whitcroft		open($FILE, '<&STDIN');
13166c72ffaaSAndy Whitcroft	} else {
131721caa13cSAndy Whitcroft		open($FILE, '<', "$filename") ||
13186c72ffaaSAndy Whitcroft			die "$P: $filename: open failed - $!\n";
13196c72ffaaSAndy Whitcroft	}
1320c2fdda0dSAndy Whitcroft	if ($filename eq '-') {
1321c2fdda0dSAndy Whitcroft		$vname = 'Your patch';
13224a593c34SDu, Changbin	} elsif ($git) {
13230dea9f1eSJoe Perches		$vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
1324c2fdda0dSAndy Whitcroft	} else {
1325c2fdda0dSAndy Whitcroft		$vname = $filename;
1326c2fdda0dSAndy Whitcroft	}
132721caa13cSAndy Whitcroft	while (<$FILE>) {
13280a920b5bSAndy Whitcroft		chomp;
132900df344fSAndy Whitcroft		push(@rawlines, $_);
1330c7f574d0SGeert Uytterhoeven		$vname = qq("$1") if ($filename eq '-' && $_ =~ m/^Subject:\s+(.+)/i);
13316c72ffaaSAndy Whitcroft	}
133221caa13cSAndy Whitcroft	close($FILE);
1333d8469f16SJoe Perches
1334d8469f16SJoe Perches	if ($#ARGV > 0 && $quiet == 0) {
1335d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1336d8469f16SJoe Perches		print "$vname\n";
1337d8469f16SJoe Perches		print '-' x length($vname) . "\n";
1338d8469f16SJoe Perches	}
1339d8469f16SJoe Perches
1340c2fdda0dSAndy Whitcroft	if (!process($filename)) {
13410a920b5bSAndy Whitcroft		$exit = 1;
13420a920b5bSAndy Whitcroft	}
134300df344fSAndy Whitcroft	@rawlines = ();
134413214adfSAndy Whitcroft	@lines = ();
13453705ce5bSJoe Perches	@fixed = ();
1346d752fcc8SJoe Perches	@fixed_inserted = ();
1347d752fcc8SJoe Perches	@fixed_deleted = ();
1348194f66fcSJoe Perches	$fixlinenr = -1;
1349485ff23eSAlex Dowad	@modifierListFile = ();
1350485ff23eSAlex Dowad	@typeListFile = ();
1351485ff23eSAlex Dowad	build_types();
1352f5f61325SJoe Perches	$file = $oldfile if ($is_git_file);
13530a920b5bSAndy Whitcroft}
13540a920b5bSAndy Whitcroft
1355d8469f16SJoe Perchesif (!$quiet) {
13563c816e49SJoe Perches	hash_show_words(\%use_type, "Used");
13573c816e49SJoe Perches	hash_show_words(\%ignore_type, "Ignored");
13583c816e49SJoe Perches
13595b57980dSJoe Perches	if (!$perl_version_ok) {
1360d8469f16SJoe Perches		print << "EOM"
1361d8469f16SJoe Perches
1362d8469f16SJoe PerchesNOTE: perl $^V is not modern enough to detect all possible issues.
13635b57980dSJoe Perches      An upgrade to at least perl $minimum_perl_version is suggested.
1364d8469f16SJoe PerchesEOM
1365d8469f16SJoe Perches	}
1366d8469f16SJoe Perches	if ($exit) {
1367d8469f16SJoe Perches		print << "EOM"
1368d8469f16SJoe Perches
1369d8469f16SJoe PerchesNOTE: If any of the errors are false positives, please report
1370d8469f16SJoe Perches      them to the maintainer, see CHECKPATCH in MAINTAINERS.
1371d8469f16SJoe PerchesEOM
1372d8469f16SJoe Perches	}
1373d8469f16SJoe Perches}
1374d8469f16SJoe Perches
13750a920b5bSAndy Whitcroftexit($exit);
13760a920b5bSAndy Whitcroft
13770a920b5bSAndy Whitcroftsub top_of_kernel_tree {
13786c72ffaaSAndy Whitcroft	my ($root) = @_;
13796c72ffaaSAndy Whitcroft
13806c72ffaaSAndy Whitcroft	my @tree_check = (
13816c72ffaaSAndy Whitcroft		"COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
13826c72ffaaSAndy Whitcroft		"README", "Documentation", "arch", "include", "drivers",
13836c72ffaaSAndy Whitcroft		"fs", "init", "ipc", "kernel", "lib", "scripts",
13846c72ffaaSAndy Whitcroft	);
13856c72ffaaSAndy Whitcroft
13866c72ffaaSAndy Whitcroft	foreach my $check (@tree_check) {
13876c72ffaaSAndy Whitcroft		if (! -e $root . '/' . $check) {
13880a920b5bSAndy Whitcroft			return 0;
13890a920b5bSAndy Whitcroft		}
13906c72ffaaSAndy Whitcroft	}
13916c72ffaaSAndy Whitcroft	return 1;
13926c72ffaaSAndy Whitcroft}
13930a920b5bSAndy Whitcroft
139420112475SJoe Perchessub parse_email {
139520112475SJoe Perches	my ($formatted_email) = @_;
139620112475SJoe Perches
139720112475SJoe Perches	my $name = "";
1398fccaebf0SDwaipayan Ray	my $quoted = "";
1399dfa05c28SJoe Perches	my $name_comment = "";
140020112475SJoe Perches	my $address = "";
140120112475SJoe Perches	my $comment = "";
140220112475SJoe Perches
140320112475SJoe Perches	if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
140420112475SJoe Perches		$name = $1;
140520112475SJoe Perches		$address = $2;
140620112475SJoe Perches		$comment = $3 if defined $3;
140720112475SJoe Perches	} elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
140820112475SJoe Perches		$address = $1;
140920112475SJoe Perches		$comment = $2 if defined $2;
141020112475SJoe Perches	} elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
141120112475SJoe Perches		$address = $1;
141220112475SJoe Perches		$comment = $2 if defined $2;
141385e12066SJoe Perches		$formatted_email =~ s/\Q$address\E.*$//;
141420112475SJoe Perches		$name = $formatted_email;
14153705ce5bSJoe Perches		$name = trim($name);
141620112475SJoe Perches		$name =~ s/^\"|\"$//g;
141720112475SJoe Perches		# If there's a name left after stripping spaces and
141820112475SJoe Perches		# leading quotes, and the address doesn't have both
141920112475SJoe Perches		# leading and trailing angle brackets, the address
142020112475SJoe Perches		# is invalid. ie:
142120112475SJoe Perches		#   "joe smith [email protected]" bad
142220112475SJoe Perches		#   "joe smith <[email protected]" bad
142320112475SJoe Perches		if ($name ne "" && $address !~ /^<[^>]+>$/) {
142420112475SJoe Perches			$name = "";
142520112475SJoe Perches			$address = "";
142620112475SJoe Perches			$comment = "";
142720112475SJoe Perches		}
142820112475SJoe Perches	}
142920112475SJoe Perches
1430fccaebf0SDwaipayan Ray	# Extract comments from names excluding quoted parts
1431fccaebf0SDwaipayan Ray	# "John D. (Doe)" - Do not extract
1432fccaebf0SDwaipayan Ray	if ($name =~ s/\"(.+)\"//) {
1433fccaebf0SDwaipayan Ray		$quoted = $1;
1434dfa05c28SJoe Perches	}
1435fccaebf0SDwaipayan Ray	while ($name =~ s/\s*($balanced_parens)\s*/ /) {
1436fccaebf0SDwaipayan Ray		$name_comment .= trim($1);
1437fccaebf0SDwaipayan Ray	}
1438fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
1439fccaebf0SDwaipayan Ray	$name = trim("$quoted $name");
1440fccaebf0SDwaipayan Ray
14413705ce5bSJoe Perches	$address = trim($address);
144220112475SJoe Perches	$address =~ s/^\<|\>$//g;
1443fccaebf0SDwaipayan Ray	$comment = trim($comment);
144420112475SJoe Perches
144520112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
144620112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
144720112475SJoe Perches		$name = "\"$name\"";
144820112475SJoe Perches	}
144920112475SJoe Perches
1450dfa05c28SJoe Perches	return ($name, $name_comment, $address, $comment);
145120112475SJoe Perches}
145220112475SJoe Perches
145320112475SJoe Perchessub format_email {
145448ca2d8aSDwaipayan Ray	my ($name, $name_comment, $address, $comment) = @_;
145520112475SJoe Perches
145620112475SJoe Perches	my $formatted_email;
145720112475SJoe Perches
1458fccaebf0SDwaipayan Ray	$name =~ s/^[ \"]+|[ \"]+$//g;
14593705ce5bSJoe Perches	$address = trim($address);
1460fccaebf0SDwaipayan Ray	$address =~ s/(?:\.|\,|\")+$//; ##trailing commas, dots or quotes
146120112475SJoe Perches
146220112475SJoe Perches	if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
146320112475SJoe Perches		$name =~ s/(?<!\\)"/\\"/g; ##escape quotes
146420112475SJoe Perches		$name = "\"$name\"";
146520112475SJoe Perches	}
146620112475SJoe Perches
1467fccaebf0SDwaipayan Ray	$name_comment = trim($name_comment);
1468fccaebf0SDwaipayan Ray	$name_comment = " $name_comment" if ($name_comment ne "");
1469fccaebf0SDwaipayan Ray	$comment = trim($comment);
1470fccaebf0SDwaipayan Ray	$comment = " $comment" if ($comment ne "");
1471fccaebf0SDwaipayan Ray
147220112475SJoe Perches	if ("$name" eq "") {
147320112475SJoe Perches		$formatted_email = "$address";
147420112475SJoe Perches	} else {
147548ca2d8aSDwaipayan Ray		$formatted_email = "$name$name_comment <$address>";
147620112475SJoe Perches	}
147748ca2d8aSDwaipayan Ray	$formatted_email .= "$comment";
147820112475SJoe Perches	return $formatted_email;
147920112475SJoe Perches}
148020112475SJoe Perches
1481dfa05c28SJoe Perchessub reformat_email {
1482dfa05c28SJoe Perches	my ($email) = @_;
1483dfa05c28SJoe Perches
1484dfa05c28SJoe Perches	my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
148548ca2d8aSDwaipayan Ray	return format_email($email_name, $name_comment, $email_address, $comment);
1486dfa05c28SJoe Perches}
1487dfa05c28SJoe Perches
1488dfa05c28SJoe Perchessub same_email_addresses {
1489fccaebf0SDwaipayan Ray	my ($email1, $email2) = @_;
1490dfa05c28SJoe Perches
1491dfa05c28SJoe Perches	my ($email1_name, $name1_comment, $email1_address, $comment1) = parse_email($email1);
1492dfa05c28SJoe Perches	my ($email2_name, $name2_comment, $email2_address, $comment2) = parse_email($email2);
1493dfa05c28SJoe Perches
149448ca2d8aSDwaipayan Ray	return $email1_name eq $email2_name &&
149548ca2d8aSDwaipayan Ray	       $email1_address eq $email2_address &&
149648ca2d8aSDwaipayan Ray	       $name1_comment eq $name2_comment &&
149748ca2d8aSDwaipayan Ray	       $comment1 eq $comment2;
149848ca2d8aSDwaipayan Ray}
1499dfa05c28SJoe Perches
1500d311cd44SJoe Perchessub which {
1501d311cd44SJoe Perches	my ($bin) = @_;
1502d311cd44SJoe Perches
1503d311cd44SJoe Perches	foreach my $path (split(/:/, $ENV{PATH})) {
1504d311cd44SJoe Perches		if (-e "$path/$bin") {
1505d311cd44SJoe Perches			return "$path/$bin";
1506d311cd44SJoe Perches		}
1507d311cd44SJoe Perches	}
1508d311cd44SJoe Perches
1509d311cd44SJoe Perches	return "";
1510d311cd44SJoe Perches}
1511d311cd44SJoe Perches
1512000d1cc1SJoe Perchessub which_conf {
1513000d1cc1SJoe Perches	my ($conf) = @_;
1514000d1cc1SJoe Perches
1515000d1cc1SJoe Perches	foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1516000d1cc1SJoe Perches		if (-e "$path/$conf") {
1517000d1cc1SJoe Perches			return "$path/$conf";
1518000d1cc1SJoe Perches		}
1519000d1cc1SJoe Perches	}
1520000d1cc1SJoe Perches
1521000d1cc1SJoe Perches	return "";
1522000d1cc1SJoe Perches}
1523000d1cc1SJoe Perches
15240a920b5bSAndy Whitcroftsub expand_tabs {
15250a920b5bSAndy Whitcroft	my ($str) = @_;
15260a920b5bSAndy Whitcroft
15270a920b5bSAndy Whitcroft	my $res = '';
15280a920b5bSAndy Whitcroft	my $n = 0;
15290a920b5bSAndy Whitcroft	for my $c (split(//, $str)) {
15300a920b5bSAndy Whitcroft		if ($c eq "\t") {
15310a920b5bSAndy Whitcroft			$res .= ' ';
15320a920b5bSAndy Whitcroft			$n++;
1533713a09deSAntonio Borneo			for (; ($n % $tabsize) != 0; $n++) {
15340a920b5bSAndy Whitcroft				$res .= ' ';
15350a920b5bSAndy Whitcroft			}
15360a920b5bSAndy Whitcroft			next;
15370a920b5bSAndy Whitcroft		}
15380a920b5bSAndy Whitcroft		$res .= $c;
15390a920b5bSAndy Whitcroft		$n++;
15400a920b5bSAndy Whitcroft	}
15410a920b5bSAndy Whitcroft
15420a920b5bSAndy Whitcroft	return $res;
15430a920b5bSAndy Whitcroft}
15446c72ffaaSAndy Whitcroftsub copy_spacing {
1545773647a0SAndy Whitcroft	(my $res = shift) =~ tr/\t/ /c;
15466c72ffaaSAndy Whitcroft	return $res;
15476c72ffaaSAndy Whitcroft}
15480a920b5bSAndy Whitcroft
15494a0df2efSAndy Whitcroftsub line_stats {
15504a0df2efSAndy Whitcroft	my ($line) = @_;
15514a0df2efSAndy Whitcroft
15524a0df2efSAndy Whitcroft	# Drop the diff line leader and expand tabs
15534a0df2efSAndy Whitcroft	$line =~ s/^.//;
15544a0df2efSAndy Whitcroft	$line = expand_tabs($line);
15554a0df2efSAndy Whitcroft
15564a0df2efSAndy Whitcroft	# Pick the indent from the front of the line.
15574a0df2efSAndy Whitcroft	my ($white) = ($line =~ /^(\s*)/);
15584a0df2efSAndy Whitcroft
15594a0df2efSAndy Whitcroft	return (length($line), length($white));
15604a0df2efSAndy Whitcroft}
15614a0df2efSAndy Whitcroft
1562773647a0SAndy Whitcroftmy $sanitise_quote = '';
1563773647a0SAndy Whitcroft
1564773647a0SAndy Whitcroftsub sanitise_line_reset {
1565773647a0SAndy Whitcroft	my ($in_comment) = @_;
1566773647a0SAndy Whitcroft
1567773647a0SAndy Whitcroft	if ($in_comment) {
1568773647a0SAndy Whitcroft		$sanitise_quote = '*/';
1569773647a0SAndy Whitcroft	} else {
1570773647a0SAndy Whitcroft		$sanitise_quote = '';
1571773647a0SAndy Whitcroft	}
1572773647a0SAndy Whitcroft}
157300df344fSAndy Whitcroftsub sanitise_line {
157400df344fSAndy Whitcroft	my ($line) = @_;
157500df344fSAndy Whitcroft
157600df344fSAndy Whitcroft	my $res = '';
157700df344fSAndy Whitcroft	my $l = '';
157800df344fSAndy Whitcroft
1579c2fdda0dSAndy Whitcroft	my $qlen = 0;
1580773647a0SAndy Whitcroft	my $off = 0;
1581773647a0SAndy Whitcroft	my $c;
158200df344fSAndy Whitcroft
1583773647a0SAndy Whitcroft	# Always copy over the diff marker.
1584773647a0SAndy Whitcroft	$res = substr($line, 0, 1);
1585773647a0SAndy Whitcroft
1586773647a0SAndy Whitcroft	for ($off = 1; $off < length($line); $off++) {
1587773647a0SAndy Whitcroft		$c = substr($line, $off, 1);
1588773647a0SAndy Whitcroft
15898d2e11b2SClaudio Fontana		# Comments we are whacking completely including the begin
1590773647a0SAndy Whitcroft		# and end, all to $;.
1591773647a0SAndy Whitcroft		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1592773647a0SAndy Whitcroft			$sanitise_quote = '*/';
1593773647a0SAndy Whitcroft
1594773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1595773647a0SAndy Whitcroft			$off++;
159600df344fSAndy Whitcroft			next;
1597773647a0SAndy Whitcroft		}
159881bc0e02SAndy Whitcroft		if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
1599773647a0SAndy Whitcroft			$sanitise_quote = '';
1600773647a0SAndy Whitcroft			substr($res, $off, 2, "$;$;");
1601773647a0SAndy Whitcroft			$off++;
1602773647a0SAndy Whitcroft			next;
1603773647a0SAndy Whitcroft		}
1604113f04a8SDaniel Walker		if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1605113f04a8SDaniel Walker			$sanitise_quote = '//';
1606113f04a8SDaniel Walker
1607113f04a8SDaniel Walker			substr($res, $off, 2, $sanitise_quote);
1608113f04a8SDaniel Walker			$off++;
1609113f04a8SDaniel Walker			next;
1610113f04a8SDaniel Walker		}
1611773647a0SAndy Whitcroft
1612773647a0SAndy Whitcroft		# A \ in a string means ignore the next character.
1613773647a0SAndy Whitcroft		if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1614773647a0SAndy Whitcroft		    $c eq "\\") {
1615773647a0SAndy Whitcroft			substr($res, $off, 2, 'XX');
1616773647a0SAndy Whitcroft			$off++;
1617773647a0SAndy Whitcroft			next;
1618773647a0SAndy Whitcroft		}
1619773647a0SAndy Whitcroft		# Regular quotes.
1620773647a0SAndy Whitcroft		if ($c eq "'" || $c eq '"') {
1621773647a0SAndy Whitcroft			if ($sanitise_quote eq '') {
1622773647a0SAndy Whitcroft				$sanitise_quote = $c;
1623773647a0SAndy Whitcroft
1624773647a0SAndy Whitcroft				substr($res, $off, 1, $c);
1625773647a0SAndy Whitcroft				next;
1626773647a0SAndy Whitcroft			} elsif ($sanitise_quote eq $c) {
1627773647a0SAndy Whitcroft				$sanitise_quote = '';
162800df344fSAndy Whitcroft			}
162900df344fSAndy Whitcroft		}
1630773647a0SAndy Whitcroft
1631fae17daeSAndy Whitcroft		#print "c<$c> SQ<$sanitise_quote>\n";
1632773647a0SAndy Whitcroft		if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1633773647a0SAndy Whitcroft			substr($res, $off, 1, $;);
1634113f04a8SDaniel Walker		} elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1635113f04a8SDaniel Walker			substr($res, $off, 1, $;);
1636773647a0SAndy Whitcroft		} elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1637773647a0SAndy Whitcroft			substr($res, $off, 1, 'X');
163800df344fSAndy Whitcroft		} else {
1639773647a0SAndy Whitcroft			substr($res, $off, 1, $c);
164000df344fSAndy Whitcroft		}
1641c2fdda0dSAndy Whitcroft	}
1642c2fdda0dSAndy Whitcroft
1643113f04a8SDaniel Walker	if ($sanitise_quote eq '//') {
1644113f04a8SDaniel Walker		$sanitise_quote = '';
1645113f04a8SDaniel Walker	}
1646113f04a8SDaniel Walker
1647c2fdda0dSAndy Whitcroft	# The pathname on a #include may be surrounded by '<' and '>'.
1648c45dcabdSAndy Whitcroft	if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
1649c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1650c2fdda0dSAndy Whitcroft		$res =~ s@\<.*\>@<$clean>@;
1651c2fdda0dSAndy Whitcroft
1652c2fdda0dSAndy Whitcroft	# The whole of a #error is a string.
1653c45dcabdSAndy Whitcroft	} elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
1654c2fdda0dSAndy Whitcroft		my $clean = 'X' x length($1);
1655c45dcabdSAndy Whitcroft		$res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
1656c2fdda0dSAndy Whitcroft	}
1657c2fdda0dSAndy Whitcroft
1658dadf680dSJoe Perches	if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1659dadf680dSJoe Perches		my $match = $1;
1660dadf680dSJoe Perches		$res =~ s/\Q$match\E/"$;" x length($match)/e;
1661dadf680dSJoe Perches	}
1662dadf680dSJoe Perches
166300df344fSAndy Whitcroft	return $res;
166400df344fSAndy Whitcroft}
166500df344fSAndy Whitcroft
1666a6962d72SJoe Perchessub get_quoted_string {
1667a6962d72SJoe Perches	my ($line, $rawline) = @_;
1668a6962d72SJoe Perches
1669478b1799SJoe Perches	return "" if (!defined($line) || !defined($rawline));
167033acb54aSJoe Perches	return "" if ($line !~ m/($String)/g);
1671a6962d72SJoe Perches	return substr($rawline, $-[0], $+[0] - $-[0]);
1672a6962d72SJoe Perches}
1673a6962d72SJoe Perches
16748905a67cSAndy Whitcroftsub ctx_statement_block {
16758905a67cSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
16768905a67cSAndy Whitcroft	my $line = $linenr - 1;
16778905a67cSAndy Whitcroft	my $blk = '';
16788905a67cSAndy Whitcroft	my $soff = $off;
16798905a67cSAndy Whitcroft	my $coff = $off - 1;
1680773647a0SAndy Whitcroft	my $coff_set = 0;
16818905a67cSAndy Whitcroft
168213214adfSAndy Whitcroft	my $loff = 0;
168313214adfSAndy Whitcroft
16848905a67cSAndy Whitcroft	my $type = '';
16858905a67cSAndy Whitcroft	my $level = 0;
1686a2750645SAndy Whitcroft	my @stack = ();
1687cf655043SAndy Whitcroft	my $p;
16888905a67cSAndy Whitcroft	my $c;
16898905a67cSAndy Whitcroft	my $len = 0;
169013214adfSAndy Whitcroft
169113214adfSAndy Whitcroft	my $remainder;
16928905a67cSAndy Whitcroft	while (1) {
1693a2750645SAndy Whitcroft		@stack = (['', 0]) if ($#stack == -1);
1694a2750645SAndy Whitcroft
1695773647a0SAndy Whitcroft		#warn "CSB: blk<$blk> remain<$remain>\n";
16968905a67cSAndy Whitcroft		# If we are about to drop off the end, pull in more
16978905a67cSAndy Whitcroft		# context.
16988905a67cSAndy Whitcroft		if ($off >= $len) {
16998905a67cSAndy Whitcroft			for (; $remain > 0; $line++) {
1700dea33496SAndy Whitcroft				last if (!defined $lines[$line]);
1701c2fdda0dSAndy Whitcroft				next if ($lines[$line] =~ /^-/);
17028905a67cSAndy Whitcroft				$remain--;
170313214adfSAndy Whitcroft				$loff = $len;
1704c2fdda0dSAndy Whitcroft				$blk .= $lines[$line] . "\n";
17058905a67cSAndy Whitcroft				$len = length($blk);
17068905a67cSAndy Whitcroft				$line++;
17078905a67cSAndy Whitcroft				last;
17088905a67cSAndy Whitcroft			}
17098905a67cSAndy Whitcroft			# Bail if there is no further context.
17108905a67cSAndy Whitcroft			#warn "CSB: blk<$blk> off<$off> len<$len>\n";
171113214adfSAndy Whitcroft			if ($off >= $len) {
17128905a67cSAndy Whitcroft				last;
17138905a67cSAndy Whitcroft			}
1714f74bd194SAndy Whitcroft			if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1715f74bd194SAndy Whitcroft				$level++;
1716f74bd194SAndy Whitcroft				$type = '#';
1717f74bd194SAndy Whitcroft			}
17188905a67cSAndy Whitcroft		}
1719cf655043SAndy Whitcroft		$p = $c;
17208905a67cSAndy Whitcroft		$c = substr($blk, $off, 1);
172113214adfSAndy Whitcroft		$remainder = substr($blk, $off);
17228905a67cSAndy Whitcroft
1723773647a0SAndy Whitcroft		#warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
17244635f4fbSAndy Whitcroft
17254635f4fbSAndy Whitcroft		# Handle nested #if/#else.
17264635f4fbSAndy Whitcroft		if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
17274635f4fbSAndy Whitcroft			push(@stack, [ $type, $level ]);
17284635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
17294635f4fbSAndy Whitcroft			($type, $level) = @{$stack[$#stack - 1]};
17304635f4fbSAndy Whitcroft		} elsif ($remainder =~ /^#\s*endif\b/) {
17314635f4fbSAndy Whitcroft			($type, $level) = @{pop(@stack)};
17324635f4fbSAndy Whitcroft		}
17334635f4fbSAndy Whitcroft
17348905a67cSAndy Whitcroft		# Statement ends at the ';' or a close '}' at the
17358905a67cSAndy Whitcroft		# outermost level.
17368905a67cSAndy Whitcroft		if ($level == 0 && $c eq ';') {
17378905a67cSAndy Whitcroft			last;
17388905a67cSAndy Whitcroft		}
17398905a67cSAndy Whitcroft
174013214adfSAndy Whitcroft		# An else is really a conditional as long as its not else if
1741773647a0SAndy Whitcroft		if ($level == 0 && $coff_set == 0 &&
1742773647a0SAndy Whitcroft				(!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1743773647a0SAndy Whitcroft				$remainder =~ /^(else)(?:\s|{)/ &&
1744773647a0SAndy Whitcroft				$remainder !~ /^else\s+if\b/) {
1745773647a0SAndy Whitcroft			$coff = $off + length($1) - 1;
1746773647a0SAndy Whitcroft			$coff_set = 1;
1747773647a0SAndy Whitcroft			#warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1748773647a0SAndy Whitcroft			#warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
174913214adfSAndy Whitcroft		}
175013214adfSAndy Whitcroft
17518905a67cSAndy Whitcroft		if (($type eq '' || $type eq '(') && $c eq '(') {
17528905a67cSAndy Whitcroft			$level++;
17538905a67cSAndy Whitcroft			$type = '(';
17548905a67cSAndy Whitcroft		}
17558905a67cSAndy Whitcroft		if ($type eq '(' && $c eq ')') {
17568905a67cSAndy Whitcroft			$level--;
17578905a67cSAndy Whitcroft			$type = ($level != 0)? '(' : '';
17588905a67cSAndy Whitcroft
17598905a67cSAndy Whitcroft			if ($level == 0 && $coff < $soff) {
17608905a67cSAndy Whitcroft				$coff = $off;
1761773647a0SAndy Whitcroft				$coff_set = 1;
1762773647a0SAndy Whitcroft				#warn "CSB: mark coff<$coff>\n";
17638905a67cSAndy Whitcroft			}
17648905a67cSAndy Whitcroft		}
17658905a67cSAndy Whitcroft		if (($type eq '' || $type eq '{') && $c eq '{') {
17668905a67cSAndy Whitcroft			$level++;
17678905a67cSAndy Whitcroft			$type = '{';
17688905a67cSAndy Whitcroft		}
17698905a67cSAndy Whitcroft		if ($type eq '{' && $c eq '}') {
17708905a67cSAndy Whitcroft			$level--;
17718905a67cSAndy Whitcroft			$type = ($level != 0)? '{' : '';
17728905a67cSAndy Whitcroft
17738905a67cSAndy Whitcroft			if ($level == 0) {
1774b998e001SPatrick Pannuto				if (substr($blk, $off + 1, 1) eq ';') {
1775b998e001SPatrick Pannuto					$off++;
1776b998e001SPatrick Pannuto				}
17778905a67cSAndy Whitcroft				last;
17788905a67cSAndy Whitcroft			}
17798905a67cSAndy Whitcroft		}
1780f74bd194SAndy Whitcroft		# Preprocessor commands end at the newline unless escaped.
1781f74bd194SAndy Whitcroft		if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1782f74bd194SAndy Whitcroft			$level--;
1783f74bd194SAndy Whitcroft			$type = '';
1784f74bd194SAndy Whitcroft			$off++;
1785f74bd194SAndy Whitcroft			last;
1786f74bd194SAndy Whitcroft		}
17878905a67cSAndy Whitcroft		$off++;
17888905a67cSAndy Whitcroft	}
1789a3bb97a7SAndy Whitcroft	# We are truly at the end, so shuffle to the next line.
179013214adfSAndy Whitcroft	if ($off == $len) {
1791a3bb97a7SAndy Whitcroft		$loff = $len + 1;
179213214adfSAndy Whitcroft		$line++;
179313214adfSAndy Whitcroft		$remain--;
179413214adfSAndy Whitcroft	}
17958905a67cSAndy Whitcroft
17968905a67cSAndy Whitcroft	my $statement = substr($blk, $soff, $off - $soff + 1);
17978905a67cSAndy Whitcroft	my $condition = substr($blk, $soff, $coff - $soff + 1);
17988905a67cSAndy Whitcroft
17998905a67cSAndy Whitcroft	#warn "STATEMENT<$statement>\n";
18008905a67cSAndy Whitcroft	#warn "CONDITION<$condition>\n";
18018905a67cSAndy Whitcroft
1802773647a0SAndy Whitcroft	#print "coff<$coff> soff<$off> loff<$loff>\n";
180313214adfSAndy Whitcroft
180413214adfSAndy Whitcroft	return ($statement, $condition,
180513214adfSAndy Whitcroft			$line, $remain + 1, $off - $loff + 1, $level);
180613214adfSAndy Whitcroft}
180713214adfSAndy Whitcroft
1808cf655043SAndy Whitcroftsub statement_lines {
1809cf655043SAndy Whitcroft	my ($stmt) = @_;
1810cf655043SAndy Whitcroft
1811cf655043SAndy Whitcroft	# Strip the diff line prefixes and rip blank lines at start and end.
1812cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1813cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1814cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1815cf655043SAndy Whitcroft
1816cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1817cf655043SAndy Whitcroft
1818cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1819cf655043SAndy Whitcroft}
1820cf655043SAndy Whitcroft
1821cf655043SAndy Whitcroftsub statement_rawlines {
1822cf655043SAndy Whitcroft	my ($stmt) = @_;
1823cf655043SAndy Whitcroft
1824cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1825cf655043SAndy Whitcroft
1826cf655043SAndy Whitcroft	return $#stmt_lines + 2;
1827cf655043SAndy Whitcroft}
1828cf655043SAndy Whitcroft
1829cf655043SAndy Whitcroftsub statement_block_size {
1830cf655043SAndy Whitcroft	my ($stmt) = @_;
1831cf655043SAndy Whitcroft
1832cf655043SAndy Whitcroft	$stmt =~ s/(^|\n)./$1/g;
1833cf655043SAndy Whitcroft	$stmt =~ s/^\s*{//;
1834cf655043SAndy Whitcroft	$stmt =~ s/}\s*$//;
1835cf655043SAndy Whitcroft	$stmt =~ s/^\s*//;
1836cf655043SAndy Whitcroft	$stmt =~ s/\s*$//;
1837cf655043SAndy Whitcroft
1838cf655043SAndy Whitcroft	my @stmt_lines = ($stmt =~ /\n/g);
1839cf655043SAndy Whitcroft	my @stmt_statements = ($stmt =~ /;/g);
1840cf655043SAndy Whitcroft
1841cf655043SAndy Whitcroft	my $stmt_lines = $#stmt_lines + 2;
1842cf655043SAndy Whitcroft	my $stmt_statements = $#stmt_statements + 1;
1843cf655043SAndy Whitcroft
1844cf655043SAndy Whitcroft	if ($stmt_lines > $stmt_statements) {
1845cf655043SAndy Whitcroft		return $stmt_lines;
1846cf655043SAndy Whitcroft	} else {
1847cf655043SAndy Whitcroft		return $stmt_statements;
1848cf655043SAndy Whitcroft	}
1849cf655043SAndy Whitcroft}
1850cf655043SAndy Whitcroft
185113214adfSAndy Whitcroftsub ctx_statement_full {
185213214adfSAndy Whitcroft	my ($linenr, $remain, $off) = @_;
185313214adfSAndy Whitcroft	my ($statement, $condition, $level);
185413214adfSAndy Whitcroft
185513214adfSAndy Whitcroft	my (@chunks);
185613214adfSAndy Whitcroft
1857cf655043SAndy Whitcroft	# Grab the first conditional/block pair.
185813214adfSAndy Whitcroft	($statement, $condition, $linenr, $remain, $off, $level) =
185913214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1860773647a0SAndy Whitcroft	#print "F: c<$condition> s<$statement> remain<$remain>\n";
186113214adfSAndy Whitcroft	push(@chunks, [ $condition, $statement ]);
1862cf655043SAndy Whitcroft	if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1863cf655043SAndy Whitcroft		return ($level, $linenr, @chunks);
1864cf655043SAndy Whitcroft	}
1865cf655043SAndy Whitcroft
1866cf655043SAndy Whitcroft	# Pull in the following conditional/block pairs and see if they
1867cf655043SAndy Whitcroft	# could continue the statement.
1868cf655043SAndy Whitcroft	for (;;) {
186913214adfSAndy Whitcroft		($statement, $condition, $linenr, $remain, $off, $level) =
187013214adfSAndy Whitcroft				ctx_statement_block($linenr, $remain, $off);
1871cf655043SAndy Whitcroft		#print "C: c<$condition> s<$statement> remain<$remain>\n";
1872773647a0SAndy Whitcroft		last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
1873cf655043SAndy Whitcroft		#print "C: push\n";
1874cf655043SAndy Whitcroft		push(@chunks, [ $condition, $statement ]);
187513214adfSAndy Whitcroft	}
187613214adfSAndy Whitcroft
187713214adfSAndy Whitcroft	return ($level, $linenr, @chunks);
18788905a67cSAndy Whitcroft}
18798905a67cSAndy Whitcroft
18804a0df2efSAndy Whitcroftsub ctx_block_get {
1881f0a594c1SAndy Whitcroft	my ($linenr, $remain, $outer, $open, $close, $off) = @_;
18824a0df2efSAndy Whitcroft	my $line;
18834a0df2efSAndy Whitcroft	my $start = $linenr - 1;
18844a0df2efSAndy Whitcroft	my $blk = '';
18854a0df2efSAndy Whitcroft	my @o;
18864a0df2efSAndy Whitcroft	my @c;
18874a0df2efSAndy Whitcroft	my @res = ();
18884a0df2efSAndy Whitcroft
1889f0a594c1SAndy Whitcroft	my $level = 0;
18904635f4fbSAndy Whitcroft	my @stack = ($level);
189100df344fSAndy Whitcroft	for ($line = $start; $remain > 0; $line++) {
189200df344fSAndy Whitcroft		next if ($rawlines[$line] =~ /^-/);
189300df344fSAndy Whitcroft		$remain--;
189400df344fSAndy Whitcroft
189500df344fSAndy Whitcroft		$blk .= $rawlines[$line];
18964635f4fbSAndy Whitcroft
18974635f4fbSAndy Whitcroft		# Handle nested #if/#else.
189801464f30SAndy Whitcroft		if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
18994635f4fbSAndy Whitcroft			push(@stack, $level);
190001464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
19014635f4fbSAndy Whitcroft			$level = $stack[$#stack - 1];
190201464f30SAndy Whitcroft		} elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
19034635f4fbSAndy Whitcroft			$level = pop(@stack);
19044635f4fbSAndy Whitcroft		}
19054635f4fbSAndy Whitcroft
190601464f30SAndy Whitcroft		foreach my $c (split(//, $lines[$line])) {
1907f0a594c1SAndy Whitcroft			##print "C<$c>L<$level><$open$close>O<$off>\n";
1908f0a594c1SAndy Whitcroft			if ($off > 0) {
1909f0a594c1SAndy Whitcroft				$off--;
1910f0a594c1SAndy Whitcroft				next;
1911f0a594c1SAndy Whitcroft			}
19124a0df2efSAndy Whitcroft
1913f0a594c1SAndy Whitcroft			if ($c eq $close && $level > 0) {
1914f0a594c1SAndy Whitcroft				$level--;
1915f0a594c1SAndy Whitcroft				last if ($level == 0);
1916f0a594c1SAndy Whitcroft			} elsif ($c eq $open) {
1917f0a594c1SAndy Whitcroft				$level++;
1918f0a594c1SAndy Whitcroft			}
1919f0a594c1SAndy Whitcroft		}
19204a0df2efSAndy Whitcroft
1921f0a594c1SAndy Whitcroft		if (!$outer || $level <= 1) {
192200df344fSAndy Whitcroft			push(@res, $rawlines[$line]);
19234a0df2efSAndy Whitcroft		}
19244a0df2efSAndy Whitcroft
1925f0a594c1SAndy Whitcroft		last if ($level == 0);
19264a0df2efSAndy Whitcroft	}
19274a0df2efSAndy Whitcroft
1928f0a594c1SAndy Whitcroft	return ($level, @res);
19294a0df2efSAndy Whitcroft}
19304a0df2efSAndy Whitcroftsub ctx_block_outer {
19314a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
19324a0df2efSAndy Whitcroft
1933f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1934f0a594c1SAndy Whitcroft	return @r;
19354a0df2efSAndy Whitcroft}
19364a0df2efSAndy Whitcroftsub ctx_block {
19374a0df2efSAndy Whitcroft	my ($linenr, $remain) = @_;
19384a0df2efSAndy Whitcroft
1939f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1940f0a594c1SAndy Whitcroft	return @r;
1941653d4876SAndy Whitcroft}
1942653d4876SAndy Whitcroftsub ctx_statement {
1943f0a594c1SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
1944f0a594c1SAndy Whitcroft
1945f0a594c1SAndy Whitcroft	my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1946f0a594c1SAndy Whitcroft	return @r;
1947f0a594c1SAndy Whitcroft}
1948f0a594c1SAndy Whitcroftsub ctx_block_level {
1949653d4876SAndy Whitcroft	my ($linenr, $remain) = @_;
1950653d4876SAndy Whitcroft
1951f0a594c1SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
19524a0df2efSAndy Whitcroft}
19539c0ca6f9SAndy Whitcroftsub ctx_statement_level {
19549c0ca6f9SAndy Whitcroft	my ($linenr, $remain, $off) = @_;
19559c0ca6f9SAndy Whitcroft
19569c0ca6f9SAndy Whitcroft	return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
19579c0ca6f9SAndy Whitcroft}
19584a0df2efSAndy Whitcroft
19594a0df2efSAndy Whitcroftsub ctx_locate_comment {
19604a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
19614a0df2efSAndy Whitcroft
1962a55ee0ccSJoe Perches	# If c99 comment on the current line, or the line before or after
1963a55ee0ccSJoe Perches	my ($current_comment) = ($rawlines[$end_line - 1] =~ m@^\+.*(//.*$)@);
1964a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1965a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 2] =~ m@^[\+ ].*(//.*$)@);
1966a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1967a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line] =~ m@^[\+ ].*(//.*$)@);
1968a55ee0ccSJoe Perches	return $current_comment if (defined $current_comment);
1969a55ee0ccSJoe Perches
19704a0df2efSAndy Whitcroft	# Catch a comment on the end of the line itself.
1971a55ee0ccSJoe Perches	($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
19724a0df2efSAndy Whitcroft	return $current_comment if (defined $current_comment);
19734a0df2efSAndy Whitcroft
19744a0df2efSAndy Whitcroft	# Look through the context and try and figure out if there is a
19754a0df2efSAndy Whitcroft	# comment.
19764a0df2efSAndy Whitcroft	my $in_comment = 0;
19774a0df2efSAndy Whitcroft	$current_comment = '';
19784a0df2efSAndy Whitcroft	for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
197900df344fSAndy Whitcroft		my $line = $rawlines[$linenr - 1];
198000df344fSAndy Whitcroft		#warn "           $line\n";
19814a0df2efSAndy Whitcroft		if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
19824a0df2efSAndy Whitcroft			$in_comment = 1;
19834a0df2efSAndy Whitcroft		}
19844a0df2efSAndy Whitcroft		if ($line =~ m@/\*@) {
19854a0df2efSAndy Whitcroft			$in_comment = 1;
19864a0df2efSAndy Whitcroft		}
19874a0df2efSAndy Whitcroft		if (!$in_comment && $current_comment ne '') {
19884a0df2efSAndy Whitcroft			$current_comment = '';
19894a0df2efSAndy Whitcroft		}
19904a0df2efSAndy Whitcroft		$current_comment .= $line . "\n" if ($in_comment);
19914a0df2efSAndy Whitcroft		if ($line =~ m@\*/@) {
19924a0df2efSAndy Whitcroft			$in_comment = 0;
19934a0df2efSAndy Whitcroft		}
19944a0df2efSAndy Whitcroft	}
19954a0df2efSAndy Whitcroft
19964a0df2efSAndy Whitcroft	chomp($current_comment);
19974a0df2efSAndy Whitcroft	return($current_comment);
19984a0df2efSAndy Whitcroft}
19994a0df2efSAndy Whitcroftsub ctx_has_comment {
20004a0df2efSAndy Whitcroft	my ($first_line, $end_line) = @_;
20014a0df2efSAndy Whitcroft	my $cmt = ctx_locate_comment($first_line, $end_line);
20024a0df2efSAndy Whitcroft
200300df344fSAndy Whitcroft	##print "LINE: $rawlines[$end_line - 1 ]\n";
20044a0df2efSAndy Whitcroft	##print "CMMT: $cmt\n";
20054a0df2efSAndy Whitcroft
20064a0df2efSAndy Whitcroft	return ($cmt ne '');
20074a0df2efSAndy Whitcroft}
20084a0df2efSAndy Whitcroft
20094d001e4dSAndy Whitcroftsub raw_line {
20104d001e4dSAndy Whitcroft	my ($linenr, $cnt) = @_;
20114d001e4dSAndy Whitcroft
20124d001e4dSAndy Whitcroft	my $offset = $linenr - 1;
20134d001e4dSAndy Whitcroft	$cnt++;
20144d001e4dSAndy Whitcroft
20154d001e4dSAndy Whitcroft	my $line;
20164d001e4dSAndy Whitcroft	while ($cnt) {
20174d001e4dSAndy Whitcroft		$line = $rawlines[$offset++];
20184d001e4dSAndy Whitcroft		next if (defined($line) && $line =~ /^-/);
20194d001e4dSAndy Whitcroft		$cnt--;
20204d001e4dSAndy Whitcroft	}
20214d001e4dSAndy Whitcroft
20224d001e4dSAndy Whitcroft	return $line;
20234d001e4dSAndy Whitcroft}
20244d001e4dSAndy Whitcroft
20252a9f9d85STobin C. Hardingsub get_stat_real {
20262a9f9d85STobin C. Harding	my ($linenr, $lc) = @_;
20272a9f9d85STobin C. Harding
20282a9f9d85STobin C. Harding	my $stat_real = raw_line($linenr, 0);
20292a9f9d85STobin C. Harding	for (my $count = $linenr + 1; $count <= $lc; $count++) {
20302a9f9d85STobin C. Harding		$stat_real = $stat_real . "\n" . raw_line($count, 0);
20312a9f9d85STobin C. Harding	}
20322a9f9d85STobin C. Harding
20332a9f9d85STobin C. Harding	return $stat_real;
20342a9f9d85STobin C. Harding}
20352a9f9d85STobin C. Harding
2036e3d95a2aSTobin C. Hardingsub get_stat_here {
2037e3d95a2aSTobin C. Harding	my ($linenr, $cnt, $here) = @_;
2038e3d95a2aSTobin C. Harding
2039e3d95a2aSTobin C. Harding	my $herectx = $here . "\n";
2040e3d95a2aSTobin C. Harding	for (my $n = 0; $n < $cnt; $n++) {
2041e3d95a2aSTobin C. Harding		$herectx .= raw_line($linenr, $n) . "\n";
2042e3d95a2aSTobin C. Harding	}
2043e3d95a2aSTobin C. Harding
2044e3d95a2aSTobin C. Harding	return $herectx;
2045e3d95a2aSTobin C. Harding}
2046e3d95a2aSTobin C. Harding
20470a920b5bSAndy Whitcroftsub cat_vet {
20480a920b5bSAndy Whitcroft	my ($vet) = @_;
20499c0ca6f9SAndy Whitcroft	my ($res, $coded);
20500a920b5bSAndy Whitcroft
20519c0ca6f9SAndy Whitcroft	$res = '';
20526c72ffaaSAndy Whitcroft	while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
20536c72ffaaSAndy Whitcroft		$res .= $1;
20546c72ffaaSAndy Whitcroft		if ($2 ne '') {
20559c0ca6f9SAndy Whitcroft			$coded = sprintf("^%c", unpack('C', $2) + 64);
20566c72ffaaSAndy Whitcroft			$res .= $coded;
20576c72ffaaSAndy Whitcroft		}
20589c0ca6f9SAndy Whitcroft	}
20599c0ca6f9SAndy Whitcroft	$res =~ s/$/\$/;
20600a920b5bSAndy Whitcroft
20619c0ca6f9SAndy Whitcroft	return $res;
20620a920b5bSAndy Whitcroft}
20630a920b5bSAndy Whitcroft
2064c2fdda0dSAndy Whitcroftmy $av_preprocessor = 0;
2065cf655043SAndy Whitcroftmy $av_pending;
2066c2fdda0dSAndy Whitcroftmy @av_paren_type;
20671f65f947SAndy Whitcroftmy $av_pend_colon;
2068c2fdda0dSAndy Whitcroft
2069c2fdda0dSAndy Whitcroftsub annotate_reset {
2070c2fdda0dSAndy Whitcroft	$av_preprocessor = 0;
2071cf655043SAndy Whitcroft	$av_pending = '_';
2072cf655043SAndy Whitcroft	@av_paren_type = ('E');
20731f65f947SAndy Whitcroft	$av_pend_colon = 'O';
2074c2fdda0dSAndy Whitcroft}
2075c2fdda0dSAndy Whitcroft
20766c72ffaaSAndy Whitcroftsub annotate_values {
20776c72ffaaSAndy Whitcroft	my ($stream, $type) = @_;
20786c72ffaaSAndy Whitcroft
20796c72ffaaSAndy Whitcroft	my $res;
20801f65f947SAndy Whitcroft	my $var = '_' x length($stream);
20816c72ffaaSAndy Whitcroft	my $cur = $stream;
20826c72ffaaSAndy Whitcroft
2083c2fdda0dSAndy Whitcroft	print "$stream\n" if ($dbg_values > 1);
20846c72ffaaSAndy Whitcroft
20856c72ffaaSAndy Whitcroft	while (length($cur)) {
2086773647a0SAndy Whitcroft		@av_paren_type = ('E') if ($#av_paren_type < 0);
2087cf655043SAndy Whitcroft		print " <" . join('', @av_paren_type) .
2088171ae1a4SAndy Whitcroft				"> <$type> <$av_pending>" if ($dbg_values > 1);
20896c72ffaaSAndy Whitcroft		if ($cur =~ /^(\s+)/o) {
2090c2fdda0dSAndy Whitcroft			print "WS($1)\n" if ($dbg_values > 1);
2091c2fdda0dSAndy Whitcroft			if ($1 =~ /\n/ && $av_preprocessor) {
2092cf655043SAndy Whitcroft				$type = pop(@av_paren_type);
2093c2fdda0dSAndy Whitcroft				$av_preprocessor = 0;
20946c72ffaaSAndy Whitcroft			}
20956c72ffaaSAndy Whitcroft
2096c023e473SFlorian Mickler		} elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
20979446ef56SAndy Whitcroft			print "CAST($1)\n" if ($dbg_values > 1);
20989446ef56SAndy Whitcroft			push(@av_paren_type, $type);
2099addcdceaSAndy Whitcroft			$type = 'c';
21009446ef56SAndy Whitcroft
2101e91b6e26SAndy Whitcroft		} elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
2102c2fdda0dSAndy Whitcroft			print "DECLARE($1)\n" if ($dbg_values > 1);
21036c72ffaaSAndy Whitcroft			$type = 'T';
21046c72ffaaSAndy Whitcroft
2105389a2fe5SAndy Whitcroft		} elsif ($cur =~ /^($Modifier)\s*/) {
2106389a2fe5SAndy Whitcroft			print "MODIFIER($1)\n" if ($dbg_values > 1);
2107389a2fe5SAndy Whitcroft			$type = 'T';
2108389a2fe5SAndy Whitcroft
2109c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
2110171ae1a4SAndy Whitcroft			print "DEFINE($1,$2)\n" if ($dbg_values > 1);
2111c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
2112171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
2113171ae1a4SAndy Whitcroft			if ($2 ne '') {
2114cf655043SAndy Whitcroft				$av_pending = 'N';
2115171ae1a4SAndy Whitcroft			}
2116171ae1a4SAndy Whitcroft			$type = 'E';
2117171ae1a4SAndy Whitcroft
2118c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
2119171ae1a4SAndy Whitcroft			print "UNDEF($1)\n" if ($dbg_values > 1);
2120171ae1a4SAndy Whitcroft			$av_preprocessor = 1;
2121171ae1a4SAndy Whitcroft			push(@av_paren_type, $type);
21226c72ffaaSAndy Whitcroft
2123c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
2124cf655043SAndy Whitcroft			print "PRE_START($1)\n" if ($dbg_values > 1);
2125c2fdda0dSAndy Whitcroft			$av_preprocessor = 1;
2126cf655043SAndy Whitcroft
2127cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2128cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2129171ae1a4SAndy Whitcroft			$type = 'E';
2130cf655043SAndy Whitcroft
2131c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
2132cf655043SAndy Whitcroft			print "PRE_RESTART($1)\n" if ($dbg_values > 1);
2133cf655043SAndy Whitcroft			$av_preprocessor = 1;
2134cf655043SAndy Whitcroft
2135cf655043SAndy Whitcroft			push(@av_paren_type, $av_paren_type[$#av_paren_type]);
2136cf655043SAndy Whitcroft
2137171ae1a4SAndy Whitcroft			$type = 'E';
2138cf655043SAndy Whitcroft
2139c45dcabdSAndy Whitcroft		} elsif ($cur =~ /^(\#\s*(?:endif))/o) {
2140cf655043SAndy Whitcroft			print "PRE_END($1)\n" if ($dbg_values > 1);
2141cf655043SAndy Whitcroft
2142cf655043SAndy Whitcroft			$av_preprocessor = 1;
2143cf655043SAndy Whitcroft
2144cf655043SAndy Whitcroft			# Assume all arms of the conditional end as this
2145cf655043SAndy Whitcroft			# one does, and continue as if the #endif was not here.
2146cf655043SAndy Whitcroft			pop(@av_paren_type);
2147cf655043SAndy Whitcroft			push(@av_paren_type, $type);
2148171ae1a4SAndy Whitcroft			$type = 'E';
21496c72ffaaSAndy Whitcroft
21506c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\\\n)/o) {
2151c2fdda0dSAndy Whitcroft			print "PRECONT($1)\n" if ($dbg_values > 1);
21526c72ffaaSAndy Whitcroft
2153171ae1a4SAndy Whitcroft		} elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
2154171ae1a4SAndy Whitcroft			print "ATTR($1)\n" if ($dbg_values > 1);
2155171ae1a4SAndy Whitcroft			$av_pending = $type;
2156171ae1a4SAndy Whitcroft			$type = 'N';
2157171ae1a4SAndy Whitcroft
21586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
2159c2fdda0dSAndy Whitcroft			print "SIZEOF($1)\n" if ($dbg_values > 1);
21606c72ffaaSAndy Whitcroft			if (defined $2) {
2161cf655043SAndy Whitcroft				$av_pending = 'V';
21626c72ffaaSAndy Whitcroft			}
21636c72ffaaSAndy Whitcroft			$type = 'N';
21646c72ffaaSAndy Whitcroft
216514b111c1SAndy Whitcroft		} elsif ($cur =~ /^(if|while|for)\b/o) {
2166c2fdda0dSAndy Whitcroft			print "COND($1)\n" if ($dbg_values > 1);
216714b111c1SAndy Whitcroft			$av_pending = 'E';
21686c72ffaaSAndy Whitcroft			$type = 'N';
21696c72ffaaSAndy Whitcroft
21701f65f947SAndy Whitcroft		} elsif ($cur =~/^(case)/o) {
21711f65f947SAndy Whitcroft			print "CASE($1)\n" if ($dbg_values > 1);
21721f65f947SAndy Whitcroft			$av_pend_colon = 'C';
21731f65f947SAndy Whitcroft			$type = 'N';
21741f65f947SAndy Whitcroft
217514b111c1SAndy Whitcroft		} elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
2176c2fdda0dSAndy Whitcroft			print "KEYWORD($1)\n" if ($dbg_values > 1);
21776c72ffaaSAndy Whitcroft			$type = 'N';
21786c72ffaaSAndy Whitcroft
21796c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\()/o) {
2180c2fdda0dSAndy Whitcroft			print "PAREN('$1')\n" if ($dbg_values > 1);
2181cf655043SAndy Whitcroft			push(@av_paren_type, $av_pending);
2182cf655043SAndy Whitcroft			$av_pending = '_';
21836c72ffaaSAndy Whitcroft			$type = 'N';
21846c72ffaaSAndy Whitcroft
21856c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^(\))/o) {
2186cf655043SAndy Whitcroft			my $new_type = pop(@av_paren_type);
2187cf655043SAndy Whitcroft			if ($new_type ne '_') {
2188cf655043SAndy Whitcroft				$type = $new_type;
2189c2fdda0dSAndy Whitcroft				print "PAREN('$1') -> $type\n"
2190c2fdda0dSAndy Whitcroft							if ($dbg_values > 1);
21916c72ffaaSAndy Whitcroft			} else {
2192c2fdda0dSAndy Whitcroft				print "PAREN('$1')\n" if ($dbg_values > 1);
21936c72ffaaSAndy Whitcroft			}
21946c72ffaaSAndy Whitcroft
2195c8cb2ca3SAndy Whitcroft		} elsif ($cur =~ /^($Ident)\s*\(/o) {
2196c2fdda0dSAndy Whitcroft			print "FUNC($1)\n" if ($dbg_values > 1);
2197c8cb2ca3SAndy Whitcroft			$type = 'V';
2198cf655043SAndy Whitcroft			$av_pending = 'V';
21996c72ffaaSAndy Whitcroft
22008e761b04SAndy Whitcroft		} elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
22018e761b04SAndy Whitcroft			if (defined $2 && $type eq 'C' || $type eq 'T') {
22021f65f947SAndy Whitcroft				$av_pend_colon = 'B';
22038e761b04SAndy Whitcroft			} elsif ($type eq 'E') {
22048e761b04SAndy Whitcroft				$av_pend_colon = 'L';
22051f65f947SAndy Whitcroft			}
22061f65f947SAndy Whitcroft			print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
22071f65f947SAndy Whitcroft			$type = 'V';
22081f65f947SAndy Whitcroft
22096c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Ident|$Constant)/o) {
2210c2fdda0dSAndy Whitcroft			print "IDENT($1)\n" if ($dbg_values > 1);
22116c72ffaaSAndy Whitcroft			$type = 'V';
22126c72ffaaSAndy Whitcroft
22136c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Assignment)/o) {
2214c2fdda0dSAndy Whitcroft			print "ASSIGN($1)\n" if ($dbg_values > 1);
22156c72ffaaSAndy Whitcroft			$type = 'N';
22166c72ffaaSAndy Whitcroft
2217cf655043SAndy Whitcroft		} elsif ($cur =~/^(;|{|})/) {
2218c2fdda0dSAndy Whitcroft			print "END($1)\n" if ($dbg_values > 1);
221913214adfSAndy Whitcroft			$type = 'E';
22201f65f947SAndy Whitcroft			$av_pend_colon = 'O';
222113214adfSAndy Whitcroft
22228e761b04SAndy Whitcroft		} elsif ($cur =~/^(,)/) {
22238e761b04SAndy Whitcroft			print "COMMA($1)\n" if ($dbg_values > 1);
22248e761b04SAndy Whitcroft			$type = 'C';
22258e761b04SAndy Whitcroft
22261f65f947SAndy Whitcroft		} elsif ($cur =~ /^(\?)/o) {
22271f65f947SAndy Whitcroft			print "QUESTION($1)\n" if ($dbg_values > 1);
22281f65f947SAndy Whitcroft			$type = 'N';
22291f65f947SAndy Whitcroft
22301f65f947SAndy Whitcroft		} elsif ($cur =~ /^(:)/o) {
22311f65f947SAndy Whitcroft			print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
22321f65f947SAndy Whitcroft
22331f65f947SAndy Whitcroft			substr($var, length($res), 1, $av_pend_colon);
22341f65f947SAndy Whitcroft			if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
22351f65f947SAndy Whitcroft				$type = 'E';
22361f65f947SAndy Whitcroft			} else {
22371f65f947SAndy Whitcroft				$type = 'N';
22381f65f947SAndy Whitcroft			}
22391f65f947SAndy Whitcroft			$av_pend_colon = 'O';
22401f65f947SAndy Whitcroft
22418e761b04SAndy Whitcroft		} elsif ($cur =~ /^(\[)/o) {
224213214adfSAndy Whitcroft			print "CLOSE($1)\n" if ($dbg_values > 1);
22436c72ffaaSAndy Whitcroft			$type = 'N';
22446c72ffaaSAndy Whitcroft
22450d413866SAndy Whitcroft		} elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
224674048ed8SAndy Whitcroft			my $variant;
224774048ed8SAndy Whitcroft
224874048ed8SAndy Whitcroft			print "OPV($1)\n" if ($dbg_values > 1);
224974048ed8SAndy Whitcroft			if ($type eq 'V') {
225074048ed8SAndy Whitcroft				$variant = 'B';
225174048ed8SAndy Whitcroft			} else {
225274048ed8SAndy Whitcroft				$variant = 'U';
225374048ed8SAndy Whitcroft			}
225474048ed8SAndy Whitcroft
225574048ed8SAndy Whitcroft			substr($var, length($res), 1, $variant);
225674048ed8SAndy Whitcroft			$type = 'N';
225774048ed8SAndy Whitcroft
22586c72ffaaSAndy Whitcroft		} elsif ($cur =~ /^($Operators)/o) {
2259c2fdda0dSAndy Whitcroft			print "OP($1)\n" if ($dbg_values > 1);
22606c72ffaaSAndy Whitcroft			if ($1 ne '++' && $1 ne '--') {
22616c72ffaaSAndy Whitcroft				$type = 'N';
22626c72ffaaSAndy Whitcroft			}
22636c72ffaaSAndy Whitcroft
22646c72ffaaSAndy Whitcroft		} elsif ($cur =~ /(^.)/o) {
2265c2fdda0dSAndy Whitcroft			print "C($1)\n" if ($dbg_values > 1);
22666c72ffaaSAndy Whitcroft		}
22676c72ffaaSAndy Whitcroft		if (defined $1) {
22686c72ffaaSAndy Whitcroft			$cur = substr($cur, length($1));
22696c72ffaaSAndy Whitcroft			$res .= $type x length($1);
22706c72ffaaSAndy Whitcroft		}
22716c72ffaaSAndy Whitcroft	}
22726c72ffaaSAndy Whitcroft
22731f65f947SAndy Whitcroft	return ($res, $var);
22746c72ffaaSAndy Whitcroft}
22756c72ffaaSAndy Whitcroft
22768905a67cSAndy Whitcroftsub possible {
227713214adfSAndy Whitcroft	my ($possible, $line) = @_;
22789a974fdbSAndy Whitcroft	my $notPermitted = qr{(?:
22790776e594SAndy Whitcroft		^(?:
22800776e594SAndy Whitcroft			$Modifier|
22810776e594SAndy Whitcroft			$Storage|
22820776e594SAndy Whitcroft			$Type|
22839a974fdbSAndy Whitcroft			DEFINE_\S+
22849a974fdbSAndy Whitcroft		)$|
22859a974fdbSAndy Whitcroft		^(?:
22860776e594SAndy Whitcroft			goto|
22870776e594SAndy Whitcroft			return|
22880776e594SAndy Whitcroft			case|
22890776e594SAndy Whitcroft			else|
22900776e594SAndy Whitcroft			asm|__asm__|
229189a88353SAndy Whitcroft			do|
229289a88353SAndy Whitcroft			\#|
229389a88353SAndy Whitcroft			\#\#|
22949a974fdbSAndy Whitcroft		)(?:\s|$)|
22950776e594SAndy Whitcroft		^(?:typedef|struct|enum)\b
22969a974fdbSAndy Whitcroft	    )}x;
22979a974fdbSAndy Whitcroft	warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
22989a974fdbSAndy Whitcroft	if ($possible !~ $notPermitted) {
2299c45dcabdSAndy Whitcroft		# Check for modifiers.
2300c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Storage\s*//g;
2301c45dcabdSAndy Whitcroft		$possible =~ s/\s*$Sparse\s*//g;
2302c45dcabdSAndy Whitcroft		if ($possible =~ /^\s*$/) {
2303c45dcabdSAndy Whitcroft
2304c45dcabdSAndy Whitcroft		} elsif ($possible =~ /\s/) {
2305c45dcabdSAndy Whitcroft			$possible =~ s/\s*$Type\s*//g;
2306d2506586SAndy Whitcroft			for my $modifier (split(' ', $possible)) {
23079a974fdbSAndy Whitcroft				if ($modifier !~ $notPermitted) {
2308d2506586SAndy Whitcroft					warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
2309485ff23eSAlex Dowad					push(@modifierListFile, $modifier);
2310d2506586SAndy Whitcroft				}
23119a974fdbSAndy Whitcroft			}
2312c45dcabdSAndy Whitcroft
2313c45dcabdSAndy Whitcroft		} else {
231413214adfSAndy Whitcroft			warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
2315485ff23eSAlex Dowad			push(@typeListFile, $possible);
2316c45dcabdSAndy Whitcroft		}
23178905a67cSAndy Whitcroft		build_types();
23180776e594SAndy Whitcroft	} else {
23190776e594SAndy Whitcroft		warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
23208905a67cSAndy Whitcroft	}
23218905a67cSAndy Whitcroft}
23228905a67cSAndy Whitcroft
23236c72ffaaSAndy Whitcroftmy $prefix = '';
23246c72ffaaSAndy Whitcroft
2325000d1cc1SJoe Perchessub show_type {
2326cbec18afSJoe Perches	my ($type) = @_;
232791bfe484SJoe Perches
2328522b837cSAlexey Dobriyan	$type =~ tr/[a-z]/[A-Z]/;
2329522b837cSAlexey Dobriyan
2330cbec18afSJoe Perches	return defined $use_type{$type} if (scalar keys %use_type > 0);
2331cbec18afSJoe Perches
2332cbec18afSJoe Perches	return !defined $ignore_type{$type};
2333000d1cc1SJoe Perches}
2334000d1cc1SJoe Perches
2335f0a594c1SAndy Whitcroftsub report {
2336cbec18afSJoe Perches	my ($level, $type, $msg) = @_;
2337cbec18afSJoe Perches
2338cbec18afSJoe Perches	if (!show_type($type) ||
2339cbec18afSJoe Perches	    (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
2340773647a0SAndy Whitcroft		return 0;
2341773647a0SAndy Whitcroft	}
234257230297SJoe Perches	my $output = '';
2343737c0767SJohn Brooks	if ($color) {
234457230297SJoe Perches		if ($level eq 'ERROR') {
234557230297SJoe Perches			$output .= RED;
234657230297SJoe Perches		} elsif ($level eq 'WARNING') {
234757230297SJoe Perches			$output .= YELLOW;
2348000d1cc1SJoe Perches		} else {
234957230297SJoe Perches			$output .= GREEN;
2350000d1cc1SJoe Perches		}
235157230297SJoe Perches	}
235257230297SJoe Perches	$output .= $prefix . $level . ':';
235357230297SJoe Perches	if ($show_types) {
2354737c0767SJohn Brooks		$output .= BLUE if ($color);
235557230297SJoe Perches		$output .= "$type:";
235657230297SJoe Perches	}
2357737c0767SJohn Brooks	$output .= RESET if ($color);
235857230297SJoe Perches	$output .= ' ' . $msg . "\n";
235934d8815fSJoe Perches
236034d8815fSJoe Perches	if ($showfile) {
236134d8815fSJoe Perches		my @lines = split("\n", $output, -1);
236234d8815fSJoe Perches		splice(@lines, 1, 1);
236334d8815fSJoe Perches		$output = join("\n", @lines);
236434d8815fSJoe Perches	}
236552178ce0SDwaipayan Ray
236652178ce0SDwaipayan Ray	if ($terse) {
236752178ce0SDwaipayan Ray		$output = (split('\n', $output))[0] . "\n";
236852178ce0SDwaipayan Ray	}
236952178ce0SDwaipayan Ray
237052178ce0SDwaipayan Ray	if ($verbose && exists($verbose_messages{$type}) &&
237152178ce0SDwaipayan Ray	    !exists($verbose_emitted{$type})) {
237252178ce0SDwaipayan Ray		$output .= $verbose_messages{$type} . "\n\n";
237352178ce0SDwaipayan Ray		$verbose_emitted{$type} = 1;
237452178ce0SDwaipayan Ray	}
23758905a67cSAndy Whitcroft
237657230297SJoe Perches	push(our @report, $output);
2377773647a0SAndy Whitcroft
2378773647a0SAndy Whitcroft	return 1;
2379f0a594c1SAndy Whitcroft}
2380cbec18afSJoe Perches
2381f0a594c1SAndy Whitcroftsub report_dump {
238213214adfSAndy Whitcroft	our @report;
2383f0a594c1SAndy Whitcroft}
2384000d1cc1SJoe Perches
2385d752fcc8SJoe Perchessub fixup_current_range {
2386d752fcc8SJoe Perches	my ($lineRef, $offset, $length) = @_;
2387d752fcc8SJoe Perches
2388d752fcc8SJoe Perches	if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
2389d752fcc8SJoe Perches		my $o = $1;
2390d752fcc8SJoe Perches		my $l = $2;
2391d752fcc8SJoe Perches		my $no = $o + $offset;
2392d752fcc8SJoe Perches		my $nl = $l + $length;
2393d752fcc8SJoe Perches		$$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
2394d752fcc8SJoe Perches	}
2395d752fcc8SJoe Perches}
2396d752fcc8SJoe Perches
2397d752fcc8SJoe Perchessub fix_inserted_deleted_lines {
2398d752fcc8SJoe Perches	my ($linesRef, $insertedRef, $deletedRef) = @_;
2399d752fcc8SJoe Perches
2400d752fcc8SJoe Perches	my $range_last_linenr = 0;
2401d752fcc8SJoe Perches	my $delta_offset = 0;
2402d752fcc8SJoe Perches
2403d752fcc8SJoe Perches	my $old_linenr = 0;
2404d752fcc8SJoe Perches	my $new_linenr = 0;
2405d752fcc8SJoe Perches
2406d752fcc8SJoe Perches	my $next_insert = 0;
2407d752fcc8SJoe Perches	my $next_delete = 0;
2408d752fcc8SJoe Perches
2409d752fcc8SJoe Perches	my @lines = ();
2410d752fcc8SJoe Perches
2411d752fcc8SJoe Perches	my $inserted = @{$insertedRef}[$next_insert++];
2412d752fcc8SJoe Perches	my $deleted = @{$deletedRef}[$next_delete++];
2413d752fcc8SJoe Perches
2414d752fcc8SJoe Perches	foreach my $old_line (@{$linesRef}) {
2415d752fcc8SJoe Perches		my $save_line = 1;
2416d752fcc8SJoe Perches		my $line = $old_line;	#don't modify the array
2417323b267fSJoe Perches		if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) {	#new filename
2418d752fcc8SJoe Perches			$delta_offset = 0;
2419d752fcc8SJoe Perches		} elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) {	#new hunk
2420d752fcc8SJoe Perches			$range_last_linenr = $new_linenr;
2421d752fcc8SJoe Perches			fixup_current_range(\$line, $delta_offset, 0);
2422d752fcc8SJoe Perches		}
2423d752fcc8SJoe Perches
2424d752fcc8SJoe Perches		while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
2425d752fcc8SJoe Perches			$deleted = @{$deletedRef}[$next_delete++];
2426d752fcc8SJoe Perches			$save_line = 0;
2427d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
2428d752fcc8SJoe Perches		}
2429d752fcc8SJoe Perches
2430d752fcc8SJoe Perches		while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
2431d752fcc8SJoe Perches			push(@lines, ${$inserted}{'LINE'});
2432d752fcc8SJoe Perches			$inserted = @{$insertedRef}[$next_insert++];
2433d752fcc8SJoe Perches			$new_linenr++;
2434d752fcc8SJoe Perches			fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
2435d752fcc8SJoe Perches		}
2436d752fcc8SJoe Perches
2437d752fcc8SJoe Perches		if ($save_line) {
2438d752fcc8SJoe Perches			push(@lines, $line);
2439d752fcc8SJoe Perches			$new_linenr++;
2440d752fcc8SJoe Perches		}
2441d752fcc8SJoe Perches
2442d752fcc8SJoe Perches		$old_linenr++;
2443d752fcc8SJoe Perches	}
2444d752fcc8SJoe Perches
2445d752fcc8SJoe Perches	return @lines;
2446d752fcc8SJoe Perches}
2447d752fcc8SJoe Perches
2448f2d7e4d4SJoe Perchessub fix_insert_line {
2449f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2450f2d7e4d4SJoe Perches
2451f2d7e4d4SJoe Perches	my $inserted = {
2452f2d7e4d4SJoe Perches		LINENR => $linenr,
2453f2d7e4d4SJoe Perches		LINE => $line,
2454f2d7e4d4SJoe Perches	};
2455f2d7e4d4SJoe Perches	push(@fixed_inserted, $inserted);
2456f2d7e4d4SJoe Perches}
2457f2d7e4d4SJoe Perches
2458f2d7e4d4SJoe Perchessub fix_delete_line {
2459f2d7e4d4SJoe Perches	my ($linenr, $line) = @_;
2460f2d7e4d4SJoe Perches
2461f2d7e4d4SJoe Perches	my $deleted = {
2462f2d7e4d4SJoe Perches		LINENR => $linenr,
2463f2d7e4d4SJoe Perches		LINE => $line,
2464f2d7e4d4SJoe Perches	};
2465f2d7e4d4SJoe Perches
2466f2d7e4d4SJoe Perches	push(@fixed_deleted, $deleted);
2467f2d7e4d4SJoe Perches}
2468f2d7e4d4SJoe Perches
2469de7d4f0eSAndy Whitcroftsub ERROR {
2470cbec18afSJoe Perches	my ($type, $msg) = @_;
2471cbec18afSJoe Perches
2472cbec18afSJoe Perches	if (report("ERROR", $type, $msg)) {
2473de7d4f0eSAndy Whitcroft		our $clean = 0;
24746c72ffaaSAndy Whitcroft		our $cnt_error++;
24753705ce5bSJoe Perches		return 1;
2476de7d4f0eSAndy Whitcroft	}
24773705ce5bSJoe Perches	return 0;
2478773647a0SAndy Whitcroft}
2479de7d4f0eSAndy Whitcroftsub WARN {
2480cbec18afSJoe Perches	my ($type, $msg) = @_;
2481cbec18afSJoe Perches
2482cbec18afSJoe Perches	if (report("WARNING", $type, $msg)) {
2483de7d4f0eSAndy Whitcroft		our $clean = 0;
24846c72ffaaSAndy Whitcroft		our $cnt_warn++;
24853705ce5bSJoe Perches		return 1;
2486de7d4f0eSAndy Whitcroft	}
24873705ce5bSJoe Perches	return 0;
2488773647a0SAndy Whitcroft}
2489de7d4f0eSAndy Whitcroftsub CHK {
2490cbec18afSJoe Perches	my ($type, $msg) = @_;
2491cbec18afSJoe Perches
2492cbec18afSJoe Perches	if ($check && report("CHECK", $type, $msg)) {
2493de7d4f0eSAndy Whitcroft		our $clean = 0;
24946c72ffaaSAndy Whitcroft		our $cnt_chk++;
24953705ce5bSJoe Perches		return 1;
24966c72ffaaSAndy Whitcroft	}
24973705ce5bSJoe Perches	return 0;
2498de7d4f0eSAndy Whitcroft}
2499de7d4f0eSAndy Whitcroft
25006ecd9674SAndy Whitcroftsub check_absolute_file {
25016ecd9674SAndy Whitcroft	my ($absolute, $herecurr) = @_;
25026ecd9674SAndy Whitcroft	my $file = $absolute;
25036ecd9674SAndy Whitcroft
25046ecd9674SAndy Whitcroft	##print "absolute<$absolute>\n";
25056ecd9674SAndy Whitcroft
25066ecd9674SAndy Whitcroft	# See if any suffix of this path is a path within the tree.
25076ecd9674SAndy Whitcroft	while ($file =~ s@^[^/]*/@@) {
25086ecd9674SAndy Whitcroft		if (-f "$root/$file") {
25096ecd9674SAndy Whitcroft			##print "file<$file>\n";
25106ecd9674SAndy Whitcroft			last;
25116ecd9674SAndy Whitcroft		}
25126ecd9674SAndy Whitcroft	}
25136ecd9674SAndy Whitcroft	if (! -f _)  {
25146ecd9674SAndy Whitcroft		return 0;
25156ecd9674SAndy Whitcroft	}
25166ecd9674SAndy Whitcroft
25176ecd9674SAndy Whitcroft	# It is, so see if the prefix is acceptable.
25186ecd9674SAndy Whitcroft	my $prefix = $absolute;
25196ecd9674SAndy Whitcroft	substr($prefix, -length($file)) = '';
25206ecd9674SAndy Whitcroft
25216ecd9674SAndy Whitcroft	##print "prefix<$prefix>\n";
25226ecd9674SAndy Whitcroft	if ($prefix ne ".../") {
2523000d1cc1SJoe Perches		WARN("USE_RELATIVE_PATH",
2524000d1cc1SJoe Perches		     "use relative pathname instead of absolute in changelog text\n" . $herecurr);
25256ecd9674SAndy Whitcroft	}
25266ecd9674SAndy Whitcroft}
25276ecd9674SAndy Whitcroft
25283705ce5bSJoe Perchessub trim {
25293705ce5bSJoe Perches	my ($string) = @_;
25303705ce5bSJoe Perches
2531b34c648bSJoe Perches	$string =~ s/^\s+|\s+$//g;
2532b34c648bSJoe Perches
2533b34c648bSJoe Perches	return $string;
2534b34c648bSJoe Perches}
2535b34c648bSJoe Perches
2536b34c648bSJoe Perchessub ltrim {
2537b34c648bSJoe Perches	my ($string) = @_;
2538b34c648bSJoe Perches
2539b34c648bSJoe Perches	$string =~ s/^\s+//;
2540b34c648bSJoe Perches
2541b34c648bSJoe Perches	return $string;
2542b34c648bSJoe Perches}
2543b34c648bSJoe Perches
2544b34c648bSJoe Perchessub rtrim {
2545b34c648bSJoe Perches	my ($string) = @_;
2546b34c648bSJoe Perches
2547b34c648bSJoe Perches	$string =~ s/\s+$//;
25483705ce5bSJoe Perches
25493705ce5bSJoe Perches	return $string;
25503705ce5bSJoe Perches}
25513705ce5bSJoe Perches
255252ea8506SJoe Perchessub string_find_replace {
255352ea8506SJoe Perches	my ($string, $find, $replace) = @_;
255452ea8506SJoe Perches
255552ea8506SJoe Perches	$string =~ s/$find/$replace/g;
255652ea8506SJoe Perches
255752ea8506SJoe Perches	return $string;
255852ea8506SJoe Perches}
255952ea8506SJoe Perches
25603705ce5bSJoe Perchessub tabify {
25613705ce5bSJoe Perches	my ($leading) = @_;
25623705ce5bSJoe Perches
2563713a09deSAntonio Borneo	my $source_indent = $tabsize;
25643705ce5bSJoe Perches	my $max_spaces_before_tab = $source_indent - 1;
25653705ce5bSJoe Perches	my $spaces_to_tab = " " x $source_indent;
25663705ce5bSJoe Perches
25673705ce5bSJoe Perches	#convert leading spaces to tabs
25683705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
25693705ce5bSJoe Perches	#Remove spaces before a tab
25703705ce5bSJoe Perches	1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
25713705ce5bSJoe Perches
25723705ce5bSJoe Perches	return "$leading";
25733705ce5bSJoe Perches}
25743705ce5bSJoe Perches
2575d1fe9c09SJoe Perchessub pos_last_openparen {
2576d1fe9c09SJoe Perches	my ($line) = @_;
2577d1fe9c09SJoe Perches
2578d1fe9c09SJoe Perches	my $pos = 0;
2579d1fe9c09SJoe Perches
2580d1fe9c09SJoe Perches	my $opens = $line =~ tr/\(/\(/;
2581d1fe9c09SJoe Perches	my $closes = $line =~ tr/\)/\)/;
2582d1fe9c09SJoe Perches
2583d1fe9c09SJoe Perches	my $last_openparen = 0;
2584d1fe9c09SJoe Perches
2585d1fe9c09SJoe Perches	if (($opens == 0) || ($closes >= $opens)) {
2586d1fe9c09SJoe Perches		return -1;
2587d1fe9c09SJoe Perches	}
2588d1fe9c09SJoe Perches
2589d1fe9c09SJoe Perches	my $len = length($line);
2590d1fe9c09SJoe Perches
2591d1fe9c09SJoe Perches	for ($pos = 0; $pos < $len; $pos++) {
2592d1fe9c09SJoe Perches		my $string = substr($line, $pos);
2593d1fe9c09SJoe Perches		if ($string =~ /^($FuncArg|$balanced_parens)/) {
2594d1fe9c09SJoe Perches			$pos += length($1) - 1;
2595d1fe9c09SJoe Perches		} elsif (substr($line, $pos, 1) eq '(') {
2596d1fe9c09SJoe Perches			$last_openparen = $pos;
2597d1fe9c09SJoe Perches		} elsif (index($string, '(') == -1) {
2598d1fe9c09SJoe Perches			last;
2599d1fe9c09SJoe Perches		}
2600d1fe9c09SJoe Perches	}
2601d1fe9c09SJoe Perches
260291cb5195SJoe Perches	return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
2603d1fe9c09SJoe Perches}
2604d1fe9c09SJoe Perches
2605f36d3eb8SJoe Perchessub get_raw_comment {
2606f36d3eb8SJoe Perches	my ($line, $rawline) = @_;
2607f36d3eb8SJoe Perches	my $comment = '';
2608f36d3eb8SJoe Perches
2609f36d3eb8SJoe Perches	for my $i (0 .. (length($line) - 1)) {
2610f36d3eb8SJoe Perches		if (substr($line, $i, 1) eq "$;") {
2611f36d3eb8SJoe Perches			$comment .= substr($rawline, $i, 1);
2612f36d3eb8SJoe Perches		}
2613f36d3eb8SJoe Perches	}
2614f36d3eb8SJoe Perches
2615f36d3eb8SJoe Perches	return $comment;
2616f36d3eb8SJoe Perches}
2617f36d3eb8SJoe Perches
26185b8f82e1SSong Liusub exclude_global_initialisers {
26195b8f82e1SSong Liu	my ($realfile) = @_;
26205b8f82e1SSong Liu
26215b8f82e1SSong Liu	# Do not check for BPF programs (tools/testing/selftests/bpf/progs/*.c, samples/bpf/*_kern.c, *.bpf.c).
26225b8f82e1SSong Liu	return $realfile =~ m@^tools/testing/selftests/bpf/progs/.*\.c$@ ||
26235b8f82e1SSong Liu		$realfile =~ m@^samples/bpf/.*_kern\.c$@ ||
26245b8f82e1SSong Liu		$realfile =~ m@/bpf/.*\.bpf\.c$@;
26255b8f82e1SSong Liu}
26265b8f82e1SSong Liu
26270a920b5bSAndy Whitcroftsub process {
26280a920b5bSAndy Whitcroft	my $filename = shift;
26290a920b5bSAndy Whitcroft
26300a920b5bSAndy Whitcroft	my $linenr=0;
26310a920b5bSAndy Whitcroft	my $prevline="";
2632c2fdda0dSAndy Whitcroft	my $prevrawline="";
26330a920b5bSAndy Whitcroft	my $stashline="";
2634c2fdda0dSAndy Whitcroft	my $stashrawline="";
26350a920b5bSAndy Whitcroft
26364a0df2efSAndy Whitcroft	my $length;
26370a920b5bSAndy Whitcroft	my $indent;
26380a920b5bSAndy Whitcroft	my $previndent=0;
26390a920b5bSAndy Whitcroft	my $stashindent=0;
26400a920b5bSAndy Whitcroft
2641de7d4f0eSAndy Whitcroft	our $clean = 1;
26420a920b5bSAndy Whitcroft	my $signoff = 0;
2643*d5d6281aSDan Carpenter	my $fixes_tag = 0;
2644*d5d6281aSDan Carpenter	my $is_revert = 0;
2645*d5d6281aSDan Carpenter	my $needs_fixes_tag = "";
2646cd261496SGeert Uytterhoeven	my $author = '';
2647cd261496SGeert Uytterhoeven	my $authorsignoff = 0;
264848ca2d8aSDwaipayan Ray	my $author_sob = '';
26490a920b5bSAndy Whitcroft	my $is_patch = 0;
2650133712a2SRob Herring	my $is_binding_patch = -1;
265129ee1b0cSJoe Perches	my $in_header_lines = $file ? 0 : 1;
265215662b3eSJoe Perches	my $in_commit_log = 0;		#Scanning lines before patch
265344d303ebSJoe Perches	my $has_patch_separator = 0;	#Found a --- line
2654ed43c4e5SAllen Hubbe	my $has_commit_log = 0;		#Encountered lines before patch
2655490b292cSJoe Perches	my $commit_log_lines = 0;	#Number of commit log lines
2656bf4daf12SJoe Perches	my $commit_log_possible_stack_dump = 0;
26572a076f40SJoe Perches	my $commit_log_long_line = 0;
2658e518e9a5SJoe Perches	my $commit_log_has_diff = 0;
265913f1937eSJoe Perches	my $reported_maintainer_file = 0;
2660fa64205dSPasi Savanainen	my $non_utf8_charset = 0;
2661fa64205dSPasi Savanainen
26624ce9f970SJoe Perches	my $last_git_commit_id_linenr = -1;
26634ce9f970SJoe Perches
2664365dd4eaSJoe Perches	my $last_blank_line = 0;
26655e4f6ba5SJoe Perches	my $last_coalesced_string_linenr = -1;
2666365dd4eaSJoe Perches
266713214adfSAndy Whitcroft	our @report = ();
26686c72ffaaSAndy Whitcroft	our $cnt_lines = 0;
26696c72ffaaSAndy Whitcroft	our $cnt_error = 0;
26706c72ffaaSAndy Whitcroft	our $cnt_warn = 0;
26716c72ffaaSAndy Whitcroft	our $cnt_chk = 0;
26726c72ffaaSAndy Whitcroft
26730a920b5bSAndy Whitcroft	# Trace the real file/line as we go.
26740a920b5bSAndy Whitcroft	my $realfile = '';
26750a920b5bSAndy Whitcroft	my $realline = 0;
26760a920b5bSAndy Whitcroft	my $realcnt = 0;
26770a920b5bSAndy Whitcroft	my $here = '';
267877cb8546SJoe Perches	my $context_function;		#undef'd unless there's a known function
26790a920b5bSAndy Whitcroft	my $in_comment = 0;
2680c2fdda0dSAndy Whitcroft	my $comment_edge = 0;
26810a920b5bSAndy Whitcroft	my $first_line = 0;
26821e855726SWolfram Sang	my $p1_prefix = '';
26830a920b5bSAndy Whitcroft
268413214adfSAndy Whitcroft	my $prev_values = 'E';
268513214adfSAndy Whitcroft
268613214adfSAndy Whitcroft	# suppression flags
2687773647a0SAndy Whitcroft	my %suppress_ifbraces;
2688170d3a22SAndy Whitcroft	my %suppress_whiletrailers;
26892b474a1aSAndy Whitcroft	my %suppress_export;
26903e469cdcSAndy Whitcroft	my $suppress_statement = 0;
2691653d4876SAndy Whitcroft
26927e51f197SJoe Perches	my %signatures = ();
2693323c1260SJoe Perches
2694c2fdda0dSAndy Whitcroft	# Pre-scan the patch sanitizing the lines.
2695de7d4f0eSAndy Whitcroft	# Pre-scan the patch looking for any __setup documentation.
2696c2fdda0dSAndy Whitcroft	#
2697de7d4f0eSAndy Whitcroft	my @setup_docs = ();
2698de7d4f0eSAndy Whitcroft	my $setup_docs = 0;
2699773647a0SAndy Whitcroft
2700d8b07710SJoe Perches	my $camelcase_file_seeded = 0;
2701d8b07710SJoe Perches
27029f3a8992SRob Herring	my $checklicenseline = 1;
27039f3a8992SRob Herring
2704773647a0SAndy Whitcroft	sanitise_line_reset();
2705c2fdda0dSAndy Whitcroft	my $line;
2706c2fdda0dSAndy Whitcroft	foreach my $rawline (@rawlines) {
2707773647a0SAndy Whitcroft		$linenr++;
2708773647a0SAndy Whitcroft		$line = $rawline;
2709c2fdda0dSAndy Whitcroft
27103705ce5bSJoe Perches		push(@fixed, $rawline) if ($fix);
27113705ce5bSJoe Perches
2712773647a0SAndy Whitcroft		if ($rawline=~/^\+\+\+\s+(\S+)/) {
2713de7d4f0eSAndy Whitcroft			$setup_docs = 0;
27142581ac7cSTim Froidcoeur			if ($1 =~ m@Documentation/admin-guide/kernel-parameters.txt$@) {
2715de7d4f0eSAndy Whitcroft				$setup_docs = 1;
2716de7d4f0eSAndy Whitcroft			}
2717773647a0SAndy Whitcroft			#next;
2718de7d4f0eSAndy Whitcroft		}
271974fd4f34SJoe Perches		if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2720773647a0SAndy Whitcroft			$realline=$1-1;
2721773647a0SAndy Whitcroft			if (defined $2) {
2722773647a0SAndy Whitcroft				$realcnt=$3+1;
2723773647a0SAndy Whitcroft			} else {
2724773647a0SAndy Whitcroft				$realcnt=1+1;
2725773647a0SAndy Whitcroft			}
2726c45dcabdSAndy Whitcroft			$in_comment = 0;
2727773647a0SAndy Whitcroft
2728773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  Run
2729773647a0SAndy Whitcroft			# the context looking for a comment "edge".  If this
2730773647a0SAndy Whitcroft			# edge is a close comment then we must be in a comment
2731773647a0SAndy Whitcroft			# at context start.
2732773647a0SAndy Whitcroft			my $edge;
273301fa9147SAndy Whitcroft			my $cnt = $realcnt;
273401fa9147SAndy Whitcroft			for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
273501fa9147SAndy Whitcroft				next if (defined $rawlines[$ln - 1] &&
273601fa9147SAndy Whitcroft					 $rawlines[$ln - 1] =~ /^-/);
273701fa9147SAndy Whitcroft				$cnt--;
273801fa9147SAndy Whitcroft				#print "RAW<$rawlines[$ln - 1]>\n";
2739721c1cb6SAndy Whitcroft				last if (!defined $rawlines[$ln - 1]);
2740fae17daeSAndy Whitcroft				if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2741fae17daeSAndy Whitcroft				    $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2742fae17daeSAndy Whitcroft					($edge) = $1;
2743fae17daeSAndy Whitcroft					last;
2744fae17daeSAndy Whitcroft				}
2745773647a0SAndy Whitcroft			}
2746773647a0SAndy Whitcroft			if (defined $edge && $edge eq '*/') {
2747773647a0SAndy Whitcroft				$in_comment = 1;
2748773647a0SAndy Whitcroft			}
2749773647a0SAndy Whitcroft
2750773647a0SAndy Whitcroft			# Guestimate if this is a continuing comment.  If this
2751773647a0SAndy Whitcroft			# is the start of a diff block and this line starts
2752773647a0SAndy Whitcroft			# ' *' then it is very likely a comment.
2753773647a0SAndy Whitcroft			if (!defined $edge &&
275483242e0cSAndy Whitcroft			    $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
2755773647a0SAndy Whitcroft			{
2756773647a0SAndy Whitcroft				$in_comment = 1;
2757773647a0SAndy Whitcroft			}
2758773647a0SAndy Whitcroft
2759773647a0SAndy Whitcroft			##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2760773647a0SAndy Whitcroft			sanitise_line_reset($in_comment);
2761773647a0SAndy Whitcroft
2762171ae1a4SAndy Whitcroft		} elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
2763773647a0SAndy Whitcroft			# Standardise the strings and chars within the input to
2764171ae1a4SAndy Whitcroft			# simplify matching -- only bother with positive lines.
2765773647a0SAndy Whitcroft			$line = sanitise_line($rawline);
2766773647a0SAndy Whitcroft		}
2767773647a0SAndy Whitcroft		push(@lines, $line);
2768773647a0SAndy Whitcroft
2769773647a0SAndy Whitcroft		if ($realcnt > 1) {
2770773647a0SAndy Whitcroft			$realcnt-- if ($line =~ /^(?:\+| |$)/);
2771773647a0SAndy Whitcroft		} else {
2772773647a0SAndy Whitcroft			$realcnt = 0;
2773773647a0SAndy Whitcroft		}
2774773647a0SAndy Whitcroft
2775773647a0SAndy Whitcroft		#print "==>$rawline\n";
2776773647a0SAndy Whitcroft		#print "-->$line\n";
2777de7d4f0eSAndy Whitcroft
2778de7d4f0eSAndy Whitcroft		if ($setup_docs && $line =~ /^\+/) {
2779de7d4f0eSAndy Whitcroft			push(@setup_docs, $line);
2780de7d4f0eSAndy Whitcroft		}
2781de7d4f0eSAndy Whitcroft	}
2782de7d4f0eSAndy Whitcroft
27836c72ffaaSAndy Whitcroft	$prefix = '';
27846c72ffaaSAndy Whitcroft
2785773647a0SAndy Whitcroft	$realcnt = 0;
2786773647a0SAndy Whitcroft	$linenr = 0;
2787194f66fcSJoe Perches	$fixlinenr = -1;
27880a920b5bSAndy Whitcroft	foreach my $line (@lines) {
27890a920b5bSAndy Whitcroft		$linenr++;
2790194f66fcSJoe Perches		$fixlinenr++;
27911b5539b1SJoe Perches		my $sline = $line;	#copy of $line
27921b5539b1SJoe Perches		$sline =~ s/$;/ /g;	#with comments as spaces
27930a920b5bSAndy Whitcroft
2794c2fdda0dSAndy Whitcroft		my $rawline = $rawlines[$linenr - 1];
2795f36d3eb8SJoe Perches		my $raw_comment = get_raw_comment($line, $rawline);
27966c72ffaaSAndy Whitcroft
279712c253abSJoe Perches# check if it's a mode change, rename or start of a patch
279812c253abSJoe Perches		if (!$in_commit_log &&
279912c253abSJoe Perches		    ($line =~ /^ mode change [0-7]+ => [0-7]+ \S+\s*$/ ||
280012c253abSJoe Perches		    ($line =~ /^rename (?:from|to) \S+\s*$/ ||
280112c253abSJoe Perches		     $line =~ /^diff --git a\/[\w\/\.\_\-]+ b\/\S+\s*$/))) {
280212c253abSJoe Perches			$is_patch = 1;
280312c253abSJoe Perches		}
280412c253abSJoe Perches
28050a920b5bSAndy Whitcroft#extract the line range in the file after the patch is applied
2806e518e9a5SJoe Perches		if (!$in_commit_log &&
280774fd4f34SJoe Perches		    $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
280874fd4f34SJoe Perches			my $context = $4;
28090a920b5bSAndy Whitcroft			$is_patch = 1;
28104a0df2efSAndy Whitcroft			$first_line = $linenr + 1;
28110a920b5bSAndy Whitcroft			$realline=$1-1;
28120a920b5bSAndy Whitcroft			if (defined $2) {
28130a920b5bSAndy Whitcroft				$realcnt=$3+1;
28140a920b5bSAndy Whitcroft			} else {
28150a920b5bSAndy Whitcroft				$realcnt=1+1;
28160a920b5bSAndy Whitcroft			}
2817c2fdda0dSAndy Whitcroft			annotate_reset();
281813214adfSAndy Whitcroft			$prev_values = 'E';
281913214adfSAndy Whitcroft
2820773647a0SAndy Whitcroft			%suppress_ifbraces = ();
2821170d3a22SAndy Whitcroft			%suppress_whiletrailers = ();
28222b474a1aSAndy Whitcroft			%suppress_export = ();
28233e469cdcSAndy Whitcroft			$suppress_statement = 0;
282474fd4f34SJoe Perches			if ($context =~ /\b(\w+)\s*\(/) {
282574fd4f34SJoe Perches				$context_function = $1;
282674fd4f34SJoe Perches			} else {
282774fd4f34SJoe Perches				undef $context_function;
282874fd4f34SJoe Perches			}
28290a920b5bSAndy Whitcroft			next;
28300a920b5bSAndy Whitcroft
28314a0df2efSAndy Whitcroft# track the line number as we move through the hunk, note that
28324a0df2efSAndy Whitcroft# new versions of GNU diff omit the leading space on completely
28334a0df2efSAndy Whitcroft# blank context lines so we need to count that too.
2834773647a0SAndy Whitcroft		} elsif ($line =~ /^( |\+|$)/) {
28350a920b5bSAndy Whitcroft			$realline++;
2836d8aaf121SAndy Whitcroft			$realcnt-- if ($realcnt != 0);
28370a920b5bSAndy Whitcroft
28384a0df2efSAndy Whitcroft			# Measure the line length and indent.
2839c2fdda0dSAndy Whitcroft			($length, $indent) = line_stats($rawline);
28400a920b5bSAndy Whitcroft
28410a920b5bSAndy Whitcroft			# Track the previous line.
28420a920b5bSAndy Whitcroft			($prevline, $stashline) = ($stashline, $line);
28430a920b5bSAndy Whitcroft			($previndent, $stashindent) = ($stashindent, $indent);
2844c2fdda0dSAndy Whitcroft			($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2845c2fdda0dSAndy Whitcroft
2846773647a0SAndy Whitcroft			#warn "line<$line>\n";
28476c72ffaaSAndy Whitcroft
2848d8aaf121SAndy Whitcroft		} elsif ($realcnt == 1) {
2849d8aaf121SAndy Whitcroft			$realcnt--;
28500a920b5bSAndy Whitcroft		}
28510a920b5bSAndy Whitcroft
2852cc77cdcaSAndy Whitcroft		my $hunk_line = ($realcnt != 0);
2853cc77cdcaSAndy Whitcroft
28546c72ffaaSAndy Whitcroft		$here = "#$linenr: " if (!$file);
28556c72ffaaSAndy Whitcroft		$here = "#$realline: " if ($file);
2856773647a0SAndy Whitcroft
28572ac73b4fSJoe Perches		my $found_file = 0;
2858773647a0SAndy Whitcroft		# extract the filename as it passes
28593bf9a009SRabin Vincent		if ($line =~ /^diff --git.*?(\S+)$/) {
28603bf9a009SRabin Vincent			$realfile = $1;
28612b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2862270c49a0SJoe Perches			$in_commit_log = 0;
28632ac73b4fSJoe Perches			$found_file = 1;
28643bf9a009SRabin Vincent		} elsif ($line =~ /^\+\+\+\s+(\S+)/) {
2865773647a0SAndy Whitcroft			$realfile = $1;
28662b7ab453SJoe Perches			$realfile =~ s@^([^/]*)/@@ if (!$file);
2867270c49a0SJoe Perches			$in_commit_log = 0;
28681e855726SWolfram Sang
28691e855726SWolfram Sang			$p1_prefix = $1;
2870e2f7aa4bSAndy Whitcroft			if (!$file && $tree && $p1_prefix ne '' &&
2871e2f7aa4bSAndy Whitcroft			    -e "$root/$p1_prefix") {
2872000d1cc1SJoe Perches				WARN("PATCH_PREFIX",
2873000d1cc1SJoe Perches				     "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
28741e855726SWolfram Sang			}
2875773647a0SAndy Whitcroft
2876c1ab3326SAndy Whitcroft			if ($realfile =~ m@^include/asm/@) {
2877000d1cc1SJoe Perches				ERROR("MODIFIED_INCLUDE_ASM",
2878000d1cc1SJoe Perches				      "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
2879773647a0SAndy Whitcroft			}
28802ac73b4fSJoe Perches			$found_file = 1;
28812ac73b4fSJoe Perches		}
28822ac73b4fSJoe Perches
288334d8815fSJoe Perches#make up the handle for any error we report on this line
288434d8815fSJoe Perches		if ($showfile) {
288534d8815fSJoe Perches			$prefix = "$realfile:$realline: "
288634d8815fSJoe Perches		} elsif ($emacs) {
28877d3a9f67SJoe Perches			if ($file) {
28887d3a9f67SJoe Perches				$prefix = "$filename:$realline: ";
28897d3a9f67SJoe Perches			} else {
289034d8815fSJoe Perches				$prefix = "$filename:$linenr: ";
289134d8815fSJoe Perches			}
28927d3a9f67SJoe Perches		}
289334d8815fSJoe Perches
28942ac73b4fSJoe Perches		if ($found_file) {
289585b0ee18SJoe Perches			if (is_maintained_obsolete($realfile)) {
289685b0ee18SJoe Perches				WARN("OBSOLETE",
289785b0ee18SJoe Perches				     "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy.  No unnecessary modifications please.\n");
289885b0ee18SJoe Perches			}
28997bd7e483SJoe Perches			if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
29002ac73b4fSJoe Perches				$check = 1;
29012ac73b4fSJoe Perches			} else {
29022ac73b4fSJoe Perches				$check = $check_orig;
29032ac73b4fSJoe Perches			}
29049f3a8992SRob Herring			$checklicenseline = 1;
2905133712a2SRob Herring
2906133712a2SRob Herring			if ($realfile !~ /^MAINTAINERS/) {
2907133712a2SRob Herring				my $last_binding_patch = $is_binding_patch;
2908133712a2SRob Herring
2909133712a2SRob Herring				$is_binding_patch = () = $realfile =~ m@^(?:Documentation/devicetree/|include/dt-bindings/)@;
2910133712a2SRob Herring
2911133712a2SRob Herring				if (($last_binding_patch != -1) &&
2912133712a2SRob Herring				    ($last_binding_patch ^ $is_binding_patch)) {
2913133712a2SRob Herring					WARN("DT_SPLIT_BINDING_PATCH",
2914858e6845SMauro Carvalho Chehab					     "DT binding docs and includes should be a separate patch. See: Documentation/devicetree/bindings/submitting-patches.rst\n");
2915133712a2SRob Herring				}
2916133712a2SRob Herring			}
2917133712a2SRob Herring
2918773647a0SAndy Whitcroft			next;
2919773647a0SAndy Whitcroft		}
2920773647a0SAndy Whitcroft
2921389834b6SRandy Dunlap		$here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
29220a920b5bSAndy Whitcroft
2923c2fdda0dSAndy Whitcroft		my $hereline = "$here\n$rawline\n";
2924c2fdda0dSAndy Whitcroft		my $herecurr = "$here\n$rawline\n";
2925c2fdda0dSAndy Whitcroft		my $hereprev = "$here\n$prevrawline\n$rawline\n";
29260a920b5bSAndy Whitcroft
29276c72ffaaSAndy Whitcroft		$cnt_lines++ if ($realcnt != 0);
29286c72ffaaSAndy Whitcroft
2929490b292cSJoe Perches# Verify the existence of a commit log if appropriate
2930490b292cSJoe Perches# 2 is used because a $signature is counted in $commit_log_lines
2931490b292cSJoe Perches		if ($in_commit_log) {
2932490b292cSJoe Perches			if ($line !~ /^\s*$/) {
2933490b292cSJoe Perches				$commit_log_lines++;	#could be a $signature
2934490b292cSJoe Perches			}
2935490b292cSJoe Perches		} elsif ($has_commit_log && $commit_log_lines < 2) {
2936490b292cSJoe Perches			WARN("COMMIT_MESSAGE",
2937490b292cSJoe Perches			     "Missing commit description - Add an appropriate one\n");
2938490b292cSJoe Perches			$commit_log_lines = 2;	#warn only once
2939490b292cSJoe Perches		}
2940490b292cSJoe Perches
2941e518e9a5SJoe Perches# Check if the commit log has what seems like a diff which can confuse patch
2942e518e9a5SJoe Perches		if ($in_commit_log && !$commit_log_has_diff &&
294313e45417SMrinal Pandey		    (($line =~ m@^\s+diff\b.*a/([\w/]+)@ &&
294413e45417SMrinal Pandey		      $line =~ m@^\s+diff\b.*a/[\w/]+\s+b/$1\b@) ||
2945e518e9a5SJoe Perches		     $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2946e518e9a5SJoe Perches		     $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2947e518e9a5SJoe Perches			ERROR("DIFF_IN_COMMIT_MSG",
2948e518e9a5SJoe Perches			      "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2949e518e9a5SJoe Perches			$commit_log_has_diff = 1;
2950e518e9a5SJoe Perches		}
2951e518e9a5SJoe Perches
29523bf9a009SRabin Vincent# Check for incorrect file permissions
29533bf9a009SRabin Vincent		if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
29543bf9a009SRabin Vincent			my $permhere = $here . "FILE: $realfile\n";
295504db4d25SJoe Perches			if ($realfile !~ m@scripts/@ &&
295604db4d25SJoe Perches			    $realfile !~ /\.(py|pl|awk|sh)$/) {
2957000d1cc1SJoe Perches				ERROR("EXECUTE_PERMISSIONS",
2958000d1cc1SJoe Perches				      "do not set execute permissions for source files\n" . $permhere);
29593bf9a009SRabin Vincent			}
29603bf9a009SRabin Vincent		}
29613bf9a009SRabin Vincent
2962cd261496SGeert Uytterhoeven# Check the patch for a From:
2963cd261496SGeert Uytterhoeven		if (decode("MIME-Header", $line) =~ /^From:\s*(.*)/) {
2964cd261496SGeert Uytterhoeven			$author = $1;
2965e7f929f3SDwaipayan Ray			my $curline = $linenr;
2966e7f929f3SDwaipayan Ray			while(defined($rawlines[$curline]) && ($rawlines[$curline++] =~ /^[ \t]\s*(.*)/)) {
2967e7f929f3SDwaipayan Ray				$author .= $1;
2968e7f929f3SDwaipayan Ray			}
2969cd261496SGeert Uytterhoeven			$author = encode("utf8", $author) if ($line =~ /=\?utf-8\?/i);
2970cd261496SGeert Uytterhoeven			$author =~ s/"//g;
2971dfa05c28SJoe Perches			$author = reformat_email($author);
2972cd261496SGeert Uytterhoeven		}
2973cd261496SGeert Uytterhoeven
297420112475SJoe Perches# Check the patch for a signoff:
2975dfa05c28SJoe Perches		if ($line =~ /^\s*signed-off-by:\s*(.*)/i) {
29764a0df2efSAndy Whitcroft			$signoff++;
297715662b3eSJoe Perches			$in_commit_log = 0;
297848ca2d8aSDwaipayan Ray			if ($author ne ''  && $authorsignoff != 1) {
2979fccaebf0SDwaipayan Ray				if (same_email_addresses($1, $author)) {
2980cd261496SGeert Uytterhoeven					$authorsignoff = 1;
298148ca2d8aSDwaipayan Ray				} else {
298248ca2d8aSDwaipayan Ray					my $ctx = $1;
298348ca2d8aSDwaipayan Ray					my ($email_name, $email_comment, $email_address, $comment1) = parse_email($ctx);
298448ca2d8aSDwaipayan Ray					my ($author_name, $author_comment, $author_address, $comment2) = parse_email($author);
298548ca2d8aSDwaipayan Ray
2986046fc741SMimi Zohar					if (lc $email_address eq lc $author_address && $email_name eq $author_name) {
298748ca2d8aSDwaipayan Ray						$author_sob = $ctx;
298848ca2d8aSDwaipayan Ray						$authorsignoff = 2;
2989046fc741SMimi Zohar					} elsif (lc $email_address eq lc $author_address) {
299048ca2d8aSDwaipayan Ray						$author_sob = $ctx;
299148ca2d8aSDwaipayan Ray						$authorsignoff = 3;
299248ca2d8aSDwaipayan Ray					} elsif ($email_name eq $author_name) {
299348ca2d8aSDwaipayan Ray						$author_sob = $ctx;
299448ca2d8aSDwaipayan Ray						$authorsignoff = 4;
299548ca2d8aSDwaipayan Ray
299648ca2d8aSDwaipayan Ray						my $address1 = $email_address;
299748ca2d8aSDwaipayan Ray						my $address2 = $author_address;
299848ca2d8aSDwaipayan Ray
299948ca2d8aSDwaipayan Ray						if ($address1 =~ /(\S+)\+\S+(\@.*)/) {
300048ca2d8aSDwaipayan Ray							$address1 = "$1$2";
300148ca2d8aSDwaipayan Ray						}
300248ca2d8aSDwaipayan Ray						if ($address2 =~ /(\S+)\+\S+(\@.*)/) {
300348ca2d8aSDwaipayan Ray							$address2 = "$1$2";
300448ca2d8aSDwaipayan Ray						}
300548ca2d8aSDwaipayan Ray						if ($address1 eq $address2) {
300648ca2d8aSDwaipayan Ray							$authorsignoff = 5;
300748ca2d8aSDwaipayan Ray						}
300848ca2d8aSDwaipayan Ray					}
3009cd261496SGeert Uytterhoeven				}
3010cd261496SGeert Uytterhoeven			}
30110a920b5bSAndy Whitcroft		}
301220112475SJoe Perches
301344d303ebSJoe Perches# Check for patch separator
301444d303ebSJoe Perches		if ($line =~ /^---$/) {
301544d303ebSJoe Perches			$has_patch_separator = 1;
301644d303ebSJoe Perches			$in_commit_log = 0;
301744d303ebSJoe Perches		}
301844d303ebSJoe Perches
3019e0d975b1SJoe Perches# Check if MAINTAINERS is being updated.  If so, there's probably no need to
3020e0d975b1SJoe Perches# emit the "does MAINTAINERS need updating?" message on file add/move/delete
3021e0d975b1SJoe Perches		if ($line =~ /^\s*MAINTAINERS\s*\|/) {
3022e0d975b1SJoe Perches			$reported_maintainer_file = 1;
3023e0d975b1SJoe Perches		}
3024e0d975b1SJoe Perches
302520112475SJoe Perches# Check signature styles
3026270c49a0SJoe Perches		if (!$in_header_lines &&
3027ce0338dfSJoe Perches		    $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
302820112475SJoe Perches			my $space_before = $1;
302920112475SJoe Perches			my $sign_off = $2;
303020112475SJoe Perches			my $space_after = $3;
303120112475SJoe Perches			my $email = $4;
303220112475SJoe Perches			my $ucfirst_sign_off = ucfirst(lc($sign_off));
303320112475SJoe Perches
3034ce0338dfSJoe Perches			if ($sign_off !~ /$signature_tags/) {
3035831242abSAditya Srivastava				my $suggested_signature = find_standard_signature($sign_off);
3036831242abSAditya Srivastava				if ($suggested_signature eq "") {
3037ce0338dfSJoe Perches					WARN("BAD_SIGN_OFF",
3038ce0338dfSJoe Perches					     "Non-standard signature: $sign_off\n" . $herecurr);
3039831242abSAditya Srivastava				} else {
3040831242abSAditya Srivastava					if (WARN("BAD_SIGN_OFF",
3041831242abSAditya Srivastava						 "Non-standard signature: '$sign_off' - perhaps '$suggested_signature'?\n" . $herecurr) &&
3042831242abSAditya Srivastava					    $fix) {
3043831242abSAditya Srivastava						$fixed[$fixlinenr] =~ s/$sign_off/$suggested_signature/;
3044831242abSAditya Srivastava					}
3045831242abSAditya Srivastava				}
3046ce0338dfSJoe Perches			}
304720112475SJoe Perches			if (defined $space_before && $space_before ne "") {
30483705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30493705ce5bSJoe Perches					 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
30503705ce5bSJoe Perches				    $fix) {
3051194f66fcSJoe Perches					$fixed[$fixlinenr] =
30523705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30533705ce5bSJoe Perches				}
305420112475SJoe Perches			}
305520112475SJoe Perches			if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
30563705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30573705ce5bSJoe Perches					 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
30583705ce5bSJoe Perches				    $fix) {
3059194f66fcSJoe Perches					$fixed[$fixlinenr] =
30603705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30613705ce5bSJoe Perches				}
30623705ce5bSJoe Perches
306320112475SJoe Perches			}
306420112475SJoe Perches			if (!defined $space_after || $space_after ne " ") {
30653705ce5bSJoe Perches				if (WARN("BAD_SIGN_OFF",
30663705ce5bSJoe Perches					 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
30673705ce5bSJoe Perches				    $fix) {
3068194f66fcSJoe Perches					$fixed[$fixlinenr] =
30693705ce5bSJoe Perches					    "$ucfirst_sign_off $email";
30703705ce5bSJoe Perches				}
307120112475SJoe Perches			}
307220112475SJoe Perches
3073dfa05c28SJoe Perches			my ($email_name, $name_comment, $email_address, $comment) = parse_email($email);
307448ca2d8aSDwaipayan Ray			my $suggested_email = format_email(($email_name, $name_comment, $email_address, $comment));
307520112475SJoe Perches			if ($suggested_email eq "") {
3076000d1cc1SJoe Perches				ERROR("BAD_SIGN_OFF",
3077000d1cc1SJoe Perches				      "Unrecognized email address: '$email'\n" . $herecurr);
307820112475SJoe Perches			} else {
307920112475SJoe Perches				my $dequoted = $suggested_email;
308020112475SJoe Perches				$dequoted =~ s/^"//;
308120112475SJoe Perches				$dequoted =~ s/" </ </;
308220112475SJoe Perches				# Don't force email to have quotes
308320112475SJoe Perches				# Allow just an angle bracketed address
3084fccaebf0SDwaipayan Ray				if (!same_email_addresses($email, $suggested_email)) {
3085fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3086fccaebf0SDwaipayan Ray						 "email address '$email' might be better as '$suggested_email'\n" . $herecurr) &&
3087fccaebf0SDwaipayan Ray					    $fix) {
3088fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$suggested_email/;
3089fccaebf0SDwaipayan Ray					}
3090fccaebf0SDwaipayan Ray				}
3091fccaebf0SDwaipayan Ray
3092fccaebf0SDwaipayan Ray				# Address part shouldn't have comments
3093fccaebf0SDwaipayan Ray				my $stripped_address = $email_address;
3094fccaebf0SDwaipayan Ray				$stripped_address =~ s/\([^\(\)]*\)//g;
3095fccaebf0SDwaipayan Ray				if ($email_address ne $stripped_address) {
3096fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3097fccaebf0SDwaipayan Ray						 "address part of email should not have comments: '$email_address'\n" . $herecurr) &&
3098fccaebf0SDwaipayan Ray					    $fix) {
3099fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email_address\E/$stripped_address/;
3100fccaebf0SDwaipayan Ray					}
3101fccaebf0SDwaipayan Ray				}
3102fccaebf0SDwaipayan Ray
3103fccaebf0SDwaipayan Ray				# Only one name comment should be allowed
3104fccaebf0SDwaipayan Ray				my $comment_count = () = $name_comment =~ /\([^\)]+\)/g;
3105fccaebf0SDwaipayan Ray				if ($comment_count > 1) {
3106000d1cc1SJoe Perches					WARN("BAD_SIGN_OFF",
3107fccaebf0SDwaipayan Ray					     "Use a single name comment in email: '$email'\n" . $herecurr);
3108fccaebf0SDwaipayan Ray				}
3109fccaebf0SDwaipayan Ray
3110fccaebf0SDwaipayan Ray
3111fccaebf0SDwaipayan Ray				# [email protected] or [email protected] shouldn't
3112e73d2715SDwaipayan Ray				# have an email name. In addition comments should strictly
3113fccaebf0SDwaipayan Ray				# begin with a #
3114fccaebf0SDwaipayan Ray				if ($email =~ /^.*stable\@(?:vger\.)?kernel\.org/i) {
3115fccaebf0SDwaipayan Ray					if (($comment ne "" && $comment !~ /^#.+/) ||
3116fccaebf0SDwaipayan Ray					    ($email_name ne "")) {
3117fccaebf0SDwaipayan Ray						my $cur_name = $email_name;
3118fccaebf0SDwaipayan Ray						my $new_comment = $comment;
3119fccaebf0SDwaipayan Ray						$cur_name =~ s/[a-zA-Z\s\-\"]+//g;
3120fccaebf0SDwaipayan Ray
3121fccaebf0SDwaipayan Ray						# Remove brackets enclosing comment text
3122fccaebf0SDwaipayan Ray						# and # from start of comments to get comment text
3123fccaebf0SDwaipayan Ray						$new_comment =~ s/^\((.*)\)$/$1/;
3124fccaebf0SDwaipayan Ray						$new_comment =~ s/^\[(.*)\]$/$1/;
3125fccaebf0SDwaipayan Ray						$new_comment =~ s/^[\s\#]+|\s+$//g;
3126fccaebf0SDwaipayan Ray
3127fccaebf0SDwaipayan Ray						$new_comment = trim("$new_comment $cur_name") if ($cur_name ne $new_comment);
3128fccaebf0SDwaipayan Ray						$new_comment = " # $new_comment" if ($new_comment ne "");
3129fccaebf0SDwaipayan Ray						my $new_email = "$email_address$new_comment";
3130fccaebf0SDwaipayan Ray
3131fccaebf0SDwaipayan Ray						if (WARN("BAD_STABLE_ADDRESS_STYLE",
3132fccaebf0SDwaipayan Ray							 "Invalid email format for stable: '$email', prefer '$new_email'\n" . $herecurr) &&
3133fccaebf0SDwaipayan Ray						    $fix) {
3134fccaebf0SDwaipayan Ray							$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3135fccaebf0SDwaipayan Ray						}
3136fccaebf0SDwaipayan Ray					}
3137fccaebf0SDwaipayan Ray				} elsif ($comment ne "" && $comment !~ /^(?:#.+|\(.+\))$/) {
3138fccaebf0SDwaipayan Ray					my $new_comment = $comment;
3139fccaebf0SDwaipayan Ray
3140fccaebf0SDwaipayan Ray					# Extract comment text from within brackets or
3141fccaebf0SDwaipayan Ray					# c89 style /*...*/ comments
3142fccaebf0SDwaipayan Ray					$new_comment =~ s/^\[(.*)\]$/$1/;
3143fccaebf0SDwaipayan Ray					$new_comment =~ s/^\/\*(.*)\*\/$/$1/;
3144fccaebf0SDwaipayan Ray
3145fccaebf0SDwaipayan Ray					$new_comment = trim($new_comment);
3146fccaebf0SDwaipayan Ray					$new_comment =~ s/^[^\w]$//; # Single lettered comment with non word character is usually a typo
3147fccaebf0SDwaipayan Ray					$new_comment = "($new_comment)" if ($new_comment ne "");
3148fccaebf0SDwaipayan Ray					my $new_email = format_email($email_name, $name_comment, $email_address, $new_comment);
3149fccaebf0SDwaipayan Ray
3150fccaebf0SDwaipayan Ray					if (WARN("BAD_SIGN_OFF",
3151fccaebf0SDwaipayan Ray						 "Unexpected content after email: '$email', should be: '$new_email'\n" . $herecurr) &&
3152fccaebf0SDwaipayan Ray					    $fix) {
3153fccaebf0SDwaipayan Ray						$fixed[$fixlinenr] =~ s/\Q$email\E/$new_email/;
3154fccaebf0SDwaipayan Ray					}
315520112475SJoe Perches				}
31560a920b5bSAndy Whitcroft			}
31577e51f197SJoe Perches
31587e51f197SJoe Perches# Check for duplicate signatures
31597e51f197SJoe Perches			my $sig_nospace = $line;
31607e51f197SJoe Perches			$sig_nospace =~ s/\s//g;
31617e51f197SJoe Perches			$sig_nospace = lc($sig_nospace);
31627e51f197SJoe Perches			if (defined $signatures{$sig_nospace}) {
31637e51f197SJoe Perches				WARN("BAD_SIGN_OFF",
31647e51f197SJoe Perches				     "Duplicate signature\n" . $herecurr);
31657e51f197SJoe Perches			} else {
31667e51f197SJoe Perches				$signatures{$sig_nospace} = 1;
31677e51f197SJoe Perches			}
31686c5d24eeSSean Christopherson
31696c5d24eeSSean Christopherson# Check Co-developed-by: immediately followed by Signed-off-by: with same name and email
31706c5d24eeSSean Christopherson			if ($sign_off =~ /^co-developed-by:$/i) {
31716c5d24eeSSean Christopherson				if ($email eq $author) {
31726c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31731916f777SThorsten Leemhuis					      "Co-developed-by: should not be used to attribute nominal patch author '$author'\n" . $herecurr);
31746c5d24eeSSean Christopherson				}
31756c5d24eeSSean Christopherson				if (!defined $lines[$linenr]) {
31766c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31771916f777SThorsten Leemhuis					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . $herecurr);
31781916f777SThorsten Leemhuis				} elsif ($rawlines[$linenr] !~ /^signed-off-by:\s*(.*)/i) {
31796c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31801916f777SThorsten Leemhuis					     "Co-developed-by: must be immediately followed by Signed-off-by:\n" . $herecurr . $rawlines[$linenr] . "\n");
31816c5d24eeSSean Christopherson				} elsif ($1 ne $email) {
31826c5d24eeSSean Christopherson					WARN("BAD_SIGN_OFF",
31831916f777SThorsten Leemhuis					     "Co-developed-by and Signed-off-by: name/email do not match\n" . $herecurr . $rawlines[$linenr] . "\n");
31846c5d24eeSSean Christopherson				}
31856c5d24eeSSean Christopherson			}
3186d7f1d71eSKai Wasserbäch
318744c31888SMatthieu Baerts# check if Reported-by: is followed by a Closes: tag
3188d7f1d71eSKai Wasserbäch			if ($sign_off =~ /^reported(?:|-and-tested)-by:$/i) {
3189d7f1d71eSKai Wasserbäch				if (!defined $lines[$linenr]) {
3190d7f1d71eSKai Wasserbäch					WARN("BAD_REPORTED_BY_LINK",
319144c31888SMatthieu Baerts					     "Reported-by: should be immediately followed by Closes: with a URL to the report\n" . $herecurr . "\n");
3192d6ccdd67SMatthieu Baerts				} elsif ($rawlines[$linenr] !~ /^closes:\s*/i) {
3193d7f1d71eSKai Wasserbäch					WARN("BAD_REPORTED_BY_LINK",
319444c31888SMatthieu Baerts					     "Reported-by: should be immediately followed by Closes: with a URL to the report\n" . $herecurr . $rawlines[$linenr] . "\n");
31950a920b5bSAndy Whitcroft				}
3196d7f1d71eSKai Wasserbäch			}
3197d7f1d71eSKai Wasserbäch		}
3198d7f1d71eSKai Wasserbäch
3199*d5d6281aSDan Carpenter# These indicate a bug fix
3200*d5d6281aSDan Carpenter		if (!$in_header_lines && !$is_patch &&
3201*d5d6281aSDan Carpenter			$line =~ /^This reverts commit/) {
3202*d5d6281aSDan Carpenter			$is_revert = 1;
3203*d5d6281aSDan Carpenter		}
3204*d5d6281aSDan Carpenter
3205*d5d6281aSDan Carpenter		if (!$in_header_lines && !$is_patch &&
3206*d5d6281aSDan Carpenter		    $line =~ /((?:(?:BUG: K.|UB)SAN: |Call Trace:|stable\@|syzkaller))/) {
3207*d5d6281aSDan Carpenter			$needs_fixes_tag = $1;
3208*d5d6281aSDan Carpenter		}
32090a920b5bSAndy Whitcroft
3210bd17e036SNiklas Söderlund# Check Fixes: styles is correct
3211bd17e036SNiklas Söderlund		if (!$in_header_lines &&
3212bd17e036SNiklas Söderlund		    $line =~ /^\s*fixes:?\s*(?:commit\s*)?[0-9a-f]{5,}\b/i) {
3213bd17e036SNiklas Söderlund			my $orig_commit = "";
3214bd17e036SNiklas Söderlund			my $id = "0123456789ab";
3215bd17e036SNiklas Söderlund			my $title = "commit title";
3216bd17e036SNiklas Söderlund			my $tag_case = 1;
3217bd17e036SNiklas Söderlund			my $tag_space = 1;
3218bd17e036SNiklas Söderlund			my $id_length = 1;
3219bd17e036SNiklas Söderlund			my $id_case = 1;
3220bd17e036SNiklas Söderlund			my $title_has_quotes = 0;
3221*d5d6281aSDan Carpenter			$fixes_tag = 1;
3222bd17e036SNiklas Söderlund
3223bd17e036SNiklas Söderlund			if ($line =~ /(\s*fixes:?)\s+([0-9a-f]{5,})\s+($balanced_parens)/i) {
3224bd17e036SNiklas Söderlund				my $tag = $1;
3225bd17e036SNiklas Söderlund				$orig_commit = $2;
3226bd17e036SNiklas Söderlund				$title = $3;
3227bd17e036SNiklas Söderlund
3228bd17e036SNiklas Söderlund				$tag_case = 0 if $tag eq "Fixes:";
3229bd17e036SNiklas Söderlund				$tag_space = 0 if ($line =~ /^fixes:? [0-9a-f]{5,} ($balanced_parens)/i);
3230bd17e036SNiklas Söderlund
3231bd17e036SNiklas Söderlund				$id_length = 0 if ($orig_commit =~ /^[0-9a-f]{12}$/i);
3232bd17e036SNiklas Söderlund				$id_case = 0 if ($orig_commit !~ /[A-F]/);
3233bd17e036SNiklas Söderlund
3234bd17e036SNiklas Söderlund				# Always strip leading/trailing parens then double quotes if existing
3235bd17e036SNiklas Söderlund				$title = substr($title, 1, -1);
3236bd17e036SNiklas Söderlund				if ($title =~ /^".*"$/) {
3237bd17e036SNiklas Söderlund					$title = substr($title, 1, -1);
3238bd17e036SNiklas Söderlund					$title_has_quotes = 1;
3239bd17e036SNiklas Söderlund				}
3240bd17e036SNiklas Söderlund			}
3241bd17e036SNiklas Söderlund
3242bd17e036SNiklas Söderlund			my ($cid, $ctitle) = git_commit_info($orig_commit, $id,
3243bd17e036SNiklas Söderlund							     $title);
3244bd17e036SNiklas Söderlund
3245bd17e036SNiklas Söderlund			if ($ctitle ne $title || $tag_case || $tag_space ||
3246bd17e036SNiklas Söderlund			    $id_length || $id_case || !$title_has_quotes) {
3247bd17e036SNiklas Söderlund				if (WARN("BAD_FIXES_TAG",
3248bd17e036SNiklas Söderlund				     "Please use correct Fixes: style 'Fixes: <12 chars of sha1> (\"<title line>\")' - ie: 'Fixes: $cid (\"$ctitle\")'\n" . $herecurr) &&
3249bd17e036SNiklas Söderlund				    $fix) {
3250bd17e036SNiklas Söderlund					$fixed[$fixlinenr] = "Fixes: $cid (\"$ctitle\")";
3251bd17e036SNiklas Söderlund				}
3252bd17e036SNiklas Söderlund			}
3253bd17e036SNiklas Söderlund		}
3254bd17e036SNiklas Söderlund
3255a2fe16b9SJoe Perches# Check email subject for common tools that don't need to be mentioned
3256a2fe16b9SJoe Perches		if ($in_header_lines &&
3257a2fe16b9SJoe Perches		    $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
3258a2fe16b9SJoe Perches			WARN("EMAIL_SUBJECT",
3259a2fe16b9SJoe Perches			     "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
3260a2fe16b9SJoe Perches		}
3261a2fe16b9SJoe Perches
326244d303ebSJoe Perches# Check for Gerrit Change-Ids not in any patch context
326344d303ebSJoe Perches		if ($realfile eq '' && !$has_patch_separator && $line =~ /^\s*change-id:/i) {
32647580c5b9SAditya Srivastava			if (ERROR("GERRIT_CHANGE_ID",
32657580c5b9SAditya Srivastava			          "Remove Gerrit Change-Id's before submitting upstream\n" . $herecurr) &&
32667580c5b9SAditya Srivastava			    $fix) {
32677580c5b9SAditya Srivastava				fix_delete_line($fixlinenr, $rawline);
32687580c5b9SAditya Srivastava			}
32697ebd05efSChristopher Covington		}
32707ebd05efSChristopher Covington
3271369c8dd3SJoe Perches# Check if the commit log is in a possible stack dump
3272369c8dd3SJoe Perches		if ($in_commit_log && !$commit_log_possible_stack_dump &&
3273369c8dd3SJoe Perches		    ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
3274369c8dd3SJoe Perches		     $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
3275369c8dd3SJoe Perches					# timestamp
3276634cffccSJoe Perches		     $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/) ||
3277634cffccSJoe Perches		     $line =~ /^(?:\s+\w+:\s+[0-9a-fA-F]+){3,3}/ ||
3278634cffccSJoe Perches		     $line =~ /^\s*\#\d+\s*\[[0-9a-fA-F]+\]\s*\w+ at [0-9a-fA-F]+/) {
3279634cffccSJoe Perches					# stack dump address styles
3280369c8dd3SJoe Perches			$commit_log_possible_stack_dump = 1;
3281369c8dd3SJoe Perches		}
3282369c8dd3SJoe Perches
32832a076f40SJoe Perches# Check for line lengths > 75 in commit log, warn once
32842a076f40SJoe Perches		if ($in_commit_log && !$commit_log_long_line &&
3285bf4daf12SJoe Perches		    length($line) > 75 &&
3286bf4daf12SJoe Perches		    !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
3287bf4daf12SJoe Perches					# file delta changes
328836f8b348SJerome Forissier		      $line =~ /^\s*(?:[\w\.\-\+]*\/)++[\w\.\-\+]+:/ ||
3289bf4daf12SJoe Perches					# filename then :
3290f94e40eaSMatthieu Baerts		      $line =~ /^\s*(?:Fixes:|$link_tags_search|$signature_tags)/i ||
3291f94e40eaSMatthieu Baerts					# A Fixes:, link or signature tag line
3292bf4daf12SJoe Perches		      $commit_log_possible_stack_dump)) {
32932a076f40SJoe Perches			WARN("COMMIT_LOG_LONG_LINE",
32948e7b7ffbSJim Cromie			     "Prefer a maximum 75 chars per line (possible unwrapped commit description?)\n" . $herecurr);
32952a076f40SJoe Perches			$commit_log_long_line = 1;
32962a076f40SJoe Perches		}
32972a076f40SJoe Perches
3298bf4daf12SJoe Perches# Reset possible stack dump if a blank line is found
3299bf4daf12SJoe Perches		if ($in_commit_log && $commit_log_possible_stack_dump &&
3300bf4daf12SJoe Perches		    $line =~ /^\s*$/) {
3301bf4daf12SJoe Perches			$commit_log_possible_stack_dump = 0;
3302bf4daf12SJoe Perches		}
3303bf4daf12SJoe Perches
330476f381bbSKai Wasserbäch# Check for odd tags before a URI/URL
330576f381bbSKai Wasserbäch		if ($in_commit_log &&
3306f94e40eaSMatthieu Baerts		    $line =~ /^\s*(\w+:)\s*http/ && $1 !~ /^$link_tags_search$/) {
330776f381bbSKai Wasserbäch			if ($1 =~ /^v(?:ersion)?\d+/i) {
330876f381bbSKai Wasserbäch				WARN("COMMIT_LOG_VERSIONING",
330976f381bbSKai Wasserbäch				     "Patch version information should be after the --- line\n" . $herecurr);
331076f381bbSKai Wasserbäch			} else {
331176f381bbSKai Wasserbäch				WARN("COMMIT_LOG_USE_LINK",
3312f94e40eaSMatthieu Baerts				     "Unknown link reference '$1', use $link_tags_print instead\n" . $herecurr);
331376f381bbSKai Wasserbäch			}
331476f381bbSKai Wasserbäch		}
331576f381bbSKai Wasserbäch
3316d6ccdd67SMatthieu Baerts# Check for misuse of the link tags
3317d6ccdd67SMatthieu Baerts		if ($in_commit_log &&
3318d6ccdd67SMatthieu Baerts		    $line =~ /^\s*(\w+:)\s*(\S+)/) {
3319d6ccdd67SMatthieu Baerts			my $tag = $1;
3320d6ccdd67SMatthieu Baerts			my $value = $2;
3321d6ccdd67SMatthieu Baerts			if ($tag =~ /^$link_tags_search$/ && $value !~ m{^https?://}) {
3322d6ccdd67SMatthieu Baerts				WARN("COMMIT_LOG_WRONG_LINK",
3323d6ccdd67SMatthieu Baerts				     "'$tag' should be followed by a public http(s) link\n" . $herecurr);
33240d7835fcSJoe Perches			}
33254ce9f970SJoe Perches		}
33264ce9f970SJoe Perches
33274ce9f970SJoe Perches# Check for lines starting with a #
33284ce9f970SJoe Perches		if ($in_commit_log && $line =~ /^#/) {
33294ce9f970SJoe Perches			if (WARN("COMMIT_COMMENT_SYMBOL",
33304ce9f970SJoe Perches				 "Commit log lines starting with '#' are dropped by git as comments\n" . $herecurr) &&
33314ce9f970SJoe Perches			    $fix) {
33324ce9f970SJoe Perches				$fixed[$fixlinenr] =~ s/^/ /;
33334ce9f970SJoe Perches			}
33344ce9f970SJoe Perches		}
3335a8972573SJohn Hubbard
3336e882dbfcSWei Wang# Check for git id commit length and improperly formed commit descriptions
33374ce9f970SJoe Perches# A correctly formed commit description is:
33384ce9f970SJoe Perches#    commit <SHA-1 hash length 12+ chars> ("Complete commit subject")
3339aab38f51SJoe Perches# with the commit subject '("' prefix and '")' suffix
3340369c8dd3SJoe Perches# This is a fairly compilicated block as it tests for what appears to be
3341bf4daf12SJoe Perches# bare SHA-1 hash with  minimum length of 5.  It also avoids several types of
3342fe043ea1SJoe Perches# possible SHA-1 matches.
3343fe043ea1SJoe Perches# A commit match can span multiple lines so this block attempts to find a
33440d7835fcSJoe Perches# complete typical commit on a maximum of 3 lines
33450d7835fcSJoe Perches		if ($perl_version_ok &&
33460d7835fcSJoe Perches		    $in_commit_log && !$commit_log_possible_stack_dump &&
33470d7835fcSJoe Perches		    $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink|base-commit):/i &&
33480d7835fcSJoe Perches		    $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
33490d7835fcSJoe Perches		    (($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
33500d7835fcSJoe Perches		      ($line =~ /\bcommit\s*$/i && defined($rawlines[$linenr]) && $rawlines[$linenr] =~ /^\s*[0-9a-f]{5,}\b/i)) ||
33514ce9f970SJoe Perches		     ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
33524ce9f970SJoe Perches		      $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
33534ce9f970SJoe Perches		      $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
33540d7835fcSJoe Perches			my $init_char = "c";
33554ce9f970SJoe Perches			my $orig_commit = "";
33564ce9f970SJoe Perches			my $short = 1;
33574ce9f970SJoe Perches			my $long = 0;
33584ce9f970SJoe Perches			my $case = 1;
33594ce9f970SJoe Perches			my $space = 1;
33604ce9f970SJoe Perches			my $id = '0123456789ab';
33614ce9f970SJoe Perches			my $orig_desc = "commit description";
33624ce9f970SJoe Perches			my $description = "";
33634ce9f970SJoe Perches			my $herectx = $herecurr;
33644ce9f970SJoe Perches			my $has_parens = 0;
33654ce9f970SJoe Perches			my $has_quotes = 0;
33664ce9f970SJoe Perches
33674ce9f970SJoe Perches			my $input = $line;
33684ce9f970SJoe Perches			if ($line =~ /(?:\bcommit\s+[0-9a-f]{5,}|\bcommit\s*$)/i) {
33694ce9f970SJoe Perches				for (my $n = 0; $n < 2; $n++) {
33704ce9f970SJoe Perches					if ($input =~ /\bcommit\s+[0-9a-f]{5,}\s*($balanced_parens)/i) {
33714ce9f970SJoe Perches						$orig_desc = $1;
33724ce9f970SJoe Perches						$has_parens = 1;
33734ce9f970SJoe Perches						# Always strip leading/trailing parens then double quotes if existing
3374fe043ea1SJoe Perches						$orig_desc = substr($orig_desc, 1, -1);
3375fe043ea1SJoe Perches						if ($orig_desc =~ /^".*"$/) {
33764ce9f970SJoe Perches							$orig_desc = substr($orig_desc, 1, -1);
33774ce9f970SJoe Perches							$has_quotes = 1;
33784ce9f970SJoe Perches						}
33794ce9f970SJoe Perches						last;
33804ce9f970SJoe Perches					}
33814ce9f970SJoe Perches					last if ($#lines < $linenr + $n);
33824ce9f970SJoe Perches					$input .= " " . trim($rawlines[$linenr + $n]);
33834ce9f970SJoe Perches					$herectx .= "$rawlines[$linenr + $n]\n";
33844ce9f970SJoe Perches				}
33850d7835fcSJoe Perches				$herectx = $herecurr if (!$has_parens);
33860d7835fcSJoe Perches			}
33870d7835fcSJoe Perches
33880d7835fcSJoe Perches			if ($input =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
33890d7835fcSJoe Perches				$init_char = $1;
3390948b133aSHeinrich Schuchardt				$orig_commit = lc($2);
33914ce9f970SJoe Perches				$short = 0 if ($input =~ /\bcommit\s+[0-9a-f]{12,40}/i);
33924ce9f970SJoe Perches				$long = 1 if ($input =~ /\bcommit\s+[0-9a-f]{41,}/i);
3393d311cd44SJoe Perches				$space = 0 if ($input =~ /\bcommit [0-9a-f]/i);
33944ce9f970SJoe Perches				$case = 0 if ($input =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
33950d7835fcSJoe Perches			} elsif ($input =~ /\b([0-9a-f]{12,40})\b/i) {
33964ce9f970SJoe Perches				$orig_commit = lc($1);
33974ce9f970SJoe Perches			}
3398d311cd44SJoe Perches
3399d311cd44SJoe Perches			($id, $description) = git_commit_info($orig_commit,
34009b71f79fSBjorn Helgaas							      $id, $orig_desc);
34019b71f79fSBjorn Helgaas
34029b71f79fSBjorn Helgaas			if (defined($id) &&
34039b71f79fSBjorn Helgaas			    ($short || $long || $space || $case || ($orig_desc ne $description) || !$has_quotes) &&
34049b71f79fSBjorn Helgaas			    $last_git_commit_id_linenr != $linenr - 1) {
34059b71f79fSBjorn Helgaas				ERROR("GIT_COMMIT_ID",
340613f1937eSJoe Perches				      "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herectx);
340713f1937eSJoe Perches			}
340813f1937eSJoe Perches			#don't report the next line if this line ends in commit and the sha1 hash is the next line
340913f1937eSJoe Perches			$last_git_commit_id_linenr = $linenr if ($line =~ /\bcommit\s*$/i);
341013f1937eSJoe Perches		}
341113f1937eSJoe Perches
3412a82603a8SAndrew Jeffery# Check for mailing list archives other than lore.kernel.org
341313f1937eSJoe Perches		if ($rawline =~ m{http.*\b$obsolete_archives}) {
341413f1937eSJoe Perches			WARN("PREFER_LORE_ARCHIVE",
341513f1937eSJoe Perches			     "Use lore.kernel.org archive links when possible - see https://lore.kernel.org/lists.html\n" . $herecurr);
341613f1937eSJoe Perches		}
341713f1937eSJoe Perches
3418e400edb1SRob Herring# Check for added, moved or deleted files
3419e400edb1SRob Herring		if (!$reported_maintainer_file && !$in_commit_log &&
3420e400edb1SRob Herring		    ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
3421e400edb1SRob Herring		     $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
3422e400edb1SRob Herring		     ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
342356ddc4cdSMauro Carvalho Chehab		      (defined($1) || defined($2))))) {
3424e400edb1SRob Herring			$is_patch = 1;
3425e400edb1SRob Herring			$reported_maintainer_file = 1;
342600df344fSAndy Whitcroft			WARN("FILE_PATH_CHANGES",
34278905a67cSAndy Whitcroft			     "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
3428000d1cc1SJoe Perches		}
3429000d1cc1SJoe Perches
34306c72ffaaSAndy Whitcroft# Check for adding new DT bindings not in schema format
3431de7d4f0eSAndy Whitcroft		if (!$in_commit_log &&
3432de7d4f0eSAndy Whitcroft		    ($line =~ /^new file mode\s*\d+\s*$/) &&
3433de7d4f0eSAndy Whitcroft		    ($realfile =~ m@^Documentation/devicetree/bindings/.*\.txt$@)) {
3434de7d4f0eSAndy Whitcroft			WARN("DT_SCHEMA_BINDING_PATCH",
3435171ae1a4SAndy Whitcroft			     "DT bindings should be in DT schema format. See: Documentation/devicetree/bindings/writing-schema.rst\n");
3436171ae1a4SAndy Whitcroft		}
3437171ae1a4SAndy Whitcroft
3438171ae1a4SAndy Whitcroft# Check for wrappage within a valid hunk of the file
3439171ae1a4SAndy Whitcroft		if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
3440171ae1a4SAndy Whitcroft			ERROR("CORRUPTED_PATCH",
3441171ae1a4SAndy Whitcroft			      "patch seems to be corrupt (line wrapped?)\n" .
344234d99219SJoe Perches				$herecurr) if (!$emitted_corrupt++);
3443000d1cc1SJoe Perches		}
344400df344fSAndy Whitcroft
34450a920b5bSAndy Whitcroft# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
344615662b3eSJoe Perches		if (($realfile =~ /^$/ || $line =~ /^\+/) &&
344715662b3eSJoe Perches		    $rawline !~ m/^$UTF8*$/) {
344815662b3eSJoe Perches			my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
3449eb3a58deSJoe Perches
3450eb3a58deSJoe Perches			my $blank = copy_spacing($rawline);
345115662b3eSJoe Perches			my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
345215662b3eSJoe Perches			my $hereptr = "$hereline$ptr\n";
3453ed43c4e5SAllen Hubbe
345415662b3eSJoe Perches			CHK("INVALID_UTF8",
345515662b3eSJoe Perches			    "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
3456fa64205dSPasi Savanainen		}
3457fa64205dSPasi Savanainen
3458fa64205dSPasi Savanainen# Check if it's the start of a commit log
3459fa64205dSPasi Savanainen# (not a header line and we haven't seen the patch filename)
3460fa64205dSPasi Savanainen		if ($in_header_lines && $realfile =~ /^$/ &&
3461fa64205dSPasi Savanainen		    !($rawline =~ /^\s+(?:\S|$)/ ||
3462fa64205dSPasi Savanainen		      $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
3463fa64205dSPasi Savanainen			$in_header_lines = 0;
3464fa64205dSPasi Savanainen			$in_commit_log = 1;
346515662b3eSJoe Perches			$has_commit_log = 1;
3466fa64205dSPasi Savanainen		}
346715662b3eSJoe Perches
346815662b3eSJoe Perches# Check if there is UTF-8 in a commit log when a mail header has explicitly
346915662b3eSJoe Perches# declined it, i.e defined some charset where it is missing.
3470d6430f71SJoe Perches		if ($in_header_lines &&
3471d6430f71SJoe Perches		    $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
3472d6430f71SJoe Perches		    $1 !~ /utf-8/i) {
3473d6430f71SJoe Perches			$non_utf8_charset = 1;
3474d6430f71SJoe Perches		}
3475d6430f71SJoe Perches
3476d6430f71SJoe Perches		if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
3477d6430f71SJoe Perches		    $rawline =~ /$NON_ASCII_UTF8/) {
3478d6430f71SJoe Perches			WARN("UTF8_BEFORE_PATCH",
3479d6430f71SJoe Perches			    "8-bit UTF-8 used in possible commit log\n" . $herecurr);
3480d6430f71SJoe Perches		}
3481d6430f71SJoe Perches
3482d6430f71SJoe Perches# Check for absolute kernel paths in commit message
3483d6430f71SJoe Perches		if ($tree && $in_commit_log) {
348466b47b4aSKees Cook			while ($line =~ m{(?:^|\s)(/\S*)}g) {
348566d7a382SJoe Perches				my $file = $1;
348666d7a382SJoe Perches
34877da07c31SDwaipayan Ray				if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
348866b47b4aSKees Cook				    check_absolute_file($1, $herecurr)) {
34897da07c31SDwaipayan Ray					#
34907da07c31SDwaipayan Ray				} else {
34917da07c31SDwaipayan Ray					check_absolute_file($file, $herecurr);
349266b47b4aSKees Cook				}
349366b47b4aSKees Cook			}
349466b47b4aSKees Cook		}
34950675a8fbSJean Delvare
34960675a8fbSJean Delvare# Check for various typo / spelling mistakes
34970675a8fbSJean Delvare		if (defined($misspellings) &&
34987da07c31SDwaipayan Ray		    ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
349966b47b4aSKees Cook			while ($rawline =~ /(?:^|[^\w\-'`])($misspellings)(?:[^\w\-'`]|$)/gi) {
350066b47b4aSKees Cook				my $typo = $1;
350166b47b4aSKees Cook				my $blank = copy_spacing($rawline);
350266b47b4aSKees Cook				my $ptr = substr($blank, 0, $-[1]) . "^" x length($typo);
350366b47b4aSKees Cook				my $hereptr = "$hereline$ptr\n";
350466b47b4aSKees Cook				my $typo_fix = $spelling_fix{lc($typo)};
3505a8dd86bfSMatteo Croce				$typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
3506a8dd86bfSMatteo Croce				$typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
3507a8dd86bfSMatteo Croce				my $msg_level = \&WARN;
3508a8dd86bfSMatteo Croce				$msg_level = \&CHK if ($file);
3509a8dd86bfSMatteo Croce				if (&{$msg_level}("TYPO_SPELLING",
3510a8dd86bfSMatteo Croce						  "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $hereptr) &&
3511a8dd86bfSMatteo Croce				    $fix) {
3512a8dd86bfSMatteo Croce					$fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
3513a8dd86bfSMatteo Croce				}
3514a8dd86bfSMatteo Croce			}
3515a8dd86bfSMatteo Croce		}
3516310cd06bSJoe Perches
35178d0325ccSAditya Srivastava# check for invalid commit id
35188d0325ccSAditya Srivastava		if ($in_commit_log && $line =~ /(^fixes:|\bcommit)\s+([0-9a-f]{6,40})\b/i) {
35198d0325ccSAditya Srivastava			my $id;
35201db81a68SDwaipayan Ray			my $description;
3521310cd06bSJoe Perches			($id, $description) = git_commit_info($2, undef, undef);
3522310cd06bSJoe Perches			if (!defined($id)) {
3523310cd06bSJoe Perches				WARN("UNKNOWN_COMMIT_ID",
3524310cd06bSJoe Perches				     "Unknown commit id '$2', maybe rebased or not pulled?\n" . $herecurr);
35251db81a68SDwaipayan Ray			}
35261db81a68SDwaipayan Ray		}
3527310cd06bSJoe Perches
3528310cd06bSJoe Perches# check for repeated words separated by a single space
3529310cd06bSJoe Perches# avoid false positive from list command eg, '-rw-r--r-- 1 root root'
3530310cd06bSJoe Perches		if (($rawline =~ /^\+/ || $in_commit_log) &&
3531310cd06bSJoe Perches		    $rawline !~ /[bcCdDlMnpPs\?-][rwxsStT-]{9}/) {
35321db81a68SDwaipayan Ray			pos($rawline) = 1 if (!$in_commit_log);
3533310cd06bSJoe Perches			while ($rawline =~ /\b($word_pattern) (?=($word_pattern))/g) {
3534310cd06bSJoe Perches
35351db81a68SDwaipayan Ray				my $first = $1;
35361db81a68SDwaipayan Ray				my $second = $2;
35371db81a68SDwaipayan Ray				my $start_pos = $-[1];
35381db81a68SDwaipayan Ray				my $end_pos = $+[2];
35391db81a68SDwaipayan Ray				if ($first =~ /(?:struct|union|enum)/) {
35401db81a68SDwaipayan Ray					pos($rawline) += length($first) + length($second) + 1;
35411db81a68SDwaipayan Ray					next;
35421db81a68SDwaipayan Ray				}
35431db81a68SDwaipayan Ray
35448d0325ccSAditya Srivastava				next if (lc($first) ne lc($second));
35458d0325ccSAditya Srivastava				next if ($first eq 'long');
35468d0325ccSAditya Srivastava
35478d0325ccSAditya Srivastava				# check for character before and after the word matches
35488d0325ccSAditya Srivastava				my $start_char = '';
3549310cd06bSJoe Perches				my $end_char = '';
3550310cd06bSJoe Perches				$start_char = substr($rawline, $start_pos - 1, 1) if ($start_pos > ($in_commit_log ? 0 : 1));
3551310cd06bSJoe Perches				$end_char = substr($rawline, $end_pos, 1) if ($end_pos < length($rawline));
3552310cd06bSJoe Perches
3553310cd06bSJoe Perches				next if ($start_char =~ /^\S$/);
3554310cd06bSJoe Perches				next if (index(" \t.,;?!", $end_char) == -1);
3555310cd06bSJoe Perches
3556310cd06bSJoe Perches				# avoid repeating hex occurrences like 'ff ff fe 09 ...'
3557310cd06bSJoe Perches				if ($first =~ /\b[0-9a-f]{2,}\b/i) {
3558310cd06bSJoe Perches					next if (!exists($allow_repeated_words{lc($first)}));
3559310cd06bSJoe Perches				}
3560310cd06bSJoe Perches
3561310cd06bSJoe Perches				if (WARN("REPEATED_WORD",
3562310cd06bSJoe Perches					 "Possible repeated word: '$first'\n" . $herecurr) &&
3563310cd06bSJoe Perches				    $fix) {
3564310cd06bSJoe Perches					$fixed[$fixlinenr] =~ s/\b$first $second\b/$first/;
3565310cd06bSJoe Perches				}
3566310cd06bSJoe Perches			}
3567310cd06bSJoe Perches
3568310cd06bSJoe Perches			# if it's a repeated word on consecutive lines in a comment block
3569310cd06bSJoe Perches			if ($prevline =~ /$;+\s*$/ &&
357030670854SAndy Whitcroft			    $prevrawline =~ /($word_pattern)\s*$/) {
357130670854SAndy Whitcroft				my $last_word = $1;
357200df344fSAndy Whitcroft				if ($rawline =~ /^\+\s*\*\s*$last_word /) {
35730a920b5bSAndy Whitcroft					if (WARN("REPEATED_WORD",
35749c0ca6f9SAndy Whitcroft						 "Possible repeated word: '$last_word'\n" . $hereprev) &&
3575c2fdda0dSAndy Whitcroft					    $fix) {
3576d5e616fcSJoe Perches						$fixed[$fixlinenr] =~ s/(\+\s*\*\s*)$last_word /$1/;
3577d5e616fcSJoe Perches					}
3578d5e616fcSJoe Perches				}
3579194f66fcSJoe Perches			}
3580d5e616fcSJoe Perches		}
3581c2fdda0dSAndy Whitcroft
3582c2fdda0dSAndy Whitcroft# ignore non-hunk lines and lines being removed
35833705ce5bSJoe Perches		next if (!$hunk_line || $line =~ /^-/);
35843705ce5bSJoe Perches
35853705ce5bSJoe Perches#trailing whitespace
3586194f66fcSJoe Perches		if ($line =~ /^\+.*\015/) {
35873705ce5bSJoe Perches			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
35883705ce5bSJoe Perches			if (ERROR("DOS_LINE_ENDINGS",
3589d2c0a235SAndy Whitcroft				  "DOS line endings\n" . $herevet) &&
35900a920b5bSAndy Whitcroft			    $fix) {
35915368df20SAndy Whitcroft				$fixed[$fixlinenr] =~ s/[\s\015]+$//;
35924783f894SJosh Triplett			}
3593109d8cb2SAlexander Duyck		} elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
35941bde561eSMatthew Wilcox			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
35953e2232f2SJoe Perches			if (ERROR("TRAILING_WHITESPACE",
35963e2232f2SJoe Perches				  "trailing whitespace\n" . $herevet) &&
35974783f894SJosh Triplett			    $fix) {
35980675a8fbSJean Delvare				$fixed[$fixlinenr] =~ s/\s+$//;
35990675a8fbSJean Delvare			}
36000675a8fbSJean Delvare
36014783f894SJosh Triplett			$rpt_cleaners = 1;
36024783f894SJosh Triplett		}
36034783f894SJosh Triplett
36043354957aSAndi Kleen# Check for FSF mailing addresses.
36059fe287d7SAndy Whitcroft		if ($rawline =~ /\bwrite to the Free/i ||
36069fe287d7SAndy Whitcroft		    $rawline =~ /\b675\s+Mass\s+Ave/i ||
36073354957aSAndi Kleen		    $rawline =~ /\b59\s+Temple\s+Pl/i ||
3608678ae162SUlf Magnusson		    $rawline =~ /\b51\s+Franklin\s+St/i) {
3609678ae162SUlf Magnusson			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3610678ae162SUlf Magnusson			my $msg_level = \&ERROR;
3611678ae162SUlf Magnusson			$msg_level = \&CHK if ($file);
3612b8709bceSJoe Perches			&{$msg_level}("FSF_MAILING_ADDRESS",
3613b8709bceSJoe Perches				      "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)
3614b8709bceSJoe Perches		}
3615b8709bceSJoe Perches
3616b8709bceSJoe Perches# check for Kconfig help text having a real description
3617b8709bceSJoe Perches# Only applies when adding the entry originally, after that we do not have
36189fe287d7SAndy Whitcroft# sufficient context to determine whether it is indeed long enough.
36199fe287d7SAndy Whitcroft		if ($realfile =~ /Kconfig/ &&
3620b8709bceSJoe Perches		    # 'choice' is usually the last thing on the line (though
3621a1385803SAndy Whitcroft		    # Kconfig supports named choices), so use a word boundary
3622b8709bceSJoe Perches		    # (\b) rather than a whitespace character (\s)
3623b8709bceSJoe Perches		    $line =~ /^\+\s*(?:config|menuconfig|choice)\b/) {
3624b8709bceSJoe Perches			my $ln = $linenr;
3625b8709bceSJoe Perches			my $needs_help = 0;
3626b8709bceSJoe Perches			my $has_help = 0;
3627b8709bceSJoe Perches			my $help_length = 0;
3628b8709bceSJoe Perches			while (defined $lines[$ln]) {
3629a1385803SAndy Whitcroft				my $f = $lines[$ln++];
3630a1385803SAndy Whitcroft
3631b8709bceSJoe Perches				next if ($f =~ /^-/);
3632b8709bceSJoe Perches				last if ($f !~ /^[\+ ]/);	# !patch context
3633b8709bceSJoe Perches
3634b8709bceSJoe Perches				if ($f =~ /^\+\s*(?:bool|tristate|prompt)\s*["']/) {
3635678ae162SUlf Magnusson					$needs_help = 1;
3636b8709bceSJoe Perches					next;
3637678ae162SUlf Magnusson				}
3638678ae162SUlf Magnusson				if ($f =~ /^\+\s*help\s*$/) {
3639678ae162SUlf Magnusson					$has_help = 1;
3640678ae162SUlf Magnusson					next;
3641b8709bceSJoe Perches				}
3642678ae162SUlf Magnusson
36439fe287d7SAndy Whitcroft				$f =~ s/^.//;	# strip patch context [+ ]
36449fe287d7SAndy Whitcroft				$f =~ s/#.*//;	# strip # directives
3645b8709bceSJoe Perches				$f =~ s/^\s+//;	# strip leading blanks
36463354957aSAndi Kleen				next if ($f =~ /^$/);	# skip blank lines
3647b8709bceSJoe Perches
3648b8709bceSJoe Perches				# At the end of this Kconfig block:
3649b8709bceSJoe Perches				# This only checks context lines in the patch
3650000d1cc1SJoe Perches				# and so hopefully shouldn't trigger false
3651b8709bceSJoe Perches				# positives, even though some of these are
365256193274SVadim Bendebury				# common words in help texts
36533354957aSAndi Kleen				if ($f =~ /^(?:config|menuconfig|choice|endchoice|
36543354957aSAndi Kleen					       if|endif|menu|endmenu|source)\b/x) {
36557ccf41a8SJoe Perches					last;
36567ccf41a8SJoe Perches				}
36577ccf41a8SJoe Perches				$help_length++ if ($has_help);
36587ccf41a8SJoe Perches			}
3659628f91a2SJoe Perches			if ($needs_help &&
3660628f91a2SJoe Perches			    $help_length < $min_conf_desc_length) {
3661628f91a2SJoe Perches				my $stat_real = get_stat_real($linenr, $ln - 1);
3662628f91a2SJoe Perches				WARN("CONFIG_DESCRIPTION",
3663628f91a2SJoe Perches				     "please write a help paragraph that fully describes the config symbol\n" . "$here\n$stat_real\n");
3664628f91a2SJoe Perches			}
3665628f91a2SJoe Perches		}
36667ccf41a8SJoe Perches
36677ccf41a8SJoe Perches# check MAINTAINERS entries
36687ccf41a8SJoe Perches		if ($realfile =~ /^MAINTAINERS$/) {
36697ccf41a8SJoe Perches# check MAINTAINERS entries for the right form
36707ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
36717ccf41a8SJoe Perches			    $rawline !~ /^\+[A-Z]:\t\S/) {
36727ccf41a8SJoe Perches				if (WARN("MAINTAINERS_STYLE",
36737ccf41a8SJoe Perches					 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
36747ccf41a8SJoe Perches				    $fix) {
36757ccf41a8SJoe Perches					$fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
36767ccf41a8SJoe Perches				}
36777ccf41a8SJoe Perches			}
36787ccf41a8SJoe Perches# check MAINTAINERS entries for the right ordering too
36797ccf41a8SJoe Perches			my $preferred_order = 'MRLSWQBCPTFXNK';
36807ccf41a8SJoe Perches			if ($rawline =~ /^\+[A-Z]:/ &&
36817ccf41a8SJoe Perches			    $prevrawline =~ /^[\+ ][A-Z]:/) {
36827ccf41a8SJoe Perches				$rawline =~ /^\+([A-Z]):\s*(.*)/;
36837ccf41a8SJoe Perches				my $cur = $1;
36847ccf41a8SJoe Perches				my $curval = $2;
36857ccf41a8SJoe Perches				$prevrawline =~ /^[\+ ]([A-Z]):\s*(.*)/;
36867ccf41a8SJoe Perches				my $prev = $1;
36877ccf41a8SJoe Perches				my $prevval = $2;
36887ccf41a8SJoe Perches				my $curindex = index($preferred_order, $cur);
36897ccf41a8SJoe Perches				my $previndex = index($preferred_order, $prev);
36907ccf41a8SJoe Perches				if ($curindex < 0) {
36917ccf41a8SJoe Perches					WARN("MAINTAINERS_STYLE",
36927ccf41a8SJoe Perches					     "Unknown MAINTAINERS entry type: '$cur'\n" . $herecurr);
36937ccf41a8SJoe Perches				} else {
3694628f91a2SJoe Perches					if ($previndex >= 0 && $curindex < $previndex) {
3695c68e5878SArnaud Lacombe						WARN("MAINTAINERS_STYLE",
3696c68e5878SArnaud Lacombe						     "Misordered MAINTAINERS entry - list '$cur:' before '$prev:'\n" . $hereprev);
3697c68e5878SArnaud Lacombe					} elsif ((($prev eq 'F' && $cur eq 'F') ||
3698c68e5878SArnaud Lacombe						  ($prev eq 'X' && $cur eq 'X')) &&
3699c68e5878SArnaud Lacombe						 ($prevval cmp $curval) > 0) {
3700c68e5878SArnaud Lacombe						WARN("MAINTAINERS_STYLE",
3701c68e5878SArnaud Lacombe						     "Misordered MAINTAINERS entry - list file patterns in alphabetic order\n" . $hereprev);
3702c68e5878SArnaud Lacombe					}
3703c68e5878SArnaud Lacombe				}
3704c68e5878SArnaud Lacombe			}
3705c68e5878SArnaud Lacombe		}
3706c68e5878SArnaud Lacombe
3707c68e5878SArnaud Lacombe		if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
3708c68e5878SArnaud Lacombe		    ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
3709bff5da43SRob Herring			my $flag = $1;
37107dd05b38SFlorian Vaussard			my $replacement = {
37117dd05b38SFlorian Vaussard				'EXTRA_AFLAGS' =>   'asflags-y',
37127dd05b38SFlorian Vaussard				'EXTRA_CFLAGS' =>   'ccflags-y',
37137dd05b38SFlorian Vaussard				'EXTRA_CPPFLAGS' => 'cppflags-y',
3714bff5da43SRob Herring				'EXTRA_LDFLAGS' =>  'ldflags-y',
3715bff5da43SRob Herring			};
3716cc93319bSFlorian Vaussard
3717852d095dSRob Herring			WARN("DEPRECATED_VARIABLE",
3718cc93319bSFlorian Vaussard			     "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
3719bff5da43SRob Herring		}
3720bff5da43SRob Herring
3721185d566bSRob Herring# check for DT compatible documentation
3722185d566bSRob Herring		if (defined $root &&
3723185d566bSRob Herring			(($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
3724185d566bSRob Herring			 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
3725bff5da43SRob Herring
3726bff5da43SRob Herring			my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
3727bff5da43SRob Herring
3728bff5da43SRob Herring			my $dt_path = $root . "/Documentation/devicetree/bindings/";
3729bff5da43SRob Herring			my $vp_file = $dt_path . "vendor-prefixes.yaml";
37304fbf32a6SFlorian Vaussard
37314fbf32a6SFlorian Vaussard			foreach my $compat (@compats) {
3732852d095dSRob Herring				my $compat2 = $compat;
3733bff5da43SRob Herring				$compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
3734bff5da43SRob Herring				my $compat3 = $compat;
3735cc93319bSFlorian Vaussard				$compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
3736bff5da43SRob Herring				`grep -Erq "$compat|$compat2|$compat3" $dt_path`;
3737bff5da43SRob Herring				if ( $? >> 8 ) {
3738bff5da43SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
3739bff5da43SRob Herring					     "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
37409f3a8992SRob Herring				}
37419f3a8992SRob Herring
37429f3a8992SRob Herring				next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
37439f3a8992SRob Herring				my $vendor = $1;
37449f3a8992SRob Herring				`grep -Eq "\\"\\^\Q$vendor\E,\\.\\*\\":" $vp_file`;
37459f3a8992SRob Herring				if ( $? >> 8 ) {
37469f3a8992SRob Herring					WARN("UNDOCUMENTED_DT_STRING",
37479f3a8992SRob Herring					     "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
3748d1d84b5fSMiguel Ojeda				}
37499f3a8992SRob Herring			}
3750c8df0ab6SLubomir Rintel		}
37519f3a8992SRob Herring
37529f3a8992SRob Herring# check for using SPDX license tag at beginning of files
37539f3a8992SRob Herring		if ($realline == $checklicenseline) {
37549f3a8992SRob Herring			if ($rawline =~ /^[ \+]\s*\#\!\s*\//) {
37559f3a8992SRob Herring				$checklicenseline = 2;
3756fdf13693SJoe Perches			} elsif ($rawline =~ /^\+/) {
3757fdf13693SJoe Perches				my $comment = "";
3758fdf13693SJoe Perches				if ($realfile =~ /\.(h|s|S)$/) {
3759ffbce897SJoe Perches					$comment = '/*';
3760fdf13693SJoe Perches				} elsif ($realfile =~ /\.(c|rs|dts|dtsi)$/) {
3761fdf13693SJoe Perches					$comment = '//';
3762fdf13693SJoe Perches				} elsif (($checklicenseline == 2) || $realfile =~ /\.(sh|pl|py|awk|tc|yaml)$/) {
3763fdf13693SJoe Perches					$comment = '#';
37649f3a8992SRob Herring				} elsif ($realfile =~ /\.rst$/) {
3765ffbce897SJoe Perches					$comment = '..';
37669f3a8992SRob Herring				}
37679f3a8992SRob Herring
37683b6e8ac9SJoe Perches# check SPDX comment style for .[chsS] files
37693b6e8ac9SJoe Perches				if ($realfile =~ /\.[chsS]$/ &&
37703b6e8ac9SJoe Perches				    $rawline =~ /SPDX-License-Identifier:/ &&
37713b6e8ac9SJoe Perches				    $rawline !~ m@^\+\s*\Q$comment\E\s*@) {
37723b6e8ac9SJoe Perches					WARN("SPDX_LICENSE_TAG",
37733b6e8ac9SJoe Perches					     "Improper SPDX comment style for '$realfile', please use '$comment' instead\n" . $herecurr);
377450c92900SLubomir Rintel				}
377550c92900SLubomir Rintel
377650c92900SLubomir Rintel				if ($comment !~ /^$/ &&
377750c92900SLubomir Rintel				    $rawline !~ m@^\+\Q$comment\E SPDX-License-Identifier: @) {
377850c92900SLubomir Rintel					WARN("SPDX_LICENSE_TAG",
377950c92900SLubomir Rintel					     "Missing or malformed SPDX-License-Identifier tag in line $checklicenseline\n" . $herecurr);
378050c92900SLubomir Rintel				} elsif ($rawline =~ /(SPDX-License-Identifier: .*)/) {
378150c92900SLubomir Rintel					my $spdx_license = $1;
378250c92900SLubomir Rintel					if (!is_SPDX_License_valid($spdx_license)) {
378350c92900SLubomir Rintel						WARN("SPDX_LICENSE_TAG",
378450c92900SLubomir Rintel						     "'$spdx_license' is not supported in LICENSES/...\n" . $herecurr);
37859f3a8992SRob Herring					}
37869f3a8992SRob Herring					if ($realfile =~ m@^Documentation/devicetree/bindings/@ &&
3787a04bb4c2SDmitry Rokosov					    $spdx_license !~ /GPL-2\.0(?:-only)? OR BSD-2-Clause/) {
37889f3a8992SRob Herring						my $msg_level = \&WARN;
37899f3a8992SRob Herring						$msg_level = \&CHK if ($file);
37905368df20SAndy Whitcroft						if (&{$msg_level}("SPDX_LICENSE_TAG",
3791d6430f71SJoe Perches
37925368df20SAndy Whitcroft								  "DT binding documents should be licensed (GPL-2.0-only OR BSD-2-Clause)\n" . $herecurr) &&
3793a8da38a9SJoe Perches						    $fix) {
3794a8da38a9SJoe Perches							$fixed[$fixlinenr] =~ s/SPDX-License-Identifier: .*/SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)/;
3795a8da38a9SJoe Perches						}
3796a8da38a9SJoe Perches					}
3797a04bb4c2SDmitry Rokosov					if ($realfile =~ m@^include/dt-bindings/@ &&
3798a04bb4c2SDmitry Rokosov					    $spdx_license !~ /GPL-2\.0(?:-only)? OR \S+/) {
3799a04bb4c2SDmitry Rokosov						WARN("SPDX_LICENSE_TAG",
3800a04bb4c2SDmitry Rokosov						     "DT binding headers should be licensed (GPL-2.0-only OR .*)\n" . $herecurr);
3801a04bb4c2SDmitry Rokosov					}
380247e0c88bSJoe Perches				}
380347e0c88bSJoe Perches			}
380447e0c88bSJoe Perches		}
380547e0c88bSJoe Perches
3806a0154cdbSJoe Perches# check for embedded filenames
380736217357SJoe Perches		if ($rawline =~ /^\+.*\b\Q$realfile\E\b/) {
3808a0154cdbSJoe Perches			WARN("EMBEDDED_FILENAME",
3809a0154cdbSJoe Perches			     "It's generally not useful to have the filename in the file\n" . $herecurr);
3810a0154cdbSJoe Perches		}
3811a0154cdbSJoe Perches
38125e79d96eSJoe Perches# check we are in a valid source file if not then ignore this hunk
3813d1d84b5fSMiguel Ojeda		next if ($realfile !~ /\.(h|c|rs|s|S|sh|dtsi|dts)$/);
38140a920b5bSAndy Whitcroft
3815a8da38a9SJoe Perches# check for using SPDX-License-Identifier on the wrong line number
3816a8da38a9SJoe Perches		if ($realline != $checklicenseline &&
3817a8da38a9SJoe Perches		    $rawline =~ /\bSPDX-License-Identifier:/ &&
3818a8da38a9SJoe Perches		    substr($line, @-, @+ - @-) eq "$;" x (@+ - @-)) {
3819a8da38a9SJoe Perches			WARN("SPDX_LICENSE_TAG",
3820a8da38a9SJoe Perches			     "Misplaced SPDX-License-Identifier tag - use line $checklicenseline instead\n" . $herecurr);
3821a8da38a9SJoe Perches		}
3822a8da38a9SJoe Perches
3823c45dcabdSAndy Whitcroft# line length limit (with some exclusions)
382447e0c88bSJoe Perches#
382547e0c88bSJoe Perches# There are a few types of lines that may extend beyond $max_line_length:
382647e0c88bSJoe Perches#	logging functions like pr_info that end in a string
382747e0c88bSJoe Perches#	lines with a single string
382847e0c88bSJoe Perches#	#defines that are a single string
382947e0c88bSJoe Perches#	lines with an RFC3986 like URL
383047e0c88bSJoe Perches#
383147e0c88bSJoe Perches# There are 3 different line length message types:
383247e0c88bSJoe Perches# LONG_LINE_COMMENT	a comment starts before but extends beyond $max_line_length
383347e0c88bSJoe Perches# LONG_LINE_STRING	a string starts before but extends beyond $max_line_length
383447e0c88bSJoe Perches# LONG_LINE		all other lines longer than $max_line_length
383547e0c88bSJoe Perches#
383647e0c88bSJoe Perches# if LONG_LINE is ignored, the other 2 types are also ignored
383747e0c88bSJoe Perches#
383847e0c88bSJoe Perches
383947e0c88bSJoe Perches		if ($line =~ /^\+/ && $length > $max_line_length) {
384047e0c88bSJoe Perches			my $msg_type = "LONG_LINE";
384147e0c88bSJoe Perches
384247e0c88bSJoe Perches			# Check the allowed long line types first
384347e0c88bSJoe Perches
384447e0c88bSJoe Perches			# logging functions that end in a string that starts
384547e0c88bSJoe Perches			# before $max_line_length
384647e0c88bSJoe Perches			if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
384747e0c88bSJoe Perches			    length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
384847e0c88bSJoe Perches				$msg_type = "";
384947e0c88bSJoe Perches
385047e0c88bSJoe Perches			# lines with only strings (w/ possible termination)
385147e0c88bSJoe Perches			# #defines with only strings
385247e0c88bSJoe Perches			} elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
385347e0c88bSJoe Perches				 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
385447e0c88bSJoe Perches				$msg_type = "";
385547e0c88bSJoe Perches
3856cc147506SJoe Perches			# More special cases
3857cc147506SJoe Perches			} elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/ ||
3858cc147506SJoe Perches				 $line =~ /^\+\s*(?:\w+)?\s*DEFINE_PER_CPU/) {
3859d560a5f8SJoe Perches				$msg_type = "";
3860d560a5f8SJoe Perches
38612e4bbbc5SAndreas Brauchli			# URL ($rawline is used in case the URL is in a comment)
38622e4bbbc5SAndreas Brauchli			} elsif ($rawline =~ /^\+.*\b[a-z][\w\.\+\-]*:\/\/\S+/i) {
38632e4bbbc5SAndreas Brauchli				$msg_type = "";
38642e4bbbc5SAndreas Brauchli
386547e0c88bSJoe Perches			# Otherwise set the alternate message types
386647e0c88bSJoe Perches
386747e0c88bSJoe Perches			# a comment starts before $max_line_length
386847e0c88bSJoe Perches			} elsif ($line =~ /($;[\s$;]*)$/ &&
386947e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
387047e0c88bSJoe Perches				$msg_type = "LONG_LINE_COMMENT"
387147e0c88bSJoe Perches
387247e0c88bSJoe Perches			# a quoted string starts before $max_line_length
387347e0c88bSJoe Perches			} elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
387447e0c88bSJoe Perches				 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
387547e0c88bSJoe Perches				$msg_type = "LONG_LINE_STRING"
387647e0c88bSJoe Perches			}
387747e0c88bSJoe Perches
387847e0c88bSJoe Perches			if ($msg_type ne "" &&
3879d6bb3951SWolfram Sang			    show_type("LONG_LINE") && show_type($msg_type)) {
3880bdc48fa1SJoe Perches				my $msg_level = \&WARN;
3881bdc48fa1SJoe Perches				$msg_level = \&CHK if ($file);
3882bdc48fa1SJoe Perches				&{$msg_level}($msg_type,
3883bdc48fa1SJoe Perches					      "line length of $length exceeds $max_line_length columns\n" . $herecurr);
38840a920b5bSAndy Whitcroft			}
388547e0c88bSJoe Perches		}
38860a920b5bSAndy Whitcroft
38878905a67cSAndy Whitcroft# check for adding lines without a newline.
38888905a67cSAndy Whitcroft		if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
388947ca69b8STom Rix			if (WARN("MISSING_EOF_NEWLINE",
389047ca69b8STom Rix			         "adding a line without newline at end of file\n" . $herecurr) &&
389147ca69b8STom Rix			    $fix) {
389247ca69b8STom Rix				fix_delete_line($fixlinenr+1, "No newline at end of file");
389347ca69b8STom Rix			}
38948905a67cSAndy Whitcroft		}
38958905a67cSAndy Whitcroft
3896de93245cSAditya Srivastava# check for .L prefix local symbols in .S files
3897de93245cSAditya Srivastava		if ($realfile =~ /\.S$/ &&
3898de93245cSAditya Srivastava		    $line =~ /^\+\s*(?:[A-Z]+_)?SYM_[A-Z]+_(?:START|END)(?:_[A-Z_]+)?\s*\(\s*\.L/) {
3899de93245cSAditya Srivastava			WARN("AVOID_L_PREFIX",
3900f4bf1cd4SJonathan Corbet			     "Avoid using '.L' prefixed local symbol names for denoting a range of code via 'SYM_*_START/END' annotations; see Documentation/core-api/asm-annotations.rst\n" . $herecurr);
3901de93245cSAditya Srivastava		}
3902de93245cSAditya Srivastava
3903b9ea10d6SAndy Whitcroft# check we are in a valid source file C or perl if not then ignore this hunk
3904de4c924cSGeert Uytterhoeven		next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
39050a920b5bSAndy Whitcroft
39060a920b5bSAndy Whitcroft# at the beginning of a line any tabs must come first and anything
3907713a09deSAntonio Borneo# more than $tabsize must use tabs.
3908c2fdda0dSAndy Whitcroft		if ($rawline =~ /^\+\s* \t\s*\S/ ||
3909c2fdda0dSAndy Whitcroft		    $rawline =~ /^\+\s*        \s*/) {
3910c2fdda0dSAndy Whitcroft			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
3911d2c0a235SAndy Whitcroft			$rpt_cleaners = 1;
39123705ce5bSJoe Perches			if (ERROR("CODE_INDENT",
39133705ce5bSJoe Perches				  "code indent should use tabs where possible\n" . $herevet) &&
39143705ce5bSJoe Perches			    $fix) {
3915194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
39163705ce5bSJoe Perches			}
39170a920b5bSAndy Whitcroft		}
39180a920b5bSAndy Whitcroft
391908e44365SAlberto Panizzo# check for space before tabs.
392008e44365SAlberto Panizzo		if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
392108e44365SAlberto Panizzo			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
39223705ce5bSJoe Perches			if (WARN("SPACE_BEFORE_TAB",
39233705ce5bSJoe Perches				"please, no space before tabs\n" . $herevet) &&
39243705ce5bSJoe Perches			    $fix) {
3925194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3926713a09deSAntonio Borneo					   s/(^\+.*) {$tabsize,$tabsize}\t/$1\t\t/) {}
3927194f66fcSJoe Perches				while ($fixed[$fixlinenr] =~
3928c76f4cb3SJoe Perches					   s/(^\+.*) +\t/$1\t/) {}
39293705ce5bSJoe Perches			}
393008e44365SAlberto Panizzo		}
393108e44365SAlberto Panizzo
39326a487211SJoe Perches# check for assignments on the start of a line
39336a487211SJoe Perches		if ($sline =~ /^\+\s+($Assignment)[^=]/) {
3934da7355abSAditya Srivastava			my $operator = $1;
3935da7355abSAditya Srivastava			if (CHK("ASSIGNMENT_CONTINUATIONS",
3936da7355abSAditya Srivastava				"Assignment operator '$1' should be on the previous line\n" . $hereprev) &&
3937da7355abSAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
3938da7355abSAditya Srivastava				# add assignment operator to the previous line, remove from current line
3939da7355abSAditya Srivastava				$fixed[$fixlinenr - 1] .= " $operator";
3940da7355abSAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
3941da7355abSAditya Srivastava			}
39426a487211SJoe Perches		}
39436a487211SJoe Perches
3944d1fe9c09SJoe Perches# check for && or || at the start of a line
3945d1fe9c09SJoe Perches		if ($rawline =~ /^\+\s*(&&|\|\|)/) {
39468e08f076SAditya Srivastava			my $operator = $1;
39478e08f076SAditya Srivastava			if (CHK("LOGICAL_CONTINUATIONS",
39488e08f076SAditya Srivastava				"Logical continuations should be on the previous line\n" . $hereprev) &&
39498e08f076SAditya Srivastava			    $fix && $prevrawline =~ /^\+/) {
39508e08f076SAditya Srivastava				# insert logical operator at last non-comment, non-whitepsace char on previous line
39518e08f076SAditya Srivastava				$prevline =~ /[\s$;]*$/;
39528e08f076SAditya Srivastava				my $line_end = substr($prevrawline, $-[0]);
39538e08f076SAditya Srivastava				$fixed[$fixlinenr - 1] =~ s/\Q$line_end\E$/ $operator$line_end/;
39548e08f076SAditya Srivastava				$fixed[$fixlinenr] =~ s/\Q$operator\E\s*//;
39558e08f076SAditya Srivastava			}
3956d1fe9c09SJoe Perches		}
3957d1fe9c09SJoe Perches
3958a91e8994SJoe Perches# check indentation starts on a tab stop
39595b57980dSJoe Perches		if ($perl_version_ok &&
3960bd49111fSJoe Perches		    $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$)|$Declare\s*$Ident\s*[;=])/) {
3961a91e8994SJoe Perches			my $indent = length($1);
3962713a09deSAntonio Borneo			if ($indent % $tabsize) {
3963a91e8994SJoe Perches				if (WARN("TABSTOP",
3964a91e8994SJoe Perches					 "Statements should start on a tabstop\n" . $herecurr) &&
3965a91e8994SJoe Perches				    $fix) {
3966713a09deSAntonio Borneo					$fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/$tabsize)@e;
3967a91e8994SJoe Perches				}
3968a91e8994SJoe Perches			}
3969a91e8994SJoe Perches		}
3970a91e8994SJoe Perches
3971d1fe9c09SJoe Perches# check multi-line statement indentation matches previous line
39725b57980dSJoe Perches		if ($perl_version_ok &&
3973fd71f632SJoe Perches		    $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
3974d1fe9c09SJoe Perches			$prevline =~ /^\+(\t*)(.*)$/;
3975d1fe9c09SJoe Perches			my $oldindent = $1;
3976d1fe9c09SJoe Perches			my $rest = $2;
3977d1fe9c09SJoe Perches
3978d1fe9c09SJoe Perches			my $pos = pos_last_openparen($rest);
3979d1fe9c09SJoe Perches			if ($pos >= 0) {
3980b34a26f3SJoe Perches				$line =~ /^(\+| )([ \t]*)/;
3981b34a26f3SJoe Perches				my $newindent = $2;
3982d1fe9c09SJoe Perches
3983d1fe9c09SJoe Perches				my $goodtabindent = $oldindent .
3984713a09deSAntonio Borneo					"\t" x ($pos / $tabsize) .
3985713a09deSAntonio Borneo					" "  x ($pos % $tabsize);
3986d1fe9c09SJoe Perches				my $goodspaceindent = $oldindent . " "  x $pos;
3987d1fe9c09SJoe Perches
3988d1fe9c09SJoe Perches				if ($newindent ne $goodtabindent &&
3989d1fe9c09SJoe Perches				    $newindent ne $goodspaceindent) {
39903705ce5bSJoe Perches
39913705ce5bSJoe Perches					if (CHK("PARENTHESIS_ALIGNMENT",
39923705ce5bSJoe Perches						"Alignment should match open parenthesis\n" . $hereprev) &&
39933705ce5bSJoe Perches					    $fix && $line =~ /^\+/) {
3994194f66fcSJoe Perches						$fixed[$fixlinenr] =~
39953705ce5bSJoe Perches						    s/^\+[ \t]*/\+$goodtabindent/;
39963705ce5bSJoe Perches					}
3997d1fe9c09SJoe Perches				}
3998d1fe9c09SJoe Perches			}
3999d1fe9c09SJoe Perches		}
4000d1fe9c09SJoe Perches
40016ab3a970SJoe Perches# check for space after cast like "(int) foo" or "(struct foo) bar"
40026ab3a970SJoe Perches# avoid checking a few false positives:
40036ab3a970SJoe Perches#   "sizeof(<type>)" or "__alignof__(<type>)"
40046ab3a970SJoe Perches#   function pointer declarations like "(*foo)(int) = bar;"
40056ab3a970SJoe Perches#   structure definitions like "(struct foo) { 0 };"
40066ab3a970SJoe Perches#   multiline macros that define functions
40076ab3a970SJoe Perches#   known attributes or the __attribute__ keyword
40086ab3a970SJoe Perches		if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
40096ab3a970SJoe Perches		    (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
40103705ce5bSJoe Perches			if (CHK("SPACING",
4011f27c95dbSJoe Perches				"No space is necessary after a cast\n" . $herecurr) &&
40123705ce5bSJoe Perches			    $fix) {
4013194f66fcSJoe Perches				$fixed[$fixlinenr] =~
4014f27c95dbSJoe Perches				    s/(\(\s*$Type\s*\))[ \t]+/$1/;
40153705ce5bSJoe Perches			}
4016aad4f614SJoe Perches		}
4017aad4f614SJoe Perches
401886406b1cSJoe Perches# Block comment styles
401986406b1cSJoe Perches# Networking with an initial /*
402005880600SJoe Perches		if ($realfile =~ m@^(drivers/net/|net/)@ &&
4021fdb4bcd6SJoe Perches		    $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
402285ad978cSJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&
4023c70735c2SŁukasz Stelmach		    $realline > 3) { # Do not warn about the initial copyright comment block after SPDX-License-Identifier
402405880600SJoe Perches			WARN("NETWORKING_BLOCK_COMMENT_STYLE",
402505880600SJoe Perches			     "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
402605880600SJoe Perches		}
402705880600SJoe Perches
402886406b1cSJoe Perches# Block comments use * on subsequent lines
402986406b1cSJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
403086406b1cSJoe Perches		    $prevrawline =~ /^\+.*?\/\*/ &&		#starting /*
4031a605e32eSJoe Perches		    $prevrawline !~ /\*\/[ \t]*$/ &&		#no trailing */
403261135e96SJoe Perches		    $rawline =~ /^\+/ &&			#line is new
4033a605e32eSJoe Perches		    $rawline !~ /^\+[ \t]*\*/) {		#no leading *
403486406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
403586406b1cSJoe Perches			     "Block comments use * on subsequent lines\n" . $hereprev);
4036a605e32eSJoe Perches		}
4037a605e32eSJoe Perches
403886406b1cSJoe Perches# Block comments use */ on trailing lines
403986406b1cSJoe Perches		if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ &&	#trailing */
4040c24f9f19SJoe Perches		    $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ &&	#inline /*...*/
4041c24f9f19SJoe Perches		    $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ &&	#trailing **/
4042c24f9f19SJoe Perches		    $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) {	#non blank */
404386406b1cSJoe Perches			WARN("BLOCK_COMMENT_STYLE",
404486406b1cSJoe Perches			     "Block comments use a trailing */ on a separate line\n" . $herecurr);
404505880600SJoe Perches		}
404605880600SJoe Perches
404708eb9b80SJoe Perches# Block comment * alignment
404808eb9b80SJoe Perches		if ($prevline =~ /$;[ \t]*$/ &&			#ends in comment
4049af207524SJoe Perches		    $line =~ /^\+[ \t]*$;/ &&			#leading comment
4050af207524SJoe Perches		    $rawline =~ /^\+[ \t]*\*/ &&		#leading *
4051af207524SJoe Perches		    (($prevrawline =~ /^\+.*?\/\*/ &&		#leading /*
405208eb9b80SJoe Perches		      $prevrawline !~ /\*\/[ \t]*$/) ||		#no trailing */
4053af207524SJoe Perches		     $prevrawline =~ /^\+[ \t]*\*/)) {		#leading *
4054af207524SJoe Perches			my $oldindent;
405508eb9b80SJoe Perches			$prevrawline =~ m@^\+([ \t]*/?)\*@;
4056af207524SJoe Perches			if (defined($1)) {
4057af207524SJoe Perches				$oldindent = expand_tabs($1);
4058af207524SJoe Perches			} else {
4059af207524SJoe Perches				$prevrawline =~ m@^\+(.*/?)\*@;
4060af207524SJoe Perches				$oldindent = expand_tabs($1);
4061af207524SJoe Perches			}
406208eb9b80SJoe Perches			$rawline =~ m@^\+([ \t]*)\*@;
406308eb9b80SJoe Perches			my $newindent = $1;
406408eb9b80SJoe Perches			$newindent = expand_tabs($newindent);
4065af207524SJoe Perches			if (length($oldindent) ne length($newindent)) {
406608eb9b80SJoe Perches				WARN("BLOCK_COMMENT_STYLE",
406708eb9b80SJoe Perches				     "Block comments should align the * on each line\n" . $hereprev);
406808eb9b80SJoe Perches			}
406908eb9b80SJoe Perches		}
407008eb9b80SJoe Perches
40717f619191SJoe Perches# check for missing blank lines after struct/union declarations
40727f619191SJoe Perches# with exceptions for various attributes and macros
40737f619191SJoe Perches		if ($prevline =~ /^[\+ ]};?\s*$/ &&
40747f619191SJoe Perches		    $line =~ /^\+/ &&
40757f619191SJoe Perches		    !($line =~ /^\+\s*$/ ||
407671aa3419SSergey Senozhatsky		      $line =~ /^\+\s*(?:EXPORT_SYMBOL|early_param|ALLOW_ERROR_INJECTION)/ ||
40777f619191SJoe Perches		      $line =~ /^\+\s*MODULE_/i ||
40787f619191SJoe Perches		      $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
40797f619191SJoe Perches		      $line =~ /^\+[a-z_]*init/ ||
40807f619191SJoe Perches		      $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
40817f619191SJoe Perches		      $line =~ /^\+\s*DECLARE/ ||
40820bc989ffSMasahiro Yamada		      $line =~ /^\+\s*builtin_[\w_]*driver/ ||
40837f619191SJoe Perches		      $line =~ /^\+\s*__setup/)) {
4084d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
4085d752fcc8SJoe Perches				"Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
4086d752fcc8SJoe Perches			    $fix) {
4087f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, "\+");
4088d752fcc8SJoe Perches			}
40897f619191SJoe Perches		}
40907f619191SJoe Perches
4091365dd4eaSJoe Perches# check for multiple consecutive blank lines
4092365dd4eaSJoe Perches		if ($prevline =~ /^[\+ ]\s*$/ &&
4093365dd4eaSJoe Perches		    $line =~ /^\+\s*$/ &&
4094365dd4eaSJoe Perches		    $last_blank_line != ($linenr - 1)) {
4095d752fcc8SJoe Perches			if (CHK("LINE_SPACING",
4096d752fcc8SJoe Perches				"Please don't use multiple blank lines\n" . $hereprev) &&
4097d752fcc8SJoe Perches			    $fix) {
4098f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4099d752fcc8SJoe Perches			}
4100d752fcc8SJoe Perches
4101365dd4eaSJoe Perches			$last_blank_line = $linenr;
4102365dd4eaSJoe Perches		}
4103365dd4eaSJoe Perches
41043b617e3bSJoe Perches# check for missing blank lines after declarations
4105b5e8736aSJoe Perches# (declarations must have the same indentation and not be at the start of line)
4106b5e8736aSJoe Perches		if (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/) {
4107b5e8736aSJoe Perches			# use temporaries
4108b5e8736aSJoe Perches			my $sl = $sline;
4109b5e8736aSJoe Perches			my $pl = $prevline;
4110b5e8736aSJoe Perches			# remove $Attribute/$Sparse uses to simplify comparisons
4111b5e8736aSJoe Perches			$sl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4112b5e8736aSJoe Perches			$pl =~ s/\b(?:$Attribute|$Sparse)\b//g;
4113b5e8736aSJoe Perches			if (($pl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
41145a4e1fd3SJoe Perches			# function pointer declarations
4115b5e8736aSJoe Perches			     $pl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
41163f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
4117b5e8736aSJoe Perches			     $pl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
41183f7bac03SJoe Perches			# known declaration macros
4119b5e8736aSJoe Perches			     $pl =~ /^\+\s+$declaration_macros/) &&
41203f7bac03SJoe Perches			# for "else if" which can look like "$Ident $Ident"
4121b5e8736aSJoe Perches			    !($pl =~ /^\+\s+$c90_Keywords\b/ ||
41223f7bac03SJoe Perches			# other possible extensions of declaration lines
4123b5e8736aSJoe Perches			      $pl =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
41243f7bac03SJoe Perches			# not starting a section or a macro "\" extended line
4125b5e8736aSJoe Perches			      $pl =~ /(?:\{\s*|\\)$/) &&
41263f7bac03SJoe Perches			# looks like a declaration
4127b5e8736aSJoe Perches			    !($sl =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
41285a4e1fd3SJoe Perches			# function pointer declarations
4129b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
41303f7bac03SJoe Perches			# foo bar; where foo is some local typedef or #define
4131b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
41323f7bac03SJoe Perches			# known declaration macros
4133b5e8736aSJoe Perches			      $sl =~ /^\+\s+$declaration_macros/ ||
41343f7bac03SJoe Perches			# start of struct or union or enum
4135b5e8736aSJoe Perches			      $sl =~ /^\+\s+(?:static\s+)?(?:const\s+)?(?:union|struct|enum|typedef)\b/ ||
41363f7bac03SJoe Perches			# start or end of block or continuation of declaration
4137b5e8736aSJoe Perches			      $sl =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
41383f7bac03SJoe Perches			# bitfield continuation
4139b5e8736aSJoe Perches			      $sl =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
41403f7bac03SJoe Perches			# other possible extensions of declaration lines
4141b5e8736aSJoe Perches			      $sl =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/)) {
4142d752fcc8SJoe Perches				if (WARN("LINE_SPACING",
4143d752fcc8SJoe Perches					 "Missing a blank line after declarations\n" . $hereprev) &&
4144d752fcc8SJoe Perches				    $fix) {
4145f2d7e4d4SJoe Perches					fix_insert_line($fixlinenr, "\+");
4146d752fcc8SJoe Perches				}
41473b617e3bSJoe Perches			}
4148b5e8736aSJoe Perches		}
41493b617e3bSJoe Perches
41505f7ddae6SRaffaele Recalcati# check for spaces at the beginning of a line.
41516b4c5bebSAndy Whitcroft# Exceptions:
41526b4c5bebSAndy Whitcroft#  1) within comments
41536b4c5bebSAndy Whitcroft#  2) indented preprocessor commands
41546b4c5bebSAndy Whitcroft#  3) hanging labels
41553705ce5bSJoe Perches		if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/)  {
41565f7ddae6SRaffaele Recalcati			my $herevet = "$here\n" . cat_vet($rawline) . "\n";
41573705ce5bSJoe Perches			if (WARN("LEADING_SPACE",
41583705ce5bSJoe Perches				 "please, no spaces at the start of a line\n" . $herevet) &&
41593705ce5bSJoe Perches			    $fix) {
4160194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
41613705ce5bSJoe Perches			}
41625f7ddae6SRaffaele Recalcati		}
41635f7ddae6SRaffaele Recalcati
4164b9ea10d6SAndy Whitcroft# check we are in a valid C source file if not then ignore this hunk
4165b9ea10d6SAndy Whitcroft		next if ($realfile !~ /\.(h|c)$/);
4166b9ea10d6SAndy Whitcroft
41675751a24eSJoe Perches# check for unusual line ending [ or (
41685751a24eSJoe Perches		if ($line =~ /^\+.*([\[\(])\s*$/) {
41695751a24eSJoe Perches			CHK("OPEN_ENDED_LINE",
41705751a24eSJoe Perches			    "Lines should not end with a '$1'\n" . $herecurr);
41715751a24eSJoe Perches		}
41725751a24eSJoe Perches
41734dbed76fSJoe Perches# check if this appears to be the start function declaration, save the name
41744dbed76fSJoe Perches		if ($sline =~ /^\+\{\s*$/ &&
41754dbed76fSJoe Perches		    $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
41764dbed76fSJoe Perches			$context_function = $1;
41774dbed76fSJoe Perches		}
41784dbed76fSJoe Perches
41794dbed76fSJoe Perches# check if this appears to be the end of function declaration
41804dbed76fSJoe Perches		if ($sline =~ /^\+\}\s*$/) {
41814dbed76fSJoe Perches			undef $context_function;
41824dbed76fSJoe Perches		}
41834dbed76fSJoe Perches
4184032a4c0fSJoe Perches# check indentation of any line with a bare else
4185840080a0SJoe Perches# (but not if it is a multiple line "if (foo) return bar; else return baz;")
4186032a4c0fSJoe Perches# if the previous line is a break or return and is indented 1 tab more...
4187032a4c0fSJoe Perches		if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
4188032a4c0fSJoe Perches			my $tabs = length($1) + 1;
4189840080a0SJoe Perches			if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
4190840080a0SJoe Perches			    ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
4191840080a0SJoe Perches			     defined $lines[$linenr] &&
4192840080a0SJoe Perches			     $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
4193032a4c0fSJoe Perches				WARN("UNNECESSARY_ELSE",
4194032a4c0fSJoe Perches				     "else is not generally useful after a break or return\n" . $hereprev);
4195032a4c0fSJoe Perches			}
4196032a4c0fSJoe Perches		}
4197032a4c0fSJoe Perches
4198c00df19aSJoe Perches# check indentation of a line with a break;
4199dc58bc55SJoe Perches# if the previous line is a goto, return or break
4200dc58bc55SJoe Perches# and is indented the same # of tabs
4201c00df19aSJoe Perches		if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
4202c00df19aSJoe Perches			my $tabs = $1;
4203dc58bc55SJoe Perches			if ($prevline =~ /^\+$tabs(goto|return|break)\b/) {
4204dc58bc55SJoe Perches				if (WARN("UNNECESSARY_BREAK",
4205dc58bc55SJoe Perches					 "break is not useful after a $1\n" . $hereprev) &&
4206dc58bc55SJoe Perches				    $fix) {
4207dc58bc55SJoe Perches					fix_delete_line($fixlinenr, $rawline);
4208dc58bc55SJoe Perches				}
4209c00df19aSJoe Perches			}
4210c00df19aSJoe Perches		}
4211c00df19aSJoe Perches
4212c2fdda0dSAndy Whitcroft# check for RCS/CVS revision markers
4213cf655043SAndy Whitcroft		if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
4214000d1cc1SJoe Perches			WARN("CVS_KEYWORD",
4215000d1cc1SJoe Perches			     "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
4216c2fdda0dSAndy Whitcroft		}
421722f2a2efSAndy Whitcroft
421856e77d70SJoe Perches# check for old HOTPLUG __dev<foo> section markings
421956e77d70SJoe Perches		if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
422056e77d70SJoe Perches			WARN("HOTPLUG_SECTION",
422156e77d70SJoe Perches			     "Using $1 is unnecessary\n" . $herecurr);
422256e77d70SJoe Perches		}
422356e77d70SJoe Perches
42249c0ca6f9SAndy Whitcroft# Check for potential 'bare' types
42252b474a1aSAndy Whitcroft		my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
42262b474a1aSAndy Whitcroft		    $realline_next);
42273e469cdcSAndy Whitcroft#print "LINE<$line>\n";
4228ca819864SJoe Perches		if ($linenr > $suppress_statement &&
42291b5539b1SJoe Perches		    $realcnt && $sline =~ /.\s*\S/) {
4230170d3a22SAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4231f5fe35ddSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
4232171ae1a4SAndy Whitcroft			$stat =~ s/\n./\n /g;
4233171ae1a4SAndy Whitcroft			$cond =~ s/\n./\n /g;
4234171ae1a4SAndy Whitcroft
42353e469cdcSAndy Whitcroft#print "linenr<$linenr> <$stat>\n";
42363e469cdcSAndy Whitcroft			# If this statement has no statement boundaries within
42373e469cdcSAndy Whitcroft			# it there is no point in retrying a statement scan
42383e469cdcSAndy Whitcroft			# until we hit end of it.
42393e469cdcSAndy Whitcroft			my $frag = $stat; $frag =~ s/;+\s*$//;
42403e469cdcSAndy Whitcroft			if ($frag !~ /(?:{|;)/) {
42413e469cdcSAndy Whitcroft#print "skip<$line_nr_next>\n";
42423e469cdcSAndy Whitcroft				$suppress_statement = $line_nr_next;
42433e469cdcSAndy Whitcroft			}
4244f74bd194SAndy Whitcroft
42452b474a1aSAndy Whitcroft			# Find the real next line.
42462b474a1aSAndy Whitcroft			$realline_next = $line_nr_next;
42472b474a1aSAndy Whitcroft			if (defined $realline_next &&
42482b474a1aSAndy Whitcroft			    (!defined $lines[$realline_next - 1] ||
42492b474a1aSAndy Whitcroft			     substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
42502b474a1aSAndy Whitcroft				$realline_next++;
42512b474a1aSAndy Whitcroft			}
42522b474a1aSAndy Whitcroft
4253171ae1a4SAndy Whitcroft			my $s = $stat;
4254171ae1a4SAndy Whitcroft			$s =~ s/{.*$//s;
4255cf655043SAndy Whitcroft
4256c2fdda0dSAndy Whitcroft			# Ignore goto labels.
4257171ae1a4SAndy Whitcroft			if ($s =~ /$Ident:\*$/s) {
4258c2fdda0dSAndy Whitcroft
4259c2fdda0dSAndy Whitcroft			# Ignore functions being called
4260171ae1a4SAndy Whitcroft			} elsif ($s =~ /^.\s*$Ident\s*\(/s) {
4261c2fdda0dSAndy Whitcroft
4262463f2864SAndy Whitcroft			} elsif ($s =~ /^.\s*else\b/s) {
4263463f2864SAndy Whitcroft
4264c45dcabdSAndy Whitcroft			# declarations always start with types
4265d2506586SAndy Whitcroft			} elsif ($prev_values eq 'E' && $s =~ /^.\s*(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?((?:\s*$Ident)+?)\b(?:\s+$Sparse)?\s*\**\s*(?:$Ident|\(\*[^\)]*\))(?:\s*$Modifier)?\s*(?:;|=|,|\()/s) {
4266c45dcabdSAndy Whitcroft				my $type = $1;
4267c45dcabdSAndy Whitcroft				$type =~ s/\s+/ /g;
4268c45dcabdSAndy Whitcroft				possible($type, "A:" . $s);
4269c45dcabdSAndy Whitcroft
42706c72ffaaSAndy Whitcroft			# definitions in global scope can only start with types
4271a6a84062SAndy Whitcroft			} elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
4272c45dcabdSAndy Whitcroft				possible($1, "B:" . $s);
4273c2fdda0dSAndy Whitcroft			}
42748905a67cSAndy Whitcroft
42756c72ffaaSAndy Whitcroft			# any (foo ... *) is a pointer cast, and foo is a type
427665863862SAndy Whitcroft			while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
4277c45dcabdSAndy Whitcroft				possible($1, "C:" . $s);
42789c0ca6f9SAndy Whitcroft			}
42798905a67cSAndy Whitcroft
42808905a67cSAndy Whitcroft			# Check for any sort of function declaration.
42818905a67cSAndy Whitcroft			# int foo(something bar, other baz);
42828905a67cSAndy Whitcroft			# void (*store_gdt)(x86_descr_ptr *);
4283171ae1a4SAndy Whitcroft			if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
42848905a67cSAndy Whitcroft				my ($name_len) = length($1);
42858905a67cSAndy Whitcroft
4286cf655043SAndy Whitcroft				my $ctx = $s;
4287773647a0SAndy Whitcroft				substr($ctx, 0, $name_len + 1, '');
42888905a67cSAndy Whitcroft				$ctx =~ s/\)[^\)]*$//;
4289cf655043SAndy Whitcroft
42908905a67cSAndy Whitcroft				for my $arg (split(/\s*,\s*/, $ctx)) {
4291c45dcabdSAndy Whitcroft					if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
42928905a67cSAndy Whitcroft
4293c45dcabdSAndy Whitcroft						possible($1, "D:" . $s);
42948905a67cSAndy Whitcroft					}
42958905a67cSAndy Whitcroft				}
42968905a67cSAndy Whitcroft			}
42978905a67cSAndy Whitcroft
42989c0ca6f9SAndy Whitcroft		}
42999c0ca6f9SAndy Whitcroft
430000df344fSAndy Whitcroft#
430100df344fSAndy Whitcroft# Checks which may be anchored in the context.
430200df344fSAndy Whitcroft#
430300df344fSAndy Whitcroft
430400df344fSAndy Whitcroft# Check for switch () and associated case and default
430500df344fSAndy Whitcroft# statements should be at the same indent.
430600df344fSAndy Whitcroft		if ($line=~/\bswitch\s*\(.*\)/) {
430700df344fSAndy Whitcroft			my $err = '';
430800df344fSAndy Whitcroft			my $sep = '';
430900df344fSAndy Whitcroft			my @ctx = ctx_block_outer($linenr, $realcnt);
431000df344fSAndy Whitcroft			shift(@ctx);
431100df344fSAndy Whitcroft			for my $ctx (@ctx) {
431200df344fSAndy Whitcroft				my ($clen, $cindent) = line_stats($ctx);
431300df344fSAndy Whitcroft				if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
431400df344fSAndy Whitcroft							$indent != $cindent) {
431500df344fSAndy Whitcroft					$err .= "$sep$ctx\n";
431600df344fSAndy Whitcroft					$sep = '';
431700df344fSAndy Whitcroft				} else {
431800df344fSAndy Whitcroft					$sep = "[...]\n";
431900df344fSAndy Whitcroft				}
432000df344fSAndy Whitcroft			}
432100df344fSAndy Whitcroft			if ($err ne '') {
4322000d1cc1SJoe Perches				ERROR("SWITCH_CASE_INDENT_LEVEL",
4323000d1cc1SJoe Perches				      "switch and case should be at the same indent\n$hereline$err");
4324de7d4f0eSAndy Whitcroft			}
4325de7d4f0eSAndy Whitcroft		}
4326de7d4f0eSAndy Whitcroft
4327de7d4f0eSAndy Whitcroft# if/while/etc brace do not go on next line, unless defining a do while loop,
4328de7d4f0eSAndy Whitcroft# or if that brace on the next line is for something else
43290fe3dc2bSJoe Perches		if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
4330773647a0SAndy Whitcroft			my $pre_ctx = "$1$2";
4331773647a0SAndy Whitcroft
43329c0ca6f9SAndy Whitcroft			my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
43338eef05ddSJoe Perches
43348eef05ddSJoe Perches			if ($line =~ /^\+\t{6,}/) {
43358eef05ddSJoe Perches				WARN("DEEP_INDENTATION",
43368eef05ddSJoe Perches				     "Too many leading tabs - consider code refactoring\n" . $herecurr);
43378eef05ddSJoe Perches			}
43388eef05ddSJoe Perches
4339de7d4f0eSAndy Whitcroft			my $ctx_cnt = $realcnt - $#ctx - 1;
4340de7d4f0eSAndy Whitcroft			my $ctx = join("\n", @ctx);
4341de7d4f0eSAndy Whitcroft
4342548596d5SAndy Whitcroft			my $ctx_ln = $linenr;
4343548596d5SAndy Whitcroft			my $ctx_skip = $realcnt;
4344de7d4f0eSAndy Whitcroft
4345548596d5SAndy Whitcroft			while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
4346548596d5SAndy Whitcroft					defined $lines[$ctx_ln - 1] &&
4347548596d5SAndy Whitcroft					$lines[$ctx_ln - 1] =~ /^-/)) {
4348548596d5SAndy Whitcroft				##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
4349548596d5SAndy Whitcroft				$ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
4350773647a0SAndy Whitcroft				$ctx_ln++;
4351773647a0SAndy Whitcroft			}
4352548596d5SAndy Whitcroft
435353210168SAndy Whitcroft			#print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
435453210168SAndy Whitcroft			#print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
4355773647a0SAndy Whitcroft
4356773647a0SAndy Whitcroft			if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
4357000d1cc1SJoe Perches				ERROR("OPEN_BRACE",
4358000d1cc1SJoe Perches				      "that open brace { should be on the previous line\n" .
435901464f30SAndy Whitcroft					"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
436000df344fSAndy Whitcroft			}
4361773647a0SAndy Whitcroft			if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
4362773647a0SAndy Whitcroft			    $ctx =~ /\)\s*\;\s*$/ &&
4363773647a0SAndy Whitcroft			    defined $lines[$ctx_ln - 1])
4364773647a0SAndy Whitcroft			{
43659c0ca6f9SAndy Whitcroft				my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
43669c0ca6f9SAndy Whitcroft				if ($nindent > $indent) {
4367000d1cc1SJoe Perches					WARN("TRAILING_SEMICOLON",
4368000d1cc1SJoe Perches					     "trailing semicolon indicates no statements, indent implies otherwise\n" .
436901464f30SAndy Whitcroft						"$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
43709c0ca6f9SAndy Whitcroft				}
43719c0ca6f9SAndy Whitcroft			}
437200df344fSAndy Whitcroft		}
437300df344fSAndy Whitcroft
43744d001e4dSAndy Whitcroft# Check relative indent for conditionals and blocks.
4375f6950a73SJoe Perches		if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
43763e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
43773e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
43783e469cdcSAndy Whitcroft					if (!defined $stat);
43794d001e4dSAndy Whitcroft			my ($s, $c) = ($stat, $cond);
43804d001e4dSAndy Whitcroft
43814d001e4dSAndy Whitcroft			substr($s, 0, length($c), '');
43824d001e4dSAndy Whitcroft
43839f5af480SJoe Perches			# remove inline comments
43849f5af480SJoe Perches			$s =~ s/$;/ /g;
43859f5af480SJoe Perches			$c =~ s/$;/ /g;
43864d001e4dSAndy Whitcroft
43874d001e4dSAndy Whitcroft			# Find out how long the conditional actually is.
43886f779c18SAndy Whitcroft			my @newlines = ($c =~ /\n/gs);
43896f779c18SAndy Whitcroft			my $cond_lines = 1 + $#newlines;
43904d001e4dSAndy Whitcroft
43919f5af480SJoe Perches			# Make sure we remove the line prefixes as we have
43929f5af480SJoe Perches			# none on the first line, and are going to readd them
43939f5af480SJoe Perches			# where necessary.
43949f5af480SJoe Perches			$s =~ s/\n./\n/gs;
43959f5af480SJoe Perches			while ($s =~ /\n\s+\\\n/) {
43969f5af480SJoe Perches				$cond_lines += $s =~ s/\n\s+\\\n/\n/g;
43979f5af480SJoe Perches			}
43989f5af480SJoe Perches
43994d001e4dSAndy Whitcroft			# We want to check the first line inside the block
44004d001e4dSAndy Whitcroft			# starting at the end of the conditional, so remove:
44014d001e4dSAndy Whitcroft			#  1) any blank line termination
44024d001e4dSAndy Whitcroft			#  2) any opening brace { on end of the line
44034d001e4dSAndy Whitcroft			#  3) any do (...) {
44044d001e4dSAndy Whitcroft			my $continuation = 0;
44054d001e4dSAndy Whitcroft			my $check = 0;
44064d001e4dSAndy Whitcroft			$s =~ s/^.*\bdo\b//;
44074d001e4dSAndy Whitcroft			$s =~ s/^\s*{//;
44084d001e4dSAndy Whitcroft			if ($s =~ s/^\s*\\//) {
44094d001e4dSAndy Whitcroft				$continuation = 1;
44104d001e4dSAndy Whitcroft			}
44119bd49efeSAndy Whitcroft			if ($s =~ s/^\s*?\n//) {
44124d001e4dSAndy Whitcroft				$check = 1;
44134d001e4dSAndy Whitcroft				$cond_lines++;
44144d001e4dSAndy Whitcroft			}
44154d001e4dSAndy Whitcroft
44164d001e4dSAndy Whitcroft			# Also ignore a loop construct at the end of a
44174d001e4dSAndy Whitcroft			# preprocessor statement.
44184d001e4dSAndy Whitcroft			if (($prevline =~ /^.\s*#\s*define\s/ ||
44194d001e4dSAndy Whitcroft			    $prevline =~ /\\\s*$/) && $continuation == 0) {
44204d001e4dSAndy Whitcroft				$check = 0;
44214d001e4dSAndy Whitcroft			}
44224d001e4dSAndy Whitcroft
44239bd49efeSAndy Whitcroft			my $cond_ptr = -1;
4424740504c6SAndy Whitcroft			$continuation = 0;
44259bd49efeSAndy Whitcroft			while ($cond_ptr != $cond_lines) {
44269bd49efeSAndy Whitcroft				$cond_ptr = $cond_lines;
44274d001e4dSAndy Whitcroft
4428f16fa28fSAndy Whitcroft				# If we see an #else/#elif then the code
4429f16fa28fSAndy Whitcroft				# is not linear.
4430f16fa28fSAndy Whitcroft				if ($s =~ /^\s*\#\s*(?:else|elif)/) {
4431f16fa28fSAndy Whitcroft					$check = 0;
4432f16fa28fSAndy Whitcroft				}
4433f16fa28fSAndy Whitcroft
44349bd49efeSAndy Whitcroft				# Ignore:
44359bd49efeSAndy Whitcroft				#  1) blank lines, they should be at 0,
44369bd49efeSAndy Whitcroft				#  2) preprocessor lines, and
44379bd49efeSAndy Whitcroft				#  3) labels.
4438740504c6SAndy Whitcroft				if ($continuation ||
4439740504c6SAndy Whitcroft				    $s =~ /^\s*?\n/ ||
44409bd49efeSAndy Whitcroft				    $s =~ /^\s*#\s*?/ ||
44419bd49efeSAndy Whitcroft				    $s =~ /^\s*$Ident\s*:/) {
4442740504c6SAndy Whitcroft					$continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
444330dad6ebSAndy Whitcroft					if ($s =~ s/^.*?\n//) {
44449bd49efeSAndy Whitcroft						$cond_lines++;
44459bd49efeSAndy Whitcroft					}
44464d001e4dSAndy Whitcroft				}
444730dad6ebSAndy Whitcroft			}
44484d001e4dSAndy Whitcroft
44494d001e4dSAndy Whitcroft			my (undef, $sindent) = line_stats("+" . $s);
44504d001e4dSAndy Whitcroft			my $stat_real = raw_line($linenr, $cond_lines);
44514d001e4dSAndy Whitcroft
44524d001e4dSAndy Whitcroft			# Check if either of these lines are modified, else
44534d001e4dSAndy Whitcroft			# this is not this patch's fault.
44544d001e4dSAndy Whitcroft			if (!defined($stat_real) ||
44554d001e4dSAndy Whitcroft			    $stat !~ /^\+/ && $stat_real !~ /^\+/) {
44564d001e4dSAndy Whitcroft				$check = 0;
44574d001e4dSAndy Whitcroft			}
44584d001e4dSAndy Whitcroft			if (defined($stat_real) && $cond_lines > 1) {
44594d001e4dSAndy Whitcroft				$stat_real = "[...]\n$stat_real";
44604d001e4dSAndy Whitcroft			}
44614d001e4dSAndy Whitcroft
44629bd49efeSAndy Whitcroft			#print "line<$line> prevline<$prevline> indent<$indent> sindent<$sindent> check<$check> continuation<$continuation> s<$s> cond_lines<$cond_lines> stat_real<$stat_real> stat<$stat>\n";
44634d001e4dSAndy Whitcroft
44649f5af480SJoe Perches			if ($check && $s ne '' &&
4465713a09deSAntonio Borneo			    (($sindent % $tabsize) != 0 ||
44669f5af480SJoe Perches			     ($sindent < $indent) ||
4467f6950a73SJoe Perches			     ($sindent == $indent &&
4468f6950a73SJoe Perches			      ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
4469713a09deSAntonio Borneo			     ($sindent > $indent + $tabsize))) {
4470000d1cc1SJoe Perches				WARN("SUSPECT_CODE_INDENT",
4471000d1cc1SJoe Perches				     "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
44724d001e4dSAndy Whitcroft			}
44734d001e4dSAndy Whitcroft		}
44744d001e4dSAndy Whitcroft
44756c72ffaaSAndy Whitcroft		# Track the 'values' across context and added lines.
44766c72ffaaSAndy Whitcroft		my $opline = $line; $opline =~ s/^./ /;
44771f65f947SAndy Whitcroft		my ($curr_values, $curr_vars) =
44781f65f947SAndy Whitcroft				annotate_values($opline . "\n", $prev_values);
44796c72ffaaSAndy Whitcroft		$curr_values = $prev_values . $curr_values;
4480c2fdda0dSAndy Whitcroft		if ($dbg_values) {
4481c2fdda0dSAndy Whitcroft			my $outline = $opline; $outline =~ s/\t/ /g;
4482cf655043SAndy Whitcroft			print "$linenr > .$outline\n";
4483cf655043SAndy Whitcroft			print "$linenr > $curr_values\n";
44841f65f947SAndy Whitcroft			print "$linenr >  $curr_vars\n";
4485c2fdda0dSAndy Whitcroft		}
44866c72ffaaSAndy Whitcroft		$prev_values = substr($curr_values, -1);
44876c72ffaaSAndy Whitcroft
448800df344fSAndy Whitcroft#ignore lines not being added
44893705ce5bSJoe Perches		next if ($line =~ /^[^\+]/);
449000df344fSAndy Whitcroft
449199ca38c2SJoe Perches# check for self assignments used to avoid compiler warnings
449299ca38c2SJoe Perches# e.g.:	int foo = foo, *bar = NULL;
449399ca38c2SJoe Perches#	struct foo bar = *(&(bar));
449499ca38c2SJoe Perches		if ($line =~ /^\+\s*(?:$Declare)?([A-Za-z_][A-Za-z\d_]*)\s*=/) {
449599ca38c2SJoe Perches			my $var = $1;
449699ca38c2SJoe Perches			if ($line =~ /^\+\s*(?:$Declare)?$var\s*=\s*(?:$var|\*\s*\(?\s*&\s*\(?\s*$var\s*\)?\s*\)?)\s*[;,]/) {
449799ca38c2SJoe Perches				WARN("SELF_ASSIGNMENT",
449899ca38c2SJoe Perches				     "Do not use self-assignments to avoid compiler warnings\n" . $herecurr);
449999ca38c2SJoe Perches			}
450099ca38c2SJoe Perches		}
450199ca38c2SJoe Perches
450211ca40a0SJoe Perches# check for dereferences that span multiple lines
450311ca40a0SJoe Perches		if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
450411ca40a0SJoe Perches		    $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
450511ca40a0SJoe Perches			$prevline =~ /($Lval\s*(?:\.|->))\s*$/;
450611ca40a0SJoe Perches			my $ref = $1;
450711ca40a0SJoe Perches			$line =~ /^.\s*($Lval)/;
450811ca40a0SJoe Perches			$ref .= $1;
450911ca40a0SJoe Perches			$ref =~ s/\s//g;
451011ca40a0SJoe Perches			WARN("MULTILINE_DEREFERENCE",
451111ca40a0SJoe Perches			     "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
451211ca40a0SJoe Perches		}
451311ca40a0SJoe Perches
4514a1ce18e4SJoe Perches# check for declarations of signed or unsigned without int
4515c8447115SJoe Perches		while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
4516a1ce18e4SJoe Perches			my $type = $1;
4517a1ce18e4SJoe Perches			my $var = $2;
4518207a8e84SJoe Perches			$var = "" if (!defined $var);
4519207a8e84SJoe Perches			if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
4520a1ce18e4SJoe Perches				my $sign = $1;
4521a1ce18e4SJoe Perches				my $pointer = $2;
4522a1ce18e4SJoe Perches
4523a1ce18e4SJoe Perches				$pointer = "" if (!defined $pointer);
4524a1ce18e4SJoe Perches
4525a1ce18e4SJoe Perches				if (WARN("UNSPECIFIED_INT",
4526a1ce18e4SJoe Perches					 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
4527a1ce18e4SJoe Perches				    $fix) {
4528a1ce18e4SJoe Perches					my $decl = trim($sign) . " int ";
4529207a8e84SJoe Perches					my $comp_pointer = $pointer;
4530207a8e84SJoe Perches					$comp_pointer =~ s/\s//g;
4531207a8e84SJoe Perches					$decl .= $comp_pointer;
4532207a8e84SJoe Perches					$decl = rtrim($decl) if ($var eq "");
4533207a8e84SJoe Perches					$fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
4534a1ce18e4SJoe Perches				}
4535a1ce18e4SJoe Perches			}
4536a1ce18e4SJoe Perches		}
4537a1ce18e4SJoe Perches
4538653d4876SAndy Whitcroft# TEST: allow direct testing of the type matcher.
45397429c690SAndy Whitcroft		if ($dbg_type) {
45407429c690SAndy Whitcroft			if ($line =~ /^.\s*$Declare\s*$/) {
4541000d1cc1SJoe Perches				ERROR("TEST_TYPE",
4542000d1cc1SJoe Perches				      "TEST: is type\n" . $herecurr);
45437429c690SAndy Whitcroft			} elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
4544000d1cc1SJoe Perches				ERROR("TEST_NOT_TYPE",
4545000d1cc1SJoe Perches				      "TEST: is not type ($1 is)\n". $herecurr);
45467429c690SAndy Whitcroft			}
4547653d4876SAndy Whitcroft			next;
4548653d4876SAndy Whitcroft		}
4549a1ef277eSAndy Whitcroft# TEST: allow direct testing of the attribute matcher.
4550a1ef277eSAndy Whitcroft		if ($dbg_attr) {
45519360b0e5SAndy Whitcroft			if ($line =~ /^.\s*$Modifier\s*$/) {
4552000d1cc1SJoe Perches				ERROR("TEST_ATTR",
4553000d1cc1SJoe Perches				      "TEST: is attr\n" . $herecurr);
45549360b0e5SAndy Whitcroft			} elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
4555000d1cc1SJoe Perches				ERROR("TEST_NOT_ATTR",
4556000d1cc1SJoe Perches				      "TEST: is not attr ($1 is)\n". $herecurr);
4557a1ef277eSAndy Whitcroft			}
4558a1ef277eSAndy Whitcroft			next;
4559a1ef277eSAndy Whitcroft		}
4560653d4876SAndy Whitcroft
4561f0a594c1SAndy Whitcroft# check for initialisation to aggregates open brace on the next line
456299423c20SAndy Whitcroft		if ($line =~ /^.\s*{/ &&
456399423c20SAndy Whitcroft		    $prevline =~ /(?:^|[^=])=\s*$/) {
4564d752fcc8SJoe Perches			if (ERROR("OPEN_BRACE",
4565d752fcc8SJoe Perches				  "that open brace { should be on the previous line\n" . $hereprev) &&
4566f2d7e4d4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4567f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
4568f2d7e4d4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
4569d752fcc8SJoe Perches				my $fixedline = $prevrawline;
4570d752fcc8SJoe Perches				$fixedline =~ s/\s*=\s*$/ = {/;
4571f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4572d752fcc8SJoe Perches				$fixedline = $line;
45738d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1/;
4574f2d7e4d4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
4575d752fcc8SJoe Perches			}
4576f0a594c1SAndy Whitcroft		}
4577f0a594c1SAndy Whitcroft
457800df344fSAndy Whitcroft#
457900df344fSAndy Whitcroft# Checks which are anchored on the added line.
458000df344fSAndy Whitcroft#
458100df344fSAndy Whitcroft
4582653d4876SAndy Whitcroft# check for malformed paths in #include statements (uses RAW line)
4583c45dcabdSAndy Whitcroft		if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
4584653d4876SAndy Whitcroft			my $path = $1;
4585653d4876SAndy Whitcroft			if ($path =~ m{//}) {
4586000d1cc1SJoe Perches				ERROR("MALFORMED_INCLUDE",
4587495e9d84SJoe Perches				      "malformed #include filename\n" . $herecurr);
4588495e9d84SJoe Perches			}
4589495e9d84SJoe Perches			if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
4590495e9d84SJoe Perches				ERROR("UAPI_INCLUDE",
4591495e9d84SJoe Perches				      "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
4592653d4876SAndy Whitcroft			}
4593653d4876SAndy Whitcroft		}
4594653d4876SAndy Whitcroft
459500df344fSAndy Whitcroft# no C99 // comments
459600df344fSAndy Whitcroft		if ($line =~ m{//}) {
45973705ce5bSJoe Perches			if (ERROR("C99_COMMENTS",
45983705ce5bSJoe Perches				  "do not use C99 // comments\n" . $herecurr) &&
45993705ce5bSJoe Perches			    $fix) {
4600194f66fcSJoe Perches				my $line = $fixed[$fixlinenr];
46013705ce5bSJoe Perches				if ($line =~ /\/\/(.*)$/) {
46023705ce5bSJoe Perches					my $comment = trim($1);
4603194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
46043705ce5bSJoe Perches				}
46053705ce5bSJoe Perches			}
460600df344fSAndy Whitcroft		}
460700df344fSAndy Whitcroft		# Remove C99 comments.
46080a920b5bSAndy Whitcroft		$line =~ s@//.*@@;
46096c72ffaaSAndy Whitcroft		$opline =~ s@//.*@@;
46100a920b5bSAndy Whitcroft
46112b474a1aSAndy Whitcroft# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
46122b474a1aSAndy Whitcroft# the whole statement.
46132b474a1aSAndy Whitcroft#print "APW <$lines[$realline_next - 1]>\n";
46142b474a1aSAndy Whitcroft		if (defined $realline_next &&
46152b474a1aSAndy Whitcroft		    exists $lines[$realline_next - 1] &&
46162b474a1aSAndy Whitcroft		    !defined $suppress_export{$realline_next} &&
461736794822SChristoph Hellwig		    ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
46183cbf62dfSAndy Whitcroft			# Handle definitions which produce identifiers with
46193cbf62dfSAndy Whitcroft			# a prefix:
46203cbf62dfSAndy Whitcroft			#   XXX(foo);
46213cbf62dfSAndy Whitcroft			#   EXPORT_SYMBOL(something_foo);
4622653d4876SAndy Whitcroft			my $name = $1;
462370a11659SJoe Perches			$name =~ s/^\s*($Ident).*/$1/;
462487a53877SAndy Whitcroft			if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
46253cbf62dfSAndy Whitcroft			    $name =~ /^${Ident}_$2/) {
46263cbf62dfSAndy Whitcroft#print "FOO C name<$name>\n";
46273cbf62dfSAndy Whitcroft				$suppress_export{$realline_next} = 1;
46283cbf62dfSAndy Whitcroft
46293cbf62dfSAndy Whitcroft			} elsif ($stat !~ /(?:
46302b474a1aSAndy Whitcroft				\n.}\s*$|
463148012058SAndy Whitcroft				^.DEFINE_$Ident\(\Q$name\E\)|
463248012058SAndy Whitcroft				^.DECLARE_$Ident\(\Q$name\E\)|
463348012058SAndy Whitcroft				^.LIST_HEAD\(\Q$name\E\)|
46342b474a1aSAndy Whitcroft				^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
46352b474a1aSAndy Whitcroft				\b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
463648012058SAndy Whitcroft			    )/x) {
46372b474a1aSAndy Whitcroft#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
46382b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 2;
46392b474a1aSAndy Whitcroft			} else {
46402b474a1aSAndy Whitcroft				$suppress_export{$realline_next} = 1;
46410a920b5bSAndy Whitcroft			}
46420a920b5bSAndy Whitcroft		}
46432b474a1aSAndy Whitcroft		if (!defined $suppress_export{$linenr} &&
46442b474a1aSAndy Whitcroft		    $prevline =~ /^.\s*$/ &&
464536794822SChristoph Hellwig		    ($line =~ /EXPORT_SYMBOL.*\((.*)\)/)) {
46462b474a1aSAndy Whitcroft#print "FOO B <$lines[$linenr - 1]>\n";
46472b474a1aSAndy Whitcroft			$suppress_export{$linenr} = 2;
46482b474a1aSAndy Whitcroft		}
46492b474a1aSAndy Whitcroft		if (defined $suppress_export{$linenr} &&
46502b474a1aSAndy Whitcroft		    $suppress_export{$linenr} == 2) {
4651000d1cc1SJoe Perches			WARN("EXPORT_SYMBOL",
4652000d1cc1SJoe Perches			     "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
46532b474a1aSAndy Whitcroft		}
46540a920b5bSAndy Whitcroft
46555150bda4SJoe Eloff# check for global initialisers.
46565b8f82e1SSong Liu		if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/ &&
46575b8f82e1SSong Liu		    !exclude_global_initialisers($realfile)) {
4658d5e616fcSJoe Perches			if (ERROR("GLOBAL_INITIALISERS",
46596d32f7a3SJoe Perches				  "do not initialise globals to $1\n" . $herecurr) &&
4660d5e616fcSJoe Perches			    $fix) {
46616d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
4662d5e616fcSJoe Perches			}
4663f0a594c1SAndy Whitcroft		}
46640a920b5bSAndy Whitcroft# check for static initialisers.
46656d32f7a3SJoe Perches		if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
4666d5e616fcSJoe Perches			if (ERROR("INITIALISED_STATIC",
46676d32f7a3SJoe Perches				  "do not initialise statics to $1\n" .
4668d5e616fcSJoe Perches				      $herecurr) &&
4669d5e616fcSJoe Perches			    $fix) {
46706d32f7a3SJoe Perches				$fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
4671d5e616fcSJoe Perches			}
46720a920b5bSAndy Whitcroft		}
46730a920b5bSAndy Whitcroft
46741813087dSJoe Perches# check for misordered declarations of char/short/int/long with signed/unsigned
46751813087dSJoe Perches		while ($sline =~ m{(\b$TypeMisordered\b)}g) {
46761813087dSJoe Perches			my $tmp = trim($1);
46771813087dSJoe Perches			WARN("MISORDERED_TYPE",
46781813087dSJoe Perches			     "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
46791813087dSJoe Perches		}
46801813087dSJoe Perches
4681809e082eSJoe Perches# check for unnecessary <signed> int declarations of short/long/long long
4682809e082eSJoe Perches		while ($sline =~ m{\b($TypeMisordered(\s*\*)*|$C90_int_types)\b}g) {
4683809e082eSJoe Perches			my $type = trim($1);
4684809e082eSJoe Perches			next if ($type !~ /\bint\b/);
4685809e082eSJoe Perches			next if ($type !~ /\b(?:short|long\s+long|long)\b/);
4686809e082eSJoe Perches			my $new_type = $type;
4687809e082eSJoe Perches			$new_type =~ s/\b\s*int\s*\b/ /;
4688809e082eSJoe Perches			$new_type =~ s/\b\s*(?:un)?signed\b\s*/ /;
4689809e082eSJoe Perches			$new_type =~ s/^const\s+//;
4690809e082eSJoe Perches			$new_type = "unsigned $new_type" if ($type =~ /\bunsigned\b/);
4691809e082eSJoe Perches			$new_type = "const $new_type" if ($type =~ /^const\b/);
4692809e082eSJoe Perches			$new_type =~ s/\s+/ /g;
4693809e082eSJoe Perches			$new_type = trim($new_type);
4694809e082eSJoe Perches			if (WARN("UNNECESSARY_INT",
4695809e082eSJoe Perches				 "Prefer '$new_type' over '$type' as the int is unnecessary\n" . $herecurr) &&
4696809e082eSJoe Perches			    $fix) {
4697809e082eSJoe Perches				$fixed[$fixlinenr] =~ s/\b\Q$type\E\b/$new_type/;
4698809e082eSJoe Perches			}
4699809e082eSJoe Perches		}
4700809e082eSJoe Perches
4701cb710ecaSJoe Perches# check for static const char * arrays.
4702cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
4703000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4704000d1cc1SJoe Perches			     "static const char * array should probably be static const char * const\n" .
4705cb710ecaSJoe Perches				$herecurr);
4706cb710ecaSJoe Perches		}
4707cb710ecaSJoe Perches
470877b8c0a8SJoe Perches# check for initialized const char arrays that should be static const
470977b8c0a8SJoe Perches		if ($line =~ /^\+\s*const\s+(char|unsigned\s+char|_*u8|(?:[us]_)?int8_t)\s+\w+\s*\[\s*(?:\w+\s*)?\]\s*=\s*"/) {
471077b8c0a8SJoe Perches			if (WARN("STATIC_CONST_CHAR_ARRAY",
471177b8c0a8SJoe Perches				 "const array should probably be static const\n" . $herecurr) &&
471277b8c0a8SJoe Perches			    $fix) {
471377b8c0a8SJoe Perches				$fixed[$fixlinenr] =~ s/(^.\s*)const\b/${1}static const/;
471477b8c0a8SJoe Perches			}
471577b8c0a8SJoe Perches		}
471677b8c0a8SJoe Perches
4717cb710ecaSJoe Perches# check for static char foo[] = "bar" declarations.
4718cb710ecaSJoe Perches		if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
4719000d1cc1SJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
4720000d1cc1SJoe Perches			     "static char array declaration should probably be static const char\n" .
4721cb710ecaSJoe Perches				$herecurr);
4722cb710ecaSJoe Perches		}
4723cb710ecaSJoe Perches
4724ab7e23f3SJoe Perches# check for const <foo> const where <foo> is not a pointer or array type
4725ab7e23f3SJoe Perches		if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
4726ab7e23f3SJoe Perches			my $found = $1;
4727ab7e23f3SJoe Perches			if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
4728ab7e23f3SJoe Perches				WARN("CONST_CONST",
4729ab7e23f3SJoe Perches				     "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
4730ab7e23f3SJoe Perches			} elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
4731ab7e23f3SJoe Perches				WARN("CONST_CONST",
4732ab7e23f3SJoe Perches				     "'const $found const' should probably be 'const $found'\n" . $herecurr);
4733ab7e23f3SJoe Perches			}
4734ab7e23f3SJoe Perches		}
4735ab7e23f3SJoe Perches
473673169765SJoe Perches# check for const static or static <non ptr type> const declarations
473773169765SJoe Perches# prefer 'static const <foo>' over 'const static <foo>' and 'static <foo> const'
473873169765SJoe Perches		if ($sline =~ /^\+\s*const\s+static\s+($Type)\b/ ||
473973169765SJoe Perches		    $sline =~ /^\+\s*static\s+($BasicType)\s+const\b/) {
474073169765SJoe Perches			if (WARN("STATIC_CONST",
474173169765SJoe Perches				 "Move const after static - use 'static const $1'\n" . $herecurr) &&
474273169765SJoe Perches			    $fix) {
474373169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bconst\s+static\b/static const/;
474473169765SJoe Perches				$fixed[$fixlinenr] =~ s/\bstatic\s+($BasicType)\s+const\b/static const $1/;
474573169765SJoe Perches			}
474673169765SJoe Perches		}
474773169765SJoe Perches
47489b0fa60dSJoe Perches# check for non-global char *foo[] = {"bar", ...} declarations.
47499b0fa60dSJoe Perches		if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
47509b0fa60dSJoe Perches			WARN("STATIC_CONST_CHAR_ARRAY",
47519b0fa60dSJoe Perches			     "char * array declaration might be better as static const\n" .
47529b0fa60dSJoe Perches				$herecurr);
47539b0fa60dSJoe Perches		}
47549b0fa60dSJoe Perches
4755b598b670SJoe Perches# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
4756b598b670SJoe Perches		if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
4757b598b670SJoe Perches			my $array = $1;
4758b598b670SJoe Perches			if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
4759b598b670SJoe Perches				my $array_div = $1;
4760b598b670SJoe Perches				if (WARN("ARRAY_SIZE",
4761b598b670SJoe Perches					 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
4762b598b670SJoe Perches				    $fix) {
4763b598b670SJoe Perches					$fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
4764b598b670SJoe Perches				}
4765b598b670SJoe Perches			}
4766b598b670SJoe Perches		}
4767b598b670SJoe Perches
4768b36190c5SJoe Perches# check for function declarations without arguments like "int foo()"
476916b7f3c8SJoe Perches		if ($line =~ /(\b$Type\s*$Ident)\s*\(\s*\)/) {
4770b36190c5SJoe Perches			if (ERROR("FUNCTION_WITHOUT_ARGS",
4771b36190c5SJoe Perches				  "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
4772b36190c5SJoe Perches			    $fix) {
4773194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
4774b36190c5SJoe Perches			}
4775b36190c5SJoe Perches		}
4776b36190c5SJoe Perches
4777653d4876SAndy Whitcroft# check for new typedefs, only function parameters and sparse annotations
4778653d4876SAndy Whitcroft# make sense.
4779653d4876SAndy Whitcroft		if ($line =~ /\btypedef\s/ &&
47808054576dSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
4781c45dcabdSAndy Whitcroft		    $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
47828ed22cadSAndy Whitcroft		    $line !~ /\b$typeTypedefs\b/ &&
478346d832f5SMichael S. Tsirkin		    $line !~ /\b__bitwise\b/) {
4784000d1cc1SJoe Perches			WARN("NEW_TYPEDEFS",
4785000d1cc1SJoe Perches			     "do not add new typedefs\n" . $herecurr);
47860a920b5bSAndy Whitcroft		}
47870a920b5bSAndy Whitcroft
47880a920b5bSAndy Whitcroft# * goes on variable not on type
478965863862SAndy Whitcroft		# (char*[ const])
4790bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
4791bfcb2cc7SAndy Whitcroft			#print "AA<$1>\n";
47923705ce5bSJoe Perches			my ($ident, $from, $to) = ($1, $2, $2);
4793d8aaf121SAndy Whitcroft
479465863862SAndy Whitcroft			# Should start with a space.
479565863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
479665863862SAndy Whitcroft			# Should not end with a space.
479765863862SAndy Whitcroft			$to =~ s/\s+$//;
479865863862SAndy Whitcroft			# '*'s should not have spaces between.
4799f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
480065863862SAndy Whitcroft			}
4801d8aaf121SAndy Whitcroft
48023705ce5bSJoe Perches##			print "1: from<$from> to<$to> ident<$ident>\n";
480365863862SAndy Whitcroft			if ($from ne $to) {
48043705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
48053705ce5bSJoe Perches					  "\"(foo$from)\" should be \"(foo$to)\"\n" .  $herecurr) &&
48063705ce5bSJoe Perches				    $fix) {
48073705ce5bSJoe Perches					my $sub_from = $ident;
48083705ce5bSJoe Perches					my $sub_to = $ident;
48093705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4810194f66fcSJoe Perches					$fixed[$fixlinenr] =~
48113705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
48123705ce5bSJoe Perches				}
481365863862SAndy Whitcroft			}
4814bfcb2cc7SAndy Whitcroft		}
4815bfcb2cc7SAndy Whitcroft		while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
4816bfcb2cc7SAndy Whitcroft			#print "BB<$1>\n";
48173705ce5bSJoe Perches			my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
4818d8aaf121SAndy Whitcroft
481965863862SAndy Whitcroft			# Should start with a space.
482065863862SAndy Whitcroft			$to =~ s/^(\S)/ $1/;
482165863862SAndy Whitcroft			# Should not end with a space.
482265863862SAndy Whitcroft			$to =~ s/\s+$//;
482365863862SAndy Whitcroft			# '*'s should not have spaces between.
4824f9a0b3d1SAndy Whitcroft			while ($to =~ s/\*\s+\*/\*\*/) {
482565863862SAndy Whitcroft			}
482665863862SAndy Whitcroft			# Modifiers should have spaces.
482765863862SAndy Whitcroft			$to =~ s/(\b$Modifier$)/$1 /;
482865863862SAndy Whitcroft
48293705ce5bSJoe Perches##			print "2: from<$from> to<$to> ident<$ident>\n";
4830667026e7SAndy Whitcroft			if ($from ne $to && $ident !~ /^$Modifier$/) {
48313705ce5bSJoe Perches				if (ERROR("POINTER_LOCATION",
48323705ce5bSJoe Perches					  "\"foo${from}bar\" should be \"foo${to}bar\"\n" .  $herecurr) &&
48333705ce5bSJoe Perches				    $fix) {
48343705ce5bSJoe Perches
48353705ce5bSJoe Perches					my $sub_from = $match;
48363705ce5bSJoe Perches					my $sub_to = $match;
48373705ce5bSJoe Perches					$sub_to =~ s/\Q$from\E/$to/;
4838194f66fcSJoe Perches					$fixed[$fixlinenr] =~
48393705ce5bSJoe Perches					    s@\Q$sub_from\E@$sub_to@;
48403705ce5bSJoe Perches				}
484165863862SAndy Whitcroft			}
48420a920b5bSAndy Whitcroft		}
48430a920b5bSAndy Whitcroft
484469d517e6SDavid Hildenbrand# do not use BUG() or variants
484569d517e6SDavid Hildenbrand		if ($line =~ /\b(?!AA_|BUILD_|DCCP_|IDA_|KVM_|RWLOCK_|snd_|SPIN_)(?:[a-zA-Z_]*_)?BUG(?:_ON)?(?:_[A-Z_]+)?\s*\(/) {
48460675a8fbSJean Delvare			my $msg_level = \&WARN;
48470675a8fbSJean Delvare			$msg_level = \&CHK if ($file);
48480675a8fbSJean Delvare			&{$msg_level}("AVOID_BUG",
484969d517e6SDavid Hildenbrand				      "Do not crash the kernel unless it is absolutely unavoidable--use WARN_ON_ONCE() plus recovery code (if feasible) instead of BUG() or variants\n" . $herecurr);
48509d3e3c70SJoe Perches		}
48510a920b5bSAndy Whitcroft
48529d3e3c70SJoe Perches# avoid LINUX_VERSION_CODE
48538905a67cSAndy Whitcroft		if ($line =~ /\bLINUX_VERSION_CODE\b/) {
4854000d1cc1SJoe Perches			WARN("LINUX_VERSION_CODE",
4855000d1cc1SJoe Perches			     "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
48568905a67cSAndy Whitcroft		}
48578905a67cSAndy Whitcroft
485817441227SJoe Perches# check for uses of printk_ratelimit
485917441227SJoe Perches		if ($line =~ /\bprintk_ratelimit\s*\(/) {
4860000d1cc1SJoe Perches			WARN("PRINTK_RATELIMITED",
4861000d1cc1SJoe Perches			     "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
486217441227SJoe Perches		}
486317441227SJoe Perches
4864eeef5733SJoe Perches# printk should use KERN_* levels
4865eeef5733SJoe Perches		if ($line =~ /\bprintk\s*\(\s*(?!KERN_[A-Z]+\b)/) {
4866000d1cc1SJoe Perches			WARN("PRINTK_WITHOUT_KERN_LEVEL",
4867eeef5733SJoe Perches			     "printk() should include KERN_<LEVEL> facility level\n" . $herecurr);
486800df344fSAndy Whitcroft		}
48690a920b5bSAndy Whitcroft
4870f5eea3b0SJoe Perches# prefer variants of (subsystem|netdev|dev|pr)_<level> to printk(KERN_<LEVEL>
4871f5eea3b0SJoe Perches		if ($line =~ /\b(printk(_once|_ratelimited)?)\s*\(\s*KERN_([A-Z]+)/) {
4872f5eea3b0SJoe Perches			my $printk = $1;
4873f5eea3b0SJoe Perches			my $modifier = $2;
4874f5eea3b0SJoe Perches			my $orig = $3;
4875f5eea3b0SJoe Perches			$modifier = "" if (!defined($modifier));
4876243f3803SJoe Perches			my $level = lc($orig);
4877243f3803SJoe Perches			$level = "warn" if ($level eq "warning");
48788f26b837SJoe Perches			my $level2 = $level;
48798f26b837SJoe Perches			$level2 = "dbg" if ($level eq "debug");
4880f5eea3b0SJoe Perches			$level .= $modifier;
4881f5eea3b0SJoe Perches			$level2 .= $modifier;
4882243f3803SJoe Perches			WARN("PREFER_PR_LEVEL",
4883f5eea3b0SJoe Perches			     "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(...  to $printk(KERN_$orig ...\n" . $herecurr);
4884243f3803SJoe Perches		}
4885243f3803SJoe Perches
4886f5eea3b0SJoe Perches# prefer dev_<level> to dev_printk(KERN_<LEVEL>
4887dc139313SJoe Perches		if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
4888dc139313SJoe Perches			my $orig = $1;
4889dc139313SJoe Perches			my $level = lc($orig);
4890dc139313SJoe Perches			$level = "warn" if ($level eq "warning");
4891dc139313SJoe Perches			$level = "dbg" if ($level eq "debug");
4892dc139313SJoe Perches			WARN("PREFER_DEV_LEVEL",
4893dc139313SJoe Perches			     "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
4894dc139313SJoe Perches		}
4895dc139313SJoe Perches
48968020b253SNicolas Boichat# trace_printk should not be used in production code.
48978020b253SNicolas Boichat		if ($line =~ /\b(trace_printk|trace_puts|ftrace_vprintk)\s*\(/) {
48988020b253SNicolas Boichat			WARN("TRACE_PRINTK",
48998020b253SNicolas Boichat			     "Do not use $1() in production code (this can be ignored if built only with a debug config option)\n" . $herecurr);
49008020b253SNicolas Boichat		}
49018020b253SNicolas Boichat
490291c9afafSAndy Lutomirski# ENOSYS means "bad syscall nr" and nothing else.  This will have a small
490391c9afafSAndy Lutomirski# number of false positives, but assembly files are not checked, so at
490491c9afafSAndy Lutomirski# least the arch entry code will not trigger this warning.
490591c9afafSAndy Lutomirski		if ($line =~ /\bENOSYS\b/) {
490691c9afafSAndy Lutomirski			WARN("ENOSYS",
490791c9afafSAndy Lutomirski			     "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
490891c9afafSAndy Lutomirski		}
490991c9afafSAndy Lutomirski
49106b9ea5ffSJakub Kicinski# ENOTSUPP is not a standard error code and should be avoided in new patches.
49116b9ea5ffSJakub Kicinski# Folks usually mean EOPNOTSUPP (also called ENOTSUP), when they type ENOTSUPP.
49126b9ea5ffSJakub Kicinski# Similarly to ENOSYS warning a small number of false positives is expected.
49136b9ea5ffSJakub Kicinski		if (!$file && $line =~ /\bENOTSUPP\b/) {
49146b9ea5ffSJakub Kicinski			if (WARN("ENOTSUPP",
49156b9ea5ffSJakub Kicinski				 "ENOTSUPP is not a SUSV4 error code, prefer EOPNOTSUPP\n" . $herecurr) &&
49166b9ea5ffSJakub Kicinski			    $fix) {
49176b9ea5ffSJakub Kicinski				$fixed[$fixlinenr] =~ s/\bENOTSUPP\b/EOPNOTSUPP/;
49186b9ea5ffSJakub Kicinski			}
49196b9ea5ffSJakub Kicinski		}
49206b9ea5ffSJakub Kicinski
4921653d4876SAndy Whitcroft# function brace can't be on same line, except for #defines of do while,
4922653d4876SAndy Whitcroft# or if closed on same line
49235b57980dSJoe Perches		if ($perl_version_ok &&
49242d453e3bSJoe Perches		    $sline =~ /$Type\s*$Ident\s*$balanced_parens\s*\{/ &&
49252d453e3bSJoe Perches		    $sline !~ /\#\s*define\b.*do\s*\{/ &&
49262d453e3bSJoe Perches		    $sline !~ /}/) {
49278d182478SJoe Perches			if (ERROR("OPEN_BRACE",
49282d453e3bSJoe Perches				  "open brace '{' following function definitions go on the next line\n" . $herecurr) &&
49298d182478SJoe Perches			    $fix) {
49308d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49318d182478SJoe Perches				my $fixed_line = $rawline;
493203f49351SDwaipayan Ray				$fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*)\{(.*)$/;
49338d182478SJoe Perches				my $line1 = $1;
49348d182478SJoe Perches				my $line2 = $2;
49358d182478SJoe Perches				fix_insert_line($fixlinenr, ltrim($line1));
49368d182478SJoe Perches				fix_insert_line($fixlinenr, "\+{");
49378d182478SJoe Perches				if ($line2 !~ /^\s*$/) {
49388d182478SJoe Perches					fix_insert_line($fixlinenr, "\+\t" . trim($line2));
49398d182478SJoe Perches				}
49408d182478SJoe Perches			}
49410a920b5bSAndy Whitcroft		}
4942653d4876SAndy Whitcroft
49438905a67cSAndy Whitcroft# open braces for enum, union and struct go on the same line.
49448905a67cSAndy Whitcroft		if ($line =~ /^.\s*{/ &&
49458905a67cSAndy Whitcroft		    $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
49468d182478SJoe Perches			if (ERROR("OPEN_BRACE",
49478d182478SJoe Perches				  "open brace '{' following $1 go on the same line\n" . $hereprev) &&
49488d182478SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
49498d182478SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
49508d182478SJoe Perches				fix_delete_line($fixlinenr, $rawline);
49518d182478SJoe Perches				my $fixedline = rtrim($prevrawline) . " {";
49528d182478SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
49538d182478SJoe Perches				$fixedline = $rawline;
49548d81ae05SCyril Bur				$fixedline =~ s/^(.\s*)\{\s*/$1\t/;
49558d182478SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
49568d182478SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
49578d182478SJoe Perches				}
49588d182478SJoe Perches			}
49598905a67cSAndy Whitcroft		}
49608905a67cSAndy Whitcroft
49610c73b4ebSAndy Whitcroft# missing space after union, struct or enum definition
49623705ce5bSJoe Perches		if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
49633705ce5bSJoe Perches			if (WARN("SPACING",
49643705ce5bSJoe Perches				 "missing space after $1 definition\n" . $herecurr) &&
49653705ce5bSJoe Perches			    $fix) {
4966194f66fcSJoe Perches				$fixed[$fixlinenr] =~
49673705ce5bSJoe Perches				    s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
49683705ce5bSJoe Perches			}
49690c73b4ebSAndy Whitcroft		}
49700c73b4ebSAndy Whitcroft
497131070b5dSJoe Perches# Function pointer declarations
497231070b5dSJoe Perches# check spacing between type, funcptr, and args
497331070b5dSJoe Perches# canonical declaration is "type (*funcptr)(args...)"
497491f72e9cSJoe Perches		if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
497531070b5dSJoe Perches			my $declare = $1;
497631070b5dSJoe Perches			my $pre_pointer_space = $2;
497731070b5dSJoe Perches			my $post_pointer_space = $3;
497831070b5dSJoe Perches			my $funcname = $4;
497931070b5dSJoe Perches			my $post_funcname_space = $5;
498031070b5dSJoe Perches			my $pre_args_space = $6;
498131070b5dSJoe Perches
498291f72e9cSJoe Perches# the $Declare variable will capture all spaces after the type
498391f72e9cSJoe Perches# so check it for a missing trailing missing space but pointer return types
498491f72e9cSJoe Perches# don't need a space so don't warn for those.
498591f72e9cSJoe Perches			my $post_declare_space = "";
498691f72e9cSJoe Perches			if ($declare =~ /(\s+)$/) {
498791f72e9cSJoe Perches				$post_declare_space = $1;
498891f72e9cSJoe Perches				$declare = rtrim($declare);
498991f72e9cSJoe Perches			}
499091f72e9cSJoe Perches			if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
499131070b5dSJoe Perches				WARN("SPACING",
499231070b5dSJoe Perches				     "missing space after return type\n" . $herecurr);
499391f72e9cSJoe Perches				$post_declare_space = " ";
499431070b5dSJoe Perches			}
499531070b5dSJoe Perches
499631070b5dSJoe Perches# unnecessary space "type  (*funcptr)(args...)"
499791f72e9cSJoe Perches# This test is not currently implemented because these declarations are
499891f72e9cSJoe Perches# equivalent to
499991f72e9cSJoe Perches#	int  foo(int bar, ...)
500091f72e9cSJoe Perches# and this is form shouldn't/doesn't generate a checkpatch warning.
500191f72e9cSJoe Perches#
500291f72e9cSJoe Perches#			elsif ($declare =~ /\s{2,}$/) {
500391f72e9cSJoe Perches#				WARN("SPACING",
500491f72e9cSJoe Perches#				     "Multiple spaces after return type\n" . $herecurr);
500591f72e9cSJoe Perches#			}
500631070b5dSJoe Perches
500731070b5dSJoe Perches# unnecessary space "type ( *funcptr)(args...)"
500831070b5dSJoe Perches			if (defined $pre_pointer_space &&
500931070b5dSJoe Perches			    $pre_pointer_space =~ /^\s/) {
501031070b5dSJoe Perches				WARN("SPACING",
501131070b5dSJoe Perches				     "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
501231070b5dSJoe Perches			}
501331070b5dSJoe Perches
501431070b5dSJoe Perches# unnecessary space "type (* funcptr)(args...)"
501531070b5dSJoe Perches			if (defined $post_pointer_space &&
501631070b5dSJoe Perches			    $post_pointer_space =~ /^\s/) {
501731070b5dSJoe Perches				WARN("SPACING",
501831070b5dSJoe Perches				     "Unnecessary space before function pointer name\n" . $herecurr);
501931070b5dSJoe Perches			}
502031070b5dSJoe Perches
502131070b5dSJoe Perches# unnecessary space "type (*funcptr )(args...)"
502231070b5dSJoe Perches			if (defined $post_funcname_space &&
502331070b5dSJoe Perches			    $post_funcname_space =~ /^\s/) {
502431070b5dSJoe Perches				WARN("SPACING",
502531070b5dSJoe Perches				     "Unnecessary space after function pointer name\n" . $herecurr);
502631070b5dSJoe Perches			}
502731070b5dSJoe Perches
502831070b5dSJoe Perches# unnecessary space "type (*funcptr) (args...)"
502931070b5dSJoe Perches			if (defined $pre_args_space &&
503031070b5dSJoe Perches			    $pre_args_space =~ /^\s/) {
503131070b5dSJoe Perches				WARN("SPACING",
503231070b5dSJoe Perches				     "Unnecessary space before function pointer arguments\n" . $herecurr);
503331070b5dSJoe Perches			}
503431070b5dSJoe Perches
503531070b5dSJoe Perches			if (show_type("SPACING") && $fix) {
5036194f66fcSJoe Perches				$fixed[$fixlinenr] =~
503791f72e9cSJoe Perches				    s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
503831070b5dSJoe Perches			}
503931070b5dSJoe Perches		}
504031070b5dSJoe Perches
50418d31cfceSAndy Whitcroft# check for spacing round square brackets; allowed:
50428d31cfceSAndy Whitcroft#  1. with a type on the left -- int [] a;
5043fe2a7dbcSAndy Whitcroft#  2. at the beginning of a line for slice initialisers -- [0...10] = 5,
5044fe2a7dbcSAndy Whitcroft#  3. inside a curly brace -- = { [0...10] = 5 }
50458d31cfceSAndy Whitcroft		while ($line =~ /(.*?\s)\[/g) {
50468d31cfceSAndy Whitcroft			my ($where, $prefix) = ($-[1], $1);
50478d31cfceSAndy Whitcroft			if ($prefix !~ /$Type\s+$/ &&
5048fe2a7dbcSAndy Whitcroft			    ($where != 0 || $prefix !~ /^.\s+$/) &&
504938dca988SHeinrich Schuchardt			    $prefix !~ /[{,:]\s+$/) {
50503705ce5bSJoe Perches				if (ERROR("BRACKET_SPACE",
50513705ce5bSJoe Perches					  "space prohibited before open square bracket '['\n" . $herecurr) &&
50523705ce5bSJoe Perches				    $fix) {
5053194f66fcSJoe Perches				    $fixed[$fixlinenr] =~
50543705ce5bSJoe Perches					s/^(\+.*?)\s+\[/$1\[/;
50553705ce5bSJoe Perches				}
50568d31cfceSAndy Whitcroft			}
50578d31cfceSAndy Whitcroft		}
50588d31cfceSAndy Whitcroft
5059f0a594c1SAndy Whitcroft# check for spaces between functions and their parentheses.
50606c72ffaaSAndy Whitcroft		while ($line =~ /($Ident)\s+\(/g) {
5061c2fdda0dSAndy Whitcroft			my $name = $1;
5062773647a0SAndy Whitcroft			my $ctx_before = substr($line, 0, $-[1]);
5063773647a0SAndy Whitcroft			my $ctx = "$ctx_before$name";
5064c2fdda0dSAndy Whitcroft
5065c2fdda0dSAndy Whitcroft			# Ignore those directives where spaces _are_ permitted.
5066773647a0SAndy Whitcroft			if ($name =~ /^(?:
5067773647a0SAndy Whitcroft				if|for|while|switch|return|case|
5068773647a0SAndy Whitcroft				volatile|__volatile__|
5069773647a0SAndy Whitcroft				__attribute__|format|__extension__|
507054da6a09SPeter Zijlstra				asm|__asm__|scoped_guard)$/x)
5071773647a0SAndy Whitcroft			{
5072c2fdda0dSAndy Whitcroft			# cpp #define statements have non-optional spaces, ie
5073c2fdda0dSAndy Whitcroft			# if there is a space between the name and the open
5074c2fdda0dSAndy Whitcroft			# parenthesis it is simply not a parameter group.
5075c45dcabdSAndy Whitcroft			} elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
5076773647a0SAndy Whitcroft
5077773647a0SAndy Whitcroft			# cpp #elif statement condition may start with a (
5078c45dcabdSAndy Whitcroft			} elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
5079c2fdda0dSAndy Whitcroft
5080c2fdda0dSAndy Whitcroft			# If this whole things ends with a type its most
5081c2fdda0dSAndy Whitcroft			# likely a typedef for a function.
5082773647a0SAndy Whitcroft			} elsif ($ctx =~ /$Type$/) {
5083c2fdda0dSAndy Whitcroft
5084c2fdda0dSAndy Whitcroft			} else {
50853705ce5bSJoe Perches				if (WARN("SPACING",
50863705ce5bSJoe Perches					 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
50873705ce5bSJoe Perches					     $fix) {
5088194f66fcSJoe Perches					$fixed[$fixlinenr] =~
50893705ce5bSJoe Perches					    s/\b$name\s+\(/$name\(/;
50903705ce5bSJoe Perches				}
5091f0a594c1SAndy Whitcroft			}
50926c72ffaaSAndy Whitcroft		}
50939a4cad4eSEric Nelson
5094653d4876SAndy Whitcroft# Check operator spacing.
50950a920b5bSAndy Whitcroft		if (!($line=~/\#\s*include/)) {
50963705ce5bSJoe Perches			my $fixed_line = "";
50973705ce5bSJoe Perches			my $line_fixed = 0;
50983705ce5bSJoe Perches
50999c0ca6f9SAndy Whitcroft			my $ops = qr{
51009c0ca6f9SAndy Whitcroft				<<=|>>=|<=|>=|==|!=|
51019c0ca6f9SAndy Whitcroft				\+=|-=|\*=|\/=|%=|\^=|\|=|&=|
51029c0ca6f9SAndy Whitcroft				=>|->|<<|>>|<|>|=|!|~|
51031f65f947SAndy Whitcroft				&&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
510484731623SJoe Perches				\?:|\?|:
51059c0ca6f9SAndy Whitcroft			}x;
5106cf655043SAndy Whitcroft			my @elements = split(/($ops|;)/, $opline);
51073705ce5bSJoe Perches
51083705ce5bSJoe Perches##			print("element count: <" . $#elements . ">\n");
51093705ce5bSJoe Perches##			foreach my $el (@elements) {
51103705ce5bSJoe Perches##				print("el: <$el>\n");
51113705ce5bSJoe Perches##			}
51123705ce5bSJoe Perches
51133705ce5bSJoe Perches			my @fix_elements = ();
511400df344fSAndy Whitcroft			my $off = 0;
51156c72ffaaSAndy Whitcroft
51163705ce5bSJoe Perches			foreach my $el (@elements) {
51173705ce5bSJoe Perches				push(@fix_elements, substr($rawline, $off, length($el)));
51183705ce5bSJoe Perches				$off += length($el);
51193705ce5bSJoe Perches			}
51203705ce5bSJoe Perches
51213705ce5bSJoe Perches			$off = 0;
51223705ce5bSJoe Perches
51236c72ffaaSAndy Whitcroft			my $blank = copy_spacing($opline);
5124b34c648bSJoe Perches			my $last_after = -1;
51256c72ffaaSAndy Whitcroft
51260a920b5bSAndy Whitcroft			for (my $n = 0; $n < $#elements; $n += 2) {
51273705ce5bSJoe Perches
51283705ce5bSJoe Perches				my $good = $fix_elements[$n] . $fix_elements[$n + 1];
51293705ce5bSJoe Perches
51303705ce5bSJoe Perches##				print("n: <$n> good: <$good>\n");
51313705ce5bSJoe Perches
51324a0df2efSAndy Whitcroft				$off += length($elements[$n]);
51334a0df2efSAndy Whitcroft
513425985edcSLucas De Marchi				# Pick up the preceding and succeeding characters.
5135773647a0SAndy Whitcroft				my $ca = substr($opline, 0, $off);
5136773647a0SAndy Whitcroft				my $cc = '';
5137773647a0SAndy Whitcroft				if (length($opline) >= ($off + length($elements[$n + 1]))) {
5138773647a0SAndy Whitcroft					$cc = substr($opline, $off + length($elements[$n + 1]));
5139773647a0SAndy Whitcroft				}
5140773647a0SAndy Whitcroft				my $cb = "$ca$;$cc";
5141773647a0SAndy Whitcroft
51424a0df2efSAndy Whitcroft				my $a = '';
51434a0df2efSAndy Whitcroft				$a = 'V' if ($elements[$n] ne '');
51444a0df2efSAndy Whitcroft				$a = 'W' if ($elements[$n] =~ /\s$/);
5145cf655043SAndy Whitcroft				$a = 'C' if ($elements[$n] =~ /$;$/);
51464a0df2efSAndy Whitcroft				$a = 'B' if ($elements[$n] =~ /(\[|\()$/);
51474a0df2efSAndy Whitcroft				$a = 'O' if ($elements[$n] eq '');
5148773647a0SAndy Whitcroft				$a = 'E' if ($ca =~ /^\s*$/);
51494a0df2efSAndy Whitcroft
51500a920b5bSAndy Whitcroft				my $op = $elements[$n + 1];
51514a0df2efSAndy Whitcroft
51524a0df2efSAndy Whitcroft				my $c = '';
51530a920b5bSAndy Whitcroft				if (defined $elements[$n + 2]) {
51544a0df2efSAndy Whitcroft					$c = 'V' if ($elements[$n + 2] ne '');
51554a0df2efSAndy Whitcroft					$c = 'W' if ($elements[$n + 2] =~ /^\s/);
5156cf655043SAndy Whitcroft					$c = 'C' if ($elements[$n + 2] =~ /^$;/);
51574a0df2efSAndy Whitcroft					$c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
51584a0df2efSAndy Whitcroft					$c = 'O' if ($elements[$n + 2] eq '');
51598b1b3378SAndy Whitcroft					$c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
51604a0df2efSAndy Whitcroft				} else {
51614a0df2efSAndy Whitcroft					$c = 'E';
51620a920b5bSAndy Whitcroft				}
51630a920b5bSAndy Whitcroft
51644a0df2efSAndy Whitcroft				my $ctx = "${a}x${c}";
51654a0df2efSAndy Whitcroft
51664a0df2efSAndy Whitcroft				my $at = "(ctx:$ctx)";
51674a0df2efSAndy Whitcroft
51686c72ffaaSAndy Whitcroft				my $ptr = substr($blank, 0, $off) . "^";
5169de7d4f0eSAndy Whitcroft				my $hereptr = "$hereline$ptr\n";
51700a920b5bSAndy Whitcroft
517174048ed8SAndy Whitcroft				# Pull out the value of this operator.
51726c72ffaaSAndy Whitcroft				my $op_type = substr($curr_values, $off + 1, 1);
51730a920b5bSAndy Whitcroft
51741f65f947SAndy Whitcroft				# Get the full operator variant.
51751f65f947SAndy Whitcroft				my $opv = $op . substr($curr_vars, $off, 1);
51761f65f947SAndy Whitcroft
517713214adfSAndy Whitcroft				# Ignore operators passed as parameters.
517813214adfSAndy Whitcroft				if ($op_type ne 'V' &&
5179d7fe8065SSam Bobroff				    $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
518013214adfSAndy Whitcroft
5181cf655043SAndy Whitcroft#				# Ignore comments
5182cf655043SAndy Whitcroft#				} elsif ($op =~ /^$;+$/) {
518313214adfSAndy Whitcroft
5184d8aaf121SAndy Whitcroft				# ; should have either the end of line or a space or \ after it
518513214adfSAndy Whitcroft				} elsif ($op eq ';') {
5186cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEBC]/ &&
5187cf655043SAndy Whitcroft					    $cc !~ /^\\/ && $cc !~ /^;/) {
51883705ce5bSJoe Perches						if (ERROR("SPACING",
51893705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
5190b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
51913705ce5bSJoe Perches							$line_fixed = 1;
51923705ce5bSJoe Perches						}
5193d8aaf121SAndy Whitcroft					}
5194d8aaf121SAndy Whitcroft
5195d8aaf121SAndy Whitcroft				# // is a comment
5196d8aaf121SAndy Whitcroft				} elsif ($op eq '//') {
51970a920b5bSAndy Whitcroft
5198b00e4814SJoe Perches				#   :   when part of a bitfield
5199b00e4814SJoe Perches				} elsif ($opv eq ':B') {
5200b00e4814SJoe Perches					# skip the bitfield test for now
5201b00e4814SJoe Perches
52021f65f947SAndy Whitcroft				# No spaces for:
52031f65f947SAndy Whitcroft				#   ->
5204b00e4814SJoe Perches				} elsif ($op eq '->') {
52054a0df2efSAndy Whitcroft					if ($ctx =~ /Wx.|.xW/) {
52063705ce5bSJoe Perches						if (ERROR("SPACING",
52073705ce5bSJoe Perches							  "spaces prohibited around that '$op' $at\n" . $hereptr)) {
5208b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
52093705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
52103705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
52113705ce5bSJoe Perches							}
5212b34c648bSJoe Perches							$line_fixed = 1;
52133705ce5bSJoe Perches						}
52140a920b5bSAndy Whitcroft					}
52150a920b5bSAndy Whitcroft
52162381097bSJoe Perches				# , must not have a space before and must have a space on the right.
52170a920b5bSAndy Whitcroft				} elsif ($op eq ',') {
52182381097bSJoe Perches					my $rtrim_before = 0;
52192381097bSJoe Perches					my $space_after = 0;
52202381097bSJoe Perches					if ($ctx =~ /Wx./) {
52212381097bSJoe Perches						if (ERROR("SPACING",
52222381097bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
52232381097bSJoe Perches							$line_fixed = 1;
52242381097bSJoe Perches							$rtrim_before = 1;
52252381097bSJoe Perches						}
52262381097bSJoe Perches					}
5227cf655043SAndy Whitcroft					if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
52283705ce5bSJoe Perches						if (ERROR("SPACING",
52293705ce5bSJoe Perches							  "space required after that '$op' $at\n" . $hereptr)) {
52303705ce5bSJoe Perches							$line_fixed = 1;
5231b34c648bSJoe Perches							$last_after = $n;
52322381097bSJoe Perches							$space_after = 1;
52332381097bSJoe Perches						}
52342381097bSJoe Perches					}
52352381097bSJoe Perches					if ($rtrim_before || $space_after) {
52362381097bSJoe Perches						if ($rtrim_before) {
52372381097bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
52382381097bSJoe Perches						} else {
52392381097bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
52402381097bSJoe Perches						}
52412381097bSJoe Perches						if ($space_after) {
52422381097bSJoe Perches							$good .= " ";
52433705ce5bSJoe Perches						}
52440a920b5bSAndy Whitcroft					}
52450a920b5bSAndy Whitcroft
52469c0ca6f9SAndy Whitcroft				# '*' as part of a type definition -- reported already.
524774048ed8SAndy Whitcroft				} elsif ($opv eq '*_') {
52489c0ca6f9SAndy Whitcroft					#warn "'*' is part of type\n";
52499c0ca6f9SAndy Whitcroft
52509c0ca6f9SAndy Whitcroft				# unary operators should have a space before and
52519c0ca6f9SAndy Whitcroft				# none after.  May be left adjacent to another
52529c0ca6f9SAndy Whitcroft				# unary operator, or a cast
52539c0ca6f9SAndy Whitcroft				} elsif ($op eq '!' || $op eq '~' ||
525474048ed8SAndy Whitcroft					 $opv eq '*U' || $opv eq '-U' ||
52550d413866SAndy Whitcroft					 $opv eq '&U' || $opv eq '&&U') {
5256cf655043SAndy Whitcroft					if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
52573705ce5bSJoe Perches						if (ERROR("SPACING",
52583705ce5bSJoe Perches							  "space required before that '$op' $at\n" . $hereptr)) {
5259b34c648bSJoe Perches							if ($n != $last_after + 2) {
5260b34c648bSJoe Perches								$good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
52613705ce5bSJoe Perches								$line_fixed = 1;
52623705ce5bSJoe Perches							}
52630a920b5bSAndy Whitcroft						}
5264b34c648bSJoe Perches					}
5265a3340b35SAndy Whitcroft					if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
5266171ae1a4SAndy Whitcroft						# A unary '*' may be const
5267171ae1a4SAndy Whitcroft
5268171ae1a4SAndy Whitcroft					} elsif ($ctx =~ /.xW/) {
52693705ce5bSJoe Perches						if (ERROR("SPACING",
52703705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
5271b34c648bSJoe Perches							$good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
52723705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
52733705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
52743705ce5bSJoe Perches							}
5275b34c648bSJoe Perches							$line_fixed = 1;
52763705ce5bSJoe Perches						}
52770a920b5bSAndy Whitcroft					}
52780a920b5bSAndy Whitcroft
52790a920b5bSAndy Whitcroft				# unary ++ and unary -- are allowed no space on one side.
52800a920b5bSAndy Whitcroft				} elsif ($op eq '++' or $op eq '--') {
5281773647a0SAndy Whitcroft					if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
52823705ce5bSJoe Perches						if (ERROR("SPACING",
52833705ce5bSJoe Perches							  "space required one side of that '$op' $at\n" . $hereptr)) {
5284b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
52853705ce5bSJoe Perches							$line_fixed = 1;
52863705ce5bSJoe Perches						}
52870a920b5bSAndy Whitcroft					}
5288773647a0SAndy Whitcroft					if ($ctx =~ /Wx[BE]/ ||
5289773647a0SAndy Whitcroft					    ($ctx =~ /Wx./ && $cc =~ /^;/)) {
52903705ce5bSJoe Perches						if (ERROR("SPACING",
52913705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5292b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
52933705ce5bSJoe Perches							$line_fixed = 1;
52943705ce5bSJoe Perches						}
5295653d4876SAndy Whitcroft					}
5296773647a0SAndy Whitcroft					if ($ctx =~ /ExW/) {
52973705ce5bSJoe Perches						if (ERROR("SPACING",
52983705ce5bSJoe Perches							  "space prohibited after that '$op' $at\n" . $hereptr)) {
5299b34c648bSJoe Perches							$good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
53003705ce5bSJoe Perches							if (defined $fix_elements[$n + 2]) {
53013705ce5bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5302773647a0SAndy Whitcroft							}
5303b34c648bSJoe Perches							$line_fixed = 1;
53043705ce5bSJoe Perches						}
53053705ce5bSJoe Perches					}
53060a920b5bSAndy Whitcroft
53070a920b5bSAndy Whitcroft				# << and >> may either have or not have spaces both sides
53089c0ca6f9SAndy Whitcroft				} elsif ($op eq '<<' or $op eq '>>' or
53099c0ca6f9SAndy Whitcroft					 $op eq '&' or $op eq '^' or $op eq '|' or
53109c0ca6f9SAndy Whitcroft					 $op eq '+' or $op eq '-' or
5311c2fdda0dSAndy Whitcroft					 $op eq '*' or $op eq '/' or
5312c2fdda0dSAndy Whitcroft					 $op eq '%')
53130a920b5bSAndy Whitcroft				{
5314d2e025f3SJoe Perches					if ($check) {
5315d2e025f3SJoe Perches						if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
5316d2e025f3SJoe Perches							if (CHK("SPACING",
5317d2e025f3SJoe Perches								"spaces preferred around that '$op' $at\n" . $hereptr)) {
5318d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5319d2e025f3SJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5320d2e025f3SJoe Perches								$line_fixed = 1;
5321d2e025f3SJoe Perches							}
5322d2e025f3SJoe Perches						} elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
5323d2e025f3SJoe Perches							if (CHK("SPACING",
5324d2e025f3SJoe Perches								"space preferred before that '$op' $at\n" . $hereptr)) {
5325d2e025f3SJoe Perches								$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
5326d2e025f3SJoe Perches								$line_fixed = 1;
5327d2e025f3SJoe Perches							}
5328d2e025f3SJoe Perches						}
5329d2e025f3SJoe Perches					} elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
53303705ce5bSJoe Perches						if (ERROR("SPACING",
53313705ce5bSJoe Perches							  "need consistent spacing around '$op' $at\n" . $hereptr)) {
5332b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5333b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5334b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5335b34c648bSJoe Perches							}
53363705ce5bSJoe Perches							$line_fixed = 1;
53373705ce5bSJoe Perches						}
53380a920b5bSAndy Whitcroft					}
53390a920b5bSAndy Whitcroft
53401f65f947SAndy Whitcroft				# A colon needs no spaces before when it is
53411f65f947SAndy Whitcroft				# terminating a case value or a label.
53421f65f947SAndy Whitcroft				} elsif ($opv eq ':C' || $opv eq ':L') {
5343263afd39SChris Down					if ($ctx =~ /Wx./ and $realfile !~ m@.*\.lds\.h$@) {
53443705ce5bSJoe Perches						if (ERROR("SPACING",
53453705ce5bSJoe Perches							  "space prohibited before that '$op' $at\n" . $hereptr)) {
5346b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
53473705ce5bSJoe Perches							$line_fixed = 1;
53483705ce5bSJoe Perches						}
53491f65f947SAndy Whitcroft					}
53501f65f947SAndy Whitcroft
53510a920b5bSAndy Whitcroft				# All the others need spaces both sides.
5352cf655043SAndy Whitcroft				} elsif ($ctx !~ /[EWC]x[CWE]/) {
53531f65f947SAndy Whitcroft					my $ok = 0;
53541f65f947SAndy Whitcroft
535522f2a2efSAndy Whitcroft					# Ignore email addresses <foo@bar>
53561f65f947SAndy Whitcroft					if (($op eq '<' &&
53571f65f947SAndy Whitcroft					     $cc =~ /^\S+\@\S+>/) ||
53581f65f947SAndy Whitcroft					    ($op eq '>' &&
53591f65f947SAndy Whitcroft					     $ca =~ /<\S+\@\S+$/))
53601f65f947SAndy Whitcroft					{
53611f65f947SAndy Whitcroft						$ok = 1;
53621f65f947SAndy Whitcroft					}
53631f65f947SAndy Whitcroft
5364e0df7e1fSJoe Perches					# for asm volatile statements
5365e0df7e1fSJoe Perches					# ignore a colon with another
5366e0df7e1fSJoe Perches					# colon immediately before or after
5367e0df7e1fSJoe Perches					if (($op eq ':') &&
5368e0df7e1fSJoe Perches					    ($ca =~ /:$/ || $cc =~ /^:/)) {
5369e0df7e1fSJoe Perches						$ok = 1;
5370e0df7e1fSJoe Perches					}
5371e0df7e1fSJoe Perches
537284731623SJoe Perches					# messages are ERROR, but ?: are CHK
53731f65f947SAndy Whitcroft					if ($ok == 0) {
53740675a8fbSJean Delvare						my $msg_level = \&ERROR;
53750675a8fbSJean Delvare						$msg_level = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
537684731623SJoe Perches
53770675a8fbSJean Delvare						if (&{$msg_level}("SPACING",
53783705ce5bSJoe Perches								  "spaces required around that '$op' $at\n" . $hereptr)) {
5379b34c648bSJoe Perches							$good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
5380b34c648bSJoe Perches							if (defined $fix_elements[$n + 2]) {
5381b34c648bSJoe Perches								$fix_elements[$n + 2] =~ s/^\s+//;
5382b34c648bSJoe Perches							}
53833705ce5bSJoe Perches							$line_fixed = 1;
53843705ce5bSJoe Perches						}
53850a920b5bSAndy Whitcroft					}
538622f2a2efSAndy Whitcroft				}
53874a0df2efSAndy Whitcroft				$off += length($elements[$n + 1]);
53883705ce5bSJoe Perches
53893705ce5bSJoe Perches##				print("n: <$n> GOOD: <$good>\n");
53903705ce5bSJoe Perches
53913705ce5bSJoe Perches				$fixed_line = $fixed_line . $good;
53920a920b5bSAndy Whitcroft			}
53933705ce5bSJoe Perches
53943705ce5bSJoe Perches			if (($#elements % 2) == 0) {
53953705ce5bSJoe Perches				$fixed_line = $fixed_line . $fix_elements[$#elements];
53963705ce5bSJoe Perches			}
53973705ce5bSJoe Perches
5398194f66fcSJoe Perches			if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
5399194f66fcSJoe Perches				$fixed[$fixlinenr] = $fixed_line;
54003705ce5bSJoe Perches			}
54013705ce5bSJoe Perches
54023705ce5bSJoe Perches
54030a920b5bSAndy Whitcroft		}
54040a920b5bSAndy Whitcroft
5405786b6326SJoe Perches# check for whitespace before a non-naked semicolon
5406d2e248e7SJoe Perches		if ($line =~ /^\+.*\S\s+;\s*$/) {
5407786b6326SJoe Perches			if (WARN("SPACING",
5408786b6326SJoe Perches				 "space prohibited before semicolon\n" . $herecurr) &&
5409786b6326SJoe Perches			    $fix) {
5410194f66fcSJoe Perches				1 while $fixed[$fixlinenr] =~
5411786b6326SJoe Perches				    s/^(\+.*\S)\s+;/$1;/;
5412786b6326SJoe Perches			}
5413786b6326SJoe Perches		}
5414786b6326SJoe Perches
5415f0a594c1SAndy Whitcroft# check for multiple assignments
5416f0a594c1SAndy Whitcroft		if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
5417000d1cc1SJoe Perches			CHK("MULTIPLE_ASSIGNMENTS",
5418000d1cc1SJoe Perches			    "multiple assignments should be avoided\n" . $herecurr);
5419f0a594c1SAndy Whitcroft		}
5420f0a594c1SAndy Whitcroft
542122f2a2efSAndy Whitcroft## # check for multiple declarations, allowing for a function declaration
542222f2a2efSAndy Whitcroft## # continuation.
542322f2a2efSAndy Whitcroft## 		if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
542422f2a2efSAndy Whitcroft## 		    $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
542522f2a2efSAndy Whitcroft##
542622f2a2efSAndy Whitcroft## 			# Remove any bracketed sections to ensure we do not
5427e73d2715SDwaipayan Ray## 			# falsely report the parameters of functions.
542822f2a2efSAndy Whitcroft## 			my $ln = $line;
542922f2a2efSAndy Whitcroft## 			while ($ln =~ s/\([^\(\)]*\)//g) {
543022f2a2efSAndy Whitcroft## 			}
543122f2a2efSAndy Whitcroft## 			if ($ln =~ /,/) {
5432000d1cc1SJoe Perches## 				WARN("MULTIPLE_DECLARATION",
5433000d1cc1SJoe Perches##				     "declaring multiple variables together should be avoided\n" . $herecurr);
543422f2a2efSAndy Whitcroft## 			}
543522f2a2efSAndy Whitcroft## 		}
5436f0a594c1SAndy Whitcroft
54370a920b5bSAndy Whitcroft#need space before brace following if, while, etc
54386b8c69e4SGeyslan G. Bem		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
54396ad724e2SMichal Zylowski		    $line =~ /\b(?:else|do)\{/) {
54403705ce5bSJoe Perches			if (ERROR("SPACING",
54413705ce5bSJoe Perches				  "space required before the open brace '{'\n" . $herecurr) &&
54423705ce5bSJoe Perches			    $fix) {
54436ad724e2SMichal Zylowski				$fixed[$fixlinenr] =~ s/^(\+.*(?:do|else|\)))\{/$1 {/;
54443705ce5bSJoe Perches			}
5445de7d4f0eSAndy Whitcroft		}
5446de7d4f0eSAndy Whitcroft
5447c4a62ef9SJoe Perches## # check for blank lines before declarations
5448c4a62ef9SJoe Perches##		if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
5449c4a62ef9SJoe Perches##		    $prevrawline =~ /^.\s*$/) {
5450c4a62ef9SJoe Perches##			WARN("SPACING",
5451c4a62ef9SJoe Perches##			     "No blank lines before declarations\n" . $hereprev);
5452c4a62ef9SJoe Perches##		}
5453c4a62ef9SJoe Perches##
5454c4a62ef9SJoe Perches
5455de7d4f0eSAndy Whitcroft# closing brace should have a space following it when it has anything
5456de7d4f0eSAndy Whitcroft# on the line
545794fb9845SJoe Perches		if ($line =~ /}(?!(?:,|;|\)|\}))\S/) {
5458d5e616fcSJoe Perches			if (ERROR("SPACING",
5459d5e616fcSJoe Perches				  "space required after that close brace '}'\n" . $herecurr) &&
5460d5e616fcSJoe Perches			    $fix) {
5461194f66fcSJoe Perches				$fixed[$fixlinenr] =~
5462d5e616fcSJoe Perches				    s/}((?!(?:,|;|\)))\S)/} $1/;
5463d5e616fcSJoe Perches			}
54640a920b5bSAndy Whitcroft		}
54650a920b5bSAndy Whitcroft
546622f2a2efSAndy Whitcroft# check spacing on square brackets
546722f2a2efSAndy Whitcroft		if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
54683705ce5bSJoe Perches			if (ERROR("SPACING",
54693705ce5bSJoe Perches				  "space prohibited after that open square bracket '['\n" . $herecurr) &&
54703705ce5bSJoe Perches			    $fix) {
5471194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54723705ce5bSJoe Perches				    s/\[\s+/\[/;
54733705ce5bSJoe Perches			}
547422f2a2efSAndy Whitcroft		}
547522f2a2efSAndy Whitcroft		if ($line =~ /\s\]/) {
54763705ce5bSJoe Perches			if (ERROR("SPACING",
54773705ce5bSJoe Perches				  "space prohibited before that close square bracket ']'\n" . $herecurr) &&
54783705ce5bSJoe Perches			    $fix) {
5479194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54803705ce5bSJoe Perches				    s/\s+\]/\]/;
54813705ce5bSJoe Perches			}
548222f2a2efSAndy Whitcroft		}
548322f2a2efSAndy Whitcroft
5484c45dcabdSAndy Whitcroft# check spacing on parentheses
54859c0ca6f9SAndy Whitcroft		if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
54869c0ca6f9SAndy Whitcroft		    $line !~ /for\s*\(\s+;/) {
54873705ce5bSJoe Perches			if (ERROR("SPACING",
54883705ce5bSJoe Perches				  "space prohibited after that open parenthesis '('\n" . $herecurr) &&
54893705ce5bSJoe Perches			    $fix) {
5490194f66fcSJoe Perches				$fixed[$fixlinenr] =~
54913705ce5bSJoe Perches				    s/\(\s+/\(/;
54923705ce5bSJoe Perches			}
549322f2a2efSAndy Whitcroft		}
549413214adfSAndy Whitcroft		if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
5495c45dcabdSAndy Whitcroft		    $line !~ /for\s*\(.*;\s+\)/ &&
5496c45dcabdSAndy Whitcroft		    $line !~ /:\s+\)/) {
54973705ce5bSJoe Perches			if (ERROR("SPACING",
54983705ce5bSJoe Perches				  "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
54993705ce5bSJoe Perches			    $fix) {
5500194f66fcSJoe Perches				$fixed[$fixlinenr] =~
55013705ce5bSJoe Perches				    s/\s+\)/\)/;
55023705ce5bSJoe Perches			}
550322f2a2efSAndy Whitcroft		}
550422f2a2efSAndy Whitcroft
5505e2826fd0SJoe Perches# check unnecessary parentheses around addressof/dereference single $Lvals
5506e2826fd0SJoe Perches# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
5507e2826fd0SJoe Perches
5508e2826fd0SJoe Perches		while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
5509ea4acbb1SJoe Perches			my $var = $1;
5510ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5511ea4acbb1SJoe Perches				"Unnecessary parentheses around $var\n" . $herecurr) &&
5512ea4acbb1SJoe Perches			    $fix) {
5513ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
5514ea4acbb1SJoe Perches			}
5515ea4acbb1SJoe Perches		}
5516ea4acbb1SJoe Perches
5517ea4acbb1SJoe Perches# check for unnecessary parentheses around function pointer uses
5518ea4acbb1SJoe Perches# ie: (foo->bar)(); should be foo->bar();
5519ea4acbb1SJoe Perches# but not "if (foo->bar) (" to avoid some false positives
5520ea4acbb1SJoe Perches		if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
5521ea4acbb1SJoe Perches			my $var = $2;
5522ea4acbb1SJoe Perches			if (CHK("UNNECESSARY_PARENTHESES",
5523ea4acbb1SJoe Perches				"Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
5524ea4acbb1SJoe Perches			    $fix) {
5525ea4acbb1SJoe Perches				my $var2 = deparenthesize($var);
5526ea4acbb1SJoe Perches				$var2 =~ s/\s//g;
5527ea4acbb1SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
5528ea4acbb1SJoe Perches			}
5529e2826fd0SJoe Perches		}
5530e2826fd0SJoe Perches
553163b7c73eSJoe Perches# check for unnecessary parentheses around comparisons in if uses
5532a032aa4cSJoe Perches# when !drivers/staging or command-line uses --strict
5533a032aa4cSJoe Perches		if (($realfile !~ m@^(?:drivers/staging/)@ || $check_orig) &&
55345b57980dSJoe Perches		    $perl_version_ok && defined($stat) &&
553563b7c73eSJoe Perches		    $stat =~ /(^.\s*if\s*($balanced_parens))/) {
553663b7c73eSJoe Perches			my $if_stat = $1;
553763b7c73eSJoe Perches			my $test = substr($2, 1, -1);
553863b7c73eSJoe Perches			my $herectx;
553963b7c73eSJoe Perches			while ($test =~ /(?:^|[^\w\&\!\~])+\s*\(\s*([\&\!\~]?\s*$Lval\s*(?:$Compare\s*$FuncArg)?)\s*\)/g) {
554063b7c73eSJoe Perches				my $match = $1;
554163b7c73eSJoe Perches				# avoid parentheses around potential macro args
554263b7c73eSJoe Perches				next if ($match =~ /^\s*\w+\s*$/);
554363b7c73eSJoe Perches				if (!defined($herectx)) {
554463b7c73eSJoe Perches					$herectx = $here . "\n";
554563b7c73eSJoe Perches					my $cnt = statement_rawlines($if_stat);
554663b7c73eSJoe Perches					for (my $n = 0; $n < $cnt; $n++) {
554763b7c73eSJoe Perches						my $rl = raw_line($linenr, $n);
554863b7c73eSJoe Perches						$herectx .=  $rl . "\n";
554963b7c73eSJoe Perches						last if $rl =~ /^[ \+].*\{/;
555063b7c73eSJoe Perches					}
555163b7c73eSJoe Perches				}
555263b7c73eSJoe Perches				CHK("UNNECESSARY_PARENTHESES",
555363b7c73eSJoe Perches				    "Unnecessary parentheses around '$match'\n" . $herectx);
555463b7c73eSJoe Perches			}
555563b7c73eSJoe Perches		}
555663b7c73eSJoe Perches
555769078651SJoe Perches# check that goto labels aren't indented (allow a single space indentation)
555869078651SJoe Perches# and ignore bitfield definitions like foo:1
555969078651SJoe Perches# Strictly, labels can have whitespace after the identifier and before the :
556069078651SJoe Perches# but this is not allowed here as many ?: uses would appear to be labels
556169078651SJoe Perches		if ($sline =~ /^.\s+[A-Za-z_][A-Za-z\d_]*:(?!\s*\d+)/ &&
556269078651SJoe Perches		    $sline !~ /^. [A-Za-z\d_][A-Za-z\d_]*:/ &&
556369078651SJoe Perches		    $sline !~ /^.\s+default:/) {
55643705ce5bSJoe Perches			if (WARN("INDENTED_LABEL",
55653705ce5bSJoe Perches				 "labels should not be indented\n" . $herecurr) &&
55663705ce5bSJoe Perches			    $fix) {
5567194f66fcSJoe Perches				$fixed[$fixlinenr] =~
55683705ce5bSJoe Perches				    s/^(.)\s+/$1/;
55693705ce5bSJoe Perches			}
55700a920b5bSAndy Whitcroft		}
55710a920b5bSAndy Whitcroft
557240873abaSJoe Perches# check if a statement with a comma should be two statements like:
557340873abaSJoe Perches#	foo = bar(),	/* comma should be semicolon */
557440873abaSJoe Perches#	bar = baz();
557540873abaSJoe Perches		if (defined($stat) &&
557640873abaSJoe Perches		    $stat =~ /^\+\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*,\s*(?:$Lval\s*$Assignment\s*)?$FuncArg\s*;\s*$/) {
557740873abaSJoe Perches			my $cnt = statement_rawlines($stat);
557840873abaSJoe Perches			my $herectx = get_stat_here($linenr, $cnt, $here);
557940873abaSJoe Perches			WARN("SUSPECT_COMMA_SEMICOLON",
558040873abaSJoe Perches			     "Possible comma where semicolon could be used\n" . $herectx);
558140873abaSJoe Perches		}
558240873abaSJoe Perches
55835b9553abSJoe Perches# return is not a function
5584507e5141SJoe Perches		if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
5585c45dcabdSAndy Whitcroft			my $spacing = $1;
55865b57980dSJoe Perches			if ($perl_version_ok &&
55875b9553abSJoe Perches			    $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
55885b9553abSJoe Perches				my $value = $1;
55895b9553abSJoe Perches				$value = deparenthesize($value);
55905b9553abSJoe Perches				if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
5591000d1cc1SJoe Perches					ERROR("RETURN_PARENTHESES",
5592000d1cc1SJoe Perches					      "return is not a function, parentheses are not required\n" . $herecurr);
55935b9553abSJoe Perches				}
5594c45dcabdSAndy Whitcroft			} elsif ($spacing !~ /\s+/) {
5595000d1cc1SJoe Perches				ERROR("SPACING",
5596000d1cc1SJoe Perches				      "space required before the open parenthesis '('\n" . $herecurr);
5597c45dcabdSAndy Whitcroft			}
5598c45dcabdSAndy Whitcroft		}
5599507e5141SJoe Perches
5600b43ae21bSJoe Perches# unnecessary return in a void function
5601b43ae21bSJoe Perches# at end-of-function, with the previous line a single leading tab, then return;
5602b43ae21bSJoe Perches# and the line before that not a goto label target like "out:"
5603b43ae21bSJoe Perches		if ($sline =~ /^[ \+]}\s*$/ &&
5604b43ae21bSJoe Perches		    $prevline =~ /^\+\treturn\s*;\s*$/ &&
5605b43ae21bSJoe Perches		    $linenr >= 3 &&
5606b43ae21bSJoe Perches		    $lines[$linenr - 3] =~ /^[ +]/ &&
5607b43ae21bSJoe Perches		    $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
56089819cf25SJoe Perches			WARN("RETURN_VOID",
5609b43ae21bSJoe Perches			     "void function return statements are not generally useful\n" . $hereprev);
56109819cf25SJoe Perches		}
56119819cf25SJoe Perches
5612189248d8SJoe Perches# if statements using unnecessary parentheses - ie: if ((foo == bar))
56135b57980dSJoe Perches		if ($perl_version_ok &&
5614189248d8SJoe Perches		    $line =~ /\bif\s*((?:\(\s*){2,})/) {
5615189248d8SJoe Perches			my $openparens = $1;
5616189248d8SJoe Perches			my $count = $openparens =~ tr@\(@\(@;
5617189248d8SJoe Perches			my $msg = "";
5618189248d8SJoe Perches			if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
5619189248d8SJoe Perches				my $comp = $4;	#Not $1 because of $LvalOrFunc
5620189248d8SJoe Perches				$msg = " - maybe == should be = ?" if ($comp eq "==");
5621189248d8SJoe Perches				WARN("UNNECESSARY_PARENTHESES",
5622189248d8SJoe Perches				     "Unnecessary parentheses$msg\n" . $herecurr);
5623189248d8SJoe Perches			}
5624189248d8SJoe Perches		}
5625189248d8SJoe Perches
5626c5595fa2SJoe Perches# comparisons with a constant or upper case identifier on the left
5627c5595fa2SJoe Perches#	avoid cases like "foo + BAR < baz"
5628c5595fa2SJoe Perches#	only fix matches surrounded by parentheses to avoid incorrect
5629c5595fa2SJoe Perches#	conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
56305b57980dSJoe Perches		if ($perl_version_ok &&
5631c5595fa2SJoe Perches		    $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
5632c5595fa2SJoe Perches			my $lead = $1;
5633c5595fa2SJoe Perches			my $const = $2;
5634c5595fa2SJoe Perches			my $comp = $3;
5635c5595fa2SJoe Perches			my $to = $4;
5636c5595fa2SJoe Perches			my $newcomp = $comp;
5637f39e1769SJoe Perches			if ($lead !~ /(?:$Operators|\.)\s*$/ &&
5638c5595fa2SJoe Perches			    $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
5639c5595fa2SJoe Perches			    WARN("CONSTANT_COMPARISON",
5640c5595fa2SJoe Perches				 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
5641c5595fa2SJoe Perches			    $fix) {
5642c5595fa2SJoe Perches				if ($comp eq "<") {
5643c5595fa2SJoe Perches					$newcomp = ">";
5644c5595fa2SJoe Perches				} elsif ($comp eq "<=") {
5645c5595fa2SJoe Perches					$newcomp = ">=";
5646c5595fa2SJoe Perches				} elsif ($comp eq ">") {
5647c5595fa2SJoe Perches					$newcomp = "<";
5648c5595fa2SJoe Perches				} elsif ($comp eq ">=") {
5649c5595fa2SJoe Perches					$newcomp = "<=";
5650c5595fa2SJoe Perches				}
5651c5595fa2SJoe Perches				$fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
5652c5595fa2SJoe Perches			}
5653c5595fa2SJoe Perches		}
5654c5595fa2SJoe Perches
5655f34e4a4fSJoe Perches# Return of what appears to be an errno should normally be negative
5656f34e4a4fSJoe Perches		if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
565753a3c448SAndy Whitcroft			my $name = $1;
565846b85bf9SGuenter Roeck			if ($name ne 'EOF' && $name ne 'ERROR' && $name !~ /^EPOLL/) {
5659000d1cc1SJoe Perches				WARN("USE_NEGATIVE_ERRNO",
5660f34e4a4fSJoe Perches				     "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
566153a3c448SAndy Whitcroft			}
566253a3c448SAndy Whitcroft		}
5663c45dcabdSAndy Whitcroft
56640a920b5bSAndy Whitcroft# Need a space before open parenthesis after if, while etc
56654a0df2efSAndy Whitcroft		if ($line =~ /\b(if|while|for|switch)\(/) {
56663705ce5bSJoe Perches			if (ERROR("SPACING",
56673705ce5bSJoe Perches				  "space required before the open parenthesis '('\n" . $herecurr) &&
56683705ce5bSJoe Perches			    $fix) {
5669194f66fcSJoe Perches				$fixed[$fixlinenr] =~
56703705ce5bSJoe Perches				    s/\b(if|while|for|switch)\(/$1 \(/;
56713705ce5bSJoe Perches			}
56720a920b5bSAndy Whitcroft		}
56730a920b5bSAndy Whitcroft
5674f5fe35ddSAndy Whitcroft# Check for illegal assignment in if conditional -- and check for trailing
5675f5fe35ddSAndy Whitcroft# statements after the conditional.
5676170d3a22SAndy Whitcroft		if ($line =~ /do\s*(?!{)/) {
56773e469cdcSAndy Whitcroft			($stat, $cond, $line_nr_next, $remain_next, $off_next) =
56783e469cdcSAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0)
56793e469cdcSAndy Whitcroft					if (!defined $stat);
5680170d3a22SAndy Whitcroft			my ($stat_next) = ctx_statement_block($line_nr_next,
5681170d3a22SAndy Whitcroft						$remain_next, $off_next);
5682170d3a22SAndy Whitcroft			$stat_next =~ s/\n./\n /g;
5683170d3a22SAndy Whitcroft			##print "stat<$stat> stat_next<$stat_next>\n";
5684170d3a22SAndy Whitcroft
5685170d3a22SAndy Whitcroft			if ($stat_next =~ /^\s*while\b/) {
5686170d3a22SAndy Whitcroft				# If the statement carries leading newlines,
5687170d3a22SAndy Whitcroft				# then count those as offsets.
5688170d3a22SAndy Whitcroft				my ($whitespace) =
5689170d3a22SAndy Whitcroft					($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
5690170d3a22SAndy Whitcroft				my $offset =
5691170d3a22SAndy Whitcroft					statement_rawlines($whitespace) - 1;
5692170d3a22SAndy Whitcroft
5693170d3a22SAndy Whitcroft				$suppress_whiletrailers{$line_nr_next +
5694170d3a22SAndy Whitcroft								$offset} = 1;
5695170d3a22SAndy Whitcroft			}
5696170d3a22SAndy Whitcroft		}
5697170d3a22SAndy Whitcroft		if (!defined $suppress_whiletrailers{$linenr} &&
5698c11230f4SJoe Perches		    defined($stat) && defined($cond) &&
5699170d3a22SAndy Whitcroft		    $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
5700171ae1a4SAndy Whitcroft			my ($s, $c) = ($stat, $cond);
5701481efd7bSJoe Perches			my $fixed_assign_in_if = 0;
57028905a67cSAndy Whitcroft
5703b53c8e10SAndy Whitcroft			if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
570465b64b3bSJoe Perches				if (ERROR("ASSIGN_IN_IF",
570565b64b3bSJoe Perches					  "do not use assignment in if condition\n" . $herecurr) &&
570665b64b3bSJoe Perches				    $fix && $perl_version_ok) {
570765b64b3bSJoe Perches					if ($rawline =~ /^\+(\s+)if\s*\(\s*(\!)?\s*\(\s*(($Lval)\s*=\s*$LvalOrFunc)\s*\)\s*(?:($Compare)\s*($FuncArg))?\s*\)\s*(\{)?\s*$/) {
570865b64b3bSJoe Perches						my $space = $1;
570965b64b3bSJoe Perches						my $not = $2;
571065b64b3bSJoe Perches						my $statement = $3;
571165b64b3bSJoe Perches						my $assigned = $4;
571265b64b3bSJoe Perches						my $test = $8;
571365b64b3bSJoe Perches						my $against = $9;
571465b64b3bSJoe Perches						my $brace = $15;
571565b64b3bSJoe Perches						fix_delete_line($fixlinenr, $rawline);
571665b64b3bSJoe Perches						fix_insert_line($fixlinenr, "$space$statement;");
571765b64b3bSJoe Perches						my $newline = "${space}if (";
571865b64b3bSJoe Perches						$newline .= '!' if defined($not);
571965b64b3bSJoe Perches						$newline .= '(' if (defined $not && defined($test) && defined($against));
572065b64b3bSJoe Perches						$newline .= "$assigned";
572165b64b3bSJoe Perches						$newline .= " $test $against" if (defined($test) && defined($against));
572265b64b3bSJoe Perches						$newline .= ')' if (defined $not && defined($test) && defined($against));
572365b64b3bSJoe Perches						$newline .= ')';
572465b64b3bSJoe Perches						$newline .= " {" if (defined($brace));
572565b64b3bSJoe Perches						fix_insert_line($fixlinenr + 1, $newline);
5726481efd7bSJoe Perches						$fixed_assign_in_if = 1;
572765b64b3bSJoe Perches					}
572865b64b3bSJoe Perches				}
57298905a67cSAndy Whitcroft			}
57308905a67cSAndy Whitcroft
57318905a67cSAndy Whitcroft			# Find out what is on the end of the line after the
57328905a67cSAndy Whitcroft			# conditional.
5733773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
57348905a67cSAndy Whitcroft			$s =~ s/\n.*//g;
573513214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
573653210168SAndy Whitcroft			if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
573753210168SAndy Whitcroft			    $c !~ /}\s*while\s*/)
5738773647a0SAndy Whitcroft			{
5739bb44ad39SAndy Whitcroft				# Find out how long the conditional actually is.
5740bb44ad39SAndy Whitcroft				my @newlines = ($c =~ /\n/gs);
5741bb44ad39SAndy Whitcroft				my $cond_lines = 1 + $#newlines;
574242bdf74cSHidetoshi Seto				my $stat_real = '';
5743bb44ad39SAndy Whitcroft
574442bdf74cSHidetoshi Seto				$stat_real = raw_line($linenr, $cond_lines)
574542bdf74cSHidetoshi Seto							. "\n" if ($cond_lines);
5746bb44ad39SAndy Whitcroft				if (defined($stat_real) && $cond_lines > 1) {
5747bb44ad39SAndy Whitcroft					$stat_real = "[...]\n$stat_real";
5748bb44ad39SAndy Whitcroft				}
5749bb44ad39SAndy Whitcroft
5750481efd7bSJoe Perches				if (ERROR("TRAILING_STATEMENTS",
5751481efd7bSJoe Perches					  "trailing statements should be on next line\n" . $herecurr . $stat_real) &&
5752481efd7bSJoe Perches				    !$fixed_assign_in_if &&
5753481efd7bSJoe Perches				    $cond_lines == 0 &&
5754481efd7bSJoe Perches				    $fix && $perl_version_ok &&
5755481efd7bSJoe Perches				    $fixed[$fixlinenr] =~ /^\+(\s*)((?:if|while|for)\s*$balanced_parens)\s*(.*)$/) {
5756481efd7bSJoe Perches					my $indent = $1;
5757481efd7bSJoe Perches					my $test = $2;
5758481efd7bSJoe Perches					my $rest = rtrim($4);
5759481efd7bSJoe Perches					if ($rest =~ /;$/) {
5760481efd7bSJoe Perches						$fixed[$fixlinenr] = "\+$indent$test";
5761481efd7bSJoe Perches						fix_insert_line($fixlinenr + 1, "$indent\t$rest");
5762481efd7bSJoe Perches					}
5763481efd7bSJoe Perches				}
57648905a67cSAndy Whitcroft			}
57658905a67cSAndy Whitcroft		}
57668905a67cSAndy Whitcroft
576713214adfSAndy Whitcroft# Check for bitwise tests written as boolean
576813214adfSAndy Whitcroft		if ($line =~ /
576913214adfSAndy Whitcroft			(?:
577013214adfSAndy Whitcroft				(?:\[|\(|\&\&|\|\|)
577113214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
577213214adfSAndy Whitcroft				(?:\&\&|\|\|)
577313214adfSAndy Whitcroft			|
577413214adfSAndy Whitcroft				(?:\&\&|\|\|)
577513214adfSAndy Whitcroft				\s*0[xX][0-9]+\s*
577613214adfSAndy Whitcroft				(?:\&\&|\|\||\)|\])
577713214adfSAndy Whitcroft			)/x)
577813214adfSAndy Whitcroft		{
5779000d1cc1SJoe Perches			WARN("HEXADECIMAL_BOOLEAN_TEST",
5780000d1cc1SJoe Perches			     "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
578113214adfSAndy Whitcroft		}
578213214adfSAndy Whitcroft
57838905a67cSAndy Whitcroft# if and else should not have general statements after it
578413214adfSAndy Whitcroft		if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
578513214adfSAndy Whitcroft			my $s = $1;
578613214adfSAndy Whitcroft			$s =~ s/$;//g;	# Remove any comments
578713214adfSAndy Whitcroft			if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
5788000d1cc1SJoe Perches				ERROR("TRAILING_STATEMENTS",
5789000d1cc1SJoe Perches				      "trailing statements should be on next line\n" . $herecurr);
57900a920b5bSAndy Whitcroft			}
579113214adfSAndy Whitcroft		}
579239667782SAndy Whitcroft# if should not continue a brace
579339667782SAndy Whitcroft		if ($line =~ /}\s*if\b/) {
5794000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5795048b123fSRasmus Villemoes			      "trailing statements should be on next line (or did you mean 'else if'?)\n" .
579639667782SAndy Whitcroft				$herecurr);
579739667782SAndy Whitcroft		}
5798a1080bf8SAndy Whitcroft# case and default should not have general statements after them
5799a1080bf8SAndy Whitcroft		if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
5800a1080bf8SAndy Whitcroft		    $line !~ /\G(?:
58013fef12d6SAndy Whitcroft			(?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
5802a1080bf8SAndy Whitcroft			\s*return\s+
5803a1080bf8SAndy Whitcroft		    )/xg)
5804a1080bf8SAndy Whitcroft		{
5805000d1cc1SJoe Perches			ERROR("TRAILING_STATEMENTS",
5806000d1cc1SJoe Perches			      "trailing statements should be on next line\n" . $herecurr);
5807a1080bf8SAndy Whitcroft		}
58080a920b5bSAndy Whitcroft
58090a920b5bSAndy Whitcroft		# Check for }<nl>else {, these must be at the same
58100a920b5bSAndy Whitcroft		# indent level to be relevant to each other.
58118b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
58120a920b5bSAndy Whitcroft		    $previndent == $indent) {
58138b8856f4SJoe Perches			if (ERROR("ELSE_AFTER_BRACE",
58148b8856f4SJoe Perches				  "else should follow close brace '}'\n" . $hereprev) &&
58158b8856f4SJoe Perches			    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
58168b8856f4SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
58178b8856f4SJoe Perches				fix_delete_line($fixlinenr, $rawline);
58188b8856f4SJoe Perches				my $fixedline = $prevrawline;
58198b8856f4SJoe Perches				$fixedline =~ s/}\s*$//;
58208b8856f4SJoe Perches				if ($fixedline !~ /^\+\s*$/) {
58218b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
58228b8856f4SJoe Perches				}
58238b8856f4SJoe Perches				$fixedline = $rawline;
58248b8856f4SJoe Perches				$fixedline =~ s/^(.\s*)else/$1} else/;
58258b8856f4SJoe Perches				fix_insert_line($fixlinenr, $fixedline);
58268b8856f4SJoe Perches			}
58270a920b5bSAndy Whitcroft		}
58280a920b5bSAndy Whitcroft
58298b8856f4SJoe Perches		if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
5830c2fdda0dSAndy Whitcroft		    $previndent == $indent) {
5831c2fdda0dSAndy Whitcroft			my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
5832c2fdda0dSAndy Whitcroft
5833c2fdda0dSAndy Whitcroft			# Find out what is on the end of the line after the
5834c2fdda0dSAndy Whitcroft			# conditional.
5835773647a0SAndy Whitcroft			substr($s, 0, length($c), '');
5836c2fdda0dSAndy Whitcroft			$s =~ s/\n.*//g;
5837c2fdda0dSAndy Whitcroft
5838c2fdda0dSAndy Whitcroft			if ($s =~ /^\s*;/) {
58398b8856f4SJoe Perches				if (ERROR("WHILE_AFTER_BRACE",
58408b8856f4SJoe Perches					  "while should follow close brace '}'\n" . $hereprev) &&
58418b8856f4SJoe Perches				    $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
58428b8856f4SJoe Perches					fix_delete_line($fixlinenr - 1, $prevrawline);
58438b8856f4SJoe Perches					fix_delete_line($fixlinenr, $rawline);
58448b8856f4SJoe Perches					my $fixedline = $prevrawline;
58458b8856f4SJoe Perches					my $trailing = $rawline;
58468b8856f4SJoe Perches					$trailing =~ s/^\+//;
58478b8856f4SJoe Perches					$trailing = trim($trailing);
58488b8856f4SJoe Perches					$fixedline =~ s/}\s*$/} $trailing/;
58498b8856f4SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
58508b8856f4SJoe Perches				}
5851c2fdda0dSAndy Whitcroft			}
5852c2fdda0dSAndy Whitcroft		}
5853c2fdda0dSAndy Whitcroft
585495e2c602SJoe Perches#Specific variable tests
5855323c1260SJoe Perches		while ($line =~ m{($Constant|$Lval)}g) {
5856323c1260SJoe Perches			my $var = $1;
585795e2c602SJoe Perches
585895e2c602SJoe Perches#CamelCase
5859807bd26cSJoe Perches			if ($var !~ /^$Constant$/ &&
5860be79794bSJoe Perches			    $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
58614104a206SŁukasz Stelmach#Ignore some autogenerated defines and enum values
58624104a206SŁukasz Stelmach			    $var !~ /^(?:[A-Z]+_){1,5}[A-Z]{1,3}[a-z]/ &&
586322735ce8SJoe Perches#Ignore Page<foo> variants
5864807bd26cSJoe Perches			    $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
5865d99a4158SGerhard Engleder#Ignore ETHTOOL_LINK_MODE_<foo> variants
5866d99a4158SGerhard Engleder			    $var !~ /^ETHTOOL_LINK_MODE_/ &&
5867d439e6a5SJoe Perches#Ignore SI style variants like nS, mV and dB
5868d439e6a5SJoe Perches#(ie: max_uV, regulator_min_uA_show, RANGE_mA_VALUE)
5869d439e6a5SJoe Perches			    $var !~ /^(?:[a-z0-9_]*|[A-Z0-9_]*)?_?[a-z][A-Z](?:_[a-z0-9_]+|_[A-Z0-9_]+)?$/ &&
5870f5123576SJulius Werner#Ignore some three character SI units explicitly, like MiB and KHz
5871f5123576SJulius Werner			    $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
5872f858e23aSAntonio Borneo				while ($var =~ m{\b($Ident)}g) {
58737e781f67SJoe Perches					my $word = $1;
58747e781f67SJoe Perches					next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
5875d8b07710SJoe Perches					if ($check) {
5876d8b07710SJoe Perches						seed_camelcase_includes();
5877d8b07710SJoe Perches						if (!$file && !$camelcase_file_seeded) {
5878d8b07710SJoe Perches							seed_camelcase_file($realfile);
5879d8b07710SJoe Perches							$camelcase_file_seeded = 1;
5880d8b07710SJoe Perches						}
5881d8b07710SJoe Perches					}
58827e781f67SJoe Perches					if (!defined $camelcase{$word}) {
58837e781f67SJoe Perches						$camelcase{$word} = 1;
5884be79794bSJoe Perches						CHK("CAMELCASE",
58857e781f67SJoe Perches						    "Avoid CamelCase: <$word>\n" . $herecurr);
58867e781f67SJoe Perches					}
5887323c1260SJoe Perches				}
5888323c1260SJoe Perches			}
58893445686aSJoe Perches		}
58900a920b5bSAndy Whitcroft
58910a920b5bSAndy Whitcroft#no spaces allowed after \ in define
5892d5e616fcSJoe Perches		if ($line =~ /\#\s*define.*\\\s+$/) {
5893d5e616fcSJoe Perches			if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
5894d5e616fcSJoe Perches				 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
5895d5e616fcSJoe Perches			    $fix) {
5896194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\s+$//;
5897d5e616fcSJoe Perches			}
58980a920b5bSAndy Whitcroft		}
58990a920b5bSAndy Whitcroft
59000e212e0aSFabian Frederick# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
59010e212e0aSFabian Frederick# itself <asm/foo.h> (uses RAW line)
5902c45dcabdSAndy Whitcroft		if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
5903e09dec48SAndy Whitcroft			my $file = "$1.h";
5904e09dec48SAndy Whitcroft			my $checkfile = "include/linux/$file";
5905e09dec48SAndy Whitcroft			if (-f "$root/$checkfile" &&
5906e09dec48SAndy Whitcroft			    $realfile ne $checkfile &&
59077840a94cSWolfram Sang			    $1 !~ /$allowed_asm_includes/)
5908c45dcabdSAndy Whitcroft			{
59090e212e0aSFabian Frederick				my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
59100e212e0aSFabian Frederick				if ($asminclude > 0) {
5911e09dec48SAndy Whitcroft					if ($realfile =~ m{^arch/}) {
5912000d1cc1SJoe Perches						CHK("ARCH_INCLUDE_LINUX",
5913000d1cc1SJoe Perches						    "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5914e09dec48SAndy Whitcroft					} else {
5915000d1cc1SJoe Perches						WARN("INCLUDE_LINUX",
5916000d1cc1SJoe Perches						     "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
5917e09dec48SAndy Whitcroft					}
59180a920b5bSAndy Whitcroft				}
59190a920b5bSAndy Whitcroft			}
59200e212e0aSFabian Frederick		}
59210a920b5bSAndy Whitcroft
5922653d4876SAndy Whitcroft# multi-statement macros should be enclosed in a do while loop, grab the
5923653d4876SAndy Whitcroft# first statement and ensure its the whole macro if its not enclosed
5924cf655043SAndy Whitcroft# in a known good container
5925b8f96a31SAndy Whitcroft		if ($realfile !~ m@/vmlinux.lds.h$@ &&
5926b8f96a31SAndy Whitcroft		    $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
5927d8aaf121SAndy Whitcroft			my $ln = $linenr;
5928d8aaf121SAndy Whitcroft			my $cnt = $realcnt;
5929c45dcabdSAndy Whitcroft			my ($off, $dstat, $dcond, $rest);
5930c45dcabdSAndy Whitcroft			my $ctx = '';
593108a2843eSJoe Perches			my $has_flow_statement = 0;
593208a2843eSJoe Perches			my $has_arg_concat = 0;
5933c45dcabdSAndy Whitcroft			($dstat, $dcond, $ln, $cnt, $off) =
5934f74bd194SAndy Whitcroft				ctx_statement_block($linenr, $realcnt, 0);
5935f74bd194SAndy Whitcroft			$ctx = $dstat;
5936c45dcabdSAndy Whitcroft			#print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
5937a3bb97a7SAndy Whitcroft			#print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
5938c45dcabdSAndy Whitcroft
593908a2843eSJoe Perches			$has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
594062e15a6dSJoe Perches			$has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
594108a2843eSJoe Perches
5942f59b64bfSJoe Perches			$dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
5943f59b64bfSJoe Perches			my $define_args = $1;
5944f59b64bfSJoe Perches			my $define_stmt = $dstat;
5945f59b64bfSJoe Perches			my @def_args = ();
5946f59b64bfSJoe Perches
5947f59b64bfSJoe Perches			if (defined $define_args && $define_args ne "") {
5948f59b64bfSJoe Perches				$define_args = substr($define_args, 1, length($define_args) - 2);
5949f59b64bfSJoe Perches				$define_args =~ s/\s*//g;
59508c8c45cfSJoe Perches				$define_args =~ s/\\\+?//g;
5951f59b64bfSJoe Perches				@def_args = split(",", $define_args);
5952f59b64bfSJoe Perches			}
5953f59b64bfSJoe Perches
5954292f1a9bSAndy Whitcroft			$dstat =~ s/$;//g;
5955c45dcabdSAndy Whitcroft			$dstat =~ s/\\\n.//g;
5956c45dcabdSAndy Whitcroft			$dstat =~ s/^\s*//s;
5957c45dcabdSAndy Whitcroft			$dstat =~ s/\s*$//s;
5958c45dcabdSAndy Whitcroft
5959c45dcabdSAndy Whitcroft			# Flatten any parentheses and braces
59602e44e803SDwaipayan Ray			while ($dstat =~ s/\([^\(\)]*\)/1u/ ||
59612e44e803SDwaipayan Ray			       $dstat =~ s/\{[^\{\}]*\}/1u/ ||
59622e44e803SDwaipayan Ray			       $dstat =~ s/.\[[^\[\]]*\]/1u/)
5963bf30d6edSAndy Whitcroft			{
5964c45dcabdSAndy Whitcroft			}
5965c45dcabdSAndy Whitcroft
5966342d3d2fSAntonio Borneo			# Flatten any obvious string concatenation.
596733acb54aSJoe Perches			while ($dstat =~ s/($String)\s*$Ident/$1/ ||
596833acb54aSJoe Perches			       $dstat =~ s/$Ident\s*($String)/$1/)
5969e45bab8eSAndy Whitcroft			{
5970e45bab8eSAndy Whitcroft			}
5971e45bab8eSAndy Whitcroft
597242e15293SJoe Perches			# Make asm volatile uses seem like a generic function
597342e15293SJoe Perches			$dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
597442e15293SJoe Perches
5975c45dcabdSAndy Whitcroft			my $exceptions = qr{
5976c45dcabdSAndy Whitcroft				$Declare|
5977c45dcabdSAndy Whitcroft				module_param_named|
5978a0a0a7a9SKees Cook				MODULE_PARM_DESC|
5979c45dcabdSAndy Whitcroft				DECLARE_PER_CPU|
5980c45dcabdSAndy Whitcroft				DEFINE_PER_CPU|
5981383099fdSAndy Whitcroft				__typeof__\(|
598222fd2d3eSStefani Seibold				union|
598322fd2d3eSStefani Seibold				struct|
5984ea71a0a0SAndy Whitcroft				\.$Ident\s*=\s*|
59856b10df42SVladimir Zapolskiy				^\"|\"$|
59866b10df42SVladimir Zapolskiy				^\[
5987c45dcabdSAndy Whitcroft			}x;
59885eaa20b9SAndy Whitcroft			#print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
5989f59b64bfSJoe Perches
5990f59b64bfSJoe Perches			$ctx =~ s/\n*$//;
5991f59b64bfSJoe Perches			my $stmt_cnt = statement_rawlines($ctx);
5992e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $stmt_cnt, $here);
5993f59b64bfSJoe Perches
5994f74bd194SAndy Whitcroft			if ($dstat ne '' &&
5995f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant),$/ &&			# 10, // foo(),
5996f74bd194SAndy Whitcroft			    $dstat !~ /^(?:$Ident|-?$Constant);$/ &&			# foo();
59973cc4b1c3SJoe Perches			    $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ &&		# 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
5998356fd398SJoe Perches			    $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ &&			# character constants
5999f74bd194SAndy Whitcroft			    $dstat !~ /$exceptions/ &&
6000f74bd194SAndy Whitcroft			    $dstat !~ /^\.$Ident\s*=/ &&				# .foo =
6001e942e2c3SJoe Perches			    $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ &&		# stringification #foo
6002dc3f4deeSStanislaw Gruszka			    $dstat !~ /^case\b/ &&					# case ...
600372f115f9SAndy Whitcroft			    $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ &&	# do {...} while (...); // do {...} while (...)
60042e44e803SDwaipayan Ray			    $dstat !~ /^while\s*$Constant\s*$Constant\s*$/ &&		# while (...) {...}
6005f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
6006f74bd194SAndy Whitcroft			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
6007f74bd194SAndy Whitcroft			    $dstat !~ /^do\s*{/ &&					# do {...
60084e5d56bdSEddie Kovsky			    $dstat !~ /^\(\{/ &&						# ({...
6009f95a7e6aSJoe Perches			    $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
6010c45dcabdSAndy Whitcroft			{
6011e795556aSJoe Perches				if ($dstat =~ /^\s*if\b/) {
6012e795556aSJoe Perches					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
6013e795556aSJoe Perches					      "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
6014e795556aSJoe Perches				} elsif ($dstat =~ /;/) {
6015f74bd194SAndy Whitcroft					ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
6016f74bd194SAndy Whitcroft					      "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
6017f74bd194SAndy Whitcroft				} else {
6018000d1cc1SJoe Perches					ERROR("COMPLEX_MACRO",
6019388982b5SAndrew Morton					      "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
6020d8aaf121SAndy Whitcroft				}
6021f59b64bfSJoe Perches
6022f59b64bfSJoe Perches			}
60235207649bSJoe Perches
60245207649bSJoe Perches			# Make $define_stmt single line, comment-free, etc
60255207649bSJoe Perches			my @stmt_array = split('\n', $define_stmt);
60265207649bSJoe Perches			my $first = 1;
60275207649bSJoe Perches			$define_stmt = "";
60285207649bSJoe Perches			foreach my $l (@stmt_array) {
60295207649bSJoe Perches				$l =~ s/\\$//;
60305207649bSJoe Perches				if ($first) {
60315207649bSJoe Perches					$define_stmt = $l;
60325207649bSJoe Perches					$first = 0;
60335207649bSJoe Perches				} elsif ($l =~ /^[\+ ]/) {
60345207649bSJoe Perches					$define_stmt .= substr($l, 1);
60355207649bSJoe Perches				}
60365207649bSJoe Perches			}
60375207649bSJoe Perches			$define_stmt =~ s/$;//g;
60385207649bSJoe Perches			$define_stmt =~ s/\s+/ /g;
60395207649bSJoe Perches			$define_stmt = trim($define_stmt);
60405207649bSJoe Perches
6041f59b64bfSJoe Perches# check if any macro arguments are reused (ignore '...' and 'type')
6042f59b64bfSJoe Perches			foreach my $arg (@def_args) {
6043f59b64bfSJoe Perches			        next if ($arg =~ /\.\.\./);
60449192d41aSJoe Perches			        next if ($arg =~ /^type$/i);
60457fe528a2SJoe Perches				my $tmp_stmt = $define_stmt;
60467b844345SVincent Mailhol				$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;
60477fe528a2SJoe Perches				$tmp_stmt =~ s/\#+\s*$arg\b//g;
60487fe528a2SJoe Perches				$tmp_stmt =~ s/\b$arg\s*\#\#//g;
6049d41362edSJoe Perches				my $use_cnt = () = $tmp_stmt =~ /\b$arg\b/g;
6050f59b64bfSJoe Perches				if ($use_cnt > 1) {
6051f59b64bfSJoe Perches					CHK("MACRO_ARG_REUSE",
6052f59b64bfSJoe Perches					    "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
6053f59b64bfSJoe Perches				    }
60549192d41aSJoe Perches# check if any macro arguments may have other precedence issues
60557fe528a2SJoe Perches				if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
60569192d41aSJoe Perches				    ((defined($1) && $1 ne ',') ||
60579192d41aSJoe Perches				     (defined($2) && $2 ne ','))) {
60589192d41aSJoe Perches					CHK("MACRO_ARG_PRECEDENCE",
60599192d41aSJoe Perches					    "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
60609192d41aSJoe Perches				}
6061b1be5844SXining Xu
6062b1be5844SXining Xu# check if this is an unused argument
6063b1be5844SXining Xu				if ($define_stmt !~ /\b$arg\b/) {
6064b1be5844SXining Xu					WARN("MACRO_ARG_UNUSED",
6065b1be5844SXining Xu					     "Argument '$arg' is not used in function-like macro\n" . "$herectx");
6066b1be5844SXining Xu				}
60670a920b5bSAndy Whitcroft			}
60685023d347SJoe Perches
606908a2843eSJoe Perches# check for macros with flow control, but without ## concatenation
607008a2843eSJoe Perches# ## concatenation is commonly a macro that defines a function so ignore those
607108a2843eSJoe Perches			if ($has_flow_statement && !$has_arg_concat) {
607208a2843eSJoe Perches				my $cnt = statement_rawlines($ctx);
6073e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
607408a2843eSJoe Perches
607508a2843eSJoe Perches				WARN("MACRO_WITH_FLOW_CONTROL",
607608a2843eSJoe Perches				     "Macros with flow control statements should be avoided\n" . "$herectx");
607708a2843eSJoe Perches			}
607808a2843eSJoe Perches
6079481eb486SJoe Perches# check for line continuations outside of #defines, preprocessor #, and asm
60805023d347SJoe Perches
60815b2c7334SJim Cromie		} elsif ($realfile =~ m@/vmlinux.lds.h$@) {
60825b2c7334SJim Cromie		    $line =~ s/(\w+)/$maybe_linker_symbol{$1}++/ge;
60835b2c7334SJim Cromie		    #print "REAL: $realfile\nln: $line\nkeys:", sort keys %maybe_linker_symbol;
60845023d347SJoe Perches		} else {
60855023d347SJoe Perches			if ($prevline !~ /^..*\\$/ &&
6086481eb486SJoe Perches			    $line !~ /^\+\s*\#.*\\$/ &&		# preprocessor
6087481eb486SJoe Perches			    $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ &&	# asm
60885023d347SJoe Perches			    $line =~ /^\+.*\\$/) {
60895023d347SJoe Perches				WARN("LINE_CONTINUATIONS",
60905023d347SJoe Perches				     "Avoid unnecessary line continuations\n" . $herecurr);
60915023d347SJoe Perches			}
6092653d4876SAndy Whitcroft		}
60930a920b5bSAndy Whitcroft
6094b13edf7fSJoe Perches# do {} while (0) macro tests:
6095b13edf7fSJoe Perches# single-statement macros do not need to be enclosed in do while (0) loop,
6096b13edf7fSJoe Perches# macro should not end with a semicolon
60975b57980dSJoe Perches		if ($perl_version_ok &&
6098b13edf7fSJoe Perches		    $realfile !~ m@/vmlinux.lds.h$@ &&
6099b13edf7fSJoe Perches		    $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
6100b13edf7fSJoe Perches			my $ln = $linenr;
6101b13edf7fSJoe Perches			my $cnt = $realcnt;
6102b13edf7fSJoe Perches			my ($off, $dstat, $dcond, $rest);
6103b13edf7fSJoe Perches			my $ctx = '';
6104b13edf7fSJoe Perches			($dstat, $dcond, $ln, $cnt, $off) =
6105b13edf7fSJoe Perches				ctx_statement_block($linenr, $realcnt, 0);
6106b13edf7fSJoe Perches			$ctx = $dstat;
6107b13edf7fSJoe Perches
6108b13edf7fSJoe Perches			$dstat =~ s/\\\n.//g;
61091b36b201SJoe Perches			$dstat =~ s/$;/ /g;
6110b13edf7fSJoe Perches
6111b13edf7fSJoe Perches			if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
6112b13edf7fSJoe Perches				my $stmts = $2;
6113b13edf7fSJoe Perches				my $semis = $3;
6114b13edf7fSJoe Perches
6115b13edf7fSJoe Perches				$ctx =~ s/\n*$//;
6116b13edf7fSJoe Perches				my $cnt = statement_rawlines($ctx);
6117e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6118b13edf7fSJoe Perches
6119ac8e97f8SJoe Perches				if (($stmts =~ tr/;/;/) == 1 &&
6120ac8e97f8SJoe Perches				    $stmts !~ /^\s*(if|while|for|switch)\b/) {
6121b13edf7fSJoe Perches					WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
6122b13edf7fSJoe Perches					     "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
6123b13edf7fSJoe Perches				}
6124b13edf7fSJoe Perches				if (defined $semis && $semis ne "") {
6125b13edf7fSJoe Perches					WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
6126b13edf7fSJoe Perches					     "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
6127b13edf7fSJoe Perches				}
6128f5ef95b1SJoe Perches			} elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
6129f5ef95b1SJoe Perches				$ctx =~ s/\n*$//;
6130f5ef95b1SJoe Perches				my $cnt = statement_rawlines($ctx);
6131e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6132f5ef95b1SJoe Perches
6133f5ef95b1SJoe Perches				WARN("TRAILING_SEMICOLON",
6134f5ef95b1SJoe Perches				     "macros should not use a trailing semicolon\n" . "$herectx");
6135b13edf7fSJoe Perches			}
6136b13edf7fSJoe Perches		}
6137b13edf7fSJoe Perches
6138f0a594c1SAndy Whitcroft# check for redundant bracing round if etc
613913214adfSAndy Whitcroft		if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
614013214adfSAndy Whitcroft			my ($level, $endln, @chunks) =
6141cf655043SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, 1);
614213214adfSAndy Whitcroft			#print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
6143cf655043SAndy Whitcroft			#print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
6144cf655043SAndy Whitcroft			if ($#chunks > 0 && $level == 0) {
6145aad4f614SJoe Perches				my @allowed = ();
6146aad4f614SJoe Perches				my $allow = 0;
614713214adfSAndy Whitcroft				my $seen = 0;
6148773647a0SAndy Whitcroft				my $herectx = $here . "\n";
6149cf655043SAndy Whitcroft				my $ln = $linenr - 1;
615013214adfSAndy Whitcroft				for my $chunk (@chunks) {
615113214adfSAndy Whitcroft					my ($cond, $block) = @{$chunk};
615213214adfSAndy Whitcroft
6153773647a0SAndy Whitcroft					# If the condition carries leading newlines, then count those as offsets.
6154773647a0SAndy Whitcroft					my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
6155773647a0SAndy Whitcroft					my $offset = statement_rawlines($whitespace) - 1;
6156773647a0SAndy Whitcroft
6157aad4f614SJoe Perches					$allowed[$allow] = 0;
6158773647a0SAndy Whitcroft					#print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
6159773647a0SAndy Whitcroft
6160773647a0SAndy Whitcroft					# We have looked at and allowed this specific line.
6161773647a0SAndy Whitcroft					$suppress_ifbraces{$ln + $offset} = 1;
6162773647a0SAndy Whitcroft
6163773647a0SAndy Whitcroft					$herectx .= "$rawlines[$ln + $offset]\n[...]\n";
6164cf655043SAndy Whitcroft					$ln += statement_rawlines($block) - 1;
6165cf655043SAndy Whitcroft
6166773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
616713214adfSAndy Whitcroft
616813214adfSAndy Whitcroft					$seen++ if ($block =~ /^\s*{/);
616913214adfSAndy Whitcroft
6170aad4f614SJoe Perches					#print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
6171cf655043SAndy Whitcroft					if (statement_lines($cond) > 1) {
6172cf655043SAndy Whitcroft						#print "APW: ALLOWED: cond<$cond>\n";
6173aad4f614SJoe Perches						$allowed[$allow] = 1;
617413214adfSAndy Whitcroft					}
617513214adfSAndy Whitcroft					if ($block =~/\b(?:if|for|while)\b/) {
6176cf655043SAndy Whitcroft						#print "APW: ALLOWED: block<$block>\n";
6177aad4f614SJoe Perches						$allowed[$allow] = 1;
617813214adfSAndy Whitcroft					}
6179cf655043SAndy Whitcroft					if (statement_block_size($block) > 1) {
6180cf655043SAndy Whitcroft						#print "APW: ALLOWED: lines block<$block>\n";
6181aad4f614SJoe Perches						$allowed[$allow] = 1;
618213214adfSAndy Whitcroft					}
6183aad4f614SJoe Perches					$allow++;
618413214adfSAndy Whitcroft				}
6185aad4f614SJoe Perches				if ($seen) {
6186aad4f614SJoe Perches					my $sum_allowed = 0;
6187aad4f614SJoe Perches					foreach (@allowed) {
6188aad4f614SJoe Perches						$sum_allowed += $_;
6189aad4f614SJoe Perches					}
6190aad4f614SJoe Perches					if ($sum_allowed == 0) {
6191000d1cc1SJoe Perches						WARN("BRACES",
6192000d1cc1SJoe Perches						     "braces {} are not necessary for any arm of this statement\n" . $herectx);
6193aad4f614SJoe Perches					} elsif ($sum_allowed != $allow &&
6194aad4f614SJoe Perches						 $seen != $allow) {
6195aad4f614SJoe Perches						CHK("BRACES",
6196aad4f614SJoe Perches						    "braces {} should be used on all arms of this statement\n" . $herectx);
6197aad4f614SJoe Perches					}
619813214adfSAndy Whitcroft				}
619913214adfSAndy Whitcroft			}
620013214adfSAndy Whitcroft		}
6201773647a0SAndy Whitcroft		if (!defined $suppress_ifbraces{$linenr - 1} &&
620213214adfSAndy Whitcroft					$line =~ /\b(if|while|for|else)\b/) {
6203cf655043SAndy Whitcroft			my $allowed = 0;
6204f0a594c1SAndy Whitcroft
6205cf655043SAndy Whitcroft			# Check the pre-context.
6206cf655043SAndy Whitcroft			if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
6207cf655043SAndy Whitcroft				#print "APW: ALLOWED: pre<$1>\n";
6208cf655043SAndy Whitcroft				$allowed = 1;
6209f0a594c1SAndy Whitcroft			}
6210773647a0SAndy Whitcroft
6211773647a0SAndy Whitcroft			my ($level, $endln, @chunks) =
6212773647a0SAndy Whitcroft				ctx_statement_full($linenr, $realcnt, $-[0]);
6213773647a0SAndy Whitcroft
6214cf655043SAndy Whitcroft			# Check the condition.
6215cf655043SAndy Whitcroft			my ($cond, $block) = @{$chunks[0]};
6216773647a0SAndy Whitcroft			#print "CHECKING<$linenr> cond<$cond> block<$block>\n";
6217cf655043SAndy Whitcroft			if (defined $cond) {
6218773647a0SAndy Whitcroft				substr($block, 0, length($cond), '');
6219cf655043SAndy Whitcroft			}
6220cf655043SAndy Whitcroft			if (statement_lines($cond) > 1) {
6221cf655043SAndy Whitcroft				#print "APW: ALLOWED: cond<$cond>\n";
6222cf655043SAndy Whitcroft				$allowed = 1;
6223cf655043SAndy Whitcroft			}
6224cf655043SAndy Whitcroft			if ($block =~/\b(?:if|for|while)\b/) {
6225cf655043SAndy Whitcroft				#print "APW: ALLOWED: block<$block>\n";
6226cf655043SAndy Whitcroft				$allowed = 1;
6227cf655043SAndy Whitcroft			}
6228cf655043SAndy Whitcroft			if (statement_block_size($block) > 1) {
6229cf655043SAndy Whitcroft				#print "APW: ALLOWED: lines block<$block>\n";
6230cf655043SAndy Whitcroft				$allowed = 1;
6231cf655043SAndy Whitcroft			}
6232cf655043SAndy Whitcroft			# Check the post-context.
6233cf655043SAndy Whitcroft			if (defined $chunks[1]) {
6234cf655043SAndy Whitcroft				my ($cond, $block) = @{$chunks[1]};
6235cf655043SAndy Whitcroft				if (defined $cond) {
6236773647a0SAndy Whitcroft					substr($block, 0, length($cond), '');
6237cf655043SAndy Whitcroft				}
6238cf655043SAndy Whitcroft				if ($block =~ /^\s*\{/) {
6239cf655043SAndy Whitcroft					#print "APW: ALLOWED: chunk-1 block<$block>\n";
6240cf655043SAndy Whitcroft					$allowed = 1;
6241cf655043SAndy Whitcroft				}
6242cf655043SAndy Whitcroft			}
6243cf655043SAndy Whitcroft			if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
6244f055663cSAndy Whitcroft				my $cnt = statement_rawlines($block);
6245e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
6246cf655043SAndy Whitcroft
6247000d1cc1SJoe Perches				WARN("BRACES",
6248000d1cc1SJoe Perches				     "braces {} are not necessary for single statement blocks\n" . $herectx);
6249f0a594c1SAndy Whitcroft			}
6250f0a594c1SAndy Whitcroft		}
6251f0a594c1SAndy Whitcroft
6252e4c5babdSJoe Perches# check for single line unbalanced braces
625395330473SSven Eckelmann		if ($sline =~ /^.\s*\}\s*else\s*$/ ||
625495330473SSven Eckelmann		    $sline =~ /^.\s*else\s*\{\s*$/) {
6255e4c5babdSJoe Perches			CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
6256e4c5babdSJoe Perches		}
6257e4c5babdSJoe Perches
62580979ae66SJoe Perches# check for unnecessary blank lines around braces
625977b9a53aSJoe Perches		if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
6260f8e58219SJoe Perches			if (CHK("BRACES",
6261f8e58219SJoe Perches				"Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
6262f8e58219SJoe Perches			    $fix && $prevrawline =~ /^\+/) {
6263f8e58219SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
6264f8e58219SJoe Perches			}
62650979ae66SJoe Perches		}
626677b9a53aSJoe Perches		if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
6267f8e58219SJoe Perches			if (CHK("BRACES",
6268f8e58219SJoe Perches				"Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
6269f8e58219SJoe Perches			    $fix) {
6270f8e58219SJoe Perches				fix_delete_line($fixlinenr, $rawline);
6271f8e58219SJoe Perches			}
62720979ae66SJoe Perches		}
62730979ae66SJoe Perches
62744a0df2efSAndy Whitcroft# no volatiles please
62756c72ffaaSAndy Whitcroft		my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
62766c72ffaaSAndy Whitcroft		if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
6277000d1cc1SJoe Perches			WARN("VOLATILE",
62788c27ceffSMauro Carvalho Chehab			     "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
62794a0df2efSAndy Whitcroft		}
62804a0df2efSAndy Whitcroft
62815e4f6ba5SJoe Perches# Check for user-visible strings broken across lines, which breaks the ability
62825e4f6ba5SJoe Perches# to grep for the string.  Make exceptions when the previous string ends in a
62835e4f6ba5SJoe Perches# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
62845e4f6ba5SJoe Perches# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
628533acb54aSJoe Perches		if ($line =~ /^\+\s*$String/ &&
62865e4f6ba5SJoe Perches		    $prevline =~ /"\s*$/ &&
62875e4f6ba5SJoe Perches		    $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
62885e4f6ba5SJoe Perches			if (WARN("SPLIT_STRING",
62895e4f6ba5SJoe Perches				 "quoted string split across lines\n" . $hereprev) &&
62905e4f6ba5SJoe Perches				     $fix &&
62915e4f6ba5SJoe Perches				     $prevrawline =~ /^\+.*"\s*$/ &&
62925e4f6ba5SJoe Perches				     $last_coalesced_string_linenr != $linenr - 1) {
62935e4f6ba5SJoe Perches				my $extracted_string = get_quoted_string($line, $rawline);
62945e4f6ba5SJoe Perches				my $comma_close = "";
62955e4f6ba5SJoe Perches				if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
62965e4f6ba5SJoe Perches					$comma_close = $1;
62975e4f6ba5SJoe Perches				}
62985e4f6ba5SJoe Perches
62995e4f6ba5SJoe Perches				fix_delete_line($fixlinenr - 1, $prevrawline);
63005e4f6ba5SJoe Perches				fix_delete_line($fixlinenr, $rawline);
63015e4f6ba5SJoe Perches				my $fixedline = $prevrawline;
63025e4f6ba5SJoe Perches				$fixedline =~ s/"\s*$//;
63035e4f6ba5SJoe Perches				$fixedline .= substr($extracted_string, 1) . trim($comma_close);
63045e4f6ba5SJoe Perches				fix_insert_line($fixlinenr - 1, $fixedline);
63055e4f6ba5SJoe Perches				$fixedline = $rawline;
63065e4f6ba5SJoe Perches				$fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
63075e4f6ba5SJoe Perches				if ($fixedline !~ /\+\s*$/) {
63085e4f6ba5SJoe Perches					fix_insert_line($fixlinenr, $fixedline);
63095e4f6ba5SJoe Perches				}
63105e4f6ba5SJoe Perches				$last_coalesced_string_linenr = $linenr;
63115e4f6ba5SJoe Perches			}
63125e4f6ba5SJoe Perches		}
63135e4f6ba5SJoe Perches
63145e4f6ba5SJoe Perches# check for missing a space in a string concatenation
63155e4f6ba5SJoe Perches		if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
63165e4f6ba5SJoe Perches			WARN('MISSING_SPACE',
63175e4f6ba5SJoe Perches			     "break quoted strings at a space character\n" . $hereprev);
63185e4f6ba5SJoe Perches		}
63195e4f6ba5SJoe Perches
632077cb8546SJoe Perches# check for an embedded function name in a string when the function is known
6321e4b7d309SJoe Perches# This does not work very well for -f --file checking as it depends on patch
6322e4b7d309SJoe Perches# context providing the function name or a single line form for in-file
6323e4b7d309SJoe Perches# function declarations
632477cb8546SJoe Perches		if ($line =~ /^\+.*$String/ &&
632577cb8546SJoe Perches		    defined($context_function) &&
6326e4b7d309SJoe Perches		    get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
6327e4b7d309SJoe Perches		    length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
632877cb8546SJoe Perches			WARN("EMBEDDED_FUNCTION_NAME",
6329e4b7d309SJoe Perches			     "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
633077cb8546SJoe Perches		}
633177cb8546SJoe Perches
6332adb2da82SJoe Perches# check for unnecessary function tracing like uses
6333adb2da82SJoe Perches# This does not use $logFunctions because there are many instances like
6334adb2da82SJoe Perches# 'dprintk(FOO, "%s()\n", __func__);' which do not match $logFunctions
6335adb2da82SJoe Perches		if ($rawline =~ /^\+.*\([^"]*"$tracing_logging_tags{0,3}%s(?:\s*\(\s*\)\s*)?$tracing_logging_tags{0,3}(?:\\n)?"\s*,\s*__func__\s*\)\s*;/) {
6336adb2da82SJoe Perches			if (WARN("TRACING_LOGGING",
6337adb2da82SJoe Perches				 "Unnecessary ftrace-like logging - prefer using ftrace\n" . $herecurr) &&
6338adb2da82SJoe Perches			    $fix) {
6339adb2da82SJoe Perches                                fix_delete_line($fixlinenr, $rawline);
6340adb2da82SJoe Perches			}
6341adb2da82SJoe Perches		}
6342adb2da82SJoe Perches
63435e4f6ba5SJoe Perches# check for spaces before a quoted newline
63445e4f6ba5SJoe Perches		if ($rawline =~ /^.*\".*\s\\n/) {
63455e4f6ba5SJoe Perches			if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
63465e4f6ba5SJoe Perches				 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
63475e4f6ba5SJoe Perches			    $fix) {
63485e4f6ba5SJoe Perches				$fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
63495e4f6ba5SJoe Perches			}
63505e4f6ba5SJoe Perches
63515e4f6ba5SJoe Perches		}
63525e4f6ba5SJoe Perches
6353f17dba4fSJoe Perches# concatenated string without spaces between elements
6354d2af5aa6SJoe Perches		if ($line =~ /$String[A-Z_]/ ||
6355d2af5aa6SJoe Perches		    ($line =~ /([A-Za-z0-9_]+)$String/ && $1 !~ /^[Lu]$/)) {
635679682c0cSJoe Perches			if (CHK("CONCATENATED_STRING",
635779682c0cSJoe Perches				"Concatenated strings should use spaces between elements\n" . $herecurr) &&
635879682c0cSJoe Perches			    $fix) {
635979682c0cSJoe Perches				while ($line =~ /($String)/g) {
636079682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
636179682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E([A-Za-z0-9_])/$extracted_string $1/;
636279682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/([A-Za-z0-9_])\Q$extracted_string\E/$1 $extracted_string/;
636379682c0cSJoe Perches				}
636479682c0cSJoe Perches			}
6365f17dba4fSJoe Perches		}
6366f17dba4fSJoe Perches
636790ad30e5SJoe Perches# uncoalesced string fragments
6368d2af5aa6SJoe Perches		if ($line =~ /$String\s*[Lu]?"/) {
636979682c0cSJoe Perches			if (WARN("STRING_FRAGMENTS",
637079682c0cSJoe Perches				 "Consecutive strings are generally better as a single string\n" . $herecurr) &&
637179682c0cSJoe Perches			    $fix) {
637279682c0cSJoe Perches				while ($line =~ /($String)(?=\s*")/g) {
637379682c0cSJoe Perches					my $extracted_string = substr($rawline, $-[0], $+[0] - $-[0]);
637479682c0cSJoe Perches					$fixed[$fixlinenr] =~ s/\Q$extracted_string\E\s*"/substr($extracted_string, 0, -1)/e;
637579682c0cSJoe Perches				}
637679682c0cSJoe Perches			}
637790ad30e5SJoe Perches		}
637890ad30e5SJoe Perches
6379522b837cSAlexey Dobriyan# check for non-standard and hex prefixed decimal printf formats
6380522b837cSAlexey Dobriyan		my $show_L = 1;	#don't show the same defect twice
6381522b837cSAlexey Dobriyan		my $show_Z = 1;
63825e4f6ba5SJoe Perches		while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
6383522b837cSAlexey Dobriyan			my $string = substr($rawline, $-[1], $+[1] - $-[1]);
63845e4f6ba5SJoe Perches			$string =~ s/%%/__/g;
6385522b837cSAlexey Dobriyan			# check for %L
6386522b837cSAlexey Dobriyan			if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
63875e4f6ba5SJoe Perches				WARN("PRINTF_L",
6388522b837cSAlexey Dobriyan				     "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
6389522b837cSAlexey Dobriyan				$show_L = 0;
63905e4f6ba5SJoe Perches			}
6391522b837cSAlexey Dobriyan			# check for %Z
6392522b837cSAlexey Dobriyan			if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
6393522b837cSAlexey Dobriyan				WARN("PRINTF_Z",
6394522b837cSAlexey Dobriyan				     "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
6395522b837cSAlexey Dobriyan				$show_Z = 0;
6396522b837cSAlexey Dobriyan			}
6397522b837cSAlexey Dobriyan			# check for 0x<decimal>
6398522b837cSAlexey Dobriyan			if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
6399522b837cSAlexey Dobriyan				ERROR("PRINTF_0XDECIMAL",
64006e300757SJoe Perches				      "Prefixing 0x with decimal output is defective\n" . $herecurr);
64016e300757SJoe Perches			}
64025e4f6ba5SJoe Perches		}
64035e4f6ba5SJoe Perches
64045e4f6ba5SJoe Perches# check for line continuations in quoted strings with odd counts of "
64053f7f335dSJoe Perches		if ($rawline =~ /\\$/ && $sline =~ tr/"/"/ % 2) {
64065e4f6ba5SJoe Perches			WARN("LINE_CONTINUATIONS",
64075e4f6ba5SJoe Perches			     "Avoid line continuations in quoted strings\n" . $herecurr);
64085e4f6ba5SJoe Perches		}
64095e4f6ba5SJoe Perches
641000df344fSAndy Whitcroft# warn about #if 0
6411c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*if\s+0\b/) {
641260f89010SPrakruthi Deepak Heragu			WARN("IF_0",
641360f89010SPrakruthi Deepak Heragu			     "Consider removing the code enclosed by this #if 0 and its #endif\n" . $herecurr);
641460f89010SPrakruthi Deepak Heragu		}
641560f89010SPrakruthi Deepak Heragu
641660f89010SPrakruthi Deepak Heragu# warn about #if 1
641760f89010SPrakruthi Deepak Heragu		if ($line =~ /^.\s*\#\s*if\s+1\b/) {
641860f89010SPrakruthi Deepak Heragu			WARN("IF_1",
641960f89010SPrakruthi Deepak Heragu			     "Consider removing the #if 1 and its #endif\n" . $herecurr);
64204a0df2efSAndy Whitcroft		}
64214a0df2efSAndy Whitcroft
642203df4b51SAndy Whitcroft# check for needless "if (<foo>) fn(<foo>)" uses
642303df4b51SAndy Whitcroft		if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
6424100425deSJoe Perches			my $tested = quotemeta($1);
6425100425deSJoe Perches			my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
6426100425deSJoe Perches			if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
6427100425deSJoe Perches				my $func = $1;
6428100425deSJoe Perches				if (WARN('NEEDLESS_IF',
6429100425deSJoe Perches					 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
6430100425deSJoe Perches				    $fix) {
6431100425deSJoe Perches					my $do_fix = 1;
6432100425deSJoe Perches					my $leading_tabs = "";
6433100425deSJoe Perches					my $new_leading_tabs = "";
6434100425deSJoe Perches					if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
6435100425deSJoe Perches						$leading_tabs = $1;
6436100425deSJoe Perches					} else {
6437100425deSJoe Perches						$do_fix = 0;
6438100425deSJoe Perches					}
6439100425deSJoe Perches					if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
6440100425deSJoe Perches						$new_leading_tabs = $1;
6441100425deSJoe Perches						if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
6442100425deSJoe Perches							$do_fix = 0;
6443100425deSJoe Perches						}
6444100425deSJoe Perches					} else {
6445100425deSJoe Perches						$do_fix = 0;
6446100425deSJoe Perches					}
6447100425deSJoe Perches					if ($do_fix) {
6448100425deSJoe Perches						fix_delete_line($fixlinenr - 1, $prevrawline);
6449100425deSJoe Perches						$fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
6450100425deSJoe Perches					}
6451100425deSJoe Perches				}
64524c432a8fSGreg Kroah-Hartman			}
64534c432a8fSGreg Kroah-Hartman		}
6454f0a594c1SAndy Whitcroft
6455ebfdc409SJoe Perches# check for unnecessary "Out of Memory" messages
6456ebfdc409SJoe Perches		if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
6457ebfdc409SJoe Perches		    $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
6458ebfdc409SJoe Perches		    (defined $1 || defined $3) &&
6459ebfdc409SJoe Perches		    $linenr > 3) {
6460ebfdc409SJoe Perches			my $testval = $2;
6461ebfdc409SJoe Perches			my $testline = $lines[$linenr - 3];
6462ebfdc409SJoe Perches
6463ebfdc409SJoe Perches			my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
6464ebfdc409SJoe Perches#			print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
6465ebfdc409SJoe Perches
6466e29a70f1SJoe Perches			if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*$allocFunctions\s*\(/ &&
6467e29a70f1SJoe Perches			    $s !~ /\b__GFP_NOWARN\b/ ) {
6468ebfdc409SJoe Perches				WARN("OOM_MESSAGE",
6469ebfdc409SJoe Perches				     "Possible unnecessary 'out of memory' message\n" . $hereprev);
6470ebfdc409SJoe Perches			}
6471ebfdc409SJoe Perches		}
6472ebfdc409SJoe Perches
6473f78d98f6SJoe Perches# check for logging functions with KERN_<LEVEL>
6474dcaf1123SPaolo Bonzini		if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
6475f78d98f6SJoe Perches		    $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
6476f78d98f6SJoe Perches			my $level = $1;
6477f78d98f6SJoe Perches			if (WARN("UNNECESSARY_KERN_LEVEL",
6478f78d98f6SJoe Perches				 "Possible unnecessary $level\n" . $herecurr) &&
6479f78d98f6SJoe Perches			    $fix) {
6480f78d98f6SJoe Perches				$fixed[$fixlinenr] =~ s/\s*$level\s*//;
6481f78d98f6SJoe Perches			}
6482f78d98f6SJoe Perches		}
6483f78d98f6SJoe Perches
648445c55e92SJoe Perches# check for logging continuations
648545c55e92SJoe Perches		if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
648645c55e92SJoe Perches			WARN("LOGGING_CONTINUATION",
648745c55e92SJoe Perches			     "Avoid logging continuation uses where feasible\n" . $herecurr);
648845c55e92SJoe Perches		}
648945c55e92SJoe Perches
649070eb2275SDwaipayan Ray# check for unnecessary use of %h[xudi] and %hh[xudi] in logging functions
649170eb2275SDwaipayan Ray		if (defined $stat &&
649270eb2275SDwaipayan Ray		    $line =~ /\b$logFunctions\s*\(/ &&
649370eb2275SDwaipayan Ray		    index($stat, '"') >= 0) {
649470eb2275SDwaipayan Ray			my $lc = $stat =~ tr@\n@@;
649570eb2275SDwaipayan Ray			$lc = $lc + $linenr;
649670eb2275SDwaipayan Ray			my $stat_real = get_stat_real($linenr, $lc);
649770eb2275SDwaipayan Ray			pos($stat_real) = index($stat_real, '"');
649870eb2275SDwaipayan Ray			while ($stat_real =~ /[^\"%]*(%[\#\d\.\*\-]*(h+)[idux])/g) {
649970eb2275SDwaipayan Ray				my $pspec = $1;
650070eb2275SDwaipayan Ray				my $h = $2;
650170eb2275SDwaipayan Ray				my $lineoff = substr($stat_real, 0, $-[1]) =~ tr@\n@@;
650270eb2275SDwaipayan Ray				if (WARN("UNNECESSARY_MODIFIER",
650370eb2275SDwaipayan Ray					 "Integer promotion: Using '$h' in '$pspec' is unnecessary\n" . "$here\n$stat_real\n") &&
650470eb2275SDwaipayan Ray				    $fix && $fixed[$fixlinenr + $lineoff] =~ /^\+/) {
650570eb2275SDwaipayan Ray					my $nspec = $pspec;
650670eb2275SDwaipayan Ray					$nspec =~ s/h//g;
650770eb2275SDwaipayan Ray					$fixed[$fixlinenr + $lineoff] =~ s/\Q$pspec\E/$nspec/;
650870eb2275SDwaipayan Ray				}
650970eb2275SDwaipayan Ray			}
651070eb2275SDwaipayan Ray		}
651170eb2275SDwaipayan Ray
6512abb08a53SJoe Perches# check for mask then right shift without a parentheses
65135b57980dSJoe Perches		if ($perl_version_ok &&
6514abb08a53SJoe Perches		    $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
6515abb08a53SJoe Perches		    $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
6516abb08a53SJoe Perches			WARN("MASK_THEN_SHIFT",
6517abb08a53SJoe Perches			     "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
6518abb08a53SJoe Perches		}
6519abb08a53SJoe Perches
6520b75ac618SJoe Perches# check for pointer comparisons to NULL
65215b57980dSJoe Perches		if ($perl_version_ok) {
6522b75ac618SJoe Perches			while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
6523b75ac618SJoe Perches				my $val = $1;
6524b75ac618SJoe Perches				my $equal = "!";
6525b75ac618SJoe Perches				$equal = "" if ($4 eq "!=");
6526b75ac618SJoe Perches				if (CHK("COMPARISON_TO_NULL",
6527b75ac618SJoe Perches					"Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
6528b75ac618SJoe Perches					    $fix) {
6529b75ac618SJoe Perches					$fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
6530b75ac618SJoe Perches				}
6531b75ac618SJoe Perches			}
6532b75ac618SJoe Perches		}
6533b75ac618SJoe Perches
65348716de38SJoe Perches# check for bad placement of section $InitAttribute (e.g.: __initdata)
65358716de38SJoe Perches		if ($line =~ /(\b$InitAttribute\b)/) {
65368716de38SJoe Perches			my $attr = $1;
65378716de38SJoe Perches			if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
65388716de38SJoe Perches				my $ptr = $1;
65398716de38SJoe Perches				my $var = $2;
65408716de38SJoe Perches				if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
65418716de38SJoe Perches				      ERROR("MISPLACED_INIT",
65428716de38SJoe Perches					    "$attr should be placed after $var\n" . $herecurr)) ||
65438716de38SJoe Perches				     ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
65448716de38SJoe Perches				      WARN("MISPLACED_INIT",
65458716de38SJoe Perches					   "$attr should be placed after $var\n" . $herecurr))) &&
65468716de38SJoe Perches				    $fix) {
6547194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/(\bstatic\s+(?:const\s+)?)(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*([=;])\s*/"$1" . trim(string_find_replace($2, "\\s*$attr\\s*", " ")) . " " . trim(string_find_replace($3, "\\s*$attr\\s*", "")) . " $attr" . ("$4" eq ";" ? ";" : " = ")/e;
65488716de38SJoe Perches				}
65498716de38SJoe Perches			}
65508716de38SJoe Perches		}
65518716de38SJoe Perches
6552e970b884SJoe Perches# check for $InitAttributeData (ie: __initdata) with const
6553e970b884SJoe Perches		if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
6554e970b884SJoe Perches			my $attr = $1;
6555e970b884SJoe Perches			$attr =~ /($InitAttributePrefix)(.*)/;
6556e970b884SJoe Perches			my $attr_prefix = $1;
6557e970b884SJoe Perches			my $attr_type = $2;
6558e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6559e970b884SJoe Perches				  "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
6560e970b884SJoe Perches			    $fix) {
6561194f66fcSJoe Perches				$fixed[$fixlinenr] =~
6562e970b884SJoe Perches				    s/$InitAttributeData/${attr_prefix}initconst/;
6563e970b884SJoe Perches			}
6564e970b884SJoe Perches		}
6565e970b884SJoe Perches
6566e970b884SJoe Perches# check for $InitAttributeConst (ie: __initconst) without const
6567e970b884SJoe Perches		if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
6568e970b884SJoe Perches			my $attr = $1;
6569e970b884SJoe Perches			if (ERROR("INIT_ATTRIBUTE",
6570e970b884SJoe Perches				  "Use of $attr requires a separate use of const\n" . $herecurr) &&
6571e970b884SJoe Perches			    $fix) {
6572194f66fcSJoe Perches				my $lead = $fixed[$fixlinenr] =~
6573e970b884SJoe Perches				    /(^\+\s*(?:static\s+))/;
6574e970b884SJoe Perches				$lead = rtrim($1);
6575e970b884SJoe Perches				$lead = "$lead " if ($lead !~ /^\+$/);
6576e970b884SJoe Perches				$lead = "${lead}const ";
6577194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
6578e970b884SJoe Perches			}
6579e970b884SJoe Perches		}
6580e970b884SJoe Perches
6581c17893c7SJoe Perches# check for __read_mostly with const non-pointer (should just be const)
6582c17893c7SJoe Perches		if ($line =~ /\b__read_mostly\b/ &&
6583c17893c7SJoe Perches		    $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
6584c17893c7SJoe Perches			if (ERROR("CONST_READ_MOSTLY",
6585c17893c7SJoe Perches				  "Invalid use of __read_mostly with const type\n" . $herecurr) &&
6586c17893c7SJoe Perches			    $fix) {
6587c17893c7SJoe Perches				$fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
6588c17893c7SJoe Perches			}
6589c17893c7SJoe Perches		}
6590c17893c7SJoe Perches
6591fbdb8138SJoe Perches# don't use __constant_<foo> functions outside of include/uapi/
6592fbdb8138SJoe Perches		if ($realfile !~ m@^include/uapi/@ &&
6593fbdb8138SJoe Perches		    $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
6594fbdb8138SJoe Perches			my $constant_func = $1;
6595fbdb8138SJoe Perches			my $func = $constant_func;
6596fbdb8138SJoe Perches			$func =~ s/^__constant_//;
6597fbdb8138SJoe Perches			if (WARN("CONSTANT_CONVERSION",
6598fbdb8138SJoe Perches				 "$constant_func should be $func\n" . $herecurr) &&
6599fbdb8138SJoe Perches			    $fix) {
6600194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
6601fbdb8138SJoe Perches			}
6602fbdb8138SJoe Perches		}
6603fbdb8138SJoe Perches
66041a15a250SPatrick Pannuto# prefer usleep_range over udelay
660537581c28SBruce Allan		if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
660643c1d77cSJoe Perches			my $delay = $1;
66071a15a250SPatrick Pannuto			# ignore udelay's < 10, however
660843c1d77cSJoe Perches			if (! ($delay < 10) ) {
6609000d1cc1SJoe Perches				CHK("USLEEP_RANGE",
6610458f69efSMauro Carvalho Chehab				    "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst\n" . $herecurr);
661143c1d77cSJoe Perches			}
661243c1d77cSJoe Perches			if ($delay > 2000) {
661343c1d77cSJoe Perches				WARN("LONG_UDELAY",
661443c1d77cSJoe Perches				     "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
66151a15a250SPatrick Pannuto			}
66161a15a250SPatrick Pannuto		}
66171a15a250SPatrick Pannuto
661809ef8725SPatrick Pannuto# warn about unexpectedly long msleep's
661909ef8725SPatrick Pannuto		if ($line =~ /\bmsleep\s*\((\d+)\);/) {
662009ef8725SPatrick Pannuto			if ($1 < 20) {
6621000d1cc1SJoe Perches				WARN("MSLEEP",
6622458f69efSMauro Carvalho Chehab				     "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.rst\n" . $herecurr);
662309ef8725SPatrick Pannuto			}
662409ef8725SPatrick Pannuto		}
662509ef8725SPatrick Pannuto
662636ec1939SJoe Perches# check for comparisons of jiffies
662736ec1939SJoe Perches		if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
662836ec1939SJoe Perches			WARN("JIFFIES_COMPARISON",
662936ec1939SJoe Perches			     "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
663036ec1939SJoe Perches		}
663136ec1939SJoe Perches
66329d7a34a5SJoe Perches# check for comparisons of get_jiffies_64()
66339d7a34a5SJoe Perches		if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
66349d7a34a5SJoe Perches			WARN("JIFFIES_COMPARISON",
66359d7a34a5SJoe Perches			     "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
66369d7a34a5SJoe Perches		}
66379d7a34a5SJoe Perches
663800df344fSAndy Whitcroft# warn about #ifdefs in C files
6639c45dcabdSAndy Whitcroft#		if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
664000df344fSAndy Whitcroft#			print "#ifdef in C files should be avoided\n";
664100df344fSAndy Whitcroft#			print "$herecurr";
664200df344fSAndy Whitcroft#			$clean = 0;
664300df344fSAndy Whitcroft#		}
664400df344fSAndy Whitcroft
664522f2a2efSAndy Whitcroft# warn about spacing in #ifdefs
6646c45dcabdSAndy Whitcroft		if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
66473705ce5bSJoe Perches			if (ERROR("SPACING",
66483705ce5bSJoe Perches				  "exactly one space required after that #$1\n" . $herecurr) &&
66493705ce5bSJoe Perches			    $fix) {
6650194f66fcSJoe Perches				$fixed[$fixlinenr] =~
66513705ce5bSJoe Perches				    s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
66523705ce5bSJoe Perches			}
66533705ce5bSJoe Perches
665422f2a2efSAndy Whitcroft		}
665522f2a2efSAndy Whitcroft
66564a0df2efSAndy Whitcroft# check for spinlock_t definitions without a comment.
6657171ae1a4SAndy Whitcroft		if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
6658171ae1a4SAndy Whitcroft		    $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
66594a0df2efSAndy Whitcroft			my $which = $1;
66604a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6661000d1cc1SJoe Perches				CHK("UNCOMMENTED_DEFINITION",
6662000d1cc1SJoe Perches				    "$1 definition without comment\n" . $herecurr);
66634a0df2efSAndy Whitcroft			}
66644a0df2efSAndy Whitcroft		}
66654a0df2efSAndy Whitcroft# check for memory barriers without a comment.
6666402c2553SMichael S. Tsirkin
6667402c2553SMichael S. Tsirkin		my $barriers = qr{
6668402c2553SMichael S. Tsirkin			mb|
6669402c2553SMichael S. Tsirkin			rmb|
6670ad83ec6cSWill Deacon			wmb
6671402c2553SMichael S. Tsirkin		}x;
6672402c2553SMichael S. Tsirkin		my $barrier_stems = qr{
6673402c2553SMichael S. Tsirkin			mb__before_atomic|
6674402c2553SMichael S. Tsirkin			mb__after_atomic|
6675402c2553SMichael S. Tsirkin			store_release|
6676402c2553SMichael S. Tsirkin			load_acquire|
6677402c2553SMichael S. Tsirkin			store_mb|
6678402c2553SMichael S. Tsirkin			(?:$barriers)
6679402c2553SMichael S. Tsirkin		}x;
6680402c2553SMichael S. Tsirkin		my $all_barriers = qr{
6681402c2553SMichael S. Tsirkin			(?:$barriers)|
668243e361f2SMichael S. Tsirkin			smp_(?:$barrier_stems)|
668343e361f2SMichael S. Tsirkin			virt_(?:$barrier_stems)
6684402c2553SMichael S. Tsirkin		}x;
6685402c2553SMichael S. Tsirkin
6686402c2553SMichael S. Tsirkin		if ($line =~ /\b(?:$all_barriers)\s*\(/) {
66874a0df2efSAndy Whitcroft			if (!ctx_has_comment($first_line, $linenr)) {
6688c1fd7bb9SJoe Perches				WARN("MEMORY_BARRIER",
6689000d1cc1SJoe Perches				     "memory barrier without comment\n" . $herecurr);
66904a0df2efSAndy Whitcroft			}
66914a0df2efSAndy Whitcroft		}
66923ad81779SPaul E. McKenney
6693f4073b0fSMichael S. Tsirkin		my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
6694f4073b0fSMichael S. Tsirkin
6695f4073b0fSMichael S. Tsirkin		if ($realfile !~ m@^include/asm-generic/@ &&
6696f4073b0fSMichael S. Tsirkin		    $realfile !~ m@/barrier\.h$@ &&
6697f4073b0fSMichael S. Tsirkin		    $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
6698f4073b0fSMichael S. Tsirkin		    $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
6699f4073b0fSMichael S. Tsirkin			WARN("MEMORY_BARRIER",
6700f4073b0fSMichael S. Tsirkin			     "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
6701f4073b0fSMichael S. Tsirkin		}
6702f4073b0fSMichael S. Tsirkin
6703cb426e99SJoe Perches# check for waitqueue_active without a comment.
6704cb426e99SJoe Perches		if ($line =~ /\bwaitqueue_active\s*\(/) {
6705cb426e99SJoe Perches			if (!ctx_has_comment($first_line, $linenr)) {
6706cb426e99SJoe Perches				WARN("WAITQUEUE_ACTIVE",
6707cb426e99SJoe Perches				     "waitqueue_active without comment\n" . $herecurr);
6708cb426e99SJoe Perches			}
6709cb426e99SJoe Perches		}
67103ad81779SPaul E. McKenney
67115099a722SMarco Elver# check for data_race without a comment.
67125099a722SMarco Elver		if ($line =~ /\bdata_race\s*\(/) {
67135099a722SMarco Elver			if (!ctx_has_comment($first_line, $linenr)) {
67145099a722SMarco Elver				WARN("DATA_RACE",
67155099a722SMarco Elver				     "data_race without comment\n" . $herecurr);
67165099a722SMarco Elver			}
67175099a722SMarco Elver		}
67185099a722SMarco Elver
67194a0df2efSAndy Whitcroft# check of hardware specific defines
6720c45dcabdSAndy Whitcroft		if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
6721000d1cc1SJoe Perches			CHK("ARCH_DEFINES",
6722000d1cc1SJoe Perches			    "architecture specific defines should be avoided\n" .  $herecurr);
67230a920b5bSAndy Whitcroft		}
6724653d4876SAndy Whitcroft
6725596ed45bSJoe Perches# check that the storage class is not after a type
6726596ed45bSJoe Perches		if ($line =~ /\b($Type)\s+($Storage)\b/) {
6727000d1cc1SJoe Perches			WARN("STORAGE_CLASS",
6728596ed45bSJoe Perches			     "storage class '$2' should be located before type '$1'\n" . $herecurr);
6729596ed45bSJoe Perches		}
6730596ed45bSJoe Perches# Check that the storage class is at the beginning of a declaration
6731596ed45bSJoe Perches		if ($line =~ /\b$Storage\b/ &&
6732596ed45bSJoe Perches		    $line !~ /^.\s*$Storage/ &&
6733596ed45bSJoe Perches		    $line =~ /^.\s*(.+?)\$Storage\s/ &&
6734596ed45bSJoe Perches		    $1 !~ /[\,\)]\s*$/) {
6735596ed45bSJoe Perches			WARN("STORAGE_CLASS",
6736596ed45bSJoe Perches			     "storage class should be at the beginning of the declaration\n" . $herecurr);
6737d4977c78STobias Klauser		}
6738d4977c78STobias Klauser
6739de7d4f0eSAndy Whitcroft# check the location of the inline attribute, that it is between
6740de7d4f0eSAndy Whitcroft# storage class and type.
67419c0ca6f9SAndy Whitcroft		if ($line =~ /\b$Type\s+$Inline\b/ ||
67429c0ca6f9SAndy Whitcroft		    $line =~ /\b$Inline\s+$Storage\b/) {
6743000d1cc1SJoe Perches			ERROR("INLINE_LOCATION",
6744000d1cc1SJoe Perches			      "inline keyword should sit between storage class and type\n" . $herecurr);
6745de7d4f0eSAndy Whitcroft		}
6746de7d4f0eSAndy Whitcroft
67478905a67cSAndy Whitcroft# Check for __inline__ and __inline, prefer inline
67482b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
67492b7ab453SJoe Perches		    $line =~ /\b(__inline__|__inline)\b/) {
6750d5e616fcSJoe Perches			if (WARN("INLINE",
6751d5e616fcSJoe Perches				 "plain inline is preferred over $1\n" . $herecurr) &&
6752d5e616fcSJoe Perches			    $fix) {
6753194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
6754d5e616fcSJoe Perches
6755d5e616fcSJoe Perches			}
67568905a67cSAndy Whitcroft		}
67578905a67cSAndy Whitcroft
67587ebe1d17SDwaipayan Ray# Check for compiler attributes
67592b7ab453SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
67607ebe1d17SDwaipayan Ray		    $rawline =~ /\b__attribute__\s*\(\s*($balanced_parens)\s*\)/) {
67617ebe1d17SDwaipayan Ray			my $attr = $1;
67627ebe1d17SDwaipayan Ray			$attr =~ s/\s*\(\s*(.*)\)\s*/$1/;
67637ebe1d17SDwaipayan Ray
67647ebe1d17SDwaipayan Ray			my %attr_list = (
67650830aab0SJoe Perches				"alias"				=> "__alias",
67667ebe1d17SDwaipayan Ray				"aligned"			=> "__aligned",
67677ebe1d17SDwaipayan Ray				"always_inline"			=> "__always_inline",
67687ebe1d17SDwaipayan Ray				"assume_aligned"		=> "__assume_aligned",
67697ebe1d17SDwaipayan Ray				"cold"				=> "__cold",
67707ebe1d17SDwaipayan Ray				"const"				=> "__attribute_const__",
67717ebe1d17SDwaipayan Ray				"copy"				=> "__copy",
67727ebe1d17SDwaipayan Ray				"designated_init"		=> "__designated_init",
67737ebe1d17SDwaipayan Ray				"externally_visible"		=> "__visible",
67747ebe1d17SDwaipayan Ray				"format"			=> "printf|scanf",
67757ebe1d17SDwaipayan Ray				"gnu_inline"			=> "__gnu_inline",
67767ebe1d17SDwaipayan Ray				"malloc"			=> "__malloc",
67777ebe1d17SDwaipayan Ray				"mode"				=> "__mode",
67787ebe1d17SDwaipayan Ray				"no_caller_saved_registers"	=> "__no_caller_saved_registers",
67797ebe1d17SDwaipayan Ray				"noclone"			=> "__noclone",
67807ebe1d17SDwaipayan Ray				"noinline"			=> "noinline",
67817ebe1d17SDwaipayan Ray				"nonstring"			=> "__nonstring",
67827ebe1d17SDwaipayan Ray				"noreturn"			=> "__noreturn",
67837ebe1d17SDwaipayan Ray				"packed"			=> "__packed",
67847ebe1d17SDwaipayan Ray				"pure"				=> "__pure",
6785339f29d9SJoe Perches				"section"			=> "__section",
67860830aab0SJoe Perches				"used"				=> "__used",
67870830aab0SJoe Perches				"weak"				=> "__weak"
67887ebe1d17SDwaipayan Ray			);
67897ebe1d17SDwaipayan Ray
67907ebe1d17SDwaipayan Ray			while ($attr =~ /\s*(\w+)\s*(${balanced_parens})?/g) {
6791339f29d9SJoe Perches				my $orig_attr = $1;
67927ebe1d17SDwaipayan Ray				my $params = '';
67937ebe1d17SDwaipayan Ray				$params = $2 if defined($2);
6794339f29d9SJoe Perches				my $curr_attr = $orig_attr;
67957ebe1d17SDwaipayan Ray				$curr_attr =~ s/^[\s_]+|[\s_]+$//g;
67967ebe1d17SDwaipayan Ray				if (exists($attr_list{$curr_attr})) {
6797339f29d9SJoe Perches					my $new = $attr_list{$curr_attr};
67987ebe1d17SDwaipayan Ray					if ($curr_attr eq "format" && $params) {
67997ebe1d17SDwaipayan Ray						$params =~ /^\s*\(\s*(\w+)\s*,\s*(.*)/;
6800339f29d9SJoe Perches						$new = "__$1\($2";
68017ebe1d17SDwaipayan Ray					} else {
6802339f29d9SJoe Perches						$new = "$new$params";
68037ebe1d17SDwaipayan Ray					}
68047ebe1d17SDwaipayan Ray					if (WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
6805339f29d9SJoe Perches						 "Prefer $new over __attribute__(($orig_attr$params))\n" . $herecurr) &&
68067ebe1d17SDwaipayan Ray					    $fix) {
6807339f29d9SJoe Perches						my $remove = "\Q$orig_attr\E" . '\s*' . "\Q$params\E" . '(?:\s*,\s*)?';
6808339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/$remove//;
6809339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\b__attribute__/$new __attribute__/;
6810339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/\}\Q$new\E/} $new/;
6811339f29d9SJoe Perches						$fixed[$fixlinenr] =~ s/ __attribute__\s*\(\s*\(\s*\)\s*\)//;
68127ebe1d17SDwaipayan Ray					}
681339b7e287SJoe Perches				}
6814462811d9SJoe Perches			}
6815462811d9SJoe Perches
68167ebe1d17SDwaipayan Ray			# Check for __attribute__ unused, prefer __always_unused or __maybe_unused
68177ebe1d17SDwaipayan Ray			if ($attr =~ /^_*unused/) {
68187ebe1d17SDwaipayan Ray				WARN("PREFER_DEFINED_ATTRIBUTE_MACRO",
68197ebe1d17SDwaipayan Ray				     "__always_unused or __maybe_unused is preferred over __attribute__((__unused__))\n" . $herecurr);
6820d5e616fcSJoe Perches			}
68216061d949SJoe Perches		}
68226061d949SJoe Perches
6823619a908aSJoe Perches# Check for __attribute__ weak, or __weak declarations (may have link issues)
68245b57980dSJoe Perches		if ($perl_version_ok &&
6825619a908aSJoe Perches		    $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
6826619a908aSJoe Perches		    ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
6827619a908aSJoe Perches		     $line =~ /\b__weak\b/)) {
6828619a908aSJoe Perches			ERROR("WEAK_DECLARATION",
6829619a908aSJoe Perches			      "Using weak declarations can have unintended link defects\n" . $herecurr);
6830619a908aSJoe Perches		}
6831619a908aSJoe Perches
6832fd39f904STomas Winkler# check for c99 types like uint8_t used outside of uapi/ and tools/
6833e6176fa4SJoe Perches		if ($realfile !~ m@\binclude/uapi/@ &&
6834fd39f904STomas Winkler		    $realfile !~ m@\btools/@ &&
6835e6176fa4SJoe Perches		    $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
6836e6176fa4SJoe Perches			my $type = $1;
6837e6176fa4SJoe Perches			if ($type =~ /\b($typeC99Typedefs)\b/) {
6838e6176fa4SJoe Perches				$type = $1;
6839e6176fa4SJoe Perches				my $kernel_type = 'u';
6840e6176fa4SJoe Perches				$kernel_type = 's' if ($type =~ /^_*[si]/);
6841e6176fa4SJoe Perches				$type =~ /(\d+)/;
6842e6176fa4SJoe Perches				$kernel_type .= $1;
6843e6176fa4SJoe Perches				if (CHK("PREFER_KERNEL_TYPES",
6844e6176fa4SJoe Perches					"Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
6845e6176fa4SJoe Perches				    $fix) {
6846e6176fa4SJoe Perches					$fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
6847e6176fa4SJoe Perches				}
6848e6176fa4SJoe Perches			}
6849e6176fa4SJoe Perches		}
6850e6176fa4SJoe Perches
6851938224b5SJoe Perches# check for cast of C90 native int or longer types constants
6852938224b5SJoe Perches		if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
6853938224b5SJoe Perches			my $cast = $1;
6854938224b5SJoe Perches			my $const = $2;
6855938224b5SJoe Perches			my $suffix = "";
6856938224b5SJoe Perches			my $newconst = $const;
6857938224b5SJoe Perches			$newconst =~ s/${Int_type}$//;
6858938224b5SJoe Perches			$suffix .= 'U' if ($cast =~ /\bunsigned\b/);
6859938224b5SJoe Perches			if ($cast =~ /\blong\s+long\b/) {
6860938224b5SJoe Perches			    $suffix .= 'LL';
6861938224b5SJoe Perches			} elsif ($cast =~ /\blong\b/) {
6862938224b5SJoe Perches			    $suffix .= 'L';
6863938224b5SJoe Perches			}
68640972b8bfSJoe Perches			if (WARN("TYPECAST_INT_CONSTANT",
68650972b8bfSJoe Perches				 "Unnecessary typecast of c90 int constant - '$cast$const' could be '$const$suffix'\n" . $herecurr) &&
68660972b8bfSJoe Perches			    $fix) {
6867938224b5SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
6868938224b5SJoe Perches			}
6869938224b5SJoe Perches		}
6870938224b5SJoe Perches
68718f53a9b8SJoe Perches# check for sizeof(&)
68728f53a9b8SJoe Perches		if ($line =~ /\bsizeof\s*\(\s*\&/) {
6873000d1cc1SJoe Perches			WARN("SIZEOF_ADDRESS",
6874000d1cc1SJoe Perches			     "sizeof(& should be avoided\n" . $herecurr);
68758f53a9b8SJoe Perches		}
68768f53a9b8SJoe Perches
687766c80b60SJoe Perches# check for sizeof without parenthesis
687866c80b60SJoe Perches		if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
6879d5e616fcSJoe Perches			if (WARN("SIZEOF_PARENTHESIS",
6880d5e616fcSJoe Perches				 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
6881d5e616fcSJoe Perches			    $fix) {
6882194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
6883d5e616fcSJoe Perches			}
688466c80b60SJoe Perches		}
688566c80b60SJoe Perches
688688982feaSJoe Perches# check for struct spinlock declarations
688788982feaSJoe Perches		if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
688888982feaSJoe Perches			WARN("USE_SPINLOCK_T",
688988982feaSJoe Perches			     "struct spinlock should be spinlock_t\n" . $herecurr);
689088982feaSJoe Perches		}
689188982feaSJoe Perches
6892a6962d72SJoe Perches# check for seq_printf uses that could be seq_puts
689306668727SJoe Perches		if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
6894a6962d72SJoe Perches			my $fmt = get_quoted_string($line, $rawline);
6895caac1d5fSHeba Aamer			$fmt =~ s/%%//g;
6896caac1d5fSHeba Aamer			if ($fmt !~ /%/) {
6897d5e616fcSJoe Perches				if (WARN("PREFER_SEQ_PUTS",
6898d5e616fcSJoe Perches					 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
6899d5e616fcSJoe Perches				    $fix) {
6900194f66fcSJoe Perches					$fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
6901d5e616fcSJoe Perches				}
6902a6962d72SJoe Perches			}
6903a6962d72SJoe Perches		}
6904a6962d72SJoe Perches
69050b523769SJoe Perches# check for vsprintf extension %p<foo> misuses
69065b57980dSJoe Perches		if ($perl_version_ok &&
69070b523769SJoe Perches		    defined $stat &&
69080b523769SJoe Perches		    $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
69090b523769SJoe Perches		    $1 !~ /^_*volatile_*$/) {
6910e3c6bc95STobin C. Harding			my $stat_real;
6911e3c6bc95STobin C. Harding
69120b523769SJoe Perches			my $lc = $stat =~ tr@\n@@;
69130b523769SJoe Perches			$lc = $lc + $linenr;
69140b523769SJoe Perches		        for (my $count = $linenr; $count <= $lc; $count++) {
6915ffe07513SJoe Perches				my $specifier;
6916ffe07513SJoe Perches				my $extension;
69173bd32d6aSSakari Ailus				my $qualifier;
6918ffe07513SJoe Perches				my $bad_specifier = "";
69190b523769SJoe Perches				my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
69200b523769SJoe Perches				$fmt =~ s/%%//g;
6921e3c6bc95STobin C. Harding
69223bd32d6aSSakari Ailus				while ($fmt =~ /(\%[\*\d\.]*p(\w)(\w*))/g) {
6923e3c6bc95STobin C. Harding					$specifier = $1;
6924e3c6bc95STobin C. Harding					$extension = $2;
69253bd32d6aSSakari Ailus					$qualifier = $3;
6926af612e43SSakari Ailus					if ($extension !~ /[4SsBKRraEehMmIiUDdgVCbGNOxtf]/ ||
69273bd32d6aSSakari Ailus					    ($extension eq "f" &&
6928af612e43SSakari Ailus					     defined $qualifier && $qualifier !~ /^w/) ||
6929af612e43SSakari Ailus					    ($extension eq "4" &&
6930af612e43SSakari Ailus					     defined $qualifier && $qualifier !~ /^cc/)) {
6931e3c6bc95STobin C. Harding						$bad_specifier = $specifier;
69320b523769SJoe Perches						last;
69330b523769SJoe Perches					}
6934e3c6bc95STobin C. Harding					if ($extension eq "x" && !defined($stat_real)) {
6935e3c6bc95STobin C. Harding						if (!defined($stat_real)) {
6936e3c6bc95STobin C. Harding							$stat_real = get_stat_real($linenr, $lc);
69370b523769SJoe Perches						}
6938e3c6bc95STobin C. Harding						WARN("VSPRINTF_SPECIFIER_PX",
6939e3c6bc95STobin C. Harding						     "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");
6940e3c6bc95STobin C. Harding					}
6941e3c6bc95STobin C. Harding				}
6942e3c6bc95STobin C. Harding				if ($bad_specifier ne "") {
69432a9f9d85STobin C. Harding					my $stat_real = get_stat_real($linenr, $lc);
6944de48fa1aSMiguel Ojeda					my $msg_level = \&WARN;
69451df7338aSSergey Senozhatsky					my $ext_type = "Invalid";
69461df7338aSSergey Senozhatsky					my $use = "";
6947e3c6bc95STobin C. Harding					if ($bad_specifier =~ /p[Ff]/) {
69481df7338aSSergey Senozhatsky						$use = " - use %pS instead";
6949e3c6bc95STobin C. Harding						$use =~ s/pS/ps/ if ($bad_specifier =~ /pf/);
6950de48fa1aSMiguel Ojeda					} elsif ($bad_specifier =~ /pA/) {
6951de48fa1aSMiguel Ojeda						$use =  " - '%pA' is only intended to be used from Rust code";
6952de48fa1aSMiguel Ojeda						$msg_level = \&ERROR;
69531df7338aSSergey Senozhatsky					}
69542a9f9d85STobin C. Harding
6955de48fa1aSMiguel Ojeda					&{$msg_level}("VSPRINTF_POINTER_EXTENSION",
6956e3c6bc95STobin C. Harding						      "$ext_type vsprintf pointer extension '$bad_specifier'$use\n" . "$here\n$stat_real\n");
6957e3c6bc95STobin C. Harding				}
69580b523769SJoe Perches			}
69590b523769SJoe Perches		}
69600b523769SJoe Perches
6961554e165cSAndy Whitcroft# Check for misused memsets
69625b57980dSJoe Perches		if ($perl_version_ok &&
6963d1fe9c09SJoe Perches		    defined $stat &&
69649e20a853SMateusz Kulikowski		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
6965554e165cSAndy Whitcroft
6966d7c76ba7SJoe Perches			my $ms_addr = $2;
6967d1fe9c09SJoe Perches			my $ms_val = $7;
6968d1fe9c09SJoe Perches			my $ms_size = $12;
6969d7c76ba7SJoe Perches
6970554e165cSAndy Whitcroft			if ($ms_size =~ /^(0x|)0$/i) {
6971554e165cSAndy Whitcroft				ERROR("MEMSET",
6972d7c76ba7SJoe Perches				      "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
6973554e165cSAndy Whitcroft			} elsif ($ms_size =~ /^(0x|)1$/i) {
6974554e165cSAndy Whitcroft				WARN("MEMSET",
6975d7c76ba7SJoe Perches				     "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
6976d7c76ba7SJoe Perches			}
6977d7c76ba7SJoe Perches		}
6978d7c76ba7SJoe Perches
697998a9bba5SJoe Perches# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
69805b57980dSJoe Perches#		if ($perl_version_ok &&
6981f333195dSJoe Perches#		    defined $stat &&
6982f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6983f333195dSJoe Perches#			if (WARN("PREFER_ETHER_ADDR_COPY",
6984f333195dSJoe Perches#				 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
6985f333195dSJoe Perches#			    $fix) {
6986f333195dSJoe Perches#				$fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
6987f333195dSJoe Perches#			}
6988f333195dSJoe Perches#		}
698998a9bba5SJoe Perches
6990b6117d17SMateusz Kulikowski# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
69915b57980dSJoe Perches#		if ($perl_version_ok &&
6992f333195dSJoe Perches#		    defined $stat &&
6993f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
6994f333195dSJoe Perches#			WARN("PREFER_ETHER_ADDR_EQUAL",
6995f333195dSJoe Perches#			     "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
6996f333195dSJoe Perches#		}
6997b6117d17SMateusz Kulikowski
69988617cd09SMateusz Kulikowski# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
69998617cd09SMateusz Kulikowski# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
70005b57980dSJoe Perches#		if ($perl_version_ok &&
7001f333195dSJoe Perches#		    defined $stat &&
7002f333195dSJoe Perches#		    $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
7003f333195dSJoe Perches#
7004f333195dSJoe Perches#			my $ms_val = $7;
7005f333195dSJoe Perches#
7006f333195dSJoe Perches#			if ($ms_val =~ /^(?:0x|)0+$/i) {
7007f333195dSJoe Perches#				if (WARN("PREFER_ETH_ZERO_ADDR",
7008f333195dSJoe Perches#					 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
7009f333195dSJoe Perches#				    $fix) {
7010f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
7011f333195dSJoe Perches#				}
7012f333195dSJoe Perches#			} elsif ($ms_val =~ /^(?:0xff|255)$/i) {
7013f333195dSJoe Perches#				if (WARN("PREFER_ETH_BROADCAST_ADDR",
7014f333195dSJoe Perches#					 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
7015f333195dSJoe Perches#				    $fix) {
7016f333195dSJoe Perches#					$fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
7017f333195dSJoe Perches#				}
7018f333195dSJoe Perches#			}
7019f333195dSJoe Perches#		}
70208617cd09SMateusz Kulikowski
7021d0f90841SKees Cook# strcpy uses that should likely be strscpy
7022d0f90841SKees Cook		if ($line =~ /\bstrcpy\s*\(/) {
7023d0f90841SKees Cook			WARN("STRCPY",
7024d0f90841SKees Cook			     "Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88\n" . $herecurr);
7025d0f90841SKees Cook		}
7026d0f90841SKees Cook
70275dbdb2d8SJoe Perches# strlcpy uses that should likely be strscpy
70285dbdb2d8SJoe Perches		if ($line =~ /\bstrlcpy\s*\(/) {
70295dbdb2d8SJoe Perches			WARN("STRLCPY",
7030d0f90841SKees Cook			     "Prefer strscpy over strlcpy - see: https://github.com/KSPP/linux/issues/89\n" . $herecurr);
7031d0f90841SKees Cook		}
7032d0f90841SKees Cook
7033d0f90841SKees Cook# strncpy uses that should likely be strscpy or strscpy_pad
7034d0f90841SKees Cook		if ($line =~ /\bstrncpy\s*\(/) {
7035d0f90841SKees Cook			WARN("STRNCPY",
7036d0f90841SKees Cook			     "Prefer strscpy, strscpy_pad, or __nonstring over strncpy - see: https://github.com/KSPP/linux/issues/90\n" . $herecurr);
70375dbdb2d8SJoe Perches		}
70385dbdb2d8SJoe Perches
70399b5f621cS[email protected]# ethtool_sprintf uses that should likely be ethtool_puts
70409b5f621cS[email protected]		if ($line =~ /\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
70419b5f621cS[email protected]			if (WARN("PREFER_ETHTOOL_PUTS",
70429b5f621cS[email protected]				 "Prefer ethtool_puts over ethtool_sprintf with only two arguments\n" . $herecurr) &&
70439b5f621cS[email protected]			    $fix) {
70449b5f621cS[email protected]				$fixed[$fixlinenr] =~ s/\bethtool_sprintf\s*\(\s*($FuncArg)\s*,\s*($FuncArg)/ethtool_puts($1, $7)/;
70459b5f621cS[email protected]			}
70469b5f621cS[email protected]		}
70479b5f621cS[email protected]
70489b5f621cS[email protected]		# use $rawline because $line loses %s via sanitization and thus we can't match against it.
70499b5f621cS[email protected]		if ($rawline =~ /\bethtool_sprintf\s*\(\s*$FuncArg\s*,\s*\"\%s\"\s*,\s*$FuncArg\s*\)/) {
70509b5f621cS[email protected]			if (WARN("PREFER_ETHTOOL_PUTS",
70519b5f621cS[email protected]				 "Prefer ethtool_puts over ethtool_sprintf with standalone \"%s\" specifier\n" . $herecurr) &&
70529b5f621cS[email protected]			    $fix) {
70539b5f621cS[email protected]				$fixed[$fixlinenr] =~ s/\bethtool_sprintf\s*\(\s*($FuncArg)\s*,\s*"\%s"\s*,\s*($FuncArg)/ethtool_puts($1, $7)/;
70549b5f621cS[email protected]			}
70559b5f621cS[email protected]		}
70569b5f621cS[email protected]
70579b5f621cS[email protected]
7058d7c76ba7SJoe Perches# typecasts on min/max could be min_t/max_t
70595b57980dSJoe Perches		if ($perl_version_ok &&
7060d1fe9c09SJoe Perches		    defined $stat &&
7061d7c76ba7SJoe Perches		    $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
7062d1fe9c09SJoe Perches			if (defined $2 || defined $7) {
7063d7c76ba7SJoe Perches				my $call = $1;
7064d7c76ba7SJoe Perches				my $cast1 = deparenthesize($2);
7065d7c76ba7SJoe Perches				my $arg1 = $3;
7066d1fe9c09SJoe Perches				my $cast2 = deparenthesize($7);
7067d1fe9c09SJoe Perches				my $arg2 = $8;
7068d7c76ba7SJoe Perches				my $cast;
7069d7c76ba7SJoe Perches
7070d1fe9c09SJoe Perches				if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
7071d7c76ba7SJoe Perches					$cast = "$cast1 or $cast2";
7072d7c76ba7SJoe Perches				} elsif ($cast1 ne "") {
7073d7c76ba7SJoe Perches					$cast = $cast1;
7074d7c76ba7SJoe Perches				} else {
7075d7c76ba7SJoe Perches					$cast = $cast2;
7076d7c76ba7SJoe Perches				}
7077d7c76ba7SJoe Perches				WARN("MINMAX",
7078d7c76ba7SJoe Perches				     "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
7079554e165cSAndy Whitcroft			}
7080554e165cSAndy Whitcroft		}
7081554e165cSAndy Whitcroft
70824a273195SJoe Perches# check usleep_range arguments
70835b57980dSJoe Perches		if ($perl_version_ok &&
70844a273195SJoe Perches		    defined $stat &&
70854a273195SJoe Perches		    $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
70864a273195SJoe Perches			my $min = $1;
70874a273195SJoe Perches			my $max = $7;
70884a273195SJoe Perches			if ($min eq $max) {
70894a273195SJoe Perches				WARN("USLEEP_RANGE",
7090458f69efSMauro Carvalho Chehab				     "usleep_range should not use min == max args; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
70914a273195SJoe Perches			} elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
70924a273195SJoe Perches				 $min > $max) {
70934a273195SJoe Perches				WARN("USLEEP_RANGE",
7094458f69efSMauro Carvalho Chehab				     "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.rst\n" . "$here\n$stat\n");
70954a273195SJoe Perches			}
70964a273195SJoe Perches		}
70974a273195SJoe Perches
7098823b794cSJoe Perches# check for naked sscanf
70995b57980dSJoe Perches		if ($perl_version_ok &&
7100823b794cSJoe Perches		    defined $stat &&
71016c8bd707SJoe Perches		    $line =~ /\bsscanf\b/ &&
7102823b794cSJoe Perches		    ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
7103823b794cSJoe Perches		     $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
7104823b794cSJoe Perches		     $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
7105823b794cSJoe Perches			my $lc = $stat =~ tr@\n@@;
7106823b794cSJoe Perches			$lc = $lc + $linenr;
71072a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
7108823b794cSJoe Perches			WARN("NAKED_SSCANF",
7109823b794cSJoe Perches			     "unchecked sscanf return value\n" . "$here\n$stat_real\n");
7110823b794cSJoe Perches		}
7111823b794cSJoe Perches
7112afc819abSJoe Perches# check for simple sscanf that should be kstrto<foo>
71135b57980dSJoe Perches		if ($perl_version_ok &&
7114afc819abSJoe Perches		    defined $stat &&
7115afc819abSJoe Perches		    $line =~ /\bsscanf\b/) {
7116afc819abSJoe Perches			my $lc = $stat =~ tr@\n@@;
7117afc819abSJoe Perches			$lc = $lc + $linenr;
71182a9f9d85STobin C. Harding			my $stat_real = get_stat_real($linenr, $lc);
7119afc819abSJoe Perches			if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
7120afc819abSJoe Perches				my $format = $6;
7121afc819abSJoe Perches				my $count = $format =~ tr@%@%@;
7122afc819abSJoe Perches				if ($count == 1 &&
7123afc819abSJoe Perches				    $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
7124afc819abSJoe Perches					WARN("SSCANF_TO_KSTRTO",
7125afc819abSJoe Perches					     "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
7126afc819abSJoe Perches				}
7127afc819abSJoe Perches			}
7128afc819abSJoe Perches		}
7129afc819abSJoe Perches
713070dc8a48SJoe Perches# check for new externs in .h files.
713170dc8a48SJoe Perches		if ($realfile =~ /\.h$/ &&
713270dc8a48SJoe Perches		    $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
7133d1d85780SJoe Perches			if (CHK("AVOID_EXTERNS",
713470dc8a48SJoe Perches				"extern prototypes should be avoided in .h files\n" . $herecurr) &&
713570dc8a48SJoe Perches			    $fix) {
7136194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
713770dc8a48SJoe Perches			}
713870dc8a48SJoe Perches		}
713970dc8a48SJoe Perches
7140de7d4f0eSAndy Whitcroft# check for new externs in .c files.
7141171ae1a4SAndy Whitcroft		if ($realfile =~ /\.c$/ && defined $stat &&
7142c45dcabdSAndy Whitcroft		    $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
7143171ae1a4SAndy Whitcroft		{
7144c45dcabdSAndy Whitcroft			my $function_name = $1;
7145c45dcabdSAndy Whitcroft			my $paren_space = $2;
7146171ae1a4SAndy Whitcroft
7147171ae1a4SAndy Whitcroft			my $s = $stat;
7148171ae1a4SAndy Whitcroft			if (defined $cond) {
7149171ae1a4SAndy Whitcroft				substr($s, 0, length($cond), '');
7150171ae1a4SAndy Whitcroft			}
7151d8b44b58SKees Cook			if ($s =~ /^\s*;/)
7152c45dcabdSAndy Whitcroft			{
7153000d1cc1SJoe Perches				WARN("AVOID_EXTERNS",
7154000d1cc1SJoe Perches				     "externs should be avoided in .c files\n" .  $herecurr);
7155de7d4f0eSAndy Whitcroft			}
7156de7d4f0eSAndy Whitcroft
7157171ae1a4SAndy Whitcroft			if ($paren_space =~ /\n/) {
7158000d1cc1SJoe Perches				WARN("FUNCTION_ARGUMENTS",
7159000d1cc1SJoe Perches				     "arguments for function declarations should follow identifier\n" . $herecurr);
7160171ae1a4SAndy Whitcroft			}
71619c9ba34eSAndy Whitcroft
71629c9ba34eSAndy Whitcroft		} elsif ($realfile =~ /\.c$/ && defined $stat &&
71635b2c7334SJim Cromie		    $stat =~ /^\+extern struct\s+(\w+)\s+(\w+)\[\];/)
71645b2c7334SJim Cromie		{
71655b2c7334SJim Cromie			my ($st_type, $st_name) = ($1, $2);
71665b2c7334SJim Cromie
71675b2c7334SJim Cromie			for my $s (keys %maybe_linker_symbol) {
71685b2c7334SJim Cromie			    #print "Linker symbol? $st_name : $s\n";
71695b2c7334SJim Cromie			    goto LIKELY_LINKER_SYMBOL
71705b2c7334SJim Cromie				if $st_name =~ /$s/;
71715b2c7334SJim Cromie			}
71725b2c7334SJim Cromie			WARN("AVOID_EXTERNS",
71735b2c7334SJim Cromie			     "found a file-scoped extern type:$st_type name:$st_name in .c file\n"
71745b2c7334SJim Cromie			     . "is this a linker symbol ?\n" . $herecurr);
71755b2c7334SJim Cromie		  LIKELY_LINKER_SYMBOL:
71765b2c7334SJim Cromie
71775b2c7334SJim Cromie		} elsif ($realfile =~ /\.c$/ && defined $stat &&
71789c9ba34eSAndy Whitcroft		    $stat =~ /^.\s*extern\s+/)
71799c9ba34eSAndy Whitcroft		{
7180000d1cc1SJoe Perches			WARN("AVOID_EXTERNS",
7181000d1cc1SJoe Perches			     "externs should be avoided in .c files\n" .  $herecurr);
7182171ae1a4SAndy Whitcroft		}
7183171ae1a4SAndy Whitcroft
7184a0ad7596SJoe Perches# check for function declarations that have arguments without identifier names
7185a0ad7596SJoe Perches		if (defined $stat &&
7186d8b44b58SKees Cook		    $stat =~ /^.\s*(?:extern\s+)?$Type\s*(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*\(\s*([^{]+)\s*\)\s*;/s &&
7187d8b44b58SKees Cook		    $1 ne "void") {
7188d8b44b58SKees Cook			my $args = trim($1);
7189ca0d8929SJoe Perches			while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
7190ca0d8929SJoe Perches				my $arg = trim($1);
7191d8b44b58SKees Cook				if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
7192ca0d8929SJoe Perches					WARN("FUNCTION_ARGUMENTS",
7193ca0d8929SJoe Perches					     "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
7194ca0d8929SJoe Perches				}
7195ca0d8929SJoe Perches			}
7196ca0d8929SJoe Perches		}
7197ca0d8929SJoe Perches
7198a0ad7596SJoe Perches# check for function definitions
71995b57980dSJoe Perches		if ($perl_version_ok &&
7200a0ad7596SJoe Perches		    defined $stat &&
7201a0ad7596SJoe Perches		    $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
7202a0ad7596SJoe Perches			$context_function = $1;
7203a0ad7596SJoe Perches
7204a0ad7596SJoe Perches# check for multiline function definition with misplaced open brace
7205a0ad7596SJoe Perches			my $ok = 0;
7206a0ad7596SJoe Perches			my $cnt = statement_rawlines($stat);
7207a0ad7596SJoe Perches			my $herectx = $here . "\n";
7208a0ad7596SJoe Perches			for (my $n = 0; $n < $cnt; $n++) {
7209a0ad7596SJoe Perches				my $rl = raw_line($linenr, $n);
7210a0ad7596SJoe Perches				$herectx .=  $rl . "\n";
7211a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /^[ \+]\{/);
7212a0ad7596SJoe Perches				$ok = 1 if ($rl =~ /\{/ && $n == 0);
7213a0ad7596SJoe Perches				last if $rl =~ /^[ \+].*\{/;
7214a0ad7596SJoe Perches			}
7215a0ad7596SJoe Perches			if (!$ok) {
7216a0ad7596SJoe Perches				ERROR("OPEN_BRACE",
7217a0ad7596SJoe Perches				      "open brace '{' following function definitions go on the next line\n" . $herectx);
7218a0ad7596SJoe Perches			}
7219a0ad7596SJoe Perches		}
7220a0ad7596SJoe Perches
7221de7d4f0eSAndy Whitcroft# checks for new __setup's
7222de7d4f0eSAndy Whitcroft		if ($rawline =~ /\b__setup\("([^"]*)"/) {
7223de7d4f0eSAndy Whitcroft			my $name = $1;
7224de7d4f0eSAndy Whitcroft
7225de7d4f0eSAndy Whitcroft			if (!grep(/$name/, @setup_docs)) {
7226000d1cc1SJoe Perches				CHK("UNDOCUMENTED_SETUP",
72272581ac7cSTim Froidcoeur				    "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.txt\n" . $herecurr);
7228de7d4f0eSAndy Whitcroft			}
7229653d4876SAndy Whitcroft		}
72309c0ca6f9SAndy Whitcroft
7231e29a70f1SJoe Perches# check for pointless casting of alloc functions
7232e29a70f1SJoe Perches		if ($line =~ /\*\s*\)\s*$allocFunctions\b/) {
7233000d1cc1SJoe Perches			WARN("UNNECESSARY_CASTS",
7234000d1cc1SJoe Perches			     "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
72359c0ca6f9SAndy Whitcroft		}
723613214adfSAndy Whitcroft
7237a640d25cSJoe Perches# alloc style
7238a640d25cSJoe Perches# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
72395b57980dSJoe Perches		if ($perl_version_ok &&
7240e29a70f1SJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k|v)[mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
7241a640d25cSJoe Perches			CHK("ALLOC_SIZEOF_STRUCT",
7242a640d25cSJoe Perches			    "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
7243a640d25cSJoe Perches		}
7244a640d25cSJoe Perches
724573f1d07eSGustavo A. R. Silva# check for (kv|k)[mz]alloc with multiplies that could be kmalloc_array/kvmalloc_array/kvcalloc/kcalloc
72465b57980dSJoe Perches		if ($perl_version_ok &&
72471b4a2ed4SJoe Perches		    defined $stat &&
724873f1d07eSGustavo A. R. Silva		    $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
724960a55369SJoe Perches			my $oldfunc = $3;
725060a55369SJoe Perches			my $a1 = $4;
725160a55369SJoe Perches			my $a2 = $10;
725260a55369SJoe Perches			my $newfunc = "kmalloc_array";
725373f1d07eSGustavo A. R. Silva			$newfunc = "kvmalloc_array" if ($oldfunc eq "kvmalloc");
725473f1d07eSGustavo A. R. Silva			$newfunc = "kvcalloc" if ($oldfunc eq "kvzalloc");
725560a55369SJoe Perches			$newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
725660a55369SJoe Perches			my $r1 = $a1;
725760a55369SJoe Perches			my $r2 = $a2;
725860a55369SJoe Perches			if ($a1 =~ /^sizeof\s*\S/) {
725960a55369SJoe Perches				$r1 = $a2;
726060a55369SJoe Perches				$r2 = $a1;
726160a55369SJoe Perches			}
7262e367455aSJoe Perches			if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
7263e367455aSJoe Perches			    !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
72641b4a2ed4SJoe Perches				my $cnt = statement_rawlines($stat);
7265e3d95a2aSTobin C. Harding				my $herectx = get_stat_here($linenr, $cnt, $here);
7266e3d95a2aSTobin C. Harding
7267e367455aSJoe Perches				if (WARN("ALLOC_WITH_MULTIPLY",
72681b4a2ed4SJoe Perches					 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
72691b4a2ed4SJoe Perches				    $cnt == 1 &&
7270e367455aSJoe Perches				    $fix) {
727173f1d07eSGustavo A. R. Silva					$fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*((?:kv|k)[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
727260a55369SJoe Perches				}
727360a55369SJoe Perches			}
727460a55369SJoe Perches		}
727560a55369SJoe Perches
7276972fdea2SJoe Perches# check for krealloc arg reuse
72775b57980dSJoe Perches		if ($perl_version_ok &&
72784cab63ceSJoe Perches		    $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*($Lval)\s*,/ &&
72794cab63ceSJoe Perches		    $1 eq $3) {
7280972fdea2SJoe Perches			WARN("KREALLOC_ARG_REUSE",
7281972fdea2SJoe Perches			     "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
7282972fdea2SJoe Perches		}
7283972fdea2SJoe Perches
72845ce59ae0SJoe Perches# check for alloc argument mismatch
72853965292aSLiao Chang		if ($line =~ /\b((?:devm_)?((?:k|kv)?(calloc|malloc_array)(?:_node)?))\s*\(\s*sizeof\b/) {
72865ce59ae0SJoe Perches			WARN("ALLOC_ARRAY_ARGS",
72875ce59ae0SJoe Perches			     "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
72885ce59ae0SJoe Perches		}
72895ce59ae0SJoe Perches
7290caf2a54fSJoe Perches# check for multiple semicolons
7291caf2a54fSJoe Perches		if ($line =~ /;\s*;\s*$/) {
7292d5e616fcSJoe Perches			if (WARN("ONE_SEMICOLON",
7293d5e616fcSJoe Perches				 "Statements terminations use 1 semicolon\n" . $herecurr) &&
7294d5e616fcSJoe Perches			    $fix) {
7295194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
7296d5e616fcSJoe Perches			}
7297d1e2ad07SJoe Perches		}
7298d1e2ad07SJoe Perches
7299cec3aaa5STomas Winkler# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
7300cec3aaa5STomas Winkler		if ($realfile !~ m@^include/uapi/@ &&
7301cec3aaa5STomas Winkler		    $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
73020ab90191SJoe Perches			my $ull = "";
73030ab90191SJoe Perches			$ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
73040ab90191SJoe Perches			if (CHK("BIT_MACRO",
73050ab90191SJoe Perches				"Prefer using the BIT$ull macro\n" . $herecurr) &&
73060ab90191SJoe Perches			    $fix) {
73070ab90191SJoe Perches				$fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
73080ab90191SJoe Perches			}
73090ab90191SJoe Perches		}
73100ab90191SJoe Perches
731150161266SJoe Perches# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
73123e89ad85SJerome Forissier		if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^${CONFIG_}/) {
731350161266SJoe Perches			WARN("IS_ENABLED_CONFIG",
73143e89ad85SJerome Forissier			     "IS_ENABLED($1) is normally used as IS_ENABLED(${CONFIG_}$1)\n" . $herecurr);
731550161266SJoe Perches		}
731650161266SJoe Perches
73172d632745SJoe Perches# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
73183e89ad85SJerome Forissier		if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(${CONFIG_}[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
73192d632745SJoe Perches			my $config = $1;
73202d632745SJoe Perches			if (WARN("PREFER_IS_ENABLED",
73213e89ad85SJerome Forissier				 "Prefer IS_ENABLED(<FOO>) to ${CONFIG_}<FOO> || ${CONFIG_}<FOO>_MODULE\n" . $herecurr) &&
73222d632745SJoe Perches			    $fix) {
73232d632745SJoe Perches				$fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
73242d632745SJoe Perches			}
73252d632745SJoe Perches		}
73262d632745SJoe Perches
7327f36d3eb8SJoe Perches# check for /* fallthrough */ like comment, prefer fallthrough;
7328f36d3eb8SJoe Perches		my @fallthroughs = (
7329f36d3eb8SJoe Perches			'fallthrough',
7330f36d3eb8SJoe Perches			'@fallthrough@',
7331f36d3eb8SJoe Perches			'lint -fallthrough[ \t]*',
7332f36d3eb8SJoe Perches			'intentional(?:ly)?[ \t]*fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)',
7333f36d3eb8SJoe Perches			'(?:else,?\s*)?FALL(?:S | |-)?THR(?:OUGH|U|EW)[ \t.!]*(?:-[^\n\r]*)?',
7334f36d3eb8SJoe Perches			'Fall(?:(?:s | |-)[Tt]|t)hr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7335f36d3eb8SJoe Perches			'fall(?:s | |-)?thr(?:ough|u|ew)[ \t.!]*(?:-[^\n\r]*)?',
7336f36d3eb8SJoe Perches		    );
7337f36d3eb8SJoe Perches		if ($raw_comment ne '') {
7338f36d3eb8SJoe Perches			foreach my $ft (@fallthroughs) {
7339f36d3eb8SJoe Perches				if ($raw_comment =~ /$ft/) {
7340f36d3eb8SJoe Perches					my $msg_level = \&WARN;
7341f36d3eb8SJoe Perches					$msg_level = \&CHK if ($file);
7342f36d3eb8SJoe Perches					&{$msg_level}("PREFER_FALLTHROUGH",
7343f36d3eb8SJoe Perches						      "Prefer 'fallthrough;' over fallthrough comment\n" . $herecurr);
7344f36d3eb8SJoe Perches					last;
7345f36d3eb8SJoe Perches				}
7346f36d3eb8SJoe Perches			}
7347f36d3eb8SJoe Perches		}
7348f36d3eb8SJoe Perches
7349d1e2ad07SJoe Perches# check for switch/default statements without a break;
73505b57980dSJoe Perches		if ($perl_version_ok &&
7351d1e2ad07SJoe Perches		    defined $stat &&
7352d1e2ad07SJoe Perches		    $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
7353d1e2ad07SJoe Perches			my $cnt = statement_rawlines($stat);
7354e3d95a2aSTobin C. Harding			my $herectx = get_stat_here($linenr, $cnt, $here);
7355e3d95a2aSTobin C. Harding
7356d1e2ad07SJoe Perches			WARN("DEFAULT_NO_BREAK",
7357d1e2ad07SJoe Perches			     "switch default: should use break\n" . $herectx);
7358caf2a54fSJoe Perches		}
7359caf2a54fSJoe Perches
736013214adfSAndy Whitcroft# check for gcc specific __FUNCTION__
7361d5e616fcSJoe Perches		if ($line =~ /\b__FUNCTION__\b/) {
7362d5e616fcSJoe Perches			if (WARN("USE_FUNC",
7363d5e616fcSJoe Perches				 "__func__ should be used instead of gcc specific __FUNCTION__\n"  . $herecurr) &&
7364d5e616fcSJoe Perches			    $fix) {
7365194f66fcSJoe Perches				$fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
7366d5e616fcSJoe Perches			}
736713214adfSAndy Whitcroft		}
7368773647a0SAndy Whitcroft
736962ec818fSJoe Perches# check for uses of __DATE__, __TIME__, __TIMESTAMP__
737062ec818fSJoe Perches		while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
737162ec818fSJoe Perches			ERROR("DATE_TIME",
737262ec818fSJoe Perches			      "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
737362ec818fSJoe Perches		}
737462ec818fSJoe Perches
73752c92488aSJoe Perches# check for use of yield()
73762c92488aSJoe Perches		if ($line =~ /\byield\s*\(\s*\)/) {
73772c92488aSJoe Perches			WARN("YIELD",
73782c92488aSJoe Perches			     "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n"  . $herecurr);
73792c92488aSJoe Perches		}
73802c92488aSJoe Perches
7381179f8f40SJoe Perches# check for comparisons against true and false
7382179f8f40SJoe Perches		if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
7383179f8f40SJoe Perches			my $lead = $1;
7384179f8f40SJoe Perches			my $arg = $2;
7385179f8f40SJoe Perches			my $test = $3;
7386179f8f40SJoe Perches			my $otype = $4;
7387179f8f40SJoe Perches			my $trail = $5;
7388179f8f40SJoe Perches			my $op = "!";
7389179f8f40SJoe Perches
7390179f8f40SJoe Perches			($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
7391179f8f40SJoe Perches
7392179f8f40SJoe Perches			my $type = lc($otype);
7393179f8f40SJoe Perches			if ($type =~ /^(?:true|false)$/) {
7394179f8f40SJoe Perches				if (("$test" eq "==" && "$type" eq "true") ||
7395179f8f40SJoe Perches				    ("$test" eq "!=" && "$type" eq "false")) {
7396179f8f40SJoe Perches					$op = "";
7397179f8f40SJoe Perches				}
7398179f8f40SJoe Perches
7399179f8f40SJoe Perches				CHK("BOOL_COMPARISON",
7400179f8f40SJoe Perches				    "Using comparison to $otype is error prone\n" . $herecurr);
7401179f8f40SJoe Perches
7402179f8f40SJoe Perches## maybe suggesting a correct construct would better
7403179f8f40SJoe Perches##				    "Using comparison to $otype is error prone.  Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
7404179f8f40SJoe Perches
7405179f8f40SJoe Perches			}
7406179f8f40SJoe Perches		}
7407179f8f40SJoe Perches
74084882720bSThomas Gleixner# check for semaphores initialized locked
74094882720bSThomas Gleixner		if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
7410000d1cc1SJoe Perches			WARN("CONSIDER_COMPLETION",
7411000d1cc1SJoe Perches			     "consider using a completion\n" . $herecurr);
7412773647a0SAndy Whitcroft		}
74136712d858SJoe Perches
741467d0a075SJoe Perches# recommend kstrto* over simple_strto* and strict_strto*
741567d0a075SJoe Perches		if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
7416000d1cc1SJoe Perches			WARN("CONSIDER_KSTRTO",
741767d0a075SJoe Perches			     "$1 is obsolete, use k$3 instead\n" . $herecurr);
7418773647a0SAndy Whitcroft		}
74196712d858SJoe Perches
7420ae3ccc46SFabian Frederick# check for __initcall(), use device_initcall() explicitly or more appropriate function please
7421f3db6639SMichael Ellerman		if ($line =~ /^.\s*__initcall\s*\(/) {
7422000d1cc1SJoe Perches			WARN("USE_DEVICE_INITCALL",
7423ae3ccc46SFabian Frederick			     "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
7424f3db6639SMichael Ellerman		}
74256712d858SJoe Perches
74263d709ab5SPaul E. McKenney# check for spin_is_locked(), suggest lockdep instead
74273d709ab5SPaul E. McKenney		if ($line =~ /\bspin_is_locked\(/) {
74283d709ab5SPaul E. McKenney			WARN("USE_LOCKDEP",
74293d709ab5SPaul E. McKenney			     "Where possible, use lockdep_assert_held instead of assertions based on spin_is_locked\n" . $herecurr);
74303d709ab5SPaul E. McKenney		}
74313d709ab5SPaul E. McKenney
74329189c7e7SJoe Perches# check for deprecated apis
74339189c7e7SJoe Perches		if ($line =~ /\b($deprecated_apis_search)\b\s*\(/) {
74349189c7e7SJoe Perches			my $deprecated_api = $1;
74359189c7e7SJoe Perches			my $new_api = $deprecated_apis{$deprecated_api};
74369189c7e7SJoe Perches			WARN("DEPRECATED_API",
74379189c7e7SJoe Perches			     "Deprecated use of '$deprecated_api', prefer '$new_api' instead\n" . $herecurr);
74389189c7e7SJoe Perches		}
74399189c7e7SJoe Perches
74400f3c5aabSJoe Perches# check for various structs that are normally const (ops, kgdb, device_tree)
7441d9190e4eSJoe Perches# and avoid what seem like struct definitions 'struct foo {'
7442ced69da1SQuentin Monnet		if (defined($const_structs) &&
7443ced69da1SQuentin Monnet		    $line !~ /\bconst\b/ &&
7444d9190e4eSJoe Perches		    $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
7445000d1cc1SJoe Perches			WARN("CONST_STRUCT",
7446d9190e4eSJoe Perches			     "struct $1 should normally be const\n" . $herecurr);
74472b6db5cbSAndy Whitcroft		}
7448773647a0SAndy Whitcroft
7449773647a0SAndy Whitcroft# use of NR_CPUS is usually wrong
7450773647a0SAndy Whitcroft# ignore definitions of NR_CPUS and usage to define arrays as likely right
745135cdcbfcSPeng Wang# ignore designated initializers using NR_CPUS
7452773647a0SAndy Whitcroft		if ($line =~ /\bNR_CPUS\b/ &&
7453c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
7454c45dcabdSAndy Whitcroft		    $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
7455171ae1a4SAndy Whitcroft		    $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
7456171ae1a4SAndy Whitcroft		    $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
745735cdcbfcSPeng Wang		    $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/ &&
745835cdcbfcSPeng Wang		    $line !~ /^.\s*\.\w+\s*=\s*.*\bNR_CPUS\b/)
7459773647a0SAndy Whitcroft		{
7460000d1cc1SJoe Perches			WARN("NR_CPUS",
7461000d1cc1SJoe Perches			     "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
7462773647a0SAndy Whitcroft		}
74639c9ba34eSAndy Whitcroft
746452ea8506SJoe Perches# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
746552ea8506SJoe Perches		if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
746652ea8506SJoe Perches			ERROR("DEFINE_ARCH_HAS",
746752ea8506SJoe Perches			      "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
746852ea8506SJoe Perches		}
746952ea8506SJoe Perches
7470acd9362cSJoe Perches# likely/unlikely comparisons similar to "(likely(foo) > 0)"
74715b57980dSJoe Perches		if ($perl_version_ok &&
7472acd9362cSJoe Perches		    $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
7473acd9362cSJoe Perches			WARN("LIKELY_MISUSE",
7474acd9362cSJoe Perches			     "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
7475acd9362cSJoe Perches		}
7476acd9362cSJoe Perches
7477fbe74541SJoe Perches# return sysfs_emit(foo, fmt, ...) fmt without newline
7478fbe74541SJoe Perches		if ($line =~ /\breturn\s+sysfs_emit\s*\(\s*$FuncArg\s*,\s*($String)/ &&
7479fbe74541SJoe Perches		    substr($rawline, $-[6], $+[6] - $-[6]) !~ /\\n"$/) {
7480fbe74541SJoe Perches			my $offset = $+[6] - 1;
7481fbe74541SJoe Perches			if (WARN("SYSFS_EMIT",
7482fbe74541SJoe Perches				 "return sysfs_emit(...) formats should include a terminating newline\n" . $herecurr) &&
7483fbe74541SJoe Perches			    $fix) {
7484fbe74541SJoe Perches				substr($fixed[$fixlinenr], $offset, 0) = '\\n';
7485fbe74541SJoe Perches			}
7486fbe74541SJoe Perches		}
7487fbe74541SJoe Perches
74888515e4a7SKees Cook# check for array definition/declarations that should use flexible arrays instead
74898515e4a7SKees Cook		if ($sline =~ /^[\+ ]\s*\}(?:\s*__packed)?\s*;\s*$/ &&
74908515e4a7SKees Cook		    $prevline =~ /^\+\s*(?:\}(?:\s*__packed\s*)?|$Type)\s*$Ident\s*\[\s*(0|1)\s*\]\s*;\s*$/) {
74918515e4a7SKees Cook			if (ERROR("FLEXIBLE_ARRAY",
74928515e4a7SKees Cook				  "Use C99 flexible arrays - see https://docs.kernel.org/process/deprecated.html#zero-length-and-one-element-arrays\n" . $hereprev) &&
74938515e4a7SKees Cook			    $1 == '0' && $fix) {
74948515e4a7SKees Cook				$fixed[$fixlinenr - 1] =~ s/\[\s*0\s*\]/[]/;
74958515e4a7SKees Cook			}
74968515e4a7SKees Cook		}
74978515e4a7SKees Cook
7498de3f186fSDenis Efremov# nested likely/unlikely calls
7499de3f186fSDenis Efremov		if ($line =~ /\b(?:(?:un)?likely)\s*\(\s*!?\s*(IS_ERR(?:_OR_NULL|_VALUE)?|WARN)/) {
7500de3f186fSDenis Efremov			WARN("LIKELY_MISUSE",
7501de3f186fSDenis Efremov			     "nested (un)?likely() calls, $1 already uses unlikely() internally\n" . $herecurr);
7502de3f186fSDenis Efremov		}
7503de3f186fSDenis Efremov
7504691d77b6SAndy Whitcroft# whine mightly about in_atomic
7505691d77b6SAndy Whitcroft		if ($line =~ /\bin_atomic\s*\(/) {
7506691d77b6SAndy Whitcroft			if ($realfile =~ m@^drivers/@) {
7507000d1cc1SJoe Perches				ERROR("IN_ATOMIC",
7508000d1cc1SJoe Perches				      "do not use in_atomic in drivers\n" . $herecurr);
7509f4a87736SAndy Whitcroft			} elsif ($realfile !~ m@^kernel/@) {
7510000d1cc1SJoe Perches				WARN("IN_ATOMIC",
7511000d1cc1SJoe Perches				     "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
7512691d77b6SAndy Whitcroft			}
7513691d77b6SAndy Whitcroft		}
75141704f47bSPeter Zijlstra
751584dd7f19SPaul E. McKenney# Complain about RCU Tasks Trace used outside of BPF (and of course, RCU).
751684dd7f19SPaul E. McKenney		our $rcu_trace_funcs = qr{(?x:
751784dd7f19SPaul E. McKenney			rcu_read_lock_trace |
751884dd7f19SPaul E. McKenney			rcu_read_lock_trace_held |
751984dd7f19SPaul E. McKenney			rcu_read_unlock_trace |
752084dd7f19SPaul E. McKenney			call_rcu_tasks_trace |
752184dd7f19SPaul E. McKenney			synchronize_rcu_tasks_trace |
752284dd7f19SPaul E. McKenney			rcu_barrier_tasks_trace |
752384dd7f19SPaul E. McKenney			rcu_request_urgent_qs_task
752484dd7f19SPaul E. McKenney		)};
752584dd7f19SPaul E. McKenney		our $rcu_trace_paths = qr{(?x:
752684dd7f19SPaul E. McKenney			kernel/bpf/ |
752784dd7f19SPaul E. McKenney			include/linux/bpf |
752884dd7f19SPaul E. McKenney			net/bpf/ |
752984dd7f19SPaul E. McKenney			kernel/rcu/ |
753084dd7f19SPaul E. McKenney			include/linux/rcu
753184dd7f19SPaul E. McKenney		)};
753284dd7f19SPaul E. McKenney		if ($line =~ /\b($rcu_trace_funcs)\s*\(/) {
753384dd7f19SPaul E. McKenney			if ($realfile !~ m{^$rcu_trace_paths}) {
753484dd7f19SPaul E. McKenney				WARN("RCU_TASKS_TRACE",
753584dd7f19SPaul E. McKenney				     "use of RCU tasks trace is incorrect outside BPF or core RCU code\n" . $herecurr);
753684dd7f19SPaul E. McKenney			}
753784dd7f19SPaul E. McKenney		}
753884dd7f19SPaul E. McKenney
75391704f47bSPeter Zijlstra# check for lockdep_set_novalidate_class
75401704f47bSPeter Zijlstra		if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
75411704f47bSPeter Zijlstra		    $line =~ /__lockdep_no_validate__\s*\)/ ) {
75421704f47bSPeter Zijlstra			if ($realfile !~ m@^kernel/lockdep@ &&
75431704f47bSPeter Zijlstra			    $realfile !~ m@^include/linux/lockdep@ &&
75441704f47bSPeter Zijlstra			    $realfile !~ m@^drivers/base/core@) {
7545000d1cc1SJoe Perches				ERROR("LOCKDEP",
7546000d1cc1SJoe Perches				      "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
75471704f47bSPeter Zijlstra			}
75481704f47bSPeter Zijlstra		}
754988f8831cSDave Jones
7550b392c64fSJoe Perches		if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
7551b392c64fSJoe Perches		    $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
7552000d1cc1SJoe Perches			WARN("EXPORTED_WORLD_WRITABLE",
7553000d1cc1SJoe Perches			     "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
755488f8831cSDave Jones		}
75552435880fSJoe Perches
755600180468SJoe Perches# check for DEVICE_ATTR uses that could be DEVICE_ATTR_<FOO>
755700180468SJoe Perches# and whether or not function naming is typical and if
755800180468SJoe Perches# DEVICE_ATTR permissions uses are unusual too
75595b57980dSJoe Perches		if ($perl_version_ok &&
756000180468SJoe Perches		    defined $stat &&
756100180468SJoe Perches		    $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*\)/) {
756200180468SJoe Perches			my $var = $1;
756300180468SJoe Perches			my $perms = $2;
756400180468SJoe Perches			my $show = $3;
756500180468SJoe Perches			my $store = $4;
756600180468SJoe Perches			my $octal_perms = perms_to_octal($perms);
756700180468SJoe Perches			if ($show =~ /^${var}_show$/ &&
756800180468SJoe Perches			    $store =~ /^${var}_store$/ &&
756900180468SJoe Perches			    $octal_perms eq "0644") {
757000180468SJoe Perches				if (WARN("DEVICE_ATTR_RW",
757100180468SJoe Perches					 "Use DEVICE_ATTR_RW\n" . $herecurr) &&
757200180468SJoe Perches				    $fix) {
757300180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*$store\s*\)/DEVICE_ATTR_RW(${var})/;
757400180468SJoe Perches				}
757500180468SJoe Perches			} elsif ($show =~ /^${var}_show$/ &&
757600180468SJoe Perches				 $store =~ /^NULL$/ &&
757700180468SJoe Perches				 $octal_perms eq "0444") {
757800180468SJoe Perches				if (WARN("DEVICE_ATTR_RO",
757900180468SJoe Perches					 "Use DEVICE_ATTR_RO\n" . $herecurr) &&
758000180468SJoe Perches				    $fix) {
758100180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*$show\s*,\s*NULL\s*\)/DEVICE_ATTR_RO(${var})/;
758200180468SJoe Perches				}
758300180468SJoe Perches			} elsif ($show =~ /^NULL$/ &&
758400180468SJoe Perches				 $store =~ /^${var}_store$/ &&
758500180468SJoe Perches				 $octal_perms eq "0200") {
758600180468SJoe Perches				if (WARN("DEVICE_ATTR_WO",
758700180468SJoe Perches					 "Use DEVICE_ATTR_WO\n" . $herecurr) &&
758800180468SJoe Perches				    $fix) {
758900180468SJoe Perches					$fixed[$fixlinenr] =~ s/\bDEVICE_ATTR\s*\(\s*$var\s*,\s*\Q$perms\E\s*,\s*NULL\s*,\s*$store\s*\)/DEVICE_ATTR_WO(${var})/;
759000180468SJoe Perches				}
759100180468SJoe Perches			} elsif ($octal_perms eq "0644" ||
759200180468SJoe Perches				 $octal_perms eq "0444" ||
759300180468SJoe Perches				 $octal_perms eq "0200") {
759400180468SJoe Perches				my $newshow = "$show";
759500180468SJoe Perches				$newshow = "${var}_show" if ($show ne "NULL" && $show ne "${var}_show");
759600180468SJoe Perches				my $newstore = $store;
759700180468SJoe Perches				$newstore = "${var}_store" if ($store ne "NULL" && $store ne "${var}_store");
759800180468SJoe Perches				my $rename = "";
759900180468SJoe Perches				if ($show ne $newshow) {
760000180468SJoe Perches					$rename .= " '$show' to '$newshow'";
760100180468SJoe Perches				}
760200180468SJoe Perches				if ($store ne $newstore) {
760300180468SJoe Perches					$rename .= " '$store' to '$newstore'";
760400180468SJoe Perches				}
760500180468SJoe Perches				WARN("DEVICE_ATTR_FUNCTIONS",
760600180468SJoe Perches				     "Consider renaming function(s)$rename\n" . $herecurr);
760700180468SJoe Perches			} else {
760800180468SJoe Perches				WARN("DEVICE_ATTR_PERMS",
760900180468SJoe Perches				     "DEVICE_ATTR unusual permissions '$perms' used\n" . $herecurr);
761000180468SJoe Perches			}
761100180468SJoe Perches		}
761200180468SJoe Perches
7613515a235eSJoe Perches# Mode permission misuses where it seems decimal should be octal
7614515a235eSJoe Perches# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
761573121534SJoe Perches# o Ignore module_param*(...) uses with a decimal 0 permission as that has a
761673121534SJoe Perches#   specific definition of not visible in sysfs.
761773121534SJoe Perches# o Ignore proc_create*(...) uses with a decimal 0 permission as that means
761873121534SJoe Perches#   use the default permissions
76195b57980dSJoe Perches		if ($perl_version_ok &&
7620459cf0aeSJoe Perches		    defined $stat &&
7621515a235eSJoe Perches		    $line =~ /$mode_perms_search/) {
76222435880fSJoe Perches			foreach my $entry (@mode_permission_funcs) {
76232435880fSJoe Perches				my $func = $entry->[0];
76242435880fSJoe Perches				my $arg_pos = $entry->[1];
76252435880fSJoe Perches
7626459cf0aeSJoe Perches				my $lc = $stat =~ tr@\n@@;
7627459cf0aeSJoe Perches				$lc = $lc + $linenr;
76282a9f9d85STobin C. Harding				my $stat_real = get_stat_real($linenr, $lc);
7629459cf0aeSJoe Perches
76302435880fSJoe Perches				my $skip_args = "";
76312435880fSJoe Perches				if ($arg_pos > 1) {
76322435880fSJoe Perches					$arg_pos--;
76332435880fSJoe Perches					$skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
76342435880fSJoe Perches				}
7635f90774e1SJoe Perches				my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
7636459cf0aeSJoe Perches				if ($stat =~ /$test/) {
76372435880fSJoe Perches					my $val = $1;
76382435880fSJoe Perches					$val = $6 if ($skip_args ne "");
763973121534SJoe Perches					if (!($func =~ /^(?:module_param|proc_create)/ && $val eq "0") &&
764073121534SJoe Perches					    (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
764173121534SJoe Perches					     ($val =~ /^$Octal$/ && length($val) ne 4))) {
76422435880fSJoe Perches						ERROR("NON_OCTAL_PERMISSIONS",
7643459cf0aeSJoe Perches						      "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
7644f90774e1SJoe Perches					}
7645f90774e1SJoe Perches					if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
7646c0a5c898SJoe Perches						ERROR("EXPORTED_WORLD_WRITABLE",
7647459cf0aeSJoe Perches						      "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
76482435880fSJoe Perches					}
7649459cf0aeSJoe Perches				}
7650459cf0aeSJoe Perches			}
7651459cf0aeSJoe Perches		}
7652459cf0aeSJoe Perches
7653459cf0aeSJoe Perches# check for uses of S_<PERMS> that could be octal for readability
7654bc22d9a7SJoe Perches		while ($line =~ m{\b($multi_mode_perms_string_search)\b}g) {
765500180468SJoe Perches			my $oval = $1;
765600180468SJoe Perches			my $octal = perms_to_octal($oval);
7657f90774e1SJoe Perches			if (WARN("SYMBOLIC_PERMS",
7658459cf0aeSJoe Perches				 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
7659f90774e1SJoe Perches			    $fix) {
766000180468SJoe Perches				$fixed[$fixlinenr] =~ s/\Q$oval\E/$octal/;
76612435880fSJoe Perches			}
766213214adfSAndy Whitcroft		}
76635a6d20ceSBjorn Andersson
76645a6d20ceSBjorn Andersson# validate content of MODULE_LICENSE against list from include/linux/module.h
76655a6d20ceSBjorn Andersson		if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
76665a6d20ceSBjorn Andersson			my $extracted_string = get_quoted_string($line, $rawline);
76675a6d20ceSBjorn Andersson			my $valid_licenses = qr{
76685a6d20ceSBjorn Andersson						GPL|
76695a6d20ceSBjorn Andersson						GPL\ v2|
76705a6d20ceSBjorn Andersson						GPL\ and\ additional\ rights|
76715a6d20ceSBjorn Andersson						Dual\ BSD/GPL|
76725a6d20ceSBjorn Andersson						Dual\ MIT/GPL|
76735a6d20ceSBjorn Andersson						Dual\ MPL/GPL|
76745a6d20ceSBjorn Andersson						Proprietary
76755a6d20ceSBjorn Andersson					}x;
76765a6d20ceSBjorn Andersson			if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
76775a6d20ceSBjorn Andersson				WARN("MODULE_LICENSE",
76785a6d20ceSBjorn Andersson				     "unknown module license " . $extracted_string . "\n" . $herecurr);
76795a6d20ceSBjorn Andersson			}
76806e8f42dcSJoe Perches			if (!$file && $extracted_string eq '"GPL v2"') {
76816e8f42dcSJoe Perches				if (WARN("MODULE_LICENSE",
76826e8f42dcSJoe Perches				     "Prefer \"GPL\" over \"GPL v2\" - see commit bf7fbeeae6db (\"module: Cure the MODULE_LICENSE \"GPL\" vs. \"GPL v2\" bogosity\")\n" . $herecurr) &&
76836e8f42dcSJoe Perches				    $fix) {
76846e8f42dcSJoe Perches					$fixed[$fixlinenr] =~ s/\bMODULE_LICENSE\s*\(\s*"GPL v2"\s*\)/MODULE_LICENSE("GPL")/;
76856e8f42dcSJoe Perches				}
76866e8f42dcSJoe Perches			}
76875a6d20ceSBjorn Andersson		}
76886a8d76cbSMatteo Croce
76896a8d76cbSMatteo Croce# check for sysctl duplicate constants
76906a8d76cbSMatteo Croce		if ($line =~ /\.extra[12]\s*=\s*&(zero|one|int_max)\b/) {
76916a8d76cbSMatteo Croce			WARN("DUPLICATED_SYSCTL_CONST",
76926a8d76cbSMatteo Croce				"duplicated sysctl range checking value '$1', consider using the shared one in include/linux/sysctl.h\n" . $herecurr);
76936a8d76cbSMatteo Croce		}
7694515a235eSJoe Perches	}
769513214adfSAndy Whitcroft
769613214adfSAndy Whitcroft	# If we have no input at all, then there is nothing to report on
769713214adfSAndy Whitcroft	# so just keep quiet.
769813214adfSAndy Whitcroft	if ($#rawlines == -1) {
769913214adfSAndy Whitcroft		exit(0);
77000a920b5bSAndy Whitcroft	}
77010a920b5bSAndy Whitcroft
77028905a67cSAndy Whitcroft	# In mailback mode only produce a report in the negative, for
77038905a67cSAndy Whitcroft	# things that appear to be patches.
77048905a67cSAndy Whitcroft	if ($mailback && ($clean == 1 || !$is_patch)) {
77058905a67cSAndy Whitcroft		exit(0);
77068905a67cSAndy Whitcroft	}
77078905a67cSAndy Whitcroft
7708e73d2715SDwaipayan Ray	# This is not a patch, and we are in 'no-patch' mode so
77098905a67cSAndy Whitcroft	# just keep quiet.
77108905a67cSAndy Whitcroft	if (!$chk_patch && !$is_patch) {
77118905a67cSAndy Whitcroft		exit(0);
77128905a67cSAndy Whitcroft	}
77138905a67cSAndy Whitcroft
7714a08ffbefSStafford Horne	if (!$is_patch && $filename !~ /cover-letter\.patch$/) {
7715000d1cc1SJoe Perches		ERROR("NOT_UNIFIED_DIFF",
7716000d1cc1SJoe Perches		      "Does not appear to be a unified-diff format patch\n");
77170a920b5bSAndy Whitcroft	}
7718*d5d6281aSDan Carpenter	if ($is_patch && $has_commit_log && $chk_fixes_tag) {
7719*d5d6281aSDan Carpenter		if ($needs_fixes_tag ne "" && !$is_revert && !$fixes_tag) {
7720*d5d6281aSDan Carpenter			WARN("MISSING_FIXES_TAG",
7721*d5d6281aSDan Carpenter				 "The commit message has '$needs_fixes_tag', perhaps it also needs a 'Fixes:' tag?\n");
7722*d5d6281aSDan Carpenter		}
7723*d5d6281aSDan Carpenter	}
7724cd261496SGeert Uytterhoeven	if ($is_patch && $has_commit_log && $chk_signoff) {
7725cd261496SGeert Uytterhoeven		if ($signoff == 0) {
7726000d1cc1SJoe Perches			ERROR("MISSING_SIGN_OFF",
7727000d1cc1SJoe Perches			      "Missing Signed-off-by: line(s)\n");
772848ca2d8aSDwaipayan Ray		} elsif ($authorsignoff != 1) {
772948ca2d8aSDwaipayan Ray			# authorsignoff values:
773048ca2d8aSDwaipayan Ray			# 0 -> missing sign off
773148ca2d8aSDwaipayan Ray			# 1 -> sign off identical
773248ca2d8aSDwaipayan Ray			# 2 -> names and addresses match, comments mismatch
773348ca2d8aSDwaipayan Ray			# 3 -> addresses match, names different
773448ca2d8aSDwaipayan Ray			# 4 -> names match, addresses different
773548ca2d8aSDwaipayan Ray			# 5 -> names match, addresses excluding subaddress details (refer RFC 5233) match
773648ca2d8aSDwaipayan Ray
773748ca2d8aSDwaipayan Ray			my $sob_msg = "'From: $author' != 'Signed-off-by: $author_sob'";
773848ca2d8aSDwaipayan Ray
773948ca2d8aSDwaipayan Ray			if ($authorsignoff == 0) {
774048ca2d8aSDwaipayan Ray				ERROR("NO_AUTHOR_SIGN_OFF",
7741cd261496SGeert Uytterhoeven				      "Missing Signed-off-by: line by nominal patch author '$author'\n");
774248ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 2) {
774348ca2d8aSDwaipayan Ray				CHK("FROM_SIGN_OFF_MISMATCH",
774448ca2d8aSDwaipayan Ray				    "From:/Signed-off-by: email comments mismatch: $sob_msg\n");
774548ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 3) {
774648ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
774748ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email name mismatch: $sob_msg\n");
774848ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 4) {
774948ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
775048ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email address mismatch: $sob_msg\n");
775148ca2d8aSDwaipayan Ray			} elsif ($authorsignoff == 5) {
775248ca2d8aSDwaipayan Ray				WARN("FROM_SIGN_OFF_MISMATCH",
775348ca2d8aSDwaipayan Ray				     "From:/Signed-off-by: email subaddress mismatch: $sob_msg\n");
775448ca2d8aSDwaipayan Ray			}
7755cd261496SGeert Uytterhoeven		}
77560a920b5bSAndy Whitcroft	}
77570a920b5bSAndy Whitcroft
7758f0a594c1SAndy Whitcroft	print report_dump();
775913214adfSAndy Whitcroft	if ($summary && !($clean == 1 && $quiet == 1)) {
776013214adfSAndy Whitcroft		print "$filename " if ($summary_file);
77616c72ffaaSAndy Whitcroft		print "total: $cnt_error errors, $cnt_warn warnings, " .
77626c72ffaaSAndy Whitcroft			(($check)? "$cnt_chk checks, " : "") .
77636c72ffaaSAndy Whitcroft			"$cnt_lines lines checked\n";
77646c72ffaaSAndy Whitcroft	}
77658905a67cSAndy Whitcroft
7766d2c0a235SAndy Whitcroft	if ($quiet == 0) {
7767ef212196SJoe Perches		# If there were any defects found and not already fixing them
7768ef212196SJoe Perches		if (!$clean and !$fix) {
7769ef212196SJoe Perches			print << "EOM"
7770ef212196SJoe Perches
7771ef212196SJoe PerchesNOTE: For some of the reported defects, checkpatch may be able to
7772ef212196SJoe Perches      mechanically convert to the typical style using --fix or --fix-inplace.
7773ef212196SJoe PerchesEOM
7774ef212196SJoe Perches		}
7775d2c0a235SAndy Whitcroft		# If there were whitespace errors which cleanpatch can fix
7776d2c0a235SAndy Whitcroft		# then suggest that.
7777d2c0a235SAndy Whitcroft		if ($rpt_cleaners) {
7778b0781216SMike Frysinger			$rpt_cleaners = 0;
7779d8469f16SJoe Perches			print << "EOM"
7780d8469f16SJoe Perches
7781d8469f16SJoe PerchesNOTE: Whitespace errors detected.
7782d8469f16SJoe Perches      You may wish to use scripts/cleanpatch or scripts/cleanfile
7783d8469f16SJoe PerchesEOM
7784d2c0a235SAndy Whitcroft		}
7785d2c0a235SAndy Whitcroft	}
7786d2c0a235SAndy Whitcroft
7787d752fcc8SJoe Perches	if ($clean == 0 && $fix &&
7788d752fcc8SJoe Perches	    ("@rawlines" ne "@fixed" ||
7789d752fcc8SJoe Perches	     $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
77909624b8d6SJoe Perches		my $newfile = $filename;
77919624b8d6SJoe Perches		$newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
77923705ce5bSJoe Perches		my $linecount = 0;
77933705ce5bSJoe Perches		my $f;
77943705ce5bSJoe Perches
7795d752fcc8SJoe Perches		@fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
7796d752fcc8SJoe Perches
77973705ce5bSJoe Perches		open($f, '>', $newfile)
77983705ce5bSJoe Perches		    or die "$P: Can't open $newfile for write\n";
77993705ce5bSJoe Perches		foreach my $fixed_line (@fixed) {
78003705ce5bSJoe Perches			$linecount++;
78013705ce5bSJoe Perches			if ($file) {
78023705ce5bSJoe Perches				if ($linecount > 3) {
78033705ce5bSJoe Perches					$fixed_line =~ s/^\+//;
78043705ce5bSJoe Perches					print $f $fixed_line . "\n";
78053705ce5bSJoe Perches				}
78063705ce5bSJoe Perches			} else {
78073705ce5bSJoe Perches				print $f $fixed_line . "\n";
78083705ce5bSJoe Perches			}
78093705ce5bSJoe Perches		}
78103705ce5bSJoe Perches		close($f);
78113705ce5bSJoe Perches
78123705ce5bSJoe Perches		if (!$quiet) {
78133705ce5bSJoe Perches			print << "EOM";
7814d8469f16SJoe Perches
78153705ce5bSJoe PerchesWrote EXPERIMENTAL --fix correction(s) to '$newfile'
78163705ce5bSJoe Perches
78173705ce5bSJoe PerchesDo _NOT_ trust the results written to this file.
78183705ce5bSJoe PerchesDo _NOT_ submit these changes without inspecting them for correctness.
78193705ce5bSJoe Perches
78203705ce5bSJoe PerchesThis EXPERIMENTAL file is simply a convenience to help rewrite patches.
78213705ce5bSJoe PerchesNo warranties, expressed or implied...
78223705ce5bSJoe PerchesEOM
78233705ce5bSJoe Perches		}
78243705ce5bSJoe Perches	}
78253705ce5bSJoe Perches
7826d8469f16SJoe Perches	if ($quiet == 0) {
7827d8469f16SJoe Perches		print "\n";
7828d8469f16SJoe Perches		if ($clean == 1) {
7829d8469f16SJoe Perches			print "$vname has no obvious style problems and is ready for submission.\n";
7830d8469f16SJoe Perches		} else {
7831d8469f16SJoe Perches			print "$vname has style problems, please review.\n";
78320a920b5bSAndy Whitcroft		}
78330a920b5bSAndy Whitcroft	}
78340a920b5bSAndy Whitcroft	return $clean;
78350a920b5bSAndy Whitcroft}
7836