blob: 49153ce22da21fedbe6f7dbaff864049c17797ec [file] [log] [blame]
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001#!/usr/bin/perl -w
Dave Jonesdbf004d2010-01-12 16:59:52 -05002# (c) 2001, Dave Jones. (the file handling bit)
Andy Whitcroft00df3442007-06-08 13:47:06 -07003# (c) 2005, Joel Schopp <jschopp@austin.ibm.com> (the ugly bit)
Andy Whitcroft2a5a2c22009-01-06 14:41:23 -08004# (c) 2007,2008, Andy Whitcroft <apw@uk.ibm.com> (new conditions, test suite)
Andy Whitcroft015830b2010-10-26 14:23:17 -07005# (c) 2008-2010 Andy Whitcroft <apw@canonical.com>
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006# Licensed under the terms of the GNU GPL License version 2
7
8use strict;
Joe Perchesc707a812013-07-08 16:00:43 -07009use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080010use File::Basename;
11use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070012use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070013
14my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080015my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070016
Joe Perches000d1cc12011-07-25 17:13:25 -070017my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070018
19use Getopt::Long qw(:config no_auto_abbrev);
20
21my $quiet = 0;
22my $tree = 1;
23my $chk_signoff = 1;
24my $chk_patch = 1;
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080025my $chk_author = 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -070026my $tst_only;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070027my $emacs = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080028my $terse = 0;
Joe Perches34d88152015-06-25 15:03:05 -070029my $showfile = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070030my $file = 0;
Du, Changbin4a593c32016-05-20 17:04:16 -070031my $git = 0;
Joe Perches0dea9f12016-05-20 17:04:19 -070032my %git_commits = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070033my $check = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -070034my $check_orig = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -080035my $summary = 1;
36my $mailback = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -080037my $summary_file = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070038my $show_types = 0;
Joe Perches3beb42e2016-05-20 17:04:14 -070039my $list_types = 0;
Joe Perches3705ce52013-07-03 15:05:31 -070040my $fix = 0;
Joe Perches9624b8d2014-01-23 15:54:44 -080041my $fix_inplace = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -070042my $root;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -080043my %debug;
Joe Perches34456862013-07-03 15:05:34 -070044my %camelcase = ();
Joe Perches91bfe482013-09-11 14:23:59 -070045my %use_type = ();
46my @use = ();
47my %ignore_type = ();
Joe Perches000d1cc12011-07-25 17:13:25 -070048my @ignore = ();
Hannes Eder77f5b102009-09-21 17:04:37 -070049my $help = 0;
Joe Perches000d1cc12011-07-25 17:13:25 -070050my $configuration_file = ".checkpatch.conf";
Joe Perches6cd7f382012-12-17 16:01:54 -080051my $max_line_length = 80;
Dave Hansend62a2012013-09-11 14:23:56 -070052my $ignore_perl_version = 0;
53my $minimum_perl_version = 5.10.0;
Vadim Bendebury56193272014-10-13 15:51:48 -070054my $min_conf_desc_length = 4;
Kees Cook66b47b42014-10-13 15:51:57 -070055my $spelling_file = "$D/spelling.txt";
Joe Perchesebfd7d62015-04-16 12:44:14 -070056my $codespell = 0;
Maxim Uvarovf1a63672015-06-25 15:03:08 -070057my $codespellfile = "/usr/share/codespell/dictionary.txt";
Joe Perches57230292015-06-25 15:03:03 -070058my $color = 1;
Joe Perchesdadf6802016-08-02 14:04:33 -070059my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070060
61sub help {
62 my ($exitcode) = @_;
63
64 print << "EOM";
65Usage: $P [OPTION]... [FILE]...
66Version: $V
67
68Options:
69 -q, --quiet quiet
70 --no-tree run without a kernel tree
71 --no-signoff do not check for 'Signed-off-by' line
Jeff Johnsondb9dbec2015-01-22 13:34:29 -080072 --no-author do not check for unexpected authors
Hannes Eder77f5b102009-09-21 17:04:37 -070073 --patch treat FILE as patchfile (default)
74 --emacs emacs compile window format
75 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070076 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070077 -g, --git treat FILE as a single commit or git revision range
78 single git commit with:
79 <rev>
80 <rev>^
81 <rev>~n
82 multiple git commits with:
83 <rev1>..<rev2>
84 <rev1>...<rev2>
85 <rev>-<count>
86 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070087 -f, --file treat FILE as regular source file
88 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070089 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070090 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070091 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070092 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080093 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070094 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070095 --root=PATH PATH to the kernel tree root
96 --no-summary suppress the per-file summary
97 --mailback only produce a report in case of warnings/errors
98 --summary-file include the filename in summary
99 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
100 'values', 'possible', 'type', and 'attr' (default
101 is all off)
102 --test-only=WORD report only warnings/errors containing WORD
103 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700104 --fix EXPERIMENTAL - may create horrible results
105 If correctable single-line errors exist, create
106 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
107 with potential errors corrected to the preferred
108 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800109 --fix-inplace EXPERIMENTAL - may create horrible results
110 Is the same as --fix, but overwrites the input
111 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700112 --ignore-perl-version override checking of perl version. expect
113 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700114 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700115 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700116 --codespellfile Use this codespell dictionary
Joe Perches57230292015-06-25 15:03:03 -0700117 --color Use colors when output is STDOUT (default: on)
Hannes Eder77f5b102009-09-21 17:04:37 -0700118 -h, --help, --version display this help and exit
119
120When FILE is - read standard input.
121EOM
122
123 exit($exitcode);
124}
125
Joe Perches3beb42e2016-05-20 17:04:14 -0700126sub uniq {
127 my %seen;
128 return grep { !$seen{$_}++ } @_;
129}
130
131sub list_types {
132 my ($exitcode) = @_;
133
134 my $count = 0;
135
136 local $/ = undef;
137
138 open(my $script, '<', abs_path($P)) or
139 die "$P: Can't read '$P' $!\n";
140
141 my $text = <$script>;
142 close($script);
143
144 my @types = ();
145 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
146 push (@types, $_);
147 }
148 @types = sort(uniq(@types));
149 print("#\tMessage type\n\n");
150 foreach my $type (@types) {
151 print(++$count . "\t" . $type . "\n");
152 }
153
154 exit($exitcode);
155}
156
Joe Perches000d1cc12011-07-25 17:13:25 -0700157my $conf = which_conf($configuration_file);
158if (-f $conf) {
159 my @conf_args;
160 open(my $conffile, '<', "$conf")
161 or warn "$P: Can't find a readable $configuration_file file $!\n";
162
163 while (<$conffile>) {
164 my $line = $_;
165
166 $line =~ s/\s*\n?$//g;
167 $line =~ s/^\s*//g;
168 $line =~ s/\s+/ /g;
169
170 next if ($line =~ m/^\s*#/);
171 next if ($line =~ m/^\s*$/);
172
173 my @words = split(" ", $line);
174 foreach my $word (@words) {
175 last if ($word =~ m/^#/);
176 push (@conf_args, $word);
177 }
178 }
179 close($conffile);
180 unshift(@ARGV, @conf_args) if @conf_args;
181}
182
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700183GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700184 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700185 'tree!' => \$tree,
186 'signoff!' => \$chk_signoff,
187 'patch!' => \$chk_patch,
Jeff Johnsondb9dbec2015-01-22 13:34:29 -0800188 'author!' => \$chk_author,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700189 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800190 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700191 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700192 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700193 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700194 'subjective!' => \$check,
195 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700196 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700197 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700198 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700199 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800200 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700201 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700202 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800203 'summary!' => \$summary,
204 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800205 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700206 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800207 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700208 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800209 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700210 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700211 'codespell!' => \$codespell,
212 'codespellfile=s' => \$codespellfile,
Joe Perches57230292015-06-25 15:03:03 -0700213 'color!' => \$color,
Hannes Eder77f5b102009-09-21 17:04:37 -0700214 'h|help' => \$help,
215 'version' => \$help
216) or help(1);
217
218help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700219
Joe Perches3beb42e2016-05-20 17:04:14 -0700220list_types(0) if ($list_types);
221
Joe Perches9624b8d2014-01-23 15:54:44 -0800222$fix = 1 if ($fix_inplace);
Joe Perches2ac73b4f2014-06-04 16:12:05 -0700223$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800224
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700225my $exit = 0;
226
Dave Hansend62a2012013-09-11 14:23:56 -0700227if ($^V && $^V lt $minimum_perl_version) {
228 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
229 if (!$ignore_perl_version) {
230 exit(1);
231 }
232}
233
Allen Hubbe45107ff2016-08-02 14:04:47 -0700234#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700235if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700236 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700237}
238
Joe Perches91bfe482013-09-11 14:23:59 -0700239sub hash_save_array_words {
240 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700241
Joe Perches91bfe482013-09-11 14:23:59 -0700242 my @array = split(/,/, join(',', @$arrayRef));
243 foreach my $word (@array) {
244 $word =~ s/\s*\n?$//g;
245 $word =~ s/^\s*//g;
246 $word =~ s/\s+/ /g;
247 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700248
Joe Perches91bfe482013-09-11 14:23:59 -0700249 next if ($word =~ m/^\s*#/);
250 next if ($word =~ m/^\s*$/);
251
252 $hashRef->{$word}++;
253 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700254}
255
Joe Perches91bfe482013-09-11 14:23:59 -0700256sub hash_show_words {
257 my ($hashRef, $prefix) = @_;
258
Joe Perches3c816e42015-06-25 15:03:29 -0700259 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700260 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700261 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700262 print " $word";
263 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700264 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700265 }
266}
267
268hash_save_array_words(\%ignore_type, \@ignore);
269hash_save_array_words(\%use_type, \@use);
270
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800271my $dbg_values = 0;
272my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700273my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700274my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800275for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800276 ## no critic
277 eval "\${dbg_$key} = '$debug{$key}';";
278 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800279}
280
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700281my $rpt_cleaners = 0;
282
Andy Whitcroft8905a672007-11-28 16:21:06 -0800283if ($terse) {
284 $emacs = 1;
285 $quiet++;
286}
287
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700288if ($tree) {
289 if (defined $root) {
290 if (!top_of_kernel_tree($root)) {
291 die "$P: $root: --root does not point at a valid tree\n";
292 }
293 } else {
294 if (top_of_kernel_tree('.')) {
295 $root = '.';
296 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
297 top_of_kernel_tree($1)) {
298 $root = $1;
299 }
300 }
301
302 if (!defined $root) {
303 print "Must be run from the top-level dir. of a kernel tree\n";
304 exit(2);
305 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700306}
307
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700308my $emitted_corrupt = 0;
309
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700310our $Ident = qr{
311 [A-Za-z_][A-Za-z\d_]*
312 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
313 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700314our $Storage = qr{extern|static|asmlinkage};
315our $Sparse = qr{
316 __user|
317 __kernel|
318 __force|
319 __iomem|
320 __must_check|
321 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800322 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700323 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800324 __rcu|
325 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700326 }x;
Joe Perchese970b882013-11-12 15:10:10 -0800327our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
328our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
329our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
330our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
331our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700332
Wolfram Sang52131292010-03-05 13:43:51 -0800333# Notes to $Attribute:
334# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700335our $Attribute = qr{
336 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700337 __percpu|
338 __nocast|
339 __safe|
340 __bitwise__|
341 __packed__|
342 __packed2__|
343 __naked|
344 __maybe_unused|
345 __always_unused|
346 __noreturn|
347 __used|
348 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800349 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700350 __noclone|
351 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700352 __read_mostly|
353 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700354 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700355 ____cacheline_aligned|
356 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800357 ____cacheline_internodealigned_in_smp|
358 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700359 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700360our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700361our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700362our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
363our $Lval = qr{$Ident(?:$Member)*};
364
Joe Perches95e2c602013-07-03 15:05:20 -0700365our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
366our $Binary = qr{(?i)0b[01]+$Int_type?};
367our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
368our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700369our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800370our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800371our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
372our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
373our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800374our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700375our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800376our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700377our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700378our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700379our $Operators = qr{
380 <=|>=|==|!=|
381 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700382 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700383 }x;
384
Joe Perches91cb5192014-04-03 14:49:32 -0700385our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
386
Joe Perchesab7e23f2015-04-16 12:44:22 -0700387our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800388our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700389our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700390our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800391our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700392our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800393our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700394our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800395
Joe Perches15662b32011-10-31 17:13:12 -0700396our $NON_ASCII_UTF8 = qr{
397 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700398 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
399 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
400 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
401 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
402 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
403 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
404}x;
405
Joe Perches15662b32011-10-31 17:13:12 -0700406our $UTF8 = qr{
407 [\x09\x0A\x0D\x20-\x7E] # ASCII
408 | $NON_ASCII_UTF8
409}x;
410
Joe Perchese6176fa2015-06-25 15:02:49 -0700411our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800412our $typeOtherOSTypedefs = qr{(?x:
413 u_(?:char|short|int|long) | # bsd
414 u(?:nchar|short|int|long) # sysv
415)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700416our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700417 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700418 atomic_t
419)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700420our $typeTypedefs = qr{(?x:
421 $typeC99Typedefs\b|
422 $typeOtherOSTypedefs\b|
423 $typeKernelTypedefs\b
424)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700425
Joe Perches6d32f7a2015-11-06 16:31:37 -0800426our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
427
Joe Perches691e6692010-03-05 13:43:51 -0800428our $logFunctions = qr{(?x:
Joe Perches6e60c022011-07-25 17:13:27 -0700429 printk(?:_ratelimited|_once|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700430 (?:[a-z0-9]+_){1,2}(?:printk|emerg|alert|crit|err|warning|warn|notice|info|debug|dbg|vdbg|devel|cont|WARN)(?:_ratelimited|_once|)|
Joe Perches6e60c022011-07-25 17:13:27 -0700431 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700432 panic|
Joe Perches06668722013-11-12 15:10:07 -0800433 MODULE_[A-Z_]+|
434 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800435)};
436
Joe Perches20112472011-07-25 17:13:23 -0700437our $signature_tags = qr{(?xi:
438 Signed-off-by:|
439 Acked-by:|
440 Tested-by:|
441 Reviewed-by:|
442 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700443 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700444 To:|
445 Cc:
446)};
447
Joe Perches18130872014-08-06 16:11:22 -0700448our @typeListMisordered = (
449 qr{char\s+(?:un)?signed},
450 qr{int\s+(?:(?:un)?signed\s+)?short\s},
451 qr{int\s+short(?:\s+(?:un)?signed)},
452 qr{short\s+int(?:\s+(?:un)?signed)},
453 qr{(?:un)?signed\s+int\s+short},
454 qr{short\s+(?:un)?signed},
455 qr{long\s+int\s+(?:un)?signed},
456 qr{int\s+long\s+(?:un)?signed},
457 qr{long\s+(?:un)?signed\s+int},
458 qr{int\s+(?:un)?signed\s+long},
459 qr{int\s+(?:un)?signed},
460 qr{int\s+long\s+long\s+(?:un)?signed},
461 qr{long\s+long\s+int\s+(?:un)?signed},
462 qr{long\s+long\s+(?:un)?signed\s+int},
463 qr{long\s+long\s+(?:un)?signed},
464 qr{long\s+(?:un)?signed},
465);
466
Andy Whitcroft8905a672007-11-28 16:21:06 -0800467our @typeList = (
468 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700469 qr{(?:(?:un)?signed\s+)?char},
470 qr{(?:(?:un)?signed\s+)?short\s+int},
471 qr{(?:(?:un)?signed\s+)?short},
472 qr{(?:(?:un)?signed\s+)?int},
473 qr{(?:(?:un)?signed\s+)?long\s+int},
474 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
475 qr{(?:(?:un)?signed\s+)?long\s+long},
476 qr{(?:(?:un)?signed\s+)?long},
477 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800478 qr{float},
479 qr{double},
480 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800481 qr{struct\s+$Ident},
482 qr{union\s+$Ident},
483 qr{enum\s+$Ident},
484 qr{${Ident}_t},
485 qr{${Ident}_handler},
486 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700487 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800488);
Joe Perches938224b2016-01-20 14:59:15 -0800489
490our $C90_int_types = qr{(?x:
491 long\s+long\s+int\s+(?:un)?signed|
492 long\s+long\s+(?:un)?signed\s+int|
493 long\s+long\s+(?:un)?signed|
494 (?:(?:un)?signed\s+)?long\s+long\s+int|
495 (?:(?:un)?signed\s+)?long\s+long|
496 int\s+long\s+long\s+(?:un)?signed|
497 int\s+(?:(?:un)?signed\s+)?long\s+long|
498
499 long\s+int\s+(?:un)?signed|
500 long\s+(?:un)?signed\s+int|
501 long\s+(?:un)?signed|
502 (?:(?:un)?signed\s+)?long\s+int|
503 (?:(?:un)?signed\s+)?long|
504 int\s+long\s+(?:un)?signed|
505 int\s+(?:(?:un)?signed\s+)?long|
506
507 int\s+(?:un)?signed|
508 (?:(?:un)?signed\s+)?int
509)};
510
Alex Dowad485ff232015-06-25 15:02:52 -0700511our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700512our @typeListWithAttr = (
513 @typeList,
514 qr{struct\s+$InitAttribute\s+$Ident},
515 qr{union\s+$InitAttribute\s+$Ident},
516);
517
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700518our @modifierList = (
519 qr{fastcall},
520);
Alex Dowad485ff232015-06-25 15:02:52 -0700521our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800522
Joe Perches24358802014-04-03 14:49:13 -0700523our @mode_permission_funcs = (
524 ["module_param", 3],
525 ["module_param_(?:array|named|string)", 4],
526 ["module_param_array_named", 5],
527 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
528 ["proc_create(?:_data|)", 2],
529 ["(?:CLASS|DEVICE|SENSOR)_ATTR", 2],
530);
531
Joe Perches515a2352014-04-03 14:49:24 -0700532#Create a search pattern for all these functions to speed up a loop below
533our $mode_perms_search = "";
534foreach my $entry (@mode_permission_funcs) {
535 $mode_perms_search .= '|' if ($mode_perms_search ne "");
536 $mode_perms_search .= $entry->[0];
537}
538
Joe Perchesb392c642015-04-16 12:44:16 -0700539our $mode_perms_world_writable = qr{
540 S_IWUGO |
541 S_IWOTH |
542 S_IRWXUGO |
543 S_IALLUGO |
544 0[0-7][0-7][2367]
545}x;
546
Wolfram Sang7840a942010-08-09 17:20:57 -0700547our $allowed_asm_includes = qr{(?x:
548 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700549 memory|
550 time|
551 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700552)};
553# memory.h: ARM has a custom one
554
Kees Cook66b47b42014-10-13 15:51:57 -0700555# Load common spelling mistakes and build regular expression list.
556my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700557my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700558
Joe Perches36061e32014-12-10 15:51:43 -0800559if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800560 while (<$spelling>) {
561 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700562
Joe Perches36061e32014-12-10 15:51:43 -0800563 $line =~ s/\s*\n?$//g;
564 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700565
Joe Perches36061e32014-12-10 15:51:43 -0800566 next if ($line =~ m/^\s*#/);
567 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700568
Joe Perches36061e32014-12-10 15:51:43 -0800569 my ($suspect, $fix) = split(/\|\|/, $line);
570
Joe Perches36061e32014-12-10 15:51:43 -0800571 $spelling_fix{$suspect} = $fix;
572 }
573 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800574} else {
575 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700576}
Kees Cook66b47b42014-10-13 15:51:57 -0700577
Joe Perchesebfd7d62015-04-16 12:44:14 -0700578if ($codespell) {
579 if (open(my $spelling, '<', $codespellfile)) {
580 while (<$spelling>) {
581 my $line = $_;
582
583 $line =~ s/\s*\n?$//g;
584 $line =~ s/^\s*//g;
585
586 next if ($line =~ m/^\s*#/);
587 next if ($line =~ m/^\s*$/);
588 next if ($line =~ m/, disabled/i);
589
590 $line =~ s/,.*$//;
591
592 my ($suspect, $fix) = split(/->/, $line);
593
594 $spelling_fix{$suspect} = $fix;
595 }
596 close($spelling);
597 } else {
598 warn "No codespell typos will be found - file '$codespellfile': $!\n";
599 }
600}
601
602$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
603
Andy Whitcroft8905a672007-11-28 16:21:06 -0800604sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700605 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
606 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700607 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700608 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700609 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700610 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700611 (?:$typeTypedefs\b)|
612 (?:${all}\b)
613 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800614 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700615 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800616 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800617 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700618 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700619 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800620 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700621 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800622 }x;
Joe Perches18130872014-08-06 16:11:22 -0700623 $NonptrTypeMisordered = qr{
624 (?:$Modifier\s+|const\s+)*
625 (?:
626 (?:${Misordered}\b)
627 )
628 (?:\s+$Modifier|\s+const)*
629 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700630 $NonptrTypeWithAttr = qr{
631 (?:$Modifier\s+|const\s+)*
632 (?:
633 (?:typeof|__typeof__)\s*\([^\)]*\)|
634 (?:$typeTypedefs\b)|
635 (?:${allWithAttr}\b)
636 )
637 (?:\s+$Modifier|\s+const)*
638 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800639 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700640 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700641 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700642 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800643 }x;
Joe Perches18130872014-08-06 16:11:22 -0700644 $TypeMisordered = qr{
645 $NonptrTypeMisordered
646 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
647 (?:\s+$Inline|\s+$Modifier)*
648 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700649 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700650 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800651}
652build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700653
Joe Perches7d2367a2011-07-25 17:13:22 -0700654our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700655
656# Using $balanced_parens, $LvalOrFunc, or $FuncArg
657# requires at least perl version v5.10.0
658# Any use must be runtime checked with $^V
659
660our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700661our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800662our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700663
Joe Perchesf8422302014-08-06 16:11:31 -0700664our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700665 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700666 (?:$Storage\s+)?LIST_HEAD\s*\(|
667 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
668)};
669
Joe Perches7d2367a2011-07-25 17:13:22 -0700670sub deparenthesize {
671 my ($string) = @_;
672 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700673
674 while ($string =~ /^\s*\(.*\)\s*$/) {
675 $string =~ s@^\s*\(\s*@@;
676 $string =~ s@\s*\)\s*$@@;
677 }
678
Joe Perches7d2367a2011-07-25 17:13:22 -0700679 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700680
Joe Perches7d2367a2011-07-25 17:13:22 -0700681 return $string;
682}
683
Joe Perches34456862013-07-03 15:05:34 -0700684sub seed_camelcase_file {
685 my ($file) = @_;
686
687 return if (!(-f $file));
688
689 local $/;
690
691 open(my $include_file, '<', "$file")
692 or warn "$P: Can't read '$file' $!\n";
693 my $text = <$include_file>;
694 close($include_file);
695
696 my @lines = split('\n', $text);
697
698 foreach my $line (@lines) {
699 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
700 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
701 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800702 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
703 $camelcase{$1} = 1;
704 } elsif ($line =~ /^\s*(?:union|struct|enum)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[;\{]/) {
Joe Perches34456862013-07-03 15:05:34 -0700705 $camelcase{$1} = 1;
706 }
707 }
708}
709
710my $camelcase_seeded = 0;
711sub seed_camelcase_includes {
712 return if ($camelcase_seeded);
713
714 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700715 my $camelcase_cache = "";
716 my @include_files = ();
717
718 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700719
Richard Genoud3645e322014-02-10 14:25:32 -0800720 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700721 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
722 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700723 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700724 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700725 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700726 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700727 @include_files = split('\n', $files);
728 foreach my $file (@include_files) {
729 my $date = POSIX::strftime("%Y%m%d%H%M",
730 localtime((stat $file)[9]));
731 $last_mod_date = $date if ($last_mod_date < $date);
732 }
733 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700734 }
Joe Perchesc707a812013-07-08 16:00:43 -0700735
736 if ($camelcase_cache ne "" && -f $camelcase_cache) {
737 open(my $camelcase_file, '<', "$camelcase_cache")
738 or warn "$P: Can't read '$camelcase_cache' $!\n";
739 while (<$camelcase_file>) {
740 chomp;
741 $camelcase{$_} = 1;
742 }
743 close($camelcase_file);
744
745 return;
746 }
747
Richard Genoud3645e322014-02-10 14:25:32 -0800748 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700749 $files = `git ls-files "include/*.h"`;
750 @include_files = split('\n', $files);
751 }
752
Joe Perches34456862013-07-03 15:05:34 -0700753 foreach my $file (@include_files) {
754 seed_camelcase_file($file);
755 }
Joe Perches351b2a12013-07-03 15:05:36 -0700756
Joe Perchesc707a812013-07-08 16:00:43 -0700757 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700758 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700759 open(my $camelcase_file, '>', "$camelcase_cache")
760 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700761 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
762 print $camelcase_file ("$_\n");
763 }
764 close($camelcase_file);
765 }
Joe Perches34456862013-07-03 15:05:34 -0700766}
767
Joe Perchesd311cd42014-08-06 16:10:57 -0700768sub git_commit_info {
769 my ($commit, $id, $desc) = @_;
770
771 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
772
773 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
774 $output =~ s/^\s*//gm;
775 my @lines = split("\n", $output);
776
Joe Perches0d7835f2015-02-13 14:38:35 -0800777 return ($id, $desc) if ($#lines < 0);
778
Joe Perchesd311cd42014-08-06 16:10:57 -0700779 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
780# Maybe one day convert this block of bash into something that returns
781# all matching commit ids, but it's very slow...
782#
783# echo "checking commits $1..."
784# git rev-list --remotes | grep -i "^$1" |
785# while read line ; do
786# git log --format='%H %s' -1 $line |
787# echo "commit $(cut -c 1-12,41-)"
788# done
789 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
790 } else {
791 $id = substr($lines[0], 0, 12);
792 $desc = substr($lines[0], 41);
793 }
794
795 return ($id, $desc);
796}
797
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700798$chk_signoff = 0 if ($file);
799
Andy Whitcroft00df3442007-06-08 13:47:06 -0700800my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800801my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700802my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700803my @fixed_inserted = ();
804my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700805my $fixlinenr = -1;
806
Du, Changbin4a593c32016-05-20 17:04:16 -0700807# If input is git commits, extract all commits from the commit expressions.
808# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
809die "$P: No git repository found\n" if ($git && !-e ".git");
810
811if ($git) {
812 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700813 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700814 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700815 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
816 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700817 } elsif ($commit_expr =~ m/\.\./) {
818 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700819 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700820 $git_range = "-1 $commit_expr";
821 }
822 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
823 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700824 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
825 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700826 my $sha1 = $1;
827 my $subject = $2;
828 unshift(@commits, $sha1);
829 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700830 }
831 }
832 die "$P: no git commits after extraction!\n" if (@commits == 0);
833 @ARGV = @commits;
834}
835
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800836my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700837for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800838 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700839 if ($git) {
840 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
841 die "$P: $filename: git format-patch failed - $!\n";
842 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800843 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700844 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800845 } elsif ($filename eq '-') {
846 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700847 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800848 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700849 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700850 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800851 if ($filename eq '-') {
852 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700853 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700854 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800855 } else {
856 $vname = $filename;
857 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800858 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700859 chomp;
860 push(@rawlines, $_);
861 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800862 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700863
864 if ($#ARGV > 0 && $quiet == 0) {
865 print '-' x length($vname) . "\n";
866 print "$vname\n";
867 print '-' x length($vname) . "\n";
868 }
869
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800870 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700871 $exit = 1;
872 }
873 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800874 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700875 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700876 @fixed_inserted = ();
877 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700878 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700879 @modifierListFile = ();
880 @typeListFile = ();
881 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700882}
883
Joe Perchesd8469f12015-06-25 15:03:00 -0700884if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700885 hash_show_words(\%use_type, "Used");
886 hash_show_words(\%ignore_type, "Ignored");
887
Joe Perchesd8469f12015-06-25 15:03:00 -0700888 if ($^V lt 5.10.0) {
889 print << "EOM"
890
891NOTE: perl $^V is not modern enough to detect all possible issues.
892 An upgrade to at least perl v5.10.0 is suggested.
893EOM
894 }
895 if ($exit) {
896 print << "EOM"
897
898NOTE: If any of the errors are false positives, please report
899 them to the maintainer, see CHECKPATCH in MAINTAINERS.
900EOM
901 }
902}
903
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700904exit($exit);
905
906sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700907 my ($root) = @_;
908
909 my @tree_check = (
910 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
911 "README", "Documentation", "arch", "include", "drivers",
912 "fs", "init", "ipc", "kernel", "lib", "scripts",
913 );
914
915 foreach my $check (@tree_check) {
916 if (! -e $root . '/' . $check) {
917 return 0;
918 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700919 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700920 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -0700921}
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700922
Joe Perches20112472011-07-25 17:13:23 -0700923sub parse_email {
924 my ($formatted_email) = @_;
925
926 my $name = "";
927 my $address = "";
928 my $comment = "";
929
930 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
931 $name = $1;
932 $address = $2;
933 $comment = $3 if defined $3;
934 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
935 $address = $1;
936 $comment = $2 if defined $2;
937 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
938 $address = $1;
939 $comment = $2 if defined $2;
940 $formatted_email =~ s/$address.*$//;
941 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -0700942 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700943 $name =~ s/^\"|\"$//g;
944 # If there's a name left after stripping spaces and
945 # leading quotes, and the address doesn't have both
946 # leading and trailing angle brackets, the address
947 # is invalid. ie:
948 # "joe smith joe@smith.com" bad
949 # "joe smith <joe@smith.com" bad
950 if ($name ne "" && $address !~ /^<[^>]+>$/) {
951 $name = "";
952 $address = "";
953 $comment = "";
954 }
955 }
956
Joe Perches3705ce52013-07-03 15:05:31 -0700957 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700958 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700959 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700960 $address =~ s/^\<|\>$//g;
961
962 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
963 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
964 $name = "\"$name\"";
965 }
966
967 return ($name, $address, $comment);
968}
969
970sub format_email {
971 my ($name, $address) = @_;
972
973 my $formatted_email;
974
Joe Perches3705ce52013-07-03 15:05:31 -0700975 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -0700976 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -0700977 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -0700978
979 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
980 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
981 $name = "\"$name\"";
982 }
983
984 if ("$name" eq "") {
985 $formatted_email = "$address";
986 } else {
987 $formatted_email = "$name <$address>";
988 }
989
990 return $formatted_email;
991}
992
Joe Perchesd311cd42014-08-06 16:10:57 -0700993sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -0700994 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -0700995
Joe Perchesbd474ca2014-08-06 16:11:10 -0700996 foreach my $path (split(/:/, $ENV{PATH})) {
997 if (-e "$path/$bin") {
998 return "$path/$bin";
999 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001000 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001001
Joe Perchesbd474ca2014-08-06 16:11:10 -07001002 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001003}
1004
Joe Perches000d1cc12011-07-25 17:13:25 -07001005sub which_conf {
1006 my ($conf) = @_;
1007
1008 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1009 if (-e "$path/$conf") {
1010 return "$path/$conf";
1011 }
1012 }
1013
1014 return "";
1015}
1016
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001017sub expand_tabs {
1018 my ($str) = @_;
1019
1020 my $res = '';
1021 my $n = 0;
1022 for my $c (split(//, $str)) {
1023 if ($c eq "\t") {
1024 $res .= ' ';
1025 $n++;
1026 for (; ($n % 8) != 0; $n++) {
1027 $res .= ' ';
1028 }
1029 next;
1030 }
1031 $res .= $c;
1032 $n++;
1033 }
1034
1035 return $res;
1036}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001037sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001038 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001039 return $res;
1040}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001041
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001042sub line_stats {
1043 my ($line) = @_;
1044
1045 # Drop the diff line leader and expand tabs
1046 $line =~ s/^.//;
1047 $line = expand_tabs($line);
1048
1049 # Pick the indent from the front of the line.
1050 my ($white) = ($line =~ /^(\s*)/);
1051
1052 return (length($line), length($white));
1053}
1054
Andy Whitcroft773647a2008-03-28 14:15:58 -07001055my $sanitise_quote = '';
1056
1057sub sanitise_line_reset {
1058 my ($in_comment) = @_;
1059
1060 if ($in_comment) {
1061 $sanitise_quote = '*/';
1062 } else {
1063 $sanitise_quote = '';
1064 }
1065}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001066sub sanitise_line {
1067 my ($line) = @_;
1068
1069 my $res = '';
1070 my $l = '';
1071
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001072 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001073 my $off = 0;
1074 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001075
Andy Whitcroft773647a2008-03-28 14:15:58 -07001076 # Always copy over the diff marker.
1077 $res = substr($line, 0, 1);
1078
1079 for ($off = 1; $off < length($line); $off++) {
1080 $c = substr($line, $off, 1);
1081
1082 # Comments we are wacking completly including the begin
1083 # and end, all to $;.
1084 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1085 $sanitise_quote = '*/';
1086
1087 substr($res, $off, 2, "$;$;");
1088 $off++;
1089 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001090 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001091 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001092 $sanitise_quote = '';
1093 substr($res, $off, 2, "$;$;");
1094 $off++;
1095 next;
1096 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001097 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1098 $sanitise_quote = '//';
1099
1100 substr($res, $off, 2, $sanitise_quote);
1101 $off++;
1102 next;
1103 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001104
1105 # A \ in a string means ignore the next character.
1106 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1107 $c eq "\\") {
1108 substr($res, $off, 2, 'XX');
1109 $off++;
1110 next;
1111 }
1112 # Regular quotes.
1113 if ($c eq "'" || $c eq '"') {
1114 if ($sanitise_quote eq '') {
1115 $sanitise_quote = $c;
1116
1117 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001118 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001119 } elsif ($sanitise_quote eq $c) {
1120 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001121 }
1122 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001123
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001124 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001125 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1126 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001127 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1128 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001129 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1130 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001131 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001132 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001133 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001134 }
1135
Daniel Walker113f04a2009-09-21 17:04:35 -07001136 if ($sanitise_quote eq '//') {
1137 $sanitise_quote = '';
1138 }
1139
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001140 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001141 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001142 my $clean = 'X' x length($1);
1143 $res =~ s@\<.*\>@<$clean>@;
1144
1145 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001146 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001147 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001148 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001149 }
1150
Joe Perchesdadf6802016-08-02 14:04:33 -07001151 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1152 my $match = $1;
1153 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1154 }
1155
Andy Whitcroft00df3442007-06-08 13:47:06 -07001156 return $res;
1157}
1158
Joe Perchesa6962d72013-04-29 16:18:13 -07001159sub get_quoted_string {
1160 my ($line, $rawline) = @_;
1161
Joe Perches33acb542015-06-25 15:02:54 -07001162 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001163 return substr($rawline, $-[0], $+[0] - $-[0]);
1164}
1165
Andy Whitcroft8905a672007-11-28 16:21:06 -08001166sub ctx_statement_block {
1167 my ($linenr, $remain, $off) = @_;
1168 my $line = $linenr - 1;
1169 my $blk = '';
1170 my $soff = $off;
1171 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001172 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001173
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001174 my $loff = 0;
1175
Andy Whitcroft8905a672007-11-28 16:21:06 -08001176 my $type = '';
1177 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001178 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001179 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001180 my $c;
1181 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001182
1183 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001184 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001185 @stack = (['', 0]) if ($#stack == -1);
1186
Andy Whitcroft773647a2008-03-28 14:15:58 -07001187 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001188 # If we are about to drop off the end, pull in more
1189 # context.
1190 if ($off >= $len) {
1191 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001192 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001193 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001194 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001195 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001196 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001197 $len = length($blk);
1198 $line++;
1199 last;
1200 }
1201 # Bail if there is no further context.
1202 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001203 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001204 last;
1205 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001206 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1207 $level++;
1208 $type = '#';
1209 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001210 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001211 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001212 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001213 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001214
Andy Whitcroft773647a2008-03-28 14:15:58 -07001215 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001216
1217 # Handle nested #if/#else.
1218 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1219 push(@stack, [ $type, $level ]);
1220 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1221 ($type, $level) = @{$stack[$#stack - 1]};
1222 } elsif ($remainder =~ /^#\s*endif\b/) {
1223 ($type, $level) = @{pop(@stack)};
1224 }
1225
Andy Whitcroft8905a672007-11-28 16:21:06 -08001226 # Statement ends at the ';' or a close '}' at the
1227 # outermost level.
1228 if ($level == 0 && $c eq ';') {
1229 last;
1230 }
1231
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001232 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001233 if ($level == 0 && $coff_set == 0 &&
1234 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1235 $remainder =~ /^(else)(?:\s|{)/ &&
1236 $remainder !~ /^else\s+if\b/) {
1237 $coff = $off + length($1) - 1;
1238 $coff_set = 1;
1239 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1240 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001241 }
1242
Andy Whitcroft8905a672007-11-28 16:21:06 -08001243 if (($type eq '' || $type eq '(') && $c eq '(') {
1244 $level++;
1245 $type = '(';
1246 }
1247 if ($type eq '(' && $c eq ')') {
1248 $level--;
1249 $type = ($level != 0)? '(' : '';
1250
1251 if ($level == 0 && $coff < $soff) {
1252 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001253 $coff_set = 1;
1254 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001255 }
1256 }
1257 if (($type eq '' || $type eq '{') && $c eq '{') {
1258 $level++;
1259 $type = '{';
1260 }
1261 if ($type eq '{' && $c eq '}') {
1262 $level--;
1263 $type = ($level != 0)? '{' : '';
1264
1265 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001266 if (substr($blk, $off + 1, 1) eq ';') {
1267 $off++;
1268 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001269 last;
1270 }
1271 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001272 # Preprocessor commands end at the newline unless escaped.
1273 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1274 $level--;
1275 $type = '';
1276 $off++;
1277 last;
1278 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001279 $off++;
1280 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001281 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001282 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001283 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001284 $line++;
1285 $remain--;
1286 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001287
1288 my $statement = substr($blk, $soff, $off - $soff + 1);
1289 my $condition = substr($blk, $soff, $coff - $soff + 1);
1290
1291 #warn "STATEMENT<$statement>\n";
1292 #warn "CONDITION<$condition>\n";
1293
Andy Whitcroft773647a2008-03-28 14:15:58 -07001294 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001295
1296 return ($statement, $condition,
1297 $line, $remain + 1, $off - $loff + 1, $level);
1298}
1299
Andy Whitcroftcf655042008-03-04 14:28:20 -08001300sub statement_lines {
1301 my ($stmt) = @_;
1302
1303 # Strip the diff line prefixes and rip blank lines at start and end.
1304 $stmt =~ s/(^|\n)./$1/g;
1305 $stmt =~ s/^\s*//;
1306 $stmt =~ s/\s*$//;
1307
1308 my @stmt_lines = ($stmt =~ /\n/g);
1309
1310 return $#stmt_lines + 2;
1311}
1312
1313sub statement_rawlines {
1314 my ($stmt) = @_;
1315
1316 my @stmt_lines = ($stmt =~ /\n/g);
1317
1318 return $#stmt_lines + 2;
1319}
1320
1321sub statement_block_size {
1322 my ($stmt) = @_;
1323
1324 $stmt =~ s/(^|\n)./$1/g;
1325 $stmt =~ s/^\s*{//;
1326 $stmt =~ s/}\s*$//;
1327 $stmt =~ s/^\s*//;
1328 $stmt =~ s/\s*$//;
1329
1330 my @stmt_lines = ($stmt =~ /\n/g);
1331 my @stmt_statements = ($stmt =~ /;/g);
1332
1333 my $stmt_lines = $#stmt_lines + 2;
1334 my $stmt_statements = $#stmt_statements + 1;
1335
1336 if ($stmt_lines > $stmt_statements) {
1337 return $stmt_lines;
1338 } else {
1339 return $stmt_statements;
1340 }
1341}
1342
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001343sub ctx_statement_full {
1344 my ($linenr, $remain, $off) = @_;
1345 my ($statement, $condition, $level);
1346
1347 my (@chunks);
1348
Andy Whitcroftcf655042008-03-04 14:28:20 -08001349 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001350 ($statement, $condition, $linenr, $remain, $off, $level) =
1351 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001352 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001353 push(@chunks, [ $condition, $statement ]);
1354 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1355 return ($level, $linenr, @chunks);
1356 }
1357
1358 # Pull in the following conditional/block pairs and see if they
1359 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001360 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001361 ($statement, $condition, $linenr, $remain, $off, $level) =
1362 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001363 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001364 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001365 #print "C: push\n";
1366 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001367 }
1368
1369 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001370}
1371
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001372sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001373 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001374 my $line;
1375 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001376 my $blk = '';
1377 my @o;
1378 my @c;
1379 my @res = ();
1380
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001381 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001382 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001383 for ($line = $start; $remain > 0; $line++) {
1384 next if ($rawlines[$line] =~ /^-/);
1385 $remain--;
1386
1387 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001388
1389 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001390 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001391 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001392 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001393 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001394 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001395 $level = pop(@stack);
1396 }
1397
Andy Whitcroft01464f32010-10-26 14:23:19 -07001398 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001399 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1400 if ($off > 0) {
1401 $off--;
1402 next;
1403 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001404
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001405 if ($c eq $close && $level > 0) {
1406 $level--;
1407 last if ($level == 0);
1408 } elsif ($c eq $open) {
1409 $level++;
1410 }
1411 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001412
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001413 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001414 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001415 }
1416
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001417 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001418 }
1419
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001420 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001421}
1422sub ctx_block_outer {
1423 my ($linenr, $remain) = @_;
1424
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001425 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1426 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001427}
1428sub ctx_block {
1429 my ($linenr, $remain) = @_;
1430
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001431 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1432 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001433}
1434sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001435 my ($linenr, $remain, $off) = @_;
1436
1437 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1438 return @r;
1439}
1440sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001441 my ($linenr, $remain) = @_;
1442
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001443 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001444}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001445sub ctx_statement_level {
1446 my ($linenr, $remain, $off) = @_;
1447
1448 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1449}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001450
1451sub ctx_locate_comment {
1452 my ($first_line, $end_line) = @_;
1453
1454 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001455 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001456 return $current_comment if (defined $current_comment);
1457
1458 # Look through the context and try and figure out if there is a
1459 # comment.
1460 my $in_comment = 0;
1461 $current_comment = '';
1462 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001463 my $line = $rawlines[$linenr - 1];
1464 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001465 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1466 $in_comment = 1;
1467 }
1468 if ($line =~ m@/\*@) {
1469 $in_comment = 1;
1470 }
1471 if (!$in_comment && $current_comment ne '') {
1472 $current_comment = '';
1473 }
1474 $current_comment .= $line . "\n" if ($in_comment);
1475 if ($line =~ m@\*/@) {
1476 $in_comment = 0;
1477 }
1478 }
1479
1480 chomp($current_comment);
1481 return($current_comment);
1482}
1483sub ctx_has_comment {
1484 my ($first_line, $end_line) = @_;
1485 my $cmt = ctx_locate_comment($first_line, $end_line);
1486
Andy Whitcroft00df3442007-06-08 13:47:06 -07001487 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001488 ##print "CMMT: $cmt\n";
1489
1490 return ($cmt ne '');
1491}
1492
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001493sub raw_line {
1494 my ($linenr, $cnt) = @_;
1495
1496 my $offset = $linenr - 1;
1497 $cnt++;
1498
1499 my $line;
1500 while ($cnt) {
1501 $line = $rawlines[$offset++];
1502 next if (defined($line) && $line =~ /^-/);
1503 $cnt--;
1504 }
1505
1506 return $line;
1507}
1508
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001509sub cat_vet {
1510 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001511 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001512
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001513 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001514 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1515 $res .= $1;
1516 if ($2 ne '') {
1517 $coded = sprintf("^%c", unpack('C', $2) + 64);
1518 $res .= $coded;
1519 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001520 }
1521 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001522
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001523 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001524}
1525
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001526my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001527my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001528my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001529my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001530
1531sub annotate_reset {
1532 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001533 $av_pending = '_';
1534 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001535 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001536}
1537
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001538sub annotate_values {
1539 my ($stream, $type) = @_;
1540
1541 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001542 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001543 my $cur = $stream;
1544
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001545 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001546
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001547 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001548 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001549 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001550 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001551 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001552 print "WS($1)\n" if ($dbg_values > 1);
1553 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001554 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001555 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001556 }
1557
Florian Micklerc023e4732011-01-12 16:59:58 -08001558 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001559 print "CAST($1)\n" if ($dbg_values > 1);
1560 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001561 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001562
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001563 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001564 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001565 $type = 'T';
1566
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001567 } elsif ($cur =~ /^($Modifier)\s*/) {
1568 print "MODIFIER($1)\n" if ($dbg_values > 1);
1569 $type = 'T';
1570
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001571 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001572 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001573 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001574 push(@av_paren_type, $type);
1575 if ($2 ne '') {
1576 $av_pending = 'N';
1577 }
1578 $type = 'E';
1579
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001580 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001581 print "UNDEF($1)\n" if ($dbg_values > 1);
1582 $av_preprocessor = 1;
1583 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001584
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001585 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001586 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001587 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001588
1589 push(@av_paren_type, $type);
1590 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001591 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001592
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001593 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001594 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1595 $av_preprocessor = 1;
1596
1597 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1598
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001599 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001600
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001601 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001602 print "PRE_END($1)\n" if ($dbg_values > 1);
1603
1604 $av_preprocessor = 1;
1605
1606 # Assume all arms of the conditional end as this
1607 # one does, and continue as if the #endif was not here.
1608 pop(@av_paren_type);
1609 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001610 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001611
1612 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001613 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001614
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001615 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1616 print "ATTR($1)\n" if ($dbg_values > 1);
1617 $av_pending = $type;
1618 $type = 'N';
1619
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001620 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001621 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001622 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001623 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001624 }
1625 $type = 'N';
1626
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001627 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001628 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001629 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001630 $type = 'N';
1631
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001632 } elsif ($cur =~/^(case)/o) {
1633 print "CASE($1)\n" if ($dbg_values > 1);
1634 $av_pend_colon = 'C';
1635 $type = 'N';
1636
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001637 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001638 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001639 $type = 'N';
1640
1641 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001642 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001643 push(@av_paren_type, $av_pending);
1644 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001645 $type = 'N';
1646
1647 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001648 my $new_type = pop(@av_paren_type);
1649 if ($new_type ne '_') {
1650 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001651 print "PAREN('$1') -> $type\n"
1652 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001653 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001654 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001655 }
1656
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001657 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001658 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001659 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001660 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001661
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001662 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1663 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001664 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001665 } elsif ($type eq 'E') {
1666 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001667 }
1668 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1669 $type = 'V';
1670
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001671 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001672 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001673 $type = 'V';
1674
1675 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001676 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001677 $type = 'N';
1678
Andy Whitcroftcf655042008-03-04 14:28:20 -08001679 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001680 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001681 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001682 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001683
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001684 } elsif ($cur =~/^(,)/) {
1685 print "COMMA($1)\n" if ($dbg_values > 1);
1686 $type = 'C';
1687
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001688 } elsif ($cur =~ /^(\?)/o) {
1689 print "QUESTION($1)\n" if ($dbg_values > 1);
1690 $type = 'N';
1691
1692 } elsif ($cur =~ /^(:)/o) {
1693 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1694
1695 substr($var, length($res), 1, $av_pend_colon);
1696 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1697 $type = 'E';
1698 } else {
1699 $type = 'N';
1700 }
1701 $av_pend_colon = 'O';
1702
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001703 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001704 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001705 $type = 'N';
1706
Andy Whitcroft0d413862008-10-15 22:02:16 -07001707 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001708 my $variant;
1709
1710 print "OPV($1)\n" if ($dbg_values > 1);
1711 if ($type eq 'V') {
1712 $variant = 'B';
1713 } else {
1714 $variant = 'U';
1715 }
1716
1717 substr($var, length($res), 1, $variant);
1718 $type = 'N';
1719
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001720 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001721 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001722 if ($1 ne '++' && $1 ne '--') {
1723 $type = 'N';
1724 }
1725
1726 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001727 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 }
1729 if (defined $1) {
1730 $cur = substr($cur, length($1));
1731 $res .= $type x length($1);
1732 }
1733 }
1734
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001735 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001736}
1737
Andy Whitcroft8905a672007-11-28 16:21:06 -08001738sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001739 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001740 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001741 ^(?:
1742 $Modifier|
1743 $Storage|
1744 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001745 DEFINE_\S+
1746 )$|
1747 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001748 goto|
1749 return|
1750 case|
1751 else|
1752 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001753 do|
1754 \#|
1755 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001756 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001757 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001758 )}x;
1759 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1760 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001761 # Check for modifiers.
1762 $possible =~ s/\s*$Storage\s*//g;
1763 $possible =~ s/\s*$Sparse\s*//g;
1764 if ($possible =~ /^\s*$/) {
1765
1766 } elsif ($possible =~ /\s/) {
1767 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001768 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001769 if ($modifier !~ $notPermitted) {
1770 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001771 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001772 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001773 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001774
1775 } else {
1776 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001777 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001778 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001779 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001780 } else {
1781 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001782 }
1783}
1784
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001785my $prefix = '';
1786
Joe Perches000d1cc12011-07-25 17:13:25 -07001787sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001788 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001789
Joe Perchescbec18a2014-04-03 14:49:19 -07001790 return defined $use_type{$type} if (scalar keys %use_type > 0);
1791
1792 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001793}
1794
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001795sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001796 my ($level, $type, $msg) = @_;
1797
1798 if (!show_type($type) ||
1799 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001800 return 0;
1801 }
Joe Perches57230292015-06-25 15:03:03 -07001802 my $output = '';
1803 if (-t STDOUT && $color) {
1804 if ($level eq 'ERROR') {
1805 $output .= RED;
1806 } elsif ($level eq 'WARNING') {
1807 $output .= YELLOW;
1808 } else {
1809 $output .= GREEN;
1810 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001811 }
Joe Perches57230292015-06-25 15:03:03 -07001812 $output .= $prefix . $level . ':';
1813 if ($show_types) {
1814 $output .= BLUE if (-t STDOUT && $color);
1815 $output .= "$type:";
1816 }
1817 $output .= RESET if (-t STDOUT && $color);
1818 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001819
1820 if ($showfile) {
1821 my @lines = split("\n", $output, -1);
1822 splice(@lines, 1, 1);
1823 $output = join("\n", @lines);
1824 }
Joe Perches57230292015-06-25 15:03:03 -07001825 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001826
Joe Perches57230292015-06-25 15:03:03 -07001827 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001828
1829 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001830}
Joe Perchescbec18a2014-04-03 14:49:19 -07001831
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001832sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001833 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001834}
Joe Perches000d1cc12011-07-25 17:13:25 -07001835
Joe Perchesd752fcc2014-08-06 16:11:05 -07001836sub fixup_current_range {
1837 my ($lineRef, $offset, $length) = @_;
1838
1839 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1840 my $o = $1;
1841 my $l = $2;
1842 my $no = $o + $offset;
1843 my $nl = $l + $length;
1844 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1845 }
1846}
1847
1848sub fix_inserted_deleted_lines {
1849 my ($linesRef, $insertedRef, $deletedRef) = @_;
1850
1851 my $range_last_linenr = 0;
1852 my $delta_offset = 0;
1853
1854 my $old_linenr = 0;
1855 my $new_linenr = 0;
1856
1857 my $next_insert = 0;
1858 my $next_delete = 0;
1859
1860 my @lines = ();
1861
1862 my $inserted = @{$insertedRef}[$next_insert++];
1863 my $deleted = @{$deletedRef}[$next_delete++];
1864
1865 foreach my $old_line (@{$linesRef}) {
1866 my $save_line = 1;
1867 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001868 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001869 $delta_offset = 0;
1870 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1871 $range_last_linenr = $new_linenr;
1872 fixup_current_range(\$line, $delta_offset, 0);
1873 }
1874
1875 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1876 $deleted = @{$deletedRef}[$next_delete++];
1877 $save_line = 0;
1878 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1879 }
1880
1881 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1882 push(@lines, ${$inserted}{'LINE'});
1883 $inserted = @{$insertedRef}[$next_insert++];
1884 $new_linenr++;
1885 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1886 }
1887
1888 if ($save_line) {
1889 push(@lines, $line);
1890 $new_linenr++;
1891 }
1892
1893 $old_linenr++;
1894 }
1895
1896 return @lines;
1897}
1898
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07001899sub fix_insert_line {
1900 my ($linenr, $line) = @_;
1901
1902 my $inserted = {
1903 LINENR => $linenr,
1904 LINE => $line,
1905 };
1906 push(@fixed_inserted, $inserted);
1907}
1908
1909sub fix_delete_line {
1910 my ($linenr, $line) = @_;
1911
1912 my $deleted = {
1913 LINENR => $linenr,
1914 LINE => $line,
1915 };
1916
1917 push(@fixed_deleted, $deleted);
1918}
1919
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001920sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07001921 my ($type, $msg) = @_;
1922
1923 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001924 our $clean = 0;
1925 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07001926 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001927 }
Joe Perches3705ce52013-07-03 15:05:31 -07001928 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001929}
1930sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07001931 my ($type, $msg) = @_;
1932
1933 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001934 our $clean = 0;
1935 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07001936 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001937 }
Joe Perches3705ce52013-07-03 15:05:31 -07001938 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001939}
1940sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07001941 my ($type, $msg) = @_;
1942
1943 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001944 our $clean = 0;
1945 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07001946 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001947 }
Joe Perches3705ce52013-07-03 15:05:31 -07001948 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07001949}
1950
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001951sub check_absolute_file {
1952 my ($absolute, $herecurr) = @_;
1953 my $file = $absolute;
1954
1955 ##print "absolute<$absolute>\n";
1956
1957 # See if any suffix of this path is a path within the tree.
1958 while ($file =~ s@^[^/]*/@@) {
1959 if (-f "$root/$file") {
1960 ##print "file<$file>\n";
1961 last;
1962 }
1963 }
1964 if (! -f _) {
1965 return 0;
1966 }
1967
1968 # It is, so see if the prefix is acceptable.
1969 my $prefix = $absolute;
1970 substr($prefix, -length($file)) = '';
1971
1972 ##print "prefix<$prefix>\n";
1973 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07001974 WARN("USE_RELATIVE_PATH",
1975 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07001976 }
1977}
1978
Joe Perches3705ce52013-07-03 15:05:31 -07001979sub trim {
1980 my ($string) = @_;
1981
Joe Perchesb34c6482013-09-11 14:24:01 -07001982 $string =~ s/^\s+|\s+$//g;
1983
1984 return $string;
1985}
1986
1987sub ltrim {
1988 my ($string) = @_;
1989
1990 $string =~ s/^\s+//;
1991
1992 return $string;
1993}
1994
1995sub rtrim {
1996 my ($string) = @_;
1997
1998 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07001999
2000 return $string;
2001}
2002
Joe Perches52ea8502013-11-12 15:10:09 -08002003sub string_find_replace {
2004 my ($string, $find, $replace) = @_;
2005
2006 $string =~ s/$find/$replace/g;
2007
2008 return $string;
2009}
2010
Joe Perches3705ce52013-07-03 15:05:31 -07002011sub tabify {
2012 my ($leading) = @_;
2013
2014 my $source_indent = 8;
2015 my $max_spaces_before_tab = $source_indent - 1;
2016 my $spaces_to_tab = " " x $source_indent;
2017
2018 #convert leading spaces to tabs
2019 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2020 #Remove spaces before a tab
2021 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2022
2023 return "$leading";
2024}
2025
Gregory Beanf07523e2011-07-06 15:36:58 -07002026sub cleanup_continuation_headers {
2027 # Collapse any header-continuation lines into a single line so they
2028 # can be parsed meaningfully, as the parser only has one line
2029 # of context to work with.
2030 my $again;
2031 do {
2032 $again = 0;
2033 foreach my $n (0 .. scalar(@rawlines) - 2) {
2034 if ($rawlines[$n]=~/^\s*$/) {
2035 # A blank line means there's no more chance
2036 # of finding headers. Shortcut to done.
2037 return;
2038 }
2039 if ($rawlines[$n]=~/^[\x21-\x39\x3b-\x7e]+:/ &&
2040 $rawlines[$n+1]=~/^\s+/) {
2041 # Continuation header. Collapse it.
2042 my $line = splice @rawlines, $n+1, 1;
2043 $line=~s/^\s+/ /;
2044 $rawlines[$n] .= $line;
2045 # We've 'destabilized' the list, so restart.
2046 $again = 1;
2047 last;
2048 }
2049 }
2050 } while ($again);
2051}
2052
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002053sub pos_last_openparen {
2054 my ($line) = @_;
2055
2056 my $pos = 0;
2057
2058 my $opens = $line =~ tr/\(/\(/;
2059 my $closes = $line =~ tr/\)/\)/;
2060
2061 my $last_openparen = 0;
2062
2063 if (($opens == 0) || ($closes >= $opens)) {
2064 return -1;
2065 }
2066
2067 my $len = length($line);
2068
2069 for ($pos = 0; $pos < $len; $pos++) {
2070 my $string = substr($line, $pos);
2071 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2072 $pos += length($1) - 1;
2073 } elsif (substr($line, $pos, 1) eq '(') {
2074 $last_openparen = $pos;
2075 } elsif (index($string, '(') == -1) {
2076 last;
2077 }
2078 }
2079
Joe Perches91cb5192014-04-03 14:49:32 -07002080 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002081}
2082
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002083sub process {
2084 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002085
2086 my $linenr=0;
2087 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002088 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002089 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002090 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002091
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002092 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002093 my $indent;
2094 my $previndent=0;
2095 my $stashindent=0;
2096
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002097 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002098 my $signoff = 0;
2099 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002100 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002101 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002102 my $has_commit_log = 0; #Encountered lines before patch
Joe Perchesbf4daf12015-09-09 15:37:50 -07002103 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002104 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002105 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002106 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002107 my $non_utf8_charset = 0;
2108
Joe Perches365dd4e2014-08-06 16:10:42 -07002109 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002110 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002111
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002112 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002113 our $cnt_lines = 0;
2114 our $cnt_error = 0;
2115 our $cnt_warn = 0;
2116 our $cnt_chk = 0;
2117
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002118 # Trace the real file/line as we go.
2119 my $realfile = '';
2120 my $realline = 0;
2121 my $realcnt = 0;
2122 my $here = '';
2123 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002124 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002125 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002126 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002127
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002128 my $prev_values = 'E';
2129
2130 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002131 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002132 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002133 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002134 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002135
Joe Perches7e51f192013-09-11 14:23:57 -07002136 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002137
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002138 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002139 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002140 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002141 my @setup_docs = ();
2142 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002143
Joe Perchesd8b07712013-11-12 15:10:06 -08002144 my $camelcase_file_seeded = 0;
2145
Andy Whitcroft773647a2008-03-28 14:15:58 -07002146 sanitise_line_reset();
Gregory Beanf07523e2011-07-06 15:36:58 -07002147 cleanup_continuation_headers();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002148 my $line;
Gregory Beanf07523e2011-07-06 15:36:58 -07002149
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002150 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002151 $linenr++;
2152 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002153
Joe Perches3705ce52013-07-03 15:05:31 -07002154 push(@fixed, $rawline) if ($fix);
2155
Andy Whitcroft773647a2008-03-28 14:15:58 -07002156 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002157 $setup_docs = 0;
2158 if ($1 =~ m@Documentation/kernel-parameters.txt$@) {
2159 $setup_docs = 1;
2160 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002161 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002162 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002163 if ($rawline=~/^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
2164 $realline=$1-1;
2165 if (defined $2) {
2166 $realcnt=$3+1;
2167 } else {
2168 $realcnt=1+1;
2169 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002170 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002171
2172 # Guestimate if this is a continuing comment. Run
2173 # the context looking for a comment "edge". If this
2174 # edge is a close comment then we must be in a comment
2175 # at context start.
2176 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002177 my $cnt = $realcnt;
2178 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2179 next if (defined $rawlines[$ln - 1] &&
2180 $rawlines[$ln - 1] =~ /^-/);
2181 $cnt--;
2182 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002183 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002184 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2185 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2186 ($edge) = $1;
2187 last;
2188 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002189 }
2190 if (defined $edge && $edge eq '*/') {
2191 $in_comment = 1;
2192 }
2193
2194 # Guestimate if this is a continuing comment. If this
2195 # is the start of a diff block and this line starts
2196 # ' *' then it is very likely a comment.
2197 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002198 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002199 {
2200 $in_comment = 1;
2201 }
2202
2203 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2204 sanitise_line_reset($in_comment);
2205
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002206 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002207 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002208 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002209 $line = sanitise_line($rawline);
2210 }
2211 push(@lines, $line);
2212
2213 if ($realcnt > 1) {
2214 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2215 } else {
2216 $realcnt = 0;
2217 }
2218
2219 #print "==>$rawline\n";
2220 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002221
2222 if ($setup_docs && $line =~ /^\+/) {
2223 push(@setup_docs, $line);
2224 }
2225 }
2226
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002227 $prefix = '';
2228
Andy Whitcroft773647a2008-03-28 14:15:58 -07002229 $realcnt = 0;
2230 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002231 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002232 foreach my $line (@lines) {
2233 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002234 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002235 my $sline = $line; #copy of $line
2236 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002237
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002238 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002239
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002240#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002241 if (!$in_commit_log &&
2242 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002243 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002244 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002245 $realline=$1-1;
2246 if (defined $2) {
2247 $realcnt=$3+1;
2248 } else {
2249 $realcnt=1+1;
2250 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002251 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002252 $prev_values = 'E';
2253
Andy Whitcroft773647a2008-03-28 14:15:58 -07002254 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002255 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002256 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002257 $suppress_statement = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002258 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002259
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002260# track the line number as we move through the hunk, note that
2261# new versions of GNU diff omit the leading space on completely
2262# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002263 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002264 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002265 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002266
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002267 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002268 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002269
2270 # Track the previous line.
2271 ($prevline, $stashline) = ($stashline, $line);
2272 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002273 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2274
Andy Whitcroft773647a2008-03-28 14:15:58 -07002275 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002276
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002277 } elsif ($realcnt == 1) {
2278 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002279 }
2280
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002281 my $hunk_line = ($realcnt != 0);
2282
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002283 $here = "#$linenr: " if (!$file);
2284 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002286 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002287 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002288 if ($line =~ /^diff --git.*?(\S+)$/) {
2289 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002290 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002291 $in_commit_log = 0;
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002292 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002293 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002294 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002295 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002296 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002297
2298 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002299 if (!$file && $tree && $p1_prefix ne '' &&
2300 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002301 WARN("PATCH_PREFIX",
2302 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002303 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002304
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002305 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002306 ERROR("MODIFIED_INCLUDE_ASM",
2307 "do not modify files in include/asm, change architecture specific files in include/asm-<architecture>\n" . "$here$rawline\n");
Andy Whitcroft773647a2008-03-28 14:15:58 -07002308 }
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002309 $found_file = 1;
2310 }
2311
Joe Perches34d88152015-06-25 15:03:05 -07002312#make up the handle for any error we report on this line
2313 if ($showfile) {
2314 $prefix = "$realfile:$realline: "
2315 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002316 if ($file) {
2317 $prefix = "$filename:$realline: ";
2318 } else {
2319 $prefix = "$filename:$linenr: ";
2320 }
Joe Perches34d88152015-06-25 15:03:05 -07002321 }
2322
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002323 if ($found_file) {
Joe Perches7bd7e482015-09-09 15:37:44 -07002324 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b4f2014-06-04 16:12:05 -07002325 $check = 1;
2326 } else {
2327 $check = $check_orig;
2328 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002329 next;
2330 }
2331
Randy Dunlap389834b2007-06-08 13:47:03 -07002332 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002333
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002334 my $hereline = "$here\n$rawline\n";
2335 my $herecurr = "$here\n$rawline\n";
2336 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002337
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002338 $cnt_lines++ if ($realcnt != 0);
2339
Joe Perchese518e9a2015-06-25 15:03:27 -07002340# Check if the commit log has what seems like a diff which can confuse patch
2341 if ($in_commit_log && !$commit_log_has_diff &&
2342 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2343 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2344 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2345 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2346 ERROR("DIFF_IN_COMMIT_MSG",
2347 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2348 $commit_log_has_diff = 1;
2349 }
2350
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002351# Check for incorrect file permissions
2352 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2353 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002354 if ($realfile !~ m@scripts/@ &&
2355 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002356 ERROR("EXECUTE_PERMISSIONS",
2357 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002358 }
2359 }
2360
Joe Perches20112472011-07-25 17:13:23 -07002361# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002362 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002363 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002364 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002365 }
2366
Joe Perchese0d975b2014-12-10 15:51:49 -08002367# Check if MAINTAINERS is being updated. If so, there's probably no need to
2368# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2369 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2370 $reported_maintainer_file = 1;
2371 }
2372
Joe Perches20112472011-07-25 17:13:23 -07002373# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002374 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002375 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002376 my $space_before = $1;
2377 my $sign_off = $2;
2378 my $space_after = $3;
2379 my $email = $4;
2380 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2381
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002382 if ($sign_off !~ /$signature_tags/) {
2383 WARN("BAD_SIGN_OFF",
2384 "Non-standard signature: $sign_off\n" . $herecurr);
2385 }
Joe Perches20112472011-07-25 17:13:23 -07002386 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002387 if (WARN("BAD_SIGN_OFF",
2388 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2389 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002390 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002391 "$ucfirst_sign_off $email";
2392 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002393 }
Joe Perches20112472011-07-25 17:13:23 -07002394 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002395 if (WARN("BAD_SIGN_OFF",
2396 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2397 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002398 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002399 "$ucfirst_sign_off $email";
2400 }
2401
Joe Perches20112472011-07-25 17:13:23 -07002402 }
2403 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002404 if (WARN("BAD_SIGN_OFF",
2405 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2406 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002407 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002408 "$ucfirst_sign_off $email";
2409 }
Joe Perches20112472011-07-25 17:13:23 -07002410 }
2411
2412 my ($email_name, $email_address, $comment) = parse_email($email);
2413 my $suggested_email = format_email(($email_name, $email_address));
2414 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002415 ERROR("BAD_SIGN_OFF",
2416 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002417 } else {
2418 my $dequoted = $suggested_email;
2419 $dequoted =~ s/^"//;
2420 $dequoted =~ s/" </ </;
2421 # Don't force email to have quotes
2422 # Allow just an angle bracketed address
2423 if ("$dequoted$comment" ne $email &&
2424 "<$email_address>$comment" ne $email &&
2425 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002426 WARN("BAD_SIGN_OFF",
2427 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002428 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002429 }
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002430 if ($chk_author && $line =~ /^\s*signed-off-by:.*(quicinc|qualcomm)\.com/i) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002431 WARN("BAD_SIGN_OFF",
2432 "invalid Signed-off-by identity\n" . $line );
2433 }
Joe Perches7e51f192013-09-11 14:23:57 -07002434
2435# Check for duplicate signatures
2436 my $sig_nospace = $line;
2437 $sig_nospace =~ s/\s//g;
2438 $sig_nospace = lc($sig_nospace);
2439 if (defined $signatures{$sig_nospace}) {
2440 WARN("BAD_SIGN_OFF",
2441 "Duplicate signature\n" . $herecurr);
2442 } else {
2443 $signatures{$sig_nospace} = 1;
2444 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002445 }
2446
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002447# Check email subject for common tools that don't need to be mentioned
2448 if ($in_header_lines &&
2449 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2450 WARN("EMAIL_SUBJECT",
2451 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2452 }
2453
Joe Perches9b3189e2014-06-04 16:12:10 -07002454# Check for old stable address
2455 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2456 ERROR("STABLE_ADDRESS",
2457 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2458 }
2459
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002460# Check for unwanted Gerrit info
2461 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2462 ERROR("GERRIT_CHANGE_ID",
2463 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2464 }
2465
Joe Perches369c8dd2015-11-06 16:31:34 -08002466# Check if the commit log is in a possible stack dump
2467 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2468 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2469 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2470 # timestamp
2471 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2472 # stack dump address
2473 $commit_log_possible_stack_dump = 1;
2474 }
2475
Joe Perches2a076f42015-04-16 12:44:28 -07002476# Check for line lengths > 75 in commit log, warn once
2477 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002478 length($line) > 75 &&
2479 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2480 # file delta changes
2481 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2482 # filename then :
2483 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2484 # A Fixes: or Link: line
2485 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002486 WARN("COMMIT_LOG_LONG_LINE",
2487 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2488 $commit_log_long_line = 1;
2489 }
2490
Joe Perchesbf4daf12015-09-09 15:37:50 -07002491# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002492 if ($in_commit_log && $commit_log_possible_stack_dump &&
2493 $line =~ /^\s*$/) {
2494 $commit_log_possible_stack_dump = 0;
2495 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002496
Joe Perches0d7835f2015-02-13 14:38:35 -08002497# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002498 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002499 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002500 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002501 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002502 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2503 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002504 my $init_char = "c";
2505 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002506 my $short = 1;
2507 my $long = 0;
2508 my $case = 1;
2509 my $space = 1;
2510 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002511 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002512 my $id = '0123456789ab';
2513 my $orig_desc = "commit description";
2514 my $description = "";
2515
Joe Perchesfe043ea2015-09-09 15:37:25 -07002516 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2517 $init_char = $1;
2518 $orig_commit = lc($2);
2519 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2520 $orig_commit = lc($1);
2521 }
2522
Joe Perches0d7835f2015-02-13 14:38:35 -08002523 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2524 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2525 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2526 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2527 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2528 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002529 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002530 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2531 defined $rawlines[$linenr] &&
2532 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2533 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002534 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002535 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2536 defined $rawlines[$linenr] &&
2537 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2538 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2539 $orig_desc = $1;
2540 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2541 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002542 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002543 }
2544
2545 ($id, $description) = git_commit_info($orig_commit,
2546 $id, $orig_desc);
2547
Joe Perches19c146a2015-02-13 14:39:00 -08002548 if ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002549 ERROR("GIT_COMMIT_ID",
2550 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2551 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002552 }
2553
Joe Perches13f19372014-08-06 16:10:59 -07002554# Check for added, moved or deleted files
2555 if (!$reported_maintainer_file && !$in_commit_log &&
2556 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2557 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2558 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2559 (defined($1) || defined($2))))) {
2560 $reported_maintainer_file = 1;
2561 WARN("FILE_PATH_CHANGES",
2562 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2563 }
2564
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002565#check the patch for invalid author credentials
Jeff Johnsondb9dbec2015-01-22 13:34:29 -08002566 if ($chk_author && $line =~ /^From:.*(quicinc|qualcomm)\.com/) {
Bryan Huntsmanb000c782010-05-04 17:31:32 -07002567 WARN("BAD_AUTHOR", "invalid author identity\n" . $line );
2568 }
2569
Andy Whitcroft00df3442007-06-08 13:47:06 -07002570# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002571 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002572 ERROR("CORRUPTED_PATCH",
2573 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002574 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002575 }
2576
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002577# Check for absolute kernel paths.
2578 if ($tree) {
2579 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2580 my $file = $1;
2581
2582 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2583 check_absolute_file($1, $herecurr)) {
2584 #
2585 } else {
2586 check_absolute_file($file, $herecurr);
2587 }
2588 }
2589 }
2590
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002591# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2592 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002593 $rawline !~ m/^$UTF8*$/) {
2594 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2595
2596 my $blank = copy_spacing($rawline);
2597 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2598 my $hereptr = "$hereline$ptr\n";
2599
Joe Perches34d99212011-07-25 17:13:26 -07002600 CHK("INVALID_UTF8",
2601 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002602 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002603
Joe Perches15662b32011-10-31 17:13:12 -07002604# Check if it's the start of a commit log
2605# (not a header line and we haven't seen the patch filename)
2606 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Perches29ee1b02014-08-06 16:10:35 -07002607 !($rawline =~ /^\s+\S/ ||
2608 $rawline =~ /^(commit\b|from\b|[\w-]+:).*$/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002609 $in_header_lines = 0;
2610 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002611 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002612 }
2613
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002614# Check if there is UTF-8 in a commit log when a mail header has explicitly
2615# declined it, i.e defined some charset where it is missing.
2616 if ($in_header_lines &&
2617 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2618 $1 !~ /utf-8/i) {
2619 $non_utf8_charset = 1;
2620 }
2621
2622 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002623 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002624 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002625 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2626 }
2627
Kees Cook66b47b42014-10-13 15:51:57 -07002628# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002629 if (defined($misspellings) &&
2630 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002631 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002632 my $typo = $1;
2633 my $typo_fix = $spelling_fix{lc($typo)};
2634 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2635 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2636 my $msg_type = \&WARN;
2637 $msg_type = \&CHK if ($file);
2638 if (&{$msg_type}("TYPO_SPELLING",
2639 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2640 $fix) {
2641 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2642 }
2643 }
2644 }
2645
Andy Whitcroft306708542008-10-15 22:02:28 -07002646# ignore non-hunk lines and lines being removed
2647 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002648
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002649#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002650 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002651 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002652 if (ERROR("DOS_LINE_ENDINGS",
2653 "DOS line endings\n" . $herevet) &&
2654 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002655 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002656 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002657 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2658 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002659 if (ERROR("TRAILING_WHITESPACE",
2660 "trailing whitespace\n" . $herevet) &&
2661 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002662 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002663 }
2664
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002665 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002666 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002667
Josh Triplett4783f892013-11-12 15:10:12 -08002668# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002669 if ($rawline =~ /\bwrite to the Free/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002670 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2671 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002672 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2673 my $msg_type = \&ERROR;
2674 $msg_type = \&CHK if ($file);
2675 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002676 "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)
Josh Triplett4783f892013-11-12 15:10:12 -08002677 }
2678
Andi Kleen33549572010-05-24 14:33:29 -07002679# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002680# Only applies when adding the entry originally, after that we do not have
2681# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002682 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002683 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002684 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002685 my $cnt = $realcnt;
2686 my $ln = $linenr + 1;
2687 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002688 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002689 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002690 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002691 $f = $lines[$ln - 1];
2692 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2693 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002694
2695 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002696 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002697
Joe Perches8d73e0e2014-08-06 16:10:46 -07002698 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002699 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002700 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002701 $length = -1;
2702 }
2703
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002704 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002705 $f =~ s/#.*//;
2706 $f =~ s/^\s+//;
2707 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002708 if ($f =~ /^\s*config\s/) {
2709 $is_end = 1;
2710 last;
2711 }
Andi Kleen33549572010-05-24 14:33:29 -07002712 $length++;
2713 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002714 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2715 WARN("CONFIG_DESCRIPTION",
2716 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2717 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002718 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002719 }
2720
Kees Cook1ba8dfd2012-12-17 16:01:48 -08002721# discourage the addition of CONFIG_EXPERIMENTAL in Kconfig.
2722 if ($realfile =~ /Kconfig/ &&
2723 $line =~ /.\s*depends on\s+.*\bEXPERIMENTAL\b/) {
2724 WARN("CONFIG_EXPERIMENTAL",
2725 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
2726 }
2727
Christoph Jaeger327953e2015-02-13 14:38:29 -08002728# discourage the use of boolean for type definition attributes of Kconfig options
2729 if ($realfile =~ /Kconfig/ &&
2730 $line =~ /^\+\s*\bboolean\b/) {
2731 WARN("CONFIG_TYPE_BOOLEAN",
2732 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2733 }
2734
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002735 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2736 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2737 my $flag = $1;
2738 my $replacement = {
2739 'EXTRA_AFLAGS' => 'asflags-y',
2740 'EXTRA_CFLAGS' => 'ccflags-y',
2741 'EXTRA_CPPFLAGS' => 'cppflags-y',
2742 'EXTRA_LDFLAGS' => 'ldflags-y',
2743 };
2744
2745 WARN("DEPRECATED_VARIABLE",
2746 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2747 }
2748
Rob Herringbff5da42014-01-23 15:54:51 -08002749# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002750 if (defined $root &&
2751 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2752 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2753
Rob Herringbff5da42014-01-23 15:54:51 -08002754 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2755
Florian Vaussardcc933192014-04-03 14:49:27 -07002756 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2757 my $vp_file = $dt_path . "vendor-prefixes.txt";
2758
Rob Herringbff5da42014-01-23 15:54:51 -08002759 foreach my $compat (@compats) {
2760 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002761 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2762 my $compat3 = $compat;
2763 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2764 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002765 if ( $? >> 8 ) {
2766 WARN("UNDOCUMENTED_DT_STRING",
2767 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2768 }
2769
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002770 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2771 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002772 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002773 if ( $? >> 8 ) {
2774 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002775 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002776 }
2777 }
2778 }
2779
Andy Whitcroft5368df22008-10-15 22:02:27 -07002780# check we are in a valid source file if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002781 next if ($realfile !~ /\.(h|c|s|S|pl|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002782
Joe Perches47e0c882015-06-25 15:02:57 -07002783# line length limit (with some exclusions)
2784#
2785# There are a few types of lines that may extend beyond $max_line_length:
2786# logging functions like pr_info that end in a string
2787# lines with a single string
2788# #defines that are a single string
2789#
2790# There are 3 different line length message types:
2791# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2792# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2793# LONG_LINE all other lines longer than $max_line_length
2794#
2795# if LONG_LINE is ignored, the other 2 types are also ignored
2796#
Israel Schlesingerc5955792010-07-27 13:28:26 -07002797 if ($line =~ /^\+/ && $length > $max_line_length && $realfile ne "scripts/checkpatch.pl") {
Joe Perches47e0c882015-06-25 15:02:57 -07002798 my $msg_type = "LONG_LINE";
2799
2800 # Check the allowed long line types first
2801
2802 # logging functions that end in a string that starts
2803 # before $max_line_length
2804 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2805 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2806 $msg_type = "";
2807
2808 # lines with only strings (w/ possible termination)
2809 # #defines with only strings
2810 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2811 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2812 $msg_type = "";
2813
Joe Perchesd560a5f2016-08-02 14:04:31 -07002814 # EFI_GUID is another special case
2815 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2816 $msg_type = "";
2817
Joe Perches47e0c882015-06-25 15:02:57 -07002818 # Otherwise set the alternate message types
2819
2820 # a comment starts before $max_line_length
2821 } elsif ($line =~ /($;[\s$;]*)$/ &&
2822 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2823 $msg_type = "LONG_LINE_COMMENT"
2824
2825 # a quoted string starts before $max_line_length
2826 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2827 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2828 $msg_type = "LONG_LINE_STRING"
2829 }
2830
2831 if ($msg_type ne "" &&
2832 (show_type("LONG_LINE") || show_type($msg_type))) {
2833 WARN($msg_type,
2834 "line over $max_line_length characters\n" . $herecurr);
2835 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002836 }
2837
Andy Whitcroft8905a672007-11-28 16:21:06 -08002838# check for adding lines without a newline.
2839 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002840 WARN("MISSING_EOF_NEWLINE",
2841 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002842 }
2843
Mike Frysinger42e41c52009-09-21 17:04:40 -07002844# Blackfin: use hi/lo macros
2845 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2846 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2847 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002848 ERROR("LO_MACRO",
2849 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002850 }
2851 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2852 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002853 ERROR("HI_MACRO",
2854 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002855 }
2856 }
2857
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002858# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002859 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002860
2861# at the beginning of a line any tabs must come first and anything
2862# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002863 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2864 $rawline =~ /^\+\s* \s*/) {
2865 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002866 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002867 if (ERROR("CODE_INDENT",
2868 "code indent should use tabs where possible\n" . $herevet) &&
2869 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002870 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002871 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002872 }
2873
Alberto Panizzo08e44362010-03-05 13:43:54 -08002874# check for space before tabs.
2875 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2876 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002877 if (WARN("SPACE_BEFORE_TAB",
2878 "please, no space before tabs\n" . $herevet) &&
2879 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002880 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002881 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002882 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002883 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002884 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002885 }
2886
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002887# check for && or || at the start of a line
2888 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2889 CHK("LOGICAL_CONTINUATIONS",
2890 "Logical continuations should be on the previous line\n" . $hereprev);
2891 }
2892
Joe Perchesa91e8992016-05-20 17:04:05 -07002893# check indentation starts on a tab stop
2894 if ($^V && $^V ge 5.10.0 &&
2895 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2896 my $indent = length($1);
2897 if ($indent % 8) {
2898 if (WARN("TABSTOP",
2899 "Statements should start on a tabstop\n" . $herecurr) &&
2900 $fix) {
2901 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2902 }
2903 }
2904 }
2905
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002906# check multi-line statement indentation matches previous line
2907 if ($^V && $^V ge 5.10.0 &&
Joe Perches91cb5192014-04-03 14:49:32 -07002908 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|$Ident\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002909 $prevline =~ /^\+(\t*)(.*)$/;
2910 my $oldindent = $1;
2911 my $rest = $2;
2912
2913 my $pos = pos_last_openparen($rest);
2914 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07002915 $line =~ /^(\+| )([ \t]*)/;
2916 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002917
2918 my $goodtabindent = $oldindent .
2919 "\t" x ($pos / 8) .
2920 " " x ($pos % 8);
2921 my $goodspaceindent = $oldindent . " " x $pos;
2922
2923 if ($newindent ne $goodtabindent &&
2924 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07002925
2926 if (CHK("PARENTHESIS_ALIGNMENT",
2927 "Alignment should match open parenthesis\n" . $hereprev) &&
2928 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07002929 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07002930 s/^\+[ \t]*/\+$goodtabindent/;
2931 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002932 }
2933 }
2934 }
2935
Joe Perches6ab3a972015-04-16 12:44:05 -07002936# check for space after cast like "(int) foo" or "(struct foo) bar"
2937# avoid checking a few false positives:
2938# "sizeof(<type>)" or "__alignof__(<type>)"
2939# function pointer declarations like "(*foo)(int) = bar;"
2940# structure definitions like "(struct foo) { 0 };"
2941# multiline macros that define functions
2942# known attributes or the __attribute__ keyword
2943 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
2944 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07002945 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07002946 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07002947 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002948 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07002949 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07002950 }
Joe Perchesaad4f612012-03-23 15:02:19 -07002951 }
2952
Joe Perches86406b12015-09-09 15:37:41 -07002953# Block comment styles
2954# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07002955 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07002956 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07002957 $rawline =~ /^\+[ \t]*\*/ &&
2958 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07002959 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
2960 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
2961 }
2962
Joe Perches86406b12015-09-09 15:37:41 -07002963# Block comments use * on subsequent lines
2964 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
2965 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07002966 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07002967 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07002968 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07002969 WARN("BLOCK_COMMENT_STYLE",
2970 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07002971 }
2972
Joe Perches86406b12015-09-09 15:37:41 -07002973# Block comments use */ on trailing lines
2974 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08002975 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
2976 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
2977 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07002978 WARN("BLOCK_COMMENT_STYLE",
2979 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07002980 }
2981
Joe Perches7f619192014-08-06 16:10:39 -07002982# check for missing blank lines after struct/union declarations
2983# with exceptions for various attributes and macros
2984 if ($prevline =~ /^[\+ ]};?\s*$/ &&
2985 $line =~ /^\+/ &&
2986 !($line =~ /^\+\s*$/ ||
2987 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
2988 $line =~ /^\+\s*MODULE_/i ||
2989 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
2990 $line =~ /^\+[a-z_]*init/ ||
2991 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
2992 $line =~ /^\+\s*DECLARE/ ||
2993 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07002994 if (CHK("LINE_SPACING",
2995 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
2996 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002997 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07002998 }
Joe Perches7f619192014-08-06 16:10:39 -07002999 }
3000
Joe Perches365dd4e2014-08-06 16:10:42 -07003001# check for multiple consecutive blank lines
3002 if ($prevline =~ /^[\+ ]\s*$/ &&
3003 $line =~ /^\+\s*$/ &&
3004 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003005 if (CHK("LINE_SPACING",
3006 "Please don't use multiple blank lines\n" . $hereprev) &&
3007 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003008 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003009 }
3010
Joe Perches365dd4e2014-08-06 16:10:42 -07003011 $last_blank_line = $linenr;
3012 }
3013
Joe Perches3b617e32014-04-03 14:49:28 -07003014# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003015 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3016 # actual declarations
3017 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003018 # function pointer declarations
3019 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003020 # foo bar; where foo is some local typedef or #define
3021 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3022 # known declaration macros
3023 $prevline =~ /^\+\s+$declaration_macros/) &&
3024 # for "else if" which can look like "$Ident $Ident"
3025 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3026 # other possible extensions of declaration lines
3027 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3028 # not starting a section or a macro "\" extended line
3029 $prevline =~ /(?:\{\s*|\\)$/) &&
3030 # looks like a declaration
3031 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003032 # function pointer declarations
3033 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003034 # foo bar; where foo is some local typedef or #define
3035 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3036 # known declaration macros
3037 $sline =~ /^\+\s+$declaration_macros/ ||
3038 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003039 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003040 # start or end of block or continuation of declaration
3041 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3042 # bitfield continuation
3043 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3044 # other possible extensions of declaration lines
3045 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3046 # indentation of previous and current line are the same
3047 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003048 if (WARN("LINE_SPACING",
3049 "Missing a blank line after declarations\n" . $hereprev) &&
3050 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003051 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003052 }
Joe Perches3b617e32014-04-03 14:49:28 -07003053 }
3054
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003055# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003056# Exceptions:
3057# 1) within comments
3058# 2) indented preprocessor commands
3059# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003060 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003061 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003062 if (WARN("LEADING_SPACE",
3063 "please, no spaces at the start of a line\n" . $herevet) &&
3064 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003065 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003066 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003067 }
3068
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003069# check we are in a valid C source file if not then ignore this hunk
3070 next if ($realfile !~ /\.(h|c)$/);
3071
Joe Perches032a4c02014-08-06 16:10:29 -07003072# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003073# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003074# if the previous line is a break or return and is indented 1 tab more...
3075 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3076 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003077 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3078 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3079 defined $lines[$linenr] &&
3080 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003081 WARN("UNNECESSARY_ELSE",
3082 "else is not generally useful after a break or return\n" . $hereprev);
3083 }
3084 }
3085
Joe Perchesc00df192014-08-06 16:11:01 -07003086# check indentation of a line with a break;
3087# if the previous line is a goto or return and is indented the same # of tabs
3088 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3089 my $tabs = $1;
3090 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3091 WARN("UNNECESSARY_BREAK",
3092 "break is not useful after a goto or return\n" . $hereprev);
3093 }
3094 }
3095
Kees Cook1ba8dfd2012-12-17 16:01:48 -08003096# discourage the addition of CONFIG_EXPERIMENTAL in #if(def).
3097 if ($line =~ /^\+\s*\#\s*if.*\bCONFIG_EXPERIMENTAL\b/) {
3098 WARN("CONFIG_EXPERIMENTAL",
3099 "Use of CONFIG_EXPERIMENTAL is deprecated. For alternatives, see https://lkml.org/lkml/2012/10/23/580\n");
3100 }
3101
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003102# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003103 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003104 WARN("CVS_KEYWORD",
3105 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003106 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003107
Mike Frysinger42e41c52009-09-21 17:04:40 -07003108# Blackfin: don't use __builtin_bfin_[cs]sync
3109 if ($line =~ /__builtin_bfin_csync/) {
3110 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003111 ERROR("CSYNC",
3112 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003113 }
3114 if ($line =~ /__builtin_bfin_ssync/) {
3115 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003116 ERROR("SSYNC",
3117 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003118 }
3119
Joe Perches56e77d72013-02-21 16:44:14 -08003120# check for old HOTPLUG __dev<foo> section markings
3121 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3122 WARN("HOTPLUG_SECTION",
3123 "Using $1 is unnecessary\n" . $herecurr);
3124 }
3125
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003126# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003127 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3128 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003129#print "LINE<$line>\n";
3130 if ($linenr >= $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003131 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003132 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003133 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003134 $stat =~ s/\n./\n /g;
3135 $cond =~ s/\n./\n /g;
3136
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003137#print "linenr<$linenr> <$stat>\n";
3138 # If this statement has no statement boundaries within
3139 # it there is no point in retrying a statement scan
3140 # until we hit end of it.
3141 my $frag = $stat; $frag =~ s/;+\s*$//;
3142 if ($frag !~ /(?:{|;)/) {
3143#print "skip<$line_nr_next>\n";
3144 $suppress_statement = $line_nr_next;
3145 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003146
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003147 # Find the real next line.
3148 $realline_next = $line_nr_next;
3149 if (defined $realline_next &&
3150 (!defined $lines[$realline_next - 1] ||
3151 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3152 $realline_next++;
3153 }
3154
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003155 my $s = $stat;
3156 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003157
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003158 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003159 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003160
3161 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003162 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003163
Andy Whitcroft463f2862009-09-21 17:04:34 -07003164 } elsif ($s =~ /^.\s*else\b/s) {
3165
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003166 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003167 } 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) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003168 my $type = $1;
3169 $type =~ s/\s+/ /g;
3170 possible($type, "A:" . $s);
3171
Andy Whitcroft8905a672007-11-28 16:21:06 -08003172 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003173 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003174 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003175 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003176
3177 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003178 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003179 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003180 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003181
3182 # Check for any sort of function declaration.
3183 # int foo(something bar, other baz);
3184 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003185 if ($prev_values eq 'E' && $s =~ /^(.(?:typedef\s*)?(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*(?:\b$Ident|\(\*\s*$Ident\))\s*)\(/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003186 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003187
Andy Whitcroftcf655042008-03-04 14:28:20 -08003188 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003189 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003190 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003191
Andy Whitcroft8905a672007-11-28 16:21:06 -08003192 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003193 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003194
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003195 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003196 }
3197 }
3198 }
3199
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003200 }
3201
Andy Whitcroft653d4872007-06-23 17:16:34 -07003202#
3203# Checks which may be anchored in the context.
3204#
3205
3206# Check for switch () and associated case and default
3207# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003208 if ($line=~/\bswitch\s*\(.*\)/) {
3209 my $err = '';
3210 my $sep = '';
3211 my @ctx = ctx_block_outer($linenr, $realcnt);
3212 shift(@ctx);
3213 for my $ctx (@ctx) {
3214 my ($clen, $cindent) = line_stats($ctx);
3215 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3216 $indent != $cindent) {
3217 $err .= "$sep$ctx\n";
3218 $sep = '';
3219 } else {
3220 $sep = "[...]\n";
3221 }
3222 }
3223 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003224 ERROR("SWITCH_CASE_INDENT_LEVEL",
3225 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003226 }
3227 }
3228
3229# if/while/etc brace do not go on next line, unless defining a do while loop,
3230# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003231 if ($line =~ /(.*)\b((?:if|while|for|switch|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b|else\b)/ && $line !~ /^.\s*\#/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003232 my $pre_ctx = "$1$2";
3233
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003234 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003235
3236 if ($line =~ /^\+\t{6,}/) {
3237 WARN("DEEP_INDENTATION",
3238 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3239 }
3240
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003241 my $ctx_cnt = $realcnt - $#ctx - 1;
3242 my $ctx = join("\n", @ctx);
3243
Andy Whitcroft548596d2008-07-23 21:29:01 -07003244 my $ctx_ln = $linenr;
3245 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003246
Andy Whitcroft548596d2008-07-23 21:29:01 -07003247 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3248 defined $lines[$ctx_ln - 1] &&
3249 $lines[$ctx_ln - 1] =~ /^-/)) {
3250 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3251 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003252 $ctx_ln++;
3253 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003254
Andy Whitcroft53210162008-07-23 21:29:03 -07003255 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3256 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003257
Joe Perchesd752fcc2014-08-06 16:11:05 -07003258 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003259 ERROR("OPEN_BRACE",
3260 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003261 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003262 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003263 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3264 $ctx =~ /\)\s*\;\s*$/ &&
3265 defined $lines[$ctx_ln - 1])
3266 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003267 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3268 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003269 WARN("TRAILING_SEMICOLON",
3270 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003271 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003272 }
3273 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003274 }
3275
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003276# Check relative indent for conditionals and blocks.
Joe Perches0fe3dc22014-08-06 16:11:16 -07003277 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|do\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003278 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3279 ctx_statement_block($linenr, $realcnt, 0)
3280 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003281 my ($s, $c) = ($stat, $cond);
3282
3283 substr($s, 0, length($c), '');
3284
Joe Perches9f5af482015-09-09 15:37:30 -07003285 # remove inline comments
3286 $s =~ s/$;/ /g;
3287 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003288
3289 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003290 my @newlines = ($c =~ /\n/gs);
3291 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003292
Joe Perches9f5af482015-09-09 15:37:30 -07003293 # Make sure we remove the line prefixes as we have
3294 # none on the first line, and are going to readd them
3295 # where necessary.
3296 $s =~ s/\n./\n/gs;
3297 while ($s =~ /\n\s+\\\n/) {
3298 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3299 }
3300
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003301 # We want to check the first line inside the block
3302 # starting at the end of the conditional, so remove:
3303 # 1) any blank line termination
3304 # 2) any opening brace { on end of the line
3305 # 3) any do (...) {
3306 my $continuation = 0;
3307 my $check = 0;
3308 $s =~ s/^.*\bdo\b//;
3309 $s =~ s/^\s*{//;
3310 if ($s =~ s/^\s*\\//) {
3311 $continuation = 1;
3312 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003313 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003314 $check = 1;
3315 $cond_lines++;
3316 }
3317
3318 # Also ignore a loop construct at the end of a
3319 # preprocessor statement.
3320 if (($prevline =~ /^.\s*#\s*define\s/ ||
3321 $prevline =~ /\\\s*$/) && $continuation == 0) {
3322 $check = 0;
3323 }
3324
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003325 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003326 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003327 while ($cond_ptr != $cond_lines) {
3328 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003329
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003330 # If we see an #else/#elif then the code
3331 # is not linear.
3332 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3333 $check = 0;
3334 }
3335
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003336 # Ignore:
3337 # 1) blank lines, they should be at 0,
3338 # 2) preprocessor lines, and
3339 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003340 if ($continuation ||
3341 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003342 $s =~ /^\s*#\s*?/ ||
3343 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003344 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003345 if ($s =~ s/^.*?\n//) {
3346 $cond_lines++;
3347 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003348 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003349 }
3350
3351 my (undef, $sindent) = line_stats("+" . $s);
3352 my $stat_real = raw_line($linenr, $cond_lines);
3353
3354 # Check if either of these lines are modified, else
3355 # this is not this patch's fault.
3356 if (!defined($stat_real) ||
3357 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3358 $check = 0;
3359 }
3360 if (defined($stat_real) && $cond_lines > 1) {
3361 $stat_real = "[...]\n$stat_real";
3362 }
3363
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003364 #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";
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003365
Joe Perches9f5af482015-09-09 15:37:30 -07003366 if ($check && $s ne '' &&
3367 (($sindent % 8) != 0 ||
3368 ($sindent < $indent) ||
3369 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003370 WARN("SUSPECT_CODE_INDENT",
3371 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003372 }
3373 }
3374
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003375 # Track the 'values' across context and added lines.
3376 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003377 my ($curr_values, $curr_vars) =
3378 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003379 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003380 if ($dbg_values) {
3381 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003382 print "$linenr > .$outline\n";
3383 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003384 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003385 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003386 $prev_values = substr($curr_values, -1);
3387
Andy Whitcroft00df3442007-06-08 13:47:06 -07003388#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003389 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003390
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003391# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003392 while ($line =~ m{\b($Declare)\s*(?!char\b|short\b|int\b|long\b)\s*($Ident)?\s*[=,;\[\)\(]}g) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003393 my $type = $1;
3394 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003395 $var = "" if (!defined $var);
3396 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003397 my $sign = $1;
3398 my $pointer = $2;
3399
3400 $pointer = "" if (!defined $pointer);
3401
3402 if (WARN("UNSPECIFIED_INT",
3403 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3404 $fix) {
3405 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003406 my $comp_pointer = $pointer;
3407 $comp_pointer =~ s/\s//g;
3408 $decl .= $comp_pointer;
3409 $decl = rtrim($decl) if ($var eq "");
3410 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003411 }
3412 }
3413 }
3414
Andy Whitcroft653d4872007-06-23 17:16:34 -07003415# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003416 if ($dbg_type) {
3417 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003418 ERROR("TEST_TYPE",
3419 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003420 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003421 ERROR("TEST_NOT_TYPE",
3422 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003423 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003424 next;
3425 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003426# TEST: allow direct testing of the attribute matcher.
3427 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003428 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003429 ERROR("TEST_ATTR",
3430 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003431 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003432 ERROR("TEST_NOT_ATTR",
3433 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003434 }
3435 next;
3436 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003437
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003438# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003439 if ($line =~ /^.\s*{/ &&
3440 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003441 if (ERROR("OPEN_BRACE",
3442 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003443 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3444 fix_delete_line($fixlinenr - 1, $prevrawline);
3445 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003446 my $fixedline = $prevrawline;
3447 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003448 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003449 $fixedline = $line;
3450 $fixedline =~ s/^(.\s*){\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003451 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003452 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003453 }
3454
Andy Whitcroft653d4872007-06-23 17:16:34 -07003455#
3456# Checks which are anchored on the added line.
3457#
3458
3459# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003460 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003461 my $path = $1;
3462 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003463 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003464 "malformed #include filename\n" . $herecurr);
3465 }
3466 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3467 ERROR("UAPI_INCLUDE",
3468 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003469 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003470 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003471
3472# no C99 // comments
3473 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003474 if (ERROR("C99_COMMENTS",
3475 "do not use C99 // comments\n" . $herecurr) &&
3476 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003477 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003478 if ($line =~ /\/\/(.*)$/) {
3479 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003480 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003481 }
3482 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003483 }
3484 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003485 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003486 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003487
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003488# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3489# the whole statement.
3490#print "APW <$lines[$realline_next - 1]>\n";
3491 if (defined $realline_next &&
3492 exists $lines[$realline_next - 1] &&
3493 !defined $suppress_export{$realline_next} &&
3494 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3495 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003496 # Handle definitions which produce identifiers with
3497 # a prefix:
3498 # XXX(foo);
3499 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003500 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003501 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003502 $name =~ /^${Ident}_$2/) {
3503#print "FOO C name<$name>\n";
3504 $suppress_export{$realline_next} = 1;
3505
3506 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003507 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003508 ^.DEFINE_$Ident\(\Q$name\E\)|
3509 ^.DECLARE_$Ident\(\Q$name\E\)|
3510 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003511 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3512 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003513 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003514#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3515 $suppress_export{$realline_next} = 2;
3516 } else {
3517 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003518 }
3519 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003520 if (!defined $suppress_export{$linenr} &&
3521 $prevline =~ /^.\s*$/ &&
3522 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3523 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3524#print "FOO B <$lines[$linenr - 1]>\n";
3525 $suppress_export{$linenr} = 2;
3526 }
3527 if (defined $suppress_export{$linenr} &&
3528 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003529 WARN("EXPORT_SYMBOL",
3530 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003531 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003532
Joe Eloff5150bda2010-08-09 17:21:00 -07003533# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003534 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003535 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003536 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003537 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003538 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003539 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003540 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003541# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003542 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003543 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003544 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003545 $herecurr) &&
3546 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003547 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003548 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003549 }
3550
Joe Perches18130872014-08-06 16:11:22 -07003551# check for misordered declarations of char/short/int/long with signed/unsigned
3552 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3553 my $tmp = trim($1);
3554 WARN("MISORDERED_TYPE",
3555 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3556 }
3557
Joe Perchescb710ec2010-10-26 14:23:20 -07003558# check for static const char * arrays.
3559 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003560 WARN("STATIC_CONST_CHAR_ARRAY",
3561 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003562 $herecurr);
3563 }
3564
3565# check for static char foo[] = "bar" declarations.
3566 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003567 WARN("STATIC_CONST_CHAR_ARRAY",
3568 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003569 $herecurr);
3570 }
3571
Joe Perchesab7e23f2015-04-16 12:44:22 -07003572# check for const <foo> const where <foo> is not a pointer or array type
3573 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3574 my $found = $1;
3575 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3576 WARN("CONST_CONST",
3577 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3578 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3579 WARN("CONST_CONST",
3580 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3581 }
3582 }
3583
Joe Perches9b0fa602014-04-03 14:49:18 -07003584# check for non-global char *foo[] = {"bar", ...} declarations.
3585 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3586 WARN("STATIC_CONST_CHAR_ARRAY",
3587 "char * array declaration might be better as static const\n" .
3588 $herecurr);
3589 }
3590
Joe Perchesb598b672015-04-16 12:44:36 -07003591# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3592 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3593 my $array = $1;
3594 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3595 my $array_div = $1;
3596 if (WARN("ARRAY_SIZE",
3597 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3598 $fix) {
3599 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3600 }
3601 }
3602 }
3603
Joe Perchesb36190c2014-01-27 17:07:18 -08003604# check for function declarations without arguments like "int foo()"
3605 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3606 if (ERROR("FUNCTION_WITHOUT_ARGS",
3607 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3608 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003609 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003610 }
3611 }
3612
Joe Perches92e112f2013-12-13 11:36:22 -07003613# check for uses of DEFINE_PCI_DEVICE_TABLE
3614 if ($line =~ /\bDEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=/) {
3615 if (WARN("DEFINE_PCI_DEVICE_TABLE",
3616 "Prefer struct pci_device_id over deprecated DEFINE_PCI_DEVICE_TABLE\n" . $herecurr) &&
3617 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003618 $fixed[$fixlinenr] =~ s/\b(?:static\s+|)DEFINE_PCI_DEVICE_TABLE\s*\(\s*(\w+)\s*\)\s*=\s*/static const struct pci_device_id $1\[\] = /;
Joe Perches92e112f2013-12-13 11:36:22 -07003619 }
Joe Perches93ed0e22010-10-26 14:23:21 -07003620 }
3621
Andy Whitcroft653d4872007-06-23 17:16:34 -07003622# check for new typedefs, only function parameters and sparse annotations
3623# make sense.
3624 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003625 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003626 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003627 $line !~ /\b$typeTypedefs\b/ &&
Andy Whitcroft653d4872007-06-23 17:16:34 -07003628 $line !~ /\b__bitwise(?:__|)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003629 WARN("NEW_TYPEDEFS",
3630 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003631 }
3632
3633# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003634 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003635 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3636 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003637 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003638
Andy Whitcroft65863862009-01-06 14:41:21 -08003639 # Should start with a space.
3640 $to =~ s/^(\S)/ $1/;
3641 # Should not end with a space.
3642 $to =~ s/\s+$//;
3643 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003644 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003645 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003646
Joe Perches3705ce52013-07-03 15:05:31 -07003647## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003648 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003649 if (ERROR("POINTER_LOCATION",
3650 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3651 $fix) {
3652 my $sub_from = $ident;
3653 my $sub_to = $ident;
3654 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003655 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003656 s@\Q$sub_from\E@$sub_to@;
3657 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003658 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003659 }
3660 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3661 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003662 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003663
Andy Whitcroft65863862009-01-06 14:41:21 -08003664 # Should start with a space.
3665 $to =~ s/^(\S)/ $1/;
3666 # Should not end with a space.
3667 $to =~ s/\s+$//;
3668 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003669 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003670 }
3671 # Modifiers should have spaces.
3672 $to =~ s/(\b$Modifier$)/$1 /;
3673
Joe Perches3705ce52013-07-03 15:05:31 -07003674## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003675 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003676 if (ERROR("POINTER_LOCATION",
3677 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3678 $fix) {
3679
3680 my $sub_from = $match;
3681 my $sub_to = $match;
3682 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003683 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003684 s@\Q$sub_from\E@$sub_to@;
3685 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003686 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003687 }
3688
Joe Perches9d3e3c72015-09-09 15:37:27 -07003689# avoid BUG() or BUG_ON()
3690 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3691 my $msg_type = \&WARN;
3692 $msg_type = \&CHK if ($file);
3693 &{$msg_type}("AVOID_BUG",
3694 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3695 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003696
Joe Perches9d3e3c72015-09-09 15:37:27 -07003697# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003698 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003699 WARN("LINUX_VERSION_CODE",
3700 "LINUX_VERSION_CODE should be avoided, code should be for the version to which it is merged\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003701 }
3702
Joe Perches17441222011-06-15 15:08:17 -07003703# check for uses of printk_ratelimit
3704 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003705 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003706 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003707 }
3708
Andy Whitcroft00df3442007-06-08 13:47:06 -07003709# printk should use KERN_* levels. Note that follow on printk's on the
3710# same line do not need a level, so we use the current block context
3711# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003712# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003713# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003714 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003715 my $ok = 0;
3716 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3717 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003718 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003719 # with "\n" ignore it, else it is to blame
3720 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3721 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3722 $ok = 1;
3723 }
3724 last;
3725 }
3726 }
3727 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003728 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3729 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003730 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003731 }
3732
Joe Perches243f3802012-05-31 16:26:09 -07003733 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3734 my $orig = $1;
3735 my $level = lc($orig);
3736 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003737 my $level2 = $level;
3738 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003739 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003740 "Prefer [subsystem eg: netdev]_$level2([subsystem]dev, ... then dev_$level2(dev, ... then pr_$level(... to printk(KERN_$orig ...\n" . $herecurr);
Joe Perches243f3802012-05-31 16:26:09 -07003741 }
3742
3743 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003744 if (WARN("PREFER_PR_LEVEL",
3745 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3746 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003747 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003748 s/\bpr_warning\b/pr_warn/;
3749 }
Joe Perches243f3802012-05-31 16:26:09 -07003750 }
3751
Joe Perchesdc139312013-02-21 16:44:13 -08003752 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3753 my $orig = $1;
3754 my $level = lc($orig);
3755 $level = "warn" if ($level eq "warning");
3756 $level = "dbg" if ($level eq "debug");
3757 WARN("PREFER_DEV_LEVEL",
3758 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3759 }
3760
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003761# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3762# number of false positives, but assembly files are not checked, so at
3763# least the arch entry code will not trigger this warning.
3764 if ($line =~ /\bENOSYS\b/) {
3765 WARN("ENOSYS",
3766 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3767 }
3768
Andy Whitcroft653d4872007-06-23 17:16:34 -07003769# function brace can't be on same line, except for #defines of do while,
3770# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003771 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003772 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003773 if (ERROR("OPEN_BRACE",
3774 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3775 $fix) {
3776 fix_delete_line($fixlinenr, $rawline);
3777 my $fixed_line = $rawline;
3778 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3779 my $line1 = $1;
3780 my $line2 = $2;
3781 fix_insert_line($fixlinenr, ltrim($line1));
3782 fix_insert_line($fixlinenr, "\+{");
3783 if ($line2 !~ /^\s*$/) {
3784 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3785 }
3786 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003787 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003788
Andy Whitcroft8905a672007-11-28 16:21:06 -08003789# open braces for enum, union and struct go on the same line.
3790 if ($line =~ /^.\s*{/ &&
3791 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003792 if (ERROR("OPEN_BRACE",
3793 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3794 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3795 fix_delete_line($fixlinenr - 1, $prevrawline);
3796 fix_delete_line($fixlinenr, $rawline);
3797 my $fixedline = rtrim($prevrawline) . " {";
3798 fix_insert_line($fixlinenr, $fixedline);
3799 $fixedline = $rawline;
3800 $fixedline =~ s/^(.\s*){\s*/$1\t/;
3801 if ($fixedline !~ /^\+\s*$/) {
3802 fix_insert_line($fixlinenr, $fixedline);
3803 }
3804 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003805 }
3806
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003807# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003808 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3809 if (WARN("SPACING",
3810 "missing space after $1 definition\n" . $herecurr) &&
3811 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003812 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003813 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3814 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003815 }
3816
Joe Perches31070b52014-01-23 15:54:49 -08003817# Function pointer declarations
3818# check spacing between type, funcptr, and args
3819# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003820 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003821 my $declare = $1;
3822 my $pre_pointer_space = $2;
3823 my $post_pointer_space = $3;
3824 my $funcname = $4;
3825 my $post_funcname_space = $5;
3826 my $pre_args_space = $6;
3827
Joe Perches91f72e92014-04-03 14:49:12 -07003828# the $Declare variable will capture all spaces after the type
3829# so check it for a missing trailing missing space but pointer return types
3830# don't need a space so don't warn for those.
3831 my $post_declare_space = "";
3832 if ($declare =~ /(\s+)$/) {
3833 $post_declare_space = $1;
3834 $declare = rtrim($declare);
3835 }
3836 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003837 WARN("SPACING",
3838 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003839 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003840 }
3841
3842# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003843# This test is not currently implemented because these declarations are
3844# equivalent to
3845# int foo(int bar, ...)
3846# and this is form shouldn't/doesn't generate a checkpatch warning.
3847#
3848# elsif ($declare =~ /\s{2,}$/) {
3849# WARN("SPACING",
3850# "Multiple spaces after return type\n" . $herecurr);
3851# }
Joe Perches31070b52014-01-23 15:54:49 -08003852
3853# unnecessary space "type ( *funcptr)(args...)"
3854 if (defined $pre_pointer_space &&
3855 $pre_pointer_space =~ /^\s/) {
3856 WARN("SPACING",
3857 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3858 }
3859
3860# unnecessary space "type (* funcptr)(args...)"
3861 if (defined $post_pointer_space &&
3862 $post_pointer_space =~ /^\s/) {
3863 WARN("SPACING",
3864 "Unnecessary space before function pointer name\n" . $herecurr);
3865 }
3866
3867# unnecessary space "type (*funcptr )(args...)"
3868 if (defined $post_funcname_space &&
3869 $post_funcname_space =~ /^\s/) {
3870 WARN("SPACING",
3871 "Unnecessary space after function pointer name\n" . $herecurr);
3872 }
3873
3874# unnecessary space "type (*funcptr) (args...)"
3875 if (defined $pre_args_space &&
3876 $pre_args_space =~ /^\s/) {
3877 WARN("SPACING",
3878 "Unnecessary space before function pointer arguments\n" . $herecurr);
3879 }
3880
3881 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003882 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07003883 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08003884 }
3885 }
3886
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003887# check for spacing round square brackets; allowed:
3888# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003889# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
3890# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003891 while ($line =~ /(.*?\s)\[/g) {
3892 my ($where, $prefix) = ($-[1], $1);
3893 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07003894 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07003895 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003896 if (ERROR("BRACKET_SPACE",
3897 "space prohibited before open square bracket '['\n" . $herecurr) &&
3898 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003899 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003900 s/^(\+.*?)\s+\[/$1\[/;
3901 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07003902 }
3903 }
3904
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003905# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003906 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003907 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003908 my $ctx_before = substr($line, 0, $-[1]);
3909 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003910
3911 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003912 if ($name =~ /^(?:
3913 if|for|while|switch|return|case|
3914 volatile|__volatile__|
3915 __attribute__|format|__extension__|
3916 asm|__asm__)$/x)
3917 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003918 # cpp #define statements have non-optional spaces, ie
3919 # if there is a space between the name and the open
3920 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003921 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07003922
3923 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003924 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003925
3926 # If this whole things ends with a type its most
3927 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003928 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003929
3930 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07003931 if (WARN("SPACING",
3932 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
3933 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003934 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003935 s/\b$name\s+\(/$name\(/;
3936 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003937 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003938 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07003939
Andy Whitcroft653d4872007-06-23 17:16:34 -07003940# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003941 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003942 my $fixed_line = "";
3943 my $line_fixed = 0;
3944
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003945 my $ops = qr{
3946 <<=|>>=|<=|>=|==|!=|
3947 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
3948 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003949 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08003950 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003951 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003952 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07003953
3954## print("element count: <" . $#elements . ">\n");
3955## foreach my $el (@elements) {
3956## print("el: <$el>\n");
3957## }
3958
3959 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07003960 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003961
Joe Perches3705ce52013-07-03 15:05:31 -07003962 foreach my $el (@elements) {
3963 push(@fix_elements, substr($rawline, $off, length($el)));
3964 $off += length($el);
3965 }
3966
3967 $off = 0;
3968
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003969 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07003970 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003971
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003972 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07003973
3974 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
3975
3976## print("n: <$n> good: <$good>\n");
3977
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003978 $off += length($elements[$n]);
3979
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003980 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07003981 my $ca = substr($opline, 0, $off);
3982 my $cc = '';
3983 if (length($opline) >= ($off + length($elements[$n + 1]))) {
3984 $cc = substr($opline, $off + length($elements[$n + 1]));
3985 }
3986 my $cb = "$ca$;$cc";
3987
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003988 my $a = '';
3989 $a = 'V' if ($elements[$n] ne '');
3990 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08003991 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003992 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
3993 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07003994 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003995
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003996 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07003997
3998 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003999 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004000 $c = 'V' if ($elements[$n + 2] ne '');
4001 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004002 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004003 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4004 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004005 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004006 } else {
4007 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004008 }
4009
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004010 my $ctx = "${a}x${c}";
4011
4012 my $at = "(ctx:$ctx)";
4013
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004014 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004015 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004016
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004017 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004018 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004019
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004020 # Get the full operator variant.
4021 my $opv = $op . substr($curr_vars, $off, 1);
4022
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004023 # Ignore operators passed as parameters.
4024 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004025 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004026
Andy Whitcroftcf655042008-03-04 14:28:20 -08004027# # Ignore comments
4028# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004029
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004030 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004031 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004032 if ($ctx !~ /.x[WEBC]/ &&
4033 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004034 if (ERROR("SPACING",
4035 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004036 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004037 $line_fixed = 1;
4038 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004039 }
4040
4041 # // is a comment
4042 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004043
Joe Perchesb00e4812014-04-03 14:49:33 -07004044 # : when part of a bitfield
4045 } elsif ($opv eq ':B') {
4046 # skip the bitfield test for now
4047
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004048 # No spaces for:
4049 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004050 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004051 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004052 if (ERROR("SPACING",
4053 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004054 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004055 if (defined $fix_elements[$n + 2]) {
4056 $fix_elements[$n + 2] =~ s/^\s+//;
4057 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004058 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004059 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004060 }
4061
Joe Perches23810972014-12-10 15:51:32 -08004062 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004063 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004064 my $rtrim_before = 0;
4065 my $space_after = 0;
4066 if ($ctx =~ /Wx./) {
4067 if (ERROR("SPACING",
4068 "space prohibited before that '$op' $at\n" . $hereptr)) {
4069 $line_fixed = 1;
4070 $rtrim_before = 1;
4071 }
4072 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004073 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004074 if (ERROR("SPACING",
4075 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004076 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004077 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004078 $space_after = 1;
4079 }
4080 }
4081 if ($rtrim_before || $space_after) {
4082 if ($rtrim_before) {
4083 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4084 } else {
4085 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4086 }
4087 if ($space_after) {
4088 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004089 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004090 }
4091
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004092 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004093 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004094 #warn "'*' is part of type\n";
4095
4096 # unary operators should have a space before and
4097 # none after. May be left adjacent to another
4098 # unary operator, or a cast
4099 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004100 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004101 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004102 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004103 if (ERROR("SPACING",
4104 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004105 if ($n != $last_after + 2) {
4106 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4107 $line_fixed = 1;
4108 }
Joe Perches3705ce52013-07-03 15:05:31 -07004109 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004110 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004111 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004112 # A unary '*' may be const
4113
4114 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004115 if (ERROR("SPACING",
4116 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004117 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004118 if (defined $fix_elements[$n + 2]) {
4119 $fix_elements[$n + 2] =~ s/^\s+//;
4120 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004121 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004122 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004123 }
4124
4125 # unary ++ and unary -- are allowed no space on one side.
4126 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004127 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004128 if (ERROR("SPACING",
4129 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004130 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004131 $line_fixed = 1;
4132 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004133 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004134 if ($ctx =~ /Wx[BE]/ ||
4135 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004136 if (ERROR("SPACING",
4137 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004138 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004139 $line_fixed = 1;
4140 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004141 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004142 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004143 if (ERROR("SPACING",
4144 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004145 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004146 if (defined $fix_elements[$n + 2]) {
4147 $fix_elements[$n + 2] =~ s/^\s+//;
4148 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004149 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004150 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004151 }
4152
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004153 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004154 } elsif ($op eq '<<' or $op eq '>>' or
4155 $op eq '&' or $op eq '^' or $op eq '|' or
4156 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004157 $op eq '*' or $op eq '/' or
4158 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004159 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004160 if ($check) {
4161 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4162 if (CHK("SPACING",
4163 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4164 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4165 $fix_elements[$n + 2] =~ s/^\s+//;
4166 $line_fixed = 1;
4167 }
4168 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4169 if (CHK("SPACING",
4170 "space preferred before that '$op' $at\n" . $hereptr)) {
4171 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4172 $line_fixed = 1;
4173 }
4174 }
4175 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004176 if (ERROR("SPACING",
4177 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004178 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4179 if (defined $fix_elements[$n + 2]) {
4180 $fix_elements[$n + 2] =~ s/^\s+//;
4181 }
Joe Perches3705ce52013-07-03 15:05:31 -07004182 $line_fixed = 1;
4183 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004184 }
4185
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004186 # A colon needs no spaces before when it is
4187 # terminating a case value or a label.
4188 } elsif ($opv eq ':C' || $opv eq ':L') {
4189 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004190 if (ERROR("SPACING",
4191 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004192 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004193 $line_fixed = 1;
4194 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004195 }
4196
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004197 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004198 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004199 my $ok = 0;
4200
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004201 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004202 if (($op eq '<' &&
4203 $cc =~ /^\S+\@\S+>/) ||
4204 ($op eq '>' &&
4205 $ca =~ /<\S+\@\S+$/))
4206 {
4207 $ok = 1;
4208 }
4209
Joe Perchese0df7e12015-04-16 12:44:53 -07004210 # for asm volatile statements
4211 # ignore a colon with another
4212 # colon immediately before or after
4213 if (($op eq ':') &&
4214 ($ca =~ /:$/ || $cc =~ /^:/)) {
4215 $ok = 1;
4216 }
4217
Joe Perches84731622013-11-12 15:10:05 -08004218 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004219 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004220 my $msg_type = \&ERROR;
4221 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4222
4223 if (&{$msg_type}("SPACING",
4224 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004225 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4226 if (defined $fix_elements[$n + 2]) {
4227 $fix_elements[$n + 2] =~ s/^\s+//;
4228 }
Joe Perches3705ce52013-07-03 15:05:31 -07004229 $line_fixed = 1;
4230 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004231 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004232 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004233 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004234
4235## print("n: <$n> GOOD: <$good>\n");
4236
4237 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004238 }
Joe Perches3705ce52013-07-03 15:05:31 -07004239
4240 if (($#elements % 2) == 0) {
4241 $fixed_line = $fixed_line . $fix_elements[$#elements];
4242 }
4243
Joe Perches194f66f2014-08-06 16:11:03 -07004244 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4245 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004246 }
4247
4248
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004249 }
4250
Joe Perches786b6322013-07-03 15:05:32 -07004251# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004252 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004253 if (WARN("SPACING",
4254 "space prohibited before semicolon\n" . $herecurr) &&
4255 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004256 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004257 s/^(\+.*\S)\s+;/$1;/;
4258 }
4259 }
4260
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004261# check for multiple assignments
4262 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004263 CHK("MULTIPLE_ASSIGNMENTS",
4264 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004265 }
4266
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004267## # check for multiple declarations, allowing for a function declaration
4268## # continuation.
4269## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4270## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4271##
4272## # Remove any bracketed sections to ensure we do not
4273## # falsly report the parameters of functions.
4274## my $ln = $line;
4275## while ($ln =~ s/\([^\(\)]*\)//g) {
4276## }
4277## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004278## WARN("MULTIPLE_DECLARATION",
4279## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004280## }
4281## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004282
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004283#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004284 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004285 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004286 if (ERROR("SPACING",
4287 "space required before the open brace '{'\n" . $herecurr) &&
4288 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004289 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\))){/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004290 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004291 }
4292
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004293## # check for blank lines before declarations
4294## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4295## $prevrawline =~ /^.\s*$/) {
4296## WARN("SPACING",
4297## "No blank lines before declarations\n" . $hereprev);
4298## }
4299##
4300
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004301# closing brace should have a space following it when it has anything
4302# on the line
4303 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004304 if (ERROR("SPACING",
4305 "space required after that close brace '}'\n" . $herecurr) &&
4306 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004307 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004308 s/}((?!(?:,|;|\)))\S)/} $1/;
4309 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004310 }
4311
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004312# check spacing on square brackets
4313 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004314 if (ERROR("SPACING",
4315 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4316 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004317 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004318 s/\[\s+/\[/;
4319 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004320 }
4321 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004322 if (ERROR("SPACING",
4323 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4324 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004325 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004326 s/\s+\]/\]/;
4327 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004328 }
4329
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004330# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004331 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
Matt Wagantall54afcc62010-06-03 12:28:18 -07004332 $line !~ /for\s*\(\s+;/ && $line !~ /^\+\s*[A-Z_][A-Z\d_]*\(\s*\d+(\,.*)?\)\,?$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004333 if (ERROR("SPACING",
4334 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4335 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004336 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004337 s/\(\s+/\(/;
4338 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004339 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004340 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004341 $line !~ /for\s*\(.*;\s+\)/ &&
4342 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004343 if (ERROR("SPACING",
4344 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4345 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004346 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004347 s/\s+\)/\)/;
4348 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004349 }
4350
Joe Perchese2826fd2014-08-06 16:10:48 -07004351# check unnecessary parentheses around addressof/dereference single $Lvals
4352# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4353
4354 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004355 my $var = $1;
4356 if (CHK("UNNECESSARY_PARENTHESES",
4357 "Unnecessary parentheses around $var\n" . $herecurr) &&
4358 $fix) {
4359 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4360 }
4361 }
4362
4363# check for unnecessary parentheses around function pointer uses
4364# ie: (foo->bar)(); should be foo->bar();
4365# but not "if (foo->bar) (" to avoid some false positives
4366 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4367 my $var = $2;
4368 if (CHK("UNNECESSARY_PARENTHESES",
4369 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4370 $fix) {
4371 my $var2 = deparenthesize($var);
4372 $var2 =~ s/\s//g;
4373 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4374 }
4375 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004376
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004377#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004378 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004379 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004380 if (WARN("INDENTED_LABEL",
4381 "labels should not be indented\n" . $herecurr) &&
4382 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004383 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004384 s/^(.)\s+/$1/;
4385 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004386 }
4387
Joe Perches5b9553a2014-04-03 14:49:21 -07004388# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004389 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004390 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004391 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004392 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4393 my $value = $1;
4394 $value = deparenthesize($value);
4395 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4396 ERROR("RETURN_PARENTHESES",
4397 "return is not a function, parentheses are not required\n" . $herecurr);
4398 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004399 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004400 ERROR("SPACING",
4401 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004402 }
4403 }
Joe Perches507e5142013-11-12 15:10:13 -08004404
Joe Perchesb43ae212014-06-23 13:22:07 -07004405# unnecessary return in a void function
4406# at end-of-function, with the previous line a single leading tab, then return;
4407# and the line before that not a goto label target like "out:"
4408 if ($sline =~ /^[ \+]}\s*$/ &&
4409 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4410 $linenr >= 3 &&
4411 $lines[$linenr - 3] =~ /^[ +]/ &&
4412 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004413 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004414 "void function return statements are not generally useful\n" . $hereprev);
4415 }
Joe Perches9819cf22014-06-04 16:12:09 -07004416
Joe Perches189248d2014-01-23 15:54:47 -08004417# if statements using unnecessary parentheses - ie: if ((foo == bar))
4418 if ($^V && $^V ge 5.10.0 &&
4419 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4420 my $openparens = $1;
4421 my $count = $openparens =~ tr@\(@\(@;
4422 my $msg = "";
4423 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4424 my $comp = $4; #Not $1 because of $LvalOrFunc
4425 $msg = " - maybe == should be = ?" if ($comp eq "==");
4426 WARN("UNNECESSARY_PARENTHESES",
4427 "Unnecessary parentheses$msg\n" . $herecurr);
4428 }
4429 }
4430
Joe Perchesc5595fa2015-09-09 15:37:58 -07004431# comparisons with a constant or upper case identifier on the left
4432# avoid cases like "foo + BAR < baz"
4433# only fix matches surrounded by parentheses to avoid incorrect
4434# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4435 if ($^V && $^V ge 5.10.0 &&
4436 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4437 my $lead = $1;
4438 my $const = $2;
4439 my $comp = $3;
4440 my $to = $4;
4441 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004442 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004443 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4444 WARN("CONSTANT_COMPARISON",
4445 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4446 $fix) {
4447 if ($comp eq "<") {
4448 $newcomp = ">";
4449 } elsif ($comp eq "<=") {
4450 $newcomp = ">=";
4451 } elsif ($comp eq ">") {
4452 $newcomp = "<";
4453 } elsif ($comp eq ">=") {
4454 $newcomp = "<=";
4455 }
4456 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4457 }
4458 }
4459
Joe Perchesf34e4a42015-04-16 12:44:19 -07004460# Return of what appears to be an errno should normally be negative
4461 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004462 my $name = $1;
4463 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004464 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004465 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004466 }
4467 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004468
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004469# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004470 if ($line =~ /\b(if|while|for|switch)\(/) {
4471 if (ERROR("SPACING",
4472 "space required before the open parenthesis '('\n" . $herecurr) &&
4473 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004474 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004475 s/\b(if|while|for|switch)\(/$1 \(/;
4476 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004477 }
4478
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004479# Check for illegal assignment in if conditional -- and check for trailing
4480# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004481 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004482 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4483 ctx_statement_block($linenr, $realcnt, 0)
4484 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004485 my ($stat_next) = ctx_statement_block($line_nr_next,
4486 $remain_next, $off_next);
4487 $stat_next =~ s/\n./\n /g;
4488 ##print "stat<$stat> stat_next<$stat_next>\n";
4489
4490 if ($stat_next =~ /^\s*while\b/) {
4491 # If the statement carries leading newlines,
4492 # then count those as offsets.
4493 my ($whitespace) =
4494 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4495 my $offset =
4496 statement_rawlines($whitespace) - 1;
4497
4498 $suppress_whiletrailers{$line_nr_next +
4499 $offset} = 1;
4500 }
4501 }
4502 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004503 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004504 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004505 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004506
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004507 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004508 ERROR("ASSIGN_IN_IF",
4509 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004510 }
4511
4512 # Find out what is on the end of the line after the
4513 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004514 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004515 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004516 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004517 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4518 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004519 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004520 # Find out how long the conditional actually is.
4521 my @newlines = ($c =~ /\n/gs);
4522 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004523 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004524
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004525 $stat_real = raw_line($linenr, $cond_lines)
4526 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004527 if (defined($stat_real) && $cond_lines > 1) {
4528 $stat_real = "[...]\n$stat_real";
4529 }
4530
Joe Perches000d1cc12011-07-25 17:13:25 -07004531 ERROR("TRAILING_STATEMENTS",
4532 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004533 }
4534 }
4535
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004536# Check for bitwise tests written as boolean
4537 if ($line =~ /
4538 (?:
4539 (?:\[|\(|\&\&|\|\|)
4540 \s*0[xX][0-9]+\s*
4541 (?:\&\&|\|\|)
4542 |
4543 (?:\&\&|\|\|)
4544 \s*0[xX][0-9]+\s*
4545 (?:\&\&|\|\||\)|\])
4546 )/x)
4547 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004548 WARN("HEXADECIMAL_BOOLEAN_TEST",
4549 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004550 }
4551
Andy Whitcroft8905a672007-11-28 16:21:06 -08004552# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004553 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4554 my $s = $1;
4555 $s =~ s/$;//g; # Remove any comments
4556 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004557 ERROR("TRAILING_STATEMENTS",
4558 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004559 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004560 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004561# if should not continue a brace
4562 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004563 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004564 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004565 $herecurr);
4566 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004567# case and default should not have general statements after them
4568 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4569 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004570 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004571 \s*return\s+
4572 )/xg)
4573 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004574 ERROR("TRAILING_STATEMENTS",
4575 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004576 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004577
4578 # Check for }<nl>else {, these must be at the same
4579 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004580 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4581 $previndent == $indent) {
4582 if (ERROR("ELSE_AFTER_BRACE",
4583 "else should follow close brace '}'\n" . $hereprev) &&
4584 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4585 fix_delete_line($fixlinenr - 1, $prevrawline);
4586 fix_delete_line($fixlinenr, $rawline);
4587 my $fixedline = $prevrawline;
4588 $fixedline =~ s/}\s*$//;
4589 if ($fixedline !~ /^\+\s*$/) {
4590 fix_insert_line($fixlinenr, $fixedline);
4591 }
4592 $fixedline = $rawline;
4593 $fixedline =~ s/^(.\s*)else/$1} else/;
4594 fix_insert_line($fixlinenr, $fixedline);
4595 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004596 }
4597
Joe Perches8b8856f2014-08-06 16:11:14 -07004598 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4599 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004600 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4601
4602 # Find out what is on the end of the line after the
4603 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004604 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004605 $s =~ s/\n.*//g;
4606
4607 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004608 if (ERROR("WHILE_AFTER_BRACE",
4609 "while should follow close brace '}'\n" . $hereprev) &&
4610 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4611 fix_delete_line($fixlinenr - 1, $prevrawline);
4612 fix_delete_line($fixlinenr, $rawline);
4613 my $fixedline = $prevrawline;
4614 my $trailing = $rawline;
4615 $trailing =~ s/^\+//;
4616 $trailing = trim($trailing);
4617 $fixedline =~ s/}\s*$/} $trailing/;
4618 fix_insert_line($fixlinenr, $fixedline);
4619 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004620 }
4621 }
4622
Joe Perches95e2c602013-07-03 15:05:20 -07004623#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004624 while ($line =~ m{($Constant|$Lval)}g) {
4625 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004626
4627#gcc binary extension
4628 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004629 if (WARN("GCC_BINARY_CONSTANT",
4630 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4631 $fix) {
4632 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004633 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004634 s/\b$var\b/$hexval/;
4635 }
Joe Perches95e2c602013-07-03 15:05:20 -07004636 }
4637
4638#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004639 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004640 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004641#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004642 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004643#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004644 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4645#Ignore some three character SI units explicitly, like MiB and KHz
4646 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004647 while ($var =~ m{($Ident)}g) {
4648 my $word = $1;
4649 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004650 if ($check) {
4651 seed_camelcase_includes();
4652 if (!$file && !$camelcase_file_seeded) {
4653 seed_camelcase_file($realfile);
4654 $camelcase_file_seeded = 1;
4655 }
4656 }
Joe Perches7e781f62013-09-11 14:23:55 -07004657 if (!defined $camelcase{$word}) {
4658 $camelcase{$word} = 1;
4659 CHK("CAMELCASE",
4660 "Avoid CamelCase: <$word>\n" . $herecurr);
4661 }
Joe Perches34456862013-07-03 15:05:34 -07004662 }
Joe Perches323c1262012-12-17 16:02:07 -08004663 }
4664 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004665
4666#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004667 if ($line =~ /\#\s*define.*\\\s+$/) {
4668 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4669 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4670 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004671 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004672 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004673 }
4674
Fabian Frederick0e212e02015-04-16 12:44:25 -07004675# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4676# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004677 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004678 my $file = "$1.h";
4679 my $checkfile = "include/linux/$file";
4680 if (-f "$root/$checkfile" &&
4681 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004682 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004683 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004684 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4685 if ($asminclude > 0) {
4686 if ($realfile =~ m{^arch/}) {
4687 CHK("ARCH_INCLUDE_LINUX",
4688 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4689 } else {
4690 WARN("INCLUDE_LINUX",
4691 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4692 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004693 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004694 }
4695 }
4696
Andy Whitcroft653d4872007-06-23 17:16:34 -07004697# multi-statement macros should be enclosed in a do while loop, grab the
4698# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004699# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004700 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4701 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004702 my $ln = $linenr;
Gregory Bean02870be2011-05-11 09:11:12 -07004703 my $cnt = $realcnt - 1;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004704 my ($off, $dstat, $dcond, $rest);
4705 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004706 my $has_flow_statement = 0;
4707 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004708 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004709 ctx_statement_block($linenr, $realcnt, 0);
4710 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004711 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004712 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004713
Joe Perches08a28432014-10-13 15:51:55 -07004714 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004715 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004716
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004717 $dstat =~ s/^.\s*\#\s*define\s+$Ident(?:\([^\)]*\))?\s*//;
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004718 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004719 $dstat =~ s/\\\n.//g;
4720 $dstat =~ s/^\s*//s;
4721 $dstat =~ s/\s*$//s;
4722
4723 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004724 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4725 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004726 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004727 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004728 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004729
Gregory Bean02870be2011-05-11 09:11:12 -07004730 # Extremely long macros may fall off the end of the
4731 # available context without closing. Give a dangling
4732 # backslash the benefit of the doubt and allow it
4733 # to gobble any hanging open-parens.
4734 $dstat =~ s/\(.+\\$/1/;
4735
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004736 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004737 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4738 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004739 {
4740 }
4741
Joe Perches42e15292016-03-15 14:58:01 -07004742 # Make asm volatile uses seem like a generic function
4743 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4744
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004745 my $exceptions = qr{
4746 $Declare|
4747 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004748 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004749 DECLARE_PER_CPU|
4750 DEFINE_PER_CPU|
Matt Wagantall54afcc62010-06-03 12:28:18 -07004751 CLK_[A-Z\d_]+|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004752 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004753 union|
4754 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004755 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004756 ^\"|\"$|
4757 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004758 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004759 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004760 if ($dstat ne '' &&
4761 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4762 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004763 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004764 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004765 $dstat !~ /$exceptions/ &&
4766 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004767 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004768 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004769 $dstat !~ /^for\s*$Constant$/ && # for (...)
4770 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4771 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004772 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004773 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004774 {
4775 $ctx =~ s/\n*$//;
4776 my $herectx = $here . "\n";
4777 my $cnt = statement_rawlines($ctx);
4778
4779 for (my $n = 0; $n < $cnt; $n++) {
4780 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004781 }
4782
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004783 if ($dstat =~ /;/) {
4784 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4785 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4786 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004787 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004788 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004789 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004790 }
Joe Perches5023d342012-12-17 16:01:47 -08004791
Joe Perches08a28432014-10-13 15:51:55 -07004792# check for macros with flow control, but without ## concatenation
4793# ## concatenation is commonly a macro that defines a function so ignore those
4794 if ($has_flow_statement && !$has_arg_concat) {
4795 my $herectx = $here . "\n";
4796 my $cnt = statement_rawlines($ctx);
4797
4798 for (my $n = 0; $n < $cnt; $n++) {
4799 $herectx .= raw_line($linenr, $n) . "\n";
4800 }
4801 WARN("MACRO_WITH_FLOW_CONTROL",
4802 "Macros with flow control statements should be avoided\n" . "$herectx");
4803 }
4804
Joe Perches481eb482012-12-17 16:01:56 -08004805# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004806
4807 } else {
4808 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004809 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4810 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004811 $line =~ /^\+.*\\$/) {
4812 WARN("LINE_CONTINUATIONS",
4813 "Avoid unnecessary line continuations\n" . $herecurr);
4814 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004815 }
4816
Joe Perchesb13edf72012-07-30 14:41:24 -07004817# do {} while (0) macro tests:
4818# single-statement macros do not need to be enclosed in do while (0) loop,
4819# macro should not end with a semicolon
4820 if ($^V && $^V ge 5.10.0 &&
4821 $realfile !~ m@/vmlinux.lds.h$@ &&
4822 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4823 my $ln = $linenr;
4824 my $cnt = $realcnt;
4825 my ($off, $dstat, $dcond, $rest);
4826 my $ctx = '';
4827 ($dstat, $dcond, $ln, $cnt, $off) =
4828 ctx_statement_block($linenr, $realcnt, 0);
4829 $ctx = $dstat;
4830
4831 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08004832 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07004833
4834 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
4835 my $stmts = $2;
4836 my $semis = $3;
4837
4838 $ctx =~ s/\n*$//;
4839 my $cnt = statement_rawlines($ctx);
4840 my $herectx = $here . "\n";
4841
4842 for (my $n = 0; $n < $cnt; $n++) {
4843 $herectx .= raw_line($linenr, $n) . "\n";
4844 }
4845
Joe Perchesac8e97f2012-08-21 16:15:53 -07004846 if (($stmts =~ tr/;/;/) == 1 &&
4847 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07004848 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
4849 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
4850 }
4851 if (defined $semis && $semis ne "") {
4852 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
4853 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
4854 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07004855 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
4856 $ctx =~ s/\n*$//;
4857 my $cnt = statement_rawlines($ctx);
4858 my $herectx = $here . "\n";
4859
4860 for (my $n = 0; $n < $cnt; $n++) {
4861 $herectx .= raw_line($linenr, $n) . "\n";
4862 }
4863
4864 WARN("TRAILING_SEMICOLON",
4865 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07004866 }
4867 }
4868
Mike Frysinger080ba922009-01-06 14:41:25 -08004869# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
4870# all assignments may have only one of the following with an assignment:
4871# .
4872# ALIGN(...)
4873# VMLINUX_SYMBOL(...)
4874 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004875 WARN("MISSING_VMLINUX_SYMBOL",
4876 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08004877 }
4878
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004879# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004880 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
4881 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08004882 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004883 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004884 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
4885 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07004886 my @allowed = ();
4887 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004888 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004889 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004890 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004891 for my $chunk (@chunks) {
4892 my ($cond, $block) = @{$chunk};
4893
Andy Whitcroft773647a2008-03-28 14:15:58 -07004894 # If the condition carries leading newlines, then count those as offsets.
4895 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
4896 my $offset = statement_rawlines($whitespace) - 1;
4897
Joe Perchesaad4f612012-03-23 15:02:19 -07004898 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004899 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
4900
4901 # We have looked at and allowed this specific line.
4902 $suppress_ifbraces{$ln + $offset} = 1;
4903
4904 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004905 $ln += statement_rawlines($block) - 1;
4906
Andy Whitcroft773647a2008-03-28 14:15:58 -07004907 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004908
4909 $seen++ if ($block =~ /^\s*{/);
4910
Joe Perchesaad4f612012-03-23 15:02:19 -07004911 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004912 if (statement_lines($cond) > 1) {
4913 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004914 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004915 }
4916 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004917 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004918 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004919 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004920 if (statement_block_size($block) > 1) {
4921 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07004922 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004923 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004924 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004925 }
Joe Perchesaad4f612012-03-23 15:02:19 -07004926 if ($seen) {
4927 my $sum_allowed = 0;
4928 foreach (@allowed) {
4929 $sum_allowed += $_;
4930 }
4931 if ($sum_allowed == 0) {
4932 WARN("BRACES",
4933 "braces {} are not necessary for any arm of this statement\n" . $herectx);
4934 } elsif ($sum_allowed != $allow &&
4935 $seen != $allow) {
4936 CHK("BRACES",
4937 "braces {} should be used on all arms of this statement\n" . $herectx);
4938 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004939 }
4940 }
4941 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004942 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004943 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004944 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004945
Andy Whitcroftcf655042008-03-04 14:28:20 -08004946 # Check the pre-context.
4947 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
4948 #print "APW: ALLOWED: pre<$1>\n";
4949 $allowed = 1;
4950 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004951
4952 my ($level, $endln, @chunks) =
4953 ctx_statement_full($linenr, $realcnt, $-[0]);
4954
Andy Whitcroftcf655042008-03-04 14:28:20 -08004955 # Check the condition.
4956 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07004957 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004958 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004959 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08004960 }
4961 if (statement_lines($cond) > 1) {
4962 #print "APW: ALLOWED: cond<$cond>\n";
4963 $allowed = 1;
4964 }
4965 if ($block =~/\b(?:if|for|while)\b/) {
4966 #print "APW: ALLOWED: block<$block>\n";
4967 $allowed = 1;
4968 }
4969 if (statement_block_size($block) > 1) {
4970 #print "APW: ALLOWED: lines block<$block>\n";
4971 $allowed = 1;
4972 }
4973 # Check the post-context.
4974 if (defined $chunks[1]) {
4975 my ($cond, $block) = @{$chunks[1]};
4976 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004977 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004978 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004979 if ($block =~ /^\s*\{/) {
4980 #print "APW: ALLOWED: chunk-1 block<$block>\n";
4981 $allowed = 1;
4982 }
4983 }
4984 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004985 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07004986 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004987
Andy Whitcroftf0556632008-10-15 22:02:23 -07004988 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07004989 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08004990 }
4991
Joe Perches000d1cc12011-07-25 17:13:25 -07004992 WARN("BRACES",
4993 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004994 }
4995 }
4996
Joe Perches0979ae62012-12-17 16:01:59 -08004997# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07004998 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08004999 if (CHK("BRACES",
5000 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5001 $fix && $prevrawline =~ /^\+/) {
5002 fix_delete_line($fixlinenr - 1, $prevrawline);
5003 }
Joe Perches0979ae62012-12-17 16:01:59 -08005004 }
Joe Perches77b9a532013-07-03 15:05:29 -07005005 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005006 if (CHK("BRACES",
5007 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5008 $fix) {
5009 fix_delete_line($fixlinenr, $rawline);
5010 }
Joe Perches0979ae62012-12-17 16:01:59 -08005011 }
5012
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005013# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005014 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5015 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005016 WARN("VOLATILE",
5017 "Use of volatile is usually wrong: see Documentation/volatile-considered-harmful.txt\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005018 }
5019
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005020# Check for user-visible strings broken across lines, which breaks the ability
5021# to grep for the string. Make exceptions when the previous string ends in a
5022# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5023# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005024 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005025 $prevline =~ /"\s*$/ &&
5026 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5027 if (WARN("SPLIT_STRING",
5028 "quoted string split across lines\n" . $hereprev) &&
5029 $fix &&
5030 $prevrawline =~ /^\+.*"\s*$/ &&
5031 $last_coalesced_string_linenr != $linenr - 1) {
5032 my $extracted_string = get_quoted_string($line, $rawline);
5033 my $comma_close = "";
5034 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5035 $comma_close = $1;
5036 }
5037
5038 fix_delete_line($fixlinenr - 1, $prevrawline);
5039 fix_delete_line($fixlinenr, $rawline);
5040 my $fixedline = $prevrawline;
5041 $fixedline =~ s/"\s*$//;
5042 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5043 fix_insert_line($fixlinenr - 1, $fixedline);
5044 $fixedline = $rawline;
5045 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5046 if ($fixedline !~ /\+\s*$/) {
5047 fix_insert_line($fixlinenr, $fixedline);
5048 }
5049 $last_coalesced_string_linenr = $linenr;
5050 }
5051 }
5052
5053# check for missing a space in a string concatenation
5054 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5055 WARN('MISSING_SPACE',
5056 "break quoted strings at a space character\n" . $hereprev);
5057 }
5058
5059# check for spaces before a quoted newline
5060 if ($rawline =~ /^.*\".*\s\\n/) {
5061 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5062 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5063 $fix) {
5064 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5065 }
5066
5067 }
5068
Joe Perchesf17dba42014-10-13 15:51:51 -07005069# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005070 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005071 CHK("CONCATENATED_STRING",
5072 "Concatenated strings should use spaces between elements\n" . $herecurr);
5073 }
5074
Joe Perches90ad30e2014-12-10 15:51:59 -08005075# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005076 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005077 WARN("STRING_FRAGMENTS",
5078 "Consecutive strings are generally better as a single string\n" . $herecurr);
5079 }
5080
Joe Perches6e300752015-09-09 15:37:47 -07005081# check for %L{u,d,i} and 0x%[udi] in strings
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005082 my $string;
5083 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
5084 $string = substr($rawline, $-[1], $+[1] - $-[1]);
5085 $string =~ s/%%/__/g;
Joe Perches6e300752015-09-09 15:37:47 -07005086 if ($string =~ /(?<!%)%[\*\d\.\$]*L[udi]/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005087 WARN("PRINTF_L",
5088 "\%Ld/%Lu are not-standard C, use %lld/%llu\n" . $herecurr);
5089 last;
5090 }
Joe Perches6e300752015-09-09 15:37:47 -07005091 if ($string =~ /0x%[\*\d\.\$\Llzth]*[udi]/) {
5092 ERROR("PRINTF_0xDECIMAL",
5093 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5094 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005095 }
5096
5097# check for line continuations in quoted strings with odd counts of "
5098 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5099 WARN("LINE_CONTINUATIONS",
5100 "Avoid line continuations in quoted strings\n" . $herecurr);
5101 }
5102
Gregory Bean6c9c6842011-03-17 14:18:20 -07005103# sys_open/read/write/close are not allowed in the kernel
5104 if ($line =~ /\b(sys_(?:open|read|write|close))\b/) {
5105 ERROR("FILE_OPS",
5106 "$1 is inappropriate in kernel code.\n" .
5107 $herecurr);
5108 }
5109
Gregory Beana7cce0a2011-06-07 08:06:45 -07005110# filp_open is a backdoor for sys_open
5111 if ($line =~ /\b(filp_open)\b/) {
5112 ERROR("FILE_OPS",
5113 "$1 is inappropriate in kernel code.\n" .
5114 $herecurr);
5115 }
5116
Gregory Bean3f6f2dd2011-05-02 13:50:35 -07005117# read[bwl] & write[bwl] use too many barriers, use the _relaxed variants
5118 if ($line =~ /\b((?:read|write)[bwl])\b/) {
5119 ERROR("NON_RELAXED_IO",
5120 "Use of $1 is deprecated: use $1_relaxed\n\t" .
5121 "with appropriate memory barriers instead.\n" .
5122 $herecurr);
5123 }
5124
5125# likewise, in/out[bwl] should be __raw_read/write[bwl]...
5126 if ($line =~ /\b((in|out)([bwl]))\b/) {
5127 my ($all, $pref, $suf) = ($1, $2, $3);
5128 $pref =~ s/in/read/;
5129 $pref =~ s/out/write/;
5130 ERROR("NON_RELAXED_IO",
5131 "Use of $all is deprecated: use " .
5132 "__raw_$pref$suf\n\t" .
5133 "with appropriate memory barriers instead.\n" .
5134 $herecurr);
5135 }
5136
Gregory Bean438bf5f2011-06-15 09:32:38 -07005137# dsb is too ARMish, and should usually be mb.
Sarangdhar Joshibbeebd52015-10-15 14:21:47 -07005138 if ($line =~ /[^-_>*\.]\bdsb\b[^-_\.;]/) {
Gregory Bean438bf5f2011-06-15 09:32:38 -07005139 WARN("ARM_BARRIER",
5140 "Use of dsb is discouranged: prefer mb.\n" .
5141 $herecurr);
5142 }
5143
Gregory Beandf3a3ee2011-05-10 12:34:09 -07005144# unbounded string functions are overflow risks
5145 my %str_fns = (
5146 "sprintf" => "snprintf",
5147 "strcpy" => "strlcpy",
5148 "strncpy" => "strlcpy",
5149 "strcat" => "strlcat",
5150 "strncat" => "strlcat",
5151 "vsprintf" => "vsnprintf",
5152 "strchr" => "strnchr",
5153 "strstr" => "strnstr",
5154 );
5155 foreach my $k (keys %str_fns) {
5156 if ($line =~ /\b$k\b/) {
5157 ERROR("UNBOUNDED_STRING_FNS",
5158 "Use of $k is deprecated: " .
5159 "use $str_fns{$k} instead.\n" .
5160 $herecurr);
5161 }
5162 }
5163
Andy Whitcroft00df3442007-06-08 13:47:06 -07005164# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005165 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Abhijeet Dharmapurikar800a3132009-10-01 12:01:44 -07005166 WARN("IF_0",
5167 "if this code is redundant consider removing it\n"
5168 . $herecurr);
5169 }
5170
5171# warn about #if 1
5172 if ($line =~ /^.\s*\#\s*if\s+1\b/) {
5173 WARN("IF_1",
5174 "if this code is required consider removing"
5175 . " #if 1\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005176 }
5177
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005178# check for needless "if (<foo>) fn(<foo>)" uses
5179 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005180 my $tested = quotemeta($1);
5181 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5182 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5183 my $func = $1;
5184 if (WARN('NEEDLESS_IF',
5185 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5186 $fix) {
5187 my $do_fix = 1;
5188 my $leading_tabs = "";
5189 my $new_leading_tabs = "";
5190 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5191 $leading_tabs = $1;
5192 } else {
5193 $do_fix = 0;
5194 }
5195 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5196 $new_leading_tabs = $1;
5197 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5198 $do_fix = 0;
5199 }
5200 } else {
5201 $do_fix = 0;
5202 }
5203 if ($do_fix) {
5204 fix_delete_line($fixlinenr - 1, $prevrawline);
5205 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5206 }
5207 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005208 }
5209 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005210
Joe Perchesebfdc402014-08-06 16:10:27 -07005211# check for unnecessary "Out of Memory" messages
5212 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5213 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5214 (defined $1 || defined $3) &&
5215 $linenr > 3) {
5216 my $testval = $2;
5217 my $testline = $lines[$linenr - 3];
5218
5219 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5220# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5221
5222 if ($c =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|(?:dev_)?alloc_skb)/) {
5223 WARN("OOM_MESSAGE",
5224 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5225 }
5226 }
5227
Joe Perchesf78d98f2014-10-13 15:52:01 -07005228# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005229 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005230 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5231 my $level = $1;
5232 if (WARN("UNNECESSARY_KERN_LEVEL",
5233 "Possible unnecessary $level\n" . $herecurr) &&
5234 $fix) {
5235 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5236 }
5237 }
5238
Joe Perchesabb08a52014-12-10 15:51:46 -08005239# check for mask then right shift without a parentheses
5240 if ($^V && $^V ge 5.10.0 &&
5241 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5242 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5243 WARN("MASK_THEN_SHIFT",
5244 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5245 }
5246
Joe Perchesb75ac612014-12-10 15:52:02 -08005247# check for pointer comparisons to NULL
5248 if ($^V && $^V ge 5.10.0) {
5249 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5250 my $val = $1;
5251 my $equal = "!";
5252 $equal = "" if ($4 eq "!=");
5253 if (CHK("COMPARISON_TO_NULL",
5254 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5255 $fix) {
5256 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5257 }
5258 }
5259 }
5260
Joe Perches8716de32013-09-11 14:24:05 -07005261# check for bad placement of section $InitAttribute (e.g.: __initdata)
5262 if ($line =~ /(\b$InitAttribute\b)/) {
5263 my $attr = $1;
5264 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5265 my $ptr = $1;
5266 my $var = $2;
5267 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5268 ERROR("MISPLACED_INIT",
5269 "$attr should be placed after $var\n" . $herecurr)) ||
5270 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5271 WARN("MISPLACED_INIT",
5272 "$attr should be placed after $var\n" . $herecurr))) &&
5273 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005274 $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;
Joe Perches8716de32013-09-11 14:24:05 -07005275 }
5276 }
5277 }
5278
Joe Perchese970b882013-11-12 15:10:10 -08005279# check for $InitAttributeData (ie: __initdata) with const
5280 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5281 my $attr = $1;
5282 $attr =~ /($InitAttributePrefix)(.*)/;
5283 my $attr_prefix = $1;
5284 my $attr_type = $2;
5285 if (ERROR("INIT_ATTRIBUTE",
5286 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5287 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005288 $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005289 s/$InitAttributeData/${attr_prefix}initconst/;
5290 }
5291 }
5292
5293# check for $InitAttributeConst (ie: __initconst) without const
5294 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5295 my $attr = $1;
5296 if (ERROR("INIT_ATTRIBUTE",
5297 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5298 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005299 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b882013-11-12 15:10:10 -08005300 /(^\+\s*(?:static\s+))/;
5301 $lead = rtrim($1);
5302 $lead = "$lead " if ($lead !~ /^\+$/);
5303 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005304 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b882013-11-12 15:10:10 -08005305 }
5306 }
5307
Joe Perchesc17893c2015-04-16 12:44:42 -07005308# check for __read_mostly with const non-pointer (should just be const)
5309 if ($line =~ /\b__read_mostly\b/ &&
5310 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5311 if (ERROR("CONST_READ_MOSTLY",
5312 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5313 $fix) {
5314 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5315 }
5316 }
5317
Joe Perchesfbdb8132014-04-03 14:49:14 -07005318# don't use __constant_<foo> functions outside of include/uapi/
5319 if ($realfile !~ m@^include/uapi/@ &&
5320 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5321 my $constant_func = $1;
5322 my $func = $constant_func;
5323 $func =~ s/^__constant_//;
5324 if (WARN("CONSTANT_CONVERSION",
5325 "$constant_func should be $func\n" . $herecurr) &&
5326 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005327 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005328 }
5329 }
5330
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005331# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005332 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005333 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005334 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005335 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005336 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005337 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5338 }
5339 if ($delay > 2000) {
5340 WARN("LONG_UDELAY",
5341 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005342 }
5343 }
5344
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005345# warn about unexpectedly long msleep's
5346 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5347 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005348 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005349 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005350 }
5351 }
5352
Joe Perches36ec1932013-07-03 15:05:25 -07005353# check for comparisons of jiffies
5354 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5355 WARN("JIFFIES_COMPARISON",
5356 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5357 }
5358
Joe Perches9d7a34a2013-07-03 15:05:26 -07005359# check for comparisons of get_jiffies_64()
5360 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5361 WARN("JIFFIES_COMPARISON",
5362 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5363 }
5364
Israel Schlesinger5f8c9a22010-07-21 13:34:18 -07005365# check the patch for use of mdelay
5366 if ($line =~ /\bmdelay\s*\(/) {
5367 WARN("MDELAY",
5368 "use of mdelay() found: msleep() is the preferred API.\n" . $herecurr );
5369 }
5370
Andy Whitcroft00df3442007-06-08 13:47:06 -07005371# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005372# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005373# print "#ifdef in C files should be avoided\n";
5374# print "$herecurr";
5375# $clean = 0;
5376# }
5377
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005378# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005379 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005380 if (ERROR("SPACING",
5381 "exactly one space required after that #$1\n" . $herecurr) &&
5382 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005383 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005384 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5385 }
5386
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005387 }
5388
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005389# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005390 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5391 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005392 my $which = $1;
5393 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005394 CHK("UNCOMMENTED_DEFINITION",
5395 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005396 }
5397 }
5398# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005399
5400 my $barriers = qr{
5401 mb|
5402 rmb|
5403 wmb|
5404 read_barrier_depends
5405 }x;
5406 my $barrier_stems = qr{
5407 mb__before_atomic|
5408 mb__after_atomic|
5409 store_release|
5410 load_acquire|
5411 store_mb|
5412 (?:$barriers)
5413 }x;
5414 my $all_barriers = qr{
5415 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005416 smp_(?:$barrier_stems)|
5417 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005418 }x;
5419
5420 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005421 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005422 WARN("MEMORY_BARRIER",
5423 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005424 }
5425 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005426
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005427 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5428
5429 if ($realfile !~ m@^include/asm-generic/@ &&
5430 $realfile !~ m@/barrier\.h$@ &&
5431 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5432 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5433 WARN("MEMORY_BARRIER",
5434 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5435 }
5436
Joe Perchescb426e92015-06-25 15:02:46 -07005437# check for waitqueue_active without a comment.
5438 if ($line =~ /\bwaitqueue_active\s*\(/) {
5439 if (!ctx_has_comment($first_line, $linenr)) {
5440 WARN("WAITQUEUE_ACTIVE",
5441 "waitqueue_active without comment\n" . $herecurr);
5442 }
5443 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005444
5445# Check for expedited grace periods that interrupt non-idle non-nohz
5446# online CPUs. These expedited can therefore degrade real-time response
5447# if used carelessly, and should be avoided where not absolutely
5448# needed. It is always OK to use synchronize_rcu_expedited() and
5449# synchronize_sched_expedited() at boot time (before real-time applications
5450# start) and in error situations where real-time response is compromised in
5451# any case. Note that synchronize_srcu_expedited() does -not- interrupt
5452# other CPUs, so don't warn on uses of synchronize_srcu_expedited().
5453# Of course, nothing comes for free, and srcu_read_lock() and
5454# srcu_read_unlock() do contain full memory barriers in payment for
5455# synchronize_srcu_expedited() non-interruption properties.
5456 if ($line =~ /\b(synchronize_rcu_expedited|synchronize_sched_expedited)\(/) {
5457 WARN("EXPEDITED_RCU_GRACE_PERIOD",
5458 "expedited RCU grace periods should be avoided where they can degrade real-time response\n" . $herecurr);
5459
5460 }
5461
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005462# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005463 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005464 CHK("ARCH_DEFINES",
5465 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005466 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005467
Tobias Klauserd4977c72010-05-24 14:33:30 -07005468# Check that the storage class is at the beginning of a declaration
5469 if ($line =~ /\b$Storage\b/ && $line !~ /^.\s*$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005470 WARN("STORAGE_CLASS",
5471 "storage class should be at the beginning of the declaration\n" . $herecurr)
Tobias Klauserd4977c72010-05-24 14:33:30 -07005472 }
5473
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005474# check the location of the inline attribute, that it is between
5475# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005476 if ($line =~ /\b$Type\s+$Inline\b/ ||
5477 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005478 ERROR("INLINE_LOCATION",
5479 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005480 }
5481
Andy Whitcroft8905a672007-11-28 16:21:06 -08005482# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005483 if ($realfile !~ m@\binclude/uapi/@ &&
5484 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005485 if (WARN("INLINE",
5486 "plain inline is preferred over $1\n" . $herecurr) &&
5487 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005488 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005489
5490 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005491 }
5492
Joe Perches3d130fd2011-01-12 17:00:00 -08005493# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005494 if ($realfile !~ m@\binclude/uapi/@ &&
5495 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005496 WARN("PREFER_PACKED",
5497 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005498 }
5499
Joe Perches39b7e282011-07-25 17:13:24 -07005500# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005501 if ($realfile !~ m@\binclude/uapi/@ &&
5502 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005503 WARN("PREFER_ALIGNED",
5504 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005505 }
5506
Joe Perches5f14d3b2012-01-10 15:09:52 -08005507# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005508 if ($realfile !~ m@\binclude/uapi/@ &&
5509 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005510 if (WARN("PREFER_PRINTF",
5511 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5512 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005513 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf\s*,\s*(.*)\)\s*\)\s*\)/"__printf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005514
5515 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005516 }
5517
Joe Perches6061d942012-03-23 15:02:16 -07005518# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005519 if ($realfile !~ m@\binclude/uapi/@ &&
5520 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005521 if (WARN("PREFER_SCANF",
5522 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5523 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005524 $fixed[$fixlinenr] =~ s/\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\s*,\s*(.*)\)\s*\)\s*\)/"__scanf(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005525 }
Joe Perches6061d942012-03-23 15:02:16 -07005526 }
5527
Joe Perches619a9082014-12-10 15:51:35 -08005528# Check for __attribute__ weak, or __weak declarations (may have link issues)
5529 if ($^V && $^V ge 5.10.0 &&
5530 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5531 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5532 $line =~ /\b__weak\b/)) {
5533 ERROR("WEAK_DECLARATION",
5534 "Using weak declarations can have unintended link defects\n" . $herecurr);
5535 }
5536
Joe Perchese6176fa2015-06-25 15:02:49 -07005537# check for c99 types like uint8_t used outside of uapi/
5538 if ($realfile !~ m@\binclude/uapi/@ &&
5539 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5540 my $type = $1;
5541 if ($type =~ /\b($typeC99Typedefs)\b/) {
5542 $type = $1;
5543 my $kernel_type = 'u';
5544 $kernel_type = 's' if ($type =~ /^_*[si]/);
5545 $type =~ /(\d+)/;
5546 $kernel_type .= $1;
5547 if (CHK("PREFER_KERNEL_TYPES",
5548 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5549 $fix) {
5550 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5551 }
5552 }
5553 }
5554
Joe Perches938224b2016-01-20 14:59:15 -08005555# check for cast of C90 native int or longer types constants
5556 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5557 my $cast = $1;
5558 my $const = $2;
5559 if (WARN("TYPECAST_INT_CONSTANT",
5560 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5561 $fix) {
5562 my $suffix = "";
5563 my $newconst = $const;
5564 $newconst =~ s/${Int_type}$//;
5565 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5566 if ($cast =~ /\blong\s+long\b/) {
5567 $suffix .= 'LL';
5568 } elsif ($cast =~ /\blong\b/) {
5569 $suffix .= 'L';
5570 }
5571 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5572 }
5573 }
5574
Joe Perches8f53a9b2010-03-05 13:43:48 -08005575# check for sizeof(&)
5576 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005577 WARN("SIZEOF_ADDRESS",
5578 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005579 }
5580
Joe Perches66c80b62012-07-30 14:41:22 -07005581# check for sizeof without parenthesis
5582 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005583 if (WARN("SIZEOF_PARENTHESIS",
5584 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5585 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005586 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005587 }
Joe Perches66c80b62012-07-30 14:41:22 -07005588 }
5589
Joe Perches88982fe2012-12-17 16:02:00 -08005590# check for struct spinlock declarations
5591 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5592 WARN("USE_SPINLOCK_T",
5593 "struct spinlock should be spinlock_t\n" . $herecurr);
5594 }
5595
Joe Perchesa6962d72013-04-29 16:18:13 -07005596# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005597 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005598 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005599 $fmt =~ s/%%//g;
5600 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005601 if (WARN("PREFER_SEQ_PUTS",
5602 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5603 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005604 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005605 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005606 }
5607 }
5608
Andy Whitcroft554e1652012-01-10 15:09:57 -08005609# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005610 if ($^V && $^V ge 5.10.0 &&
5611 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005612 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005613
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005614 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005615 my $ms_val = $7;
5616 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005617
Andy Whitcroft554e1652012-01-10 15:09:57 -08005618 if ($ms_size =~ /^(0x|)0$/i) {
5619 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005620 "memset to 0's uses 0 as the 2nd argument, not the 3rd\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005621 } elsif ($ms_size =~ /^(0x|)1$/i) {
5622 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005623 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5624 }
5625 }
5626
Joe Perches98a9bba2014-01-23 15:54:52 -08005627# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
5628 if ($^V && $^V ge 5.10.0 &&
Mateusz Kulikowski10895d22015-06-25 15:03:21 -07005629 defined $stat &&
5630 $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
Joe Perches98a9bba2014-01-23 15:54:52 -08005631 if (WARN("PREFER_ETHER_ADDR_COPY",
Mateusz Kulikowski10895d22015-06-25 15:03:21 -07005632 "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
Joe Perches98a9bba2014-01-23 15:54:52 -08005633 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005634 $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
Joe Perches98a9bba2014-01-23 15:54:52 -08005635 }
5636 }
5637
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005638# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
5639 if ($^V && $^V ge 5.10.0 &&
5640 defined $stat &&
5641 $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5642 WARN("PREFER_ETHER_ADDR_EQUAL",
5643 "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5644 }
5645
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005646# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5647# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
5648 if ($^V && $^V ge 5.10.0 &&
5649 defined $stat &&
5650 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5651
5652 my $ms_val = $7;
5653
5654 if ($ms_val =~ /^(?:0x|)0+$/i) {
5655 if (WARN("PREFER_ETH_ZERO_ADDR",
5656 "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5657 $fix) {
5658 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5659 }
5660 } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5661 if (WARN("PREFER_ETH_BROADCAST_ADDR",
5662 "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5663 $fix) {
5664 $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5665 }
5666 }
5667 }
5668
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005669# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005670 if ($^V && $^V ge 5.10.0 &&
5671 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005672 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005673 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005674 my $call = $1;
5675 my $cast1 = deparenthesize($2);
5676 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005677 my $cast2 = deparenthesize($7);
5678 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005679 my $cast;
5680
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005681 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005682 $cast = "$cast1 or $cast2";
5683 } elsif ($cast1 ne "") {
5684 $cast = $cast1;
5685 } else {
5686 $cast = $cast2;
5687 }
5688 WARN("MINMAX",
5689 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005690 }
5691 }
5692
Joe Perches4a273192012-07-30 14:41:20 -07005693# check usleep_range arguments
5694 if ($^V && $^V ge 5.10.0 &&
5695 defined $stat &&
5696 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5697 my $min = $1;
5698 my $max = $7;
5699 if ($min eq $max) {
5700 WARN("USLEEP_RANGE",
5701 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5702 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5703 $min > $max) {
5704 WARN("USLEEP_RANGE",
5705 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5706 }
5707 }
5708
Joe Perches823b7942013-11-12 15:10:15 -08005709# check for naked sscanf
5710 if ($^V && $^V ge 5.10.0 &&
5711 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005712 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005713 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5714 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5715 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5716 my $lc = $stat =~ tr@\n@@;
5717 $lc = $lc + $linenr;
5718 my $stat_real = raw_line($linenr, 0);
5719 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5720 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5721 }
5722 WARN("NAKED_SSCANF",
5723 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5724 }
5725
Joe Perchesafc819a2014-06-04 16:12:08 -07005726# check for simple sscanf that should be kstrto<foo>
5727 if ($^V && $^V ge 5.10.0 &&
5728 defined $stat &&
5729 $line =~ /\bsscanf\b/) {
5730 my $lc = $stat =~ tr@\n@@;
5731 $lc = $lc + $linenr;
5732 my $stat_real = raw_line($linenr, 0);
5733 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5734 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5735 }
5736 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5737 my $format = $6;
5738 my $count = $format =~ tr@%@%@;
5739 if ($count == 1 &&
5740 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5741 WARN("SSCANF_TO_KSTRTO",
5742 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5743 }
5744 }
5745 }
5746
Joe Perches70dc8a42013-09-11 14:23:58 -07005747# check for new externs in .h files.
5748 if ($realfile =~ /\.h$/ &&
5749 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005750 if (CHK("AVOID_EXTERNS",
5751 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005752 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005753 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005754 }
5755 }
5756
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005757# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005758 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005759 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005760 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005761 my $function_name = $1;
5762 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005763
5764 my $s = $stat;
5765 if (defined $cond) {
5766 substr($s, 0, length($cond), '');
5767 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005768 if ($s =~ /^\s*;/ &&
5769 $function_name ne 'uninitialized_var')
5770 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005771 WARN("AVOID_EXTERNS",
5772 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005773 }
5774
5775 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005776 WARN("FUNCTION_ARGUMENTS",
5777 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005778 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005779
5780 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5781 $stat =~ /^.\s*extern\s+/)
5782 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005783 WARN("AVOID_EXTERNS",
5784 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005785 }
5786
5787# checks for new __setup's
5788 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5789 my $name = $1;
5790
5791 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005792 CHK("UNDOCUMENTED_SETUP",
5793 "__setup appears un-documented -- check Documentation/kernel-parameters.txt\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005794 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005795 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005796
5797# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005798 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005799 WARN("UNNECESSARY_CASTS",
5800 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005801 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005802
Joe Perchesa640d252013-07-03 15:05:21 -07005803# alloc style
5804# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5805 if ($^V && $^V ge 5.10.0 &&
5806 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5807 CHK("ALLOC_SIZEOF_STRUCT",
5808 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5809 }
5810
Joe Perches60a55362014-06-04 16:12:07 -07005811# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5812 if ($^V && $^V ge 5.10.0 &&
Joe Perchese3674552014-08-06 16:10:55 -07005813 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07005814 my $oldfunc = $3;
5815 my $a1 = $4;
5816 my $a2 = $10;
5817 my $newfunc = "kmalloc_array";
5818 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07005819 my $r1 = $a1;
5820 my $r2 = $a2;
5821 if ($a1 =~ /^sizeof\s*\S/) {
5822 $r1 = $a2;
5823 $r2 = $a1;
5824 }
5825 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
5826 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches60a55362014-06-04 16:12:07 -07005827 if (WARN("ALLOC_WITH_MULTIPLY",
5828 "Prefer $newfunc over $oldfunc with multiply\n" . $herecurr) &&
5829 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005830 $fixed[$fixlinenr] =~ s/\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)/$1 . ' = ' . "$newfunc(" . trim($r1) . ', ' . trim($r2)/e;
Joe Perches60a55362014-06-04 16:12:07 -07005831
5832 }
5833 }
5834 }
5835
Joe Perches972fdea2013-04-29 16:18:12 -07005836# check for krealloc arg reuse
5837 if ($^V && $^V ge 5.10.0 &&
5838 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
5839 WARN("KREALLOC_ARG_REUSE",
5840 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
5841 }
5842
Joe Perches5ce59ae2013-02-21 16:44:18 -08005843# check for alloc argument mismatch
5844 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
5845 WARN("ALLOC_ARRAY_ARGS",
5846 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
5847 }
5848
Joe Perchescaf2a542011-01-12 16:59:56 -08005849# check for multiple semicolons
5850 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005851 if (WARN("ONE_SEMICOLON",
5852 "Statements terminations use 1 semicolon\n" . $herecurr) &&
5853 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005854 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005855 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005856 }
5857
Tomas Winklercec3aaa2016-08-02 14:04:39 -07005858# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
5859 if ($realfile !~ m@^include/uapi/@ &&
5860 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08005861 my $ull = "";
5862 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
5863 if (CHK("BIT_MACRO",
5864 "Prefer using the BIT$ull macro\n" . $herecurr) &&
5865 $fix) {
5866 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
5867 }
5868 }
5869
Joe Perches2d632742016-05-20 17:04:00 -07005870# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
5871 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
5872 my $config = $1;
5873 if (WARN("PREFER_IS_ENABLED",
5874 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
5875 $fix) {
5876 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
5877 }
5878 }
5879
Joe Perchese81f2392014-08-06 16:11:25 -07005880# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08005881 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
5882 my $has_break = 0;
5883 my $has_statement = 0;
5884 my $count = 0;
5885 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07005886 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08005887 $prevline--;
5888 my $rline = $rawlines[$prevline - 1];
5889 my $fline = $lines[$prevline - 1];
5890 last if ($fline =~ /^\@\@/);
5891 next if ($fline =~ /^\-/);
5892 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
5893 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
5894 next if ($fline =~ /^.[\s$;]*$/);
5895 $has_statement = 1;
5896 $count++;
5897 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
5898 }
5899 if (!$has_break && $has_statement) {
5900 WARN("MISSING_BREAK",
5901 "Possible switch case/default not preceeded by break or fallthrough comment\n" . $herecurr);
5902 }
5903 }
5904
Joe Perchesd1e2ad02012-12-17 16:02:01 -08005905# check for switch/default statements without a break;
5906 if ($^V && $^V ge 5.10.0 &&
5907 defined $stat &&
5908 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
5909 my $ctx = '';
5910 my $herectx = $here . "\n";
5911 my $cnt = statement_rawlines($stat);
5912 for (my $n = 0; $n < $cnt; $n++) {
5913 $herectx .= raw_line($linenr, $n) . "\n";
5914 }
5915 WARN("DEFAULT_NO_BREAK",
5916 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08005917 }
5918
Patrick Pannutof370e252010-07-23 13:34:18 -07005919# check for return codes on error paths
5920 if ($line =~ /\breturn\s+-\d+/) {
5921 ERROR("NO_ERROR_CODE",
Patrick Pannutoe50031c2010-08-04 14:30:59 -07005922 "illegal return value, please use an error code\n" . $herecurr);
Patrick Pannutof370e252010-07-23 13:34:18 -07005923 }
5924
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005925# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07005926 if ($line =~ /\b__FUNCTION__\b/) {
5927 if (WARN("USE_FUNC",
5928 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
5929 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005930 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005931 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005932 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005933
Joe Perches62ec8182015-02-13 14:38:18 -08005934# check for uses of __DATE__, __TIME__, __TIMESTAMP__
5935 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
5936 ERROR("DATE_TIME",
5937 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
5938 }
5939
Joe Perches2c924882012-03-23 15:02:20 -07005940# check for use of yield()
5941 if ($line =~ /\byield\s*\(\s*\)/) {
5942 WARN("YIELD",
5943 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
5944 }
5945
Joe Perches179f8f42013-07-03 15:05:30 -07005946# check for comparisons against true and false
5947 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
5948 my $lead = $1;
5949 my $arg = $2;
5950 my $test = $3;
5951 my $otype = $4;
5952 my $trail = $5;
5953 my $op = "!";
5954
5955 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
5956
5957 my $type = lc($otype);
5958 if ($type =~ /^(?:true|false)$/) {
5959 if (("$test" eq "==" && "$type" eq "true") ||
5960 ("$test" eq "!=" && "$type" eq "false")) {
5961 $op = "";
5962 }
5963
5964 CHK("BOOL_COMPARISON",
5965 "Using comparison to $otype is error prone\n" . $herecurr);
5966
5967## maybe suggesting a correct construct would better
5968## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
5969
5970 }
5971 }
5972
Thomas Gleixner4882720b2010-09-07 14:34:01 +00005973# check for semaphores initialized locked
5974 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005975 WARN("CONSIDER_COMPLETION",
5976 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005977 }
Joe Perches6712d852012-03-23 15:02:20 -07005978
Joe Perches67d0a072011-10-31 17:13:10 -07005979# recommend kstrto* over simple_strto* and strict_strto*
5980 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005981 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07005982 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07005983 }
Joe Perches6712d852012-03-23 15:02:20 -07005984
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005985# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07005986 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005987 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07005988 "please use device_initcall() or more appropriate function instead of __initcall() (see include/linux/init.h)\n" . $herecurr);
Michael Ellermanf3db6632008-07-23 21:28:57 -07005989 }
Joe Perches6712d852012-03-23 15:02:20 -07005990
Joe Perches0f3c5aa2015-02-13 14:39:05 -08005991# check for various structs that are normally const (ops, kgdb, device_tree)
5992 my $const_structs = qr{
5993 acpi_dock_ops|
Emese Revfy79404842010-03-05 13:43:53 -08005994 address_space_operations|
5995 backlight_ops|
5996 block_device_operations|
5997 dentry_operations|
5998 dev_pm_ops|
5999 dma_map_ops|
6000 extent_io_ops|
6001 file_lock_operations|
6002 file_operations|
6003 hv_ops|
6004 ide_dma_ops|
6005 intel_dvo_dev_ops|
6006 item_operations|
6007 iwl_ops|
6008 kgdb_arch|
6009 kgdb_io|
6010 kset_uevent_ops|
6011 lock_manager_operations|
6012 microcode_ops|
6013 mtrr_ops|
6014 neigh_ops|
6015 nlmsvc_binding|
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006016 of_device_id|
Emese Revfy79404842010-03-05 13:43:53 -08006017 pci_raw_ops|
6018 pipe_buf_operations|
6019 platform_hibernation_ops|
6020 platform_suspend_ops|
6021 proto_ops|
6022 rpc_pipe_ops|
6023 seq_operations|
6024 snd_ac97_build_ops|
6025 soc_pcmcia_socket_ops|
6026 stacktrace_ops|
6027 sysfs_ops|
6028 tty_operations|
Joe Perches6d07d01b2015-04-16 12:44:33 -07006029 uart_ops|
Emese Revfy79404842010-03-05 13:43:53 -08006030 usb_mon_operations|
6031 wd_ops}x;
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006032 if ($line !~ /\bconst\b/ &&
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006033 $line =~ /\bstruct\s+($const_structs)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006034 WARN("CONST_STRUCT",
6035 "struct $1 should normally be const\n" .
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006036 $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006037 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006038
6039# use of NR_CPUS is usually wrong
6040# ignore definitions of NR_CPUS and usage to define arrays as likely right
6041 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006042 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6043 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006044 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6045 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6046 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006047 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006048 WARN("NR_CPUS",
6049 "usage of NR_CPUS is often wrong - consider using cpu_possible(), num_possible_cpus(), for_each_possible_cpu(), etc\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006050 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006051
Joe Perches52ea8502013-11-12 15:10:09 -08006052# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6053 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6054 ERROR("DEFINE_ARCH_HAS",
6055 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6056 }
6057
Joe Perchesacd93622015-02-13 14:38:38 -08006058# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6059 if ($^V && $^V ge 5.10.0 &&
6060 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6061 WARN("LIKELY_MISUSE",
6062 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6063 }
6064
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006065# whine mightly about in_atomic
6066 if ($line =~ /\bin_atomic\s*\(/) {
6067 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006068 ERROR("IN_ATOMIC",
6069 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006070 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006071 WARN("IN_ATOMIC",
6072 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006073 }
6074 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006075
Joe Perches481aea52016-05-20 17:04:08 -07006076# whine about ACCESS_ONCE
6077 if ($^V && $^V ge 5.10.0 &&
6078 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6079 my $par = $1;
6080 my $eq = $2;
6081 my $fun = $3;
6082 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6083 if (defined($eq)) {
6084 if (WARN("PREFER_WRITE_ONCE",
6085 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6086 $fix) {
6087 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6088 }
6089 } else {
6090 if (WARN("PREFER_READ_ONCE",
6091 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6092 $fix) {
6093 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6094 }
6095 }
6096 }
6097
Peter Zijlstra1704f472010-03-19 01:37:42 +01006098# check for lockdep_set_novalidate_class
6099 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6100 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6101 if ($realfile !~ m@^kernel/lockdep@ &&
6102 $realfile !~ m@^include/linux/lockdep@ &&
6103 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006104 ERROR("LOCKDEP",
6105 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006106 }
6107 }
Dave Jones88f88312011-01-12 16:59:59 -08006108
Joe Perchesb392c642015-04-16 12:44:16 -07006109 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6110 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006111 WARN("EXPORTED_WORLD_WRITABLE",
6112 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006113 }
Joe Perches24358802014-04-03 14:49:13 -07006114
Joe Perches515a2352014-04-03 14:49:24 -07006115# Mode permission misuses where it seems decimal should be octal
6116# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6117 if ($^V && $^V ge 5.10.0 &&
6118 $line =~ /$mode_perms_search/) {
6119 foreach my $entry (@mode_permission_funcs) {
6120 my $func = $entry->[0];
6121 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006122
Joe Perches515a2352014-04-03 14:49:24 -07006123 my $skip_args = "";
6124 if ($arg_pos > 1) {
6125 $arg_pos--;
6126 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6127 }
6128 my $test = "\\b$func\\s*\\(${skip_args}([\\d]+)\\s*[,\\)]";
6129 if ($line =~ /$test/) {
6130 my $val = $1;
6131 $val = $6 if ($skip_args ne "");
Joe Perches24358802014-04-03 14:49:13 -07006132
Joe Perches515a2352014-04-03 14:49:24 -07006133 if ($val !~ /^0$/ &&
6134 (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6135 length($val) ne 4)) {
6136 ERROR("NON_OCTAL_PERMISSIONS",
6137 "Use 4 digit octal (0777) not decimal permissions\n" . $herecurr);
Joe Perchesc0a5c892015-02-13 14:38:21 -08006138 } elsif ($val =~ /^$Octal$/ && (oct($val) & 02)) {
6139 ERROR("EXPORTED_WORLD_WRITABLE",
6140 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Joe Perches515a2352014-04-03 14:49:24 -07006141 }
Joe Perches24358802014-04-03 14:49:13 -07006142 }
6143 }
6144 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006145
6146# validate content of MODULE_LICENSE against list from include/linux/module.h
6147 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6148 my $extracted_string = get_quoted_string($line, $rawline);
6149 my $valid_licenses = qr{
6150 GPL|
6151 GPL\ v2|
6152 GPL\ and\ additional\ rights|
6153 Dual\ BSD/GPL|
6154 Dual\ MIT/GPL|
6155 Dual\ MPL/GPL|
6156 Proprietary
6157 }x;
6158 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6159 WARN("MODULE_LICENSE",
6160 "unknown module license " . $extracted_string . "\n" . $herecurr);
6161 }
6162 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006163 }
6164
6165 # If we have no input at all, then there is nothing to report on
6166 # so just keep quiet.
6167 if ($#rawlines == -1) {
6168 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006169 }
6170
Andy Whitcroft8905a672007-11-28 16:21:06 -08006171 # In mailback mode only produce a report in the negative, for
6172 # things that appear to be patches.
6173 if ($mailback && ($clean == 1 || !$is_patch)) {
6174 exit(0);
6175 }
6176
6177 # This is not a patch, and we are are in 'no-patch' mode so
6178 # just keep quiet.
6179 if (!$chk_patch && !$is_patch) {
6180 exit(0);
6181 }
6182
Joe Perches06330fc2015-06-25 15:03:11 -07006183 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006184 ERROR("NOT_UNIFIED_DIFF",
6185 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006186 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006187 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006188 ERROR("MISSING_SIGN_OFF",
6189 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006190 }
6191
Andy Whitcroft8905a672007-11-28 16:21:06 -08006192 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006193 if ($summary && !($clean == 1 && $quiet == 1)) {
6194 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006195 print "total: $cnt_error errors, $cnt_warn warnings, " .
6196 (($check)? "$cnt_chk checks, " : "") .
6197 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006198 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006199
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006200 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006201 # If there were any defects found and not already fixing them
6202 if (!$clean and !$fix) {
6203 print << "EOM"
6204
6205NOTE: For some of the reported defects, checkpatch may be able to
6206 mechanically convert to the typical style using --fix or --fix-inplace.
6207EOM
6208 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006209 # If there were whitespace errors which cleanpatch can fix
6210 # then suggest that.
6211 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006212 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006213 print << "EOM"
6214
6215NOTE: Whitespace errors detected.
6216 You may wish to use scripts/cleanpatch or scripts/cleanfile
6217EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006218 }
6219 }
6220
Joe Perchesd752fcc2014-08-06 16:11:05 -07006221 if ($clean == 0 && $fix &&
6222 ("@rawlines" ne "@fixed" ||
6223 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006224 my $newfile = $filename;
6225 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006226 my $linecount = 0;
6227 my $f;
6228
Joe Perchesd752fcc2014-08-06 16:11:05 -07006229 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6230
Joe Perches3705ce52013-07-03 15:05:31 -07006231 open($f, '>', $newfile)
6232 or die "$P: Can't open $newfile for write\n";
6233 foreach my $fixed_line (@fixed) {
6234 $linecount++;
6235 if ($file) {
6236 if ($linecount > 3) {
6237 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006238 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006239 }
6240 } else {
6241 print $f $fixed_line . "\n";
6242 }
6243 }
6244 close($f);
6245
6246 if (!$quiet) {
6247 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006248
Joe Perches3705ce52013-07-03 15:05:31 -07006249Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6250
6251Do _NOT_ trust the results written to this file.
6252Do _NOT_ submit these changes without inspecting them for correctness.
6253
6254This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6255No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006256EOM
6257 }
6258 }
6259
Joe Perchesd8469f12015-06-25 15:03:00 -07006260 if ($quiet == 0) {
6261 print "\n";
6262 if ($clean == 1) {
6263 print "$vname has no obvious style problems and is ready for submission.\n";
6264 } else {
6265 print "$vname has style problems, please review.\n";
6266 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006267 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006268 return $clean;
6269}