blob: 2287a0bca863bf57ffabaae9de58aab0174826c3 [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
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;
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02009use warnings;
Joe Perchesc707a812013-07-08 16:00:43 -070010use POSIX;
Joe Perches36061e32014-12-10 15:51:43 -080011use File::Basename;
12use Cwd 'abs_path';
Joe Perches57230292015-06-25 15:03:03 -070013use Term::ANSIColor qw(:constants);
Andy Whitcroft0a920b52007-06-01 00:46:48 -070014
15my $P = $0;
Joe Perches36061e32014-12-10 15:51:43 -080016my $D = dirname(abs_path($P));
Andy Whitcroft0a920b52007-06-01 00:46:48 -070017
Joe Perches000d1cc12011-07-25 17:13:25 -070018my $V = '0.32';
Andy Whitcroft0a920b52007-06-01 00:46:48 -070019
20use Getopt::Long qw(:config no_auto_abbrev);
21
22my $quiet = 0;
23my $tree = 1;
24my $chk_signoff = 1;
25my $chk_patch = 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 Perches2ac73b42014-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 Perchesbf1fa1d2016-10-11 13:51:56 -070058my $conststructsfile = "$D/const_structs.checkpatch";
Jerome Forissier75ad8c52017-05-08 15:56:00 -070059my $typedefsfile = "";
John Brooks737c0762017-07-10 15:52:24 -070060my $color = "auto";
Joe Perchesdadf6802016-08-02 14:04:33 -070061my $allow_c99_comments = 1;
Hannes Eder77f5b102009-09-21 17:04:37 -070062
63sub help {
64 my ($exitcode) = @_;
65
66 print << "EOM";
67Usage: $P [OPTION]... [FILE]...
68Version: $V
69
70Options:
71 -q, --quiet quiet
72 --no-tree run without a kernel tree
73 --no-signoff do not check for 'Signed-off-by' line
74 --patch treat FILE as patchfile (default)
75 --emacs emacs compile window format
76 --terse one line per report
Joe Perches34d88152015-06-25 15:03:05 -070077 --showfile emit diffed file position, not input file position
Du, Changbin4a593c32016-05-20 17:04:16 -070078 -g, --git treat FILE as a single commit or git revision range
79 single git commit with:
80 <rev>
81 <rev>^
82 <rev>~n
83 multiple git commits with:
84 <rev1>..<rev2>
85 <rev1>...<rev2>
86 <rev>-<count>
87 git merges are ignored
Hannes Eder77f5b102009-09-21 17:04:37 -070088 -f, --file treat FILE as regular source file
89 --subjective, --strict enable more subjective tests
Joe Perches3beb42e2016-05-20 17:04:14 -070090 --list-types list the possible message types
Joe Perches91bfe482013-09-11 14:23:59 -070091 --types TYPE(,TYPE2...) show only these comma separated message types
Joe Perches000d1cc12011-07-25 17:13:25 -070092 --ignore TYPE(,TYPE2...) ignore various comma separated message types
Joe Perches3beb42e2016-05-20 17:04:14 -070093 --show-types show the specific message type in the output
Joe Perches6cd7f382012-12-17 16:01:54 -080094 --max-line-length=n set the maximum line length, if exceeded, warn
Vadim Bendebury56193272014-10-13 15:51:48 -070095 --min-conf-desc-length=n set the min description length, if shorter, warn
Hannes Eder77f5b102009-09-21 17:04:37 -070096 --root=PATH PATH to the kernel tree root
97 --no-summary suppress the per-file summary
98 --mailback only produce a report in case of warnings/errors
99 --summary-file include the filename in summary
100 --debug KEY=[0|1] turn on/off debugging of KEY, where KEY is one of
101 'values', 'possible', 'type', and 'attr' (default
102 is all off)
103 --test-only=WORD report only warnings/errors containing WORD
104 literally
Joe Perches3705ce52013-07-03 15:05:31 -0700105 --fix EXPERIMENTAL - may create horrible results
106 If correctable single-line errors exist, create
107 "<inputfile>.EXPERIMENTAL-checkpatch-fixes"
108 with potential errors corrected to the preferred
109 checkpatch style
Joe Perches9624b8d2014-01-23 15:54:44 -0800110 --fix-inplace EXPERIMENTAL - may create horrible results
111 Is the same as --fix, but overwrites the input
112 file. It's your fault if there's no backup or git
Dave Hansend62a2012013-09-11 14:23:56 -0700113 --ignore-perl-version override checking of perl version. expect
114 runtime errors.
Joe Perchesebfd7d62015-04-16 12:44:14 -0700115 --codespell Use the codespell dictionary for spelling/typos
Maxim Uvarovf1a63672015-06-25 15:03:08 -0700116 (default:/usr/share/codespell/dictionary.txt)
Joe Perchesebfd7d62015-04-16 12:44:14 -0700117 --codespellfile Use this codespell dictionary
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700118 --typedefsfile Read additional types from this file
John Brooks737c0762017-07-10 15:52:24 -0700119 --color[=WHEN] Use colors 'always', 'never', or only when output
120 is a terminal ('auto'). Default is 'auto'.
Hannes Eder77f5b102009-09-21 17:04:37 -0700121 -h, --help, --version display this help and exit
122
123When FILE is - read standard input.
124EOM
125
126 exit($exitcode);
127}
128
Joe Perches3beb42e2016-05-20 17:04:14 -0700129sub uniq {
130 my %seen;
131 return grep { !$seen{$_}++ } @_;
132}
133
134sub list_types {
135 my ($exitcode) = @_;
136
137 my $count = 0;
138
139 local $/ = undef;
140
141 open(my $script, '<', abs_path($P)) or
142 die "$P: Can't read '$P' $!\n";
143
144 my $text = <$script>;
145 close($script);
146
147 my @types = ();
148 for ($text =~ /\b(?:(?:CHK|WARN|ERROR)\s*\(\s*"([^"]+)")/g) {
149 push (@types, $_);
150 }
151 @types = sort(uniq(@types));
152 print("#\tMessage type\n\n");
153 foreach my $type (@types) {
154 print(++$count . "\t" . $type . "\n");
155 }
156
157 exit($exitcode);
158}
159
Joe Perches000d1cc12011-07-25 17:13:25 -0700160my $conf = which_conf($configuration_file);
161if (-f $conf) {
162 my @conf_args;
163 open(my $conffile, '<', "$conf")
164 or warn "$P: Can't find a readable $configuration_file file $!\n";
165
166 while (<$conffile>) {
167 my $line = $_;
168
169 $line =~ s/\s*\n?$//g;
170 $line =~ s/^\s*//g;
171 $line =~ s/\s+/ /g;
172
173 next if ($line =~ m/^\s*#/);
174 next if ($line =~ m/^\s*$/);
175
176 my @words = split(" ", $line);
177 foreach my $word (@words) {
178 last if ($word =~ m/^#/);
179 push (@conf_args, $word);
180 }
181 }
182 close($conffile);
183 unshift(@ARGV, @conf_args) if @conf_args;
184}
185
John Brooks737c0762017-07-10 15:52:24 -0700186# Perl's Getopt::Long allows options to take optional arguments after a space.
187# Prevent --color by itself from consuming other arguments
188foreach (@ARGV) {
189 if ($_ eq "--color" || $_ eq "-color") {
190 $_ = "--color=$color";
191 }
192}
193
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700194GetOptions(
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700195 'q|quiet+' => \$quiet,
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700196 'tree!' => \$tree,
197 'signoff!' => \$chk_signoff,
198 'patch!' => \$chk_patch,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700199 'emacs!' => \$emacs,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800200 'terse!' => \$terse,
Joe Perches34d88152015-06-25 15:03:05 -0700201 'showfile!' => \$showfile,
Hannes Eder77f5b102009-09-21 17:04:37 -0700202 'f|file!' => \$file,
Du, Changbin4a593c32016-05-20 17:04:16 -0700203 'g|git!' => \$git,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700204 'subjective!' => \$check,
205 'strict!' => \$check,
Joe Perches000d1cc12011-07-25 17:13:25 -0700206 'ignore=s' => \@ignore,
Joe Perches91bfe482013-09-11 14:23:59 -0700207 'types=s' => \@use,
Joe Perches000d1cc12011-07-25 17:13:25 -0700208 'show-types!' => \$show_types,
Joe Perches3beb42e2016-05-20 17:04:14 -0700209 'list-types!' => \$list_types,
Joe Perches6cd7f382012-12-17 16:01:54 -0800210 'max-line-length=i' => \$max_line_length,
Vadim Bendebury56193272014-10-13 15:51:48 -0700211 'min-conf-desc-length=i' => \$min_conf_desc_length,
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700212 'root=s' => \$root,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800213 'summary!' => \$summary,
214 'mailback!' => \$mailback,
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800215 'summary-file!' => \$summary_file,
Joe Perches3705ce52013-07-03 15:05:31 -0700216 'fix!' => \$fix,
Joe Perches9624b8d2014-01-23 15:54:44 -0800217 'fix-inplace!' => \$fix_inplace,
Dave Hansend62a2012013-09-11 14:23:56 -0700218 'ignore-perl-version!' => \$ignore_perl_version,
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800219 'debug=s' => \%debug,
Andy Whitcroft773647a2008-03-28 14:15:58 -0700220 'test-only=s' => \$tst_only,
Joe Perchesebfd7d62015-04-16 12:44:14 -0700221 'codespell!' => \$codespell,
222 'codespellfile=s' => \$codespellfile,
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700223 'typedefsfile=s' => \$typedefsfile,
John Brooks737c0762017-07-10 15:52:24 -0700224 'color=s' => \$color,
225 'no-color' => \$color, #keep old behaviors of -nocolor
226 'nocolor' => \$color, #keep old behaviors of -nocolor
Hannes Eder77f5b102009-09-21 17:04:37 -0700227 'h|help' => \$help,
228 'version' => \$help
229) or help(1);
230
231help(0) if ($help);
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700232
Joe Perches3beb42e2016-05-20 17:04:14 -0700233list_types(0) if ($list_types);
234
Joe Perches9624b8d2014-01-23 15:54:44 -0800235$fix = 1 if ($fix_inplace);
Joe Perches2ac73b42014-06-04 16:12:05 -0700236$check_orig = $check;
Joe Perches9624b8d2014-01-23 15:54:44 -0800237
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700238my $exit = 0;
239
Dave Hansend62a2012013-09-11 14:23:56 -0700240if ($^V && $^V lt $minimum_perl_version) {
241 printf "$P: requires at least perl version %vd\n", $minimum_perl_version;
242 if (!$ignore_perl_version) {
243 exit(1);
244 }
245}
246
Allen Hubbe45107ff2016-08-02 14:04:47 -0700247#if no filenames are given, push '-' to read patch from stdin
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700248if ($#ARGV < 0) {
Allen Hubbe45107ff2016-08-02 14:04:47 -0700249 push(@ARGV, '-');
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700250}
251
John Brooks737c0762017-07-10 15:52:24 -0700252if ($color =~ /^[01]$/) {
253 $color = !$color;
254} elsif ($color =~ /^always$/i) {
255 $color = 1;
256} elsif ($color =~ /^never$/i) {
257 $color = 0;
258} elsif ($color =~ /^auto$/i) {
259 $color = (-t STDOUT);
260} else {
261 die "Invalid color mode: $color\n";
262}
263
Joe Perches91bfe482013-09-11 14:23:59 -0700264sub hash_save_array_words {
265 my ($hashRef, $arrayRef) = @_;
Joe Perches000d1cc12011-07-25 17:13:25 -0700266
Joe Perches91bfe482013-09-11 14:23:59 -0700267 my @array = split(/,/, join(',', @$arrayRef));
268 foreach my $word (@array) {
269 $word =~ s/\s*\n?$//g;
270 $word =~ s/^\s*//g;
271 $word =~ s/\s+/ /g;
272 $word =~ tr/[a-z]/[A-Z]/;
Joe Perches000d1cc12011-07-25 17:13:25 -0700273
Joe Perches91bfe482013-09-11 14:23:59 -0700274 next if ($word =~ m/^\s*#/);
275 next if ($word =~ m/^\s*$/);
276
277 $hashRef->{$word}++;
278 }
Joe Perches000d1cc12011-07-25 17:13:25 -0700279}
280
Joe Perches91bfe482013-09-11 14:23:59 -0700281sub hash_show_words {
282 my ($hashRef, $prefix) = @_;
283
Joe Perches3c816e42015-06-25 15:03:29 -0700284 if (keys %$hashRef) {
Joe Perchesd8469f12015-06-25 15:03:00 -0700285 print "\nNOTE: $prefix message types:";
Joe Perches58cb3cf2013-09-11 14:24:04 -0700286 foreach my $word (sort keys %$hashRef) {
Joe Perches91bfe482013-09-11 14:23:59 -0700287 print " $word";
288 }
Joe Perchesd8469f12015-06-25 15:03:00 -0700289 print "\n";
Joe Perches91bfe482013-09-11 14:23:59 -0700290 }
291}
292
293hash_save_array_words(\%ignore_type, \@ignore);
294hash_save_array_words(\%use_type, \@use);
295
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800296my $dbg_values = 0;
297my $dbg_possible = 0;
Andy Whitcroft7429c692008-07-23 21:29:06 -0700298my $dbg_type = 0;
Andy Whitcrofta1ef2772008-10-15 22:02:17 -0700299my $dbg_attr = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800300for my $key (keys %debug) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800301 ## no critic
302 eval "\${dbg_$key} = '$debug{$key}';";
303 die "$@" if ($@);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800304}
305
Andy Whitcroftd2c0a232010-10-26 14:23:12 -0700306my $rpt_cleaners = 0;
307
Andy Whitcroft8905a672007-11-28 16:21:06 -0800308if ($terse) {
309 $emacs = 1;
310 $quiet++;
311}
312
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700313if ($tree) {
314 if (defined $root) {
315 if (!top_of_kernel_tree($root)) {
316 die "$P: $root: --root does not point at a valid tree\n";
317 }
318 } else {
319 if (top_of_kernel_tree('.')) {
320 $root = '.';
321 } elsif ($0 =~ m@(.*)/scripts/[^/]*$@ &&
322 top_of_kernel_tree($1)) {
323 $root = $1;
324 }
325 }
326
327 if (!defined $root) {
328 print "Must be run from the top-level dir. of a kernel tree\n";
329 exit(2);
330 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700331}
332
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700333my $emitted_corrupt = 0;
334
Andy Whitcroft2ceb5322009-10-26 16:50:14 -0700335our $Ident = qr{
336 [A-Za-z_][A-Za-z\d_]*
337 (?:\s*\#\#\s*[A-Za-z_][A-Za-z\d_]*)*
338 }x;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700339our $Storage = qr{extern|static|asmlinkage};
340our $Sparse = qr{
341 __user|
342 __kernel|
343 __force|
344 __iomem|
345 __must_check|
346 __init_refok|
Andy Whitcroft417495e2009-02-27 14:03:08 -0800347 __kprobes|
Sven Eckelmann165e72a2011-07-25 17:13:23 -0700348 __ref|
Boqun Fengad315452015-12-29 12:18:46 +0800349 __rcu|
350 __private
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700351 }x;
Joe Perchese970b8842013-11-12 15:10:10 -0800352our $InitAttributePrefix = qr{__(?:mem|cpu|dev|net_|)};
353our $InitAttributeData = qr{$InitAttributePrefix(?:initdata\b)};
354our $InitAttributeConst = qr{$InitAttributePrefix(?:initconst\b)};
355our $InitAttributeInit = qr{$InitAttributePrefix(?:init\b)};
356our $InitAttribute = qr{$InitAttributeData|$InitAttributeConst|$InitAttributeInit};
Joe Perches8716de32013-09-11 14:24:05 -0700357
Wolfram Sang52131292010-03-05 13:43:51 -0800358# Notes to $Attribute:
359# We need \b after 'init' otherwise 'initconst' will cause a false positive in a check
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700360our $Attribute = qr{
361 const|
Joe Perches03f1df72010-10-26 14:23:16 -0700362 __percpu|
363 __nocast|
364 __safe|
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +0200365 __bitwise|
Joe Perches03f1df72010-10-26 14:23:16 -0700366 __packed__|
367 __packed2__|
368 __naked|
369 __maybe_unused|
370 __always_unused|
371 __noreturn|
372 __used|
373 __cold|
Joe Perchese23ef1f2015-02-13 14:38:24 -0800374 __pure|
Joe Perches03f1df72010-10-26 14:23:16 -0700375 __noclone|
376 __deprecated|
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700377 __read_mostly|
378 __kprobes|
Joe Perches8716de32013-09-11 14:24:05 -0700379 $InitAttribute|
Andy Whitcroft24e1d812008-10-15 22:02:18 -0700380 ____cacheline_aligned|
381 ____cacheline_aligned_in_smp|
Andy Whitcroft5fe3af12009-01-06 14:41:18 -0800382 ____cacheline_internodealigned_in_smp|
383 __weak
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700384 }x;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700385our $Modifier;
Joe Perches91cb5192014-04-03 14:49:32 -0700386our $Inline = qr{inline|__always_inline|noinline|__inline|__inline__};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700387our $Member = qr{->$Ident|\.$Ident|\[[^]]*\]};
388our $Lval = qr{$Ident(?:$Member)*};
389
Joe Perches95e2c602013-07-03 15:05:20 -0700390our $Int_type = qr{(?i)llu|ull|ll|lu|ul|l|u};
391our $Binary = qr{(?i)0b[01]+$Int_type?};
392our $Hex = qr{(?i)0x[0-9a-f]+$Int_type?};
393our $Int = qr{[0-9]+$Int_type?};
Joe Perches24358802014-04-03 14:49:13 -0700394our $Octal = qr{0[0-7]+$Int_type?};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800395our $String = qr{"[X\t]*"};
Joe Perches326b1ff2013-02-04 14:28:51 -0800396our $Float_hex = qr{(?i)0x[0-9a-f]+p-?[0-9]+[fl]?};
397our $Float_dec = qr{(?i)(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e-?[0-9]+)?[fl]?};
398our $Float_int = qr{(?i)[0-9]+e-?[0-9]+[fl]?};
Joe Perches74349bc2012-12-17 16:02:05 -0800399our $Float = qr{$Float_hex|$Float_dec|$Float_int};
Joe Perches24358802014-04-03 14:49:13 -0700400our $Constant = qr{$Float|$Binary|$Octal|$Hex|$Int};
Joe Perches326b1ff2013-02-04 14:28:51 -0800401our $Assignment = qr{\*\=|/=|%=|\+=|-=|<<=|>>=|&=|\^=|\|=|=};
Joe Perches447432f2014-04-03 14:49:17 -0700402our $Compare = qr{<=|>=|==|!=|<|(?<!-)>};
Joe Perches23f780c2013-07-03 15:05:31 -0700403our $Arithmetic = qr{\+|-|\*|\/|%};
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700404our $Operators = qr{
405 <=|>=|==|!=|
406 =>|->|<<|>>|<|>|!|~|
Joe Perches23f780c2013-07-03 15:05:31 -0700407 &&|\|\||,|\^|\+\+|--|&|\||$Arithmetic
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700408 }x;
409
Joe Perches91cb5192014-04-03 14:49:32 -0700410our $c90_Keywords = qr{do|for|while|if|else|return|goto|continue|switch|default|case|break}x;
411
Joe Perchesab7e23f2015-04-16 12:44:22 -0700412our $BasicType;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800413our $NonptrType;
Joe Perches18130872014-08-06 16:11:22 -0700414our $NonptrTypeMisordered;
Joe Perches8716de32013-09-11 14:24:05 -0700415our $NonptrTypeWithAttr;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800416our $Type;
Joe Perches18130872014-08-06 16:11:22 -0700417our $TypeMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800418our $Declare;
Joe Perches18130872014-08-06 16:11:22 -0700419our $DeclareMisordered;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800420
Joe Perches15662b32011-10-31 17:13:12 -0700421our $NON_ASCII_UTF8 = qr{
422 [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
Andy Whitcroft171ae1a2008-04-29 00:59:32 -0700423 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
424 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
425 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
426 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
427 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
428 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
429}x;
430
Joe Perches15662b32011-10-31 17:13:12 -0700431our $UTF8 = qr{
432 [\x09\x0A\x0D\x20-\x7E] # ASCII
433 | $NON_ASCII_UTF8
434}x;
435
Joe Perchese6176fa2015-06-25 15:02:49 -0700436our $typeC99Typedefs = qr{(?:__)?(?:[us]_?)?int_?(?:8|16|32|64)_t};
Joe Perches021158b2015-02-13 14:38:43 -0800437our $typeOtherOSTypedefs = qr{(?x:
438 u_(?:char|short|int|long) | # bsd
439 u(?:nchar|short|int|long) # sysv
440)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700441our $typeKernelTypedefs = qr{(?x:
Andy Whitcroftfb9e9092009-09-21 17:04:38 -0700442 (?:__)?(?:u|s|be|le)(?:8|16|32|64)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700443 atomic_t
444)};
Joe Perchese6176fa2015-06-25 15:02:49 -0700445our $typeTypedefs = qr{(?x:
446 $typeC99Typedefs\b|
447 $typeOtherOSTypedefs\b|
448 $typeKernelTypedefs\b
449)};
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700450
Joe Perches6d32f7a2015-11-06 16:31:37 -0800451our $zero_initializer = qr{(?:(?:0[xX])?0+$Int_type?|NULL|false)\b};
452
Joe Perches691e6692010-03-05 13:43:51 -0800453our $logFunctions = qr{(?x:
Miles Chen758d7aa2017-02-24 15:01:34 -0800454 printk(?:_ratelimited|_once|_deferred_once|_deferred|)|
Jacob Keller7d0b6592013-07-03 15:05:35 -0700455 (?:[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 -0700456 WARN(?:_RATELIMIT|_ONCE|)|
Joe Perchesb0531722011-05-24 17:13:40 -0700457 panic|
Joe Perches06668722013-11-12 15:10:07 -0800458 MODULE_[A-Z_]+|
459 seq_vprintf|seq_printf|seq_puts
Joe Perches691e6692010-03-05 13:43:51 -0800460)};
461
Joe Perches20112472011-07-25 17:13:23 -0700462our $signature_tags = qr{(?xi:
463 Signed-off-by:|
464 Acked-by:|
465 Tested-by:|
466 Reviewed-by:|
467 Reported-by:|
Mugunthan V N8543ae12013-04-29 16:18:17 -0700468 Suggested-by:|
Joe Perches20112472011-07-25 17:13:23 -0700469 To:|
470 Cc:
471)};
472
Joe Perches18130872014-08-06 16:11:22 -0700473our @typeListMisordered = (
474 qr{char\s+(?:un)?signed},
475 qr{int\s+(?:(?:un)?signed\s+)?short\s},
476 qr{int\s+short(?:\s+(?:un)?signed)},
477 qr{short\s+int(?:\s+(?:un)?signed)},
478 qr{(?:un)?signed\s+int\s+short},
479 qr{short\s+(?:un)?signed},
480 qr{long\s+int\s+(?:un)?signed},
481 qr{int\s+long\s+(?:un)?signed},
482 qr{long\s+(?:un)?signed\s+int},
483 qr{int\s+(?:un)?signed\s+long},
484 qr{int\s+(?:un)?signed},
485 qr{int\s+long\s+long\s+(?:un)?signed},
486 qr{long\s+long\s+int\s+(?:un)?signed},
487 qr{long\s+long\s+(?:un)?signed\s+int},
488 qr{long\s+long\s+(?:un)?signed},
489 qr{long\s+(?:un)?signed},
490);
491
Andy Whitcroft8905a672007-11-28 16:21:06 -0800492our @typeList = (
493 qr{void},
Joe Perches0c773d92014-08-06 16:11:20 -0700494 qr{(?:(?:un)?signed\s+)?char},
495 qr{(?:(?:un)?signed\s+)?short\s+int},
496 qr{(?:(?:un)?signed\s+)?short},
497 qr{(?:(?:un)?signed\s+)?int},
498 qr{(?:(?:un)?signed\s+)?long\s+int},
499 qr{(?:(?:un)?signed\s+)?long\s+long\s+int},
500 qr{(?:(?:un)?signed\s+)?long\s+long},
501 qr{(?:(?:un)?signed\s+)?long},
502 qr{(?:un)?signed},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800503 qr{float},
504 qr{double},
505 qr{bool},
Andy Whitcroft8905a672007-11-28 16:21:06 -0800506 qr{struct\s+$Ident},
507 qr{union\s+$Ident},
508 qr{enum\s+$Ident},
509 qr{${Ident}_t},
510 qr{${Ident}_handler},
511 qr{${Ident}_handler_fn},
Joe Perches18130872014-08-06 16:11:22 -0700512 @typeListMisordered,
Andy Whitcroft8905a672007-11-28 16:21:06 -0800513);
Joe Perches938224b2016-01-20 14:59:15 -0800514
515our $C90_int_types = qr{(?x:
516 long\s+long\s+int\s+(?:un)?signed|
517 long\s+long\s+(?:un)?signed\s+int|
518 long\s+long\s+(?:un)?signed|
519 (?:(?:un)?signed\s+)?long\s+long\s+int|
520 (?:(?:un)?signed\s+)?long\s+long|
521 int\s+long\s+long\s+(?:un)?signed|
522 int\s+(?:(?:un)?signed\s+)?long\s+long|
523
524 long\s+int\s+(?:un)?signed|
525 long\s+(?:un)?signed\s+int|
526 long\s+(?:un)?signed|
527 (?:(?:un)?signed\s+)?long\s+int|
528 (?:(?:un)?signed\s+)?long|
529 int\s+long\s+(?:un)?signed|
530 int\s+(?:(?:un)?signed\s+)?long|
531
532 int\s+(?:un)?signed|
533 (?:(?:un)?signed\s+)?int
534)};
535
Alex Dowad485ff232015-06-25 15:02:52 -0700536our @typeListFile = ();
Joe Perches8716de32013-09-11 14:24:05 -0700537our @typeListWithAttr = (
538 @typeList,
539 qr{struct\s+$InitAttribute\s+$Ident},
540 qr{union\s+$InitAttribute\s+$Ident},
541);
542
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700543our @modifierList = (
544 qr{fastcall},
545);
Alex Dowad485ff232015-06-25 15:02:52 -0700546our @modifierListFile = ();
Andy Whitcroft8905a672007-11-28 16:21:06 -0800547
Joe Perches24358802014-04-03 14:49:13 -0700548our @mode_permission_funcs = (
549 ["module_param", 3],
550 ["module_param_(?:array|named|string)", 4],
551 ["module_param_array_named", 5],
552 ["debugfs_create_(?:file|u8|u16|u32|u64|x8|x16|x32|x64|size_t|atomic_t|bool|blob|regset32|u32_array)", 2],
553 ["proc_create(?:_data|)", 2],
Joe Perches459cf0a2016-10-11 13:52:19 -0700554 ["(?:CLASS|DEVICE|SENSOR|SENSOR_DEVICE|IIO_DEVICE)_ATTR", 2],
555 ["IIO_DEV_ATTR_[A-Z_]+", 1],
556 ["SENSOR_(?:DEVICE_|)ATTR_2", 2],
557 ["SENSOR_TEMPLATE(?:_2|)", 3],
558 ["__ATTR", 2],
Joe Perches24358802014-04-03 14:49:13 -0700559);
560
Joe Perches515a2352014-04-03 14:49:24 -0700561#Create a search pattern for all these functions to speed up a loop below
562our $mode_perms_search = "";
563foreach my $entry (@mode_permission_funcs) {
564 $mode_perms_search .= '|' if ($mode_perms_search ne "");
565 $mode_perms_search .= $entry->[0];
566}
567
Joe Perchesb392c642015-04-16 12:44:16 -0700568our $mode_perms_world_writable = qr{
569 S_IWUGO |
570 S_IWOTH |
571 S_IRWXUGO |
572 S_IALLUGO |
573 0[0-7][0-7][2367]
574}x;
575
Joe Perchesf90774e2016-10-11 13:51:47 -0700576our %mode_permission_string_types = (
577 "S_IRWXU" => 0700,
578 "S_IRUSR" => 0400,
579 "S_IWUSR" => 0200,
580 "S_IXUSR" => 0100,
581 "S_IRWXG" => 0070,
582 "S_IRGRP" => 0040,
583 "S_IWGRP" => 0020,
584 "S_IXGRP" => 0010,
585 "S_IRWXO" => 0007,
586 "S_IROTH" => 0004,
587 "S_IWOTH" => 0002,
588 "S_IXOTH" => 0001,
589 "S_IRWXUGO" => 0777,
590 "S_IRUGO" => 0444,
591 "S_IWUGO" => 0222,
592 "S_IXUGO" => 0111,
593);
594
595#Create a search pattern for all these strings to speed up a loop below
596our $mode_perms_string_search = "";
597foreach my $entry (keys %mode_permission_string_types) {
598 $mode_perms_string_search .= '|' if ($mode_perms_string_search ne "");
599 $mode_perms_string_search .= $entry;
600}
601
Wolfram Sang7840a942010-08-09 17:20:57 -0700602our $allowed_asm_includes = qr{(?x:
603 irq|
Sergey Ryazanovcdcee682014-10-13 15:51:44 -0700604 memory|
605 time|
606 reboot
Wolfram Sang7840a942010-08-09 17:20:57 -0700607)};
608# memory.h: ARM has a custom one
609
Kees Cook66b47b42014-10-13 15:51:57 -0700610# Load common spelling mistakes and build regular expression list.
611my $misspellings;
Kees Cook66b47b42014-10-13 15:51:57 -0700612my %spelling_fix;
Kees Cook66b47b42014-10-13 15:51:57 -0700613
Joe Perches36061e32014-12-10 15:51:43 -0800614if (open(my $spelling, '<', $spelling_file)) {
Joe Perches36061e32014-12-10 15:51:43 -0800615 while (<$spelling>) {
616 my $line = $_;
Kees Cook66b47b42014-10-13 15:51:57 -0700617
Joe Perches36061e32014-12-10 15:51:43 -0800618 $line =~ s/\s*\n?$//g;
619 $line =~ s/^\s*//g;
Kees Cook66b47b42014-10-13 15:51:57 -0700620
Joe Perches36061e32014-12-10 15:51:43 -0800621 next if ($line =~ m/^\s*#/);
622 next if ($line =~ m/^\s*$/);
Kees Cook66b47b42014-10-13 15:51:57 -0700623
Joe Perches36061e32014-12-10 15:51:43 -0800624 my ($suspect, $fix) = split(/\|\|/, $line);
625
Joe Perches36061e32014-12-10 15:51:43 -0800626 $spelling_fix{$suspect} = $fix;
627 }
628 close($spelling);
Joe Perches36061e32014-12-10 15:51:43 -0800629} else {
630 warn "No typos will be found - file '$spelling_file': $!\n";
Kees Cook66b47b42014-10-13 15:51:57 -0700631}
Kees Cook66b47b42014-10-13 15:51:57 -0700632
Joe Perchesebfd7d62015-04-16 12:44:14 -0700633if ($codespell) {
634 if (open(my $spelling, '<', $codespellfile)) {
635 while (<$spelling>) {
636 my $line = $_;
637
638 $line =~ s/\s*\n?$//g;
639 $line =~ s/^\s*//g;
640
641 next if ($line =~ m/^\s*#/);
642 next if ($line =~ m/^\s*$/);
643 next if ($line =~ m/, disabled/i);
644
645 $line =~ s/,.*$//;
646
647 my ($suspect, $fix) = split(/->/, $line);
648
649 $spelling_fix{$suspect} = $fix;
650 }
651 close($spelling);
652 } else {
653 warn "No codespell typos will be found - file '$codespellfile': $!\n";
654 }
655}
656
657$misspellings = join("|", sort keys %spelling_fix) if keys %spelling_fix;
658
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700659sub read_words {
660 my ($wordsRef, $file) = @_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700661
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700662 if (open(my $words, '<', $file)) {
663 while (<$words>) {
664 my $line = $_;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700665
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700666 $line =~ s/\s*\n?$//g;
667 $line =~ s/^\s*//g;
668
669 next if ($line =~ m/^\s*#/);
670 next if ($line =~ m/^\s*$/);
671 if ($line =~ /\s/) {
672 print("$file: '$line' invalid - ignored\n");
673 next;
674 }
675
676 $$wordsRef .= '|' if ($$wordsRef ne "");
677 $$wordsRef .= $line;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700678 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700679 close($file);
680 return 1;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700681 }
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700682
683 return 0;
Joe Perchesbf1fa1d2016-10-11 13:51:56 -0700684}
685
Jerome Forissier75ad8c52017-05-08 15:56:00 -0700686my $const_structs = "";
687read_words(\$const_structs, $conststructsfile)
688 or warn "No structs that should be const will be found - file '$conststructsfile': $!\n";
689
690my $typeOtherTypedefs = "";
691if (length($typedefsfile)) {
692 read_words(\$typeOtherTypedefs, $typedefsfile)
693 or warn "No additional types will be considered - file '$typedefsfile': $!\n";
694}
695$typeTypedefs .= '|' . $typeOtherTypedefs if ($typeOtherTypedefs ne "");
696
Andy Whitcroft8905a672007-11-28 16:21:06 -0800697sub build_types {
Alex Dowad485ff232015-06-25 15:02:52 -0700698 my $mods = "(?x: \n" . join("|\n ", (@modifierList, @modifierListFile)) . "\n)";
699 my $all = "(?x: \n" . join("|\n ", (@typeList, @typeListFile)) . "\n)";
Joe Perches18130872014-08-06 16:11:22 -0700700 my $Misordered = "(?x: \n" . join("|\n ", @typeListMisordered) . "\n)";
Joe Perches8716de32013-09-11 14:24:05 -0700701 my $allWithAttr = "(?x: \n" . join("|\n ", @typeListWithAttr) . "\n)";
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700702 $Modifier = qr{(?:$Attribute|$Sparse|$mods)};
Joe Perchesab7e23f2015-04-16 12:44:22 -0700703 $BasicType = qr{
Joe Perchesab7e23f2015-04-16 12:44:22 -0700704 (?:$typeTypedefs\b)|
705 (?:${all}\b)
706 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800707 $NonptrType = qr{
Andy Whitcroftd2172eb2008-07-23 21:29:07 -0700708 (?:$Modifier\s+|const\s+)*
Andy Whitcroftcf655042008-03-04 14:28:20 -0800709 (?:
Andy Whitcroft6b48db22012-01-10 15:10:13 -0800710 (?:typeof|__typeof__)\s*\([^\)]*\)|
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -0700711 (?:$typeTypedefs\b)|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700712 (?:${all}\b)
Andy Whitcroftcf655042008-03-04 14:28:20 -0800713 )
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700714 (?:\s+$Modifier|\s+const)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800715 }x;
Joe Perches18130872014-08-06 16:11:22 -0700716 $NonptrTypeMisordered = qr{
717 (?:$Modifier\s+|const\s+)*
718 (?:
719 (?:${Misordered}\b)
720 )
721 (?:\s+$Modifier|\s+const)*
722 }x;
Joe Perches8716de32013-09-11 14:24:05 -0700723 $NonptrTypeWithAttr = qr{
724 (?:$Modifier\s+|const\s+)*
725 (?:
726 (?:typeof|__typeof__)\s*\([^\)]*\)|
727 (?:$typeTypedefs\b)|
728 (?:${allWithAttr}\b)
729 )
730 (?:\s+$Modifier|\s+const)*
731 }x;
Andy Whitcroft8905a672007-11-28 16:21:06 -0800732 $Type = qr{
Andy Whitcroftc45dcab2008-06-05 22:46:01 -0700733 $NonptrType
Joe Perches1574a292014-08-06 16:10:50 -0700734 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -0700735 (?:\s+$Inline|\s+$Modifier)*
Andy Whitcroft8905a672007-11-28 16:21:06 -0800736 }x;
Joe Perches18130872014-08-06 16:11:22 -0700737 $TypeMisordered = qr{
738 $NonptrTypeMisordered
739 (?:(?:\s|\*|\[\])+\s*const|(?:\s|\*\s*(?:const\s*)?|\[\])+|(?:\s*\[\s*\])+)?
740 (?:\s+$Inline|\s+$Modifier)*
741 }x;
Joe Perches91cb5192014-04-03 14:49:32 -0700742 $Declare = qr{(?:$Storage\s+(?:$Inline\s+)?)?$Type};
Joe Perches18130872014-08-06 16:11:22 -0700743 $DeclareMisordered = qr{(?:$Storage\s+(?:$Inline\s+)?)?$TypeMisordered};
Andy Whitcroft8905a672007-11-28 16:21:06 -0800744}
745build_types();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700746
Joe Perches7d2367a2011-07-25 17:13:22 -0700747our $Typecast = qr{\s*(\(\s*$NonptrType\s*\)){0,1}\s*};
Joe Perchesd1fe9c02012-03-23 15:02:16 -0700748
749# Using $balanced_parens, $LvalOrFunc, or $FuncArg
750# requires at least perl version v5.10.0
751# Any use must be runtime checked with $^V
752
753our $balanced_parens = qr/(\((?:[^\(\)]++|(?-1))*\))/;
Joe Perches24358802014-04-03 14:49:13 -0700754our $LvalOrFunc = qr{((?:[\&\*]\s*)?$Lval)\s*($balanced_parens{0,1})\s*};
Joe Perchesc0a5c892015-02-13 14:38:21 -0800755our $FuncArg = qr{$Typecast{0,1}($LvalOrFunc|$Constant|$String)};
Joe Perches7d2367a2011-07-25 17:13:22 -0700756
Joe Perchesf8422302014-08-06 16:11:31 -0700757our $declaration_macros = qr{(?x:
Joe Perches3e838b62015-09-09 15:37:33 -0700758 (?:$Storage\s+)?(?:[A-Z_][A-Z0-9]*_){0,2}(?:DEFINE|DECLARE)(?:_[A-Z0-9]+){1,6}\s*\(|
Steffen Maierfe658f92017-07-10 15:52:10 -0700759 (?:$Storage\s+)?[HLP]?LIST_HEAD\s*\(|
Joe Perchesf8422302014-08-06 16:11:31 -0700760 (?:$Storage\s+)?${Type}\s+uninitialized_var\s*\(
761)};
762
Joe Perches7d2367a2011-07-25 17:13:22 -0700763sub deparenthesize {
764 my ($string) = @_;
765 return "" if (!defined($string));
Joe Perches5b9553a2014-04-03 14:49:21 -0700766
767 while ($string =~ /^\s*\(.*\)\s*$/) {
768 $string =~ s@^\s*\(\s*@@;
769 $string =~ s@\s*\)\s*$@@;
770 }
771
Joe Perches7d2367a2011-07-25 17:13:22 -0700772 $string =~ s@\s+@ @g;
Joe Perches5b9553a2014-04-03 14:49:21 -0700773
Joe Perches7d2367a2011-07-25 17:13:22 -0700774 return $string;
775}
776
Joe Perches34456862013-07-03 15:05:34 -0700777sub seed_camelcase_file {
778 my ($file) = @_;
779
780 return if (!(-f $file));
781
782 local $/;
783
784 open(my $include_file, '<', "$file")
785 or warn "$P: Can't read '$file' $!\n";
786 my $text = <$include_file>;
787 close($include_file);
788
789 my @lines = split('\n', $text);
790
791 foreach my $line (@lines) {
792 next if ($line !~ /(?:[A-Z][a-z]|[a-z][A-Z])/);
793 if ($line =~ /^[ \t]*(?:#[ \t]*define|typedef\s+$Type)\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)/) {
794 $camelcase{$1} = 1;
Joe Perches11ea5162013-11-12 15:10:08 -0800795 } elsif ($line =~ /^\s*$Declare\s+(\w*(?:[A-Z][a-z]|[a-z][A-Z])\w*)\s*[\(\[,;]/) {
796 $camelcase{$1} = 1;
797 } 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 -0700798 $camelcase{$1} = 1;
799 }
800 }
801}
802
Joe Perches85b0ee12016-10-11 13:51:44 -0700803sub is_maintained_obsolete {
804 my ($filename) = @_;
805
Jerome Forissierf2c19c22016-12-12 16:46:23 -0800806 return 0 if (!$tree || !(-e "$root/scripts/get_maintainer.pl"));
Joe Perches85b0ee12016-10-11 13:51:44 -0700807
Joe Perches0616efa2016-10-11 13:52:02 -0700808 my $status = `perl $root/scripts/get_maintainer.pl --status --nom --nol --nogit --nogit-fallback -f $filename 2>&1`;
Joe Perches85b0ee12016-10-11 13:51:44 -0700809
810 return $status =~ /obsolete/i;
811}
812
Joe Perches34456862013-07-03 15:05:34 -0700813my $camelcase_seeded = 0;
814sub seed_camelcase_includes {
815 return if ($camelcase_seeded);
816
817 my $files;
Joe Perchesc707a812013-07-08 16:00:43 -0700818 my $camelcase_cache = "";
819 my @include_files = ();
820
821 $camelcase_seeded = 1;
Joe Perches351b2a12013-07-03 15:05:36 -0700822
Richard Genoud3645e322014-02-10 14:25:32 -0800823 if (-e ".git") {
Joe Perches351b2a12013-07-03 15:05:36 -0700824 my $git_last_include_commit = `git log --no-merges --pretty=format:"%h%n" -1 -- include`;
825 chomp $git_last_include_commit;
Joe Perchesc707a812013-07-08 16:00:43 -0700826 $camelcase_cache = ".checkpatch-camelcase.git.$git_last_include_commit";
Joe Perches34456862013-07-03 15:05:34 -0700827 } else {
Joe Perchesc707a812013-07-08 16:00:43 -0700828 my $last_mod_date = 0;
Joe Perches34456862013-07-03 15:05:34 -0700829 $files = `find $root/include -name "*.h"`;
Joe Perchesc707a812013-07-08 16:00:43 -0700830 @include_files = split('\n', $files);
831 foreach my $file (@include_files) {
832 my $date = POSIX::strftime("%Y%m%d%H%M",
833 localtime((stat $file)[9]));
834 $last_mod_date = $date if ($last_mod_date < $date);
835 }
836 $camelcase_cache = ".checkpatch-camelcase.date.$last_mod_date";
Joe Perches34456862013-07-03 15:05:34 -0700837 }
Joe Perchesc707a812013-07-08 16:00:43 -0700838
839 if ($camelcase_cache ne "" && -f $camelcase_cache) {
840 open(my $camelcase_file, '<', "$camelcase_cache")
841 or warn "$P: Can't read '$camelcase_cache' $!\n";
842 while (<$camelcase_file>) {
843 chomp;
844 $camelcase{$_} = 1;
845 }
846 close($camelcase_file);
847
848 return;
849 }
850
Richard Genoud3645e322014-02-10 14:25:32 -0800851 if (-e ".git") {
Joe Perchesc707a812013-07-08 16:00:43 -0700852 $files = `git ls-files "include/*.h"`;
853 @include_files = split('\n', $files);
854 }
855
Joe Perches34456862013-07-03 15:05:34 -0700856 foreach my $file (@include_files) {
857 seed_camelcase_file($file);
858 }
Joe Perches351b2a12013-07-03 15:05:36 -0700859
Joe Perchesc707a812013-07-08 16:00:43 -0700860 if ($camelcase_cache ne "") {
Joe Perches351b2a12013-07-03 15:05:36 -0700861 unlink glob ".checkpatch-camelcase.*";
Joe Perchesc707a812013-07-08 16:00:43 -0700862 open(my $camelcase_file, '>', "$camelcase_cache")
863 or warn "$P: Can't write '$camelcase_cache' $!\n";
Joe Perches351b2a12013-07-03 15:05:36 -0700864 foreach (sort { lc($a) cmp lc($b) } keys(%camelcase)) {
865 print $camelcase_file ("$_\n");
866 }
867 close($camelcase_file);
868 }
Joe Perches34456862013-07-03 15:05:34 -0700869}
870
Joe Perchesd311cd42014-08-06 16:10:57 -0700871sub git_commit_info {
872 my ($commit, $id, $desc) = @_;
873
874 return ($id, $desc) if ((which("git") eq "") || !(-e ".git"));
875
876 my $output = `git log --no-color --format='%H %s' -1 $commit 2>&1`;
877 $output =~ s/^\s*//gm;
878 my @lines = split("\n", $output);
879
Joe Perches0d7835f2015-02-13 14:38:35 -0800880 return ($id, $desc) if ($#lines < 0);
881
Joe Perchesd311cd42014-08-06 16:10:57 -0700882 if ($lines[0] =~ /^error: short SHA1 $commit is ambiguous\./) {
883# Maybe one day convert this block of bash into something that returns
884# all matching commit ids, but it's very slow...
885#
886# echo "checking commits $1..."
887# git rev-list --remotes | grep -i "^$1" |
888# while read line ; do
889# git log --format='%H %s' -1 $line |
890# echo "commit $(cut -c 1-12,41-)"
891# done
892 } elsif ($lines[0] =~ /^fatal: ambiguous argument '$commit': unknown revision or path not in the working tree\./) {
Heinrich Schuchardt948b1332017-07-10 15:52:16 -0700893 $id = undef;
Joe Perchesd311cd42014-08-06 16:10:57 -0700894 } else {
895 $id = substr($lines[0], 0, 12);
896 $desc = substr($lines[0], 41);
897 }
898
899 return ($id, $desc);
900}
901
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700902$chk_signoff = 0 if ($file);
903
Andy Whitcroft00df3442007-06-08 13:47:06 -0700904my @rawlines = ();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800905my @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700906my @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700907my @fixed_inserted = ();
908my @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700909my $fixlinenr = -1;
910
Du, Changbin4a593c32016-05-20 17:04:16 -0700911# If input is git commits, extract all commits from the commit expressions.
912# For example, HEAD-3 means we need check 'HEAD, HEAD~1, HEAD~2'.
913die "$P: No git repository found\n" if ($git && !-e ".git");
914
915if ($git) {
916 my @commits = ();
Joe Perches0dea9f12016-05-20 17:04:19 -0700917 foreach my $commit_expr (@ARGV) {
Du, Changbin4a593c32016-05-20 17:04:16 -0700918 my $git_range;
Joe Perches28898fd2016-05-20 17:04:22 -0700919 if ($commit_expr =~ m/^(.*)-(\d+)$/) {
920 $git_range = "-$2 $1";
Du, Changbin4a593c32016-05-20 17:04:16 -0700921 } elsif ($commit_expr =~ m/\.\./) {
922 $git_range = "$commit_expr";
Du, Changbin4a593c32016-05-20 17:04:16 -0700923 } else {
Joe Perches0dea9f12016-05-20 17:04:19 -0700924 $git_range = "-1 $commit_expr";
925 }
926 my $lines = `git log --no-color --no-merges --pretty=format:'%H %s' $git_range`;
927 foreach my $line (split(/\n/, $lines)) {
Joe Perches28898fd2016-05-20 17:04:22 -0700928 $line =~ /^([0-9a-fA-F]{40,40}) (.*)$/;
929 next if (!defined($1) || !defined($2));
Joe Perches0dea9f12016-05-20 17:04:19 -0700930 my $sha1 = $1;
931 my $subject = $2;
932 unshift(@commits, $sha1);
933 $git_commits{$sha1} = $subject;
Du, Changbin4a593c32016-05-20 17:04:16 -0700934 }
935 }
936 die "$P: no git commits after extraction!\n" if (@commits == 0);
937 @ARGV = @commits;
938}
939
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800940my $vname;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700941for my $filename (@ARGV) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800942 my $FILE;
Du, Changbin4a593c32016-05-20 17:04:16 -0700943 if ($git) {
944 open($FILE, '-|', "git format-patch -M --stdout -1 $filename") ||
945 die "$P: $filename: git format-patch failed - $!\n";
946 } elsif ($file) {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800947 open($FILE, '-|', "diff -u /dev/null $filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700948 die "$P: $filename: diff failed - $!\n";
Andy Whitcroft21caa132009-01-06 14:41:30 -0800949 } elsif ($filename eq '-') {
950 open($FILE, '<&STDIN');
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700951 } else {
Andy Whitcroft21caa132009-01-06 14:41:30 -0800952 open($FILE, '<', "$filename") ||
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700953 die "$P: $filename: open failed - $!\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700954 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800955 if ($filename eq '-') {
956 $vname = 'Your patch';
Du, Changbin4a593c32016-05-20 17:04:16 -0700957 } elsif ($git) {
Joe Perches0dea9f12016-05-20 17:04:19 -0700958 $vname = "Commit " . substr($filename, 0, 12) . ' ("' . $git_commits{$filename} . '")';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800959 } else {
960 $vname = $filename;
961 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800962 while (<$FILE>) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700963 chomp;
964 push(@rawlines, $_);
965 }
Andy Whitcroft21caa132009-01-06 14:41:30 -0800966 close($FILE);
Joe Perchesd8469f12015-06-25 15:03:00 -0700967
968 if ($#ARGV > 0 && $quiet == 0) {
969 print '-' x length($vname) . "\n";
970 print "$vname\n";
971 print '-' x length($vname) . "\n";
972 }
973
Andy Whitcroftc2fdda02008-02-08 04:20:54 -0800974 if (!process($filename)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -0700975 $exit = 1;
976 }
977 @rawlines = ();
Andy Whitcroft13214ad2008-02-08 04:22:03 -0800978 @lines = ();
Joe Perches3705ce52013-07-03 15:05:31 -0700979 @fixed = ();
Joe Perchesd752fcc2014-08-06 16:11:05 -0700980 @fixed_inserted = ();
981 @fixed_deleted = ();
Joe Perches194f66f2014-08-06 16:11:03 -0700982 $fixlinenr = -1;
Alex Dowad485ff232015-06-25 15:02:52 -0700983 @modifierListFile = ();
984 @typeListFile = ();
985 build_types();
Andy Whitcroft0a920b52007-06-01 00:46:48 -0700986}
987
Joe Perchesd8469f12015-06-25 15:03:00 -0700988if (!$quiet) {
Joe Perches3c816e42015-06-25 15:03:29 -0700989 hash_show_words(\%use_type, "Used");
990 hash_show_words(\%ignore_type, "Ignored");
991
Joe Perchesd8469f12015-06-25 15:03:00 -0700992 if ($^V lt 5.10.0) {
993 print << "EOM"
994
995NOTE: perl $^V is not modern enough to detect all possible issues.
996 An upgrade to at least perl v5.10.0 is suggested.
997EOM
998 }
999 if ($exit) {
1000 print << "EOM"
1001
1002NOTE: If any of the errors are false positives, please report
1003 them to the maintainer, see CHECKPATCH in MAINTAINERS.
1004EOM
1005 }
1006}
1007
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001008exit($exit);
1009
1010sub top_of_kernel_tree {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001011 my ($root) = @_;
1012
1013 my @tree_check = (
1014 "COPYING", "CREDITS", "Kbuild", "MAINTAINERS", "Makefile",
1015 "README", "Documentation", "arch", "include", "drivers",
1016 "fs", "init", "ipc", "kernel", "lib", "scripts",
1017 );
1018
1019 foreach my $check (@tree_check) {
1020 if (! -e $root . '/' . $check) {
1021 return 0;
1022 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001023 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001024 return 1;
Joe Perches8f26b832012-10-04 17:13:32 -07001025}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001026
Joe Perches20112472011-07-25 17:13:23 -07001027sub parse_email {
1028 my ($formatted_email) = @_;
1029
1030 my $name = "";
1031 my $address = "";
1032 my $comment = "";
1033
1034 if ($formatted_email =~ /^(.*)<(\S+\@\S+)>(.*)$/) {
1035 $name = $1;
1036 $address = $2;
1037 $comment = $3 if defined $3;
1038 } elsif ($formatted_email =~ /^\s*<(\S+\@\S+)>(.*)$/) {
1039 $address = $1;
1040 $comment = $2 if defined $2;
1041 } elsif ($formatted_email =~ /(\S+\@\S+)(.*)$/) {
1042 $address = $1;
1043 $comment = $2 if defined $2;
1044 $formatted_email =~ s/$address.*$//;
1045 $name = $formatted_email;
Joe Perches3705ce52013-07-03 15:05:31 -07001046 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001047 $name =~ s/^\"|\"$//g;
1048 # If there's a name left after stripping spaces and
1049 # leading quotes, and the address doesn't have both
1050 # leading and trailing angle brackets, the address
1051 # is invalid. ie:
1052 # "joe smith joe@smith.com" bad
1053 # "joe smith <joe@smith.com" bad
1054 if ($name ne "" && $address !~ /^<[^>]+>$/) {
1055 $name = "";
1056 $address = "";
1057 $comment = "";
1058 }
1059 }
1060
Joe Perches3705ce52013-07-03 15:05:31 -07001061 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001062 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001063 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001064 $address =~ s/^\<|\>$//g;
1065
1066 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1067 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1068 $name = "\"$name\"";
1069 }
1070
1071 return ($name, $address, $comment);
1072}
1073
1074sub format_email {
1075 my ($name, $address) = @_;
1076
1077 my $formatted_email;
1078
Joe Perches3705ce52013-07-03 15:05:31 -07001079 $name = trim($name);
Joe Perches20112472011-07-25 17:13:23 -07001080 $name =~ s/^\"|\"$//g;
Joe Perches3705ce52013-07-03 15:05:31 -07001081 $address = trim($address);
Joe Perches20112472011-07-25 17:13:23 -07001082
1083 if ($name =~ /[^\w \-]/i) { ##has "must quote" chars
1084 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
1085 $name = "\"$name\"";
1086 }
1087
1088 if ("$name" eq "") {
1089 $formatted_email = "$address";
1090 } else {
1091 $formatted_email = "$name <$address>";
1092 }
1093
1094 return $formatted_email;
1095}
1096
Joe Perchesd311cd42014-08-06 16:10:57 -07001097sub which {
Joe Perchesbd474ca2014-08-06 16:11:10 -07001098 my ($bin) = @_;
Joe Perchesd311cd42014-08-06 16:10:57 -07001099
Joe Perchesbd474ca2014-08-06 16:11:10 -07001100 foreach my $path (split(/:/, $ENV{PATH})) {
1101 if (-e "$path/$bin") {
1102 return "$path/$bin";
1103 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001104 }
Joe Perchesd311cd42014-08-06 16:10:57 -07001105
Joe Perchesbd474ca2014-08-06 16:11:10 -07001106 return "";
Joe Perchesd311cd42014-08-06 16:10:57 -07001107}
1108
Joe Perches000d1cc12011-07-25 17:13:25 -07001109sub which_conf {
1110 my ($conf) = @_;
1111
1112 foreach my $path (split(/:/, ".:$ENV{HOME}:.scripts")) {
1113 if (-e "$path/$conf") {
1114 return "$path/$conf";
1115 }
1116 }
1117
1118 return "";
1119}
1120
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001121sub expand_tabs {
1122 my ($str) = @_;
1123
1124 my $res = '';
1125 my $n = 0;
1126 for my $c (split(//, $str)) {
1127 if ($c eq "\t") {
1128 $res .= ' ';
1129 $n++;
1130 for (; ($n % 8) != 0; $n++) {
1131 $res .= ' ';
1132 }
1133 next;
1134 }
1135 $res .= $c;
1136 $n++;
1137 }
1138
1139 return $res;
1140}
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001141sub copy_spacing {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001142 (my $res = shift) =~ tr/\t/ /c;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001143 return $res;
1144}
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001145
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001146sub line_stats {
1147 my ($line) = @_;
1148
1149 # Drop the diff line leader and expand tabs
1150 $line =~ s/^.//;
1151 $line = expand_tabs($line);
1152
1153 # Pick the indent from the front of the line.
1154 my ($white) = ($line =~ /^(\s*)/);
1155
1156 return (length($line), length($white));
1157}
1158
Andy Whitcroft773647a2008-03-28 14:15:58 -07001159my $sanitise_quote = '';
1160
1161sub sanitise_line_reset {
1162 my ($in_comment) = @_;
1163
1164 if ($in_comment) {
1165 $sanitise_quote = '*/';
1166 } else {
1167 $sanitise_quote = '';
1168 }
1169}
Andy Whitcroft00df3442007-06-08 13:47:06 -07001170sub sanitise_line {
1171 my ($line) = @_;
1172
1173 my $res = '';
1174 my $l = '';
1175
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001176 my $qlen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001177 my $off = 0;
1178 my $c;
Andy Whitcroft00df3442007-06-08 13:47:06 -07001179
Andy Whitcroft773647a2008-03-28 14:15:58 -07001180 # Always copy over the diff marker.
1181 $res = substr($line, 0, 1);
1182
1183 for ($off = 1; $off < length($line); $off++) {
1184 $c = substr($line, $off, 1);
1185
1186 # Comments we are wacking completly including the begin
1187 # and end, all to $;.
1188 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '/*') {
1189 $sanitise_quote = '*/';
1190
1191 substr($res, $off, 2, "$;$;");
1192 $off++;
1193 next;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001194 }
Andy Whitcroft81bc0e02008-10-15 22:02:26 -07001195 if ($sanitise_quote eq '*/' && substr($line, $off, 2) eq '*/') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001196 $sanitise_quote = '';
1197 substr($res, $off, 2, "$;$;");
1198 $off++;
1199 next;
1200 }
Daniel Walker113f04a2009-09-21 17:04:35 -07001201 if ($sanitise_quote eq '' && substr($line, $off, 2) eq '//') {
1202 $sanitise_quote = '//';
1203
1204 substr($res, $off, 2, $sanitise_quote);
1205 $off++;
1206 next;
1207 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001208
1209 # A \ in a string means ignore the next character.
1210 if (($sanitise_quote eq "'" || $sanitise_quote eq '"') &&
1211 $c eq "\\") {
1212 substr($res, $off, 2, 'XX');
1213 $off++;
1214 next;
1215 }
1216 # Regular quotes.
1217 if ($c eq "'" || $c eq '"') {
1218 if ($sanitise_quote eq '') {
1219 $sanitise_quote = $c;
1220
1221 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001222 next;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001223 } elsif ($sanitise_quote eq $c) {
1224 $sanitise_quote = '';
Andy Whitcroft00df3442007-06-08 13:47:06 -07001225 }
1226 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07001227
Andy Whitcroftfae17da2009-01-06 14:41:20 -08001228 #print "c<$c> SQ<$sanitise_quote>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001229 if ($off != 0 && $sanitise_quote eq '*/' && $c ne "\t") {
1230 substr($res, $off, 1, $;);
Daniel Walker113f04a2009-09-21 17:04:35 -07001231 } elsif ($off != 0 && $sanitise_quote eq '//' && $c ne "\t") {
1232 substr($res, $off, 1, $;);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001233 } elsif ($off != 0 && $sanitise_quote && $c ne "\t") {
1234 substr($res, $off, 1, 'X');
Andy Whitcroft00df3442007-06-08 13:47:06 -07001235 } else {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001236 substr($res, $off, 1, $c);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001237 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001238 }
1239
Daniel Walker113f04a2009-09-21 17:04:35 -07001240 if ($sanitise_quote eq '//') {
1241 $sanitise_quote = '';
1242 }
1243
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001244 # The pathname on a #include may be surrounded by '<' and '>'.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001245 if ($res =~ /^.\s*\#\s*include\s+\<(.*)\>/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001246 my $clean = 'X' x length($1);
1247 $res =~ s@\<.*\>@<$clean>@;
1248
1249 # The whole of a #error is a string.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001250 } elsif ($res =~ /^.\s*\#\s*(?:error|warning)\s+(.*)\b/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001251 my $clean = 'X' x length($1);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001252 $res =~ s@(\#\s*(?:error|warning)\s+).*@$1$clean@;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001253 }
1254
Joe Perchesdadf6802016-08-02 14:04:33 -07001255 if ($allow_c99_comments && $res =~ m@(//.*$)@) {
1256 my $match = $1;
1257 $res =~ s/\Q$match\E/"$;" x length($match)/e;
1258 }
1259
Andy Whitcroft00df3442007-06-08 13:47:06 -07001260 return $res;
1261}
1262
Joe Perchesa6962d72013-04-29 16:18:13 -07001263sub get_quoted_string {
1264 my ($line, $rawline) = @_;
1265
Joe Perches33acb542015-06-25 15:02:54 -07001266 return "" if ($line !~ m/($String)/g);
Joe Perchesa6962d72013-04-29 16:18:13 -07001267 return substr($rawline, $-[0], $+[0] - $-[0]);
1268}
1269
Andy Whitcroft8905a672007-11-28 16:21:06 -08001270sub ctx_statement_block {
1271 my ($linenr, $remain, $off) = @_;
1272 my $line = $linenr - 1;
1273 my $blk = '';
1274 my $soff = $off;
1275 my $coff = $off - 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001276 my $coff_set = 0;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001277
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001278 my $loff = 0;
1279
Andy Whitcroft8905a672007-11-28 16:21:06 -08001280 my $type = '';
1281 my $level = 0;
Andy Whitcrofta2750642009-01-15 13:51:04 -08001282 my @stack = ();
Andy Whitcroftcf655042008-03-04 14:28:20 -08001283 my $p;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001284 my $c;
1285 my $len = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001286
1287 my $remainder;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001288 while (1) {
Andy Whitcrofta2750642009-01-15 13:51:04 -08001289 @stack = (['', 0]) if ($#stack == -1);
1290
Andy Whitcroft773647a2008-03-28 14:15:58 -07001291 #warn "CSB: blk<$blk> remain<$remain>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001292 # If we are about to drop off the end, pull in more
1293 # context.
1294 if ($off >= $len) {
1295 for (; $remain > 0; $line++) {
Andy Whitcroftdea33492008-10-15 22:02:25 -07001296 last if (!defined $lines[$line]);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001297 next if ($lines[$line] =~ /^-/);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001298 $remain--;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001299 $loff = $len;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001300 $blk .= $lines[$line] . "\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001301 $len = length($blk);
1302 $line++;
1303 last;
1304 }
1305 # Bail if there is no further context.
1306 #warn "CSB: blk<$blk> off<$off> len<$len>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001307 if ($off >= $len) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08001308 last;
1309 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001310 if ($level == 0 && substr($blk, $off) =~ /^.\s*#\s*define/) {
1311 $level++;
1312 $type = '#';
1313 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001314 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08001315 $p = $c;
Andy Whitcroft8905a672007-11-28 16:21:06 -08001316 $c = substr($blk, $off, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001317 $remainder = substr($blk, $off);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001318
Andy Whitcroft773647a2008-03-28 14:15:58 -07001319 #warn "CSB: c<$c> type<$type> level<$level> remainder<$remainder> coff_set<$coff_set>\n";
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001320
1321 # Handle nested #if/#else.
1322 if ($remainder =~ /^#\s*(?:ifndef|ifdef|if)\s/) {
1323 push(@stack, [ $type, $level ]);
1324 } elsif ($remainder =~ /^#\s*(?:else|elif)\b/) {
1325 ($type, $level) = @{$stack[$#stack - 1]};
1326 } elsif ($remainder =~ /^#\s*endif\b/) {
1327 ($type, $level) = @{pop(@stack)};
1328 }
1329
Andy Whitcroft8905a672007-11-28 16:21:06 -08001330 # Statement ends at the ';' or a close '}' at the
1331 # outermost level.
1332 if ($level == 0 && $c eq ';') {
1333 last;
1334 }
1335
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001336 # An else is really a conditional as long as its not else if
Andy Whitcroft773647a2008-03-28 14:15:58 -07001337 if ($level == 0 && $coff_set == 0 &&
1338 (!defined($p) || $p =~ /(?:\s|\}|\+)/) &&
1339 $remainder =~ /^(else)(?:\s|{)/ &&
1340 $remainder !~ /^else\s+if\b/) {
1341 $coff = $off + length($1) - 1;
1342 $coff_set = 1;
1343 #warn "CSB: mark coff<$coff> soff<$soff> 1<$1>\n";
1344 #warn "[" . substr($blk, $soff, $coff - $soff + 1) . "]\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001345 }
1346
Andy Whitcroft8905a672007-11-28 16:21:06 -08001347 if (($type eq '' || $type eq '(') && $c eq '(') {
1348 $level++;
1349 $type = '(';
1350 }
1351 if ($type eq '(' && $c eq ')') {
1352 $level--;
1353 $type = ($level != 0)? '(' : '';
1354
1355 if ($level == 0 && $coff < $soff) {
1356 $coff = $off;
Andy Whitcroft773647a2008-03-28 14:15:58 -07001357 $coff_set = 1;
1358 #warn "CSB: mark coff<$coff>\n";
Andy Whitcroft8905a672007-11-28 16:21:06 -08001359 }
1360 }
1361 if (($type eq '' || $type eq '{') && $c eq '{') {
1362 $level++;
1363 $type = '{';
1364 }
1365 if ($type eq '{' && $c eq '}') {
1366 $level--;
1367 $type = ($level != 0)? '{' : '';
1368
1369 if ($level == 0) {
Patrick Pannutob998e002010-08-09 17:21:03 -07001370 if (substr($blk, $off + 1, 1) eq ';') {
1371 $off++;
1372 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001373 last;
1374 }
1375 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08001376 # Preprocessor commands end at the newline unless escaped.
1377 if ($type eq '#' && $c eq "\n" && $p ne "\\") {
1378 $level--;
1379 $type = '';
1380 $off++;
1381 last;
1382 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001383 $off++;
1384 }
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001385 # We are truly at the end, so shuffle to the next line.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001386 if ($off == $len) {
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07001387 $loff = $len + 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001388 $line++;
1389 $remain--;
1390 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001391
1392 my $statement = substr($blk, $soff, $off - $soff + 1);
1393 my $condition = substr($blk, $soff, $coff - $soff + 1);
1394
1395 #warn "STATEMENT<$statement>\n";
1396 #warn "CONDITION<$condition>\n";
1397
Andy Whitcroft773647a2008-03-28 14:15:58 -07001398 #print "coff<$coff> soff<$off> loff<$loff>\n";
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001399
1400 return ($statement, $condition,
1401 $line, $remain + 1, $off - $loff + 1, $level);
1402}
1403
Andy Whitcroftcf655042008-03-04 14:28:20 -08001404sub statement_lines {
1405 my ($stmt) = @_;
1406
1407 # Strip the diff line prefixes and rip blank lines at start and end.
1408 $stmt =~ s/(^|\n)./$1/g;
1409 $stmt =~ s/^\s*//;
1410 $stmt =~ s/\s*$//;
1411
1412 my @stmt_lines = ($stmt =~ /\n/g);
1413
1414 return $#stmt_lines + 2;
1415}
1416
1417sub statement_rawlines {
1418 my ($stmt) = @_;
1419
1420 my @stmt_lines = ($stmt =~ /\n/g);
1421
1422 return $#stmt_lines + 2;
1423}
1424
1425sub statement_block_size {
1426 my ($stmt) = @_;
1427
1428 $stmt =~ s/(^|\n)./$1/g;
1429 $stmt =~ s/^\s*{//;
1430 $stmt =~ s/}\s*$//;
1431 $stmt =~ s/^\s*//;
1432 $stmt =~ s/\s*$//;
1433
1434 my @stmt_lines = ($stmt =~ /\n/g);
1435 my @stmt_statements = ($stmt =~ /;/g);
1436
1437 my $stmt_lines = $#stmt_lines + 2;
1438 my $stmt_statements = $#stmt_statements + 1;
1439
1440 if ($stmt_lines > $stmt_statements) {
1441 return $stmt_lines;
1442 } else {
1443 return $stmt_statements;
1444 }
1445}
1446
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001447sub ctx_statement_full {
1448 my ($linenr, $remain, $off) = @_;
1449 my ($statement, $condition, $level);
1450
1451 my (@chunks);
1452
Andy Whitcroftcf655042008-03-04 14:28:20 -08001453 # Grab the first conditional/block pair.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001454 ($statement, $condition, $linenr, $remain, $off, $level) =
1455 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001456 #print "F: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08001457 push(@chunks, [ $condition, $statement ]);
1458 if (!($remain > 0 && $condition =~ /^\s*(?:\n[+-])?\s*(?:if|else|do)\b/s)) {
1459 return ($level, $linenr, @chunks);
1460 }
1461
1462 # Pull in the following conditional/block pairs and see if they
1463 # could continue the statement.
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001464 for (;;) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001465 ($statement, $condition, $linenr, $remain, $off, $level) =
1466 ctx_statement_block($linenr, $remain, $off);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001467 #print "C: c<$condition> s<$statement> remain<$remain>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07001468 last if (!($remain > 0 && $condition =~ /^(?:\s*\n[+-])*\s*(?:else|do)\b/s));
Andy Whitcroftcf655042008-03-04 14:28:20 -08001469 #print "C: push\n";
1470 push(@chunks, [ $condition, $statement ]);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001471 }
1472
1473 return ($level, $linenr, @chunks);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001474}
1475
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001476sub ctx_block_get {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001477 my ($linenr, $remain, $outer, $open, $close, $off) = @_;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001478 my $line;
1479 my $start = $linenr - 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001480 my $blk = '';
1481 my @o;
1482 my @c;
1483 my @res = ();
1484
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001485 my $level = 0;
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001486 my @stack = ($level);
Andy Whitcroft00df3442007-06-08 13:47:06 -07001487 for ($line = $start; $remain > 0; $line++) {
1488 next if ($rawlines[$line] =~ /^-/);
1489 $remain--;
1490
1491 $blk .= $rawlines[$line];
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001492
1493 # Handle nested #if/#else.
Andy Whitcroft01464f32010-10-26 14:23:19 -07001494 if ($lines[$line] =~ /^.\s*#\s*(?:ifndef|ifdef|if)\s/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001495 push(@stack, $level);
Andy Whitcroft01464f32010-10-26 14:23:19 -07001496 } elsif ($lines[$line] =~ /^.\s*#\s*(?:else|elif)\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001497 $level = $stack[$#stack - 1];
Andy Whitcroft01464f32010-10-26 14:23:19 -07001498 } elsif ($lines[$line] =~ /^.\s*#\s*endif\b/) {
Andy Whitcroft4635f4f2009-01-06 14:41:27 -08001499 $level = pop(@stack);
1500 }
1501
Andy Whitcroft01464f32010-10-26 14:23:19 -07001502 foreach my $c (split(//, $lines[$line])) {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001503 ##print "C<$c>L<$level><$open$close>O<$off>\n";
1504 if ($off > 0) {
1505 $off--;
1506 next;
1507 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001508
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001509 if ($c eq $close && $level > 0) {
1510 $level--;
1511 last if ($level == 0);
1512 } elsif ($c eq $open) {
1513 $level++;
1514 }
1515 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001516
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001517 if (!$outer || $level <= 1) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001518 push(@res, $rawlines[$line]);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001519 }
1520
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001521 last if ($level == 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001522 }
1523
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001524 return ($level, @res);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001525}
1526sub ctx_block_outer {
1527 my ($linenr, $remain) = @_;
1528
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001529 my ($level, @r) = ctx_block_get($linenr, $remain, 1, '{', '}', 0);
1530 return @r;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001531}
1532sub ctx_block {
1533 my ($linenr, $remain) = @_;
1534
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001535 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '{', '}', 0);
1536 return @r;
Andy Whitcroft653d4872007-06-23 17:16:34 -07001537}
1538sub ctx_statement {
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001539 my ($linenr, $remain, $off) = @_;
1540
1541 my ($level, @r) = ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1542 return @r;
1543}
1544sub ctx_block_level {
Andy Whitcroft653d4872007-06-23 17:16:34 -07001545 my ($linenr, $remain) = @_;
1546
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001547 return ctx_block_get($linenr, $remain, 0, '{', '}', 0);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001548}
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001549sub ctx_statement_level {
1550 my ($linenr, $remain, $off) = @_;
1551
1552 return ctx_block_get($linenr, $remain, 0, '(', ')', $off);
1553}
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001554
1555sub ctx_locate_comment {
1556 my ($first_line, $end_line) = @_;
1557
1558 # Catch a comment on the end of the line itself.
Andy Whitcroftbeae6332008-07-23 21:28:59 -07001559 my ($current_comment) = ($rawlines[$end_line - 1] =~ m@.*(/\*.*\*/)\s*(?:\\\s*)?$@);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001560 return $current_comment if (defined $current_comment);
1561
1562 # Look through the context and try and figure out if there is a
1563 # comment.
1564 my $in_comment = 0;
1565 $current_comment = '';
1566 for (my $linenr = $first_line; $linenr < $end_line; $linenr++) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07001567 my $line = $rawlines[$linenr - 1];
1568 #warn " $line\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001569 if ($linenr == $first_line and $line =~ m@^.\s*\*@) {
1570 $in_comment = 1;
1571 }
1572 if ($line =~ m@/\*@) {
1573 $in_comment = 1;
1574 }
1575 if (!$in_comment && $current_comment ne '') {
1576 $current_comment = '';
1577 }
1578 $current_comment .= $line . "\n" if ($in_comment);
1579 if ($line =~ m@\*/@) {
1580 $in_comment = 0;
1581 }
1582 }
1583
1584 chomp($current_comment);
1585 return($current_comment);
1586}
1587sub ctx_has_comment {
1588 my ($first_line, $end_line) = @_;
1589 my $cmt = ctx_locate_comment($first_line, $end_line);
1590
Andy Whitcroft00df3442007-06-08 13:47:06 -07001591 ##print "LINE: $rawlines[$end_line - 1 ]\n";
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07001592 ##print "CMMT: $cmt\n";
1593
1594 return ($cmt ne '');
1595}
1596
Andy Whitcroft4d001e42008-10-15 22:02:21 -07001597sub raw_line {
1598 my ($linenr, $cnt) = @_;
1599
1600 my $offset = $linenr - 1;
1601 $cnt++;
1602
1603 my $line;
1604 while ($cnt) {
1605 $line = $rawlines[$offset++];
1606 next if (defined($line) && $line =~ /^-/);
1607 $cnt--;
1608 }
1609
1610 return $line;
1611}
1612
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001613sub cat_vet {
1614 my ($vet) = @_;
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001615 my ($res, $coded);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001616
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001617 $res = '';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001618 while ($vet =~ /([^[:cntrl:]]*)([[:cntrl:]]|$)/g) {
1619 $res .= $1;
1620 if ($2 ne '') {
1621 $coded = sprintf("^%c", unpack('C', $2) + 64);
1622 $res .= $coded;
1623 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001624 }
1625 $res =~ s/$/\$/;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001626
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07001627 return $res;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07001628}
1629
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001630my $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001631my $av_pending;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001632my @av_paren_type;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001633my $av_pend_colon;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001634
1635sub annotate_reset {
1636 $av_preprocessor = 0;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001637 $av_pending = '_';
1638 @av_paren_type = ('E');
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001639 $av_pend_colon = 'O';
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001640}
1641
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001642sub annotate_values {
1643 my ($stream, $type) = @_;
1644
1645 my $res;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001646 my $var = '_' x length($stream);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001647 my $cur = $stream;
1648
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001649 print "$stream\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001650
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001651 while (length($cur)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001652 @av_paren_type = ('E') if ($#av_paren_type < 0);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001653 print " <" . join('', @av_paren_type) .
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001654 "> <$type> <$av_pending>" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001655 if ($cur =~ /^(\s+)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001656 print "WS($1)\n" if ($dbg_values > 1);
1657 if ($1 =~ /\n/ && $av_preprocessor) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001658 $type = pop(@av_paren_type);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001659 $av_preprocessor = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001660 }
1661
Florian Micklerc023e4732011-01-12 16:59:58 -08001662 } elsif ($cur =~ /^(\(\s*$Type\s*)\)/ && $av_pending eq '_') {
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001663 print "CAST($1)\n" if ($dbg_values > 1);
1664 push(@av_paren_type, $type);
Andy Whitcroftaddcdce2012-01-10 15:10:11 -08001665 $type = 'c';
Andy Whitcroft9446ef52010-10-26 14:23:13 -07001666
Andy Whitcrofte91b6e22010-10-26 14:23:11 -07001667 } elsif ($cur =~ /^($Type)\s*(?:$Ident|,|\)|\(|\s*$)/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001668 print "DECLARE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001669 $type = 'T';
1670
Andy Whitcroft389a2fe2008-07-23 21:29:05 -07001671 } elsif ($cur =~ /^($Modifier)\s*/) {
1672 print "MODIFIER($1)\n" if ($dbg_values > 1);
1673 $type = 'T';
1674
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001675 } elsif ($cur =~ /^(\#\s*define\s*$Ident)(\(?)/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001676 print "DEFINE($1,$2)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001677 $av_preprocessor = 1;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001678 push(@av_paren_type, $type);
1679 if ($2 ne '') {
1680 $av_pending = 'N';
1681 }
1682 $type = 'E';
1683
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001684 } elsif ($cur =~ /^(\#\s*(?:undef\s*$Ident|include\b))/o) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001685 print "UNDEF($1)\n" if ($dbg_values > 1);
1686 $av_preprocessor = 1;
1687 push(@av_paren_type, $type);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001688
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001689 } elsif ($cur =~ /^(\#\s*(?:ifdef|ifndef|if))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001690 print "PRE_START($1)\n" if ($dbg_values > 1);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001691 $av_preprocessor = 1;
Andy Whitcroftcf655042008-03-04 14:28:20 -08001692
1693 push(@av_paren_type, $type);
1694 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001695 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001696
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001697 } elsif ($cur =~ /^(\#\s*(?:else|elif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001698 print "PRE_RESTART($1)\n" if ($dbg_values > 1);
1699 $av_preprocessor = 1;
1700
1701 push(@av_paren_type, $av_paren_type[$#av_paren_type]);
1702
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001703 $type = 'E';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001704
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001705 } elsif ($cur =~ /^(\#\s*(?:endif))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001706 print "PRE_END($1)\n" if ($dbg_values > 1);
1707
1708 $av_preprocessor = 1;
1709
1710 # Assume all arms of the conditional end as this
1711 # one does, and continue as if the #endif was not here.
1712 pop(@av_paren_type);
1713 push(@av_paren_type, $type);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001714 $type = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001715
1716 } elsif ($cur =~ /^(\\\n)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001717 print "PRECONT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001718
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07001719 } elsif ($cur =~ /^(__attribute__)\s*\(?/o) {
1720 print "ATTR($1)\n" if ($dbg_values > 1);
1721 $av_pending = $type;
1722 $type = 'N';
1723
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001724 } elsif ($cur =~ /^(sizeof)\s*(\()?/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001725 print "SIZEOF($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001726 if (defined $2) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001727 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001728 }
1729 $type = 'N';
1730
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001731 } elsif ($cur =~ /^(if|while|for)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001732 print "COND($1)\n" if ($dbg_values > 1);
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001733 $av_pending = 'E';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001734 $type = 'N';
1735
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001736 } elsif ($cur =~/^(case)/o) {
1737 print "CASE($1)\n" if ($dbg_values > 1);
1738 $av_pend_colon = 'C';
1739 $type = 'N';
1740
Andy Whitcroft14b111c2008-10-15 22:02:16 -07001741 } elsif ($cur =~/^(return|else|goto|typeof|__typeof__)\b/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001742 print "KEYWORD($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001743 $type = 'N';
1744
1745 } elsif ($cur =~ /^(\()/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001746 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroftcf655042008-03-04 14:28:20 -08001747 push(@av_paren_type, $av_pending);
1748 $av_pending = '_';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001749 $type = 'N';
1750
1751 } elsif ($cur =~ /^(\))/o) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08001752 my $new_type = pop(@av_paren_type);
1753 if ($new_type ne '_') {
1754 $type = $new_type;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001755 print "PAREN('$1') -> $type\n"
1756 if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001757 } else {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001758 print "PAREN('$1')\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001759 }
1760
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001761 } elsif ($cur =~ /^($Ident)\s*\(/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001762 print "FUNC($1)\n" if ($dbg_values > 1);
Andy Whitcroftc8cb2ca2008-07-23 21:28:57 -07001763 $type = 'V';
Andy Whitcroftcf655042008-03-04 14:28:20 -08001764 $av_pending = 'V';
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001765
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001766 } elsif ($cur =~ /^($Ident\s*):(?:\s*\d+\s*(,|=|;))?/) {
1767 if (defined $2 && $type eq 'C' || $type eq 'T') {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001768 $av_pend_colon = 'B';
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001769 } elsif ($type eq 'E') {
1770 $av_pend_colon = 'L';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001771 }
1772 print "IDENT_COLON($1,$type>$av_pend_colon)\n" if ($dbg_values > 1);
1773 $type = 'V';
1774
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001775 } elsif ($cur =~ /^($Ident|$Constant)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001776 print "IDENT($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001777 $type = 'V';
1778
1779 } elsif ($cur =~ /^($Assignment)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001780 print "ASSIGN($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001781 $type = 'N';
1782
Andy Whitcroftcf655042008-03-04 14:28:20 -08001783 } elsif ($cur =~/^(;|{|})/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001784 print "END($1)\n" if ($dbg_values > 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001785 $type = 'E';
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001786 $av_pend_colon = 'O';
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001787
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001788 } elsif ($cur =~/^(,)/) {
1789 print "COMMA($1)\n" if ($dbg_values > 1);
1790 $type = 'C';
1791
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001792 } elsif ($cur =~ /^(\?)/o) {
1793 print "QUESTION($1)\n" if ($dbg_values > 1);
1794 $type = 'N';
1795
1796 } elsif ($cur =~ /^(:)/o) {
1797 print "COLON($1,$av_pend_colon)\n" if ($dbg_values > 1);
1798
1799 substr($var, length($res), 1, $av_pend_colon);
1800 if ($av_pend_colon eq 'C' || $av_pend_colon eq 'L') {
1801 $type = 'E';
1802 } else {
1803 $type = 'N';
1804 }
1805 $av_pend_colon = 'O';
1806
Andy Whitcroft8e761b02009-01-06 14:41:19 -08001807 } elsif ($cur =~ /^(\[)/o) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001808 print "CLOSE($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001809 $type = 'N';
1810
Andy Whitcroft0d413862008-10-15 22:02:16 -07001811 } elsif ($cur =~ /^(-(?![->])|\+(?!\+)|\*|\&\&|\&)/o) {
Andy Whitcroft74048ed2008-07-23 21:29:10 -07001812 my $variant;
1813
1814 print "OPV($1)\n" if ($dbg_values > 1);
1815 if ($type eq 'V') {
1816 $variant = 'B';
1817 } else {
1818 $variant = 'U';
1819 }
1820
1821 substr($var, length($res), 1, $variant);
1822 $type = 'N';
1823
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001824 } elsif ($cur =~ /^($Operators)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001825 print "OP($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001826 if ($1 ne '++' && $1 ne '--') {
1827 $type = 'N';
1828 }
1829
1830 } elsif ($cur =~ /(^.)/o) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08001831 print "C($1)\n" if ($dbg_values > 1);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001832 }
1833 if (defined $1) {
1834 $cur = substr($cur, length($1));
1835 $res .= $type x length($1);
1836 }
1837 }
1838
Andy Whitcroft1f65f942008-07-23 21:29:10 -07001839 return ($res, $var);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001840}
1841
Andy Whitcroft8905a672007-11-28 16:21:06 -08001842sub possible {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001843 my ($possible, $line) = @_;
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001844 my $notPermitted = qr{(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001845 ^(?:
1846 $Modifier|
1847 $Storage|
1848 $Type|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001849 DEFINE_\S+
1850 )$|
1851 ^(?:
Andy Whitcroft0776e592008-10-15 22:02:29 -07001852 goto|
1853 return|
1854 case|
1855 else|
1856 asm|__asm__|
Andy Whitcroft89a88352012-01-10 15:10:00 -08001857 do|
1858 \#|
1859 \#\#|
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001860 )(?:\s|$)|
Andy Whitcroft0776e592008-10-15 22:02:29 -07001861 ^(?:typedef|struct|enum)\b
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001862 )}x;
1863 warn "CHECK<$possible> ($line)\n" if ($dbg_possible > 2);
1864 if ($possible !~ $notPermitted) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001865 # Check for modifiers.
1866 $possible =~ s/\s*$Storage\s*//g;
1867 $possible =~ s/\s*$Sparse\s*//g;
1868 if ($possible =~ /^\s*$/) {
1869
1870 } elsif ($possible =~ /\s/) {
1871 $possible =~ s/\s*$Type\s*//g;
Andy Whitcroftd2506582008-07-23 21:29:09 -07001872 for my $modifier (split(' ', $possible)) {
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001873 if ($modifier !~ $notPermitted) {
1874 warn "MODIFIER: $modifier ($possible) ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001875 push(@modifierListFile, $modifier);
Andy Whitcroft9a974fd2009-10-26 16:50:12 -07001876 }
Andy Whitcroftd2506582008-07-23 21:29:09 -07001877 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001878
1879 } else {
1880 warn "POSSIBLE: $possible ($line)\n" if ($dbg_possible);
Alex Dowad485ff232015-06-25 15:02:52 -07001881 push(@typeListFile, $possible);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07001882 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08001883 build_types();
Andy Whitcroft0776e592008-10-15 22:02:29 -07001884 } else {
1885 warn "NOTPOSS: $possible ($line)\n" if ($dbg_possible > 1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001886 }
1887}
1888
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07001889my $prefix = '';
1890
Joe Perches000d1cc12011-07-25 17:13:25 -07001891sub show_type {
Joe Perchescbec18a2014-04-03 14:49:19 -07001892 my ($type) = @_;
Joe Perches91bfe482013-09-11 14:23:59 -07001893
Alexey Dobriyan522b8372017-02-27 14:30:05 -08001894 $type =~ tr/[a-z]/[A-Z]/;
1895
Joe Perchescbec18a2014-04-03 14:49:19 -07001896 return defined $use_type{$type} if (scalar keys %use_type > 0);
1897
1898 return !defined $ignore_type{$type};
Joe Perches000d1cc12011-07-25 17:13:25 -07001899}
1900
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001901sub report {
Joe Perchescbec18a2014-04-03 14:49:19 -07001902 my ($level, $type, $msg) = @_;
1903
1904 if (!show_type($type) ||
1905 (defined $tst_only && $msg !~ /\Q$tst_only\E/)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07001906 return 0;
1907 }
Joe Perches57230292015-06-25 15:03:03 -07001908 my $output = '';
John Brooks737c0762017-07-10 15:52:24 -07001909 if ($color) {
Joe Perches57230292015-06-25 15:03:03 -07001910 if ($level eq 'ERROR') {
1911 $output .= RED;
1912 } elsif ($level eq 'WARNING') {
1913 $output .= YELLOW;
1914 } else {
1915 $output .= GREEN;
1916 }
Joe Perches000d1cc12011-07-25 17:13:25 -07001917 }
Joe Perches57230292015-06-25 15:03:03 -07001918 $output .= $prefix . $level . ':';
1919 if ($show_types) {
John Brooks737c0762017-07-10 15:52:24 -07001920 $output .= BLUE if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001921 $output .= "$type:";
1922 }
John Brooks737c0762017-07-10 15:52:24 -07001923 $output .= RESET if ($color);
Joe Perches57230292015-06-25 15:03:03 -07001924 $output .= ' ' . $msg . "\n";
Joe Perches34d88152015-06-25 15:03:05 -07001925
1926 if ($showfile) {
1927 my @lines = split("\n", $output, -1);
1928 splice(@lines, 1, 1);
1929 $output = join("\n", @lines);
1930 }
Joe Perches57230292015-06-25 15:03:03 -07001931 $output = (split('\n', $output))[0] . "\n" if ($terse);
Andy Whitcroft8905a672007-11-28 16:21:06 -08001932
Joe Perches57230292015-06-25 15:03:03 -07001933 push(our @report, $output);
Andy Whitcroft773647a2008-03-28 14:15:58 -07001934
1935 return 1;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001936}
Joe Perchescbec18a2014-04-03 14:49:19 -07001937
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001938sub report_dump {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08001939 our @report;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07001940}
Joe Perches000d1cc12011-07-25 17:13:25 -07001941
Joe Perchesd752fcc2014-08-06 16:11:05 -07001942sub fixup_current_range {
1943 my ($lineRef, $offset, $length) = @_;
1944
1945 if ($$lineRef =~ /^\@\@ -\d+,\d+ \+(\d+),(\d+) \@\@/) {
1946 my $o = $1;
1947 my $l = $2;
1948 my $no = $o + $offset;
1949 my $nl = $l + $length;
1950 $$lineRef =~ s/\+$o,$l \@\@/\+$no,$nl \@\@/;
1951 }
1952}
1953
1954sub fix_inserted_deleted_lines {
1955 my ($linesRef, $insertedRef, $deletedRef) = @_;
1956
1957 my $range_last_linenr = 0;
1958 my $delta_offset = 0;
1959
1960 my $old_linenr = 0;
1961 my $new_linenr = 0;
1962
1963 my $next_insert = 0;
1964 my $next_delete = 0;
1965
1966 my @lines = ();
1967
1968 my $inserted = @{$insertedRef}[$next_insert++];
1969 my $deleted = @{$deletedRef}[$next_delete++];
1970
1971 foreach my $old_line (@{$linesRef}) {
1972 my $save_line = 1;
1973 my $line = $old_line; #don't modify the array
Joe Perches323b2672015-04-16 12:44:50 -07001974 if ($line =~ /^(?:\+\+\+|\-\-\-)\s+\S+/) { #new filename
Joe Perchesd752fcc2014-08-06 16:11:05 -07001975 $delta_offset = 0;
1976 } elsif ($line =~ /^\@\@ -\d+,\d+ \+\d+,\d+ \@\@/) { #new hunk
1977 $range_last_linenr = $new_linenr;
1978 fixup_current_range(\$line, $delta_offset, 0);
1979 }
1980
1981 while (defined($deleted) && ${$deleted}{'LINENR'} == $old_linenr) {
1982 $deleted = @{$deletedRef}[$next_delete++];
1983 $save_line = 0;
1984 fixup_current_range(\$lines[$range_last_linenr], $delta_offset--, -1);
1985 }
1986
1987 while (defined($inserted) && ${$inserted}{'LINENR'} == $old_linenr) {
1988 push(@lines, ${$inserted}{'LINE'});
1989 $inserted = @{$insertedRef}[$next_insert++];
1990 $new_linenr++;
1991 fixup_current_range(\$lines[$range_last_linenr], $delta_offset++, 1);
1992 }
1993
1994 if ($save_line) {
1995 push(@lines, $line);
1996 $new_linenr++;
1997 }
1998
1999 $old_linenr++;
2000 }
2001
2002 return @lines;
2003}
2004
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07002005sub fix_insert_line {
2006 my ($linenr, $line) = @_;
2007
2008 my $inserted = {
2009 LINENR => $linenr,
2010 LINE => $line,
2011 };
2012 push(@fixed_inserted, $inserted);
2013}
2014
2015sub fix_delete_line {
2016 my ($linenr, $line) = @_;
2017
2018 my $deleted = {
2019 LINENR => $linenr,
2020 LINE => $line,
2021 };
2022
2023 push(@fixed_deleted, $deleted);
2024}
2025
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002026sub ERROR {
Joe Perchescbec18a2014-04-03 14:49:19 -07002027 my ($type, $msg) = @_;
2028
2029 if (report("ERROR", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002030 our $clean = 0;
2031 our $cnt_error++;
Joe Perches3705ce52013-07-03 15:05:31 -07002032 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002033 }
Joe Perches3705ce52013-07-03 15:05:31 -07002034 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002035}
2036sub WARN {
Joe Perchescbec18a2014-04-03 14:49:19 -07002037 my ($type, $msg) = @_;
2038
2039 if (report("WARNING", $type, $msg)) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002040 our $clean = 0;
2041 our $cnt_warn++;
Joe Perches3705ce52013-07-03 15:05:31 -07002042 return 1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002043 }
Joe Perches3705ce52013-07-03 15:05:31 -07002044 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002045}
2046sub CHK {
Joe Perchescbec18a2014-04-03 14:49:19 -07002047 my ($type, $msg) = @_;
2048
2049 if ($check && report("CHECK", $type, $msg)) {
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002050 our $clean = 0;
2051 our $cnt_chk++;
Joe Perches3705ce52013-07-03 15:05:31 -07002052 return 1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002053 }
Joe Perches3705ce52013-07-03 15:05:31 -07002054 return 0;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002055}
2056
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002057sub check_absolute_file {
2058 my ($absolute, $herecurr) = @_;
2059 my $file = $absolute;
2060
2061 ##print "absolute<$absolute>\n";
2062
2063 # See if any suffix of this path is a path within the tree.
2064 while ($file =~ s@^[^/]*/@@) {
2065 if (-f "$root/$file") {
2066 ##print "file<$file>\n";
2067 last;
2068 }
2069 }
2070 if (! -f _) {
2071 return 0;
2072 }
2073
2074 # It is, so see if the prefix is acceptable.
2075 my $prefix = $absolute;
2076 substr($prefix, -length($file)) = '';
2077
2078 ##print "prefix<$prefix>\n";
2079 if ($prefix ne ".../") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002080 WARN("USE_RELATIVE_PATH",
2081 "use relative pathname instead of absolute in changelog text\n" . $herecurr);
Andy Whitcroft6ecd9672008-10-15 22:02:21 -07002082 }
2083}
2084
Joe Perches3705ce52013-07-03 15:05:31 -07002085sub trim {
2086 my ($string) = @_;
2087
Joe Perchesb34c6482013-09-11 14:24:01 -07002088 $string =~ s/^\s+|\s+$//g;
2089
2090 return $string;
2091}
2092
2093sub ltrim {
2094 my ($string) = @_;
2095
2096 $string =~ s/^\s+//;
2097
2098 return $string;
2099}
2100
2101sub rtrim {
2102 my ($string) = @_;
2103
2104 $string =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002105
2106 return $string;
2107}
2108
Joe Perches52ea8502013-11-12 15:10:09 -08002109sub string_find_replace {
2110 my ($string, $find, $replace) = @_;
2111
2112 $string =~ s/$find/$replace/g;
2113
2114 return $string;
2115}
2116
Joe Perches3705ce52013-07-03 15:05:31 -07002117sub tabify {
2118 my ($leading) = @_;
2119
2120 my $source_indent = 8;
2121 my $max_spaces_before_tab = $source_indent - 1;
2122 my $spaces_to_tab = " " x $source_indent;
2123
2124 #convert leading spaces to tabs
2125 1 while $leading =~ s@^([\t]*)$spaces_to_tab@$1\t@g;
2126 #Remove spaces before a tab
2127 1 while $leading =~ s@^([\t]*)( {1,$max_spaces_before_tab})\t@$1\t@g;
2128
2129 return "$leading";
2130}
2131
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002132sub pos_last_openparen {
2133 my ($line) = @_;
2134
2135 my $pos = 0;
2136
2137 my $opens = $line =~ tr/\(/\(/;
2138 my $closes = $line =~ tr/\)/\)/;
2139
2140 my $last_openparen = 0;
2141
2142 if (($opens == 0) || ($closes >= $opens)) {
2143 return -1;
2144 }
2145
2146 my $len = length($line);
2147
2148 for ($pos = 0; $pos < $len; $pos++) {
2149 my $string = substr($line, $pos);
2150 if ($string =~ /^($FuncArg|$balanced_parens)/) {
2151 $pos += length($1) - 1;
2152 } elsif (substr($line, $pos, 1) eq '(') {
2153 $last_openparen = $pos;
2154 } elsif (index($string, '(') == -1) {
2155 last;
2156 }
2157 }
2158
Joe Perches91cb5192014-04-03 14:49:32 -07002159 return length(expand_tabs(substr($line, 0, $last_openparen))) + 1;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002160}
2161
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002162sub process {
2163 my $filename = shift;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002164
2165 my $linenr=0;
2166 my $prevline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002167 my $prevrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002168 my $stashline="";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002169 my $stashrawline="";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002170
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002171 my $length;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002172 my $indent;
2173 my $previndent=0;
2174 my $stashindent=0;
2175
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002176 our $clean = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002177 my $signoff = 0;
2178 my $is_patch = 0;
Joe Perches29ee1b02014-08-06 16:10:35 -07002179 my $in_header_lines = $file ? 0 : 1;
Joe Perches15662b32011-10-31 17:13:12 -07002180 my $in_commit_log = 0; #Scanning lines before patch
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002181 my $has_commit_log = 0; #Encountered lines before patch
Joe Perches77cb8542017-02-24 15:01:28 -08002182 my $commit_log_possible_stack_dump = 0;
Joe Perches2a076f42015-04-16 12:44:28 -07002183 my $commit_log_long_line = 0;
Joe Perchese518e9a2015-06-25 15:03:27 -07002184 my $commit_log_has_diff = 0;
Joe Perches13f19372014-08-06 16:10:59 -07002185 my $reported_maintainer_file = 0;
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002186 my $non_utf8_charset = 0;
2187
Joe Perches365dd4e2014-08-06 16:10:42 -07002188 my $last_blank_line = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08002189 my $last_coalesced_string_linenr = -1;
Joe Perches365dd4e2014-08-06 16:10:42 -07002190
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002191 our @report = ();
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002192 our $cnt_lines = 0;
2193 our $cnt_error = 0;
2194 our $cnt_warn = 0;
2195 our $cnt_chk = 0;
2196
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002197 # Trace the real file/line as we go.
2198 my $realfile = '';
2199 my $realline = 0;
2200 my $realcnt = 0;
2201 my $here = '';
Joe Perches77cb8542017-02-24 15:01:28 -08002202 my $context_function; #undef'd unless there's a known function
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002203 my $in_comment = 0;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002204 my $comment_edge = 0;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002205 my $first_line = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002206 my $p1_prefix = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002207
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002208 my $prev_values = 'E';
2209
2210 # suppression flags
Andy Whitcroft773647a2008-03-28 14:15:58 -07002211 my %suppress_ifbraces;
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002212 my %suppress_whiletrailers;
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002213 my %suppress_export;
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002214 my $suppress_statement = 0;
Andy Whitcroft653d4872007-06-23 17:16:34 -07002215
Joe Perches7e51f192013-09-11 14:23:57 -07002216 my %signatures = ();
Joe Perches323c1262012-12-17 16:02:07 -08002217
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002218 # Pre-scan the patch sanitizing the lines.
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002219 # Pre-scan the patch looking for any __setup documentation.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002220 #
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002221 my @setup_docs = ();
2222 my $setup_docs = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002223
Joe Perchesd8b07712013-11-12 15:10:06 -08002224 my $camelcase_file_seeded = 0;
2225
Andy Whitcroft773647a2008-03-28 14:15:58 -07002226 sanitise_line_reset();
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002227 my $line;
2228 foreach my $rawline (@rawlines) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002229 $linenr++;
2230 $line = $rawline;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002231
Joe Perches3705ce52013-07-03 15:05:31 -07002232 push(@fixed, $rawline) if ($fix);
2233
Andy Whitcroft773647a2008-03-28 14:15:58 -07002234 if ($rawline=~/^\+\+\+\s+(\S+)/) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002235 $setup_docs = 0;
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02002236 if ($1 =~ m@Documentation/admin-guide/kernel-parameters.rst$@) {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002237 $setup_docs = 1;
2238 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002239 #next;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002240 }
Joe Perches74fd4f32017-05-08 15:56:02 -07002241 if ($rawline =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002242 $realline=$1-1;
2243 if (defined $2) {
2244 $realcnt=$3+1;
2245 } else {
2246 $realcnt=1+1;
2247 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07002248 $in_comment = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002249
2250 # Guestimate if this is a continuing comment. Run
2251 # the context looking for a comment "edge". If this
2252 # edge is a close comment then we must be in a comment
2253 # at context start.
2254 my $edge;
Andy Whitcroft01fa9142008-10-15 22:02:19 -07002255 my $cnt = $realcnt;
2256 for (my $ln = $linenr + 1; $cnt > 0; $ln++) {
2257 next if (defined $rawlines[$ln - 1] &&
2258 $rawlines[$ln - 1] =~ /^-/);
2259 $cnt--;
2260 #print "RAW<$rawlines[$ln - 1]>\n";
Andy Whitcroft721c1cb2009-01-06 14:41:16 -08002261 last if (!defined $rawlines[$ln - 1]);
Andy Whitcroftfae17da2009-01-06 14:41:20 -08002262 if ($rawlines[$ln - 1] =~ m@(/\*|\*/)@ &&
2263 $rawlines[$ln - 1] !~ m@"[^"]*(?:/\*|\*/)[^"]*"@) {
2264 ($edge) = $1;
2265 last;
2266 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002267 }
2268 if (defined $edge && $edge eq '*/') {
2269 $in_comment = 1;
2270 }
2271
2272 # Guestimate if this is a continuing comment. If this
2273 # is the start of a diff block and this line starts
2274 # ' *' then it is very likely a comment.
2275 if (!defined $edge &&
Andy Whitcroft83242e02009-01-06 14:41:17 -08002276 $rawlines[$linenr] =~ m@^.\s*(?:\*\*+| \*)(?:\s|$)@)
Andy Whitcroft773647a2008-03-28 14:15:58 -07002277 {
2278 $in_comment = 1;
2279 }
2280
2281 ##print "COMMENT:$in_comment edge<$edge> $rawline\n";
2282 sanitise_line_reset($in_comment);
2283
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002284 } elsif ($realcnt && $rawline =~ /^(?:\+| |$)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002285 # Standardise the strings and chars within the input to
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002286 # simplify matching -- only bother with positive lines.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002287 $line = sanitise_line($rawline);
2288 }
2289 push(@lines, $line);
2290
2291 if ($realcnt > 1) {
2292 $realcnt-- if ($line =~ /^(?:\+| |$)/);
2293 } else {
2294 $realcnt = 0;
2295 }
2296
2297 #print "==>$rawline\n";
2298 #print "-->$line\n";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002299
2300 if ($setup_docs && $line =~ /^\+/) {
2301 push(@setup_docs, $line);
2302 }
2303 }
2304
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002305 $prefix = '';
2306
Andy Whitcroft773647a2008-03-28 14:15:58 -07002307 $realcnt = 0;
2308 $linenr = 0;
Joe Perches194f66f2014-08-06 16:11:03 -07002309 $fixlinenr = -1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002310 foreach my $line (@lines) {
2311 $linenr++;
Joe Perches194f66f2014-08-06 16:11:03 -07002312 $fixlinenr++;
Joe Perches1b5539b2013-09-11 14:24:03 -07002313 my $sline = $line; #copy of $line
2314 $sline =~ s/$;/ /g; #with comments as spaces
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002315
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002316 my $rawline = $rawlines[$linenr - 1];
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002317
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002318#extract the line range in the file after the patch is applied
Joe Perchese518e9a2015-06-25 15:03:27 -07002319 if (!$in_commit_log &&
Joe Perches74fd4f32017-05-08 15:56:02 -07002320 $line =~ /^\@\@ -\d+(?:,\d+)? \+(\d+)(,(\d+))? \@\@(.*)/) {
2321 my $context = $4;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002322 $is_patch = 1;
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002323 $first_line = $linenr + 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002324 $realline=$1-1;
2325 if (defined $2) {
2326 $realcnt=$3+1;
2327 } else {
2328 $realcnt=1+1;
2329 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002330 annotate_reset();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08002331 $prev_values = 'E';
2332
Andy Whitcroft773647a2008-03-28 14:15:58 -07002333 %suppress_ifbraces = ();
Andy Whitcroft170d3a22008-10-15 22:02:30 -07002334 %suppress_whiletrailers = ();
Andy Whitcroft2b474a12009-10-26 16:50:16 -07002335 %suppress_export = ();
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08002336 $suppress_statement = 0;
Joe Perches74fd4f32017-05-08 15:56:02 -07002337 if ($context =~ /\b(\w+)\s*\(/) {
2338 $context_function = $1;
2339 } else {
2340 undef $context_function;
2341 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002342 next;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002343
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002344# track the line number as we move through the hunk, note that
2345# new versions of GNU diff omit the leading space on completely
2346# blank context lines so we need to count that too.
Andy Whitcroft773647a2008-03-28 14:15:58 -07002347 } elsif ($line =~ /^( |\+|$)/) {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002348 $realline++;
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002349 $realcnt-- if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002350
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002351 # Measure the line length and indent.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002352 ($length, $indent) = line_stats($rawline);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002353
2354 # Track the previous line.
2355 ($prevline, $stashline) = ($stashline, $line);
2356 ($previndent, $stashindent) = ($stashindent, $indent);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002357 ($prevrawline, $stashrawline) = ($stashrawline, $rawline);
2358
Andy Whitcroft773647a2008-03-28 14:15:58 -07002359 #warn "line<$line>\n";
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002360
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002361 } elsif ($realcnt == 1) {
2362 $realcnt--;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002363 }
2364
Andy Whitcroftcc77cdc2009-10-26 16:50:13 -07002365 my $hunk_line = ($realcnt != 0);
2366
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002367 $here = "#$linenr: " if (!$file);
2368 $here = "#$realline: " if ($file);
Andy Whitcroft773647a2008-03-28 14:15:58 -07002369
Joe Perches2ac73b42014-06-04 16:12:05 -07002370 my $found_file = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07002371 # extract the filename as it passes
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002372 if ($line =~ /^diff --git.*?(\S+)$/) {
2373 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002374 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002375 $in_commit_log = 0;
Joe Perches2ac73b42014-06-04 16:12:05 -07002376 $found_file = 1;
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002377 } elsif ($line =~ /^\+\+\+\s+(\S+)/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07002378 $realfile = $1;
Joe Perches2b7ab452013-11-12 15:10:14 -08002379 $realfile =~ s@^([^/]*)/@@ if (!$file);
Joe Perches270c49a2012-01-10 15:09:50 -08002380 $in_commit_log = 0;
Wolfram Sang1e855722009-01-06 14:41:24 -08002381
2382 $p1_prefix = $1;
Andy Whitcrofte2f7aa42009-02-27 14:03:06 -08002383 if (!$file && $tree && $p1_prefix ne '' &&
2384 -e "$root/$p1_prefix") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002385 WARN("PATCH_PREFIX",
2386 "patch prefix '$p1_prefix' exists, appears to be a -p0 patch\n");
Wolfram Sang1e855722009-01-06 14:41:24 -08002387 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002388
Andy Whitcroftc1ab3322008-10-15 22:02:20 -07002389 if ($realfile =~ m@^include/asm/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002390 ERROR("MODIFIED_INCLUDE_ASM",
2391 "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 -07002392 }
Joe Perches2ac73b42014-06-04 16:12:05 -07002393 $found_file = 1;
2394 }
2395
Joe Perches34d88152015-06-25 15:03:05 -07002396#make up the handle for any error we report on this line
2397 if ($showfile) {
2398 $prefix = "$realfile:$realline: "
2399 } elsif ($emacs) {
Joe Perches7d3a9f62015-09-09 15:37:39 -07002400 if ($file) {
2401 $prefix = "$filename:$realline: ";
2402 } else {
2403 $prefix = "$filename:$linenr: ";
2404 }
Joe Perches34d88152015-06-25 15:03:05 -07002405 }
2406
Joe Perches2ac73b42014-06-04 16:12:05 -07002407 if ($found_file) {
Joe Perches85b0ee12016-10-11 13:51:44 -07002408 if (is_maintained_obsolete($realfile)) {
2409 WARN("OBSOLETE",
2410 "$realfile is marked as 'obsolete' in the MAINTAINERS hierarchy. No unnecessary modifications please.\n");
2411 }
Joe Perches7bd7e482015-09-09 15:37:44 -07002412 if ($realfile =~ m@^(?:drivers/net/|net/|drivers/staging/)@) {
Joe Perches2ac73b42014-06-04 16:12:05 -07002413 $check = 1;
2414 } else {
2415 $check = $check_orig;
2416 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07002417 next;
2418 }
2419
Randy Dunlap389834b2007-06-08 13:47:03 -07002420 $here .= "FILE: $realfile:$realline:" if ($realcnt != 0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002421
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002422 my $hereline = "$here\n$rawline\n";
2423 my $herecurr = "$here\n$rawline\n";
2424 my $hereprev = "$here\n$prevrawline\n$rawline\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002425
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002426 $cnt_lines++ if ($realcnt != 0);
2427
Joe Perchese518e9a2015-06-25 15:03:27 -07002428# Check if the commit log has what seems like a diff which can confuse patch
2429 if ($in_commit_log && !$commit_log_has_diff &&
2430 (($line =~ m@^\s+diff\b.*a/[\w/]+@ &&
2431 $line =~ m@^\s+diff\b.*a/([\w/]+)\s+b/$1\b@) ||
2432 $line =~ m@^\s*(?:\-\-\-\s+a/|\+\+\+\s+b/)@ ||
2433 $line =~ m/^\s*\@\@ \-\d+,\d+ \+\d+,\d+ \@\@/)) {
2434 ERROR("DIFF_IN_COMMIT_MSG",
2435 "Avoid using diff content in the commit message - patch(1) might not work\n" . $herecurr);
2436 $commit_log_has_diff = 1;
2437 }
2438
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002439# Check for incorrect file permissions
2440 if ($line =~ /^new (file )?mode.*[7531]\d{0,2}$/) {
2441 my $permhere = $here . "FILE: $realfile\n";
Joe Perches04db4d22013-04-29 16:18:14 -07002442 if ($realfile !~ m@scripts/@ &&
2443 $realfile !~ /\.(py|pl|awk|sh)$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002444 ERROR("EXECUTE_PERMISSIONS",
2445 "do not set execute permissions for source files\n" . $permhere);
Rabin Vincent3bf9a002010-10-26 14:23:16 -07002446 }
2447 }
2448
Joe Perches20112472011-07-25 17:13:23 -07002449# Check the patch for a signoff:
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07002450 if ($line =~ /^\s*signed-off-by:/i) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07002451 $signoff++;
Joe Perches15662b32011-10-31 17:13:12 -07002452 $in_commit_log = 0;
Joe Perches20112472011-07-25 17:13:23 -07002453 }
2454
Joe Perchese0d975b2014-12-10 15:51:49 -08002455# Check if MAINTAINERS is being updated. If so, there's probably no need to
2456# emit the "does MAINTAINERS need updating?" message on file add/move/delete
2457 if ($line =~ /^\s*MAINTAINERS\s*\|/) {
2458 $reported_maintainer_file = 1;
2459 }
2460
Joe Perches20112472011-07-25 17:13:23 -07002461# Check signature styles
Joe Perches270c49a2012-01-10 15:09:50 -08002462 if (!$in_header_lines &&
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002463 $line =~ /^(\s*)([a-z0-9_-]+by:|$signature_tags)(\s*)(.*)/i) {
Joe Perches20112472011-07-25 17:13:23 -07002464 my $space_before = $1;
2465 my $sign_off = $2;
2466 my $space_after = $3;
2467 my $email = $4;
2468 my $ucfirst_sign_off = ucfirst(lc($sign_off));
2469
Joe Perchesce0338df3c2012-07-30 14:41:18 -07002470 if ($sign_off !~ /$signature_tags/) {
2471 WARN("BAD_SIGN_OFF",
2472 "Non-standard signature: $sign_off\n" . $herecurr);
2473 }
Joe Perches20112472011-07-25 17:13:23 -07002474 if (defined $space_before && $space_before ne "") {
Joe Perches3705ce52013-07-03 15:05:31 -07002475 if (WARN("BAD_SIGN_OFF",
2476 "Do not use whitespace before $ucfirst_sign_off\n" . $herecurr) &&
2477 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002478 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002479 "$ucfirst_sign_off $email";
2480 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002481 }
Joe Perches20112472011-07-25 17:13:23 -07002482 if ($sign_off =~ /-by:$/i && $sign_off ne $ucfirst_sign_off) {
Joe Perches3705ce52013-07-03 15:05:31 -07002483 if (WARN("BAD_SIGN_OFF",
2484 "'$ucfirst_sign_off' is the preferred signature form\n" . $herecurr) &&
2485 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002486 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002487 "$ucfirst_sign_off $email";
2488 }
2489
Joe Perches20112472011-07-25 17:13:23 -07002490 }
2491 if (!defined $space_after || $space_after ne " ") {
Joe Perches3705ce52013-07-03 15:05:31 -07002492 if (WARN("BAD_SIGN_OFF",
2493 "Use a single space after $ucfirst_sign_off\n" . $herecurr) &&
2494 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002495 $fixed[$fixlinenr] =
Joe Perches3705ce52013-07-03 15:05:31 -07002496 "$ucfirst_sign_off $email";
2497 }
Joe Perches20112472011-07-25 17:13:23 -07002498 }
2499
2500 my ($email_name, $email_address, $comment) = parse_email($email);
2501 my $suggested_email = format_email(($email_name, $email_address));
2502 if ($suggested_email eq "") {
Joe Perches000d1cc12011-07-25 17:13:25 -07002503 ERROR("BAD_SIGN_OFF",
2504 "Unrecognized email address: '$email'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002505 } else {
2506 my $dequoted = $suggested_email;
2507 $dequoted =~ s/^"//;
2508 $dequoted =~ s/" </ </;
2509 # Don't force email to have quotes
2510 # Allow just an angle bracketed address
2511 if ("$dequoted$comment" ne $email &&
2512 "<$email_address>$comment" ne $email &&
2513 "$suggested_email$comment" ne $email) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002514 WARN("BAD_SIGN_OFF",
2515 "email address '$email' might be better as '$suggested_email$comment'\n" . $herecurr);
Joe Perches20112472011-07-25 17:13:23 -07002516 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002517 }
Joe Perches7e51f192013-09-11 14:23:57 -07002518
2519# Check for duplicate signatures
2520 my $sig_nospace = $line;
2521 $sig_nospace =~ s/\s//g;
2522 $sig_nospace = lc($sig_nospace);
2523 if (defined $signatures{$sig_nospace}) {
2524 WARN("BAD_SIGN_OFF",
2525 "Duplicate signature\n" . $herecurr);
2526 } else {
2527 $signatures{$sig_nospace} = 1;
2528 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002529 }
2530
Joe Perchesa2fe16b2015-02-13 14:39:02 -08002531# Check email subject for common tools that don't need to be mentioned
2532 if ($in_header_lines &&
2533 $line =~ /^Subject:.*\b(?:checkpatch|sparse|smatch)\b[^:]/i) {
2534 WARN("EMAIL_SUBJECT",
2535 "A patch subject line should describe the change not the tool that found it\n" . $herecurr);
2536 }
2537
Joe Perches9b3189e2014-06-04 16:12:10 -07002538# Check for old stable address
2539 if ($line =~ /^\s*cc:\s*.*<?\bstable\@kernel\.org\b>?.*$/i) {
2540 ERROR("STABLE_ADDRESS",
2541 "The 'stable' address should be 'stable\@vger.kernel.org'\n" . $herecurr);
2542 }
2543
Christopher Covington7ebd05e2014-04-03 14:49:31 -07002544# Check for unwanted Gerrit info
2545 if ($in_commit_log && $line =~ /^\s*change-id:/i) {
2546 ERROR("GERRIT_CHANGE_ID",
2547 "Remove Gerrit Change-Id's before submitting upstream.\n" . $herecurr);
2548 }
2549
Joe Perches369c8dd2015-11-06 16:31:34 -08002550# Check if the commit log is in a possible stack dump
2551 if ($in_commit_log && !$commit_log_possible_stack_dump &&
2552 ($line =~ /^\s*(?:WARNING:|BUG:)/ ||
2553 $line =~ /^\s*\[\s*\d+\.\d{6,6}\s*\]/ ||
2554 # timestamp
2555 $line =~ /^\s*\[\<[0-9a-fA-F]{8,}\>\]/)) {
2556 # stack dump address
2557 $commit_log_possible_stack_dump = 1;
2558 }
2559
Joe Perches2a076f42015-04-16 12:44:28 -07002560# Check for line lengths > 75 in commit log, warn once
2561 if ($in_commit_log && !$commit_log_long_line &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002562 length($line) > 75 &&
2563 !($line =~ /^\s*[a-zA-Z0-9_\/\.]+\s+\|\s+\d+/ ||
2564 # file delta changes
2565 $line =~ /^\s*(?:[\w\.\-]+\/)++[\w\.\-]+:/ ||
2566 # filename then :
2567 $line =~ /^\s*(?:Fixes:|Link:)/i ||
2568 # A Fixes: or Link: line
2569 $commit_log_possible_stack_dump)) {
Joe Perches2a076f42015-04-16 12:44:28 -07002570 WARN("COMMIT_LOG_LONG_LINE",
2571 "Possible unwrapped commit description (prefer a maximum 75 chars per line)\n" . $herecurr);
2572 $commit_log_long_line = 1;
2573 }
2574
Joe Perchesbf4daf12015-09-09 15:37:50 -07002575# Reset possible stack dump if a blank line is found
Joe Perches369c8dd2015-11-06 16:31:34 -08002576 if ($in_commit_log && $commit_log_possible_stack_dump &&
2577 $line =~ /^\s*$/) {
2578 $commit_log_possible_stack_dump = 0;
2579 }
Joe Perchesbf4daf12015-09-09 15:37:50 -07002580
Joe Perches0d7835f2015-02-13 14:38:35 -08002581# Check for git id commit length and improperly formed commit descriptions
Joe Perches369c8dd2015-11-06 16:31:34 -08002582 if ($in_commit_log && !$commit_log_possible_stack_dump &&
Joe Perchesaab38f52016-08-02 14:04:36 -07002583 $line !~ /^\s*(?:Link|Patchwork|http|https|BugLink):/i &&
Wei Wange882dbf2017-05-08 15:55:54 -07002584 $line !~ /^This reverts commit [0-9a-f]{7,40}/ &&
Joe Perchesfe043ea2015-09-09 15:37:25 -07002585 ($line =~ /\bcommit\s+[0-9a-f]{5,}\b/i ||
Joe Perchesaab38f52016-08-02 14:04:36 -07002586 ($line =~ /(?:\s|^)[0-9a-f]{12,40}(?:[\s"'\(\[]|$)/i &&
Joe Perches369c8dd2015-11-06 16:31:34 -08002587 $line !~ /[\<\[][0-9a-f]{12,40}[\>\]]/i &&
2588 $line !~ /\bfixes:\s*[0-9a-f]{12,40}/i))) {
Joe Perchesfe043ea2015-09-09 15:37:25 -07002589 my $init_char = "c";
2590 my $orig_commit = "";
Joe Perches0d7835f2015-02-13 14:38:35 -08002591 my $short = 1;
2592 my $long = 0;
2593 my $case = 1;
2594 my $space = 1;
2595 my $hasdesc = 0;
Joe Perches19c146a2015-02-13 14:39:00 -08002596 my $hasparens = 0;
Joe Perches0d7835f2015-02-13 14:38:35 -08002597 my $id = '0123456789ab';
2598 my $orig_desc = "commit description";
2599 my $description = "";
2600
Joe Perchesfe043ea2015-09-09 15:37:25 -07002601 if ($line =~ /\b(c)ommit\s+([0-9a-f]{5,})\b/i) {
2602 $init_char = $1;
2603 $orig_commit = lc($2);
2604 } elsif ($line =~ /\b([0-9a-f]{12,40})\b/i) {
2605 $orig_commit = lc($1);
2606 }
2607
Joe Perches0d7835f2015-02-13 14:38:35 -08002608 $short = 0 if ($line =~ /\bcommit\s+[0-9a-f]{12,40}/i);
2609 $long = 1 if ($line =~ /\bcommit\s+[0-9a-f]{41,}/i);
2610 $space = 0 if ($line =~ /\bcommit [0-9a-f]/i);
2611 $case = 0 if ($line =~ /\b[Cc]ommit\s+[0-9a-f]{5,40}[^A-F]/);
2612 if ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)"\)/i) {
2613 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002614 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002615 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s*$/i &&
2616 defined $rawlines[$linenr] &&
2617 $rawlines[$linenr] =~ /^\s*\("([^"]+)"\)/) {
2618 $orig_desc = $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002619 $hasparens = 1;
Joe Perchesb671fde2015-02-13 14:38:41 -08002620 } elsif ($line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("[^"]+$/i &&
2621 defined $rawlines[$linenr] &&
2622 $rawlines[$linenr] =~ /^\s*[^"]+"\)/) {
2623 $line =~ /\bcommit\s+[0-9a-f]{5,}\s+\("([^"]+)$/i;
2624 $orig_desc = $1;
2625 $rawlines[$linenr] =~ /^\s*([^"]+)"\)/;
2626 $orig_desc .= " " . $1;
Joe Perches19c146a2015-02-13 14:39:00 -08002627 $hasparens = 1;
Joe Perches0d7835f2015-02-13 14:38:35 -08002628 }
2629
2630 ($id, $description) = git_commit_info($orig_commit,
2631 $id, $orig_desc);
2632
Heinrich Schuchardt948b1332017-07-10 15:52:16 -07002633 if (defined($id) &&
2634 ($short || $long || $space || $case || ($orig_desc ne $description) || !$hasparens)) {
Joe Perches0d7835f2015-02-13 14:38:35 -08002635 ERROR("GIT_COMMIT_ID",
2636 "Please use git commit description style 'commit <12+ chars of sha1> (\"<title line>\")' - ie: '${init_char}ommit $id (\"$description\")'\n" . $herecurr);
2637 }
Joe Perchesd311cd42014-08-06 16:10:57 -07002638 }
2639
Joe Perches13f19372014-08-06 16:10:59 -07002640# Check for added, moved or deleted files
2641 if (!$reported_maintainer_file && !$in_commit_log &&
2642 ($line =~ /^(?:new|deleted) file mode\s*\d+\s*$/ ||
2643 $line =~ /^rename (?:from|to) [\w\/\.\-]+\s*$/ ||
2644 ($line =~ /\{\s*([\w\/\.\-]*)\s*\=\>\s*([\w\/\.\-]*)\s*\}/ &&
2645 (defined($1) || defined($2))))) {
Andrew Jefferya82603a2016-12-12 16:46:37 -08002646 $is_patch = 1;
Joe Perches13f19372014-08-06 16:10:59 -07002647 $reported_maintainer_file = 1;
2648 WARN("FILE_PATH_CHANGES",
2649 "added, moved or deleted file(s), does MAINTAINERS need updating?\n" . $herecurr);
2650 }
2651
Andy Whitcroft00df3442007-06-08 13:47:06 -07002652# Check for wrappage within a valid hunk of the file
Andy Whitcroft8905a672007-11-28 16:21:06 -08002653 if ($realcnt != 0 && $line !~ m{^(?:\+|-| |\\ No newline|$)}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002654 ERROR("CORRUPTED_PATCH",
2655 "patch seems to be corrupt (line wrapped?)\n" .
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07002656 $herecurr) if (!$emitted_corrupt++);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07002657 }
2658
2659# UTF-8 regex found at http://www.w3.org/International/questions/qa-forms-utf-8.en.php
2660 if (($realfile =~ /^$/ || $line =~ /^\+/) &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07002661 $rawline !~ m/^$UTF8*$/) {
2662 my ($utf8_prefix) = ($rawline =~ /^($UTF8*)/);
2663
2664 my $blank = copy_spacing($rawline);
2665 my $ptr = substr($blank, 0, length($utf8_prefix)) . "^";
2666 my $hereptr = "$hereline$ptr\n";
2667
Joe Perches34d99212011-07-25 17:13:26 -07002668 CHK("INVALID_UTF8",
2669 "Invalid UTF-8, patch and commit message should be encoded in UTF-8\n" . $hereptr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002670 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002671
Joe Perches15662b32011-10-31 17:13:12 -07002672# Check if it's the start of a commit log
2673# (not a header line and we haven't seen the patch filename)
2674 if ($in_header_lines && $realfile =~ /^$/ &&
Joe Percheseb3a58d2017-05-08 15:55:42 -07002675 !($rawline =~ /^\s+(?:\S|$)/ ||
2676 $rawline =~ /^(?:commit\b|from\b|[\w-]+:)/i)) {
Joe Perches15662b32011-10-31 17:13:12 -07002677 $in_header_lines = 0;
2678 $in_commit_log = 1;
Allen Hubbeed43c4e2016-08-02 14:04:45 -07002679 $has_commit_log = 1;
Joe Perches15662b32011-10-31 17:13:12 -07002680 }
2681
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002682# Check if there is UTF-8 in a commit log when a mail header has explicitly
2683# declined it, i.e defined some charset where it is missing.
2684 if ($in_header_lines &&
2685 $rawline =~ /^Content-Type:.+charset="(.+)".*$/ &&
2686 $1 !~ /utf-8/i) {
2687 $non_utf8_charset = 1;
2688 }
2689
2690 if ($in_commit_log && $non_utf8_charset && $realfile =~ /^$/ &&
Joe Perches15662b32011-10-31 17:13:12 -07002691 $rawline =~ /$NON_ASCII_UTF8/) {
Pasi Savanainenfa64205d2012-10-04 17:13:29 -07002692 WARN("UTF8_BEFORE_PATCH",
Joe Perches15662b32011-10-31 17:13:12 -07002693 "8-bit UTF-8 used in possible commit log\n" . $herecurr);
2694 }
2695
Joe Perchesd6430f72016-12-12 16:46:28 -08002696# Check for absolute kernel paths in commit message
2697 if ($tree && $in_commit_log) {
2698 while ($line =~ m{(?:^|\s)(/\S*)}g) {
2699 my $file = $1;
2700
2701 if ($file =~ m{^(.*?)(?::\d+)+:?$} &&
2702 check_absolute_file($1, $herecurr)) {
2703 #
2704 } else {
2705 check_absolute_file($file, $herecurr);
2706 }
2707 }
2708 }
2709
Kees Cook66b47b42014-10-13 15:51:57 -07002710# Check for various typo / spelling mistakes
Joe Perches66d7a382015-04-16 12:44:08 -07002711 if (defined($misspellings) &&
2712 ($in_commit_log || $line =~ /^(?:\+|Subject:)/i)) {
Joe Perchesebfd7d62015-04-16 12:44:14 -07002713 while ($rawline =~ /(?:^|[^a-z@])($misspellings)(?:\b|$|[^a-z@])/gi) {
Kees Cook66b47b42014-10-13 15:51:57 -07002714 my $typo = $1;
2715 my $typo_fix = $spelling_fix{lc($typo)};
2716 $typo_fix = ucfirst($typo_fix) if ($typo =~ /^[A-Z]/);
2717 $typo_fix = uc($typo_fix) if ($typo =~ /^[A-Z]+$/);
2718 my $msg_type = \&WARN;
2719 $msg_type = \&CHK if ($file);
2720 if (&{$msg_type}("TYPO_SPELLING",
2721 "'$typo' may be misspelled - perhaps '$typo_fix'?\n" . $herecurr) &&
2722 $fix) {
2723 $fixed[$fixlinenr] =~ s/(^|[^A-Za-z@])($typo)($|[^A-Za-z@])/$1$typo_fix$3/;
2724 }
2725 }
2726 }
2727
Andy Whitcroft306708542008-10-15 22:02:28 -07002728# ignore non-hunk lines and lines being removed
2729 next if (!$hunk_line || $line =~ /^-/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07002730
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002731#trailing whitespace
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07002732 if ($line =~ /^\+.*\015/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002733 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perchesd5e616f2013-09-11 14:23:54 -07002734 if (ERROR("DOS_LINE_ENDINGS",
2735 "DOS line endings\n" . $herevet) &&
2736 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002737 $fixed[$fixlinenr] =~ s/[\s\015]+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07002738 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002739 } elsif ($rawline =~ /^\+.*\S\s+$/ || $rawline =~ /^\+\s+$/) {
2740 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002741 if (ERROR("TRAILING_WHITESPACE",
2742 "trailing whitespace\n" . $herevet) &&
2743 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002744 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perches3705ce52013-07-03 15:05:31 -07002745 }
2746
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002747 $rpt_cleaners = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002748 }
Andy Whitcroft5368df22008-10-15 22:02:27 -07002749
Josh Triplett4783f892013-11-12 15:10:12 -08002750# Check for FSF mailing addresses.
Alexander Duyck109d8cb2014-01-23 15:54:50 -08002751 if ($rawline =~ /\bwrite to the Free/i ||
Matthew Wilcox1bde5612017-02-24 15:01:38 -08002752 $rawline =~ /\b675\s+Mass\s+Ave/i ||
Joe Perches3e2232f2014-01-23 15:54:48 -08002753 $rawline =~ /\b59\s+Temple\s+Pl/i ||
2754 $rawline =~ /\b51\s+Franklin\s+St/i) {
Josh Triplett4783f892013-11-12 15:10:12 -08002755 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
2756 my $msg_type = \&ERROR;
2757 $msg_type = \&CHK if ($file);
2758 &{$msg_type}("FSF_MAILING_ADDRESS",
Joe Perches3e2232f2014-01-23 15:54:48 -08002759 "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 -08002760 }
2761
Andi Kleen33549572010-05-24 14:33:29 -07002762# check for Kconfig help text having a real description
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002763# Only applies when adding the entry originally, after that we do not have
2764# sufficient context to determine whether it is indeed long enough.
Andi Kleen33549572010-05-24 14:33:29 -07002765 if ($realfile =~ /Kconfig/ &&
Joe Perches8d73e0e2014-08-06 16:10:46 -07002766 $line =~ /^\+\s*config\s+/) {
Andi Kleen33549572010-05-24 14:33:29 -07002767 my $length = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002768 my $cnt = $realcnt;
2769 my $ln = $linenr + 1;
2770 my $f;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002771 my $is_start = 0;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002772 my $is_end = 0;
Andy Whitcrofta1385802012-01-10 15:10:03 -08002773 for (; $cnt > 0 && defined $lines[$ln - 1]; $ln++) {
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002774 $f = $lines[$ln - 1];
2775 $cnt-- if ($lines[$ln - 1] !~ /^-/);
2776 $is_end = $lines[$ln - 1] =~ /^\+/;
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002777
2778 next if ($f =~ /^-/);
Joe Perches8d73e0e2014-08-06 16:10:46 -07002779 last if (!$file && $f =~ /^\@\@/);
Andy Whitcrofta1385802012-01-10 15:10:03 -08002780
Joe Perches8d73e0e2014-08-06 16:10:46 -07002781 if ($lines[$ln - 1] =~ /^\+\s*(?:bool|tristate)\s*\"/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002782 $is_start = 1;
Joe Perches8d73e0e2014-08-06 16:10:46 -07002783 } elsif ($lines[$ln - 1] =~ /^\+\s*(?:---)?help(?:---)?$/) {
Andy Whitcrofta1385802012-01-10 15:10:03 -08002784 $length = -1;
2785 }
2786
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002787 $f =~ s/^.//;
Andi Kleen33549572010-05-24 14:33:29 -07002788 $f =~ s/#.*//;
2789 $f =~ s/^\s+//;
2790 next if ($f =~ /^$/);
Andy Whitcroft9fe287d72010-10-26 14:23:15 -07002791 if ($f =~ /^\s*config\s/) {
2792 $is_end = 1;
2793 last;
2794 }
Andi Kleen33549572010-05-24 14:33:29 -07002795 $length++;
2796 }
Vadim Bendebury56193272014-10-13 15:51:48 -07002797 if ($is_start && $is_end && $length < $min_conf_desc_length) {
2798 WARN("CONFIG_DESCRIPTION",
2799 "please write a paragraph that describes the config symbol fully\n" . $herecurr);
2800 }
Andy Whitcrofta1385802012-01-10 15:10:03 -08002801 #print "is_start<$is_start> is_end<$is_end> length<$length>\n";
Andi Kleen33549572010-05-24 14:33:29 -07002802 }
2803
Joe Perches628f91a2017-07-10 15:52:07 -07002804# check for MAINTAINERS entries that don't have the right form
2805 if ($realfile =~ /^MAINTAINERS$/ &&
2806 $rawline =~ /^\+[A-Z]:/ &&
2807 $rawline !~ /^\+[A-Z]:\t\S/) {
2808 if (WARN("MAINTAINERS_STYLE",
2809 "MAINTAINERS entries use one tab after TYPE:\n" . $herecurr) &&
2810 $fix) {
2811 $fixed[$fixlinenr] =~ s/^(\+[A-Z]):\s*/$1:\t/;
2812 }
2813 }
2814
Christoph Jaeger327953e2015-02-13 14:38:29 -08002815# discourage the use of boolean for type definition attributes of Kconfig options
2816 if ($realfile =~ /Kconfig/ &&
2817 $line =~ /^\+\s*\bboolean\b/) {
2818 WARN("CONFIG_TYPE_BOOLEAN",
2819 "Use of boolean is deprecated, please use bool instead.\n" . $herecurr);
2820 }
2821
Arnaud Lacombec68e5872011-08-15 01:07:14 -04002822 if (($realfile =~ /Makefile.*/ || $realfile =~ /Kbuild.*/) &&
2823 ($line =~ /\+(EXTRA_[A-Z]+FLAGS).*/)) {
2824 my $flag = $1;
2825 my $replacement = {
2826 'EXTRA_AFLAGS' => 'asflags-y',
2827 'EXTRA_CFLAGS' => 'ccflags-y',
2828 'EXTRA_CPPFLAGS' => 'cppflags-y',
2829 'EXTRA_LDFLAGS' => 'ldflags-y',
2830 };
2831
2832 WARN("DEPRECATED_VARIABLE",
2833 "Use of $flag is deprecated, please use \`$replacement->{$flag} instead.\n" . $herecurr) if ($replacement->{$flag});
2834 }
2835
Rob Herringbff5da42014-01-23 15:54:51 -08002836# check for DT compatible documentation
Florian Vaussard7dd05b32014-04-03 14:49:26 -07002837 if (defined $root &&
2838 (($realfile =~ /\.dtsi?$/ && $line =~ /^\+\s*compatible\s*=\s*\"/) ||
2839 ($realfile =~ /\.[ch]$/ && $line =~ /^\+.*\.compatible\s*=\s*\"/))) {
2840
Rob Herringbff5da42014-01-23 15:54:51 -08002841 my @compats = $rawline =~ /\"([a-zA-Z0-9\-\,\.\+_]+)\"/g;
2842
Florian Vaussardcc933192014-04-03 14:49:27 -07002843 my $dt_path = $root . "/Documentation/devicetree/bindings/";
2844 my $vp_file = $dt_path . "vendor-prefixes.txt";
2845
Rob Herringbff5da42014-01-23 15:54:51 -08002846 foreach my $compat (@compats) {
2847 my $compat2 = $compat;
Rob Herring185d5662014-06-04 16:12:03 -07002848 $compat2 =~ s/\,[a-zA-Z0-9]*\-/\,<\.\*>\-/;
2849 my $compat3 = $compat;
2850 $compat3 =~ s/\,([a-z]*)[0-9]*\-/\,$1<\.\*>\-/;
2851 `grep -Erq "$compat|$compat2|$compat3" $dt_path`;
Rob Herringbff5da42014-01-23 15:54:51 -08002852 if ( $? >> 8 ) {
2853 WARN("UNDOCUMENTED_DT_STRING",
2854 "DT compatible string \"$compat\" appears un-documented -- check $dt_path\n" . $herecurr);
2855 }
2856
Florian Vaussard4fbf32a2014-04-03 14:49:25 -07002857 next if $compat !~ /^([a-zA-Z0-9\-]+)\,/;
2858 my $vendor = $1;
Florian Vaussardcc933192014-04-03 14:49:27 -07002859 `grep -Eq "^$vendor\\b" $vp_file`;
Rob Herringbff5da42014-01-23 15:54:51 -08002860 if ( $? >> 8 ) {
2861 WARN("UNDOCUMENTED_DT_STRING",
Florian Vaussardcc933192014-04-03 14:49:27 -07002862 "DT compatible string vendor \"$vendor\" appears un-documented -- check $vp_file\n" . $herecurr);
Rob Herringbff5da42014-01-23 15:54:51 -08002863 }
2864 }
2865 }
2866
Andy Whitcroft5368df22008-10-15 22:02:27 -07002867# check we are in a valid source file if not then ignore this hunk
Joe Perchesd6430f72016-12-12 16:46:28 -08002868 next if ($realfile !~ /\.(h|c|s|S|sh|dtsi|dts)$/);
Andy Whitcroft5368df22008-10-15 22:02:27 -07002869
Joe Perches47e0c882015-06-25 15:02:57 -07002870# line length limit (with some exclusions)
2871#
2872# There are a few types of lines that may extend beyond $max_line_length:
2873# logging functions like pr_info that end in a string
2874# lines with a single string
2875# #defines that are a single string
2876#
2877# There are 3 different line length message types:
2878# LONG_LINE_COMMENT a comment starts before but extends beyond $max_linelength
2879# LONG_LINE_STRING a string starts before but extends beyond $max_line_length
2880# LONG_LINE all other lines longer than $max_line_length
2881#
2882# if LONG_LINE is ignored, the other 2 types are also ignored
2883#
2884
Joe Perchesb4749e92015-07-17 16:24:01 -07002885 if ($line =~ /^\+/ && $length > $max_line_length) {
Joe Perches47e0c882015-06-25 15:02:57 -07002886 my $msg_type = "LONG_LINE";
2887
2888 # Check the allowed long line types first
2889
2890 # logging functions that end in a string that starts
2891 # before $max_line_length
2892 if ($line =~ /^\+\s*$logFunctions\s*\(\s*(?:(?:KERN_\S+\s*|[^"]*))?($String\s*(?:|,|\)\s*;)\s*)$/ &&
2893 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2894 $msg_type = "";
2895
2896 # lines with only strings (w/ possible termination)
2897 # #defines with only strings
2898 } elsif ($line =~ /^\+\s*$String\s*(?:\s*|,|\)\s*;)\s*$/ ||
2899 $line =~ /^\+\s*#\s*define\s+\w+\s+$String$/) {
2900 $msg_type = "";
2901
Joe Perchesd560a5f2016-08-02 14:04:31 -07002902 # EFI_GUID is another special case
2903 } elsif ($line =~ /^\+.*\bEFI_GUID\s*\(/) {
2904 $msg_type = "";
2905
Joe Perches47e0c882015-06-25 15:02:57 -07002906 # Otherwise set the alternate message types
2907
2908 # a comment starts before $max_line_length
2909 } elsif ($line =~ /($;[\s$;]*)$/ &&
2910 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2911 $msg_type = "LONG_LINE_COMMENT"
2912
2913 # a quoted string starts before $max_line_length
2914 } elsif ($sline =~ /\s*($String(?:\s*(?:\\|,\s*|\)\s*;\s*))?)$/ &&
2915 length(expand_tabs(substr($line, 1, length($line) - length($1) - 1))) <= $max_line_length) {
2916 $msg_type = "LONG_LINE_STRING"
2917 }
2918
2919 if ($msg_type ne "" &&
2920 (show_type("LONG_LINE") || show_type($msg_type))) {
2921 WARN($msg_type,
2922 "line over $max_line_length characters\n" . $herecurr);
2923 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002924 }
2925
Andy Whitcroft8905a672007-11-28 16:21:06 -08002926# check for adding lines without a newline.
2927 if ($line =~ /^\+/ && defined $lines[$linenr] && $lines[$linenr] =~ /^\\ No newline at end of file/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07002928 WARN("MISSING_EOF_NEWLINE",
2929 "adding a line without newline at end of file\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08002930 }
2931
Mike Frysinger42e41c52009-09-21 17:04:40 -07002932# Blackfin: use hi/lo macros
2933 if ($realfile =~ m@arch/blackfin/.*\.S$@) {
2934 if ($line =~ /\.[lL][[:space:]]*=.*&[[:space:]]*0x[fF][fF][fF][fF]/) {
2935 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002936 ERROR("LO_MACRO",
2937 "use the LO() macro, not (... & 0xFFFF)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002938 }
2939 if ($line =~ /\.[hH][[:space:]]*=.*>>[[:space:]]*16/) {
2940 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07002941 ERROR("HI_MACRO",
2942 "use the HI() macro, not (... >> 16)\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07002943 }
2944 }
2945
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07002946# check we are in a valid source file C or perl if not then ignore this hunk
Geert Uytterhoevende4c9242014-10-13 15:51:46 -07002947 next if ($realfile !~ /\.(h|c|pl|dtsi|dts)$/);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002948
2949# at the beginning of a line any tabs must come first and anything
2950# more than 8 must use tabs.
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08002951 if ($rawline =~ /^\+\s* \t\s*\S/ ||
2952 $rawline =~ /^\+\s* \s*/) {
2953 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07002954 $rpt_cleaners = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07002955 if (ERROR("CODE_INDENT",
2956 "code indent should use tabs where possible\n" . $herevet) &&
2957 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002958 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07002959 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07002960 }
2961
Alberto Panizzo08e44362010-03-05 13:43:54 -08002962# check for space before tabs.
2963 if ($rawline =~ /^\+/ && $rawline =~ / \t/) {
2964 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07002965 if (WARN("SPACE_BEFORE_TAB",
2966 "please, no space before tabs\n" . $herevet) &&
2967 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07002968 while ($fixed[$fixlinenr] =~
Joe Perchesd2207cc2014-10-13 15:51:53 -07002969 s/(^\+.*) {8,8}\t/$1\t\t/) {}
Joe Perches194f66f2014-08-06 16:11:03 -07002970 while ($fixed[$fixlinenr] =~
Joe Perchesc76f4cb2014-01-23 15:54:46 -08002971 s/(^\+.*) +\t/$1\t/) {}
Joe Perches3705ce52013-07-03 15:05:31 -07002972 }
Alberto Panizzo08e44362010-03-05 13:43:54 -08002973 }
2974
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002975# check for && or || at the start of a line
2976 if ($rawline =~ /^\+\s*(&&|\|\|)/) {
2977 CHK("LOGICAL_CONTINUATIONS",
2978 "Logical continuations should be on the previous line\n" . $hereprev);
2979 }
2980
Joe Perchesa91e8992016-05-20 17:04:05 -07002981# check indentation starts on a tab stop
2982 if ($^V && $^V ge 5.10.0 &&
2983 $sline =~ /^\+\t+( +)(?:$c90_Keywords\b|\{\s*$|\}\s*(?:else\b|while\b|\s*$))/) {
2984 my $indent = length($1);
2985 if ($indent % 8) {
2986 if (WARN("TABSTOP",
2987 "Statements should start on a tabstop\n" . $herecurr) &&
2988 $fix) {
2989 $fixed[$fixlinenr] =~ s@(^\+\t+) +@$1 . "\t" x ($indent/8)@e;
2990 }
2991 }
2992 }
2993
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002994# check multi-line statement indentation matches previous line
2995 if ($^V && $^V ge 5.10.0 &&
Joe Perchesfd71f632017-07-10 15:52:30 -07002996 $prevline =~ /^\+([ \t]*)((?:$c90_Keywords(?:\s+if)\s*)|(?:$Declare\s*)?(?:$Ident|\(\s*\*\s*$Ident\s*\))\s*|(?:\*\s*)*$Lval\s*=\s*$Ident\s*)\(.*(\&\&|\|\||,)\s*$/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07002997 $prevline =~ /^\+(\t*)(.*)$/;
2998 my $oldindent = $1;
2999 my $rest = $2;
3000
3001 my $pos = pos_last_openparen($rest);
3002 if ($pos >= 0) {
Joe Perchesb34a26f2012-07-30 14:41:16 -07003003 $line =~ /^(\+| )([ \t]*)/;
3004 my $newindent = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003005
3006 my $goodtabindent = $oldindent .
3007 "\t" x ($pos / 8) .
3008 " " x ($pos % 8);
3009 my $goodspaceindent = $oldindent . " " x $pos;
3010
3011 if ($newindent ne $goodtabindent &&
3012 $newindent ne $goodspaceindent) {
Joe Perches3705ce52013-07-03 15:05:31 -07003013
3014 if (CHK("PARENTHESIS_ALIGNMENT",
3015 "Alignment should match open parenthesis\n" . $hereprev) &&
3016 $fix && $line =~ /^\+/) {
Joe Perches194f66f2014-08-06 16:11:03 -07003017 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003018 s/^\+[ \t]*/\+$goodtabindent/;
3019 }
Joe Perchesd1fe9c02012-03-23 15:02:16 -07003020 }
3021 }
3022 }
3023
Joe Perches6ab3a972015-04-16 12:44:05 -07003024# check for space after cast like "(int) foo" or "(struct foo) bar"
3025# avoid checking a few false positives:
3026# "sizeof(<type>)" or "__alignof__(<type>)"
3027# function pointer declarations like "(*foo)(int) = bar;"
3028# structure definitions like "(struct foo) { 0 };"
3029# multiline macros that define functions
3030# known attributes or the __attribute__ keyword
3031 if ($line =~ /^\+(.*)\(\s*$Type\s*\)([ \t]++)((?![={]|\\$|$Attribute|__attribute__))/ &&
3032 (!defined($1) || $1 !~ /\b(?:sizeof|__alignof__)\s*$/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07003033 if (CHK("SPACING",
Joe Perchesf27c95d2014-08-06 16:10:52 -07003034 "No space is necessary after a cast\n" . $herecurr) &&
Joe Perches3705ce52013-07-03 15:05:31 -07003035 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003036 $fixed[$fixlinenr] =~
Joe Perchesf27c95d2014-08-06 16:10:52 -07003037 s/(\(\s*$Type\s*\))[ \t]+/$1/;
Joe Perches3705ce52013-07-03 15:05:31 -07003038 }
Joe Perchesaad4f612012-03-23 15:02:19 -07003039 }
3040
Joe Perches86406b12015-09-09 15:37:41 -07003041# Block comment styles
3042# Networking with an initial /*
Joe Perches05880602012-10-04 17:13:35 -07003043 if ($realfile =~ m@^(drivers/net/|net/)@ &&
Joe Perchesfdb4bcd2013-07-03 15:05:23 -07003044 $prevrawline =~ /^\+[ \t]*\/\*[ \t]*$/ &&
Joe Perches85ad9782014-04-03 14:49:20 -07003045 $rawline =~ /^\+[ \t]*\*/ &&
3046 $realline > 2) {
Joe Perches05880602012-10-04 17:13:35 -07003047 WARN("NETWORKING_BLOCK_COMMENT_STYLE",
3048 "networking block comments don't use an empty /* line, use /* Comment...\n" . $hereprev);
3049 }
3050
Joe Perches86406b12015-09-09 15:37:41 -07003051# Block comments use * on subsequent lines
3052 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
3053 $prevrawline =~ /^\+.*?\/\*/ && #starting /*
Joe Perchesa605e322013-07-03 15:05:24 -07003054 $prevrawline !~ /\*\/[ \t]*$/ && #no trailing */
Joe Perches61135e92013-09-11 14:23:59 -07003055 $rawline =~ /^\+/ && #line is new
Joe Perchesa605e322013-07-03 15:05:24 -07003056 $rawline !~ /^\+[ \t]*\*/) { #no leading *
Joe Perches86406b12015-09-09 15:37:41 -07003057 WARN("BLOCK_COMMENT_STYLE",
3058 "Block comments use * on subsequent lines\n" . $hereprev);
Joe Perchesa605e322013-07-03 15:05:24 -07003059 }
3060
Joe Perches86406b12015-09-09 15:37:41 -07003061# Block comments use */ on trailing lines
3062 if ($rawline !~ m@^\+[ \t]*\*/[ \t]*$@ && #trailing */
Joe Perchesc24f9f12012-11-08 15:53:29 -08003063 $rawline !~ m@^\+.*/\*.*\*/[ \t]*$@ && #inline /*...*/
3064 $rawline !~ m@^\+.*\*{2,}/[ \t]*$@ && #trailing **/
3065 $rawline =~ m@^\+[ \t]*.+\*\/[ \t]*$@) { #non blank */
Joe Perches86406b12015-09-09 15:37:41 -07003066 WARN("BLOCK_COMMENT_STYLE",
3067 "Block comments use a trailing */ on a separate line\n" . $herecurr);
Joe Perches05880602012-10-04 17:13:35 -07003068 }
3069
Joe Perches08eb9b82016-10-11 13:51:50 -07003070# Block comment * alignment
3071 if ($prevline =~ /$;[ \t]*$/ && #ends in comment
Joe Perchesaf207522016-10-11 13:52:05 -07003072 $line =~ /^\+[ \t]*$;/ && #leading comment
3073 $rawline =~ /^\+[ \t]*\*/ && #leading *
3074 (($prevrawline =~ /^\+.*?\/\*/ && #leading /*
Joe Perches08eb9b82016-10-11 13:51:50 -07003075 $prevrawline !~ /\*\/[ \t]*$/) || #no trailing */
Joe Perchesaf207522016-10-11 13:52:05 -07003076 $prevrawline =~ /^\+[ \t]*\*/)) { #leading *
3077 my $oldindent;
Joe Perches08eb9b82016-10-11 13:51:50 -07003078 $prevrawline =~ m@^\+([ \t]*/?)\*@;
Joe Perchesaf207522016-10-11 13:52:05 -07003079 if (defined($1)) {
3080 $oldindent = expand_tabs($1);
3081 } else {
3082 $prevrawline =~ m@^\+(.*/?)\*@;
3083 $oldindent = expand_tabs($1);
3084 }
Joe Perches08eb9b82016-10-11 13:51:50 -07003085 $rawline =~ m@^\+([ \t]*)\*@;
3086 my $newindent = $1;
Joe Perches08eb9b82016-10-11 13:51:50 -07003087 $newindent = expand_tabs($newindent);
Joe Perchesaf207522016-10-11 13:52:05 -07003088 if (length($oldindent) ne length($newindent)) {
Joe Perches08eb9b82016-10-11 13:51:50 -07003089 WARN("BLOCK_COMMENT_STYLE",
3090 "Block comments should align the * on each line\n" . $hereprev);
3091 }
3092 }
3093
Joe Perches7f619192014-08-06 16:10:39 -07003094# check for missing blank lines after struct/union declarations
3095# with exceptions for various attributes and macros
3096 if ($prevline =~ /^[\+ ]};?\s*$/ &&
3097 $line =~ /^\+/ &&
3098 !($line =~ /^\+\s*$/ ||
3099 $line =~ /^\+\s*EXPORT_SYMBOL/ ||
3100 $line =~ /^\+\s*MODULE_/i ||
3101 $line =~ /^\+\s*\#\s*(?:end|elif|else)/ ||
3102 $line =~ /^\+[a-z_]*init/ ||
3103 $line =~ /^\+\s*(?:static\s+)?[A-Z_]*ATTR/ ||
3104 $line =~ /^\+\s*DECLARE/ ||
3105 $line =~ /^\+\s*__setup/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003106 if (CHK("LINE_SPACING",
3107 "Please use a blank line after function/struct/union/enum declarations\n" . $hereprev) &&
3108 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003109 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003110 }
Joe Perches7f619192014-08-06 16:10:39 -07003111 }
3112
Joe Perches365dd4e2014-08-06 16:10:42 -07003113# check for multiple consecutive blank lines
3114 if ($prevline =~ /^[\+ ]\s*$/ &&
3115 $line =~ /^\+\s*$/ &&
3116 $last_blank_line != ($linenr - 1)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003117 if (CHK("LINE_SPACING",
3118 "Please don't use multiple blank lines\n" . $hereprev) &&
3119 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003120 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003121 }
3122
Joe Perches365dd4e2014-08-06 16:10:42 -07003123 $last_blank_line = $linenr;
3124 }
3125
Joe Perches3b617e32014-04-03 14:49:28 -07003126# check for missing blank lines after declarations
Joe Perches3f7bac02014-06-04 16:12:04 -07003127 if ($sline =~ /^\+\s+\S/ && #Not at char 1
3128 # actual declarations
3129 ($prevline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003130 # function pointer declarations
3131 $prevline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003132 # foo bar; where foo is some local typedef or #define
3133 $prevline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3134 # known declaration macros
3135 $prevline =~ /^\+\s+$declaration_macros/) &&
3136 # for "else if" which can look like "$Ident $Ident"
3137 !($prevline =~ /^\+\s+$c90_Keywords\b/ ||
3138 # other possible extensions of declaration lines
3139 $prevline =~ /(?:$Compare|$Assignment|$Operators)\s*$/ ||
3140 # not starting a section or a macro "\" extended line
3141 $prevline =~ /(?:\{\s*|\\)$/) &&
3142 # looks like a declaration
3143 !($sline =~ /^\+\s+$Declare\s*$Ident\s*[=,;:\[]/ ||
Joe Perches5a4e1fd2014-08-06 16:10:33 -07003144 # function pointer declarations
3145 $sline =~ /^\+\s+$Declare\s*\(\s*\*\s*$Ident\s*\)\s*[=,;:\[\(]/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003146 # foo bar; where foo is some local typedef or #define
3147 $sline =~ /^\+\s+$Ident(?:\s+|\s*\*\s*)$Ident\s*[=,;\[]/ ||
3148 # known declaration macros
3149 $sline =~ /^\+\s+$declaration_macros/ ||
3150 # start of struct or union or enum
Joe Perches3b617e32014-04-03 14:49:28 -07003151 $sline =~ /^\+\s+(?:union|struct|enum|typedef)\b/ ||
Joe Perches3f7bac02014-06-04 16:12:04 -07003152 # start or end of block or continuation of declaration
3153 $sline =~ /^\+\s+(?:$|[\{\}\.\#\"\?\:\(\[])/ ||
3154 # bitfield continuation
3155 $sline =~ /^\+\s+$Ident\s*:\s*\d+\s*[,;]/ ||
3156 # other possible extensions of declaration lines
3157 $sline =~ /^\+\s+\(?\s*(?:$Compare|$Assignment|$Operators)/) &&
3158 # indentation of previous and current line are the same
3159 (($prevline =~ /\+(\s+)\S/) && $sline =~ /^\+$1\S/)) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003160 if (WARN("LINE_SPACING",
3161 "Missing a blank line after declarations\n" . $hereprev) &&
3162 $fix) {
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003163 fix_insert_line($fixlinenr, "\+");
Joe Perchesd752fcc2014-08-06 16:11:05 -07003164 }
Joe Perches3b617e32014-04-03 14:49:28 -07003165 }
3166
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003167# check for spaces at the beginning of a line.
Andy Whitcroft6b4c5be2010-10-26 14:23:11 -07003168# Exceptions:
3169# 1) within comments
3170# 2) indented preprocessor commands
3171# 3) hanging labels
Joe Perches3705ce52013-07-03 15:05:31 -07003172 if ($rawline =~ /^\+ / && $line !~ /^\+ *(?:$;|#|$Ident:)/) {
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003173 my $herevet = "$here\n" . cat_vet($rawline) . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003174 if (WARN("LEADING_SPACE",
3175 "please, no spaces at the start of a line\n" . $herevet) &&
3176 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003177 $fixed[$fixlinenr] =~ s/^\+([ \t]+)/"\+" . tabify($1)/e;
Joe Perches3705ce52013-07-03 15:05:31 -07003178 }
Raffaele Recalcati5f7ddae2010-08-09 17:20:59 -07003179 }
3180
Andy Whitcroftb9ea10d2008-10-15 22:02:24 -07003181# check we are in a valid C source file if not then ignore this hunk
3182 next if ($realfile !~ /\.(h|c)$/);
3183
Joe Perches4dbed762017-05-08 15:55:39 -07003184# check if this appears to be the start function declaration, save the name
3185 if ($sline =~ /^\+\{\s*$/ &&
3186 $prevline =~ /^\+(?:(?:(?:$Storage|$Inline)\s*)*\s*$Type\s*)?($Ident)\(/) {
3187 $context_function = $1;
3188 }
3189
3190# check if this appears to be the end of function declaration
3191 if ($sline =~ /^\+\}\s*$/) {
3192 undef $context_function;
3193 }
3194
Joe Perches032a4c02014-08-06 16:10:29 -07003195# check indentation of any line with a bare else
Joe Perches840080a2014-10-13 15:51:59 -07003196# (but not if it is a multiple line "if (foo) return bar; else return baz;")
Joe Perches032a4c02014-08-06 16:10:29 -07003197# if the previous line is a break or return and is indented 1 tab more...
3198 if ($sline =~ /^\+([\t]+)(?:}[ \t]*)?else(?:[ \t]*{)?\s*$/) {
3199 my $tabs = length($1) + 1;
Joe Perches840080a2014-10-13 15:51:59 -07003200 if ($prevline =~ /^\+\t{$tabs,$tabs}break\b/ ||
3201 ($prevline =~ /^\+\t{$tabs,$tabs}return\b/ &&
3202 defined $lines[$linenr] &&
3203 $lines[$linenr] !~ /^[ \+]\t{$tabs,$tabs}return/)) {
Joe Perches032a4c02014-08-06 16:10:29 -07003204 WARN("UNNECESSARY_ELSE",
3205 "else is not generally useful after a break or return\n" . $hereprev);
3206 }
3207 }
3208
Joe Perchesc00df192014-08-06 16:11:01 -07003209# check indentation of a line with a break;
3210# if the previous line is a goto or return and is indented the same # of tabs
3211 if ($sline =~ /^\+([\t]+)break\s*;\s*$/) {
3212 my $tabs = $1;
3213 if ($prevline =~ /^\+$tabs(?:goto|return)\b/) {
3214 WARN("UNNECESSARY_BREAK",
3215 "break is not useful after a goto or return\n" . $hereprev);
3216 }
3217 }
3218
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003219# check for RCS/CVS revision markers
Andy Whitcroftcf655042008-03-04 14:28:20 -08003220 if ($rawline =~ /^\+.*\$(Revision|Log|Id)(?:\$|)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003221 WARN("CVS_KEYWORD",
3222 "CVS style keyword markers, these will _not_ be updated\n". $herecurr);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003223 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07003224
Mike Frysinger42e41c52009-09-21 17:04:40 -07003225# Blackfin: don't use __builtin_bfin_[cs]sync
3226 if ($line =~ /__builtin_bfin_csync/) {
3227 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003228 ERROR("CSYNC",
3229 "use the CSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003230 }
3231 if ($line =~ /__builtin_bfin_ssync/) {
3232 my $herevet = "$here\n" . cat_vet($line) . "\n";
Joe Perches000d1cc12011-07-25 17:13:25 -07003233 ERROR("SSYNC",
3234 "use the SSYNC() macro in asm/blackfin.h\n" . $herevet);
Mike Frysinger42e41c52009-09-21 17:04:40 -07003235 }
3236
Joe Perches56e77d72013-02-21 16:44:14 -08003237# check for old HOTPLUG __dev<foo> section markings
3238 if ($line =~ /\b(__dev(init|exit)(data|const|))\b/) {
3239 WARN("HOTPLUG_SECTION",
3240 "Using $1 is unnecessary\n" . $herecurr);
3241 }
3242
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003243# Check for potential 'bare' types
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003244 my ($stat, $cond, $line_nr_next, $remain_next, $off_next,
3245 $realline_next);
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003246#print "LINE<$line>\n";
Joe Perchesca819862017-07-10 15:52:13 -07003247 if ($linenr > $suppress_statement &&
Joe Perches1b5539b2013-09-11 14:24:03 -07003248 $realcnt && $sline =~ /.\s*\S/) {
Andy Whitcroft170d3a22008-10-15 22:02:30 -07003249 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07003250 ctx_statement_block($linenr, $realcnt, 0);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003251 $stat =~ s/\n./\n /g;
3252 $cond =~ s/\n./\n /g;
3253
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003254#print "linenr<$linenr> <$stat>\n";
3255 # If this statement has no statement boundaries within
3256 # it there is no point in retrying a statement scan
3257 # until we hit end of it.
3258 my $frag = $stat; $frag =~ s/;+\s*$//;
3259 if ($frag !~ /(?:{|;)/) {
3260#print "skip<$line_nr_next>\n";
3261 $suppress_statement = $line_nr_next;
3262 }
Andy Whitcroftf74bd192012-01-10 15:09:54 -08003263
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003264 # Find the real next line.
3265 $realline_next = $line_nr_next;
3266 if (defined $realline_next &&
3267 (!defined $lines[$realline_next - 1] ||
3268 substr($lines[$realline_next - 1], $off_next) =~ /^\s*$/)) {
3269 $realline_next++;
3270 }
3271
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003272 my $s = $stat;
3273 $s =~ s/{.*$//s;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003274
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003275 # Ignore goto labels.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003276 if ($s =~ /$Ident:\*$/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003277
3278 # Ignore functions being called
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003279 } elsif ($s =~ /^.\s*$Ident\s*\(/s) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003280
Andy Whitcroft463f2862009-09-21 17:04:34 -07003281 } elsif ($s =~ /^.\s*else\b/s) {
3282
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003283 # declarations always start with types
Andy Whitcroftd2506582008-07-23 21:29:09 -07003284 } 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 -07003285 my $type = $1;
3286 $type =~ s/\s+/ /g;
3287 possible($type, "A:" . $s);
3288
Andy Whitcroft8905a672007-11-28 16:21:06 -08003289 # definitions in global scope can only start with types
Andy Whitcrofta6a840622008-10-15 22:02:30 -07003290 } elsif ($s =~ /^.(?:$Storage\s+)?(?:$Inline\s+)?(?:const\s+)?($Ident)\b\s*(?!:)/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003291 possible($1, "B:" . $s);
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003292 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003293
3294 # any (foo ... *) is a pointer cast, and foo is a type
Andy Whitcroft65863862009-01-06 14:41:21 -08003295 while ($s =~ /\(($Ident)(?:\s+$Sparse)*[\s\*]+\s*\)/sg) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003296 possible($1, "C:" . $s);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003297 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003298
3299 # Check for any sort of function declaration.
3300 # int foo(something bar, other baz);
3301 # void (*store_gdt)(x86_descr_ptr *);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07003302 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 -08003303 my ($name_len) = length($1);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003304
Andy Whitcroftcf655042008-03-04 14:28:20 -08003305 my $ctx = $s;
Andy Whitcroft773647a2008-03-28 14:15:58 -07003306 substr($ctx, 0, $name_len + 1, '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08003307 $ctx =~ s/\)[^\)]*$//;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003308
Andy Whitcroft8905a672007-11-28 16:21:06 -08003309 for my $arg (split(/\s*,\s*/, $ctx)) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003310 if ($arg =~ /^(?:const\s+)?($Ident)(?:\s+$Sparse)*\s*\**\s*(:?\b$Ident)?$/s || $arg =~ /^($Ident)$/s) {
Andy Whitcroft8905a672007-11-28 16:21:06 -08003311
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003312 possible($1, "D:" . $s);
Andy Whitcroft8905a672007-11-28 16:21:06 -08003313 }
3314 }
3315 }
3316
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003317 }
3318
Andy Whitcroft653d4872007-06-23 17:16:34 -07003319#
3320# Checks which may be anchored in the context.
3321#
3322
3323# Check for switch () and associated case and default
3324# statements should be at the same indent.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003325 if ($line=~/\bswitch\s*\(.*\)/) {
3326 my $err = '';
3327 my $sep = '';
3328 my @ctx = ctx_block_outer($linenr, $realcnt);
3329 shift(@ctx);
3330 for my $ctx (@ctx) {
3331 my ($clen, $cindent) = line_stats($ctx);
3332 if ($ctx =~ /^\+\s*(case\s+|default:)/ &&
3333 $indent != $cindent) {
3334 $err .= "$sep$ctx\n";
3335 $sep = '';
3336 } else {
3337 $sep = "[...]\n";
3338 }
3339 }
3340 if ($err ne '') {
Joe Perches000d1cc12011-07-25 17:13:25 -07003341 ERROR("SWITCH_CASE_INDENT_LEVEL",
3342 "switch and case should be at the same indent\n$hereline$err");
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003343 }
3344 }
3345
3346# if/while/etc brace do not go on next line, unless defining a do while loop,
3347# or if that brace on the next line is for something else
Joe Perches0fe3dc22014-08-06 16:11:16 -07003348 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 -07003349 my $pre_ctx = "$1$2";
3350
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003351 my ($level, @ctx) = ctx_statement_level($linenr, $realcnt, 0);
Joe Perches8eef05d2012-02-03 15:20:39 -08003352
3353 if ($line =~ /^\+\t{6,}/) {
3354 WARN("DEEP_INDENTATION",
3355 "Too many leading tabs - consider code refactoring\n" . $herecurr);
3356 }
3357
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003358 my $ctx_cnt = $realcnt - $#ctx - 1;
3359 my $ctx = join("\n", @ctx);
3360
Andy Whitcroft548596d2008-07-23 21:29:01 -07003361 my $ctx_ln = $linenr;
3362 my $ctx_skip = $realcnt;
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07003363
Andy Whitcroft548596d2008-07-23 21:29:01 -07003364 while ($ctx_skip > $ctx_cnt || ($ctx_skip == $ctx_cnt &&
3365 defined $lines[$ctx_ln - 1] &&
3366 $lines[$ctx_ln - 1] =~ /^-/)) {
3367 ##print "SKIP<$ctx_skip> CNT<$ctx_cnt>\n";
3368 $ctx_skip-- if (!defined $lines[$ctx_ln - 1] || $lines[$ctx_ln - 1] !~ /^-/);
Andy Whitcroft773647a2008-03-28 14:15:58 -07003369 $ctx_ln++;
3370 }
Andy Whitcroft548596d2008-07-23 21:29:01 -07003371
Andy Whitcroft53210162008-07-23 21:29:03 -07003372 #print "realcnt<$realcnt> ctx_cnt<$ctx_cnt>\n";
3373 #print "pre<$pre_ctx>\nline<$line>\nctx<$ctx>\nnext<$lines[$ctx_ln - 1]>\n";
Andy Whitcroft773647a2008-03-28 14:15:58 -07003374
Joe Perchesd752fcc2014-08-06 16:11:05 -07003375 if ($ctx !~ /{\s*/ && defined($lines[$ctx_ln - 1]) && $lines[$ctx_ln - 1] =~ /^\+\s*{/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003376 ERROR("OPEN_BRACE",
3377 "that open brace { should be on the previous line\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003378 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft00df3442007-06-08 13:47:06 -07003379 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07003380 if ($level == 0 && $pre_ctx !~ /}\s*while\s*\($/ &&
3381 $ctx =~ /\)\s*\;\s*$/ &&
3382 defined $lines[$ctx_ln - 1])
3383 {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003384 my ($nlength, $nindent) = line_stats($lines[$ctx_ln - 1]);
3385 if ($nindent > $indent) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003386 WARN("TRAILING_SEMICOLON",
3387 "trailing semicolon indicates no statements, indent implies otherwise\n" .
Andy Whitcroft01464f32010-10-26 14:23:19 -07003388 "$here\n$ctx\n$rawlines[$ctx_ln - 1]\n");
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07003389 }
3390 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003391 }
3392
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003393# Check relative indent for conditionals and blocks.
Joe Perchesf6950a72017-05-08 15:56:05 -07003394 if ($line =~ /\b(?:(?:if|while|for|(?:[a-z_]+|)for_each[a-z_]+)\s*\(|(?:do|else)\b)/ && $line !~ /^.\s*#/ && $line !~ /\}\s*while\s*/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08003395 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
3396 ctx_statement_block($linenr, $realcnt, 0)
3397 if (!defined $stat);
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003398 my ($s, $c) = ($stat, $cond);
3399
3400 substr($s, 0, length($c), '');
3401
Joe Perches9f5af482015-09-09 15:37:30 -07003402 # remove inline comments
3403 $s =~ s/$;/ /g;
3404 $c =~ s/$;/ /g;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003405
3406 # Find out how long the conditional actually is.
Andy Whitcroft6f779c12008-10-15 22:02:27 -07003407 my @newlines = ($c =~ /\n/gs);
3408 my $cond_lines = 1 + $#newlines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003409
Joe Perches9f5af482015-09-09 15:37:30 -07003410 # Make sure we remove the line prefixes as we have
3411 # none on the first line, and are going to readd them
3412 # where necessary.
3413 $s =~ s/\n./\n/gs;
3414 while ($s =~ /\n\s+\\\n/) {
3415 $cond_lines += $s =~ s/\n\s+\\\n/\n/g;
3416 }
3417
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003418 # We want to check the first line inside the block
3419 # starting at the end of the conditional, so remove:
3420 # 1) any blank line termination
3421 # 2) any opening brace { on end of the line
3422 # 3) any do (...) {
3423 my $continuation = 0;
3424 my $check = 0;
3425 $s =~ s/^.*\bdo\b//;
3426 $s =~ s/^\s*{//;
3427 if ($s =~ s/^\s*\\//) {
3428 $continuation = 1;
3429 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003430 if ($s =~ s/^\s*?\n//) {
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003431 $check = 1;
3432 $cond_lines++;
3433 }
3434
3435 # Also ignore a loop construct at the end of a
3436 # preprocessor statement.
3437 if (($prevline =~ /^.\s*#\s*define\s/ ||
3438 $prevline =~ /\\\s*$/) && $continuation == 0) {
3439 $check = 0;
3440 }
3441
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003442 my $cond_ptr = -1;
Andy Whitcroft740504c2008-10-15 22:02:35 -07003443 $continuation = 0;
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003444 while ($cond_ptr != $cond_lines) {
3445 $cond_ptr = $cond_lines;
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003446
Andy Whitcroftf16fa282008-10-15 22:02:32 -07003447 # If we see an #else/#elif then the code
3448 # is not linear.
3449 if ($s =~ /^\s*\#\s*(?:else|elif)/) {
3450 $check = 0;
3451 }
3452
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003453 # Ignore:
3454 # 1) blank lines, they should be at 0,
3455 # 2) preprocessor lines, and
3456 # 3) labels.
Andy Whitcroft740504c2008-10-15 22:02:35 -07003457 if ($continuation ||
3458 $s =~ /^\s*?\n/ ||
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003459 $s =~ /^\s*#\s*?/ ||
3460 $s =~ /^\s*$Ident\s*:/) {
Andy Whitcroft740504c2008-10-15 22:02:35 -07003461 $continuation = ($s =~ /^.*?\\\n/) ? 1 : 0;
Andy Whitcroft30dad6e2009-09-21 17:04:36 -07003462 if ($s =~ s/^.*?\n//) {
3463 $cond_lines++;
3464 }
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003465 }
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003466 }
3467
3468 my (undef, $sindent) = line_stats("+" . $s);
3469 my $stat_real = raw_line($linenr, $cond_lines);
3470
3471 # Check if either of these lines are modified, else
3472 # this is not this patch's fault.
3473 if (!defined($stat_real) ||
3474 $stat !~ /^\+/ && $stat_real !~ /^\+/) {
3475 $check = 0;
3476 }
3477 if (defined($stat_real) && $cond_lines > 1) {
3478 $stat_real = "[...]\n$stat_real";
3479 }
3480
Andy Whitcroft9bd49ef2008-10-15 22:02:22 -07003481 #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 -07003482
Joe Perches9f5af482015-09-09 15:37:30 -07003483 if ($check && $s ne '' &&
3484 (($sindent % 8) != 0 ||
3485 ($sindent < $indent) ||
Joe Perchesf6950a72017-05-08 15:56:05 -07003486 ($sindent == $indent &&
3487 ($s !~ /^\s*(?:\}|\{|else\b)/)) ||
Joe Perches9f5af482015-09-09 15:37:30 -07003488 ($sindent > $indent + 8))) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003489 WARN("SUSPECT_CODE_INDENT",
3490 "suspect code indent for conditional statements ($indent, $sindent)\n" . $herecurr . "$stat_real\n");
Andy Whitcroft4d001e42008-10-15 22:02:21 -07003491 }
3492 }
3493
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003494 # Track the 'values' across context and added lines.
3495 my $opline = $line; $opline =~ s/^./ /;
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003496 my ($curr_values, $curr_vars) =
3497 annotate_values($opline . "\n", $prev_values);
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003498 $curr_values = $prev_values . $curr_values;
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003499 if ($dbg_values) {
3500 my $outline = $opline; $outline =~ s/\t/ /g;
Andy Whitcroftcf655042008-03-04 14:28:20 -08003501 print "$linenr > .$outline\n";
3502 print "$linenr > $curr_values\n";
Andy Whitcroft1f65f942008-07-23 21:29:10 -07003503 print "$linenr > $curr_vars\n";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08003504 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003505 $prev_values = substr($curr_values, -1);
3506
Andy Whitcroft00df3442007-06-08 13:47:06 -07003507#ignore lines not being added
Joe Perches3705ce52013-07-03 15:05:31 -07003508 next if ($line =~ /^[^\+]/);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003509
Joe Perches11ca40a2016-12-12 16:46:31 -08003510# check for dereferences that span multiple lines
3511 if ($prevline =~ /^\+.*$Lval\s*(?:\.|->)\s*$/ &&
3512 $line =~ /^\+\s*(?!\#\s*(?!define\s+|if))\s*$Lval/) {
3513 $prevline =~ /($Lval\s*(?:\.|->))\s*$/;
3514 my $ref = $1;
3515 $line =~ /^.\s*($Lval)/;
3516 $ref .= $1;
3517 $ref =~ s/\s//g;
3518 WARN("MULTILINE_DEREFERENCE",
3519 "Avoid multiple line dereference - prefer '$ref'\n" . $hereprev);
3520 }
3521
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003522# check for declarations of signed or unsigned without int
Joe Perchesc8447112016-08-02 14:04:42 -07003523 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 -07003524 my $type = $1;
3525 my $var = $2;
Joe Perches207a8e82016-03-15 14:58:06 -07003526 $var = "" if (!defined $var);
3527 if ($type =~ /^(?:(?:$Storage|$Inline|$Attribute)\s+)*((?:un)?signed)((?:\s*\*)*)\s*$/) {
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003528 my $sign = $1;
3529 my $pointer = $2;
3530
3531 $pointer = "" if (!defined $pointer);
3532
3533 if (WARN("UNSPECIFIED_INT",
3534 "Prefer '" . trim($sign) . " int" . rtrim($pointer) . "' to bare use of '$sign" . rtrim($pointer) . "'\n" . $herecurr) &&
3535 $fix) {
3536 my $decl = trim($sign) . " int ";
Joe Perches207a8e82016-03-15 14:58:06 -07003537 my $comp_pointer = $pointer;
3538 $comp_pointer =~ s/\s//g;
3539 $decl .= $comp_pointer;
3540 $decl = rtrim($decl) if ($var eq "");
3541 $fixed[$fixlinenr] =~ s@\b$sign\s*\Q$pointer\E\s*$var\b@$decl$var@;
Joe Perchesa1ce18e2016-03-15 14:58:03 -07003542 }
3543 }
3544 }
3545
Andy Whitcroft653d4872007-06-23 17:16:34 -07003546# TEST: allow direct testing of the type matcher.
Andy Whitcroft7429c692008-07-23 21:29:06 -07003547 if ($dbg_type) {
3548 if ($line =~ /^.\s*$Declare\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003549 ERROR("TEST_TYPE",
3550 "TEST: is type\n" . $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003551 } elsif ($dbg_type > 1 && $line =~ /^.+($Declare)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003552 ERROR("TEST_NOT_TYPE",
3553 "TEST: is not type ($1 is)\n". $herecurr);
Andy Whitcroft7429c692008-07-23 21:29:06 -07003554 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003555 next;
3556 }
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003557# TEST: allow direct testing of the attribute matcher.
3558 if ($dbg_attr) {
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003559 if ($line =~ /^.\s*$Modifier\s*$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003560 ERROR("TEST_ATTR",
3561 "TEST: is attr\n" . $herecurr);
Andy Whitcroft9360b0e2009-02-27 14:03:08 -08003562 } elsif ($dbg_attr > 1 && $line =~ /^.+($Modifier)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003563 ERROR("TEST_NOT_ATTR",
3564 "TEST: is not attr ($1 is)\n". $herecurr);
Andy Whitcrofta1ef2772008-10-15 22:02:17 -07003565 }
3566 next;
3567 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003568
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003569# check for initialisation to aggregates open brace on the next line
Andy Whitcroft99423c22009-10-26 16:50:15 -07003570 if ($line =~ /^.\s*{/ &&
3571 $prevline =~ /(?:^|[^=])=\s*$/) {
Joe Perchesd752fcc2014-08-06 16:11:05 -07003572 if (ERROR("OPEN_BRACE",
3573 "that open brace { should be on the previous line\n" . $hereprev) &&
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003574 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3575 fix_delete_line($fixlinenr - 1, $prevrawline);
3576 fix_delete_line($fixlinenr, $rawline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003577 my $fixedline = $prevrawline;
3578 $fixedline =~ s/\s*=\s*$/ = {/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003579 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003580 $fixedline = $line;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003581 $fixedline =~ s/^(.\s*)\{\s*/$1/;
Joe Perchesf2d7e4d2014-08-06 16:11:07 -07003582 fix_insert_line($fixlinenr, $fixedline);
Joe Perchesd752fcc2014-08-06 16:11:05 -07003583 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003584 }
3585
Andy Whitcroft653d4872007-06-23 17:16:34 -07003586#
3587# Checks which are anchored on the added line.
3588#
3589
3590# check for malformed paths in #include statements (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003591 if ($rawline =~ m{^.\s*\#\s*include\s+[<"](.*)[">]}) {
Andy Whitcroft653d4872007-06-23 17:16:34 -07003592 my $path = $1;
3593 if ($path =~ m{//}) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003594 ERROR("MALFORMED_INCLUDE",
Joe Perches495e9d82012-12-20 15:05:37 -08003595 "malformed #include filename\n" . $herecurr);
3596 }
3597 if ($path =~ "^uapi/" && $realfile =~ m@\binclude/uapi/@) {
3598 ERROR("UAPI_INCLUDE",
3599 "No #include in ...include/uapi/... should use a uapi/ path prefix\n" . $herecurr);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003600 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003601 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003602
3603# no C99 // comments
3604 if ($line =~ m{//}) {
Joe Perches3705ce52013-07-03 15:05:31 -07003605 if (ERROR("C99_COMMENTS",
3606 "do not use C99 // comments\n" . $herecurr) &&
3607 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003608 my $line = $fixed[$fixlinenr];
Joe Perches3705ce52013-07-03 15:05:31 -07003609 if ($line =~ /\/\/(.*)$/) {
3610 my $comment = trim($1);
Joe Perches194f66f2014-08-06 16:11:03 -07003611 $fixed[$fixlinenr] =~ s@\/\/(.*)$@/\* $comment \*/@;
Joe Perches3705ce52013-07-03 15:05:31 -07003612 }
3613 }
Andy Whitcroft00df3442007-06-08 13:47:06 -07003614 }
3615 # Remove C99 comments.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003616 $line =~ s@//.*@@;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07003617 $opline =~ s@//.*@@;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003618
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003619# EXPORT_SYMBOL should immediately follow the thing it is exporting, consider
3620# the whole statement.
3621#print "APW <$lines[$realline_next - 1]>\n";
3622 if (defined $realline_next &&
3623 exists $lines[$realline_next - 1] &&
3624 !defined $suppress_export{$realline_next} &&
3625 ($lines[$realline_next - 1] =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3626 $lines[$realline_next - 1] =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003627 # Handle definitions which produce identifiers with
3628 # a prefix:
3629 # XXX(foo);
3630 # EXPORT_SYMBOL(something_foo);
Andy Whitcroft653d4872007-06-23 17:16:34 -07003631 my $name = $1;
Andy Whitcroft87a53872012-01-10 15:10:04 -08003632 if ($stat =~ /^(?:.\s*}\s*\n)?.([A-Z_]+)\s*\(\s*($Ident)/ &&
Andy Whitcroft3cbf62d2010-10-26 14:23:18 -07003633 $name =~ /^${Ident}_$2/) {
3634#print "FOO C name<$name>\n";
3635 $suppress_export{$realline_next} = 1;
3636
3637 } elsif ($stat !~ /(?:
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003638 \n.}\s*$|
Andy Whitcroft48012052008-10-15 22:02:34 -07003639 ^.DEFINE_$Ident\(\Q$name\E\)|
3640 ^.DECLARE_$Ident\(\Q$name\E\)|
3641 ^.LIST_HEAD\(\Q$name\E\)|
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003642 ^.(?:$Storage\s+)?$Type\s*\(\s*\*\s*\Q$name\E\s*\)\s*\(|
3643 \b\Q$name\E(?:\s+$Attribute)*\s*(?:;|=|\[|\()
Andy Whitcroft48012052008-10-15 22:02:34 -07003644 )/x) {
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003645#print "FOO A<$lines[$realline_next - 1]> stat<$stat> name<$name>\n";
3646 $suppress_export{$realline_next} = 2;
3647 } else {
3648 $suppress_export{$realline_next} = 1;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003649 }
3650 }
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003651 if (!defined $suppress_export{$linenr} &&
3652 $prevline =~ /^.\s*$/ &&
3653 ($line =~ /EXPORT_SYMBOL.*\((.*)\)/ ||
3654 $line =~ /EXPORT_UNUSED_SYMBOL.*\((.*)\)/)) {
3655#print "FOO B <$lines[$linenr - 1]>\n";
3656 $suppress_export{$linenr} = 2;
3657 }
3658 if (defined $suppress_export{$linenr} &&
3659 $suppress_export{$linenr} == 2) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003660 WARN("EXPORT_SYMBOL",
3661 "EXPORT_SYMBOL(foo); should immediately follow its function/variable\n" . $herecurr);
Andy Whitcroft2b474a12009-10-26 16:50:16 -07003662 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003663
Joe Eloff5150bda2010-08-09 17:21:00 -07003664# check for global initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003665 if ($line =~ /^\+$Type\s*$Ident(?:\s+$Modifier)*\s*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003666 if (ERROR("GLOBAL_INITIALISERS",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003667 "do not initialise globals to $1\n" . $herecurr) &&
Joe Perchesd5e616f2013-09-11 14:23:54 -07003668 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003669 $fixed[$fixlinenr] =~ s/(^.$Type\s*$Ident(?:\s+$Modifier)*)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003670 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003671 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003672# check for static initialisers.
Joe Perches6d32f7a2015-11-06 16:31:37 -08003673 if ($line =~ /^\+.*\bstatic\s.*=\s*($zero_initializer)\s*;/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003674 if (ERROR("INITIALISED_STATIC",
Joe Perches6d32f7a2015-11-06 16:31:37 -08003675 "do not initialise statics to $1\n" .
Joe Perchesd5e616f2013-09-11 14:23:54 -07003676 $herecurr) &&
3677 $fix) {
Joe Perches6d32f7a2015-11-06 16:31:37 -08003678 $fixed[$fixlinenr] =~ s/(\bstatic\s.*?)\s*=\s*$zero_initializer\s*;/$1;/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07003679 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003680 }
3681
Joe Perches18130872014-08-06 16:11:22 -07003682# check for misordered declarations of char/short/int/long with signed/unsigned
3683 while ($sline =~ m{(\b$TypeMisordered\b)}g) {
3684 my $tmp = trim($1);
3685 WARN("MISORDERED_TYPE",
3686 "type '$tmp' should be specified in [[un]signed] [short|int|long|long long] order\n" . $herecurr);
3687 }
3688
Joe Perchescb710ec2010-10-26 14:23:20 -07003689# check for static const char * arrays.
3690 if ($line =~ /\bstatic\s+const\s+char\s*\*\s*(\w+)\s*\[\s*\]\s*=\s*/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003691 WARN("STATIC_CONST_CHAR_ARRAY",
3692 "static const char * array should probably be static const char * const\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003693 $herecurr);
3694 }
3695
3696# check for static char foo[] = "bar" declarations.
3697 if ($line =~ /\bstatic\s+char\s+(\w+)\s*\[\s*\]\s*=\s*"/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003698 WARN("STATIC_CONST_CHAR_ARRAY",
3699 "static char array declaration should probably be static const char\n" .
Joe Perchescb710ec2010-10-26 14:23:20 -07003700 $herecurr);
3701 }
3702
Joe Perchesab7e23f2015-04-16 12:44:22 -07003703# check for const <foo> const where <foo> is not a pointer or array type
3704 if ($sline =~ /\bconst\s+($BasicType)\s+const\b/) {
3705 my $found = $1;
3706 if ($sline =~ /\bconst\s+\Q$found\E\s+const\b\s*\*/) {
3707 WARN("CONST_CONST",
3708 "'const $found const *' should probably be 'const $found * const'\n" . $herecurr);
3709 } elsif ($sline !~ /\bconst\s+\Q$found\E\s+const\s+\w+\s*\[/) {
3710 WARN("CONST_CONST",
3711 "'const $found const' should probably be 'const $found'\n" . $herecurr);
3712 }
3713 }
3714
Joe Perches9b0fa602014-04-03 14:49:18 -07003715# check for non-global char *foo[] = {"bar", ...} declarations.
3716 if ($line =~ /^.\s+(?:static\s+|const\s+)?char\s+\*\s*\w+\s*\[\s*\]\s*=\s*\{/) {
3717 WARN("STATIC_CONST_CHAR_ARRAY",
3718 "char * array declaration might be better as static const\n" .
3719 $herecurr);
3720 }
3721
Joe Perchesb598b672015-04-16 12:44:36 -07003722# check for sizeof(foo)/sizeof(foo[0]) that could be ARRAY_SIZE(foo)
3723 if ($line =~ m@\bsizeof\s*\(\s*($Lval)\s*\)@) {
3724 my $array = $1;
3725 if ($line =~ m@\b(sizeof\s*\(\s*\Q$array\E\s*\)\s*/\s*sizeof\s*\(\s*\Q$array\E\s*\[\s*0\s*\]\s*\))@) {
3726 my $array_div = $1;
3727 if (WARN("ARRAY_SIZE",
3728 "Prefer ARRAY_SIZE($array)\n" . $herecurr) &&
3729 $fix) {
3730 $fixed[$fixlinenr] =~ s/\Q$array_div\E/ARRAY_SIZE($array)/;
3731 }
3732 }
3733 }
3734
Joe Perchesb36190c2014-01-27 17:07:18 -08003735# check for function declarations without arguments like "int foo()"
3736 if ($line =~ /(\b$Type\s+$Ident)\s*\(\s*\)/) {
3737 if (ERROR("FUNCTION_WITHOUT_ARGS",
3738 "Bad function definition - $1() should probably be $1(void)\n" . $herecurr) &&
3739 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003740 $fixed[$fixlinenr] =~ s/(\b($Type)\s+($Ident))\s*\(\s*\)/$2 $3(void)/;
Joe Perchesb36190c2014-01-27 17:07:18 -08003741 }
3742 }
3743
Andy Whitcroft653d4872007-06-23 17:16:34 -07003744# check for new typedefs, only function parameters and sparse annotations
3745# make sense.
3746 if ($line =~ /\btypedef\s/ &&
Andy Whitcroft80545762009-01-06 14:41:26 -08003747 $line !~ /\btypedef\s+$Type\s*\(\s*\*?$Ident\s*\)\s*\(/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07003748 $line !~ /\btypedef\s+$Type\s+$Ident\s*\(/ &&
Andy Whitcroft8ed22ca2008-10-15 22:02:32 -07003749 $line !~ /\b$typeTypedefs\b/ &&
Michael S. Tsirkin46d832f2016-12-11 06:29:58 +02003750 $line !~ /\b__bitwise\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003751 WARN("NEW_TYPEDEFS",
3752 "do not add new typedefs\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003753 }
3754
3755# * goes on variable not on type
Andy Whitcroft65863862009-01-06 14:41:21 -08003756 # (char*[ const])
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003757 while ($line =~ m{(\($NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)\))}g) {
3758 #print "AA<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003759 my ($ident, $from, $to) = ($1, $2, $2);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003760
Andy Whitcroft65863862009-01-06 14:41:21 -08003761 # Should start with a space.
3762 $to =~ s/^(\S)/ $1/;
3763 # Should not end with a space.
3764 $to =~ s/\s+$//;
3765 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003766 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003767 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003768
Joe Perches3705ce52013-07-03 15:05:31 -07003769## print "1: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft65863862009-01-06 14:41:21 -08003770 if ($from ne $to) {
Joe Perches3705ce52013-07-03 15:05:31 -07003771 if (ERROR("POINTER_LOCATION",
3772 "\"(foo$from)\" should be \"(foo$to)\"\n" . $herecurr) &&
3773 $fix) {
3774 my $sub_from = $ident;
3775 my $sub_to = $ident;
3776 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003777 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003778 s@\Q$sub_from\E@$sub_to@;
3779 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003780 }
Andy Whitcroftbfcb2cc2012-01-10 15:10:15 -08003781 }
3782 while ($line =~ m{(\b$NonptrType(\s*(?:$Modifier\b\s*|\*\s*)+)($Ident))}g) {
3783 #print "BB<$1>\n";
Joe Perches3705ce52013-07-03 15:05:31 -07003784 my ($match, $from, $to, $ident) = ($1, $2, $2, $3);
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07003785
Andy Whitcroft65863862009-01-06 14:41:21 -08003786 # Should start with a space.
3787 $to =~ s/^(\S)/ $1/;
3788 # Should not end with a space.
3789 $to =~ s/\s+$//;
3790 # '*'s should not have spaces between.
Andy Whitcroftf9a0b3d2009-01-15 13:51:05 -08003791 while ($to =~ s/\*\s+\*/\*\*/) {
Andy Whitcroft65863862009-01-06 14:41:21 -08003792 }
3793 # Modifiers should have spaces.
3794 $to =~ s/(\b$Modifier$)/$1 /;
3795
Joe Perches3705ce52013-07-03 15:05:31 -07003796## print "2: from<$from> to<$to> ident<$ident>\n";
Andy Whitcroft667026e2009-02-27 14:03:08 -08003797 if ($from ne $to && $ident !~ /^$Modifier$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07003798 if (ERROR("POINTER_LOCATION",
3799 "\"foo${from}bar\" should be \"foo${to}bar\"\n" . $herecurr) &&
3800 $fix) {
3801
3802 my $sub_from = $match;
3803 my $sub_to = $match;
3804 $sub_to =~ s/\Q$from\E/$to/;
Joe Perches194f66f2014-08-06 16:11:03 -07003805 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003806 s@\Q$sub_from\E@$sub_to@;
3807 }
Andy Whitcroft65863862009-01-06 14:41:21 -08003808 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003809 }
3810
Joe Perches9d3e3c72015-09-09 15:37:27 -07003811# avoid BUG() or BUG_ON()
3812 if ($line =~ /\b(?:BUG|BUG_ON)\b/) {
3813 my $msg_type = \&WARN;
3814 $msg_type = \&CHK if ($file);
3815 &{$msg_type}("AVOID_BUG",
3816 "Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()\n" . $herecurr);
3817 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003818
Joe Perches9d3e3c72015-09-09 15:37:27 -07003819# avoid LINUX_VERSION_CODE
Andy Whitcroft8905a672007-11-28 16:21:06 -08003820 if ($line =~ /\bLINUX_VERSION_CODE\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003821 WARN("LINUX_VERSION_CODE",
3822 "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 -08003823 }
3824
Joe Perches17441222011-06-15 15:08:17 -07003825# check for uses of printk_ratelimit
3826 if ($line =~ /\bprintk_ratelimit\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003827 WARN("PRINTK_RATELIMITED",
Joe Perches101ee682015-02-13 14:38:54 -08003828 "Prefer printk_ratelimited or pr_<level>_ratelimited to printk_ratelimit\n" . $herecurr);
Joe Perches17441222011-06-15 15:08:17 -07003829 }
3830
Andy Whitcroft00df3442007-06-08 13:47:06 -07003831# printk should use KERN_* levels. Note that follow on printk's on the
3832# same line do not need a level, so we use the current block context
3833# to try and find and validate the current printk. In summary the current
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003834# printk includes all preceding printk's which have no newline on the end.
Andy Whitcroft00df3442007-06-08 13:47:06 -07003835# we assume the first bad printk is the one to report.
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07003836 if ($line =~ /\bprintk\((?!KERN_)\s*"/) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07003837 my $ok = 0;
3838 for (my $ln = $linenr - 1; $ln >= $first_line; $ln--) {
3839 #print "CHECK<$lines[$ln - 1]\n";
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003840 # we have a preceding printk if it ends
Andy Whitcroft00df3442007-06-08 13:47:06 -07003841 # with "\n" ignore it, else it is to blame
3842 if ($lines[$ln - 1] =~ m{\bprintk\(}) {
3843 if ($rawlines[$ln - 1] !~ m{\\n"}) {
3844 $ok = 1;
3845 }
3846 last;
3847 }
3848 }
3849 if ($ok == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07003850 WARN("PRINTK_WITHOUT_KERN_LEVEL",
3851 "printk() should include KERN_ facility level\n" . $herecurr);
Andy Whitcroft00df3442007-06-08 13:47:06 -07003852 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003853 }
3854
Joe Perches243f3802012-05-31 16:26:09 -07003855 if ($line =~ /\bprintk\s*\(\s*KERN_([A-Z]+)/) {
3856 my $orig = $1;
3857 my $level = lc($orig);
3858 $level = "warn" if ($level eq "warning");
Joe Perches8f26b832012-10-04 17:13:32 -07003859 my $level2 = $level;
3860 $level2 = "dbg" if ($level eq "debug");
Joe Perches243f3802012-05-31 16:26:09 -07003861 WARN("PREFER_PR_LEVEL",
Yogesh Chaudharidaa8b052014-04-03 14:49:23 -07003862 "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 -07003863 }
3864
3865 if ($line =~ /\bpr_warning\s*\(/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07003866 if (WARN("PREFER_PR_LEVEL",
3867 "Prefer pr_warn(... to pr_warning(...\n" . $herecurr) &&
3868 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003869 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07003870 s/\bpr_warning\b/pr_warn/;
3871 }
Joe Perches243f3802012-05-31 16:26:09 -07003872 }
3873
Joe Perchesdc139312013-02-21 16:44:13 -08003874 if ($line =~ /\bdev_printk\s*\(\s*KERN_([A-Z]+)/) {
3875 my $orig = $1;
3876 my $level = lc($orig);
3877 $level = "warn" if ($level eq "warning");
3878 $level = "dbg" if ($level eq "debug");
3879 WARN("PREFER_DEV_LEVEL",
3880 "Prefer dev_$level(... to dev_printk(KERN_$orig, ...\n" . $herecurr);
3881 }
3882
Andy Lutomirski91c9afa2015-04-16 12:44:44 -07003883# ENOSYS means "bad syscall nr" and nothing else. This will have a small
3884# number of false positives, but assembly files are not checked, so at
3885# least the arch entry code will not trigger this warning.
3886 if ($line =~ /\bENOSYS\b/) {
3887 WARN("ENOSYS",
3888 "ENOSYS means 'invalid syscall nr' and nothing else\n" . $herecurr);
3889 }
3890
Andy Whitcroft653d4872007-06-23 17:16:34 -07003891# function brace can't be on same line, except for #defines of do while,
3892# or if closed on same line
Joe Perches8d182472014-08-06 16:11:12 -07003893 if (($line=~/$Type\s*$Ident\(.*\).*\s*{/) and
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07003894 !($line=~/\#\s*define.*do\s\{/) and !($line=~/}/)) {
Joe Perches8d182472014-08-06 16:11:12 -07003895 if (ERROR("OPEN_BRACE",
3896 "open brace '{' following function declarations go on the next line\n" . $herecurr) &&
3897 $fix) {
3898 fix_delete_line($fixlinenr, $rawline);
3899 my $fixed_line = $rawline;
3900 $fixed_line =~ /(^..*$Type\s*$Ident\(.*\)\s*){(.*)$/;
3901 my $line1 = $1;
3902 my $line2 = $2;
3903 fix_insert_line($fixlinenr, ltrim($line1));
3904 fix_insert_line($fixlinenr, "\+{");
3905 if ($line2 !~ /^\s*$/) {
3906 fix_insert_line($fixlinenr, "\+\t" . trim($line2));
3907 }
3908 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07003909 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07003910
Andy Whitcroft8905a672007-11-28 16:21:06 -08003911# open braces for enum, union and struct go on the same line.
3912 if ($line =~ /^.\s*{/ &&
3913 $prevline =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident)?\s*$/) {
Joe Perches8d182472014-08-06 16:11:12 -07003914 if (ERROR("OPEN_BRACE",
3915 "open brace '{' following $1 go on the same line\n" . $hereprev) &&
3916 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
3917 fix_delete_line($fixlinenr - 1, $prevrawline);
3918 fix_delete_line($fixlinenr, $rawline);
3919 my $fixedline = rtrim($prevrawline) . " {";
3920 fix_insert_line($fixlinenr, $fixedline);
3921 $fixedline = $rawline;
Cyril Bur8d81ae02017-07-10 15:52:21 -07003922 $fixedline =~ s/^(.\s*)\{\s*/$1\t/;
Joe Perches8d182472014-08-06 16:11:12 -07003923 if ($fixedline !~ /^\+\s*$/) {
3924 fix_insert_line($fixlinenr, $fixedline);
3925 }
3926 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08003927 }
3928
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003929# missing space after union, struct or enum definition
Joe Perches3705ce52013-07-03 15:05:31 -07003930 if ($line =~ /^.\s*(?:typedef\s+)?(enum|union|struct)(?:\s+$Ident){1,2}[=\{]/) {
3931 if (WARN("SPACING",
3932 "missing space after $1 definition\n" . $herecurr) &&
3933 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07003934 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07003935 s/^(.\s*(?:typedef\s+)?(?:enum|union|struct)(?:\s+$Ident){1,2})([=\{])/$1 $2/;
3936 }
Andy Whitcroft0c73b4e2010-10-26 14:23:15 -07003937 }
3938
Joe Perches31070b52014-01-23 15:54:49 -08003939# Function pointer declarations
3940# check spacing between type, funcptr, and args
3941# canonical declaration is "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003942 if ($line =~ /^.\s*($Declare)\((\s*)\*(\s*)($Ident)(\s*)\)(\s*)\(/) {
Joe Perches31070b52014-01-23 15:54:49 -08003943 my $declare = $1;
3944 my $pre_pointer_space = $2;
3945 my $post_pointer_space = $3;
3946 my $funcname = $4;
3947 my $post_funcname_space = $5;
3948 my $pre_args_space = $6;
3949
Joe Perches91f72e92014-04-03 14:49:12 -07003950# the $Declare variable will capture all spaces after the type
3951# so check it for a missing trailing missing space but pointer return types
3952# don't need a space so don't warn for those.
3953 my $post_declare_space = "";
3954 if ($declare =~ /(\s+)$/) {
3955 $post_declare_space = $1;
3956 $declare = rtrim($declare);
3957 }
3958 if ($declare !~ /\*$/ && $post_declare_space =~ /^$/) {
Joe Perches31070b52014-01-23 15:54:49 -08003959 WARN("SPACING",
3960 "missing space after return type\n" . $herecurr);
Joe Perches91f72e92014-04-03 14:49:12 -07003961 $post_declare_space = " ";
Joe Perches31070b52014-01-23 15:54:49 -08003962 }
3963
3964# unnecessary space "type (*funcptr)(args...)"
Joe Perches91f72e92014-04-03 14:49:12 -07003965# This test is not currently implemented because these declarations are
3966# equivalent to
3967# int foo(int bar, ...)
3968# and this is form shouldn't/doesn't generate a checkpatch warning.
3969#
3970# elsif ($declare =~ /\s{2,}$/) {
3971# WARN("SPACING",
3972# "Multiple spaces after return type\n" . $herecurr);
3973# }
Joe Perches31070b52014-01-23 15:54:49 -08003974
3975# unnecessary space "type ( *funcptr)(args...)"
3976 if (defined $pre_pointer_space &&
3977 $pre_pointer_space =~ /^\s/) {
3978 WARN("SPACING",
3979 "Unnecessary space after function pointer open parenthesis\n" . $herecurr);
3980 }
3981
3982# unnecessary space "type (* funcptr)(args...)"
3983 if (defined $post_pointer_space &&
3984 $post_pointer_space =~ /^\s/) {
3985 WARN("SPACING",
3986 "Unnecessary space before function pointer name\n" . $herecurr);
3987 }
3988
3989# unnecessary space "type (*funcptr )(args...)"
3990 if (defined $post_funcname_space &&
3991 $post_funcname_space =~ /^\s/) {
3992 WARN("SPACING",
3993 "Unnecessary space after function pointer name\n" . $herecurr);
3994 }
3995
3996# unnecessary space "type (*funcptr) (args...)"
3997 if (defined $pre_args_space &&
3998 $pre_args_space =~ /^\s/) {
3999 WARN("SPACING",
4000 "Unnecessary space before function pointer arguments\n" . $herecurr);
4001 }
4002
4003 if (show_type("SPACING") && $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004004 $fixed[$fixlinenr] =~
Joe Perches91f72e92014-04-03 14:49:12 -07004005 s/^(.\s*)$Declare\s*\(\s*\*\s*$Ident\s*\)\s*\(/$1 . $declare . $post_declare_space . '(*' . $funcname . ')('/ex;
Joe Perches31070b52014-01-23 15:54:49 -08004006 }
4007 }
4008
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004009# check for spacing round square brackets; allowed:
4010# 1. with a type on the left -- int [] a;
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004011# 2. at the beginning of a line for slice initialisers -- [0...10] = 5,
4012# 3. inside a curly brace -- = { [0...10] = 5 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004013 while ($line =~ /(.*?\s)\[/g) {
4014 my ($where, $prefix) = ($-[1], $1);
4015 if ($prefix !~ /$Type\s+$/ &&
Andy Whitcroftfe2a7db2008-10-15 22:02:15 -07004016 ($where != 0 || $prefix !~ /^.\s+$/) &&
Andy Whitcroftdaebc532012-03-23 15:02:17 -07004017 $prefix !~ /[{,]\s+$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004018 if (ERROR("BRACKET_SPACE",
4019 "space prohibited before open square bracket '['\n" . $herecurr) &&
4020 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004021 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004022 s/^(\+.*?)\s+\[/$1\[/;
4023 }
Andy Whitcroft8d31cfc2008-07-23 21:29:02 -07004024 }
4025 }
4026
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004027# check for spaces between functions and their parentheses.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004028 while ($line =~ /($Ident)\s+\(/g) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004029 my $name = $1;
Andy Whitcroft773647a2008-03-28 14:15:58 -07004030 my $ctx_before = substr($line, 0, $-[1]);
4031 my $ctx = "$ctx_before$name";
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004032
4033 # Ignore those directives where spaces _are_ permitted.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004034 if ($name =~ /^(?:
4035 if|for|while|switch|return|case|
4036 volatile|__volatile__|
4037 __attribute__|format|__extension__|
4038 asm|__asm__)$/x)
4039 {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004040 # cpp #define statements have non-optional spaces, ie
4041 # if there is a space between the name and the open
4042 # parenthesis it is simply not a parameter group.
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004043 } elsif ($ctx_before =~ /^.\s*\#\s*define\s*$/) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004044
4045 # cpp #elif statement condition may start with a (
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004046 } elsif ($ctx =~ /^.\s*\#\s*elif\s*$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004047
4048 # If this whole things ends with a type its most
4049 # likely a typedef for a function.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004050 } elsif ($ctx =~ /$Type$/) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004051
4052 } else {
Joe Perches3705ce52013-07-03 15:05:31 -07004053 if (WARN("SPACING",
4054 "space prohibited between function name and open parenthesis '('\n" . $herecurr) &&
4055 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004056 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004057 s/\b$name\s+\(/$name\(/;
4058 }
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004059 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004060 }
Eric Nelson9a4cad42012-05-31 16:26:09 -07004061
Andy Whitcroft653d4872007-06-23 17:16:34 -07004062# Check operator spacing.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004063 if (!($line=~/\#\s*include/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004064 my $fixed_line = "";
4065 my $line_fixed = 0;
4066
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004067 my $ops = qr{
4068 <<=|>>=|<=|>=|==|!=|
4069 \+=|-=|\*=|\/=|%=|\^=|\|=|&=|
4070 =>|->|<<|>>|<|>|=|!|~|
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004071 &&|\|\||,|\^|\+\+|--|&|\||\+|-|\*|\/|%|
Joe Perches84731622013-11-12 15:10:05 -08004072 \?:|\?|:
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004073 }x;
Andy Whitcroftcf655042008-03-04 14:28:20 -08004074 my @elements = split(/($ops|;)/, $opline);
Joe Perches3705ce52013-07-03 15:05:31 -07004075
4076## print("element count: <" . $#elements . ">\n");
4077## foreach my $el (@elements) {
4078## print("el: <$el>\n");
4079## }
4080
4081 my @fix_elements = ();
Andy Whitcroft00df3442007-06-08 13:47:06 -07004082 my $off = 0;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004083
Joe Perches3705ce52013-07-03 15:05:31 -07004084 foreach my $el (@elements) {
4085 push(@fix_elements, substr($rawline, $off, length($el)));
4086 $off += length($el);
4087 }
4088
4089 $off = 0;
4090
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004091 my $blank = copy_spacing($opline);
Joe Perchesb34c6482013-09-11 14:24:01 -07004092 my $last_after = -1;
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004093
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004094 for (my $n = 0; $n < $#elements; $n += 2) {
Joe Perches3705ce52013-07-03 15:05:31 -07004095
4096 my $good = $fix_elements[$n] . $fix_elements[$n + 1];
4097
4098## print("n: <$n> good: <$good>\n");
4099
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004100 $off += length($elements[$n]);
4101
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004102 # Pick up the preceding and succeeding characters.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004103 my $ca = substr($opline, 0, $off);
4104 my $cc = '';
4105 if (length($opline) >= ($off + length($elements[$n + 1]))) {
4106 $cc = substr($opline, $off + length($elements[$n + 1]));
4107 }
4108 my $cb = "$ca$;$cc";
4109
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004110 my $a = '';
4111 $a = 'V' if ($elements[$n] ne '');
4112 $a = 'W' if ($elements[$n] =~ /\s$/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004113 $a = 'C' if ($elements[$n] =~ /$;$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004114 $a = 'B' if ($elements[$n] =~ /(\[|\()$/);
4115 $a = 'O' if ($elements[$n] eq '');
Andy Whitcroft773647a2008-03-28 14:15:58 -07004116 $a = 'E' if ($ca =~ /^\s*$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004117
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004118 my $op = $elements[$n + 1];
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004119
4120 my $c = '';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004121 if (defined $elements[$n + 2]) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004122 $c = 'V' if ($elements[$n + 2] ne '');
4123 $c = 'W' if ($elements[$n + 2] =~ /^\s/);
Andy Whitcroftcf655042008-03-04 14:28:20 -08004124 $c = 'C' if ($elements[$n + 2] =~ /^$;/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004125 $c = 'B' if ($elements[$n + 2] =~ /^(\)|\]|;)/);
4126 $c = 'O' if ($elements[$n + 2] eq '');
Andy Whitcroft8b1b3372009-01-06 14:41:27 -08004127 $c = 'E' if ($elements[$n + 2] =~ /^\s*\\$/);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004128 } else {
4129 $c = 'E';
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004130 }
4131
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004132 my $ctx = "${a}x${c}";
4133
4134 my $at = "(ctx:$ctx)";
4135
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004136 my $ptr = substr($blank, 0, $off) . "^";
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004137 my $hereptr = "$hereline$ptr\n";
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004138
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004139 # Pull out the value of this operator.
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07004140 my $op_type = substr($curr_values, $off + 1, 1);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004141
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004142 # Get the full operator variant.
4143 my $opv = $op . substr($curr_vars, $off, 1);
4144
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004145 # Ignore operators passed as parameters.
4146 if ($op_type ne 'V' &&
Sam Bobroffd7fe8062015-04-16 12:44:39 -07004147 $ca =~ /\s$/ && $cc =~ /^\s*[,\)]/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004148
Andy Whitcroftcf655042008-03-04 14:28:20 -08004149# # Ignore comments
4150# } elsif ($op =~ /^$;+$/) {
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004151
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004152 # ; should have either the end of line or a space or \ after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004153 } elsif ($op eq ';') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004154 if ($ctx !~ /.x[WEBC]/ &&
4155 $cc !~ /^\\/ && $cc !~ /^;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004156 if (ERROR("SPACING",
4157 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004158 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004159 $line_fixed = 1;
4160 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004161 }
4162
4163 # // is a comment
4164 } elsif ($op eq '//') {
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004165
Joe Perchesb00e4812014-04-03 14:49:33 -07004166 # : when part of a bitfield
4167 } elsif ($opv eq ':B') {
4168 # skip the bitfield test for now
4169
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004170 # No spaces for:
4171 # ->
Joe Perchesb00e4812014-04-03 14:49:33 -07004172 } elsif ($op eq '->') {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004173 if ($ctx =~ /Wx.|.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004174 if (ERROR("SPACING",
4175 "spaces prohibited around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004176 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004177 if (defined $fix_elements[$n + 2]) {
4178 $fix_elements[$n + 2] =~ s/^\s+//;
4179 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004180 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004181 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004182 }
4183
Joe Perches23810972014-12-10 15:51:32 -08004184 # , must not have a space before and must have a space on the right.
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004185 } elsif ($op eq ',') {
Joe Perches23810972014-12-10 15:51:32 -08004186 my $rtrim_before = 0;
4187 my $space_after = 0;
4188 if ($ctx =~ /Wx./) {
4189 if (ERROR("SPACING",
4190 "space prohibited before that '$op' $at\n" . $hereptr)) {
4191 $line_fixed = 1;
4192 $rtrim_before = 1;
4193 }
4194 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08004195 if ($ctx !~ /.x[WEC]/ && $cc !~ /^}/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004196 if (ERROR("SPACING",
4197 "space required after that '$op' $at\n" . $hereptr)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004198 $line_fixed = 1;
Joe Perchesb34c6482013-09-11 14:24:01 -07004199 $last_after = $n;
Joe Perches23810972014-12-10 15:51:32 -08004200 $space_after = 1;
4201 }
4202 }
4203 if ($rtrim_before || $space_after) {
4204 if ($rtrim_before) {
4205 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
4206 } else {
4207 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
4208 }
4209 if ($space_after) {
4210 $good .= " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004211 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004212 }
4213
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004214 # '*' as part of a type definition -- reported already.
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004215 } elsif ($opv eq '*_') {
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004216 #warn "'*' is part of type\n";
4217
4218 # unary operators should have a space before and
4219 # none after. May be left adjacent to another
4220 # unary operator, or a cast
4221 } elsif ($op eq '!' || $op eq '~' ||
Andy Whitcroft74048ed2008-07-23 21:29:10 -07004222 $opv eq '*U' || $opv eq '-U' ||
Andy Whitcroft0d413862008-10-15 22:02:16 -07004223 $opv eq '&U' || $opv eq '&&U') {
Andy Whitcroftcf655042008-03-04 14:28:20 -08004224 if ($ctx !~ /[WEBC]x./ && $ca !~ /(?:\)|!|~|\*|-|\&|\||\+\+|\-\-|\{)$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004225 if (ERROR("SPACING",
4226 "space required before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004227 if ($n != $last_after + 2) {
4228 $good = $fix_elements[$n] . " " . ltrim($fix_elements[$n + 1]);
4229 $line_fixed = 1;
4230 }
Joe Perches3705ce52013-07-03 15:05:31 -07004231 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004232 }
Andy Whitcrofta3340b32009-02-27 14:03:07 -08004233 if ($op eq '*' && $cc =~/\s*$Modifier\b/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004234 # A unary '*' may be const
4235
4236 } elsif ($ctx =~ /.xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004237 if (ERROR("SPACING",
4238 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004239 $good = $fix_elements[$n] . rtrim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004240 if (defined $fix_elements[$n + 2]) {
4241 $fix_elements[$n + 2] =~ s/^\s+//;
4242 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004243 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004244 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004245 }
4246
4247 # unary ++ and unary -- are allowed no space on one side.
4248 } elsif ($op eq '++' or $op eq '--') {
Andy Whitcroft773647a2008-03-28 14:15:58 -07004249 if ($ctx !~ /[WEOBC]x[^W]/ && $ctx !~ /[^W]x[WOBEC]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004250 if (ERROR("SPACING",
4251 "space required one side of that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004252 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]) . " ";
Joe Perches3705ce52013-07-03 15:05:31 -07004253 $line_fixed = 1;
4254 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004255 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004256 if ($ctx =~ /Wx[BE]/ ||
4257 ($ctx =~ /Wx./ && $cc =~ /^;/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004258 if (ERROR("SPACING",
4259 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004260 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004261 $line_fixed = 1;
4262 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004263 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004264 if ($ctx =~ /ExW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004265 if (ERROR("SPACING",
4266 "space prohibited after that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004267 $good = $fix_elements[$n] . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004268 if (defined $fix_elements[$n + 2]) {
4269 $fix_elements[$n + 2] =~ s/^\s+//;
4270 }
Joe Perchesb34c6482013-09-11 14:24:01 -07004271 $line_fixed = 1;
Joe Perches3705ce52013-07-03 15:05:31 -07004272 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07004273 }
4274
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004275 # << and >> may either have or not have spaces both sides
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004276 } elsif ($op eq '<<' or $op eq '>>' or
4277 $op eq '&' or $op eq '^' or $op eq '|' or
4278 $op eq '+' or $op eq '-' or
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004279 $op eq '*' or $op eq '/' or
4280 $op eq '%')
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004281 {
Joe Perchesd2e025f2015-02-13 14:38:57 -08004282 if ($check) {
4283 if (defined $fix_elements[$n + 2] && $ctx !~ /[EW]x[EW]/) {
4284 if (CHK("SPACING",
4285 "spaces preferred around that '$op' $at\n" . $hereptr)) {
4286 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4287 $fix_elements[$n + 2] =~ s/^\s+//;
4288 $line_fixed = 1;
4289 }
4290 } elsif (!defined $fix_elements[$n + 2] && $ctx !~ /Wx[OE]/) {
4291 if (CHK("SPACING",
4292 "space preferred before that '$op' $at\n" . $hereptr)) {
4293 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]);
4294 $line_fixed = 1;
4295 }
4296 }
4297 } elsif ($ctx =~ /Wx[^WCE]|[^WCE]xW/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004298 if (ERROR("SPACING",
4299 "need consistent spacing around '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004300 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4301 if (defined $fix_elements[$n + 2]) {
4302 $fix_elements[$n + 2] =~ s/^\s+//;
4303 }
Joe Perches3705ce52013-07-03 15:05:31 -07004304 $line_fixed = 1;
4305 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004306 }
4307
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004308 # A colon needs no spaces before when it is
4309 # terminating a case value or a label.
4310 } elsif ($opv eq ':C' || $opv eq ':L') {
4311 if ($ctx =~ /Wx./) {
Joe Perches3705ce52013-07-03 15:05:31 -07004312 if (ERROR("SPACING",
4313 "space prohibited before that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004314 $good = rtrim($fix_elements[$n]) . trim($fix_elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004315 $line_fixed = 1;
4316 }
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004317 }
4318
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004319 # All the others need spaces both sides.
Andy Whitcroftcf655042008-03-04 14:28:20 -08004320 } elsif ($ctx !~ /[EWC]x[CWE]/) {
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004321 my $ok = 0;
4322
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004323 # Ignore email addresses <foo@bar>
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004324 if (($op eq '<' &&
4325 $cc =~ /^\S+\@\S+>/) ||
4326 ($op eq '>' &&
4327 $ca =~ /<\S+\@\S+$/))
4328 {
4329 $ok = 1;
4330 }
4331
Joe Perchese0df7e12015-04-16 12:44:53 -07004332 # for asm volatile statements
4333 # ignore a colon with another
4334 # colon immediately before or after
4335 if (($op eq ':') &&
4336 ($ca =~ /:$/ || $cc =~ /^:/)) {
4337 $ok = 1;
4338 }
4339
Joe Perches84731622013-11-12 15:10:05 -08004340 # messages are ERROR, but ?: are CHK
Andy Whitcroft1f65f942008-07-23 21:29:10 -07004341 if ($ok == 0) {
Joe Perches84731622013-11-12 15:10:05 -08004342 my $msg_type = \&ERROR;
4343 $msg_type = \&CHK if (($op eq '?:' || $op eq '?' || $op eq ':') && $ctx =~ /VxV/);
4344
4345 if (&{$msg_type}("SPACING",
4346 "spaces required around that '$op' $at\n" . $hereptr)) {
Joe Perchesb34c6482013-09-11 14:24:01 -07004347 $good = rtrim($fix_elements[$n]) . " " . trim($fix_elements[$n + 1]) . " ";
4348 if (defined $fix_elements[$n + 2]) {
4349 $fix_elements[$n + 2] =~ s/^\s+//;
4350 }
Joe Perches3705ce52013-07-03 15:05:31 -07004351 $line_fixed = 1;
4352 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004353 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004354 }
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004355 $off += length($elements[$n + 1]);
Joe Perches3705ce52013-07-03 15:05:31 -07004356
4357## print("n: <$n> GOOD: <$good>\n");
4358
4359 $fixed_line = $fixed_line . $good;
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004360 }
Joe Perches3705ce52013-07-03 15:05:31 -07004361
4362 if (($#elements % 2) == 0) {
4363 $fixed_line = $fixed_line . $fix_elements[$#elements];
4364 }
4365
Joe Perches194f66f2014-08-06 16:11:03 -07004366 if ($fix && $line_fixed && $fixed_line ne $fixed[$fixlinenr]) {
4367 $fixed[$fixlinenr] = $fixed_line;
Joe Perches3705ce52013-07-03 15:05:31 -07004368 }
4369
4370
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004371 }
4372
Joe Perches786b6322013-07-03 15:05:32 -07004373# check for whitespace before a non-naked semicolon
Joe Perchesd2e248e2014-01-23 15:54:41 -08004374 if ($line =~ /^\+.*\S\s+;\s*$/) {
Joe Perches786b6322013-07-03 15:05:32 -07004375 if (WARN("SPACING",
4376 "space prohibited before semicolon\n" . $herecurr) &&
4377 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004378 1 while $fixed[$fixlinenr] =~
Joe Perches786b6322013-07-03 15:05:32 -07004379 s/^(\+.*\S)\s+;/$1;/;
4380 }
4381 }
4382
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004383# check for multiple assignments
4384 if ($line =~ /^.\s*$Lval\s*=\s*$Lval\s*=(?!=)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004385 CHK("MULTIPLE_ASSIGNMENTS",
4386 "multiple assignments should be avoided\n" . $herecurr);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004387 }
4388
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004389## # check for multiple declarations, allowing for a function declaration
4390## # continuation.
4391## if ($line =~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Ident.*/ &&
4392## $line !~ /^.\s*$Type\s+$Ident(?:\s*=[^,{]*)?\s*,\s*$Type\s*$Ident.*/) {
4393##
4394## # Remove any bracketed sections to ensure we do not
4395## # falsly report the parameters of functions.
4396## my $ln = $line;
4397## while ($ln =~ s/\([^\(\)]*\)//g) {
4398## }
4399## if ($ln =~ /,/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004400## WARN("MULTIPLE_DECLARATION",
4401## "declaring multiple variables together should be avoided\n" . $herecurr);
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004402## }
4403## }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07004404
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004405#need space before brace following if, while, etc
Geyslan G. Bem6b8c69e2016-03-15 14:58:09 -07004406 if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\)\{/) ||
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004407 $line =~ /do\{/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004408 if (ERROR("SPACING",
4409 "space required before the open brace '{'\n" . $herecurr) &&
4410 $fix) {
Cyril Bur8d81ae02017-07-10 15:52:21 -07004411 $fixed[$fixlinenr] =~ s/^(\+.*(?:do|\)))\{/$1 {/;
Joe Perches3705ce52013-07-03 15:05:31 -07004412 }
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004413 }
4414
Joe Perchesc4a62ef2013-07-03 15:05:28 -07004415## # check for blank lines before declarations
4416## if ($line =~ /^.\t+$Type\s+$Ident(?:\s*=.*)?;/ &&
4417## $prevrawline =~ /^.\s*$/) {
4418## WARN("SPACING",
4419## "No blank lines before declarations\n" . $hereprev);
4420## }
4421##
4422
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004423# closing brace should have a space following it when it has anything
4424# on the line
4425 if ($line =~ /}(?!(?:,|;|\)))\S/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004426 if (ERROR("SPACING",
4427 "space required after that close brace '}'\n" . $herecurr) &&
4428 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004429 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004430 s/}((?!(?:,|;|\)))\S)/} $1/;
4431 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004432 }
4433
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004434# check spacing on square brackets
4435 if ($line =~ /\[\s/ && $line !~ /\[\s*$/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004436 if (ERROR("SPACING",
4437 "space prohibited after that open square bracket '['\n" . $herecurr) &&
4438 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004439 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004440 s/\[\s+/\[/;
4441 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004442 }
4443 if ($line =~ /\s\]/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004444 if (ERROR("SPACING",
4445 "space prohibited before that close square bracket ']'\n" . $herecurr) &&
4446 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004447 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004448 s/\s+\]/\]/;
4449 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004450 }
4451
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004452# check spacing on parentheses
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07004453 if ($line =~ /\(\s/ && $line !~ /\(\s*(?:\\)?$/ &&
4454 $line !~ /for\s*\(\s+;/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004455 if (ERROR("SPACING",
4456 "space prohibited after that open parenthesis '('\n" . $herecurr) &&
4457 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004458 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004459 s/\(\s+/\(/;
4460 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004461 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004462 if ($line =~ /(\s+)\)/ && $line !~ /^.\s*\)/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004463 $line !~ /for\s*\(.*;\s+\)/ &&
4464 $line !~ /:\s+\)/) {
Joe Perches3705ce52013-07-03 15:05:31 -07004465 if (ERROR("SPACING",
4466 "space prohibited before that close parenthesis ')'\n" . $herecurr) &&
4467 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004468 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004469 s/\s+\)/\)/;
4470 }
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07004471 }
4472
Joe Perchese2826fd2014-08-06 16:10:48 -07004473# check unnecessary parentheses around addressof/dereference single $Lvals
4474# ie: &(foo->bar) should be &foo->bar and *(foo->bar) should be *foo->bar
4475
4476 while ($line =~ /(?:[^&]&\s*|\*)\(\s*($Ident\s*(?:$Member\s*)+)\s*\)/g) {
Joe Perchesea4acbb2014-12-10 15:51:51 -08004477 my $var = $1;
4478 if (CHK("UNNECESSARY_PARENTHESES",
4479 "Unnecessary parentheses around $var\n" . $herecurr) &&
4480 $fix) {
4481 $fixed[$fixlinenr] =~ s/\(\s*\Q$var\E\s*\)/$var/;
4482 }
4483 }
4484
4485# check for unnecessary parentheses around function pointer uses
4486# ie: (foo->bar)(); should be foo->bar();
4487# but not "if (foo->bar) (" to avoid some false positives
4488 if ($line =~ /(\bif\s*|)(\(\s*$Ident\s*(?:$Member\s*)+\))[ \t]*\(/ && $1 !~ /^if/) {
4489 my $var = $2;
4490 if (CHK("UNNECESSARY_PARENTHESES",
4491 "Unnecessary parentheses around function pointer $var\n" . $herecurr) &&
4492 $fix) {
4493 my $var2 = deparenthesize($var);
4494 $var2 =~ s/\s//g;
4495 $fixed[$fixlinenr] =~ s/\Q$var\E/$var2/;
4496 }
4497 }
Joe Perchese2826fd2014-08-06 16:10:48 -07004498
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004499#goto labels aren't indented, allow a single space however
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07004500 if ($line=~/^.\s+[A-Za-z\d_]+:(?![0-9]+)/ and
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004501 !($line=~/^. [A-Za-z\d_]+:/) and !($line=~/^.\s+default:/)) {
Joe Perches3705ce52013-07-03 15:05:31 -07004502 if (WARN("INDENTED_LABEL",
4503 "labels should not be indented\n" . $herecurr) &&
4504 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004505 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004506 s/^(.)\s+/$1/;
4507 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004508 }
4509
Joe Perches5b9553a2014-04-03 14:49:21 -07004510# return is not a function
Joe Perches507e5142013-11-12 15:10:13 -08004511 if (defined($stat) && $stat =~ /^.\s*return(\s*)\(/s) {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004512 my $spacing = $1;
Joe Perches507e5142013-11-12 15:10:13 -08004513 if ($^V && $^V ge 5.10.0 &&
Joe Perches5b9553a2014-04-03 14:49:21 -07004514 $stat =~ /^.\s*return\s*($balanced_parens)\s*;\s*$/) {
4515 my $value = $1;
4516 $value = deparenthesize($value);
4517 if ($value =~ m/^\s*$FuncArg\s*(?:\?|$)/) {
4518 ERROR("RETURN_PARENTHESES",
4519 "return is not a function, parentheses are not required\n" . $herecurr);
4520 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004521 } elsif ($spacing !~ /\s+/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004522 ERROR("SPACING",
4523 "space required before the open parenthesis '('\n" . $herecurr);
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004524 }
4525 }
Joe Perches507e5142013-11-12 15:10:13 -08004526
Joe Perchesb43ae212014-06-23 13:22:07 -07004527# unnecessary return in a void function
4528# at end-of-function, with the previous line a single leading tab, then return;
4529# and the line before that not a goto label target like "out:"
4530 if ($sline =~ /^[ \+]}\s*$/ &&
4531 $prevline =~ /^\+\treturn\s*;\s*$/ &&
4532 $linenr >= 3 &&
4533 $lines[$linenr - 3] =~ /^[ +]/ &&
4534 $lines[$linenr - 3] !~ /^[ +]\s*$Ident\s*:/) {
Joe Perches9819cf22014-06-04 16:12:09 -07004535 WARN("RETURN_VOID",
Joe Perchesb43ae212014-06-23 13:22:07 -07004536 "void function return statements are not generally useful\n" . $hereprev);
4537 }
Joe Perches9819cf22014-06-04 16:12:09 -07004538
Joe Perches189248d2014-01-23 15:54:47 -08004539# if statements using unnecessary parentheses - ie: if ((foo == bar))
4540 if ($^V && $^V ge 5.10.0 &&
4541 $line =~ /\bif\s*((?:\(\s*){2,})/) {
4542 my $openparens = $1;
4543 my $count = $openparens =~ tr@\(@\(@;
4544 my $msg = "";
4545 if ($line =~ /\bif\s*(?:\(\s*){$count,$count}$LvalOrFunc\s*($Compare)\s*$LvalOrFunc(?:\s*\)){$count,$count}/) {
4546 my $comp = $4; #Not $1 because of $LvalOrFunc
4547 $msg = " - maybe == should be = ?" if ($comp eq "==");
4548 WARN("UNNECESSARY_PARENTHESES",
4549 "Unnecessary parentheses$msg\n" . $herecurr);
4550 }
4551 }
4552
Joe Perchesc5595fa2015-09-09 15:37:58 -07004553# comparisons with a constant or upper case identifier on the left
4554# avoid cases like "foo + BAR < baz"
4555# only fix matches surrounded by parentheses to avoid incorrect
4556# conversions like "FOO < baz() + 5" being "misfixed" to "baz() > FOO + 5"
4557 if ($^V && $^V ge 5.10.0 &&
4558 $line =~ /^\+(.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
4559 my $lead = $1;
4560 my $const = $2;
4561 my $comp = $3;
4562 my $to = $4;
4563 my $newcomp = $comp;
Joe Perchesf39e1762016-05-20 17:04:02 -07004564 if ($lead !~ /(?:$Operators|\.)\s*$/ &&
Joe Perchesc5595fa2015-09-09 15:37:58 -07004565 $to !~ /^(?:Constant|[A-Z_][A-Z0-9_]*)$/ &&
4566 WARN("CONSTANT_COMPARISON",
4567 "Comparisons should place the constant on the right side of the test\n" . $herecurr) &&
4568 $fix) {
4569 if ($comp eq "<") {
4570 $newcomp = ">";
4571 } elsif ($comp eq "<=") {
4572 $newcomp = ">=";
4573 } elsif ($comp eq ">") {
4574 $newcomp = "<";
4575 } elsif ($comp eq ">=") {
4576 $newcomp = "<=";
4577 }
4578 $fixed[$fixlinenr] =~ s/\(\s*\Q$const\E\s*$Compare\s*\Q$to\E\s*\)/($to $newcomp $const)/;
4579 }
4580 }
4581
Joe Perchesf34e4a42015-04-16 12:44:19 -07004582# Return of what appears to be an errno should normally be negative
4583 if ($sline =~ /\breturn(?:\s*\(+\s*|\s+)(E[A-Z]+)(?:\s*\)+\s*|\s*)[;:,]/) {
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004584 my $name = $1;
4585 if ($name ne 'EOF' && $name ne 'ERROR') {
Joe Perches000d1cc12011-07-25 17:13:25 -07004586 WARN("USE_NEGATIVE_ERRNO",
Joe Perchesf34e4a42015-04-16 12:44:19 -07004587 "return of an errno should typically be negative (ie: return -$1)\n" . $herecurr);
Andy Whitcroft53a3c442010-10-26 14:23:14 -07004588 }
4589 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004590
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004591# Need a space before open parenthesis after if, while etc
Joe Perches3705ce52013-07-03 15:05:31 -07004592 if ($line =~ /\b(if|while|for|switch)\(/) {
4593 if (ERROR("SPACING",
4594 "space required before the open parenthesis '('\n" . $herecurr) &&
4595 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004596 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07004597 s/\b(if|while|for|switch)\(/$1 \(/;
4598 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004599 }
4600
Andy Whitcroftf5fe35d2008-07-23 21:29:03 -07004601# Check for illegal assignment in if conditional -- and check for trailing
4602# statements after the conditional.
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004603 if ($line =~ /do\s*(?!{)/) {
Andy Whitcroft3e469cd2012-01-10 15:10:01 -08004604 ($stat, $cond, $line_nr_next, $remain_next, $off_next) =
4605 ctx_statement_block($linenr, $realcnt, 0)
4606 if (!defined $stat);
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004607 my ($stat_next) = ctx_statement_block($line_nr_next,
4608 $remain_next, $off_next);
4609 $stat_next =~ s/\n./\n /g;
4610 ##print "stat<$stat> stat_next<$stat_next>\n";
4611
4612 if ($stat_next =~ /^\s*while\b/) {
4613 # If the statement carries leading newlines,
4614 # then count those as offsets.
4615 my ($whitespace) =
4616 ($stat_next =~ /^((?:\s*\n[+-])*\s*)/s);
4617 my $offset =
4618 statement_rawlines($whitespace) - 1;
4619
4620 $suppress_whiletrailers{$line_nr_next +
4621 $offset} = 1;
4622 }
4623 }
4624 if (!defined $suppress_whiletrailers{$linenr} &&
Joe Perchesc11230f2013-11-21 14:31:57 -08004625 defined($stat) && defined($cond) &&
Andy Whitcroft170d3a22008-10-15 22:02:30 -07004626 $line =~ /\b(?:if|while|for)\s*\(/ && $line !~ /^.\s*#/) {
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07004627 my ($s, $c) = ($stat, $cond);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004628
Andy Whitcroftb53c8e12009-01-06 14:41:29 -08004629 if ($c =~ /\bif\s*\(.*[^<>!=]=[^=].*/s) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004630 ERROR("ASSIGN_IN_IF",
4631 "do not use assignment in if condition\n" . $herecurr);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004632 }
4633
4634 # Find out what is on the end of the line after the
4635 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004636 substr($s, 0, length($c), '');
Andy Whitcroft8905a672007-11-28 16:21:06 -08004637 $s =~ s/\n.*//g;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004638 $s =~ s/$;//g; # Remove any comments
Andy Whitcroft53210162008-07-23 21:29:03 -07004639 if (length($c) && $s !~ /^\s*{?\s*\\*\s*$/ &&
4640 $c !~ /}\s*while\s*/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07004641 {
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004642 # Find out how long the conditional actually is.
4643 my @newlines = ($c =~ /\n/gs);
4644 my $cond_lines = 1 + $#newlines;
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004645 my $stat_real = '';
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004646
Hidetoshi Seto42bdf742010-03-05 13:43:50 -08004647 $stat_real = raw_line($linenr, $cond_lines)
4648 . "\n" if ($cond_lines);
Andy Whitcroftbb44ad32008-10-15 22:02:34 -07004649 if (defined($stat_real) && $cond_lines > 1) {
4650 $stat_real = "[...]\n$stat_real";
4651 }
4652
Joe Perches000d1cc12011-07-25 17:13:25 -07004653 ERROR("TRAILING_STATEMENTS",
4654 "trailing statements should be on next line\n" . $herecurr . $stat_real);
Andy Whitcroft8905a672007-11-28 16:21:06 -08004655 }
4656 }
4657
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004658# Check for bitwise tests written as boolean
4659 if ($line =~ /
4660 (?:
4661 (?:\[|\(|\&\&|\|\|)
4662 \s*0[xX][0-9]+\s*
4663 (?:\&\&|\|\|)
4664 |
4665 (?:\&\&|\|\|)
4666 \s*0[xX][0-9]+\s*
4667 (?:\&\&|\|\||\)|\])
4668 )/x)
4669 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004670 WARN("HEXADECIMAL_BOOLEAN_TEST",
4671 "boolean test with hexadecimal, perhaps just 1 \& or \|?\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004672 }
4673
Andy Whitcroft8905a672007-11-28 16:21:06 -08004674# if and else should not have general statements after it
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004675 if ($line =~ /^.\s*(?:}\s*)?else\b(.*)/) {
4676 my $s = $1;
4677 $s =~ s/$;//g; # Remove any comments
4678 if ($s !~ /^\s*(?:\sif|(?:{|)\s*\\?\s*$)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004679 ERROR("TRAILING_STATEMENTS",
4680 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08004681 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004682 }
Andy Whitcroft39667782009-01-15 13:51:06 -08004683# if should not continue a brace
4684 if ($line =~ /}\s*if\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07004685 ERROR("TRAILING_STATEMENTS",
Rasmus Villemoes048b1232014-08-06 16:10:37 -07004686 "trailing statements should be on next line (or did you mean 'else if'?)\n" .
Andy Whitcroft39667782009-01-15 13:51:06 -08004687 $herecurr);
4688 }
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004689# case and default should not have general statements after them
4690 if ($line =~ /^.\s*(?:case\s*.*|default\s*):/g &&
4691 $line !~ /\G(?:
Andy Whitcroft3fef12d2008-10-15 22:02:36 -07004692 (?:\s*$;*)(?:\s*{)?(?:\s*$;*)(?:\s*\\)?\s*$|
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004693 \s*return\s+
4694 )/xg)
4695 {
Joe Perches000d1cc12011-07-25 17:13:25 -07004696 ERROR("TRAILING_STATEMENTS",
4697 "trailing statements should be on next line\n" . $herecurr);
Andy Whitcrofta1080bf2008-10-15 22:02:25 -07004698 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004699
4700 # Check for }<nl>else {, these must be at the same
4701 # indent level to be relevant to each other.
Joe Perches8b8856f2014-08-06 16:11:14 -07004702 if ($prevline=~/}\s*$/ and $line=~/^.\s*else\s*/ &&
4703 $previndent == $indent) {
4704 if (ERROR("ELSE_AFTER_BRACE",
4705 "else should follow close brace '}'\n" . $hereprev) &&
4706 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4707 fix_delete_line($fixlinenr - 1, $prevrawline);
4708 fix_delete_line($fixlinenr, $rawline);
4709 my $fixedline = $prevrawline;
4710 $fixedline =~ s/}\s*$//;
4711 if ($fixedline !~ /^\+\s*$/) {
4712 fix_insert_line($fixlinenr, $fixedline);
4713 }
4714 $fixedline = $rawline;
4715 $fixedline =~ s/^(.\s*)else/$1} else/;
4716 fix_insert_line($fixlinenr, $fixedline);
4717 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004718 }
4719
Joe Perches8b8856f2014-08-06 16:11:14 -07004720 if ($prevline=~/}\s*$/ and $line=~/^.\s*while\s*/ &&
4721 $previndent == $indent) {
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004722 my ($s, $c) = ctx_statement_block($linenr, $realcnt, 0);
4723
4724 # Find out what is on the end of the line after the
4725 # conditional.
Andy Whitcroft773647a2008-03-28 14:15:58 -07004726 substr($s, 0, length($c), '');
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004727 $s =~ s/\n.*//g;
4728
4729 if ($s =~ /^\s*;/) {
Joe Perches8b8856f2014-08-06 16:11:14 -07004730 if (ERROR("WHILE_AFTER_BRACE",
4731 "while should follow close brace '}'\n" . $hereprev) &&
4732 $fix && $prevline =~ /^\+/ && $line =~ /^\+/) {
4733 fix_delete_line($fixlinenr - 1, $prevrawline);
4734 fix_delete_line($fixlinenr, $rawline);
4735 my $fixedline = $prevrawline;
4736 my $trailing = $rawline;
4737 $trailing =~ s/^\+//;
4738 $trailing = trim($trailing);
4739 $fixedline =~ s/}\s*$/} $trailing/;
4740 fix_insert_line($fixlinenr, $fixedline);
4741 }
Andy Whitcroftc2fdda02008-02-08 04:20:54 -08004742 }
4743 }
4744
Joe Perches95e2c602013-07-03 15:05:20 -07004745#Specific variable tests
Joe Perches323c1262012-12-17 16:02:07 -08004746 while ($line =~ m{($Constant|$Lval)}g) {
4747 my $var = $1;
Joe Perches95e2c602013-07-03 15:05:20 -07004748
4749#gcc binary extension
4750 if ($var =~ /^$Binary$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07004751 if (WARN("GCC_BINARY_CONSTANT",
4752 "Avoid gcc v4.3+ binary constant extension: <$var>\n" . $herecurr) &&
4753 $fix) {
4754 my $hexval = sprintf("0x%x", oct($var));
Joe Perches194f66f2014-08-06 16:11:03 -07004755 $fixed[$fixlinenr] =~
Joe Perchesd5e616f2013-09-11 14:23:54 -07004756 s/\b$var\b/$hexval/;
4757 }
Joe Perches95e2c602013-07-03 15:05:20 -07004758 }
4759
4760#CamelCase
Joe Perches807bd262013-07-03 15:05:22 -07004761 if ($var !~ /^$Constant$/ &&
Joe Perchesbe797942013-07-03 15:05:20 -07004762 $var =~ /[A-Z][a-z]|[a-z][A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004763#Ignore Page<foo> variants
Joe Perches807bd262013-07-03 15:05:22 -07004764 $var !~ /^(?:Clear|Set|TestClear|TestSet|)Page[A-Z]/ &&
Joe Perches22735ce2013-07-03 15:05:33 -07004765#Ignore SI style variants like nS, mV and dB (ie: max_uV, regulator_min_uA_show)
Julius Wernerf5123572014-12-10 15:51:54 -08004766 $var !~ /^(?:[a-z_]*?)_?[a-z][A-Z](?:_[a-z_]+)?$/ &&
4767#Ignore some three character SI units explicitly, like MiB and KHz
4768 $var !~ /^(?:[a-z_]*?)_?(?:[KMGT]iB|[KMGT]?Hz)(?:_[a-z_]+)?$/) {
Joe Perches7e781f62013-09-11 14:23:55 -07004769 while ($var =~ m{($Ident)}g) {
4770 my $word = $1;
4771 next if ($word !~ /[A-Z][a-z]|[a-z][A-Z]/);
Joe Perchesd8b07712013-11-12 15:10:06 -08004772 if ($check) {
4773 seed_camelcase_includes();
4774 if (!$file && !$camelcase_file_seeded) {
4775 seed_camelcase_file($realfile);
4776 $camelcase_file_seeded = 1;
4777 }
4778 }
Joe Perches7e781f62013-09-11 14:23:55 -07004779 if (!defined $camelcase{$word}) {
4780 $camelcase{$word} = 1;
4781 CHK("CAMELCASE",
4782 "Avoid CamelCase: <$word>\n" . $herecurr);
4783 }
Joe Perches34456862013-07-03 15:05:34 -07004784 }
Joe Perches323c1262012-12-17 16:02:07 -08004785 }
4786 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004787
4788#no spaces allowed after \ in define
Joe Perchesd5e616f2013-09-11 14:23:54 -07004789 if ($line =~ /\#\s*define.*\\\s+$/) {
4790 if (WARN("WHITESPACE_AFTER_LINE_CONTINUATION",
4791 "Whitespace after \\ makes next lines useless\n" . $herecurr) &&
4792 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07004793 $fixed[$fixlinenr] =~ s/\s+$//;
Joe Perchesd5e616f2013-09-11 14:23:54 -07004794 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004795 }
4796
Fabian Frederick0e212e02015-04-16 12:44:25 -07004797# warn if <asm/foo.h> is #included and <linux/foo.h> is available and includes
4798# itself <asm/foo.h> (uses RAW line)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004799 if ($tree && $rawline =~ m{^.\s*\#\s*include\s*\<asm\/(.*)\.h\>}) {
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004800 my $file = "$1.h";
4801 my $checkfile = "include/linux/$file";
4802 if (-f "$root/$checkfile" &&
4803 $realfile ne $checkfile &&
Wolfram Sang7840a942010-08-09 17:20:57 -07004804 $1 !~ /$allowed_asm_includes/)
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004805 {
Fabian Frederick0e212e02015-04-16 12:44:25 -07004806 my $asminclude = `grep -Ec "#include\\s+<asm/$file>" $root/$checkfile`;
4807 if ($asminclude > 0) {
4808 if ($realfile =~ m{^arch/}) {
4809 CHK("ARCH_INCLUDE_LINUX",
4810 "Consider using #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4811 } else {
4812 WARN("INCLUDE_LINUX",
4813 "Use #include <linux/$file> instead of <asm/$file>\n" . $herecurr);
4814 }
Andy Whitcrofte09dec42008-10-15 22:02:20 -07004815 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004816 }
4817 }
4818
Andy Whitcroft653d4872007-06-23 17:16:34 -07004819# multi-statement macros should be enclosed in a do while loop, grab the
4820# first statement and ensure its the whole macro if its not enclosed
Andy Whitcroftcf655042008-03-04 14:28:20 -08004821# in a known good container
Andy Whitcroftb8f96a32008-07-23 21:29:07 -07004822 if ($realfile !~ m@/vmlinux.lds.h$@ &&
4823 $line =~ /^.\s*\#\s*define\s*$Ident(\()?/) {
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004824 my $ln = $linenr;
4825 my $cnt = $realcnt;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004826 my ($off, $dstat, $dcond, $rest);
4827 my $ctx = '';
Joe Perches08a28432014-10-13 15:51:55 -07004828 my $has_flow_statement = 0;
4829 my $has_arg_concat = 0;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004830 ($dstat, $dcond, $ln, $cnt, $off) =
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004831 ctx_statement_block($linenr, $realcnt, 0);
4832 $ctx = $dstat;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004833 #print "dstat<$dstat> dcond<$dcond> cnt<$cnt> off<$off>\n";
Andy Whitcrofta3bb97a2008-07-23 21:29:00 -07004834 #print "LINE<$lines[$ln-1]> len<" . length($lines[$ln-1]) . "\n";
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004835
Joe Perches08a28432014-10-13 15:51:55 -07004836 $has_flow_statement = 1 if ($ctx =~ /\b(goto|return)\b/);
Joe Perches62e15a62016-01-20 14:59:18 -08004837 $has_arg_concat = 1 if ($ctx =~ /\#\#/ && $ctx !~ /\#\#\s*(?:__VA_ARGS__|args)\b/);
Joe Perches08a28432014-10-13 15:51:55 -07004838
Joe Perchesf59b64b2016-10-11 13:52:08 -07004839 $dstat =~ s/^.\s*\#\s*define\s+$Ident(\([^\)]*\))?\s*//;
4840 my $define_args = $1;
4841 my $define_stmt = $dstat;
4842 my @def_args = ();
4843
4844 if (defined $define_args && $define_args ne "") {
4845 $define_args = substr($define_args, 1, length($define_args) - 2);
4846 $define_args =~ s/\s*//g;
4847 @def_args = split(",", $define_args);
4848 }
4849
Andy Whitcroft292f1a92008-07-23 21:29:11 -07004850 $dstat =~ s/$;//g;
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004851 $dstat =~ s/\\\n.//g;
4852 $dstat =~ s/^\s*//s;
4853 $dstat =~ s/\s*$//s;
4854
4855 # Flatten any parentheses and braces
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004856 while ($dstat =~ s/\([^\(\)]*\)/1/ ||
4857 $dstat =~ s/\{[^\{\}]*\}/1/ ||
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004858 $dstat =~ s/.\[[^\[\]]*\]/1/)
Andy Whitcroftbf30d6e2008-10-15 22:02:33 -07004859 {
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07004860 }
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004861
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004862 # Flatten any obvious string concatentation.
Joe Perches33acb542015-06-25 15:02:54 -07004863 while ($dstat =~ s/($String)\s*$Ident/$1/ ||
4864 $dstat =~ s/$Ident\s*($String)/$1/)
Andy Whitcrofte45bab82012-03-23 15:02:18 -07004865 {
4866 }
4867
Joe Perches42e15292016-03-15 14:58:01 -07004868 # Make asm volatile uses seem like a generic function
4869 $dstat =~ s/\b_*asm_*\s+_*volatile_*\b/asm_volatile/g;
4870
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004871 my $exceptions = qr{
4872 $Declare|
4873 module_param_named|
Kees Cooka0a0a7a2012-10-04 17:13:38 -07004874 MODULE_PARM_DESC|
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004875 DECLARE_PER_CPU|
4876 DEFINE_PER_CPU|
Andy Whitcroft383099f2009-01-06 14:41:18 -08004877 __typeof__\(|
Stefani Seibold22fd2d32010-03-05 13:43:52 -08004878 union|
4879 struct|
Andy Whitcroftea71a0a2009-09-21 17:04:38 -07004880 \.$Ident\s*=\s*|
Vladimir Zapolskiy6b10df42016-01-20 14:59:21 -08004881 ^\"|\"$|
4882 ^\[
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07004883 }x;
Andy Whitcroft5eaa20b2010-10-26 14:23:18 -07004884 #print "REST<$rest> dstat<$dstat> ctx<$ctx>\n";
Joe Perchesf59b64b2016-10-11 13:52:08 -07004885
4886 $ctx =~ s/\n*$//;
4887 my $herectx = $here . "\n";
4888 my $stmt_cnt = statement_rawlines($ctx);
4889
4890 for (my $n = 0; $n < $stmt_cnt; $n++) {
4891 $herectx .= raw_line($linenr, $n) . "\n";
4892 }
4893
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004894 if ($dstat ne '' &&
4895 $dstat !~ /^(?:$Ident|-?$Constant),$/ && # 10, // foo(),
4896 $dstat !~ /^(?:$Ident|-?$Constant);$/ && # foo();
Joe Perches3cc4b1c2013-07-03 15:05:27 -07004897 $dstat !~ /^[!~-]?(?:$Lval|$Constant)$/ && # 10 // foo() // !foo // ~foo // -foo // foo->bar // foo.bar->baz
Joe Perches356fd392014-08-06 16:10:31 -07004898 $dstat !~ /^'X'$/ && $dstat !~ /^'XX'$/ && # character constants
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004899 $dstat !~ /$exceptions/ &&
4900 $dstat !~ /^\.$Ident\s*=/ && # .foo =
Joe Perchese942e2c2013-04-17 15:58:26 -07004901 $dstat !~ /^(?:\#\s*$Ident|\#\s*$Constant)\s*$/ && # stringification #foo
Andy Whitcroft72f115f2012-01-10 15:10:06 -08004902 $dstat !~ /^do\s*$Constant\s*while\s*$Constant;?$/ && # do {...} while (...); // do {...} while (...)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004903 $dstat !~ /^for\s*$Constant$/ && # for (...)
4904 $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ && # for (...) bar()
4905 $dstat !~ /^do\s*{/ && # do {...
Eddie Kovsky4e5d56b2015-09-09 15:37:52 -07004906 $dstat !~ /^\(\{/ && # ({...
Joe Perchesf95a7e62013-09-11 14:24:00 -07004907 $ctx !~ /^.\s*#\s*define\s+TRACE_(?:SYSTEM|INCLUDE_FILE|INCLUDE_PATH)\b/)
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004908 {
Joe Perchese7955562017-05-08 15:55:48 -07004909 if ($dstat =~ /^\s*if\b/) {
4910 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4911 "Macros starting with if should be enclosed by a do - while loop to avoid possible if/else logic defects\n" . "$herectx");
4912 } elsif ($dstat =~ /;/) {
Andy Whitcroftf74bd192012-01-10 15:09:54 -08004913 ERROR("MULTISTATEMENT_MACRO_USE_DO_WHILE",
4914 "Macros with multiple statements should be enclosed in a do - while loop\n" . "$herectx");
4915 } else {
Joe Perches000d1cc12011-07-25 17:13:25 -07004916 ERROR("COMPLEX_MACRO",
Andrew Morton388982b2014-10-13 15:51:40 -07004917 "Macros with complex values should be enclosed in parentheses\n" . "$herectx");
Andy Whitcroftd8aaf122007-06-23 17:16:44 -07004918 }
Joe Perchesf59b64b2016-10-11 13:52:08 -07004919
4920 }
Joe Perches52076492016-10-11 13:52:14 -07004921
4922 # Make $define_stmt single line, comment-free, etc
4923 my @stmt_array = split('\n', $define_stmt);
4924 my $first = 1;
4925 $define_stmt = "";
4926 foreach my $l (@stmt_array) {
4927 $l =~ s/\\$//;
4928 if ($first) {
4929 $define_stmt = $l;
4930 $first = 0;
4931 } elsif ($l =~ /^[\+ ]/) {
4932 $define_stmt .= substr($l, 1);
4933 }
4934 }
4935 $define_stmt =~ s/$;//g;
4936 $define_stmt =~ s/\s+/ /g;
4937 $define_stmt = trim($define_stmt);
4938
Joe Perchesf59b64b2016-10-11 13:52:08 -07004939# check if any macro arguments are reused (ignore '...' and 'type')
4940 foreach my $arg (@def_args) {
4941 next if ($arg =~ /\.\.\./);
Joe Perches9192d412016-10-11 13:52:11 -07004942 next if ($arg =~ /^type$/i);
Joe Perches7fe528a22017-07-10 15:52:27 -07004943 my $tmp_stmt = $define_stmt;
4944 $tmp_stmt =~ s/\b(typeof|__typeof__|__builtin\w+|typecheck\s*\(\s*$Type\s*,|\#+)\s*\(*\s*$arg\s*\)*\b//g;
4945 $tmp_stmt =~ s/\#+\s*$arg\b//g;
4946 $tmp_stmt =~ s/\b$arg\s*\#\#//g;
4947 my $use_cnt = $tmp_stmt =~ s/\b$arg\b//g;
Joe Perchesf59b64b2016-10-11 13:52:08 -07004948 if ($use_cnt > 1) {
4949 CHK("MACRO_ARG_REUSE",
4950 "Macro argument reuse '$arg' - possible side-effects?\n" . "$herectx");
Joe Perches9192d412016-10-11 13:52:11 -07004951 }
4952# check if any macro arguments may have other precedence issues
Joe Perches7fe528a22017-07-10 15:52:27 -07004953 if ($tmp_stmt =~ m/($Operators)?\s*\b$arg\b\s*($Operators)?/m &&
Joe Perches9192d412016-10-11 13:52:11 -07004954 ((defined($1) && $1 ne ',') ||
4955 (defined($2) && $2 ne ','))) {
4956 CHK("MACRO_ARG_PRECEDENCE",
4957 "Macro argument '$arg' may be better as '($arg)' to avoid precedence issues\n" . "$herectx");
Joe Perchesf59b64b2016-10-11 13:52:08 -07004958 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07004959 }
Joe Perches5023d342012-12-17 16:01:47 -08004960
Joe Perches08a28432014-10-13 15:51:55 -07004961# check for macros with flow control, but without ## concatenation
4962# ## concatenation is commonly a macro that defines a function so ignore those
4963 if ($has_flow_statement && !$has_arg_concat) {
4964 my $herectx = $here . "\n";
4965 my $cnt = statement_rawlines($ctx);
4966
4967 for (my $n = 0; $n < $cnt; $n++) {
4968 $herectx .= raw_line($linenr, $n) . "\n";
4969 }
4970 WARN("MACRO_WITH_FLOW_CONTROL",
4971 "Macros with flow control statements should be avoided\n" . "$herectx");
4972 }
4973
Joe Perches481eb482012-12-17 16:01:56 -08004974# check for line continuations outside of #defines, preprocessor #, and asm
Joe Perches5023d342012-12-17 16:01:47 -08004975
4976 } else {
4977 if ($prevline !~ /^..*\\$/ &&
Joe Perches481eb482012-12-17 16:01:56 -08004978 $line !~ /^\+\s*\#.*\\$/ && # preprocessor
4979 $line !~ /^\+.*\b(__asm__|asm)\b.*\\$/ && # asm
Joe Perches5023d342012-12-17 16:01:47 -08004980 $line =~ /^\+.*\\$/) {
4981 WARN("LINE_CONTINUATIONS",
4982 "Avoid unnecessary line continuations\n" . $herecurr);
4983 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07004984 }
4985
Joe Perchesb13edf72012-07-30 14:41:24 -07004986# do {} while (0) macro tests:
4987# single-statement macros do not need to be enclosed in do while (0) loop,
4988# macro should not end with a semicolon
4989 if ($^V && $^V ge 5.10.0 &&
4990 $realfile !~ m@/vmlinux.lds.h$@ &&
4991 $line =~ /^.\s*\#\s*define\s+$Ident(\()?/) {
4992 my $ln = $linenr;
4993 my $cnt = $realcnt;
4994 my ($off, $dstat, $dcond, $rest);
4995 my $ctx = '';
4996 ($dstat, $dcond, $ln, $cnt, $off) =
4997 ctx_statement_block($linenr, $realcnt, 0);
4998 $ctx = $dstat;
4999
5000 $dstat =~ s/\\\n.//g;
Joe Perches1b36b202015-02-13 14:38:32 -08005001 $dstat =~ s/$;/ /g;
Joe Perchesb13edf72012-07-30 14:41:24 -07005002
5003 if ($dstat =~ /^\+\s*#\s*define\s+$Ident\s*${balanced_parens}\s*do\s*{(.*)\s*}\s*while\s*\(\s*0\s*\)\s*([;\s]*)\s*$/) {
5004 my $stmts = $2;
5005 my $semis = $3;
5006
5007 $ctx =~ s/\n*$//;
5008 my $cnt = statement_rawlines($ctx);
5009 my $herectx = $here . "\n";
5010
5011 for (my $n = 0; $n < $cnt; $n++) {
5012 $herectx .= raw_line($linenr, $n) . "\n";
5013 }
5014
Joe Perchesac8e97f2012-08-21 16:15:53 -07005015 if (($stmts =~ tr/;/;/) == 1 &&
5016 $stmts !~ /^\s*(if|while|for|switch)\b/) {
Joe Perchesb13edf72012-07-30 14:41:24 -07005017 WARN("SINGLE_STATEMENT_DO_WHILE_MACRO",
5018 "Single statement macros should not use a do {} while (0) loop\n" . "$herectx");
5019 }
5020 if (defined $semis && $semis ne "") {
5021 WARN("DO_WHILE_MACRO_WITH_TRAILING_SEMICOLON",
5022 "do {} while (0) macros should not be semicolon terminated\n" . "$herectx");
5023 }
Joe Perchesf5ef95b2014-06-04 16:12:06 -07005024 } elsif ($dstat =~ /^\+\s*#\s*define\s+$Ident.*;\s*$/) {
5025 $ctx =~ s/\n*$//;
5026 my $cnt = statement_rawlines($ctx);
5027 my $herectx = $here . "\n";
5028
5029 for (my $n = 0; $n < $cnt; $n++) {
5030 $herectx .= raw_line($linenr, $n) . "\n";
5031 }
5032
5033 WARN("TRAILING_SEMICOLON",
5034 "macros should not use a trailing semicolon\n" . "$herectx");
Joe Perchesb13edf72012-07-30 14:41:24 -07005035 }
5036 }
5037
Mike Frysinger080ba922009-01-06 14:41:25 -08005038# make sure symbols are always wrapped with VMLINUX_SYMBOL() ...
5039# all assignments may have only one of the following with an assignment:
5040# .
5041# ALIGN(...)
5042# VMLINUX_SYMBOL(...)
5043 if ($realfile eq 'vmlinux.lds.h' && $line =~ /(?:(?:^|\s)$Ident\s*=|=\s*$Ident(?:\s|$))/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005044 WARN("MISSING_VMLINUX_SYMBOL",
5045 "vmlinux.lds.h needs VMLINUX_SYMBOL() around C-visible symbols\n" . $herecurr);
Mike Frysinger080ba922009-01-06 14:41:25 -08005046 }
5047
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005048# check for redundant bracing round if etc
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005049 if ($line =~ /(^.*)\bif\b/ && $1 !~ /else\s*$/) {
5050 my ($level, $endln, @chunks) =
Andy Whitcroftcf655042008-03-04 14:28:20 -08005051 ctx_statement_full($linenr, $realcnt, 1);
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005052 #print "chunks<$#chunks> linenr<$linenr> endln<$endln> level<$level>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005053 #print "APW: <<$chunks[1][0]>><<$chunks[1][1]>>\n";
5054 if ($#chunks > 0 && $level == 0) {
Joe Perchesaad4f612012-03-23 15:02:19 -07005055 my @allowed = ();
5056 my $allow = 0;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005057 my $seen = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005058 my $herectx = $here . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005059 my $ln = $linenr - 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005060 for my $chunk (@chunks) {
5061 my ($cond, $block) = @{$chunk};
5062
Andy Whitcroft773647a2008-03-28 14:15:58 -07005063 # If the condition carries leading newlines, then count those as offsets.
5064 my ($whitespace) = ($cond =~ /^((?:\s*\n[+-])*\s*)/s);
5065 my $offset = statement_rawlines($whitespace) - 1;
5066
Joe Perchesaad4f612012-03-23 15:02:19 -07005067 $allowed[$allow] = 0;
Andy Whitcroft773647a2008-03-28 14:15:58 -07005068 #print "COND<$cond> whitespace<$whitespace> offset<$offset>\n";
5069
5070 # We have looked at and allowed this specific line.
5071 $suppress_ifbraces{$ln + $offset} = 1;
5072
5073 $herectx .= "$rawlines[$ln + $offset]\n[...]\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005074 $ln += statement_rawlines($block) - 1;
5075
Andy Whitcroft773647a2008-03-28 14:15:58 -07005076 substr($block, 0, length($cond), '');
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005077
5078 $seen++ if ($block =~ /^\s*{/);
5079
Joe Perchesaad4f612012-03-23 15:02:19 -07005080 #print "cond<$cond> block<$block> allowed<$allowed[$allow]>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005081 if (statement_lines($cond) > 1) {
5082 #print "APW: ALLOWED: cond<$cond>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005083 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005084 }
5085 if ($block =~/\b(?:if|for|while)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005086 #print "APW: ALLOWED: block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005087 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005088 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005089 if (statement_block_size($block) > 1) {
5090 #print "APW: ALLOWED: lines block<$block>\n";
Joe Perchesaad4f612012-03-23 15:02:19 -07005091 $allowed[$allow] = 1;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005092 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005093 $allow++;
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005094 }
Joe Perchesaad4f612012-03-23 15:02:19 -07005095 if ($seen) {
5096 my $sum_allowed = 0;
5097 foreach (@allowed) {
5098 $sum_allowed += $_;
5099 }
5100 if ($sum_allowed == 0) {
5101 WARN("BRACES",
5102 "braces {} are not necessary for any arm of this statement\n" . $herectx);
5103 } elsif ($sum_allowed != $allow &&
5104 $seen != $allow) {
5105 CHK("BRACES",
5106 "braces {} should be used on all arms of this statement\n" . $herectx);
5107 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005108 }
5109 }
5110 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005111 if (!defined $suppress_ifbraces{$linenr - 1} &&
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005112 $line =~ /\b(if|while|for|else)\b/) {
Andy Whitcroftcf655042008-03-04 14:28:20 -08005113 my $allowed = 0;
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005114
Andy Whitcroftcf655042008-03-04 14:28:20 -08005115 # Check the pre-context.
5116 if (substr($line, 0, $-[0]) =~ /(\}\s*)$/) {
5117 #print "APW: ALLOWED: pre<$1>\n";
5118 $allowed = 1;
5119 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07005120
5121 my ($level, $endln, @chunks) =
5122 ctx_statement_full($linenr, $realcnt, $-[0]);
5123
Andy Whitcroftcf655042008-03-04 14:28:20 -08005124 # Check the condition.
5125 my ($cond, $block) = @{$chunks[0]};
Andy Whitcroft773647a2008-03-28 14:15:58 -07005126 #print "CHECKING<$linenr> cond<$cond> block<$block>\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005127 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005128 substr($block, 0, length($cond), '');
Andy Whitcroftcf655042008-03-04 14:28:20 -08005129 }
5130 if (statement_lines($cond) > 1) {
5131 #print "APW: ALLOWED: cond<$cond>\n";
5132 $allowed = 1;
5133 }
5134 if ($block =~/\b(?:if|for|while)\b/) {
5135 #print "APW: ALLOWED: block<$block>\n";
5136 $allowed = 1;
5137 }
5138 if (statement_block_size($block) > 1) {
5139 #print "APW: ALLOWED: lines block<$block>\n";
5140 $allowed = 1;
5141 }
5142 # Check the post-context.
5143 if (defined $chunks[1]) {
5144 my ($cond, $block) = @{$chunks[1]};
5145 if (defined $cond) {
Andy Whitcroft773647a2008-03-28 14:15:58 -07005146 substr($block, 0, length($cond), '');
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005147 }
Andy Whitcroftcf655042008-03-04 14:28:20 -08005148 if ($block =~ /^\s*\{/) {
5149 #print "APW: ALLOWED: chunk-1 block<$block>\n";
5150 $allowed = 1;
5151 }
5152 }
5153 if ($level == 0 && $block =~ /^\s*\{/ && !$allowed) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005154 my $herectx = $here . "\n";
Andy Whitcroftf0556632008-10-15 22:02:23 -07005155 my $cnt = statement_rawlines($block);
Andy Whitcroftcf655042008-03-04 14:28:20 -08005156
Andy Whitcroftf0556632008-10-15 22:02:23 -07005157 for (my $n = 0; $n < $cnt; $n++) {
Justin P. Mattock69932482011-07-26 23:06:29 -07005158 $herectx .= raw_line($linenr, $n) . "\n";
Andy Whitcroftcf655042008-03-04 14:28:20 -08005159 }
5160
Joe Perches000d1cc12011-07-25 17:13:25 -07005161 WARN("BRACES",
5162 "braces {} are not necessary for single statement blocks\n" . $herectx);
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005163 }
5164 }
5165
Joe Perchese4c5bab2017-02-24 15:01:41 -08005166# check for single line unbalanced braces
Sven Eckelmann95330472017-02-24 15:01:43 -08005167 if ($sline =~ /^.\s*\}\s*else\s*$/ ||
5168 $sline =~ /^.\s*else\s*\{\s*$/) {
Joe Perchese4c5bab2017-02-24 15:01:41 -08005169 CHK("BRACES", "Unbalanced braces around else statement\n" . $herecurr);
5170 }
5171
Joe Perches0979ae62012-12-17 16:01:59 -08005172# check for unnecessary blank lines around braces
Joe Perches77b9a532013-07-03 15:05:29 -07005173 if (($line =~ /^.\s*}\s*$/ && $prevrawline =~ /^.\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005174 if (CHK("BRACES",
5175 "Blank lines aren't necessary before a close brace '}'\n" . $hereprev) &&
5176 $fix && $prevrawline =~ /^\+/) {
5177 fix_delete_line($fixlinenr - 1, $prevrawline);
5178 }
Joe Perches0979ae62012-12-17 16:01:59 -08005179 }
Joe Perches77b9a532013-07-03 15:05:29 -07005180 if (($rawline =~ /^.\s*$/ && $prevline =~ /^..*{\s*$/)) {
Joe Perchesf8e58212015-02-13 14:38:46 -08005181 if (CHK("BRACES",
5182 "Blank lines aren't necessary after an open brace '{'\n" . $hereprev) &&
5183 $fix) {
5184 fix_delete_line($fixlinenr, $rawline);
5185 }
Joe Perches0979ae62012-12-17 16:01:59 -08005186 }
5187
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005188# no volatiles please
Andy Whitcroft6c72ffa2007-10-18 03:05:08 -07005189 my $asm_volatile = qr{\b(__asm__|asm)\s+(__volatile__|volatile)\b};
5190 if ($line =~ /\bvolatile\b/ && $line !~ /$asm_volatile/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005191 WARN("VOLATILE",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005192 "Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005193 }
5194
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005195# Check for user-visible strings broken across lines, which breaks the ability
5196# to grep for the string. Make exceptions when the previous string ends in a
5197# newline (multiple lines in one string constant) or '\t', '\r', ';', or '{'
5198# (common in inline assembly) or is a octal \123 or hexadecimal \xaf value
Joe Perches33acb542015-06-25 15:02:54 -07005199 if ($line =~ /^\+\s*$String/ &&
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005200 $prevline =~ /"\s*$/ &&
5201 $prevrawline !~ /(?:\\(?:[ntr]|[0-7]{1,3}|x[0-9a-fA-F]{1,2})|;\s*|\{\s*)"\s*$/) {
5202 if (WARN("SPLIT_STRING",
5203 "quoted string split across lines\n" . $hereprev) &&
5204 $fix &&
5205 $prevrawline =~ /^\+.*"\s*$/ &&
5206 $last_coalesced_string_linenr != $linenr - 1) {
5207 my $extracted_string = get_quoted_string($line, $rawline);
5208 my $comma_close = "";
5209 if ($rawline =~ /\Q$extracted_string\E(\s*\)\s*;\s*$|\s*,\s*)/) {
5210 $comma_close = $1;
5211 }
5212
5213 fix_delete_line($fixlinenr - 1, $prevrawline);
5214 fix_delete_line($fixlinenr, $rawline);
5215 my $fixedline = $prevrawline;
5216 $fixedline =~ s/"\s*$//;
5217 $fixedline .= substr($extracted_string, 1) . trim($comma_close);
5218 fix_insert_line($fixlinenr - 1, $fixedline);
5219 $fixedline = $rawline;
5220 $fixedline =~ s/\Q$extracted_string\E\Q$comma_close\E//;
5221 if ($fixedline !~ /\+\s*$/) {
5222 fix_insert_line($fixlinenr, $fixedline);
5223 }
5224 $last_coalesced_string_linenr = $linenr;
5225 }
5226 }
5227
5228# check for missing a space in a string concatenation
5229 if ($prevrawline =~ /[^\\]\w"$/ && $rawline =~ /^\+[\t ]+"\w/) {
5230 WARN('MISSING_SPACE',
5231 "break quoted strings at a space character\n" . $hereprev);
5232 }
5233
Joe Perchese4b7d302017-05-08 15:55:51 -07005234# check for an embedded function name in a string when the function is known
5235# This does not work very well for -f --file checking as it depends on patch
5236# context providing the function name or a single line form for in-file
5237# function declarations
Joe Perches77cb8542017-02-24 15:01:28 -08005238 if ($line =~ /^\+.*$String/ &&
5239 defined($context_function) &&
Joe Perchese4b7d302017-05-08 15:55:51 -07005240 get_quoted_string($line, $rawline) =~ /\b$context_function\b/ &&
5241 length(get_quoted_string($line, $rawline)) != (length($context_function) + 2)) {
Joe Perches77cb8542017-02-24 15:01:28 -08005242 WARN("EMBEDDED_FUNCTION_NAME",
Joe Perchese4b7d302017-05-08 15:55:51 -07005243 "Prefer using '\"%s...\", __func__' to using '$context_function', this function's name, in a string\n" . $herecurr);
Joe Perches77cb8542017-02-24 15:01:28 -08005244 }
5245
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005246# check for spaces before a quoted newline
5247 if ($rawline =~ /^.*\".*\s\\n/) {
5248 if (WARN("QUOTED_WHITESPACE_BEFORE_NEWLINE",
5249 "unnecessary whitespace before a quoted newline\n" . $herecurr) &&
5250 $fix) {
5251 $fixed[$fixlinenr] =~ s/^(\+.*\".*)\s+\\n/$1\\n/;
5252 }
5253
5254 }
5255
Joe Perchesf17dba42014-10-13 15:51:51 -07005256# concatenated string without spaces between elements
Joe Perches33acb542015-06-25 15:02:54 -07005257 if ($line =~ /$String[A-Z_]/ || $line =~ /[A-Za-z0-9_]$String/) {
Joe Perchesf17dba42014-10-13 15:51:51 -07005258 CHK("CONCATENATED_STRING",
5259 "Concatenated strings should use spaces between elements\n" . $herecurr);
5260 }
5261
Joe Perches90ad30e2014-12-10 15:51:59 -08005262# uncoalesced string fragments
Joe Perches33acb542015-06-25 15:02:54 -07005263 if ($line =~ /$String\s*"/) {
Joe Perches90ad30e2014-12-10 15:51:59 -08005264 WARN("STRING_FRAGMENTS",
5265 "Consecutive strings are generally better as a single string\n" . $herecurr);
5266 }
5267
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005268# check for non-standard and hex prefixed decimal printf formats
5269 my $show_L = 1; #don't show the same defect twice
5270 my $show_Z = 1;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005271 while ($line =~ /(?:^|")([X\t]*)(?:"|$)/g) {
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005272 my $string = substr($rawline, $-[1], $+[1] - $-[1]);
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005273 $string =~ s/%%/__/g;
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005274 # check for %L
5275 if ($show_L && $string =~ /%[\*\d\.\$]*L([diouxX])/) {
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005276 WARN("PRINTF_L",
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005277 "\%L$1 is non-standard C, use %ll$1\n" . $herecurr);
5278 $show_L = 0;
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005279 }
Alexey Dobriyan522b8372017-02-27 14:30:05 -08005280 # check for %Z
5281 if ($show_Z && $string =~ /%[\*\d\.\$]*Z([diouxX])/) {
5282 WARN("PRINTF_Z",
5283 "%Z$1 is non-standard C, use %z$1\n" . $herecurr);
5284 $show_Z = 0;
5285 }
5286 # check for 0x<decimal>
5287 if ($string =~ /0x%[\*\d\.\$\Llzth]*[diou]/) {
5288 ERROR("PRINTF_0XDECIMAL",
Joe Perches6e300752015-09-09 15:37:47 -07005289 "Prefixing 0x with decimal output is defective\n" . $herecurr);
5290 }
Joe Perches5e4f6ba2014-12-10 15:52:05 -08005291 }
5292
5293# check for line continuations in quoted strings with odd counts of "
5294 if ($rawline =~ /\\$/ && $rawline =~ tr/"/"/ % 2) {
5295 WARN("LINE_CONTINUATIONS",
5296 "Avoid line continuations in quoted strings\n" . $herecurr);
5297 }
5298
Andy Whitcroft00df3442007-06-08 13:47:06 -07005299# warn about #if 0
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005300 if ($line =~ /^.\s*\#\s*if\s+0\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005301 CHK("REDUNDANT_CODE",
5302 "if this code is redundant consider removing it\n" .
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005303 $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005304 }
5305
Andy Whitcroft03df4b52012-12-17 16:01:52 -08005306# check for needless "if (<foo>) fn(<foo>)" uses
5307 if ($prevline =~ /\bif\s*\(\s*($Lval)\s*\)/) {
Joe Perches100425d2015-09-09 15:37:36 -07005308 my $tested = quotemeta($1);
5309 my $expr = '\s*\(\s*' . $tested . '\s*\)\s*;';
5310 if ($line =~ /\b(kfree|usb_free_urb|debugfs_remove(?:_recursive)?|(?:kmem_cache|mempool|dma_pool)_destroy)$expr/) {
5311 my $func = $1;
5312 if (WARN('NEEDLESS_IF',
5313 "$func(NULL) is safe and this check is probably not required\n" . $hereprev) &&
5314 $fix) {
5315 my $do_fix = 1;
5316 my $leading_tabs = "";
5317 my $new_leading_tabs = "";
5318 if ($lines[$linenr - 2] =~ /^\+(\t*)if\s*\(\s*$tested\s*\)\s*$/) {
5319 $leading_tabs = $1;
5320 } else {
5321 $do_fix = 0;
5322 }
5323 if ($lines[$linenr - 1] =~ /^\+(\t+)$func\s*\(\s*$tested\s*\)\s*;\s*$/) {
5324 $new_leading_tabs = $1;
5325 if (length($leading_tabs) + 1 ne length($new_leading_tabs)) {
5326 $do_fix = 0;
5327 }
5328 } else {
5329 $do_fix = 0;
5330 }
5331 if ($do_fix) {
5332 fix_delete_line($fixlinenr - 1, $prevrawline);
5333 $fixed[$fixlinenr] =~ s/^\+$new_leading_tabs/\+$leading_tabs/;
5334 }
5335 }
Greg Kroah-Hartman4c432a82008-07-23 21:29:04 -07005336 }
5337 }
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07005338
Joe Perchesebfdc402014-08-06 16:10:27 -07005339# check for unnecessary "Out of Memory" messages
5340 if ($line =~ /^\+.*\b$logFunctions\s*\(/ &&
5341 $prevline =~ /^[ \+]\s*if\s*\(\s*(\!\s*|NULL\s*==\s*)?($Lval)(\s*==\s*NULL\s*)?\s*\)/ &&
5342 (defined $1 || defined $3) &&
5343 $linenr > 3) {
5344 my $testval = $2;
5345 my $testline = $lines[$linenr - 3];
5346
5347 my ($s, $c) = ctx_statement_block($linenr - 3, $realcnt, 0);
5348# print("line: <$line>\nprevline: <$prevline>\ns: <$s>\nc: <$c>\n\n\n");
5349
Joe Perchesfb0d0e02017-07-10 15:52:04 -07005350 if ($s =~ /(?:^|\n)[ \+]\s*(?:$Type\s*)?\Q$testval\E\s*=\s*(?:\([^\)]*\)\s*)?\s*(?:devm_)?(?:[kv][czm]alloc(?:_node|_array)?\b|kstrdup|kmemdup|(?:dev_)?alloc_skb)/) {
Joe Perchesebfdc402014-08-06 16:10:27 -07005351 WARN("OOM_MESSAGE",
5352 "Possible unnecessary 'out of memory' message\n" . $hereprev);
5353 }
5354 }
5355
Joe Perchesf78d98f2014-10-13 15:52:01 -07005356# check for logging functions with KERN_<LEVEL>
Paolo Bonzinidcaf1122015-02-13 14:38:26 -08005357 if ($line !~ /printk(?:_ratelimited|_once)?\s*\(/ &&
Joe Perchesf78d98f2014-10-13 15:52:01 -07005358 $line =~ /\b$logFunctions\s*\(.*\b(KERN_[A-Z]+)\b/) {
5359 my $level = $1;
5360 if (WARN("UNNECESSARY_KERN_LEVEL",
5361 "Possible unnecessary $level\n" . $herecurr) &&
5362 $fix) {
5363 $fixed[$fixlinenr] =~ s/\s*$level\s*//;
5364 }
5365 }
5366
Joe Perches45c55e92017-02-24 15:01:31 -08005367# check for logging continuations
5368 if ($line =~ /\bprintk\s*\(\s*KERN_CONT\b|\bpr_cont\s*\(/) {
5369 WARN("LOGGING_CONTINUATION",
5370 "Avoid logging continuation uses where feasible\n" . $herecurr);
5371 }
5372
Joe Perchesabb08a52014-12-10 15:51:46 -08005373# check for mask then right shift without a parentheses
5374 if ($^V && $^V ge 5.10.0 &&
5375 $line =~ /$LvalOrFunc\s*\&\s*($LvalOrFunc)\s*>>/ &&
5376 $4 !~ /^\&/) { # $LvalOrFunc may be &foo, ignore if so
5377 WARN("MASK_THEN_SHIFT",
5378 "Possible precedence defect with mask then right shift - may need parentheses\n" . $herecurr);
5379 }
5380
Joe Perchesb75ac612014-12-10 15:52:02 -08005381# check for pointer comparisons to NULL
5382 if ($^V && $^V ge 5.10.0) {
5383 while ($line =~ /\b$LvalOrFunc\s*(==|\!=)\s*NULL\b/g) {
5384 my $val = $1;
5385 my $equal = "!";
5386 $equal = "" if ($4 eq "!=");
5387 if (CHK("COMPARISON_TO_NULL",
5388 "Comparison to NULL could be written \"${equal}${val}\"\n" . $herecurr) &&
5389 $fix) {
5390 $fixed[$fixlinenr] =~ s/\b\Q$val\E\s*(?:==|\!=)\s*NULL\b/$equal$val/;
5391 }
5392 }
5393 }
5394
Joe Perches8716de32013-09-11 14:24:05 -07005395# check for bad placement of section $InitAttribute (e.g.: __initdata)
5396 if ($line =~ /(\b$InitAttribute\b)/) {
5397 my $attr = $1;
5398 if ($line =~ /^\+\s*static\s+(?:const\s+)?(?:$attr\s+)?($NonptrTypeWithAttr)\s+(?:$attr\s+)?($Ident(?:\[[^]]*\])?)\s*[=;]/) {
5399 my $ptr = $1;
5400 my $var = $2;
5401 if ((($ptr =~ /\b(union|struct)\s+$attr\b/ &&
5402 ERROR("MISPLACED_INIT",
5403 "$attr should be placed after $var\n" . $herecurr)) ||
5404 ($ptr !~ /\b(union|struct)\s+$attr\b/ &&
5405 WARN("MISPLACED_INIT",
5406 "$attr should be placed after $var\n" . $herecurr))) &&
5407 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005408 $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 -07005409 }
5410 }
5411 }
5412
Joe Perchese970b8842013-11-12 15:10:10 -08005413# check for $InitAttributeData (ie: __initdata) with const
5414 if ($line =~ /\bconst\b/ && $line =~ /($InitAttributeData)/) {
5415 my $attr = $1;
5416 $attr =~ /($InitAttributePrefix)(.*)/;
5417 my $attr_prefix = $1;
5418 my $attr_type = $2;
5419 if (ERROR("INIT_ATTRIBUTE",
5420 "Use of const init definition must use ${attr_prefix}initconst\n" . $herecurr) &&
5421 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005422 $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005423 s/$InitAttributeData/${attr_prefix}initconst/;
5424 }
5425 }
5426
5427# check for $InitAttributeConst (ie: __initconst) without const
5428 if ($line !~ /\bconst\b/ && $line =~ /($InitAttributeConst)/) {
5429 my $attr = $1;
5430 if (ERROR("INIT_ATTRIBUTE",
5431 "Use of $attr requires a separate use of const\n" . $herecurr) &&
5432 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005433 my $lead = $fixed[$fixlinenr] =~
Joe Perchese970b8842013-11-12 15:10:10 -08005434 /(^\+\s*(?:static\s+))/;
5435 $lead = rtrim($1);
5436 $lead = "$lead " if ($lead !~ /^\+$/);
5437 $lead = "${lead}const ";
Joe Perches194f66f2014-08-06 16:11:03 -07005438 $fixed[$fixlinenr] =~ s/(^\+\s*(?:static\s+))/$lead/;
Joe Perchese970b8842013-11-12 15:10:10 -08005439 }
5440 }
5441
Joe Perchesc17893c2015-04-16 12:44:42 -07005442# check for __read_mostly with const non-pointer (should just be const)
5443 if ($line =~ /\b__read_mostly\b/ &&
5444 $line =~ /($Type)\s*$Ident/ && $1 !~ /\*\s*$/ && $1 =~ /\bconst\b/) {
5445 if (ERROR("CONST_READ_MOSTLY",
5446 "Invalid use of __read_mostly with const type\n" . $herecurr) &&
5447 $fix) {
5448 $fixed[$fixlinenr] =~ s/\s+__read_mostly\b//;
5449 }
5450 }
5451
Joe Perchesfbdb8132014-04-03 14:49:14 -07005452# don't use __constant_<foo> functions outside of include/uapi/
5453 if ($realfile !~ m@^include/uapi/@ &&
5454 $line =~ /(__constant_(?:htons|ntohs|[bl]e(?:16|32|64)_to_cpu|cpu_to_[bl]e(?:16|32|64)))\s*\(/) {
5455 my $constant_func = $1;
5456 my $func = $constant_func;
5457 $func =~ s/^__constant_//;
5458 if (WARN("CONSTANT_CONVERSION",
5459 "$constant_func should be $func\n" . $herecurr) &&
5460 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005461 $fixed[$fixlinenr] =~ s/\b$constant_func\b/$func/g;
Joe Perchesfbdb8132014-04-03 14:49:14 -07005462 }
5463 }
5464
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005465# prefer usleep_range over udelay
Bruce Allan37581c22013-02-21 16:44:19 -08005466 if ($line =~ /\budelay\s*\(\s*(\d+)\s*\)/) {
Joe Perches43c1d772014-04-03 14:49:11 -07005467 my $delay = $1;
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005468 # ignore udelay's < 10, however
Joe Perches43c1d772014-04-03 14:49:11 -07005469 if (! ($delay < 10) ) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005470 CHK("USLEEP_RANGE",
Joe Perches43c1d772014-04-03 14:49:11 -07005471 "usleep_range is preferred over udelay; see Documentation/timers/timers-howto.txt\n" . $herecurr);
5472 }
5473 if ($delay > 2000) {
5474 WARN("LONG_UDELAY",
5475 "long udelay - prefer mdelay; see arch/arm/include/asm/delay.h\n" . $herecurr);
Patrick Pannuto1a15a252010-08-09 17:21:01 -07005476 }
5477 }
5478
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005479# warn about unexpectedly long msleep's
5480 if ($line =~ /\bmsleep\s*\((\d+)\);/) {
5481 if ($1 < 20) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005482 WARN("MSLEEP",
Joe Perches43c1d772014-04-03 14:49:11 -07005483 "msleep < 20ms can sleep for up to 20ms; see Documentation/timers/timers-howto.txt\n" . $herecurr);
Patrick Pannuto09ef8722010-08-09 17:21:02 -07005484 }
5485 }
5486
Joe Perches36ec1932013-07-03 15:05:25 -07005487# check for comparisons of jiffies
5488 if ($line =~ /\bjiffies\s*$Compare|$Compare\s*jiffies\b/) {
5489 WARN("JIFFIES_COMPARISON",
5490 "Comparing jiffies is almost always wrong; prefer time_after, time_before and friends\n" . $herecurr);
5491 }
5492
Joe Perches9d7a34a2013-07-03 15:05:26 -07005493# check for comparisons of get_jiffies_64()
5494 if ($line =~ /\bget_jiffies_64\s*\(\s*\)\s*$Compare|$Compare\s*get_jiffies_64\s*\(\s*\)/) {
5495 WARN("JIFFIES_COMPARISON",
5496 "Comparing get_jiffies_64() is almost always wrong; prefer time_after64, time_before64 and friends\n" . $herecurr);
5497 }
5498
Andy Whitcroft00df3442007-06-08 13:47:06 -07005499# warn about #ifdefs in C files
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005500# if ($line =~ /^.\s*\#\s*if(|n)def/ && ($realfile =~ /\.c$/)) {
Andy Whitcroft00df3442007-06-08 13:47:06 -07005501# print "#ifdef in C files should be avoided\n";
5502# print "$herecurr";
5503# $clean = 0;
5504# }
5505
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005506# warn about spacing in #ifdefs
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005507 if ($line =~ /^.\s*\#\s*(ifdef|ifndef|elif)\s\s+/) {
Joe Perches3705ce52013-07-03 15:05:31 -07005508 if (ERROR("SPACING",
5509 "exactly one space required after that #$1\n" . $herecurr) &&
5510 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005511 $fixed[$fixlinenr] =~
Joe Perches3705ce52013-07-03 15:05:31 -07005512 s/^(.\s*\#\s*(ifdef|ifndef|elif))\s{2,}/$1 /;
5513 }
5514
Andy Whitcroft22f2a2e2007-08-10 13:01:03 -07005515 }
5516
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005517# check for spinlock_t definitions without a comment.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005518 if ($line =~ /^.\s*(struct\s+mutex|spinlock_t)\s+\S+;/ ||
5519 $line =~ /^.\s*(DEFINE_MUTEX)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005520 my $which = $1;
5521 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005522 CHK("UNCOMMENTED_DEFINITION",
5523 "$1 definition without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005524 }
5525 }
5526# check for memory barriers without a comment.
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005527
5528 my $barriers = qr{
5529 mb|
5530 rmb|
5531 wmb|
5532 read_barrier_depends
5533 }x;
5534 my $barrier_stems = qr{
5535 mb__before_atomic|
5536 mb__after_atomic|
5537 store_release|
5538 load_acquire|
5539 store_mb|
5540 (?:$barriers)
5541 }x;
5542 my $all_barriers = qr{
5543 (?:$barriers)|
Michael S. Tsirkin43e361f2016-01-04 10:00:10 +02005544 smp_(?:$barrier_stems)|
5545 virt_(?:$barrier_stems)
Michael S. Tsirkin402c2552016-01-04 09:39:01 +02005546 }x;
5547
5548 if ($line =~ /\b(?:$all_barriers)\s*\(/) {
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005549 if (!ctx_has_comment($first_line, $linenr)) {
Joe Perchesc1fd7bb2013-11-12 15:10:11 -08005550 WARN("MEMORY_BARRIER",
5551 "memory barrier without comment\n" . $herecurr);
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005552 }
5553 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005554
Michael S. Tsirkinf4073b02016-01-04 09:54:51 +02005555 my $underscore_smp_barriers = qr{__smp_(?:$barrier_stems)}x;
5556
5557 if ($realfile !~ m@^include/asm-generic/@ &&
5558 $realfile !~ m@/barrier\.h$@ &&
5559 $line =~ m/\b(?:$underscore_smp_barriers)\s*\(/ &&
5560 $line !~ m/^.\s*\#\s*define\s+(?:$underscore_smp_barriers)\s*\(/) {
5561 WARN("MEMORY_BARRIER",
5562 "__smp memory barriers shouldn't be used outside barrier.h and asm-generic\n" . $herecurr);
5563 }
5564
Joe Perchescb426e92015-06-25 15:02:46 -07005565# check for waitqueue_active without a comment.
5566 if ($line =~ /\bwaitqueue_active\s*\(/) {
5567 if (!ctx_has_comment($first_line, $linenr)) {
5568 WARN("WAITQUEUE_ACTIVE",
5569 "waitqueue_active without comment\n" . $herecurr);
5570 }
5571 }
Paul E. McKenney3ad81772015-07-02 11:55:40 -07005572
Andy Whitcroft4a0df2e2007-06-08 13:46:39 -07005573# check of hardware specific defines
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005574 if ($line =~ m@^.\s*\#\s*if.*\b(__i386__|__powerpc64__|__sun__|__s390x__)\b@ && $realfile !~ m@include/asm-@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005575 CHK("ARCH_DEFINES",
5576 "architecture specific defines should be avoided\n" . $herecurr);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07005577 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005578
Joe Perches596ed452017-07-12 14:37:02 -07005579# check that the storage class is not after a type
5580 if ($line =~ /\b($Type)\s+($Storage)\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005581 WARN("STORAGE_CLASS",
Joe Perches596ed452017-07-12 14:37:02 -07005582 "storage class '$2' should be located before type '$1'\n" . $herecurr);
5583 }
5584# Check that the storage class is at the beginning of a declaration
5585 if ($line =~ /\b$Storage\b/ &&
5586 $line !~ /^.\s*$Storage/ &&
5587 $line =~ /^.\s*(.+?)\$Storage\s/ &&
5588 $1 !~ /[\,\)]\s*$/) {
5589 WARN("STORAGE_CLASS",
5590 "storage class should be at the beginning of the declaration\n" . $herecurr);
Tobias Klauserd4977c72010-05-24 14:33:30 -07005591 }
5592
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005593# check the location of the inline attribute, that it is between
5594# storage class and type.
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005595 if ($line =~ /\b$Type\s+$Inline\b/ ||
5596 $line =~ /\b$Inline\s+$Storage\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005597 ERROR("INLINE_LOCATION",
5598 "inline keyword should sit between storage class and type\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005599 }
5600
Andy Whitcroft8905a672007-11-28 16:21:06 -08005601# Check for __inline__ and __inline, prefer inline
Joe Perches2b7ab452013-11-12 15:10:14 -08005602 if ($realfile !~ m@\binclude/uapi/@ &&
5603 $line =~ /\b(__inline__|__inline)\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005604 if (WARN("INLINE",
5605 "plain inline is preferred over $1\n" . $herecurr) &&
5606 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005607 $fixed[$fixlinenr] =~ s/\b(__inline__|__inline)\b/inline/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005608
5609 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08005610 }
5611
Joe Perches3d130fd2011-01-12 17:00:00 -08005612# Check for __attribute__ packed, prefer __packed
Joe Perches2b7ab452013-11-12 15:10:14 -08005613 if ($realfile !~ m@\binclude/uapi/@ &&
5614 $line =~ /\b__attribute__\s*\(\s*\(.*\bpacked\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005615 WARN("PREFER_PACKED",
5616 "__packed is preferred over __attribute__((packed))\n" . $herecurr);
Joe Perches3d130fd2011-01-12 17:00:00 -08005617 }
5618
Joe Perches39b7e282011-07-25 17:13:24 -07005619# Check for __attribute__ aligned, prefer __aligned
Joe Perches2b7ab452013-11-12 15:10:14 -08005620 if ($realfile !~ m@\binclude/uapi/@ &&
5621 $line =~ /\b__attribute__\s*\(\s*\(.*aligned/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005622 WARN("PREFER_ALIGNED",
5623 "__aligned(size) is preferred over __attribute__((aligned(size)))\n" . $herecurr);
Joe Perches39b7e282011-07-25 17:13:24 -07005624 }
5625
Joe Perches5f14d3b2012-01-10 15:09:52 -08005626# Check for __attribute__ format(printf, prefer __printf
Joe Perches2b7ab452013-11-12 15:10:14 -08005627 if ($realfile !~ m@\binclude/uapi/@ &&
5628 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*printf/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005629 if (WARN("PREFER_PRINTF",
5630 "__printf(string-index, first-to-check) is preferred over __attribute__((format(printf, string-index, first-to-check)))\n" . $herecurr) &&
5631 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005632 $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 -07005633
5634 }
Joe Perches5f14d3b2012-01-10 15:09:52 -08005635 }
5636
Joe Perches6061d942012-03-23 15:02:16 -07005637# Check for __attribute__ format(scanf, prefer __scanf
Joe Perches2b7ab452013-11-12 15:10:14 -08005638 if ($realfile !~ m@\binclude/uapi/@ &&
5639 $line =~ /\b__attribute__\s*\(\s*\(\s*format\s*\(\s*scanf\b/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005640 if (WARN("PREFER_SCANF",
5641 "__scanf(string-index, first-to-check) is preferred over __attribute__((format(scanf, string-index, first-to-check)))\n" . $herecurr) &&
5642 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005643 $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 -07005644 }
Joe Perches6061d942012-03-23 15:02:16 -07005645 }
5646
Joe Perches619a9082014-12-10 15:51:35 -08005647# Check for __attribute__ weak, or __weak declarations (may have link issues)
5648 if ($^V && $^V ge 5.10.0 &&
5649 $line =~ /(?:$Declare|$DeclareMisordered)\s*$Ident\s*$balanced_parens\s*(?:$Attribute)?\s*;/ &&
5650 ($line =~ /\b__attribute__\s*\(\s*\(.*\bweak\b/ ||
5651 $line =~ /\b__weak\b/)) {
5652 ERROR("WEAK_DECLARATION",
5653 "Using weak declarations can have unintended link defects\n" . $herecurr);
5654 }
5655
Tomas Winklerfd39f902016-12-12 16:46:34 -08005656# check for c99 types like uint8_t used outside of uapi/ and tools/
Joe Perchese6176fa2015-06-25 15:02:49 -07005657 if ($realfile !~ m@\binclude/uapi/@ &&
Tomas Winklerfd39f902016-12-12 16:46:34 -08005658 $realfile !~ m@\btools/@ &&
Joe Perchese6176fa2015-06-25 15:02:49 -07005659 $line =~ /\b($Declare)\s*$Ident\s*[=;,\[]/) {
5660 my $type = $1;
5661 if ($type =~ /\b($typeC99Typedefs)\b/) {
5662 $type = $1;
5663 my $kernel_type = 'u';
5664 $kernel_type = 's' if ($type =~ /^_*[si]/);
5665 $type =~ /(\d+)/;
5666 $kernel_type .= $1;
5667 if (CHK("PREFER_KERNEL_TYPES",
5668 "Prefer kernel type '$kernel_type' over '$type'\n" . $herecurr) &&
5669 $fix) {
5670 $fixed[$fixlinenr] =~ s/\b$type\b/$kernel_type/;
5671 }
5672 }
5673 }
5674
Joe Perches938224b2016-01-20 14:59:15 -08005675# check for cast of C90 native int or longer types constants
5676 if ($line =~ /(\(\s*$C90_int_types\s*\)\s*)($Constant)\b/) {
5677 my $cast = $1;
5678 my $const = $2;
5679 if (WARN("TYPECAST_INT_CONSTANT",
5680 "Unnecessary typecast of c90 int constant\n" . $herecurr) &&
5681 $fix) {
5682 my $suffix = "";
5683 my $newconst = $const;
5684 $newconst =~ s/${Int_type}$//;
5685 $suffix .= 'U' if ($cast =~ /\bunsigned\b/);
5686 if ($cast =~ /\blong\s+long\b/) {
5687 $suffix .= 'LL';
5688 } elsif ($cast =~ /\blong\b/) {
5689 $suffix .= 'L';
5690 }
5691 $fixed[$fixlinenr] =~ s/\Q$cast\E$const\b/$newconst$suffix/;
5692 }
5693 }
5694
Joe Perches8f53a9b2010-03-05 13:43:48 -08005695# check for sizeof(&)
5696 if ($line =~ /\bsizeof\s*\(\s*\&/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005697 WARN("SIZEOF_ADDRESS",
5698 "sizeof(& should be avoided\n" . $herecurr);
Joe Perches8f53a9b2010-03-05 13:43:48 -08005699 }
5700
Joe Perches66c80b62012-07-30 14:41:22 -07005701# check for sizeof without parenthesis
5702 if ($line =~ /\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005703 if (WARN("SIZEOF_PARENTHESIS",
5704 "sizeof $1 should be sizeof($1)\n" . $herecurr) &&
5705 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005706 $fixed[$fixlinenr] =~ s/\bsizeof\s+((?:\*\s*|)$Lval|$Type(?:\s+$Lval|))/"sizeof(" . trim($1) . ")"/ex;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005707 }
Joe Perches66c80b62012-07-30 14:41:22 -07005708 }
5709
Joe Perches88982fe2012-12-17 16:02:00 -08005710# check for struct spinlock declarations
5711 if ($line =~ /^.\s*\bstruct\s+spinlock\s+\w+\s*;/) {
5712 WARN("USE_SPINLOCK_T",
5713 "struct spinlock should be spinlock_t\n" . $herecurr);
5714 }
5715
Joe Perchesa6962d72013-04-29 16:18:13 -07005716# check for seq_printf uses that could be seq_puts
Joe Perches06668722013-11-12 15:10:07 -08005717 if ($sline =~ /\bseq_printf\s*\(.*"\s*\)\s*;\s*$/) {
Joe Perchesa6962d72013-04-29 16:18:13 -07005718 my $fmt = get_quoted_string($line, $rawline);
Heba Aamercaac1d52015-02-13 14:38:49 -08005719 $fmt =~ s/%%//g;
5720 if ($fmt !~ /%/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07005721 if (WARN("PREFER_SEQ_PUTS",
5722 "Prefer seq_puts to seq_printf\n" . $herecurr) &&
5723 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005724 $fixed[$fixlinenr] =~ s/\bseq_printf\b/seq_puts/;
Joe Perchesd5e616f2013-09-11 14:23:54 -07005725 }
Joe Perchesa6962d72013-04-29 16:18:13 -07005726 }
5727 }
5728
Joe Perches0b523762017-05-08 15:55:36 -07005729 # check for vsprintf extension %p<foo> misuses
5730 if ($^V && $^V ge 5.10.0 &&
5731 defined $stat &&
5732 $stat =~ /^\+(?![^\{]*\{\s*).*\b(\w+)\s*\(.*$String\s*,/s &&
5733 $1 !~ /^_*volatile_*$/) {
5734 my $bad_extension = "";
5735 my $lc = $stat =~ tr@\n@@;
5736 $lc = $lc + $linenr;
5737 for (my $count = $linenr; $count <= $lc; $count++) {
5738 my $fmt = get_quoted_string($lines[$count - 1], raw_line($count, 0));
5739 $fmt =~ s/%%//g;
Pantelis Antoniouce4fecf2015-01-21 19:06:14 +02005740 if ($fmt =~ /(\%[\*\d\.]*p(?![\WFfSsBKRraEhMmIiUDdgVCbGNO]).)/) {
Joe Perches0b523762017-05-08 15:55:36 -07005741 $bad_extension = $1;
5742 last;
5743 }
5744 }
5745 if ($bad_extension ne "") {
5746 my $stat_real = raw_line($linenr, 0);
5747 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5748 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5749 }
5750 WARN("VSPRINTF_POINTER_EXTENSION",
5751 "Invalid vsprintf pointer extension '$bad_extension'\n" . "$here\n$stat_real\n");
5752 }
5753 }
5754
Andy Whitcroft554e1652012-01-10 15:09:57 -08005755# Check for misused memsets
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005756 if ($^V && $^V ge 5.10.0 &&
5757 defined $stat &&
Mateusz Kulikowski9e20a852015-06-25 15:03:16 -07005758 $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*$FuncArg\s*\)/) {
Andy Whitcroft554e1652012-01-10 15:09:57 -08005759
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005760 my $ms_addr = $2;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005761 my $ms_val = $7;
5762 my $ms_size = $12;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005763
Andy Whitcroft554e1652012-01-10 15:09:57 -08005764 if ($ms_size =~ /^(0x|)0$/i) {
5765 ERROR("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005766 "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 -08005767 } elsif ($ms_size =~ /^(0x|)1$/i) {
5768 WARN("MEMSET",
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005769 "single byte memset is suspicious. Swapped 2nd/3rd argument?\n" . "$here\n$stat\n");
5770 }
5771 }
5772
Joe Perches98a9bba2014-01-23 15:54:52 -08005773# Check for memcpy(foo, bar, ETH_ALEN) that could be ether_addr_copy(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005774# if ($^V && $^V ge 5.10.0 &&
5775# defined $stat &&
5776# $stat =~ /^\+(?:.*?)\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5777# if (WARN("PREFER_ETHER_ADDR_COPY",
5778# "Prefer ether_addr_copy() over memcpy() if the Ethernet addresses are __aligned(2)\n" . "$here\n$stat\n") &&
5779# $fix) {
5780# $fixed[$fixlinenr] =~ s/\bmemcpy\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/ether_addr_copy($2, $7)/;
5781# }
5782# }
Joe Perches98a9bba2014-01-23 15:54:52 -08005783
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005784# Check for memcmp(foo, bar, ETH_ALEN) that could be ether_addr_equal*(foo, bar)
Joe Perchesf3331952016-10-11 13:51:53 -07005785# if ($^V && $^V ge 5.10.0 &&
5786# defined $stat &&
5787# $stat =~ /^\+(?:.*?)\bmemcmp\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5788# WARN("PREFER_ETHER_ADDR_EQUAL",
5789# "Prefer ether_addr_equal() or ether_addr_equal_unaligned() over memcmp()\n" . "$here\n$stat\n")
5790# }
Mateusz Kulikowskib6117d12015-06-25 15:03:13 -07005791
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005792# check for memset(foo, 0x0, ETH_ALEN) that could be eth_zero_addr
5793# check for memset(foo, 0xFF, ETH_ALEN) that could be eth_broadcast_addr
Joe Perchesf3331952016-10-11 13:51:53 -07005794# if ($^V && $^V ge 5.10.0 &&
5795# defined $stat &&
5796# $stat =~ /^\+(?:.*?)\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\,\s*ETH_ALEN\s*\)/) {
5797#
5798# my $ms_val = $7;
5799#
5800# if ($ms_val =~ /^(?:0x|)0+$/i) {
5801# if (WARN("PREFER_ETH_ZERO_ADDR",
5802# "Prefer eth_zero_addr over memset()\n" . "$here\n$stat\n") &&
5803# $fix) {
5804# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_zero_addr($2)/;
5805# }
5806# } elsif ($ms_val =~ /^(?:0xff|255)$/i) {
5807# if (WARN("PREFER_ETH_BROADCAST_ADDR",
5808# "Prefer eth_broadcast_addr() over memset()\n" . "$here\n$stat\n") &&
5809# $fix) {
5810# $fixed[$fixlinenr] =~ s/\bmemset\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*,\s*ETH_ALEN\s*\)/eth_broadcast_addr($2)/;
5811# }
5812# }
5813# }
Mateusz Kulikowski8617cd02015-06-25 15:03:19 -07005814
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005815# typecasts on min/max could be min_t/max_t
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005816 if ($^V && $^V ge 5.10.0 &&
5817 defined $stat &&
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005818 $stat =~ /^\+(?:.*?)\b(min|max)\s*\(\s*$FuncArg\s*,\s*$FuncArg\s*\)/) {
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005819 if (defined $2 || defined $7) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005820 my $call = $1;
5821 my $cast1 = deparenthesize($2);
5822 my $arg1 = $3;
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005823 my $cast2 = deparenthesize($7);
5824 my $arg2 = $8;
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005825 my $cast;
5826
Joe Perchesd1fe9c02012-03-23 15:02:16 -07005827 if ($cast1 ne "" && $cast2 ne "" && $cast1 ne $cast2) {
Joe Perchesd7c76ba2012-01-10 15:09:58 -08005828 $cast = "$cast1 or $cast2";
5829 } elsif ($cast1 ne "") {
5830 $cast = $cast1;
5831 } else {
5832 $cast = $cast2;
5833 }
5834 WARN("MINMAX",
5835 "$call() should probably be ${call}_t($cast, $arg1, $arg2)\n" . "$here\n$stat\n");
Andy Whitcroft554e1652012-01-10 15:09:57 -08005836 }
5837 }
5838
Joe Perches4a273192012-07-30 14:41:20 -07005839# check usleep_range arguments
5840 if ($^V && $^V ge 5.10.0 &&
5841 defined $stat &&
5842 $stat =~ /^\+(?:.*?)\busleep_range\s*\(\s*($FuncArg)\s*,\s*($FuncArg)\s*\)/) {
5843 my $min = $1;
5844 my $max = $7;
5845 if ($min eq $max) {
5846 WARN("USLEEP_RANGE",
5847 "usleep_range should not use min == max args; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5848 } elsif ($min =~ /^\d+$/ && $max =~ /^\d+$/ &&
5849 $min > $max) {
5850 WARN("USLEEP_RANGE",
5851 "usleep_range args reversed, use min then max; see Documentation/timers/timers-howto.txt\n" . "$here\n$stat\n");
5852 }
5853 }
5854
Joe Perches823b7942013-11-12 15:10:15 -08005855# check for naked sscanf
5856 if ($^V && $^V ge 5.10.0 &&
5857 defined $stat &&
Joe Perches6c8bd702014-04-03 14:49:16 -07005858 $line =~ /\bsscanf\b/ &&
Joe Perches823b7942013-11-12 15:10:15 -08005859 ($stat !~ /$Ident\s*=\s*sscanf\s*$balanced_parens/ &&
5860 $stat !~ /\bsscanf\s*$balanced_parens\s*(?:$Compare)/ &&
5861 $stat !~ /(?:$Compare)\s*\bsscanf\s*$balanced_parens/)) {
5862 my $lc = $stat =~ tr@\n@@;
5863 $lc = $lc + $linenr;
5864 my $stat_real = raw_line($linenr, 0);
5865 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5866 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5867 }
5868 WARN("NAKED_SSCANF",
5869 "unchecked sscanf return value\n" . "$here\n$stat_real\n");
5870 }
5871
Joe Perchesafc819a2014-06-04 16:12:08 -07005872# check for simple sscanf that should be kstrto<foo>
5873 if ($^V && $^V ge 5.10.0 &&
5874 defined $stat &&
5875 $line =~ /\bsscanf\b/) {
5876 my $lc = $stat =~ tr@\n@@;
5877 $lc = $lc + $linenr;
5878 my $stat_real = raw_line($linenr, 0);
5879 for (my $count = $linenr + 1; $count <= $lc; $count++) {
5880 $stat_real = $stat_real . "\n" . raw_line($count, 0);
5881 }
5882 if ($stat_real =~ /\bsscanf\b\s*\(\s*$FuncArg\s*,\s*("[^"]+")/) {
5883 my $format = $6;
5884 my $count = $format =~ tr@%@%@;
5885 if ($count == 1 &&
5886 $format =~ /^"\%(?i:ll[udxi]|[udxi]ll|ll|[hl]h?[udxi]|[udxi][hl]h?|[hl]h?|[udxi])"$/) {
5887 WARN("SSCANF_TO_KSTRTO",
5888 "Prefer kstrto<type> to single variable sscanf\n" . "$here\n$stat_real\n");
5889 }
5890 }
5891 }
5892
Joe Perches70dc8a42013-09-11 14:23:58 -07005893# check for new externs in .h files.
5894 if ($realfile =~ /\.h$/ &&
5895 $line =~ /^\+\s*(extern\s+)$Type\s*$Ident\s*\(/s) {
Joe Perchesd1d85782013-09-24 15:27:46 -07005896 if (CHK("AVOID_EXTERNS",
5897 "extern prototypes should be avoided in .h files\n" . $herecurr) &&
Joe Perches70dc8a42013-09-11 14:23:58 -07005898 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07005899 $fixed[$fixlinenr] =~ s/(.*)\bextern\b\s*(.*)/$1$2/;
Joe Perches70dc8a42013-09-11 14:23:58 -07005900 }
5901 }
5902
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005903# check for new externs in .c files.
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005904 if ($realfile =~ /\.c$/ && defined $stat &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005905 $stat =~ /^.\s*(?:extern\s+)?$Type\s+($Ident)(\s*)\(/s)
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005906 {
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005907 my $function_name = $1;
5908 my $paren_space = $2;
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005909
5910 my $s = $stat;
5911 if (defined $cond) {
5912 substr($s, 0, length($cond), '');
5913 }
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07005914 if ($s =~ /^\s*;/ &&
5915 $function_name ne 'uninitialized_var')
5916 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005917 WARN("AVOID_EXTERNS",
5918 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005919 }
5920
5921 if ($paren_space =~ /\n/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005922 WARN("FUNCTION_ARGUMENTS",
5923 "arguments for function declarations should follow identifier\n" . $herecurr);
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07005924 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07005925
5926 } elsif ($realfile =~ /\.c$/ && defined $stat &&
5927 $stat =~ /^.\s*extern\s+/)
5928 {
Joe Perches000d1cc12011-07-25 17:13:25 -07005929 WARN("AVOID_EXTERNS",
5930 "externs should be avoided in .c files\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005931 }
5932
Joe Perchesa0ad7592017-07-10 15:52:19 -07005933# check for function declarations that have arguments without identifier names
5934 if (defined $stat &&
Joe Perchesca0d8922016-10-11 13:52:16 -07005935 $stat =~ /^.\s*(?:extern\s+)?$Type\s*$Ident\s*\(\s*([^{]+)\s*\)\s*;/s &&
5936 $1 ne "void") {
5937 my $args = trim($1);
5938 while ($args =~ m/\s*($Type\s*(?:$Ident|\(\s*\*\s*$Ident?\s*\)\s*$balanced_parens)?)/g) {
5939 my $arg = trim($1);
5940 if ($arg =~ /^$Type$/ && $arg !~ /enum\s+$Ident$/) {
5941 WARN("FUNCTION_ARGUMENTS",
5942 "function definition argument '$arg' should also have an identifier name\n" . $herecurr);
5943 }
5944 }
5945 }
5946
Joe Perchesa0ad7592017-07-10 15:52:19 -07005947# check for function definitions
5948 if ($^V && $^V ge 5.10.0 &&
5949 defined $stat &&
5950 $stat =~ /^.\s*(?:$Storage\s+)?$Type\s*($Ident)\s*$balanced_parens\s*{/s) {
5951 $context_function = $1;
5952
5953# check for multiline function definition with misplaced open brace
5954 my $ok = 0;
5955 my $cnt = statement_rawlines($stat);
5956 my $herectx = $here . "\n";
5957 for (my $n = 0; $n < $cnt; $n++) {
5958 my $rl = raw_line($linenr, $n);
5959 $herectx .= $rl . "\n";
5960 $ok = 1 if ($rl =~ /^[ \+]\{/);
5961 $ok = 1 if ($rl =~ /\{/ && $n == 0);
5962 last if $rl =~ /^[ \+].*\{/;
5963 }
5964 if (!$ok) {
5965 ERROR("OPEN_BRACE",
5966 "open brace '{' following function definitions go on the next line\n" . $herectx);
5967 }
5968 }
5969
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005970# checks for new __setup's
5971 if ($rawline =~ /\b__setup\("([^"]*)"/) {
5972 my $name = $1;
5973
5974 if (!grep(/$name/, @setup_docs)) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005975 CHK("UNDOCUMENTED_SETUP",
Mauro Carvalho Chehab8c27ceff32016-10-18 10:12:27 -02005976 "__setup appears un-documented -- check Documentation/admin-guide/kernel-parameters.rst\n" . $herecurr);
Andy Whitcroftde7d4f02007-07-15 23:37:22 -07005977 }
Andy Whitcroft653d4872007-06-23 17:16:34 -07005978 }
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005979
5980# check for pointless casting of kmalloc return
Joe Perchescaf2a542011-01-12 16:59:56 -08005981 if ($line =~ /\*\s*\)\s*[kv][czm]alloc(_node){0,1}\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07005982 WARN("UNNECESSARY_CASTS",
5983 "unnecessary cast may hide bugs, see http://c-faq.com/malloc/mallocnocast.html\n" . $herecurr);
Andy Whitcroft9c0ca6f2007-10-16 23:29:38 -07005984 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08005985
Joe Perchesa640d252013-07-03 15:05:21 -07005986# alloc style
5987# p = alloc(sizeof(struct foo), ...) should be p = alloc(sizeof(*p), ...)
5988 if ($^V && $^V ge 5.10.0 &&
5989 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*([kv][mz]alloc(?:_node)?)\s*\(\s*(sizeof\s*\(\s*struct\s+$Lval\s*\))/) {
5990 CHK("ALLOC_SIZEOF_STRUCT",
5991 "Prefer $3(sizeof(*$1)...) over $3($4...)\n" . $herecurr);
5992 }
5993
Joe Perches60a55362014-06-04 16:12:07 -07005994# check for k[mz]alloc with multiplies that could be kmalloc_array/kcalloc
5995 if ($^V && $^V ge 5.10.0 &&
Joe Perches1b4a2ed2017-05-08 15:55:57 -07005996 defined $stat &&
5997 $stat =~ /^\+\s*($Lval)\s*\=\s*(?:$balanced_parens)?\s*(k[mz]alloc)\s*\(\s*($FuncArg)\s*\*\s*($FuncArg)\s*,/) {
Joe Perches60a55362014-06-04 16:12:07 -07005998 my $oldfunc = $3;
5999 my $a1 = $4;
6000 my $a2 = $10;
6001 my $newfunc = "kmalloc_array";
6002 $newfunc = "kcalloc" if ($oldfunc eq "kzalloc");
Joe Perchese3674552014-08-06 16:10:55 -07006003 my $r1 = $a1;
6004 my $r2 = $a2;
6005 if ($a1 =~ /^sizeof\s*\S/) {
6006 $r1 = $a2;
6007 $r2 = $a1;
6008 }
6009 if ($r1 !~ /^sizeof\b/ && $r2 =~ /^sizeof\s*\S/ &&
6010 !($r1 =~ /^$Constant$/ || $r1 =~ /^[A-Z_][A-Z0-9_]*$/)) {
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006011 my $ctx = '';
6012 my $herectx = $here . "\n";
6013 my $cnt = statement_rawlines($stat);
6014 for (my $n = 0; $n < $cnt; $n++) {
6015 $herectx .= raw_line($linenr, $n) . "\n";
6016 }
Joe Perches60a55362014-06-04 16:12:07 -07006017 if (WARN("ALLOC_WITH_MULTIPLY",
Joe Perches1b4a2ed2017-05-08 15:55:57 -07006018 "Prefer $newfunc over $oldfunc with multiply\n" . $herectx) &&
6019 $cnt == 1 &&
Joe Perches60a55362014-06-04 16:12:07 -07006020 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006021 $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 -07006022 }
6023 }
6024 }
6025
Joe Perches972fdea2013-04-29 16:18:12 -07006026# check for krealloc arg reuse
6027 if ($^V && $^V ge 5.10.0 &&
6028 $line =~ /\b($Lval)\s*\=\s*(?:$balanced_parens)?\s*krealloc\s*\(\s*\1\s*,/) {
6029 WARN("KREALLOC_ARG_REUSE",
6030 "Reusing the krealloc arg is almost always a bug\n" . $herecurr);
6031 }
6032
Joe Perches5ce59ae2013-02-21 16:44:18 -08006033# check for alloc argument mismatch
6034 if ($line =~ /\b(kcalloc|kmalloc_array)\s*\(\s*sizeof\b/) {
6035 WARN("ALLOC_ARRAY_ARGS",
6036 "$1 uses number as first arg, sizeof is generally wrong\n" . $herecurr);
6037 }
6038
Joe Perchescaf2a542011-01-12 16:59:56 -08006039# check for multiple semicolons
6040 if ($line =~ /;\s*;\s*$/) {
Joe Perchesd5e616f2013-09-11 14:23:54 -07006041 if (WARN("ONE_SEMICOLON",
6042 "Statements terminations use 1 semicolon\n" . $herecurr) &&
6043 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006044 $fixed[$fixlinenr] =~ s/(\s*;\s*){2,}$/;/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006045 }
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006046 }
6047
Tomas Winklercec3aaa2016-08-02 14:04:39 -07006048# check for #defines like: 1 << <digit> that could be BIT(digit), it is not exported to uapi
6049 if ($realfile !~ m@^include/uapi/@ &&
6050 $line =~ /#\s*define\s+\w+\s+\(?\s*1\s*([ulUL]*)\s*\<\<\s*(?:\d+|$Ident)\s*\)?/) {
Joe Perches0ab90192014-12-10 15:51:57 -08006051 my $ull = "";
6052 $ull = "_ULL" if (defined($1) && $1 =~ /ll/i);
6053 if (CHK("BIT_MACRO",
6054 "Prefer using the BIT$ull macro\n" . $herecurr) &&
6055 $fix) {
6056 $fixed[$fixlinenr] =~ s/\(?\s*1\s*[ulUL]*\s*<<\s*(\d+|$Ident)\s*\)?/BIT${ull}($1)/;
6057 }
6058 }
6059
Joe Perches2d632742016-05-20 17:04:00 -07006060# check for #if defined CONFIG_<FOO> || defined CONFIG_<FOO>_MODULE
6061 if ($line =~ /^\+\s*#\s*if\s+defined(?:\s*\(?\s*|\s+)(CONFIG_[A-Z_]+)\s*\)?\s*\|\|\s*defined(?:\s*\(?\s*|\s+)\1_MODULE\s*\)?\s*$/) {
6062 my $config = $1;
6063 if (WARN("PREFER_IS_ENABLED",
6064 "Prefer IS_ENABLED(<FOO>) to CONFIG_<FOO> || CONFIG_<FOO>_MODULE\n" . $herecurr) &&
6065 $fix) {
6066 $fixed[$fixlinenr] = "\+#if IS_ENABLED($config)";
6067 }
6068 }
6069
Joe Perchese81f2392014-08-06 16:11:25 -07006070# check for case / default statements not preceded by break/fallthrough/switch
Joe Perchesc34c09a2014-01-23 15:54:43 -08006071 if ($line =~ /^.\s*(?:case\s+(?:$Ident|$Constant)\s*|default):/) {
6072 my $has_break = 0;
6073 my $has_statement = 0;
6074 my $count = 0;
6075 my $prevline = $linenr;
Joe Perchese81f2392014-08-06 16:11:25 -07006076 while ($prevline > 1 && ($file || $count < 3) && !$has_break) {
Joe Perchesc34c09a2014-01-23 15:54:43 -08006077 $prevline--;
6078 my $rline = $rawlines[$prevline - 1];
6079 my $fline = $lines[$prevline - 1];
6080 last if ($fline =~ /^\@\@/);
6081 next if ($fline =~ /^\-/);
6082 next if ($fline =~ /^.(?:\s*(?:case\s+(?:$Ident|$Constant)[\s$;]*|default):[\s$;]*)*$/);
6083 $has_break = 1 if ($rline =~ /fall[\s_-]*(through|thru)/i);
6084 next if ($fline =~ /^.[\s$;]*$/);
6085 $has_statement = 1;
6086 $count++;
6087 $has_break = 1 if ($fline =~ /\bswitch\b|\b(?:break\s*;[\s$;]*$|return\b|goto\b|continue\b)/);
6088 }
6089 if (!$has_break && $has_statement) {
6090 WARN("MISSING_BREAK",
Andrew Morton224236d2016-12-12 16:46:26 -08006091 "Possible switch case/default not preceded by break or fallthrough comment\n" . $herecurr);
Joe Perchesc34c09a2014-01-23 15:54:43 -08006092 }
6093 }
6094
Joe Perchesd1e2ad02012-12-17 16:02:01 -08006095# check for switch/default statements without a break;
6096 if ($^V && $^V ge 5.10.0 &&
6097 defined $stat &&
6098 $stat =~ /^\+[$;\s]*(?:case[$;\s]+\w+[$;\s]*:[$;\s]*|)*[$;\s]*\bdefault[$;\s]*:[$;\s]*;/g) {
6099 my $ctx = '';
6100 my $herectx = $here . "\n";
6101 my $cnt = statement_rawlines($stat);
6102 for (my $n = 0; $n < $cnt; $n++) {
6103 $herectx .= raw_line($linenr, $n) . "\n";
6104 }
6105 WARN("DEFAULT_NO_BREAK",
6106 "switch default: should use break\n" . $herectx);
Joe Perchescaf2a542011-01-12 16:59:56 -08006107 }
6108
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006109# check for gcc specific __FUNCTION__
Joe Perchesd5e616f2013-09-11 14:23:54 -07006110 if ($line =~ /\b__FUNCTION__\b/) {
6111 if (WARN("USE_FUNC",
6112 "__func__ should be used instead of gcc specific __FUNCTION__\n" . $herecurr) &&
6113 $fix) {
Joe Perches194f66f2014-08-06 16:11:03 -07006114 $fixed[$fixlinenr] =~ s/\b__FUNCTION__\b/__func__/g;
Joe Perchesd5e616f2013-09-11 14:23:54 -07006115 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006116 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006117
Joe Perches62ec8182015-02-13 14:38:18 -08006118# check for uses of __DATE__, __TIME__, __TIMESTAMP__
6119 while ($line =~ /\b(__(?:DATE|TIME|TIMESTAMP)__)\b/g) {
6120 ERROR("DATE_TIME",
6121 "Use of the '$1' macro makes the build non-deterministic\n" . $herecurr);
6122 }
6123
Joe Perches2c924882012-03-23 15:02:20 -07006124# check for use of yield()
6125 if ($line =~ /\byield\s*\(\s*\)/) {
6126 WARN("YIELD",
6127 "Using yield() is generally wrong. See yield() kernel-doc (sched/core.c)\n" . $herecurr);
6128 }
6129
Joe Perches179f8f42013-07-03 15:05:30 -07006130# check for comparisons against true and false
6131 if ($line =~ /\+\s*(.*?)\b(true|false|$Lval)\s*(==|\!=)\s*(true|false|$Lval)\b(.*)$/i) {
6132 my $lead = $1;
6133 my $arg = $2;
6134 my $test = $3;
6135 my $otype = $4;
6136 my $trail = $5;
6137 my $op = "!";
6138
6139 ($arg, $otype) = ($otype, $arg) if ($arg =~ /^(?:true|false)$/i);
6140
6141 my $type = lc($otype);
6142 if ($type =~ /^(?:true|false)$/) {
6143 if (("$test" eq "==" && "$type" eq "true") ||
6144 ("$test" eq "!=" && "$type" eq "false")) {
6145 $op = "";
6146 }
6147
6148 CHK("BOOL_COMPARISON",
6149 "Using comparison to $otype is error prone\n" . $herecurr);
6150
6151## maybe suggesting a correct construct would better
6152## "Using comparison to $otype is error prone. Perhaps use '${lead}${op}${arg}${trail}'\n" . $herecurr);
6153
6154 }
6155 }
6156
Thomas Gleixner4882720b2010-09-07 14:34:01 +00006157# check for semaphores initialized locked
6158 if ($line =~ /^.\s*sema_init.+,\W?0\W?\)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006159 WARN("CONSIDER_COMPLETION",
6160 "consider using a completion\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006161 }
Joe Perches6712d852012-03-23 15:02:20 -07006162
Joe Perches67d0a072011-10-31 17:13:10 -07006163# recommend kstrto* over simple_strto* and strict_strto*
6164 if ($line =~ /\b((simple|strict)_(strto(l|ll|ul|ull)))\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006165 WARN("CONSIDER_KSTRTO",
Joe Perches67d0a072011-10-31 17:13:10 -07006166 "$1 is obsolete, use k$3 instead\n" . $herecurr);
Andy Whitcroft773647a2008-03-28 14:15:58 -07006167 }
Joe Perches6712d852012-03-23 15:02:20 -07006168
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006169# check for __initcall(), use device_initcall() explicitly or more appropriate function please
Michael Ellermanf3db6632008-07-23 21:28:57 -07006170 if ($line =~ /^.\s*__initcall\s*\(/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006171 WARN("USE_DEVICE_INITCALL",
Fabian Frederickae3ccc42014-06-04 16:12:10 -07006172 "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 -07006173 }
Joe Perches6712d852012-03-23 15:02:20 -07006174
Joe Perches0f3c5aa2015-02-13 14:39:05 -08006175# check for various structs that are normally const (ops, kgdb, device_tree)
Joe Perchesd9190e42017-05-08 15:55:45 -07006176# and avoid what seem like struct definitions 'struct foo {'
Andy Whitcroft6903ffb2009-01-15 13:51:07 -08006177 if ($line !~ /\bconst\b/ &&
Joe Perchesd9190e42017-05-08 15:55:45 -07006178 $line =~ /\bstruct\s+($const_structs)\b(?!\s*\{)/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006179 WARN("CONST_STRUCT",
Joe Perchesd9190e42017-05-08 15:55:45 -07006180 "struct $1 should normally be const\n" . $herecurr);
Andy Whitcroft2b6db5c2009-01-06 14:41:29 -08006181 }
Andy Whitcroft773647a2008-03-28 14:15:58 -07006182
6183# use of NR_CPUS is usually wrong
6184# ignore definitions of NR_CPUS and usage to define arrays as likely right
6185 if ($line =~ /\bNR_CPUS\b/ &&
Andy Whitcroftc45dcab2008-06-05 22:46:01 -07006186 $line !~ /^.\s*\s*#\s*if\b.*\bNR_CPUS\b/ &&
6187 $line !~ /^.\s*\s*#\s*define\b.*\bNR_CPUS\b/ &&
Andy Whitcroft171ae1a2008-04-29 00:59:32 -07006188 $line !~ /^.\s*$Declare\s.*\[[^\]]*NR_CPUS[^\]]*\]/ &&
6189 $line !~ /\[[^\]]*\.\.\.[^\]]*NR_CPUS[^\]]*\]/ &&
6190 $line !~ /\[[^\]]*NR_CPUS[^\]]*\.\.\.[^\]]*\]/)
Andy Whitcroft773647a2008-03-28 14:15:58 -07006191 {
Joe Perches000d1cc12011-07-25 17:13:25 -07006192 WARN("NR_CPUS",
6193 "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 -07006194 }
Andy Whitcroft9c9ba342008-04-29 00:59:33 -07006195
Joe Perches52ea8502013-11-12 15:10:09 -08006196# Use of __ARCH_HAS_<FOO> or ARCH_HAVE_<BAR> is wrong.
6197 if ($line =~ /\+\s*#\s*define\s+((?:__)?ARCH_(?:HAS|HAVE)\w*)\b/) {
6198 ERROR("DEFINE_ARCH_HAS",
6199 "#define of '$1' is wrong - use Kconfig variables or standard guards instead\n" . $herecurr);
6200 }
6201
Joe Perchesacd93622015-02-13 14:38:38 -08006202# likely/unlikely comparisons similar to "(likely(foo) > 0)"
6203 if ($^V && $^V ge 5.10.0 &&
6204 $line =~ /\b((?:un)?likely)\s*\(\s*$FuncArg\s*\)\s*$Compare/) {
6205 WARN("LIKELY_MISUSE",
6206 "Using $1 should generally have parentheses around the comparison\n" . $herecurr);
6207 }
6208
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006209# whine mightly about in_atomic
6210 if ($line =~ /\bin_atomic\s*\(/) {
6211 if ($realfile =~ m@^drivers/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006212 ERROR("IN_ATOMIC",
6213 "do not use in_atomic in drivers\n" . $herecurr);
Andy Whitcroftf4a87732009-02-27 14:03:05 -08006214 } elsif ($realfile !~ m@^kernel/@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006215 WARN("IN_ATOMIC",
6216 "use of in_atomic() is incorrect outside core kernel code\n" . $herecurr);
Andy Whitcroft691d77b2009-01-06 14:41:16 -08006217 }
6218 }
Peter Zijlstra1704f472010-03-19 01:37:42 +01006219
Joe Perches481aea52016-05-20 17:04:08 -07006220# whine about ACCESS_ONCE
6221 if ($^V && $^V ge 5.10.0 &&
6222 $line =~ /\bACCESS_ONCE\s*$balanced_parens\s*(=(?!=))?\s*($FuncArg)?/) {
6223 my $par = $1;
6224 my $eq = $2;
6225 my $fun = $3;
6226 $par =~ s/^\(\s*(.*)\s*\)$/$1/;
6227 if (defined($eq)) {
6228 if (WARN("PREFER_WRITE_ONCE",
6229 "Prefer WRITE_ONCE(<FOO>, <BAR>) over ACCESS_ONCE(<FOO>) = <BAR>\n" . $herecurr) &&
6230 $fix) {
6231 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)\s*$eq\s*\Q$fun\E/WRITE_ONCE($par, $fun)/;
6232 }
6233 } else {
6234 if (WARN("PREFER_READ_ONCE",
6235 "Prefer READ_ONCE(<FOO>) over ACCESS_ONCE(<FOO>)\n" . $herecurr) &&
6236 $fix) {
6237 $fixed[$fixlinenr] =~ s/\bACCESS_ONCE\s*\(\s*\Q$par\E\s*\)/READ_ONCE($par)/;
6238 }
6239 }
6240 }
6241
Peter Zijlstra0f5225b2016-10-07 17:43:51 +02006242# check for mutex_trylock_recursive usage
6243 if ($line =~ /mutex_trylock_recursive/) {
6244 ERROR("LOCKING",
6245 "recursive locking is bad, do not use this ever.\n" . $herecurr);
6246 }
6247
Peter Zijlstra1704f472010-03-19 01:37:42 +01006248# check for lockdep_set_novalidate_class
6249 if ($line =~ /^.\s*lockdep_set_novalidate_class\s*\(/ ||
6250 $line =~ /__lockdep_no_validate__\s*\)/ ) {
6251 if ($realfile !~ m@^kernel/lockdep@ &&
6252 $realfile !~ m@^include/linux/lockdep@ &&
6253 $realfile !~ m@^drivers/base/core@) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006254 ERROR("LOCKDEP",
6255 "lockdep_no_validate class is reserved for device->mutex.\n" . $herecurr);
Peter Zijlstra1704f472010-03-19 01:37:42 +01006256 }
6257 }
Dave Jones88f88312011-01-12 16:59:59 -08006258
Joe Perchesb392c642015-04-16 12:44:16 -07006259 if ($line =~ /debugfs_create_\w+.*\b$mode_perms_world_writable\b/ ||
6260 $line =~ /DEVICE_ATTR.*\b$mode_perms_world_writable\b/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006261 WARN("EXPORTED_WORLD_WRITABLE",
6262 "Exporting world writable files is usually an error. Consider more restrictive permissions.\n" . $herecurr);
Dave Jones88f88312011-01-12 16:59:59 -08006263 }
Joe Perches24358802014-04-03 14:49:13 -07006264
Joe Perches515a2352014-04-03 14:49:24 -07006265# Mode permission misuses where it seems decimal should be octal
6266# This uses a shortcut match to avoid unnecessary uses of a slow foreach loop
6267 if ($^V && $^V ge 5.10.0 &&
Joe Perches459cf0a2016-10-11 13:52:19 -07006268 defined $stat &&
Joe Perches515a2352014-04-03 14:49:24 -07006269 $line =~ /$mode_perms_search/) {
6270 foreach my $entry (@mode_permission_funcs) {
6271 my $func = $entry->[0];
6272 my $arg_pos = $entry->[1];
Joe Perches24358802014-04-03 14:49:13 -07006273
Joe Perches459cf0a2016-10-11 13:52:19 -07006274 my $lc = $stat =~ tr@\n@@;
6275 $lc = $lc + $linenr;
6276 my $stat_real = raw_line($linenr, 0);
6277 for (my $count = $linenr + 1; $count <= $lc; $count++) {
6278 $stat_real = $stat_real . "\n" . raw_line($count, 0);
6279 }
6280
Joe Perches515a2352014-04-03 14:49:24 -07006281 my $skip_args = "";
6282 if ($arg_pos > 1) {
6283 $arg_pos--;
6284 $skip_args = "(?:\\s*$FuncArg\\s*,\\s*){$arg_pos,$arg_pos}";
6285 }
Joe Perchesf90774e2016-10-11 13:51:47 -07006286 my $test = "\\b$func\\s*\\(${skip_args}($FuncArg(?:\\|\\s*$FuncArg)*)\\s*[,\\)]";
Joe Perches459cf0a2016-10-11 13:52:19 -07006287 if ($stat =~ /$test/) {
Joe Perches515a2352014-04-03 14:49:24 -07006288 my $val = $1;
6289 $val = $6 if ($skip_args ne "");
Joe Perchesf90774e2016-10-11 13:51:47 -07006290 if (($val =~ /^$Int$/ && $val !~ /^$Octal$/) ||
6291 ($val =~ /^$Octal$/ && length($val) ne 4)) {
Joe Perches515a2352014-04-03 14:49:24 -07006292 ERROR("NON_OCTAL_PERMISSIONS",
Joe Perches459cf0a2016-10-11 13:52:19 -07006293 "Use 4 digit octal (0777) not decimal permissions\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006294 }
6295 if ($val =~ /^$Octal$/ && (oct($val) & 02)) {
Joe Perchesc0a5c892015-02-13 14:38:21 -08006296 ERROR("EXPORTED_WORLD_WRITABLE",
Joe Perches459cf0a2016-10-11 13:52:19 -07006297 "Exporting writable files is usually an error. Consider more restrictive permissions.\n" . "$here\n" . $stat_real);
Joe Perchesf90774e2016-10-11 13:51:47 -07006298 }
Joe Perches24358802014-04-03 14:49:13 -07006299 }
6300 }
6301 }
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006302
Joe Perches459cf0a2016-10-11 13:52:19 -07006303# check for uses of S_<PERMS> that could be octal for readability
6304 if ($line =~ /\b$mode_perms_string_search\b/) {
6305 my $val = "";
6306 my $oval = "";
6307 my $to = 0;
6308 my $curpos = 0;
6309 my $lastpos = 0;
6310 while ($line =~ /\b(($mode_perms_string_search)\b(?:\s*\|\s*)?\s*)/g) {
6311 $curpos = pos($line);
6312 my $match = $2;
6313 my $omatch = $1;
6314 last if ($lastpos > 0 && ($curpos - length($omatch) != $lastpos));
6315 $lastpos = $curpos;
6316 $to |= $mode_permission_string_types{$match};
6317 $val .= '\s*\|\s*' if ($val ne "");
6318 $val .= $match;
6319 $oval .= $omatch;
6320 }
6321 $oval =~ s/^\s*\|\s*//;
6322 $oval =~ s/\s*\|\s*$//;
6323 my $octal = sprintf("%04o", $to);
6324 if (WARN("SYMBOLIC_PERMS",
6325 "Symbolic permissions '$oval' are not preferred. Consider using octal permissions '$octal'.\n" . $herecurr) &&
6326 $fix) {
6327 $fixed[$fixlinenr] =~ s/$val/$octal/;
6328 }
6329 }
6330
Bjorn Andersson5a6d20c2015-06-25 15:03:24 -07006331# validate content of MODULE_LICENSE against list from include/linux/module.h
6332 if ($line =~ /\bMODULE_LICENSE\s*\(\s*($String)\s*\)/) {
6333 my $extracted_string = get_quoted_string($line, $rawline);
6334 my $valid_licenses = qr{
6335 GPL|
6336 GPL\ v2|
6337 GPL\ and\ additional\ rights|
6338 Dual\ BSD/GPL|
6339 Dual\ MIT/GPL|
6340 Dual\ MPL/GPL|
6341 Proprietary
6342 }x;
6343 if ($extracted_string !~ /^"(?:$valid_licenses)"$/x) {
6344 WARN("MODULE_LICENSE",
6345 "unknown module license " . $extracted_string . "\n" . $herecurr);
6346 }
6347 }
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006348 }
6349
6350 # If we have no input at all, then there is nothing to report on
6351 # so just keep quiet.
6352 if ($#rawlines == -1) {
6353 exit(0);
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006354 }
6355
Andy Whitcroft8905a672007-11-28 16:21:06 -08006356 # In mailback mode only produce a report in the negative, for
6357 # things that appear to be patches.
6358 if ($mailback && ($clean == 1 || !$is_patch)) {
6359 exit(0);
6360 }
6361
6362 # This is not a patch, and we are are in 'no-patch' mode so
6363 # just keep quiet.
6364 if (!$chk_patch && !$is_patch) {
6365 exit(0);
6366 }
6367
Joe Perches06330fc2015-06-25 15:03:11 -07006368 if (!$is_patch && $file !~ /cover-letter\.patch$/) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006369 ERROR("NOT_UNIFIED_DIFF",
6370 "Does not appear to be a unified-diff format patch\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006371 }
Allen Hubbeed43c4e2016-08-02 14:04:45 -07006372 if ($is_patch && $has_commit_log && $chk_signoff && $signoff == 0) {
Joe Perches000d1cc12011-07-25 17:13:25 -07006373 ERROR("MISSING_SIGN_OFF",
6374 "Missing Signed-off-by: line(s)\n");
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006375 }
6376
Andy Whitcroft8905a672007-11-28 16:21:06 -08006377 print report_dump();
Andy Whitcroft13214ad2008-02-08 04:22:03 -08006378 if ($summary && !($clean == 1 && $quiet == 1)) {
6379 print "$filename " if ($summary_file);
Andy Whitcroft8905a672007-11-28 16:21:06 -08006380 print "total: $cnt_error errors, $cnt_warn warnings, " .
6381 (($check)? "$cnt_chk checks, " : "") .
6382 "$cnt_lines lines checked\n";
Andy Whitcroftf0a594c2007-07-19 01:48:34 -07006383 }
Andy Whitcroft8905a672007-11-28 16:21:06 -08006384
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006385 if ($quiet == 0) {
Joe Perchesef212192016-05-20 17:04:11 -07006386 # If there were any defects found and not already fixing them
6387 if (!$clean and !$fix) {
6388 print << "EOM"
6389
6390NOTE: For some of the reported defects, checkpatch may be able to
6391 mechanically convert to the typical style using --fix or --fix-inplace.
6392EOM
6393 }
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006394 # If there were whitespace errors which cleanpatch can fix
6395 # then suggest that.
6396 if ($rpt_cleaners) {
Mike Frysingerb0781212011-03-22 16:34:43 -07006397 $rpt_cleaners = 0;
Joe Perchesd8469f12015-06-25 15:03:00 -07006398 print << "EOM"
6399
6400NOTE: Whitespace errors detected.
6401 You may wish to use scripts/cleanpatch or scripts/cleanfile
6402EOM
Andy Whitcroftd2c0a232010-10-26 14:23:12 -07006403 }
6404 }
6405
Joe Perchesd752fcc2014-08-06 16:11:05 -07006406 if ($clean == 0 && $fix &&
6407 ("@rawlines" ne "@fixed" ||
6408 $#fixed_inserted >= 0 || $#fixed_deleted >= 0)) {
Joe Perches9624b8d2014-01-23 15:54:44 -08006409 my $newfile = $filename;
6410 $newfile .= ".EXPERIMENTAL-checkpatch-fixes" if (!$fix_inplace);
Joe Perches3705ce52013-07-03 15:05:31 -07006411 my $linecount = 0;
6412 my $f;
6413
Joe Perchesd752fcc2014-08-06 16:11:05 -07006414 @fixed = fix_inserted_deleted_lines(\@fixed, \@fixed_inserted, \@fixed_deleted);
6415
Joe Perches3705ce52013-07-03 15:05:31 -07006416 open($f, '>', $newfile)
6417 or die "$P: Can't open $newfile for write\n";
6418 foreach my $fixed_line (@fixed) {
6419 $linecount++;
6420 if ($file) {
6421 if ($linecount > 3) {
6422 $fixed_line =~ s/^\+//;
Joe Perchesd752fcc2014-08-06 16:11:05 -07006423 print $f $fixed_line . "\n";
Joe Perches3705ce52013-07-03 15:05:31 -07006424 }
6425 } else {
6426 print $f $fixed_line . "\n";
6427 }
6428 }
6429 close($f);
6430
6431 if (!$quiet) {
6432 print << "EOM";
Joe Perchesd8469f12015-06-25 15:03:00 -07006433
Joe Perches3705ce52013-07-03 15:05:31 -07006434Wrote EXPERIMENTAL --fix correction(s) to '$newfile'
6435
6436Do _NOT_ trust the results written to this file.
6437Do _NOT_ submit these changes without inspecting them for correctness.
6438
6439This EXPERIMENTAL file is simply a convenience to help rewrite patches.
6440No warranties, expressed or implied...
Joe Perches3705ce52013-07-03 15:05:31 -07006441EOM
6442 }
6443 }
6444
Joe Perchesd8469f12015-06-25 15:03:00 -07006445 if ($quiet == 0) {
6446 print "\n";
6447 if ($clean == 1) {
6448 print "$vname has no obvious style problems and is ready for submission.\n";
6449 } else {
6450 print "$vname has style problems, please review.\n";
6451 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006452 }
Andy Whitcroft0a920b52007-06-01 00:46:48 -07006453 return $clean;
6454}